19454b2d8SWarner Losh /*-
251369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni *
4df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1989, 1991, 1993
5f344fb0bSWarner Losh * The Regents of the University of California. All Rights Reserved.
6f344fb0bSWarner Losh * Copyright (c) 2004-2009 Robert N. M. Watson All Rights Reserved.
7c0874c34SMatt Macy * Copyright (c) 2018 Matthew Macy
85716d902SGleb Smirnoff * Copyright (c) 2022 Gleb Smirnoff <glebius@FreeBSD.org>
9df8bae1dSRodney W. Grimes *
10df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without
11df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions
12df8bae1dSRodney W. Grimes * are met:
13df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright
14df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer.
15df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright
16df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the
17df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution.
1869a28758SEd Maste * 3. Neither the name of the University nor the names of its contributors
19df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software
20df8bae1dSRodney W. Grimes * without specific prior written permission.
21df8bae1dSRodney W. Grimes *
22df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32df8bae1dSRodney W. Grimes * SUCH DAMAGE.
33df8bae1dSRodney W. Grimes */
34df8bae1dSRodney W. Grimes
35f23929fbSRobert Watson /*
36f23929fbSRobert Watson * UNIX Domain (Local) Sockets
37f23929fbSRobert Watson *
38f23929fbSRobert Watson * This is an implementation of UNIX (local) domain sockets. Each socket has
39f23929fbSRobert Watson * an associated struct unpcb (UNIX protocol control block). Stream sockets
40f23929fbSRobert Watson * may be connected to 0 or 1 other socket. Datagram sockets may be
41f23929fbSRobert Watson * connected to 0, 1, or many other sockets. Sockets may be created and
42f23929fbSRobert Watson * connected in pairs (socketpair(2)), or bound/connected to using the file
43f23929fbSRobert Watson * system name space. For most purposes, only the receive socket buffer is
44f23929fbSRobert Watson * used, as sending on one socket delivers directly to the receive socket
455b950deaSRobert Watson * buffer of a second socket.
465b950deaSRobert Watson *
475b950deaSRobert Watson * The implementation is substantially complicated by the fact that
485b950deaSRobert Watson * "ancillary data", such as file descriptors or credentials, may be passed
495b950deaSRobert Watson * across UNIX domain sockets. The potential for passing UNIX domain sockets
505b950deaSRobert Watson * over other UNIX domain sockets requires the implementation of a simple
515b950deaSRobert Watson * garbage collector to find and tear down cycles of disconnected sockets.
52aea52f1bSRobert Watson *
53aea52f1bSRobert Watson * TODO:
5484d61770SRobert Watson * RDM
55aea52f1bSRobert Watson * rethink name space problems
56aea52f1bSRobert Watson * need a proper out-of-band
57f23929fbSRobert Watson */
58f23929fbSRobert Watson
59677b542eSDavid E. O'Brien #include <sys/cdefs.h>
6003c96c31SRobert Watson #include "opt_ddb.h"
61335654d7SRobert Watson
62df8bae1dSRodney W. Grimes #include <sys/param.h>
634a144410SRobert Watson #include <sys/capsicum.h>
64fb919e4dSMark Murray #include <sys/domain.h>
654f590175SPaul Saab #include <sys/eventhandler.h>
66d5cbccecSMark Johnston #include <sys/fcntl.h>
67639acc13SGarrett Wollman #include <sys/file.h>
68960ed29cSSeigo Tanimura #include <sys/filedesc.h>
69960ed29cSSeigo Tanimura #include <sys/kernel.h>
70960ed29cSSeigo Tanimura #include <sys/lock.h>
71d5cbccecSMark Johnston #include <sys/malloc.h>
72639acc13SGarrett Wollman #include <sys/mbuf.h>
73033eb86eSJeff Roberson #include <sys/mount.h>
74960ed29cSSeigo Tanimura #include <sys/mutex.h>
75639acc13SGarrett Wollman #include <sys/namei.h>
76639acc13SGarrett Wollman #include <sys/proc.h>
77df8bae1dSRodney W. Grimes #include <sys/protosw.h>
780cb64678SKonstantin Belousov #include <sys/queue.h>
79960ed29cSSeigo Tanimura #include <sys/resourcevar.h>
80e7c33e29SRobert Watson #include <sys/rwlock.h>
81df8bae1dSRodney W. Grimes #include <sys/socket.h>
82df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
83960ed29cSSeigo Tanimura #include <sys/signalvar.h>
84df8bae1dSRodney W. Grimes #include <sys/stat.h>
85960ed29cSSeigo Tanimura #include <sys/sx.h>
86639acc13SGarrett Wollman #include <sys/sysctl.h>
87960ed29cSSeigo Tanimura #include <sys/systm.h>
88a0ec558aSRobert Watson #include <sys/taskqueue.h>
89639acc13SGarrett Wollman #include <sys/un.h>
9098271db4SGarrett Wollman #include <sys/unpcb.h>
91639acc13SGarrett Wollman #include <sys/vnode.h>
92530c0060SRobert Watson
93530c0060SRobert Watson #include <net/vnet.h>
94df8bae1dSRodney W. Grimes
9503c96c31SRobert Watson #ifdef DDB
9603c96c31SRobert Watson #include <ddb/ddb.h>
9703c96c31SRobert Watson #endif
9803c96c31SRobert Watson
99aed55708SRobert Watson #include <security/mac/mac_framework.h>
100aed55708SRobert Watson
1019e9d298aSJeff Roberson #include <vm/uma.h>
10298271db4SGarrett Wollman
1038cb539f1SPawel Jakub Dawidek MALLOC_DECLARE(M_FILECAPS);
1048cb539f1SPawel Jakub Dawidek
105e7d02be1SGleb Smirnoff static struct domain localdomain;
1063dab55bcSRobert Watson
1079e9d298aSJeff Roberson static uma_zone_t unp_zone;
1083dab55bcSRobert Watson static unp_gen_t unp_gencnt; /* (l) */
1093dab55bcSRobert Watson static u_int unp_count; /* (l) Count of local sockets. */
110aea52f1bSRobert Watson static ino_t unp_ino; /* Prototype for fake inode numbers. */
1113dab55bcSRobert Watson static int unp_rights; /* (g) File descriptors in flight. */
1123dab55bcSRobert Watson static struct unp_head unp_shead; /* (l) List of stream sockets. */
1133dab55bcSRobert Watson static struct unp_head unp_dhead; /* (l) List of datagram sockets. */
11484d61770SRobert Watson static struct unp_head unp_sphead; /* (l) List of seqpacket sockets. */
115c08e016fSJohn Baldwin static struct mtx_pool *unp_vp_mtxpool;
11698271db4SGarrett Wollman
1170cb64678SKonstantin Belousov struct unp_defer {
1180cb64678SKonstantin Belousov SLIST_ENTRY(unp_defer) ud_link;
1190cb64678SKonstantin Belousov struct file *ud_fp;
1200cb64678SKonstantin Belousov };
1210cb64678SKonstantin Belousov static SLIST_HEAD(, unp_defer) unp_defers;
1220cb64678SKonstantin Belousov static int unp_defers_count;
1230cb64678SKonstantin Belousov
124cfb1e929SGleb Smirnoff static const struct sockaddr sun_noname = {
125cfb1e929SGleb Smirnoff .sa_len = sizeof(sun_noname),
126cfb1e929SGleb Smirnoff .sa_family = AF_LOCAL,
127cfb1e929SGleb Smirnoff };
12898271db4SGarrett Wollman
129df8bae1dSRodney W. Grimes /*
130aea52f1bSRobert Watson * Garbage collection of cyclic file descriptor/socket references occurs
131aea52f1bSRobert Watson * asynchronously in a taskqueue context in order to avoid recursion and
132aea52f1bSRobert Watson * reentrance in the UNIX domain socket, file descriptor, and socket layer
133aea52f1bSRobert Watson * code. See unp_gc() for a full description.
134df8bae1dSRodney W. Grimes */
135daee0f0bSKonstantin Belousov static struct timeout_task unp_gc_task;
136f708ef1bSPoul-Henning Kamp
137ce5f32deSRobert Watson /*
1380cb64678SKonstantin Belousov * The close of unix domain sockets attached as SCM_RIGHTS is
1390cb64678SKonstantin Belousov * postponed to the taskqueue, to avoid arbitrary recursion depth.
1400cb64678SKonstantin Belousov * The attached sockets might have another sockets attached.
1410cb64678SKonstantin Belousov */
1420cb64678SKonstantin Belousov static struct task unp_defer_task;
1430cb64678SKonstantin Belousov
1440cb64678SKonstantin Belousov /*
1455716d902SGleb Smirnoff * Both send and receive buffers are allocated PIPSIZ bytes of buffering for
1465716d902SGleb Smirnoff * stream sockets, although the total for sender and receiver is actually
1475716d902SGleb Smirnoff * only PIPSIZ.
1487e711c3aSRobert Watson *
1495716d902SGleb Smirnoff * Datagram sockets really use the sendspace as the maximum datagram size,
1507e711c3aSRobert Watson * and don't really want to reserve the sendspace. Their recvspace should be
1517e711c3aSRobert Watson * large enough for at least one max-size datagram plus address.
1527e711c3aSRobert Watson */
1537e711c3aSRobert Watson #ifndef PIPSIZ
1547e711c3aSRobert Watson #define PIPSIZ 8192
1557e711c3aSRobert Watson #endif
1567e711c3aSRobert Watson static u_long unpst_sendspace = PIPSIZ;
1577e711c3aSRobert Watson static u_long unpst_recvspace = PIPSIZ;
158be7c095aSGleb Smirnoff static u_long unpdg_maxdgram = 8*1024; /* support 8KB syslog msgs */
159be7c095aSGleb Smirnoff static u_long unpdg_recvspace = 16*1024;
1605716d902SGleb Smirnoff static u_long unpsp_sendspace = PIPSIZ; /* really max datagram size */
16184d61770SRobert Watson static u_long unpsp_recvspace = PIPSIZ;
1627e711c3aSRobert Watson
1637029da5cSPawel Biernacki static SYSCTL_NODE(_net, PF_LOCAL, local, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1647029da5cSPawel Biernacki "Local domain");
1657029da5cSPawel Biernacki static SYSCTL_NODE(_net_local, SOCK_STREAM, stream,
1667029da5cSPawel Biernacki CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1676472ac3dSEd Schouten "SOCK_STREAM");
1687029da5cSPawel Biernacki static SYSCTL_NODE(_net_local, SOCK_DGRAM, dgram,
1697029da5cSPawel Biernacki CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1707029da5cSPawel Biernacki "SOCK_DGRAM");
1717029da5cSPawel Biernacki static SYSCTL_NODE(_net_local, SOCK_SEQPACKET, seqpacket,
1727029da5cSPawel Biernacki CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
17384d61770SRobert Watson "SOCK_SEQPACKET");
174e4445a03SRobert Watson
1757e711c3aSRobert Watson SYSCTL_ULONG(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW,
176be6b1304STom Rhodes &unpst_sendspace, 0, "Default stream send space.");
1777e711c3aSRobert Watson SYSCTL_ULONG(_net_local_stream, OID_AUTO, recvspace, CTLFLAG_RW,
178be6b1304STom Rhodes &unpst_recvspace, 0, "Default stream receive space.");
1797e711c3aSRobert Watson SYSCTL_ULONG(_net_local_dgram, OID_AUTO, maxdgram, CTLFLAG_RW,
1802573e6ceSGleb Smirnoff &unpdg_maxdgram, 0, "Maximum datagram size.");
1817e711c3aSRobert Watson SYSCTL_ULONG(_net_local_dgram, OID_AUTO, recvspace, CTLFLAG_RW,
182be6b1304STom Rhodes &unpdg_recvspace, 0, "Default datagram receive space.");
18384d61770SRobert Watson SYSCTL_ULONG(_net_local_seqpacket, OID_AUTO, maxseqpacket, CTLFLAG_RW,
18484d61770SRobert Watson &unpsp_sendspace, 0, "Default seqpacket send space.");
18584d61770SRobert Watson SYSCTL_ULONG(_net_local_seqpacket, OID_AUTO, recvspace, CTLFLAG_RW,
18684d61770SRobert Watson &unpsp_recvspace, 0, "Default seqpacket receive space.");
187be6b1304STom Rhodes SYSCTL_INT(_net_local, OID_AUTO, inflight, CTLFLAG_RD, &unp_rights, 0,
188be6b1304STom Rhodes "File descriptors in flight.");
1890cb64678SKonstantin Belousov SYSCTL_INT(_net_local, OID_AUTO, deferred, CTLFLAG_RD,
1900cb64678SKonstantin Belousov &unp_defers_count, 0,
1910cb64678SKonstantin Belousov "File descriptors deferred to taskqueue for close.");
1927e711c3aSRobert Watson
193175389cfSEdward Tomasz Napierala /*
194e7c33e29SRobert Watson * Locking and synchronization:
195ce5f32deSRobert Watson *
196d5cbccecSMark Johnston * Several types of locks exist in the local domain socket implementation:
197d5cbccecSMark Johnston * - a global linkage lock
198d5cbccecSMark Johnston * - a global connection list lock
199d5cbccecSMark Johnston * - the mtxpool lock
200d5cbccecSMark Johnston * - per-unpcb mutexes
201d5cbccecSMark Johnston *
202d5cbccecSMark Johnston * The linkage lock protects the global socket lists, the generation number
203d5cbccecSMark Johnston * counter and garbage collector state.
204d5cbccecSMark Johnston *
205d5cbccecSMark Johnston * The connection list lock protects the list of referring sockets in a datagram
206d5cbccecSMark Johnston * socket PCB. This lock is also overloaded to protect a global list of
207d5cbccecSMark Johnston * sockets whose buffers contain socket references in the form of SCM_RIGHTS
208d5cbccecSMark Johnston * messages. To avoid recursion, such references are released by a dedicated
209d5cbccecSMark Johnston * thread.
21075a67bf3SMatt Macy *
21175a67bf3SMatt Macy * The mtxpool lock protects the vnode from being modified while referenced.
212d5cbccecSMark Johnston * Lock ordering rules require that it be acquired before any PCB locks.
21375a67bf3SMatt Macy *
214d5cbccecSMark Johnston * The unpcb lock (unp_mtx) protects the most commonly referenced fields in the
215d5cbccecSMark Johnston * unpcb. This includes the unp_conn field, which either links two connected
216d5cbccecSMark Johnston * PCBs together (for connected socket types) or points at the destination
217d5cbccecSMark Johnston * socket (for connectionless socket types). The operations of creating or
218d5cbccecSMark Johnston * destroying a connection therefore involve locking multiple PCBs. To avoid
219d5cbccecSMark Johnston * lock order reversals, in some cases this involves dropping a PCB lock and
220d5cbccecSMark Johnston * using a reference counter to maintain liveness.
221ce5f32deSRobert Watson *
222e7c33e29SRobert Watson * UNIX domain sockets each have an unpcb hung off of their so_pcb pointer,
223e7d02be1SGleb Smirnoff * allocated in pr_attach() and freed in pr_detach(). The validity of that
224e7c33e29SRobert Watson * pointer is an invariant, so no lock is required to dereference the so_pcb
225e7c33e29SRobert Watson * pointer if a valid socket reference is held by the caller. In practice,
226e7c33e29SRobert Watson * this is always true during operations performed on a socket. Each unpcb
227e7c33e29SRobert Watson * has a back-pointer to its socket, unp_socket, which will be stable under
228e7c33e29SRobert Watson * the same circumstances.
229e7c33e29SRobert Watson *
230e7c33e29SRobert Watson * This pointer may only be safely dereferenced as long as a valid reference
231e7c33e29SRobert Watson * to the unpcb is held. Typically, this reference will be from the socket,
232e7c33e29SRobert Watson * or from another unpcb when the referring unpcb's lock is held (in order
233e7c33e29SRobert Watson * that the reference not be invalidated during use). For example, to follow
23475a67bf3SMatt Macy * unp->unp_conn->unp_socket, you need to hold a lock on unp_conn to guarantee
23575a67bf3SMatt Macy * that detach is not run clearing unp_socket.
236e7c33e29SRobert Watson *
237e7c33e29SRobert Watson * Blocking with UNIX domain sockets is a tricky issue: unlike most network
238e7c33e29SRobert Watson * protocols, bind() is a non-atomic operation, and connect() requires
239e7c33e29SRobert Watson * potential sleeping in the protocol, due to potentially waiting on local or
240e7c33e29SRobert Watson * distributed file systems. We try to separate "lookup" operations, which
241e7c33e29SRobert Watson * may sleep, and the IPC operations themselves, which typically can occur
242e7c33e29SRobert Watson * with relative atomicity as locks can be held over the entire operation.
243e7c33e29SRobert Watson *
244e7c33e29SRobert Watson * Another tricky issue is simultaneous multi-threaded or multi-process
245e7c33e29SRobert Watson * access to a single UNIX domain socket. These are handled by the flags
246e7c33e29SRobert Watson * UNP_CONNECTING and UNP_BINDING, which prevent concurrent connecting or
247e7c33e29SRobert Watson * binding, both of which involve dropping UNIX domain socket locks in order
248e7c33e29SRobert Watson * to perform namei() and other file system operations.
249ce5f32deSRobert Watson */
2503dab55bcSRobert Watson static struct rwlock unp_link_rwlock;
2510cb64678SKonstantin Belousov static struct mtx unp_defers_lock;
252e7c33e29SRobert Watson
2533dab55bcSRobert Watson #define UNP_LINK_LOCK_INIT() rw_init(&unp_link_rwlock, \
2543dab55bcSRobert Watson "unp_link_rwlock")
255e7c33e29SRobert Watson
2563dab55bcSRobert Watson #define UNP_LINK_LOCK_ASSERT() rw_assert(&unp_link_rwlock, \
257e7c33e29SRobert Watson RA_LOCKED)
2583dab55bcSRobert Watson #define UNP_LINK_UNLOCK_ASSERT() rw_assert(&unp_link_rwlock, \
259e7c33e29SRobert Watson RA_UNLOCKED)
260e7c33e29SRobert Watson
2613dab55bcSRobert Watson #define UNP_LINK_RLOCK() rw_rlock(&unp_link_rwlock)
2623dab55bcSRobert Watson #define UNP_LINK_RUNLOCK() rw_runlock(&unp_link_rwlock)
2633dab55bcSRobert Watson #define UNP_LINK_WLOCK() rw_wlock(&unp_link_rwlock)
2643dab55bcSRobert Watson #define UNP_LINK_WUNLOCK() rw_wunlock(&unp_link_rwlock)
2653dab55bcSRobert Watson #define UNP_LINK_WLOCK_ASSERT() rw_assert(&unp_link_rwlock, \
266e7c33e29SRobert Watson RA_WLOCKED)
267779f106aSGleb Smirnoff #define UNP_LINK_WOWNED() rw_wowned(&unp_link_rwlock)
268e7c33e29SRobert Watson
2690cb64678SKonstantin Belousov #define UNP_DEFERRED_LOCK_INIT() mtx_init(&unp_defers_lock, \
2700cb64678SKonstantin Belousov "unp_defer", NULL, MTX_DEF)
2710cb64678SKonstantin Belousov #define UNP_DEFERRED_LOCK() mtx_lock(&unp_defers_lock)
2720cb64678SKonstantin Belousov #define UNP_DEFERRED_UNLOCK() mtx_unlock(&unp_defers_lock)
2730cb64678SKonstantin Belousov
27475a67bf3SMatt Macy #define UNP_REF_LIST_LOCK() UNP_DEFERRED_LOCK();
27575a67bf3SMatt Macy #define UNP_REF_LIST_UNLOCK() UNP_DEFERRED_UNLOCK();
27675a67bf3SMatt Macy
277e7c33e29SRobert Watson #define UNP_PCB_LOCK_INIT(unp) mtx_init(&(unp)->unp_mtx, \
278d9525340SMatt Macy "unp", "unp", \
27975a67bf3SMatt Macy MTX_DUPOK|MTX_DEF)
280e7c33e29SRobert Watson #define UNP_PCB_LOCK_DESTROY(unp) mtx_destroy(&(unp)->unp_mtx)
281ccdadf1aSMark Johnston #define UNP_PCB_LOCKPTR(unp) (&(unp)->unp_mtx)
282e7c33e29SRobert Watson #define UNP_PCB_LOCK(unp) mtx_lock(&(unp)->unp_mtx)
28375a67bf3SMatt Macy #define UNP_PCB_TRYLOCK(unp) mtx_trylock(&(unp)->unp_mtx)
284e7c33e29SRobert Watson #define UNP_PCB_UNLOCK(unp) mtx_unlock(&(unp)->unp_mtx)
28575a67bf3SMatt Macy #define UNP_PCB_OWNED(unp) mtx_owned(&(unp)->unp_mtx)
286e7c33e29SRobert Watson #define UNP_PCB_LOCK_ASSERT(unp) mtx_assert(&(unp)->unp_mtx, MA_OWNED)
28775a67bf3SMatt Macy #define UNP_PCB_UNLOCK_ASSERT(unp) mtx_assert(&(unp)->unp_mtx, MA_NOTOWNED)
2880d9ce3a1SRobert Watson
2892c899584SRobert Watson static int uipc_connect2(struct socket *, struct socket *);
2900b36cd25SRobert Watson static int uipc_ctloutput(struct socket *, struct sockopt *);
291aea52f1bSRobert Watson static int unp_connect(struct socket *, struct sockaddr *,
292aea52f1bSRobert Watson struct thread *);
2937493f24eSPawel Jakub Dawidek static int unp_connectat(int, struct socket *, struct sockaddr *,
294315167c0SGleb Smirnoff struct thread *, bool);
2950bd8eb3eSGleb Smirnoff static void unp_connect2(struct socket *so, struct socket *so2);
296e7c33e29SRobert Watson static void unp_disconnect(struct unpcb *unp, struct unpcb *unp2);
29799ab95dbSMark Johnston static void unp_dispose(struct socket *so);
2984d77a549SAlfred Perlstein static void unp_shutdown(struct unpcb *);
299afc055d9SEd Schouten static void unp_drop(struct unpcb *);
300a0ec558aSRobert Watson static void unp_gc(__unused void *, int);
301be26ba7cSPawel Jakub Dawidek static void unp_scan(struct mbuf *, void (*)(struct filedescent **, int));
3024d77a549SAlfred Perlstein static void unp_discard(struct file *);
3038cb539f1SPawel Jakub Dawidek static void unp_freerights(struct filedescent **, int);
3045716d902SGleb Smirnoff static int unp_internalize(struct mbuf **, struct thread *,
3055716d902SGleb Smirnoff struct mbuf **, u_int *, u_int *);
306397c19d1SJeff Roberson static void unp_internalize_fp(struct file *);
307c2e3c52eSJilles Tjoelker static int unp_externalize(struct mbuf *, struct mbuf **, int);
3080cb64678SKonstantin Belousov static int unp_externalize_fp(struct file *);
3095716d902SGleb Smirnoff static struct mbuf *unp_addsockcred(struct thread *, struct mbuf *,
3105716d902SGleb Smirnoff int, struct mbuf **, u_int *, u_int *);
3110cb64678SKonstantin Belousov static void unp_process_defers(void * __unused, int);
312f708ef1bSPoul-Henning Kamp
31375a67bf3SMatt Macy static void
unp_pcb_hold(struct unpcb * unp)31475a67bf3SMatt Macy unp_pcb_hold(struct unpcb *unp)
31575a67bf3SMatt Macy {
3165362170dSMark Johnston u_int old __unused;
3175362170dSMark Johnston
3185362170dSMark Johnston old = refcount_acquire(&unp->unp_refcount);
3195362170dSMark Johnston KASSERT(old > 0, ("%s: unpcb %p has no references", __func__, unp));
32075a67bf3SMatt Macy }
32175a67bf3SMatt Macy
3225362170dSMark Johnston static __result_use_check bool
unp_pcb_rele(struct unpcb * unp)32375a67bf3SMatt Macy unp_pcb_rele(struct unpcb *unp)
32475a67bf3SMatt Macy {
3255362170dSMark Johnston bool ret;
32675a67bf3SMatt Macy
32775a67bf3SMatt Macy UNP_PCB_LOCK_ASSERT(unp);
3285362170dSMark Johnston
3295362170dSMark Johnston if ((ret = refcount_release(&unp->unp_refcount))) {
33075a67bf3SMatt Macy UNP_PCB_UNLOCK(unp);
33175a67bf3SMatt Macy UNP_PCB_LOCK_DESTROY(unp);
33275a67bf3SMatt Macy uma_zfree(unp_zone, unp);
33375a67bf3SMatt Macy }
3345362170dSMark Johnston return (ret);
33575a67bf3SMatt Macy }
33675a67bf3SMatt Macy
33775a67bf3SMatt Macy static void
unp_pcb_rele_notlast(struct unpcb * unp)338f0317f86SMark Johnston unp_pcb_rele_notlast(struct unpcb *unp)
339f0317f86SMark Johnston {
340f0317f86SMark Johnston bool ret __unused;
341f0317f86SMark Johnston
342f0317f86SMark Johnston ret = refcount_release(&unp->unp_refcount);
343f0317f86SMark Johnston KASSERT(!ret, ("%s: unpcb %p has no references", __func__, unp));
344f0317f86SMark Johnston }
345f0317f86SMark Johnston
346f0317f86SMark Johnston static void
unp_pcb_lock_pair(struct unpcb * unp,struct unpcb * unp2)3474820bf6aSMark Johnston unp_pcb_lock_pair(struct unpcb *unp, struct unpcb *unp2)
34875a67bf3SMatt Macy {
34975a67bf3SMatt Macy UNP_PCB_UNLOCK_ASSERT(unp);
35075a67bf3SMatt Macy UNP_PCB_UNLOCK_ASSERT(unp2);
3514820bf6aSMark Johnston
3524820bf6aSMark Johnston if (unp == unp2) {
3534820bf6aSMark Johnston UNP_PCB_LOCK(unp);
3544820bf6aSMark Johnston } else if ((uintptr_t)unp2 > (uintptr_t)unp) {
35575a67bf3SMatt Macy UNP_PCB_LOCK(unp);
35675a67bf3SMatt Macy UNP_PCB_LOCK(unp2);
35775a67bf3SMatt Macy } else {
35875a67bf3SMatt Macy UNP_PCB_LOCK(unp2);
35975a67bf3SMatt Macy UNP_PCB_LOCK(unp);
36075a67bf3SMatt Macy }
36175a67bf3SMatt Macy }
36275a67bf3SMatt Macy
3634820bf6aSMark Johnston static void
unp_pcb_unlock_pair(struct unpcb * unp,struct unpcb * unp2)3644820bf6aSMark Johnston unp_pcb_unlock_pair(struct unpcb *unp, struct unpcb *unp2)
3654820bf6aSMark Johnston {
3664820bf6aSMark Johnston UNP_PCB_UNLOCK(unp);
3674820bf6aSMark Johnston if (unp != unp2)
3684820bf6aSMark Johnston UNP_PCB_UNLOCK(unp2);
3694820bf6aSMark Johnston }
3704820bf6aSMark Johnston
371ccdadf1aSMark Johnston /*
372ccdadf1aSMark Johnston * Try to lock the connected peer of an already locked socket. In some cases
373ccdadf1aSMark Johnston * this requires that we unlock the current socket. The pairbusy counter is
374ccdadf1aSMark Johnston * used to block concurrent connection attempts while the lock is dropped. The
375ccdadf1aSMark Johnston * caller must be careful to revalidate PCB state.
376ccdadf1aSMark Johnston */
377ccdadf1aSMark Johnston static struct unpcb *
unp_pcb_lock_peer(struct unpcb * unp)378ccdadf1aSMark Johnston unp_pcb_lock_peer(struct unpcb *unp)
37975a67bf3SMatt Macy {
38075a67bf3SMatt Macy struct unpcb *unp2;
38175a67bf3SMatt Macy
382ccdadf1aSMark Johnston UNP_PCB_LOCK_ASSERT(unp);
383ccdadf1aSMark Johnston unp2 = unp->unp_conn;
3846404d7ffSMateusz Guzik if (unp2 == NULL)
385ccdadf1aSMark Johnston return (NULL);
386ccdadf1aSMark Johnston if (__predict_false(unp == unp2))
387ccdadf1aSMark Johnston return (unp);
388ccdadf1aSMark Johnston
389ccdadf1aSMark Johnston UNP_PCB_UNLOCK_ASSERT(unp2);
390ccdadf1aSMark Johnston
391ccdadf1aSMark Johnston if (__predict_true(UNP_PCB_TRYLOCK(unp2)))
392ccdadf1aSMark Johnston return (unp2);
393ccdadf1aSMark Johnston if ((uintptr_t)unp2 > (uintptr_t)unp) {
394ccdadf1aSMark Johnston UNP_PCB_LOCK(unp2);
395ccdadf1aSMark Johnston return (unp2);
396ccdadf1aSMark Johnston }
397ccdadf1aSMark Johnston unp->unp_pairbusy++;
398e62ca80bSMark Johnston unp_pcb_hold(unp2);
399e62ca80bSMark Johnston UNP_PCB_UNLOCK(unp);
400ccdadf1aSMark Johnston
401e62ca80bSMark Johnston UNP_PCB_LOCK(unp2);
402e62ca80bSMark Johnston UNP_PCB_LOCK(unp);
403ccdadf1aSMark Johnston KASSERT(unp->unp_conn == unp2 || unp->unp_conn == NULL,
404ccdadf1aSMark Johnston ("%s: socket %p was reconnected", __func__, unp));
405ccdadf1aSMark Johnston if (--unp->unp_pairbusy == 0 && (unp->unp_flags & UNP_WAITING) != 0) {
406ccdadf1aSMark Johnston unp->unp_flags &= ~UNP_WAITING;
407ccdadf1aSMark Johnston wakeup(unp);
40875a67bf3SMatt Macy }
409ccdadf1aSMark Johnston if (unp_pcb_rele(unp2)) {
410ccdadf1aSMark Johnston /* unp2 is unlocked. */
411ccdadf1aSMark Johnston return (NULL);
412ccdadf1aSMark Johnston }
413ccdadf1aSMark Johnston if (unp->unp_conn == NULL) {
414ccdadf1aSMark Johnston UNP_PCB_UNLOCK(unp2);
415ccdadf1aSMark Johnston return (NULL);
416ccdadf1aSMark Johnston }
417ccdadf1aSMark Johnston return (unp2);
418ccdadf1aSMark Johnston }
41975a67bf3SMatt Macy
420ac45e92fSRobert Watson static void
uipc_abort(struct socket * so)421a29f300eSGarrett Wollman uipc_abort(struct socket *so)
422df8bae1dSRodney W. Grimes {
423e7c33e29SRobert Watson struct unpcb *unp, *unp2;
424df8bae1dSRodney W. Grimes
42540f2ac28SRobert Watson unp = sotounpcb(so);
4264d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_abort: unp == NULL"));
42775a67bf3SMatt Macy UNP_PCB_UNLOCK_ASSERT(unp);
428e7c33e29SRobert Watson
429e7c33e29SRobert Watson UNP_PCB_LOCK(unp);
430e7c33e29SRobert Watson unp2 = unp->unp_conn;
431e7c33e29SRobert Watson if (unp2 != NULL) {
43275a67bf3SMatt Macy unp_pcb_hold(unp2);
433e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp);
43475a67bf3SMatt Macy unp_drop(unp2);
43575a67bf3SMatt Macy } else
43675a67bf3SMatt Macy UNP_PCB_UNLOCK(unp);
437df8bae1dSRodney W. Grimes }
438df8bae1dSRodney W. Grimes
439a29f300eSGarrett Wollman static int
uipc_attach(struct socket * so,int proto,struct thread * td)440b40ce416SJulian Elischer uipc_attach(struct socket *so, int proto, struct thread *td)
441a29f300eSGarrett Wollman {
442e7c33e29SRobert Watson u_long sendspace, recvspace;
4436d32873cSRobert Watson struct unpcb *unp;
4443dab55bcSRobert Watson int error;
445779f106aSGleb Smirnoff bool locked;
446df8bae1dSRodney W. Grimes
4476d32873cSRobert Watson KASSERT(so->so_pcb == NULL, ("uipc_attach: so_pcb != NULL"));
4486d32873cSRobert Watson switch (so->so_type) {
4496d32873cSRobert Watson case SOCK_STREAM:
450e7c33e29SRobert Watson sendspace = unpst_sendspace;
451e7c33e29SRobert Watson recvspace = unpst_recvspace;
4526d32873cSRobert Watson break;
4536d32873cSRobert Watson
4546d32873cSRobert Watson case SOCK_DGRAM:
455a7444f80SGleb Smirnoff STAILQ_INIT(&so->so_rcv.uxdg_mb);
456458f475dSGleb Smirnoff STAILQ_INIT(&so->so_snd.uxdg_mb);
457458f475dSGleb Smirnoff TAILQ_INIT(&so->so_rcv.uxdg_conns);
458458f475dSGleb Smirnoff /*
459458f475dSGleb Smirnoff * Since send buffer is either bypassed or is a part
460458f475dSGleb Smirnoff * of one-to-many receive buffer, we assign both space
461458f475dSGleb Smirnoff * limits to unpdg_recvspace.
462458f475dSGleb Smirnoff */
463458f475dSGleb Smirnoff sendspace = recvspace = unpdg_recvspace;
4646d32873cSRobert Watson break;
4656d32873cSRobert Watson
46684d61770SRobert Watson case SOCK_SEQPACKET:
46784d61770SRobert Watson sendspace = unpsp_sendspace;
46884d61770SRobert Watson recvspace = unpsp_recvspace;
46984d61770SRobert Watson break;
47084d61770SRobert Watson
4716d32873cSRobert Watson default:
472e7c33e29SRobert Watson panic("uipc_attach");
4736d32873cSRobert Watson }
474e7c33e29SRobert Watson error = soreserve(so, sendspace, recvspace);
4756d32873cSRobert Watson if (error)
4766d32873cSRobert Watson return (error);
47746a1d9bfSRobert Watson unp = uma_zalloc(unp_zone, M_NOWAIT | M_ZERO);
4786d32873cSRobert Watson if (unp == NULL)
4796d32873cSRobert Watson return (ENOBUFS);
4806d32873cSRobert Watson LIST_INIT(&unp->unp_refs);
481e7c33e29SRobert Watson UNP_PCB_LOCK_INIT(unp);
4826d32873cSRobert Watson unp->unp_socket = so;
4836d32873cSRobert Watson so->so_pcb = unp;
4845362170dSMark Johnston refcount_init(&unp->unp_refcount, 1);
485bfd03046SMark Johnston unp->unp_mode = ACCESSPERMS;
486e7c33e29SRobert Watson
487779f106aSGleb Smirnoff if ((locked = UNP_LINK_WOWNED()) == false)
488779f106aSGleb Smirnoff UNP_LINK_WLOCK();
489779f106aSGleb Smirnoff
4906d32873cSRobert Watson unp->unp_gencnt = ++unp_gencnt;
491f218ac50SMateusz Guzik unp->unp_ino = ++unp_ino;
4926d32873cSRobert Watson unp_count++;
49384d61770SRobert Watson switch (so->so_type) {
49484d61770SRobert Watson case SOCK_STREAM:
49584d61770SRobert Watson LIST_INSERT_HEAD(&unp_shead, unp, unp_link);
49684d61770SRobert Watson break;
49784d61770SRobert Watson
49884d61770SRobert Watson case SOCK_DGRAM:
49984d61770SRobert Watson LIST_INSERT_HEAD(&unp_dhead, unp, unp_link);
50084d61770SRobert Watson break;
50184d61770SRobert Watson
50284d61770SRobert Watson case SOCK_SEQPACKET:
50384d61770SRobert Watson LIST_INSERT_HEAD(&unp_sphead, unp, unp_link);
50484d61770SRobert Watson break;
50584d61770SRobert Watson
50684d61770SRobert Watson default:
50784d61770SRobert Watson panic("uipc_attach");
50884d61770SRobert Watson }
509779f106aSGleb Smirnoff
510779f106aSGleb Smirnoff if (locked == false)
511779f106aSGleb Smirnoff UNP_LINK_WUNLOCK();
5126d32873cSRobert Watson
5136d32873cSRobert Watson return (0);
514a29f300eSGarrett Wollman }
515a29f300eSGarrett Wollman
516a29f300eSGarrett Wollman static int
uipc_bindat(int fd,struct socket * so,struct sockaddr * nam,struct thread * td)5177493f24eSPawel Jakub Dawidek uipc_bindat(int fd, struct socket *so, struct sockaddr *nam, struct thread *td)
518a29f300eSGarrett Wollman {
519dd47f5caSRobert Watson struct sockaddr_un *soun = (struct sockaddr_un *)nam;
520dd47f5caSRobert Watson struct vattr vattr;
5215050aa86SKonstantin Belousov int error, namelen;
522dd47f5caSRobert Watson struct nameidata nd;
52340f2ac28SRobert Watson struct unpcb *unp;
524dd47f5caSRobert Watson struct vnode *vp;
525dd47f5caSRobert Watson struct mount *mp;
5267008be5bSPawel Jakub Dawidek cap_rights_t rights;
527dd47f5caSRobert Watson char *buf;
528bfd03046SMark Johnston mode_t mode;
529a29f300eSGarrett Wollman
530cb7df69bSKevin Lo if (nam->sa_family != AF_UNIX)
531cb7df69bSKevin Lo return (EAFNOSUPPORT);
532cb7df69bSKevin Lo
53340f2ac28SRobert Watson unp = sotounpcb(so);
5344d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_bind: unp == NULL"));
5354f1f0ef5SRobert Watson
536a06534c3SBjoern A. Zeeb if (soun->sun_len > sizeof(struct sockaddr_un))
537a06534c3SBjoern A. Zeeb return (EINVAL);
5384f1f0ef5SRobert Watson namelen = soun->sun_len - offsetof(struct sockaddr_un, sun_path);
5394f1f0ef5SRobert Watson if (namelen <= 0)
5404f1f0ef5SRobert Watson return (EINVAL);
541dd47f5caSRobert Watson
542dd47f5caSRobert Watson /*
5434f1f0ef5SRobert Watson * We don't allow simultaneous bind() calls on a single UNIX domain
5444f1f0ef5SRobert Watson * socket, so flag in-progress operations, and return an error if an
5454f1f0ef5SRobert Watson * operation is already in progress.
5464f1f0ef5SRobert Watson *
5474f1f0ef5SRobert Watson * Historically, we have not allowed a socket to be rebound, so this
548d7924b70SRobert Watson * also returns an error. Not allowing re-binding simplifies the
549d7924b70SRobert Watson * implementation and avoids a great many possible failure modes.
550dd47f5caSRobert Watson */
551e7c33e29SRobert Watson UNP_PCB_LOCK(unp);
552dd47f5caSRobert Watson if (unp->unp_vnode != NULL) {
553e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp);
554dd47f5caSRobert Watson return (EINVAL);
555dd47f5caSRobert Watson }
5564f1f0ef5SRobert Watson if (unp->unp_flags & UNP_BINDING) {
557e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp);
5584f1f0ef5SRobert Watson return (EALREADY);
559dd47f5caSRobert Watson }
5604f1f0ef5SRobert Watson unp->unp_flags |= UNP_BINDING;
561bfd03046SMark Johnston mode = unp->unp_mode & ~td->td_proc->p_pd->pd_cmask;
562e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp);
563dd47f5caSRobert Watson
564dd47f5caSRobert Watson buf = malloc(namelen + 1, M_TEMP, M_WAITOK);
5657928893dSEd Maste bcopy(soun->sun_path, buf, namelen);
5667928893dSEd Maste buf[namelen] = 0;
567dd47f5caSRobert Watson
568dd47f5caSRobert Watson restart:
5695b5b7e2cSMateusz Guzik NDINIT_ATRIGHTS(&nd, CREATE, NOFOLLOW | LOCKPARENT | NOCACHE,
5707e1d3eefSMateusz Guzik UIO_SYSSPACE, buf, fd, cap_rights_init_one(&rights, CAP_BINDAT));
571dd47f5caSRobert Watson /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */
572dd47f5caSRobert Watson error = namei(&nd);
573dd47f5caSRobert Watson if (error)
5744f1f0ef5SRobert Watson goto error;
575dd47f5caSRobert Watson vp = nd.ni_vp;
576dd47f5caSRobert Watson if (vp != NULL || vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
577bb92cd7bSMateusz Guzik NDFREE_PNBUF(&nd);
578dd47f5caSRobert Watson if (nd.ni_dvp == vp)
579dd47f5caSRobert Watson vrele(nd.ni_dvp);
580dd47f5caSRobert Watson else
581dd47f5caSRobert Watson vput(nd.ni_dvp);
582dd47f5caSRobert Watson if (vp != NULL) {
583dd47f5caSRobert Watson vrele(vp);
584dd47f5caSRobert Watson error = EADDRINUSE;
5854f1f0ef5SRobert Watson goto error;
586dd47f5caSRobert Watson }
587a75d1dddSMateusz Guzik error = vn_start_write(NULL, &mp, V_XSLEEP | V_PCATCH);
588dd47f5caSRobert Watson if (error)
5894f1f0ef5SRobert Watson goto error;
590dd47f5caSRobert Watson goto restart;
591dd47f5caSRobert Watson }
592dd47f5caSRobert Watson VATTR_NULL(&vattr);
593dd47f5caSRobert Watson vattr.va_type = VSOCK;
594bfd03046SMark Johnston vattr.va_mode = mode;
595dd47f5caSRobert Watson #ifdef MAC
59630d239bcSRobert Watson error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
597dd47f5caSRobert Watson &vattr);
598dd47f5caSRobert Watson #endif
599d56c175aSJason A. Harmening if (error == 0) {
600d56c175aSJason A. Harmening /*
601d56c175aSJason A. Harmening * The prior lookup may have left LK_SHARED in cn_lkflags,
602d56c175aSJason A. Harmening * and VOP_CREATE technically only requires the new vnode to
603d56c175aSJason A. Harmening * be locked shared. Most filesystems will return the new vnode
604d56c175aSJason A. Harmening * locked exclusive regardless, but we should explicitly
605d56c175aSJason A. Harmening * specify that here since we require it and assert to that
606d56c175aSJason A. Harmening * effect below.
607d56c175aSJason A. Harmening */
608d56c175aSJason A. Harmening nd.ni_cnd.cn_lkflags = (nd.ni_cnd.cn_lkflags & ~LK_SHARED) |
609d56c175aSJason A. Harmening LK_EXCLUSIVE;
610dd47f5caSRobert Watson error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
611d56c175aSJason A. Harmening }
612bb92cd7bSMateusz Guzik NDFREE_PNBUF(&nd);
613dd47f5caSRobert Watson if (error) {
6143b2aa360SKonstantin Belousov VOP_VPUT_PAIR(nd.ni_dvp, NULL, true);
615dd47f5caSRobert Watson vn_finished_write(mp);
616441eb16aSKonstantin Belousov if (error == ERELOOKUP)
617441eb16aSKonstantin Belousov goto restart;
6184f1f0ef5SRobert Watson goto error;
619dd47f5caSRobert Watson }
620dd47f5caSRobert Watson vp = nd.ni_vp;
62157fd3d55SPawel Jakub Dawidek ASSERT_VOP_ELOCKED(vp, "uipc_bind");
622dd47f5caSRobert Watson soun = (struct sockaddr_un *)sodupsockaddr(nam, M_WAITOK);
623e7c33e29SRobert Watson
624e7c33e29SRobert Watson UNP_PCB_LOCK(unp);
6250c3c207fSGleb Smirnoff VOP_UNP_BIND(vp, unp);
626dd47f5caSRobert Watson unp->unp_vnode = vp;
627dd47f5caSRobert Watson unp->unp_addr = soun;
6284f1f0ef5SRobert Watson unp->unp_flags &= ~UNP_BINDING;
629e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp);
6303b2aa360SKonstantin Belousov vref(vp);
6313b2aa360SKonstantin Belousov VOP_VPUT_PAIR(nd.ni_dvp, &vp, true);
632dd47f5caSRobert Watson vn_finished_write(mp);
6334f1f0ef5SRobert Watson free(buf, M_TEMP);
6344f1f0ef5SRobert Watson return (0);
635e7c33e29SRobert Watson
6364f1f0ef5SRobert Watson error:
637e7c33e29SRobert Watson UNP_PCB_LOCK(unp);
6384f1f0ef5SRobert Watson unp->unp_flags &= ~UNP_BINDING;
639e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp);
640dd47f5caSRobert Watson free(buf, M_TEMP);
64140f2ac28SRobert Watson return (error);
642a29f300eSGarrett Wollman }
643a29f300eSGarrett Wollman
644a29f300eSGarrett Wollman static int
uipc_bind(struct socket * so,struct sockaddr * nam,struct thread * td)6457493f24eSPawel Jakub Dawidek uipc_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
6467493f24eSPawel Jakub Dawidek {
6477493f24eSPawel Jakub Dawidek
6487493f24eSPawel Jakub Dawidek return (uipc_bindat(AT_FDCWD, so, nam, td));
6497493f24eSPawel Jakub Dawidek }
6507493f24eSPawel Jakub Dawidek
6517493f24eSPawel Jakub Dawidek static int
uipc_connect(struct socket * so,struct sockaddr * nam,struct thread * td)652b40ce416SJulian Elischer uipc_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
653a29f300eSGarrett Wollman {
6540d9ce3a1SRobert Watson int error;
655a29f300eSGarrett Wollman
656fd179ee9SRobert Watson KASSERT(td == curthread, ("uipc_connect: td != curthread"));
657fd179ee9SRobert Watson error = unp_connect(so, nam, td);
6580d9ce3a1SRobert Watson return (error);
659a29f300eSGarrett Wollman }
660a29f300eSGarrett Wollman
6617493f24eSPawel Jakub Dawidek static int
uipc_connectat(int fd,struct socket * so,struct sockaddr * nam,struct thread * td)6627493f24eSPawel Jakub Dawidek uipc_connectat(int fd, struct socket *so, struct sockaddr *nam,
6637493f24eSPawel Jakub Dawidek struct thread *td)
6647493f24eSPawel Jakub Dawidek {
6657493f24eSPawel Jakub Dawidek int error;
6667493f24eSPawel Jakub Dawidek
6677493f24eSPawel Jakub Dawidek KASSERT(td == curthread, ("uipc_connectat: td != curthread"));
668315167c0SGleb Smirnoff error = unp_connectat(fd, so, nam, td, false);
6697493f24eSPawel Jakub Dawidek return (error);
6707493f24eSPawel Jakub Dawidek }
6717493f24eSPawel Jakub Dawidek
672a152f8a3SRobert Watson static void
uipc_close(struct socket * so)673a152f8a3SRobert Watson uipc_close(struct socket *so)
674a152f8a3SRobert Watson {
675e7c33e29SRobert Watson struct unpcb *unp, *unp2;
676779f106aSGleb Smirnoff struct vnode *vp = NULL;
67775a67bf3SMatt Macy struct mtx *vplock;
678ccdadf1aSMark Johnston
679a152f8a3SRobert Watson unp = sotounpcb(so);
680a152f8a3SRobert Watson KASSERT(unp != NULL, ("uipc_close: unp == NULL"));
681e7c33e29SRobert Watson
68275a67bf3SMatt Macy vplock = NULL;
68375a67bf3SMatt Macy if ((vp = unp->unp_vnode) != NULL) {
684c08e016fSJohn Baldwin vplock = mtx_pool_find(unp_vp_mtxpool, vp);
68575a67bf3SMatt Macy mtx_lock(vplock);
686e7c33e29SRobert Watson }
68775a67bf3SMatt Macy UNP_PCB_LOCK(unp);
68875a67bf3SMatt Macy if (vp && unp->unp_vnode == NULL) {
68975a67bf3SMatt Macy mtx_unlock(vplock);
69075a67bf3SMatt Macy vp = NULL;
69175a67bf3SMatt Macy }
69275a67bf3SMatt Macy if (vp != NULL) {
693779f106aSGleb Smirnoff VOP_UNP_DETACH(vp);
694779f106aSGleb Smirnoff unp->unp_vnode = NULL;
695779f106aSGleb Smirnoff }
696ccdadf1aSMark Johnston if ((unp2 = unp_pcb_lock_peer(unp)) != NULL)
697acf9fd05SMatt Macy unp_disconnect(unp, unp2);
698ccdadf1aSMark Johnston else
699e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp);
70075a67bf3SMatt Macy if (vp) {
70175a67bf3SMatt Macy mtx_unlock(vplock);
702779f106aSGleb Smirnoff vrele(vp);
703a152f8a3SRobert Watson }
70475a67bf3SMatt Macy }
705a152f8a3SRobert Watson
7062c899584SRobert Watson static int
uipc_chmod(struct socket * so,mode_t mode,struct ucred * cred __unused,struct thread * td __unused)707bfd03046SMark Johnston uipc_chmod(struct socket *so, mode_t mode, struct ucred *cred __unused,
708bfd03046SMark Johnston struct thread *td __unused)
709bfd03046SMark Johnston {
710bfd03046SMark Johnston struct unpcb *unp;
711bfd03046SMark Johnston int error;
712bfd03046SMark Johnston
713bfd03046SMark Johnston if ((mode & ~ACCESSPERMS) != 0)
714bfd03046SMark Johnston return (EINVAL);
715bfd03046SMark Johnston
716bfd03046SMark Johnston error = 0;
717bfd03046SMark Johnston unp = sotounpcb(so);
718bfd03046SMark Johnston UNP_PCB_LOCK(unp);
719bfd03046SMark Johnston if (unp->unp_vnode != NULL || (unp->unp_flags & UNP_BINDING) != 0)
720bfd03046SMark Johnston error = EINVAL;
721bfd03046SMark Johnston else
722bfd03046SMark Johnston unp->unp_mode = mode;
723bfd03046SMark Johnston UNP_PCB_UNLOCK(unp);
724bfd03046SMark Johnston return (error);
725bfd03046SMark Johnston }
726bfd03046SMark Johnston
727bfd03046SMark Johnston static int
uipc_connect2(struct socket * so1,struct socket * so2)728a29f300eSGarrett Wollman uipc_connect2(struct socket *so1, struct socket *so2)
729a29f300eSGarrett Wollman {
730e7c33e29SRobert Watson struct unpcb *unp, *unp2;
73108f17d14SGleb Smirnoff
73208f17d14SGleb Smirnoff if (so1->so_type != so2->so_type)
73308f17d14SGleb Smirnoff return (EPROTOTYPE);
734a29f300eSGarrett Wollman
735e7c33e29SRobert Watson unp = so1->so_pcb;
7364d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_connect2: unp == NULL"));
737e7c33e29SRobert Watson unp2 = so2->so_pcb;
738e7c33e29SRobert Watson KASSERT(unp2 != NULL, ("uipc_connect2: unp2 == NULL"));
7394820bf6aSMark Johnston unp_pcb_lock_pair(unp, unp2);
7400bd8eb3eSGleb Smirnoff unp_connect2(so1, so2);
7414820bf6aSMark Johnston unp_pcb_unlock_pair(unp, unp2);
74208f17d14SGleb Smirnoff
74308f17d14SGleb Smirnoff return (0);
744a29f300eSGarrett Wollman }
745a29f300eSGarrett Wollman
746bc725eafSRobert Watson static void
uipc_detach(struct socket * so)747a29f300eSGarrett Wollman uipc_detach(struct socket *so)
748a29f300eSGarrett Wollman {
749e7c33e29SRobert Watson struct unpcb *unp, *unp2;
75075a67bf3SMatt Macy struct mtx *vplock;
7516d32873cSRobert Watson struct vnode *vp;
752ccdadf1aSMark Johnston int local_unp_rights;
753a29f300eSGarrett Wollman
75440f2ac28SRobert Watson unp = sotounpcb(so);
7554d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_detach: unp == NULL"));
756e7c33e29SRobert Watson
757434ac8b6SMark Johnston vp = NULL;
758c0874c34SMatt Macy vplock = NULL;
759434ac8b6SMark Johnston
760289bee16SGleb Smirnoff if (!SOLISTENING(so))
761289bee16SGleb Smirnoff unp_dispose(so);
762289bee16SGleb Smirnoff
763779f106aSGleb Smirnoff UNP_LINK_WLOCK();
7646d32873cSRobert Watson LIST_REMOVE(unp, unp_link);
765a9aa06f7SJason A. Harmening if (unp->unp_gcflag & UNPGC_DEAD)
766a9aa06f7SJason A. Harmening LIST_REMOVE(unp, unp_dead);
7676d32873cSRobert Watson unp->unp_gencnt = ++unp_gencnt;
7686d32873cSRobert Watson --unp_count;
76975a67bf3SMatt Macy UNP_LINK_WUNLOCK();
770434ac8b6SMark Johnston
77175a67bf3SMatt Macy UNP_PCB_UNLOCK_ASSERT(unp);
77275a67bf3SMatt Macy restart:
77375a67bf3SMatt Macy if ((vp = unp->unp_vnode) != NULL) {
774c08e016fSJohn Baldwin vplock = mtx_pool_find(unp_vp_mtxpool, vp);
77575a67bf3SMatt Macy mtx_lock(vplock);
77675a67bf3SMatt Macy }
77775a67bf3SMatt Macy UNP_PCB_LOCK(unp);
778db38b699SMark Johnston if (unp->unp_vnode != vp && unp->unp_vnode != NULL) {
779c0874c34SMatt Macy if (vplock)
78075a67bf3SMatt Macy mtx_unlock(vplock);
78175a67bf3SMatt Macy UNP_PCB_UNLOCK(unp);
78275a67bf3SMatt Macy goto restart;
78375a67bf3SMatt Macy }
7846d32873cSRobert Watson if ((vp = unp->unp_vnode) != NULL) {
785c7e41c8bSMikolaj Golub VOP_UNP_DETACH(vp);
7866d32873cSRobert Watson unp->unp_vnode = NULL;
7876d32873cSRobert Watson }
788ccdadf1aSMark Johnston if ((unp2 = unp_pcb_lock_peer(unp)) != NULL)
789e7c33e29SRobert Watson unp_disconnect(unp, unp2);
790f0317f86SMark Johnston else
79175a67bf3SMatt Macy UNP_PCB_UNLOCK(unp);
792ccdadf1aSMark Johnston
79375a67bf3SMatt Macy UNP_REF_LIST_LOCK();
7946d32873cSRobert Watson while (!LIST_EMPTY(&unp->unp_refs)) {
7956d32873cSRobert Watson struct unpcb *ref = LIST_FIRST(&unp->unp_refs);
796e7c33e29SRobert Watson
79775a67bf3SMatt Macy unp_pcb_hold(ref);
79875a67bf3SMatt Macy UNP_REF_LIST_UNLOCK();
79975a67bf3SMatt Macy
80075a67bf3SMatt Macy MPASS(ref != unp);
80175a67bf3SMatt Macy UNP_PCB_UNLOCK_ASSERT(ref);
802afc055d9SEd Schouten unp_drop(ref);
80375a67bf3SMatt Macy UNP_REF_LIST_LOCK();
8046d32873cSRobert Watson }
80575a67bf3SMatt Macy UNP_REF_LIST_UNLOCK();
806ccdadf1aSMark Johnston
80775a67bf3SMatt Macy UNP_PCB_LOCK(unp);
808397c19d1SJeff Roberson local_unp_rights = unp_rights;
8096d32873cSRobert Watson unp->unp_socket->so_pcb = NULL;
81075a67bf3SMatt Macy unp->unp_socket = NULL;
811db38b699SMark Johnston free(unp->unp_addr, M_SONAME);
812db38b699SMark Johnston unp->unp_addr = NULL;
813db38b699SMark Johnston if (!unp_pcb_rele(unp))
8146e2faa24SRobert Watson UNP_PCB_UNLOCK(unp);
81575a67bf3SMatt Macy if (vp) {
81675a67bf3SMatt Macy mtx_unlock(vplock);
8176d32873cSRobert Watson vrele(vp);
81875a67bf3SMatt Macy }
8196d32873cSRobert Watson if (local_unp_rights)
820daee0f0bSKonstantin Belousov taskqueue_enqueue_timeout(taskqueue_thread, &unp_gc_task, -1);
821a7444f80SGleb Smirnoff
822a7444f80SGleb Smirnoff switch (so->so_type) {
823a7444f80SGleb Smirnoff case SOCK_DGRAM:
824a7444f80SGleb Smirnoff /*
825458f475dSGleb Smirnoff * Everything should have been unlinked/freed by unp_dispose()
826458f475dSGleb Smirnoff * and/or unp_disconnect().
827a7444f80SGleb Smirnoff */
828458f475dSGleb Smirnoff MPASS(so->so_rcv.uxdg_peeked == NULL);
829a7444f80SGleb Smirnoff MPASS(STAILQ_EMPTY(&so->so_rcv.uxdg_mb));
830458f475dSGleb Smirnoff MPASS(TAILQ_EMPTY(&so->so_rcv.uxdg_conns));
831458f475dSGleb Smirnoff MPASS(STAILQ_EMPTY(&so->so_snd.uxdg_mb));
832a7444f80SGleb Smirnoff }
833a29f300eSGarrett Wollman }
834a29f300eSGarrett Wollman
835a29f300eSGarrett Wollman static int
uipc_disconnect(struct socket * so)836a29f300eSGarrett Wollman uipc_disconnect(struct socket *so)
837a29f300eSGarrett Wollman {
838e7c33e29SRobert Watson struct unpcb *unp, *unp2;
839a29f300eSGarrett Wollman
84040f2ac28SRobert Watson unp = sotounpcb(so);
8414d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_disconnect: unp == NULL"));
842e7c33e29SRobert Watson
843e7c33e29SRobert Watson UNP_PCB_LOCK(unp);
844ccdadf1aSMark Johnston if ((unp2 = unp_pcb_lock_peer(unp)) != NULL)
84575a67bf3SMatt Macy unp_disconnect(unp, unp2);
846ccdadf1aSMark Johnston else
847ccdadf1aSMark Johnston UNP_PCB_UNLOCK(unp);
848e5aeaa0cSDag-Erling Smørgrav return (0);
849a29f300eSGarrett Wollman }
850a29f300eSGarrett Wollman
851a29f300eSGarrett Wollman static int
uipc_listen(struct socket * so,int backlog,struct thread * td)852d374e81eSRobert Watson uipc_listen(struct socket *so, int backlog, struct thread *td)
853a29f300eSGarrett Wollman {
85440f2ac28SRobert Watson struct unpcb *unp;
8550d9ce3a1SRobert Watson int error;
856a29f300eSGarrett Wollman
85701235012SGleb Smirnoff MPASS(so->so_type != SOCK_DGRAM);
858beb4b312SGleb Smirnoff
859bd4a39ccSMark Johnston /*
860bd4a39ccSMark Johnston * Synchronize with concurrent connection attempts.
861bd4a39ccSMark Johnston */
862bd4a39ccSMark Johnston error = 0;
86340f2ac28SRobert Watson unp = sotounpcb(so);
864e7c33e29SRobert Watson UNP_PCB_LOCK(unp);
865bd4a39ccSMark Johnston if (unp->unp_conn != NULL || (unp->unp_flags & UNP_CONNECTING) != 0)
866bd4a39ccSMark Johnston error = EINVAL;
867bd4a39ccSMark Johnston else if (unp->unp_vnode == NULL)
868bd4a39ccSMark Johnston error = EDESTADDRREQ;
869bd4a39ccSMark Johnston if (error != 0) {
870e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp);
87147a84387SEd Schouten return (error);
87240f2ac28SRobert Watson }
873e7c33e29SRobert Watson
874e7c33e29SRobert Watson SOCK_LOCK(so);
875e7c33e29SRobert Watson error = solisten_proto_check(so);
876e7c33e29SRobert Watson if (error == 0) {
877c5afec6eSDmitry Chagin cru2xt(td, &unp->unp_peercred);
878e7c33e29SRobert Watson solisten_proto(so, backlog);
879e7c33e29SRobert Watson }
880e7c33e29SRobert Watson SOCK_UNLOCK(so);
881e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp);
8820d9ce3a1SRobert Watson return (error);
883a29f300eSGarrett Wollman }
884a29f300eSGarrett Wollman
885a29f300eSGarrett Wollman static int
uipc_peeraddr(struct socket * so,struct sockaddr * ret)8860fac350cSGleb Smirnoff uipc_peeraddr(struct socket *so, struct sockaddr *ret)
887a29f300eSGarrett Wollman {
888e7c33e29SRobert Watson struct unpcb *unp, *unp2;
8890d9ce3a1SRobert Watson const struct sockaddr *sa;
890a29f300eSGarrett Wollman
8914d4b555eSRobert Watson unp = sotounpcb(so);
8924d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_peeraddr: unp == NULL"));
893e7c33e29SRobert Watson
894e8f6e5b2SMark Johnston UNP_PCB_LOCK(unp);
895e8f6e5b2SMark Johnston unp2 = unp_pcb_lock_peer(unp);
896e7c33e29SRobert Watson if (unp2 != NULL) {
897e7c33e29SRobert Watson if (unp2->unp_addr != NULL)
898afd9f91cSJohn Baldwin sa = (struct sockaddr *)unp2->unp_addr;
899e7c33e29SRobert Watson else
9000d9ce3a1SRobert Watson sa = &sun_noname;
9010fac350cSGleb Smirnoff bcopy(sa, ret, sa->sa_len);
902712079d3SMark Johnston unp_pcb_unlock_pair(unp, unp2);
903e7c33e29SRobert Watson } else {
904e8f6e5b2SMark Johnston UNP_PCB_UNLOCK(unp);
9050fac350cSGleb Smirnoff sa = &sun_noname;
9060fac350cSGleb Smirnoff bcopy(sa, ret, sa->sa_len);
907712079d3SMark Johnston }
908e5aeaa0cSDag-Erling Smørgrav return (0);
909a29f300eSGarrett Wollman }
910a29f300eSGarrett Wollman
911a29f300eSGarrett Wollman static int
uipc_rcvd(struct socket * so,int flags)9125716d902SGleb Smirnoff uipc_rcvd(struct socket *so, int flags)
913a29f300eSGarrett Wollman {
914f3f49bbbSRobert Watson struct unpcb *unp, *unp2;
915a29f300eSGarrett Wollman struct socket *so2;
9165716d902SGleb Smirnoff u_int mbcnt, sbcc;
917d80a97deSGleb Smirnoff
918d80a97deSGleb Smirnoff unp = sotounpcb(so);
9195716d902SGleb Smirnoff KASSERT(unp != NULL, ("%s: unp == NULL", __func__));
9205716d902SGleb Smirnoff KASSERT(so->so_type == SOCK_STREAM || so->so_type == SOCK_SEQPACKET,
9215716d902SGleb Smirnoff ("%s: socktype %d", __func__, so->so_type));
922d80a97deSGleb Smirnoff
9235716d902SGleb Smirnoff /*
9245716d902SGleb Smirnoff * Adjust backpressure on sender and wakeup any waiting to write.
9255716d902SGleb Smirnoff *
9265716d902SGleb Smirnoff * The unp lock is acquired to maintain the validity of the unp_conn
9275716d902SGleb Smirnoff * pointer; no lock on unp2 is required as unp2->unp_socket will be
9285716d902SGleb Smirnoff * static as long as we don't permit unp2 to disconnect from unp,
9295716d902SGleb Smirnoff * which is prevented by the lock on unp. We cache values from
9305716d902SGleb Smirnoff * so_rcv to avoid holding the so_rcv lock over the entire
9315716d902SGleb Smirnoff * transaction on the remote so_snd.
9325716d902SGleb Smirnoff */
9335716d902SGleb Smirnoff SOCKBUF_LOCK(&so->so_rcv);
9345716d902SGleb Smirnoff mbcnt = so->so_rcv.sb_mbcnt;
9355716d902SGleb Smirnoff sbcc = sbavail(&so->so_rcv);
9365716d902SGleb Smirnoff SOCKBUF_UNLOCK(&so->so_rcv);
9375716d902SGleb Smirnoff /*
9385716d902SGleb Smirnoff * There is a benign race condition at this point. If we're planning to
9395716d902SGleb Smirnoff * clear SB_STOP, but uipc_send is called on the connected socket at
9405716d902SGleb Smirnoff * this instant, it might add data to the sockbuf and set SB_STOP. Then
9415716d902SGleb Smirnoff * we would erroneously clear SB_STOP below, even though the sockbuf is
9425716d902SGleb Smirnoff * full. The race is benign because the only ill effect is to allow the
9435716d902SGleb Smirnoff * sockbuf to exceed its size limit, and the size limits are not
9445716d902SGleb Smirnoff * strictly guaranteed anyway.
9455716d902SGleb Smirnoff */
9465716d902SGleb Smirnoff UNP_PCB_LOCK(unp);
9475716d902SGleb Smirnoff unp2 = unp->unp_conn;
9485716d902SGleb Smirnoff if (unp2 == NULL) {
9495716d902SGleb Smirnoff UNP_PCB_UNLOCK(unp);
9505716d902SGleb Smirnoff return (0);
9515716d902SGleb Smirnoff }
9525716d902SGleb Smirnoff so2 = unp2->unp_socket;
9535716d902SGleb Smirnoff SOCKBUF_LOCK(&so2->so_snd);
9545716d902SGleb Smirnoff if (sbcc < so2->so_snd.sb_hiwat && mbcnt < so2->so_snd.sb_mbmax)
9555716d902SGleb Smirnoff so2->so_snd.sb_flags &= ~SB_STOP;
9565716d902SGleb Smirnoff sowwakeup_locked(so2);
9575716d902SGleb Smirnoff UNP_PCB_UNLOCK(unp);
9585716d902SGleb Smirnoff return (0);
9595716d902SGleb Smirnoff }
9605716d902SGleb Smirnoff
9615716d902SGleb Smirnoff static int
uipc_send(struct socket * so,int flags,struct mbuf * m,struct sockaddr * nam,struct mbuf * control,struct thread * td)9625716d902SGleb Smirnoff uipc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
9635716d902SGleb Smirnoff struct mbuf *control, struct thread *td)
9645716d902SGleb Smirnoff {
9655716d902SGleb Smirnoff struct unpcb *unp, *unp2;
9665716d902SGleb Smirnoff struct socket *so2;
9675716d902SGleb Smirnoff u_int mbcnt, sbcc;
9685716d902SGleb Smirnoff int error;
9695716d902SGleb Smirnoff
9705716d902SGleb Smirnoff unp = sotounpcb(so);
9715716d902SGleb Smirnoff KASSERT(unp != NULL, ("%s: unp == NULL", __func__));
9725716d902SGleb Smirnoff KASSERT(so->so_type == SOCK_STREAM || so->so_type == SOCK_SEQPACKET,
9735716d902SGleb Smirnoff ("%s: socktype %d", __func__, so->so_type));
9745716d902SGleb Smirnoff
9755716d902SGleb Smirnoff error = 0;
9765716d902SGleb Smirnoff if (flags & PRUS_OOB) {
9775716d902SGleb Smirnoff error = EOPNOTSUPP;
9785716d902SGleb Smirnoff goto release;
9795716d902SGleb Smirnoff }
9805716d902SGleb Smirnoff if (control != NULL &&
9815716d902SGleb Smirnoff (error = unp_internalize(&control, td, NULL, NULL, NULL)))
9825716d902SGleb Smirnoff goto release;
9835716d902SGleb Smirnoff
9845716d902SGleb Smirnoff unp2 = NULL;
9855716d902SGleb Smirnoff if ((so->so_state & SS_ISCONNECTED) == 0) {
9865716d902SGleb Smirnoff if (nam != NULL) {
9875716d902SGleb Smirnoff if ((error = unp_connect(so, nam, td)) != 0)
9885716d902SGleb Smirnoff goto out;
9895716d902SGleb Smirnoff } else {
9905716d902SGleb Smirnoff error = ENOTCONN;
9915716d902SGleb Smirnoff goto out;
9925716d902SGleb Smirnoff }
9935716d902SGleb Smirnoff }
9945716d902SGleb Smirnoff
9955716d902SGleb Smirnoff UNP_PCB_LOCK(unp);
9965716d902SGleb Smirnoff if ((unp2 = unp_pcb_lock_peer(unp)) == NULL) {
9975716d902SGleb Smirnoff UNP_PCB_UNLOCK(unp);
9985716d902SGleb Smirnoff error = ENOTCONN;
9995716d902SGleb Smirnoff goto out;
10005716d902SGleb Smirnoff } else if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
10015716d902SGleb Smirnoff unp_pcb_unlock_pair(unp, unp2);
10025716d902SGleb Smirnoff error = EPIPE;
10035716d902SGleb Smirnoff goto out;
10045716d902SGleb Smirnoff }
10055716d902SGleb Smirnoff UNP_PCB_UNLOCK(unp);
10065716d902SGleb Smirnoff if ((so2 = unp2->unp_socket) == NULL) {
10075716d902SGleb Smirnoff UNP_PCB_UNLOCK(unp2);
10085716d902SGleb Smirnoff error = ENOTCONN;
10095716d902SGleb Smirnoff goto out;
10105716d902SGleb Smirnoff }
10115716d902SGleb Smirnoff SOCKBUF_LOCK(&so2->so_rcv);
10122de07e40SConrad Meyer if (unp2->unp_flags & UNP_WANTCRED_MASK) {
10136a2989fdSMatthew N. Dodd /*
10142de07e40SConrad Meyer * Credentials are passed only once on SOCK_STREAM and
10152de07e40SConrad Meyer * SOCK_SEQPACKET (LOCAL_CREDS => WANTCRED_ONESHOT), or
10162de07e40SConrad Meyer * forever (LOCAL_CREDS_PERSISTENT => WANTCRED_ALWAYS).
10176a2989fdSMatthew N. Dodd */
10185716d902SGleb Smirnoff control = unp_addsockcred(td, control, unp2->unp_flags, NULL,
10195716d902SGleb Smirnoff NULL, NULL);
10202de07e40SConrad Meyer unp2->unp_flags &= ~UNP_WANTCRED_ONESHOT;
10216a2989fdSMatthew N. Dodd }
10225b0480f2SMark Johnston
1023df8bae1dSRodney W. Grimes /*
10245716d902SGleb Smirnoff * Send to paired receive port and wake up readers. Don't
10255716d902SGleb Smirnoff * check for space available in the receive buffer if we're
10265716d902SGleb Smirnoff * attaching ancillary data; Unix domain sockets only check
10275716d902SGleb Smirnoff * for space in the sending sockbuf, and that check is
10285716d902SGleb Smirnoff * performed one level up the stack. At that level we cannot
10295716d902SGleb Smirnoff * precisely account for the amount of buffer space used
10305716d902SGleb Smirnoff * (e.g., because control messages are not yet internalized).
1031df8bae1dSRodney W. Grimes */
10325716d902SGleb Smirnoff switch (so->so_type) {
10335716d902SGleb Smirnoff case SOCK_STREAM:
10345716d902SGleb Smirnoff if (control != NULL) {
10355716d902SGleb Smirnoff sbappendcontrol_locked(&so2->so_rcv,
10365716d902SGleb Smirnoff m->m_len > 0 ? m : NULL, control, flags);
1037d80a97deSGleb Smirnoff control = NULL;
1038d80a97deSGleb Smirnoff } else
10395716d902SGleb Smirnoff sbappend_locked(&so2->so_rcv, m, flags);
10405716d902SGleb Smirnoff break;
1041d80a97deSGleb Smirnoff
10425716d902SGleb Smirnoff case SOCK_SEQPACKET:
10435716d902SGleb Smirnoff if (sbappendaddr_nospacecheck_locked(&so2->so_rcv,
10445716d902SGleb Smirnoff &sun_noname, m, control))
10455716d902SGleb Smirnoff control = NULL;
10465716d902SGleb Smirnoff break;
10475716d902SGleb Smirnoff }
1048d80a97deSGleb Smirnoff
10495716d902SGleb Smirnoff mbcnt = so2->so_rcv.sb_mbcnt;
10505716d902SGleb Smirnoff sbcc = sbavail(&so2->so_rcv);
10515716d902SGleb Smirnoff if (sbcc)
10525716d902SGleb Smirnoff sorwakeup_locked(so2);
1053d80a97deSGleb Smirnoff else
10545716d902SGleb Smirnoff SOCKBUF_UNLOCK(&so2->so_rcv);
10555716d902SGleb Smirnoff
1056d80a97deSGleb Smirnoff /*
10575716d902SGleb Smirnoff * The PCB lock on unp2 protects the SB_STOP flag. Without it,
10585716d902SGleb Smirnoff * it would be possible for uipc_rcvd to be called at this
10595716d902SGleb Smirnoff * point, drain the receiving sockbuf, clear SB_STOP, and then
10605716d902SGleb Smirnoff * we would set SB_STOP below. That could lead to an empty
10615716d902SGleb Smirnoff * sockbuf having SB_STOP set
1062d80a97deSGleb Smirnoff */
10635716d902SGleb Smirnoff SOCKBUF_LOCK(&so->so_snd);
10645716d902SGleb Smirnoff if (sbcc >= so->so_snd.sb_hiwat || mbcnt >= so->so_snd.sb_mbmax)
10655716d902SGleb Smirnoff so->so_snd.sb_flags |= SB_STOP;
10665716d902SGleb Smirnoff SOCKBUF_UNLOCK(&so->so_snd);
10675716d902SGleb Smirnoff UNP_PCB_UNLOCK(unp2);
10685716d902SGleb Smirnoff m = NULL;
10695716d902SGleb Smirnoff out:
10705716d902SGleb Smirnoff /*
10715716d902SGleb Smirnoff * PRUS_EOF is equivalent to pr_send followed by pr_shutdown.
10725716d902SGleb Smirnoff */
10735716d902SGleb Smirnoff if (flags & PRUS_EOF) {
10745716d902SGleb Smirnoff UNP_PCB_LOCK(unp);
10755716d902SGleb Smirnoff socantsendmore(so);
10765716d902SGleb Smirnoff unp_shutdown(unp);
10775716d902SGleb Smirnoff UNP_PCB_UNLOCK(unp);
1078d80a97deSGleb Smirnoff }
10795716d902SGleb Smirnoff if (control != NULL && error != 0)
10805716d902SGleb Smirnoff unp_scan(control, unp_freerights);
1081d80a97deSGleb Smirnoff
10825716d902SGleb Smirnoff release:
10835716d902SGleb Smirnoff if (control != NULL)
10845716d902SGleb Smirnoff m_freem(control);
10855716d902SGleb Smirnoff /*
10865716d902SGleb Smirnoff * In case of PRUS_NOTREADY, uipc_ready() is responsible
10875716d902SGleb Smirnoff * for freeing memory.
10885716d902SGleb Smirnoff */
10895716d902SGleb Smirnoff if (m != NULL && (flags & PRUS_NOTREADY) == 0)
10905716d902SGleb Smirnoff m_freem(m);
1091d80a97deSGleb Smirnoff return (error);
1092d80a97deSGleb Smirnoff }
1093d80a97deSGleb Smirnoff
1094458f475dSGleb Smirnoff /* PF_UNIX/SOCK_DGRAM version of sbspace() */
1095458f475dSGleb Smirnoff static inline bool
uipc_dgram_sbspace(struct sockbuf * sb,u_int cc,u_int mbcnt)1096458f475dSGleb Smirnoff uipc_dgram_sbspace(struct sockbuf *sb, u_int cc, u_int mbcnt)
1097458f475dSGleb Smirnoff {
1098458f475dSGleb Smirnoff u_int bleft, mleft;
1099458f475dSGleb Smirnoff
1100820bafd0SGleb Smirnoff /*
1101820bafd0SGleb Smirnoff * Negative space may happen if send(2) is followed by
1102820bafd0SGleb Smirnoff * setsockopt(SO_SNDBUF/SO_RCVBUF) that shrinks maximum.
1103820bafd0SGleb Smirnoff */
1104820bafd0SGleb Smirnoff if (__predict_false(sb->sb_hiwat < sb->uxdg_cc ||
1105820bafd0SGleb Smirnoff sb->sb_mbmax < sb->uxdg_mbcnt))
1106820bafd0SGleb Smirnoff return (false);
1107458f475dSGleb Smirnoff
1108458f475dSGleb Smirnoff if (__predict_false(sb->sb_state & SBS_CANTRCVMORE))
1109458f475dSGleb Smirnoff return (false);
1110458f475dSGleb Smirnoff
1111458f475dSGleb Smirnoff bleft = sb->sb_hiwat - sb->uxdg_cc;
1112458f475dSGleb Smirnoff mleft = sb->sb_mbmax - sb->uxdg_mbcnt;
1113458f475dSGleb Smirnoff
1114458f475dSGleb Smirnoff return (bleft >= cc && mleft >= mbcnt);
1115458f475dSGleb Smirnoff }
1116458f475dSGleb Smirnoff
111734649582SGleb Smirnoff /*
111834649582SGleb Smirnoff * PF_UNIX/SOCK_DGRAM send
11195dc8dd5fSGleb Smirnoff *
11205dc8dd5fSGleb Smirnoff * Allocate a record consisting of 3 mbufs in the sequence of
11215dc8dd5fSGleb Smirnoff * from -> control -> data and append it to the socket buffer.
11221093f164SGleb Smirnoff *
11231093f164SGleb Smirnoff * The first mbuf carries sender's name and is a pkthdr that stores
11241093f164SGleb Smirnoff * overall length of datagram, its memory consumption and control length.
112534649582SGleb Smirnoff */
11261093f164SGleb Smirnoff #define ctllen PH_loc.thirtytwo[1]
11271093f164SGleb Smirnoff _Static_assert(offsetof(struct pkthdr, memlen) + sizeof(u_int) <=
11281093f164SGleb Smirnoff offsetof(struct pkthdr, ctllen), "unix/dgram can not store ctllen");
112934649582SGleb Smirnoff static int
uipc_sosend_dgram(struct socket * so,struct sockaddr * addr,struct uio * uio,struct mbuf * m,struct mbuf * c,int flags,struct thread * td)113034649582SGleb Smirnoff uipc_sosend_dgram(struct socket *so, struct sockaddr *addr, struct uio *uio,
113134649582SGleb Smirnoff struct mbuf *m, struct mbuf *c, int flags, struct thread *td)
113234649582SGleb Smirnoff {
113334649582SGleb Smirnoff struct unpcb *unp, *unp2;
113434649582SGleb Smirnoff const struct sockaddr *from;
113534649582SGleb Smirnoff struct socket *so2;
11365dc8dd5fSGleb Smirnoff struct sockbuf *sb;
11375716d902SGleb Smirnoff struct mbuf *f, *clast;
11381093f164SGleb Smirnoff u_int cc, ctl, mbcnt;
11391093f164SGleb Smirnoff u_int dcc __diagused, dctl __diagused, dmbcnt __diagused;
1140a7444f80SGleb Smirnoff int error;
114134649582SGleb Smirnoff
114234649582SGleb Smirnoff MPASS((uio != NULL && m == NULL) || (m != NULL && uio == NULL));
114334649582SGleb Smirnoff
114434649582SGleb Smirnoff error = 0;
11455dc8dd5fSGleb Smirnoff f = NULL;
11465716d902SGleb Smirnoff ctl = 0;
114734649582SGleb Smirnoff
114834649582SGleb Smirnoff if (__predict_false(flags & MSG_OOB)) {
114934649582SGleb Smirnoff error = EOPNOTSUPP;
115034649582SGleb Smirnoff goto out;
115134649582SGleb Smirnoff }
115234649582SGleb Smirnoff if (m == NULL) {
115334649582SGleb Smirnoff if (__predict_false(uio->uio_resid > unpdg_maxdgram)) {
115434649582SGleb Smirnoff error = EMSGSIZE;
115534649582SGleb Smirnoff goto out;
115634649582SGleb Smirnoff }
115734649582SGleb Smirnoff m = m_uiotombuf(uio, M_WAITOK, 0, max_hdr, M_PKTHDR);
115834649582SGleb Smirnoff if (__predict_false(m == NULL)) {
115934649582SGleb Smirnoff error = EFAULT;
116034649582SGleb Smirnoff goto out;
116134649582SGleb Smirnoff }
11621093f164SGleb Smirnoff f = m_gethdr(M_WAITOK, MT_SONAME);
11631093f164SGleb Smirnoff cc = m->m_pkthdr.len;
11641093f164SGleb Smirnoff mbcnt = MSIZE + m->m_pkthdr.memlen;
11655716d902SGleb Smirnoff if (c != NULL &&
11665716d902SGleb Smirnoff (error = unp_internalize(&c, td, &clast, &ctl, &mbcnt)))
116734649582SGleb Smirnoff goto out;
116834649582SGleb Smirnoff } else {
11695716d902SGleb Smirnoff /* pr_sosend() with mbuf usually is a kernel thread. */
117034649582SGleb Smirnoff
11715716d902SGleb Smirnoff M_ASSERTPKTHDR(m);
11725716d902SGleb Smirnoff if (__predict_false(c != NULL))
11735716d902SGleb Smirnoff panic("%s: control from a kernel thread", __func__);
11745716d902SGleb Smirnoff
117534649582SGleb Smirnoff if (__predict_false(m->m_pkthdr.len > unpdg_maxdgram)) {
117634649582SGleb Smirnoff error = EMSGSIZE;
117734649582SGleb Smirnoff goto out;
117834649582SGleb Smirnoff }
11791093f164SGleb Smirnoff if ((f = m_gethdr(M_NOWAIT, MT_SONAME)) == NULL) {
11805dc8dd5fSGleb Smirnoff error = ENOBUFS;
11815dc8dd5fSGleb Smirnoff goto out;
11825dc8dd5fSGleb Smirnoff }
11835716d902SGleb Smirnoff /* Condition the foreign mbuf to our standards. */
11845716d902SGleb Smirnoff m_clrprotoflags(m);
11855716d902SGleb Smirnoff m_tag_delete_chain(m, NULL);
11865716d902SGleb Smirnoff m->m_pkthdr.rcvif = NULL;
11875716d902SGleb Smirnoff m->m_pkthdr.flowid = 0;
11885716d902SGleb Smirnoff m->m_pkthdr.csum_flags = 0;
11895716d902SGleb Smirnoff m->m_pkthdr.fibnum = 0;
11905716d902SGleb Smirnoff m->m_pkthdr.rsstype = 0;
11915716d902SGleb Smirnoff
11925716d902SGleb Smirnoff cc = m->m_pkthdr.len;
11935716d902SGleb Smirnoff mbcnt = MSIZE;
11945716d902SGleb Smirnoff for (struct mbuf *mb = m; mb != NULL; mb = mb->m_next) {
11955716d902SGleb Smirnoff mbcnt += MSIZE;
11965716d902SGleb Smirnoff if (mb->m_flags & M_EXT)
11975716d902SGleb Smirnoff mbcnt += mb->m_ext.ext_size;
11985716d902SGleb Smirnoff }
119934649582SGleb Smirnoff }
120034649582SGleb Smirnoff
120134649582SGleb Smirnoff unp = sotounpcb(so);
120234649582SGleb Smirnoff MPASS(unp);
120334649582SGleb Smirnoff
120434649582SGleb Smirnoff /*
120534649582SGleb Smirnoff * XXXGL: would be cool to fully remove so_snd out of the equation
120634649582SGleb Smirnoff * and avoid this lock, which is not only extraneous, but also being
120734649582SGleb Smirnoff * released, thus still leaving possibility for a race. We can easily
120834649582SGleb Smirnoff * handle SBS_CANTSENDMORE/SS_ISCONNECTED complement in unpcb, but it
120934649582SGleb Smirnoff * is more difficult to invent something to handle so_error.
121034649582SGleb Smirnoff */
121134649582SGleb Smirnoff error = SOCK_IO_SEND_LOCK(so, SBLOCKWAIT(flags));
121234649582SGleb Smirnoff if (error)
121334649582SGleb Smirnoff goto out2;
1214a7444f80SGleb Smirnoff SOCK_SENDBUF_LOCK(so);
121534649582SGleb Smirnoff if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
121634649582SGleb Smirnoff SOCK_SENDBUF_UNLOCK(so);
121734649582SGleb Smirnoff error = EPIPE;
121834649582SGleb Smirnoff goto out3;
121934649582SGleb Smirnoff }
122034649582SGleb Smirnoff if (so->so_error != 0) {
122134649582SGleb Smirnoff error = so->so_error;
122234649582SGleb Smirnoff so->so_error = 0;
1223a7444f80SGleb Smirnoff SOCK_SENDBUF_UNLOCK(so);
122434649582SGleb Smirnoff goto out3;
122534649582SGleb Smirnoff }
122634649582SGleb Smirnoff if (((so->so_state & SS_ISCONNECTED) == 0) && addr == NULL) {
1227a7444f80SGleb Smirnoff SOCK_SENDBUF_UNLOCK(so);
122834649582SGleb Smirnoff error = EDESTADDRREQ;
122934649582SGleb Smirnoff goto out3;
123034649582SGleb Smirnoff }
1231a7444f80SGleb Smirnoff SOCK_SENDBUF_UNLOCK(so);
123234649582SGleb Smirnoff
1233315167c0SGleb Smirnoff if (addr != NULL) {
1234315167c0SGleb Smirnoff if ((error = unp_connectat(AT_FDCWD, so, addr, td, true)))
123534649582SGleb Smirnoff goto out3;
1236315167c0SGleb Smirnoff UNP_PCB_LOCK_ASSERT(unp);
1237315167c0SGleb Smirnoff unp2 = unp->unp_conn;
1238315167c0SGleb Smirnoff UNP_PCB_LOCK_ASSERT(unp2);
1239315167c0SGleb Smirnoff } else {
124034649582SGleb Smirnoff UNP_PCB_LOCK(unp);
124134649582SGleb Smirnoff unp2 = unp_pcb_lock_peer(unp);
124234649582SGleb Smirnoff if (unp2 == NULL) {
124334649582SGleb Smirnoff UNP_PCB_UNLOCK(unp);
124434649582SGleb Smirnoff error = ENOTCONN;
124534649582SGleb Smirnoff goto out3;
124634649582SGleb Smirnoff }
1247315167c0SGleb Smirnoff }
124834649582SGleb Smirnoff
124934649582SGleb Smirnoff if (unp2->unp_flags & UNP_WANTCRED_MASK)
12505716d902SGleb Smirnoff c = unp_addsockcred(td, c, unp2->unp_flags, &clast, &ctl,
12515716d902SGleb Smirnoff &mbcnt);
125234649582SGleb Smirnoff if (unp->unp_addr != NULL)
125334649582SGleb Smirnoff from = (struct sockaddr *)unp->unp_addr;
125434649582SGleb Smirnoff else
125534649582SGleb Smirnoff from = &sun_noname;
12565dc8dd5fSGleb Smirnoff f->m_len = from->sa_len;
12575dc8dd5fSGleb Smirnoff MPASS(from->sa_len <= MLEN);
12585dc8dd5fSGleb Smirnoff bcopy(from, mtod(f, void *), from->sa_len);
12595716d902SGleb Smirnoff ctl += f->m_len;
1260a7444f80SGleb Smirnoff
12611093f164SGleb Smirnoff /*
12621093f164SGleb Smirnoff * Concatenate mbufs: from -> control -> data.
12631093f164SGleb Smirnoff * Save overall cc and mbcnt in "from" mbuf.
12641093f164SGleb Smirnoff */
12655716d902SGleb Smirnoff if (c != NULL) {
12665716d902SGleb Smirnoff #ifdef INVARIANTS
12675716d902SGleb Smirnoff struct mbuf *mc;
12685716d902SGleb Smirnoff
12695716d902SGleb Smirnoff for (mc = c; mc->m_next != NULL; mc = mc->m_next);
12705716d902SGleb Smirnoff MPASS(mc == clast);
12715716d902SGleb Smirnoff #endif
12725716d902SGleb Smirnoff f->m_next = c;
12735716d902SGleb Smirnoff clast->m_next = m;
12745716d902SGleb Smirnoff c = NULL;
12755dc8dd5fSGleb Smirnoff } else
12765dc8dd5fSGleb Smirnoff f->m_next = m;
1277a7444f80SGleb Smirnoff m = NULL;
12781093f164SGleb Smirnoff #ifdef INVARIANTS
12791093f164SGleb Smirnoff dcc = dctl = dmbcnt = 0;
12801093f164SGleb Smirnoff for (struct mbuf *mb = f; mb != NULL; mb = mb->m_next) {
12811093f164SGleb Smirnoff if (mb->m_type == MT_DATA)
12821093f164SGleb Smirnoff dcc += mb->m_len;
12831093f164SGleb Smirnoff else
12841093f164SGleb Smirnoff dctl += mb->m_len;
12851093f164SGleb Smirnoff dmbcnt += MSIZE;
12861093f164SGleb Smirnoff if (mb->m_flags & M_EXT)
12871093f164SGleb Smirnoff dmbcnt += mb->m_ext.ext_size;
12881093f164SGleb Smirnoff }
12891093f164SGleb Smirnoff MPASS(dcc == cc);
12901093f164SGleb Smirnoff MPASS(dctl == ctl);
12911093f164SGleb Smirnoff MPASS(dmbcnt == mbcnt);
12921093f164SGleb Smirnoff #endif
12931093f164SGleb Smirnoff f->m_pkthdr.len = cc + ctl;
12941093f164SGleb Smirnoff f->m_pkthdr.memlen = mbcnt;
12951093f164SGleb Smirnoff f->m_pkthdr.ctllen = ctl;
1296a7444f80SGleb Smirnoff
1297458f475dSGleb Smirnoff /*
1298458f475dSGleb Smirnoff * Destination socket buffer selection.
1299458f475dSGleb Smirnoff *
1300458f475dSGleb Smirnoff * Unconnected sends, when !(so->so_state & SS_ISCONNECTED) and the
1301458f475dSGleb Smirnoff * destination address is supplied, create a temporary connection for
1302458f475dSGleb Smirnoff * the run time of the function (see call to unp_connectat() above and
1303458f475dSGleb Smirnoff * to unp_disconnect() below). We distinguish them by condition of
1304458f475dSGleb Smirnoff * (addr != NULL). We intentionally avoid adding 'bool connected' for
1305458f475dSGleb Smirnoff * that condition, since, again, through the run time of this code we
1306458f475dSGleb Smirnoff * are always connected. For such "unconnected" sends, the destination
1307458f475dSGleb Smirnoff * buffer would be the receive buffer of destination socket so2.
1308458f475dSGleb Smirnoff *
1309458f475dSGleb Smirnoff * For connected sends, data lands on the send buffer of the sender's
1310458f475dSGleb Smirnoff * socket "so". Then, if we just added the very first datagram
1311458f475dSGleb Smirnoff * on this send buffer, we need to add the send buffer on to the
1312458f475dSGleb Smirnoff * receiving socket's buffer list. We put ourselves on top of the
1313458f475dSGleb Smirnoff * list. Such logic gives infrequent senders priority over frequent
1314458f475dSGleb Smirnoff * senders.
1315458f475dSGleb Smirnoff *
1316458f475dSGleb Smirnoff * Note on byte count management. As long as event methods kevent(2),
1317458f475dSGleb Smirnoff * select(2) are not protocol specific (yet), we need to maintain
1318458f475dSGleb Smirnoff * meaningful values on the receive buffer. So, the receive buffer
1319458f475dSGleb Smirnoff * would accumulate counters from all connected buffers potentially
1320458f475dSGleb Smirnoff * having sb_ccc > sb_hiwat or sb_mbcnt > sb_mbmax.
1321458f475dSGleb Smirnoff */
1322a7444f80SGleb Smirnoff so2 = unp2->unp_socket;
1323458f475dSGleb Smirnoff sb = (addr == NULL) ? &so->so_snd : &so2->so_rcv;
1324a7444f80SGleb Smirnoff SOCK_RECVBUF_LOCK(so2);
1325458f475dSGleb Smirnoff if (uipc_dgram_sbspace(sb, cc + ctl, mbcnt)) {
1326458f475dSGleb Smirnoff if (addr == NULL && STAILQ_EMPTY(&sb->uxdg_mb))
1327458f475dSGleb Smirnoff TAILQ_INSERT_HEAD(&so2->so_rcv.uxdg_conns, &so->so_snd,
1328458f475dSGleb Smirnoff uxdg_clist);
1329a7444f80SGleb Smirnoff STAILQ_INSERT_TAIL(&sb->uxdg_mb, f, m_stailqpkt);
1330458f475dSGleb Smirnoff sb->uxdg_cc += cc + ctl;
1331458f475dSGleb Smirnoff sb->uxdg_ctl += ctl;
1332458f475dSGleb Smirnoff sb->uxdg_mbcnt += mbcnt;
1333458f475dSGleb Smirnoff so2->so_rcv.sb_acc += cc + ctl;
1334458f475dSGleb Smirnoff so2->so_rcv.sb_ccc += cc + ctl;
1335458f475dSGleb Smirnoff so2->so_rcv.sb_ctl += ctl;
1336458f475dSGleb Smirnoff so2->so_rcv.sb_mbcnt += mbcnt;
133734649582SGleb Smirnoff sorwakeup_locked(so2);
13381093f164SGleb Smirnoff f = NULL;
133934649582SGleb Smirnoff } else {
134034649582SGleb Smirnoff soroverflow_locked(so2);
134171e70c25SGleb Smirnoff error = ENOBUFS;
134261a14ddfSMark Johnston if (f->m_next->m_type == MT_CONTROL) {
13435716d902SGleb Smirnoff c = f->m_next;
134461a14ddfSMark Johnston f->m_next = NULL;
134561a14ddfSMark Johnston }
134634649582SGleb Smirnoff }
134734649582SGleb Smirnoff
134834649582SGleb Smirnoff if (addr != NULL)
134934649582SGleb Smirnoff unp_disconnect(unp, unp2);
135034649582SGleb Smirnoff else
135134649582SGleb Smirnoff unp_pcb_unlock_pair(unp, unp2);
135234649582SGleb Smirnoff
135334649582SGleb Smirnoff td->td_ru.ru_msgsnd++;
135434649582SGleb Smirnoff
135534649582SGleb Smirnoff out3:
135634649582SGleb Smirnoff SOCK_IO_SEND_UNLOCK(so);
135734649582SGleb Smirnoff out2:
13585716d902SGleb Smirnoff if (c)
13595716d902SGleb Smirnoff unp_scan(c, unp_freerights);
136034649582SGleb Smirnoff out:
13615dc8dd5fSGleb Smirnoff if (f)
1362a7444f80SGleb Smirnoff m_freem(f);
13635716d902SGleb Smirnoff if (c)
13645716d902SGleb Smirnoff m_freem(c);
136534649582SGleb Smirnoff if (m)
136634649582SGleb Smirnoff m_freem(m);
136734649582SGleb Smirnoff
136834649582SGleb Smirnoff return (error);
136934649582SGleb Smirnoff }
137034649582SGleb Smirnoff
1371e3fbbf96SGleb Smirnoff /*
1372458f475dSGleb Smirnoff * PF_UNIX/SOCK_DGRAM receive with MSG_PEEK.
1373458f475dSGleb Smirnoff * The mbuf has already been unlinked from the uxdg_mb of socket buffer
1374458f475dSGleb Smirnoff * and needs to be linked onto uxdg_peeked of receive socket buffer.
1375e3fbbf96SGleb Smirnoff */
1376e3fbbf96SGleb Smirnoff static int
uipc_peek_dgram(struct socket * so,struct mbuf * m,struct sockaddr ** psa,struct uio * uio,struct mbuf ** controlp,int * flagsp)1377458f475dSGleb Smirnoff uipc_peek_dgram(struct socket *so, struct mbuf *m, struct sockaddr **psa,
1378458f475dSGleb Smirnoff struct uio *uio, struct mbuf **controlp, int *flagsp)
1379e3fbbf96SGleb Smirnoff {
1380be1f485dSAlexander V. Chernikov ssize_t len = 0;
1381e3fbbf96SGleb Smirnoff int error;
1382e3fbbf96SGleb Smirnoff
1383458f475dSGleb Smirnoff so->so_rcv.uxdg_peeked = m;
1384458f475dSGleb Smirnoff so->so_rcv.uxdg_cc += m->m_pkthdr.len;
1385458f475dSGleb Smirnoff so->so_rcv.uxdg_ctl += m->m_pkthdr.ctllen;
1386458f475dSGleb Smirnoff so->so_rcv.uxdg_mbcnt += m->m_pkthdr.memlen;
1387a7444f80SGleb Smirnoff SOCK_RECVBUF_UNLOCK(so);
1388e3fbbf96SGleb Smirnoff
1389e3fbbf96SGleb Smirnoff KASSERT(m->m_type == MT_SONAME, ("m->m_type == %d", m->m_type));
1390e3fbbf96SGleb Smirnoff if (psa != NULL)
1391e3fbbf96SGleb Smirnoff *psa = sodupsockaddr(mtod(m, struct sockaddr *), M_WAITOK);
1392e3fbbf96SGleb Smirnoff
1393a7444f80SGleb Smirnoff m = m->m_next;
1394a7444f80SGleb Smirnoff KASSERT(m, ("%s: no data or control after soname", __func__));
1395e3fbbf96SGleb Smirnoff
1396e3fbbf96SGleb Smirnoff /*
1397e3fbbf96SGleb Smirnoff * With MSG_PEEK the control isn't executed, just copied.
1398e3fbbf96SGleb Smirnoff */
1399e3fbbf96SGleb Smirnoff while (m != NULL && m->m_type == MT_CONTROL) {
1400e3fbbf96SGleb Smirnoff if (controlp != NULL) {
1401e3fbbf96SGleb Smirnoff *controlp = m_copym(m, 0, m->m_len, M_WAITOK);
1402e3fbbf96SGleb Smirnoff controlp = &(*controlp)->m_next;
1403e3fbbf96SGleb Smirnoff }
1404e3fbbf96SGleb Smirnoff m = m->m_next;
1405e3fbbf96SGleb Smirnoff }
1406e3fbbf96SGleb Smirnoff KASSERT(m == NULL || m->m_type == MT_DATA,
1407e3fbbf96SGleb Smirnoff ("%s: not MT_DATA mbuf %p", __func__, m));
1408e3fbbf96SGleb Smirnoff while (m != NULL && uio->uio_resid > 0) {
1409e3fbbf96SGleb Smirnoff len = uio->uio_resid;
1410e3fbbf96SGleb Smirnoff if (len > m->m_len)
1411e3fbbf96SGleb Smirnoff len = m->m_len;
1412e3fbbf96SGleb Smirnoff error = uiomove(mtod(m, char *), (int)len, uio);
1413e3fbbf96SGleb Smirnoff if (error) {
1414e3fbbf96SGleb Smirnoff SOCK_IO_RECV_UNLOCK(so);
1415e3fbbf96SGleb Smirnoff return (error);
1416e3fbbf96SGleb Smirnoff }
1417e3fbbf96SGleb Smirnoff if (len == m->m_len)
1418e3fbbf96SGleb Smirnoff m = m->m_next;
1419e3fbbf96SGleb Smirnoff }
1420e3fbbf96SGleb Smirnoff SOCK_IO_RECV_UNLOCK(so);
1421e3fbbf96SGleb Smirnoff
1422be1f485dSAlexander V. Chernikov if (flagsp != NULL) {
1423be1f485dSAlexander V. Chernikov if (m != NULL) {
1424be1f485dSAlexander V. Chernikov if (*flagsp & MSG_TRUNC) {
1425be1f485dSAlexander V. Chernikov /* Report real length of the packet */
1426be1f485dSAlexander V. Chernikov uio->uio_resid -= m_length(m, NULL) - len;
1427be1f485dSAlexander V. Chernikov }
1428e3fbbf96SGleb Smirnoff *flagsp |= MSG_TRUNC;
1429be1f485dSAlexander V. Chernikov } else
1430be1f485dSAlexander V. Chernikov *flagsp &= ~MSG_TRUNC;
1431be1f485dSAlexander V. Chernikov }
1432e3fbbf96SGleb Smirnoff
1433e3fbbf96SGleb Smirnoff return (0);
1434e3fbbf96SGleb Smirnoff }
1435e3fbbf96SGleb Smirnoff
1436e3fbbf96SGleb Smirnoff /*
1437e3fbbf96SGleb Smirnoff * PF_UNIX/SOCK_DGRAM receive
1438e3fbbf96SGleb Smirnoff */
1439e3fbbf96SGleb Smirnoff static int
uipc_soreceive_dgram(struct socket * so,struct sockaddr ** psa,struct uio * uio,struct mbuf ** mp0,struct mbuf ** controlp,int * flagsp)1440e3fbbf96SGleb Smirnoff uipc_soreceive_dgram(struct socket *so, struct sockaddr **psa, struct uio *uio,
1441e3fbbf96SGleb Smirnoff struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
1442e3fbbf96SGleb Smirnoff {
1443458f475dSGleb Smirnoff struct sockbuf *sb = NULL;
14441093f164SGleb Smirnoff struct mbuf *m;
1445e3fbbf96SGleb Smirnoff int flags, error;
1446be1f485dSAlexander V. Chernikov ssize_t len = 0;
1447e3fbbf96SGleb Smirnoff bool nonblock;
1448e3fbbf96SGleb Smirnoff
1449e3fbbf96SGleb Smirnoff MPASS(mp0 == NULL);
1450e3fbbf96SGleb Smirnoff
1451e3fbbf96SGleb Smirnoff if (psa != NULL)
1452e3fbbf96SGleb Smirnoff *psa = NULL;
1453e3fbbf96SGleb Smirnoff if (controlp != NULL)
1454e3fbbf96SGleb Smirnoff *controlp = NULL;
1455e3fbbf96SGleb Smirnoff
1456e3fbbf96SGleb Smirnoff flags = flagsp != NULL ? *flagsp : 0;
1457e3fbbf96SGleb Smirnoff nonblock = (so->so_state & SS_NBIO) ||
1458e3fbbf96SGleb Smirnoff (flags & (MSG_DONTWAIT | MSG_NBIO));
1459e3fbbf96SGleb Smirnoff
1460e3fbbf96SGleb Smirnoff error = SOCK_IO_RECV_LOCK(so, SBLOCKWAIT(flags));
1461e3fbbf96SGleb Smirnoff if (__predict_false(error))
1462e3fbbf96SGleb Smirnoff return (error);
1463e3fbbf96SGleb Smirnoff
1464e3fbbf96SGleb Smirnoff /*
1465458f475dSGleb Smirnoff * Loop blocking while waiting for a datagram. Prioritize connected
1466458f475dSGleb Smirnoff * peers over unconnected sends. Set sb to selected socket buffer
1467458f475dSGleb Smirnoff * containing an mbuf on exit from the wait loop. A datagram that
1468458f475dSGleb Smirnoff * had already been peeked at has top priority.
1469e3fbbf96SGleb Smirnoff */
1470e3fbbf96SGleb Smirnoff SOCK_RECVBUF_LOCK(so);
1471458f475dSGleb Smirnoff while ((m = so->so_rcv.uxdg_peeked) == NULL &&
1472458f475dSGleb Smirnoff (sb = TAILQ_FIRST(&so->so_rcv.uxdg_conns)) == NULL &&
1473458f475dSGleb Smirnoff (m = STAILQ_FIRST(&so->so_rcv.uxdg_mb)) == NULL) {
1474e3fbbf96SGleb Smirnoff if (so->so_error) {
1475e3fbbf96SGleb Smirnoff error = so->so_error;
14765bdf8150SGleb Smirnoff if (!(flags & MSG_PEEK))
1477e3fbbf96SGleb Smirnoff so->so_error = 0;
1478a7444f80SGleb Smirnoff SOCK_RECVBUF_UNLOCK(so);
1479e3fbbf96SGleb Smirnoff SOCK_IO_RECV_UNLOCK(so);
1480e3fbbf96SGleb Smirnoff return (error);
1481e3fbbf96SGleb Smirnoff }
1482e3fbbf96SGleb Smirnoff if (so->so_rcv.sb_state & SBS_CANTRCVMORE ||
1483e3fbbf96SGleb Smirnoff uio->uio_resid == 0) {
1484a7444f80SGleb Smirnoff SOCK_RECVBUF_UNLOCK(so);
1485e3fbbf96SGleb Smirnoff SOCK_IO_RECV_UNLOCK(so);
1486e3fbbf96SGleb Smirnoff return (0);
1487e3fbbf96SGleb Smirnoff }
1488e3fbbf96SGleb Smirnoff if (nonblock) {
1489a7444f80SGleb Smirnoff SOCK_RECVBUF_UNLOCK(so);
1490e3fbbf96SGleb Smirnoff SOCK_IO_RECV_UNLOCK(so);
1491e3fbbf96SGleb Smirnoff return (EWOULDBLOCK);
1492e3fbbf96SGleb Smirnoff }
1493e3fbbf96SGleb Smirnoff error = sbwait(so, SO_RCV);
1494e3fbbf96SGleb Smirnoff if (error) {
1495a7444f80SGleb Smirnoff SOCK_RECVBUF_UNLOCK(so);
1496e3fbbf96SGleb Smirnoff SOCK_IO_RECV_UNLOCK(so);
1497e3fbbf96SGleb Smirnoff return (error);
1498e3fbbf96SGleb Smirnoff }
1499e3fbbf96SGleb Smirnoff }
15001093f164SGleb Smirnoff
1501458f475dSGleb Smirnoff if (sb == NULL)
1502458f475dSGleb Smirnoff sb = &so->so_rcv;
1503458f475dSGleb Smirnoff else if (m == NULL)
1504458f475dSGleb Smirnoff m = STAILQ_FIRST(&sb->uxdg_mb);
1505458f475dSGleb Smirnoff else
1506458f475dSGleb Smirnoff MPASS(m == so->so_rcv.uxdg_peeked);
1507458f475dSGleb Smirnoff
1508458f475dSGleb Smirnoff MPASS(sb->uxdg_cc > 0);
15091093f164SGleb Smirnoff M_ASSERTPKTHDR(m);
15101093f164SGleb Smirnoff KASSERT(m->m_type == MT_SONAME, ("m->m_type == %d", m->m_type));
1511e3fbbf96SGleb Smirnoff
1512e3fbbf96SGleb Smirnoff if (uio->uio_td)
1513e3fbbf96SGleb Smirnoff uio->uio_td->td_ru.ru_msgrcv++;
1514e3fbbf96SGleb Smirnoff
1515458f475dSGleb Smirnoff if (__predict_true(m != so->so_rcv.uxdg_peeked)) {
1516458f475dSGleb Smirnoff STAILQ_REMOVE_HEAD(&sb->uxdg_mb, m_stailqpkt);
1517458f475dSGleb Smirnoff if (STAILQ_EMPTY(&sb->uxdg_mb) && sb != &so->so_rcv)
1518458f475dSGleb Smirnoff TAILQ_REMOVE(&so->so_rcv.uxdg_conns, sb, uxdg_clist);
1519458f475dSGleb Smirnoff } else
1520458f475dSGleb Smirnoff so->so_rcv.uxdg_peeked = NULL;
1521e3fbbf96SGleb Smirnoff
1522458f475dSGleb Smirnoff sb->uxdg_cc -= m->m_pkthdr.len;
1523458f475dSGleb Smirnoff sb->uxdg_ctl -= m->m_pkthdr.ctllen;
1524458f475dSGleb Smirnoff sb->uxdg_mbcnt -= m->m_pkthdr.memlen;
1525458f475dSGleb Smirnoff
1526458f475dSGleb Smirnoff if (__predict_false(flags & MSG_PEEK))
1527458f475dSGleb Smirnoff return (uipc_peek_dgram(so, m, psa, uio, controlp, flagsp));
1528458f475dSGleb Smirnoff
15291093f164SGleb Smirnoff so->so_rcv.sb_acc -= m->m_pkthdr.len;
15301093f164SGleb Smirnoff so->so_rcv.sb_ccc -= m->m_pkthdr.len;
15311093f164SGleb Smirnoff so->so_rcv.sb_ctl -= m->m_pkthdr.ctllen;
15321093f164SGleb Smirnoff so->so_rcv.sb_mbcnt -= m->m_pkthdr.memlen;
1533a7444f80SGleb Smirnoff SOCK_RECVBUF_UNLOCK(so);
1534e3fbbf96SGleb Smirnoff
1535e3fbbf96SGleb Smirnoff if (psa != NULL)
1536e3fbbf96SGleb Smirnoff *psa = sodupsockaddr(mtod(m, struct sockaddr *), M_WAITOK);
1537e3fbbf96SGleb Smirnoff m = m_free(m);
1538a7444f80SGleb Smirnoff KASSERT(m, ("%s: no data or control after soname", __func__));
1539e3fbbf96SGleb Smirnoff
1540e3fbbf96SGleb Smirnoff /*
1541e3fbbf96SGleb Smirnoff * Packet to copyout() is now in 'm' and it is disconnected from the
1542e3fbbf96SGleb Smirnoff * queue.
1543e3fbbf96SGleb Smirnoff *
1544e3fbbf96SGleb Smirnoff * Process one or more MT_CONTROL mbufs present before any data mbufs
1545e3fbbf96SGleb Smirnoff * in the first mbuf chain on the socket buffer. We call into the
1546e3fbbf96SGleb Smirnoff * unp_externalize() to perform externalization (or freeing if
1547e3fbbf96SGleb Smirnoff * controlp == NULL). In some cases there can be only MT_CONTROL mbufs
1548e3fbbf96SGleb Smirnoff * without MT_DATA mbufs.
1549e3fbbf96SGleb Smirnoff */
1550e3fbbf96SGleb Smirnoff while (m != NULL && m->m_type == MT_CONTROL) {
1551e3fbbf96SGleb Smirnoff struct mbuf *cm;
1552e3fbbf96SGleb Smirnoff
1553e3fbbf96SGleb Smirnoff /* XXXGL: unp_externalize() is also dom_externalize() KBI and
1554e3fbbf96SGleb Smirnoff * it frees whole chain, so we must disconnect the mbuf.
1555e3fbbf96SGleb Smirnoff */
1556e3fbbf96SGleb Smirnoff cm = m; m = m->m_next; cm->m_next = NULL;
1557e3fbbf96SGleb Smirnoff error = unp_externalize(cm, controlp, flags);
1558e3fbbf96SGleb Smirnoff if (error != 0) {
1559e3fbbf96SGleb Smirnoff SOCK_IO_RECV_UNLOCK(so);
1560e3fbbf96SGleb Smirnoff unp_scan(m, unp_freerights);
1561e3fbbf96SGleb Smirnoff m_freem(m);
1562e3fbbf96SGleb Smirnoff return (error);
1563e3fbbf96SGleb Smirnoff }
1564e3fbbf96SGleb Smirnoff if (controlp != NULL) {
1565e3fbbf96SGleb Smirnoff while (*controlp != NULL)
1566e3fbbf96SGleb Smirnoff controlp = &(*controlp)->m_next;
1567e3fbbf96SGleb Smirnoff }
1568e3fbbf96SGleb Smirnoff }
1569e3fbbf96SGleb Smirnoff KASSERT(m == NULL || m->m_type == MT_DATA,
1570e3fbbf96SGleb Smirnoff ("%s: not MT_DATA mbuf %p", __func__, m));
1571e3fbbf96SGleb Smirnoff while (m != NULL && uio->uio_resid > 0) {
1572e3fbbf96SGleb Smirnoff len = uio->uio_resid;
1573e3fbbf96SGleb Smirnoff if (len > m->m_len)
1574e3fbbf96SGleb Smirnoff len = m->m_len;
1575e3fbbf96SGleb Smirnoff error = uiomove(mtod(m, char *), (int)len, uio);
1576e3fbbf96SGleb Smirnoff if (error) {
1577e3fbbf96SGleb Smirnoff SOCK_IO_RECV_UNLOCK(so);
1578e3fbbf96SGleb Smirnoff m_freem(m);
1579e3fbbf96SGleb Smirnoff return (error);
1580e3fbbf96SGleb Smirnoff }
1581e3fbbf96SGleb Smirnoff if (len == m->m_len)
1582e3fbbf96SGleb Smirnoff m = m_free(m);
1583e3fbbf96SGleb Smirnoff else {
1584e3fbbf96SGleb Smirnoff m->m_data += len;
1585e3fbbf96SGleb Smirnoff m->m_len -= len;
1586e3fbbf96SGleb Smirnoff }
1587e3fbbf96SGleb Smirnoff }
1588e3fbbf96SGleb Smirnoff SOCK_IO_RECV_UNLOCK(so);
1589e3fbbf96SGleb Smirnoff
1590e3fbbf96SGleb Smirnoff if (m != NULL) {
1591be1f485dSAlexander V. Chernikov if (flagsp != NULL) {
1592be1f485dSAlexander V. Chernikov if (flags & MSG_TRUNC) {
1593be1f485dSAlexander V. Chernikov /* Report real length of the packet */
1594be1f485dSAlexander V. Chernikov uio->uio_resid -= m_length(m, NULL);
1595e3fbbf96SGleb Smirnoff }
1596be1f485dSAlexander V. Chernikov *flagsp |= MSG_TRUNC;
1597be1f485dSAlexander V. Chernikov }
1598be1f485dSAlexander V. Chernikov m_freem(m);
1599be1f485dSAlexander V. Chernikov } else if (flagsp != NULL)
1600be1f485dSAlexander V. Chernikov *flagsp &= ~MSG_TRUNC;
1601e3fbbf96SGleb Smirnoff
1602e3fbbf96SGleb Smirnoff return (0);
1603e3fbbf96SGleb Smirnoff }
1604e3fbbf96SGleb Smirnoff
1605a50b1900SMark Johnston static bool
uipc_ready_scan(struct socket * so,struct mbuf * m,int count,int * errorp)1606a50b1900SMark Johnston uipc_ready_scan(struct socket *so, struct mbuf *m, int count, int *errorp)
1607a50b1900SMark Johnston {
1608a50b1900SMark Johnston struct mbuf *mb, *n;
1609a50b1900SMark Johnston struct sockbuf *sb;
1610a50b1900SMark Johnston
1611a50b1900SMark Johnston SOCK_LOCK(so);
1612a50b1900SMark Johnston if (SOLISTENING(so)) {
1613a50b1900SMark Johnston SOCK_UNLOCK(so);
1614a50b1900SMark Johnston return (false);
1615a50b1900SMark Johnston }
1616a50b1900SMark Johnston mb = NULL;
1617a50b1900SMark Johnston sb = &so->so_rcv;
1618a50b1900SMark Johnston SOCKBUF_LOCK(sb);
1619a50b1900SMark Johnston if (sb->sb_fnrdy != NULL) {
1620a50b1900SMark Johnston for (mb = sb->sb_mb, n = mb->m_nextpkt; mb != NULL;) {
1621a50b1900SMark Johnston if (mb == m) {
1622a50b1900SMark Johnston *errorp = sbready(sb, m, count);
1623a50b1900SMark Johnston break;
1624a50b1900SMark Johnston }
1625a50b1900SMark Johnston mb = mb->m_next;
1626a50b1900SMark Johnston if (mb == NULL) {
1627a50b1900SMark Johnston mb = n;
16281b778ba2SMark Johnston if (mb != NULL)
1629a50b1900SMark Johnston n = mb->m_nextpkt;
1630a50b1900SMark Johnston }
1631a50b1900SMark Johnston }
1632a50b1900SMark Johnston }
1633a50b1900SMark Johnston SOCKBUF_UNLOCK(sb);
1634a50b1900SMark Johnston SOCK_UNLOCK(so);
1635a50b1900SMark Johnston return (mb != NULL);
1636a50b1900SMark Johnston }
1637a50b1900SMark Johnston
1638a29f300eSGarrett Wollman static int
uipc_ready(struct socket * so,struct mbuf * m,int count)1639c80ea19bSGleb Smirnoff uipc_ready(struct socket *so, struct mbuf *m, int count)
1640c80ea19bSGleb Smirnoff {
1641c80ea19bSGleb Smirnoff struct unpcb *unp, *unp2;
1642c80ea19bSGleb Smirnoff struct socket *so2;
1643a50b1900SMark Johnston int error, i;
1644c80ea19bSGleb Smirnoff
1645c80ea19bSGleb Smirnoff unp = sotounpcb(so);
1646c80ea19bSGleb Smirnoff
1647a50b1900SMark Johnston KASSERT(so->so_type == SOCK_STREAM,
1648a50b1900SMark Johnston ("%s: unexpected socket type for %p", __func__, so));
1649a50b1900SMark Johnston
1650a62b4665SMatt Macy UNP_PCB_LOCK(unp);
1651ccdadf1aSMark Johnston if ((unp2 = unp_pcb_lock_peer(unp)) != NULL) {
1652a62b4665SMatt Macy UNP_PCB_UNLOCK(unp);
1653c80ea19bSGleb Smirnoff so2 = unp2->unp_socket;
1654c80ea19bSGleb Smirnoff SOCKBUF_LOCK(&so2->so_rcv);
1655c80ea19bSGleb Smirnoff if ((error = sbready(&so2->so_rcv, m, count)) == 0)
1656c80ea19bSGleb Smirnoff sorwakeup_locked(so2);
1657c80ea19bSGleb Smirnoff else
1658c80ea19bSGleb Smirnoff SOCKBUF_UNLOCK(&so2->so_rcv);
1659c80ea19bSGleb Smirnoff UNP_PCB_UNLOCK(unp2);
1660c80ea19bSGleb Smirnoff return (error);
1661ccdadf1aSMark Johnston }
1662ccdadf1aSMark Johnston UNP_PCB_UNLOCK(unp);
1663a50b1900SMark Johnston
1664a50b1900SMark Johnston /*
1665a50b1900SMark Johnston * The receiving socket has been disconnected, but may still be valid.
1666a50b1900SMark Johnston * In this case, the now-ready mbufs are still present in its socket
1667a50b1900SMark Johnston * buffer, so perform an exhaustive search before giving up and freeing
1668a50b1900SMark Johnston * the mbufs.
1669a50b1900SMark Johnston */
1670a50b1900SMark Johnston UNP_LINK_RLOCK();
1671a50b1900SMark Johnston LIST_FOREACH(unp, &unp_shead, unp_link) {
1672a50b1900SMark Johnston if (uipc_ready_scan(unp->unp_socket, m, count, &error))
1673a50b1900SMark Johnston break;
1674a50b1900SMark Johnston }
1675a50b1900SMark Johnston UNP_LINK_RUNLOCK();
1676a50b1900SMark Johnston
1677a50b1900SMark Johnston if (unp == NULL) {
1678a50b1900SMark Johnston for (i = 0; i < count; i++)
1679a62b4665SMatt Macy m = m_free(m);
1680a50b1900SMark Johnston error = ECONNRESET;
1681a50b1900SMark Johnston }
1682a50b1900SMark Johnston return (error);
1683c80ea19bSGleb Smirnoff }
1684c80ea19bSGleb Smirnoff
1685c80ea19bSGleb Smirnoff static int
uipc_sense(struct socket * so,struct stat * sb)1686a29f300eSGarrett Wollman uipc_sense(struct socket *so, struct stat *sb)
1687a29f300eSGarrett Wollman {
1688c2090e73SAlan Somers struct unpcb *unp;
1689a29f300eSGarrett Wollman
169040f2ac28SRobert Watson unp = sotounpcb(so);
16914d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_sense: unp == NULL"));
1692e7c33e29SRobert Watson
1693a29f300eSGarrett Wollman sb->st_blksize = so->so_snd.sb_hiwat;
1694f3732fd1SPoul-Henning Kamp sb->st_dev = NODEV;
1695a29f300eSGarrett Wollman sb->st_ino = unp->unp_ino;
1696df8bae1dSRodney W. Grimes return (0);
1697a29f300eSGarrett Wollman }
1698df8bae1dSRodney W. Grimes
1699a29f300eSGarrett Wollman static int
uipc_shutdown(struct socket * so,enum shutdown_how how)17005bba2728SGleb Smirnoff uipc_shutdown(struct socket *so, enum shutdown_how how)
1701a29f300eSGarrett Wollman {
17025bba2728SGleb Smirnoff struct unpcb *unp = sotounpcb(so);
17035bba2728SGleb Smirnoff int error;
1704df8bae1dSRodney W. Grimes
17055bba2728SGleb Smirnoff SOCK_LOCK(so);
1706abe8379bSGleb Smirnoff if (SOLISTENING(so)) {
1707abe8379bSGleb Smirnoff if (how != SHUT_WR) {
1708abe8379bSGleb Smirnoff so->so_error = ECONNABORTED;
1709abe8379bSGleb Smirnoff solisten_wakeup(so); /* unlocks so */
1710abe8379bSGleb Smirnoff } else
1711abe8379bSGleb Smirnoff SOCK_UNLOCK(so);
1712abe8379bSGleb Smirnoff return (ENOTCONN);
1713abe8379bSGleb Smirnoff } else if ((so->so_state &
17145bba2728SGleb Smirnoff (SS_ISCONNECTED | SS_ISCONNECTING | SS_ISDISCONNECTING)) == 0) {
17155bba2728SGleb Smirnoff /*
17165bba2728SGleb Smirnoff * POSIX mandates us to just return ENOTCONN when shutdown(2) is
17175bba2728SGleb Smirnoff * invoked on a datagram sockets, however historically we would
17185bba2728SGleb Smirnoff * actually tear socket down. This is known to be leveraged by
17195bba2728SGleb Smirnoff * some applications to unblock process waiting in recv(2) by
17205bba2728SGleb Smirnoff * other process that it shares that socket with. Try to meet
17215bba2728SGleb Smirnoff * both backward-compatibility and POSIX requirements by forcing
17225bba2728SGleb Smirnoff * ENOTCONN but still flushing buffers and performing wakeup(9).
17235bba2728SGleb Smirnoff *
17245bba2728SGleb Smirnoff * XXXGL: it remains unknown what applications expect this
17255bba2728SGleb Smirnoff * behavior and is this isolated to unix/dgram or inet/dgram or
17265bba2728SGleb Smirnoff * both. See: D10351, D3039.
17275bba2728SGleb Smirnoff */
17285bba2728SGleb Smirnoff error = ENOTCONN;
17295bba2728SGleb Smirnoff if (so->so_type != SOCK_DGRAM) {
17305bba2728SGleb Smirnoff SOCK_UNLOCK(so);
17315bba2728SGleb Smirnoff return (error);
17325bba2728SGleb Smirnoff }
17335bba2728SGleb Smirnoff } else
17345bba2728SGleb Smirnoff error = 0;
17355bba2728SGleb Smirnoff SOCK_UNLOCK(so);
1736e7c33e29SRobert Watson
17375bba2728SGleb Smirnoff switch (how) {
17385bba2728SGleb Smirnoff case SHUT_RD:
1739289bee16SGleb Smirnoff socantrcvmore(so);
1740289bee16SGleb Smirnoff unp_dispose(so);
17415bba2728SGleb Smirnoff break;
17425bba2728SGleb Smirnoff case SHUT_RDWR:
1743289bee16SGleb Smirnoff socantrcvmore(so);
1744289bee16SGleb Smirnoff unp_dispose(so);
17455bba2728SGleb Smirnoff /* FALLTHROUGH */
17465bba2728SGleb Smirnoff case SHUT_WR:
1747e7c33e29SRobert Watson UNP_PCB_LOCK(unp);
1748a29f300eSGarrett Wollman socantsendmore(so);
1749a29f300eSGarrett Wollman unp_shutdown(unp);
1750e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp);
17515bba2728SGleb Smirnoff }
17525bba2728SGleb Smirnoff wakeup(&so->so_timeo);
17535bba2728SGleb Smirnoff
17545bba2728SGleb Smirnoff return (error);
1755a29f300eSGarrett Wollman }
1756df8bae1dSRodney W. Grimes
1757a29f300eSGarrett Wollman static int
uipc_sockaddr(struct socket * so,struct sockaddr * ret)17580fac350cSGleb Smirnoff uipc_sockaddr(struct socket *so, struct sockaddr *ret)
1759a29f300eSGarrett Wollman {
176040f2ac28SRobert Watson struct unpcb *unp;
17610d9ce3a1SRobert Watson const struct sockaddr *sa;
1762a29f300eSGarrett Wollman
17634d4b555eSRobert Watson unp = sotounpcb(so);
17644d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_sockaddr: unp == NULL"));
1765e7c33e29SRobert Watson
1766e7c33e29SRobert Watson UNP_PCB_LOCK(unp);
1767fc3fcacfSRobert Watson if (unp->unp_addr != NULL)
17680d9ce3a1SRobert Watson sa = (struct sockaddr *) unp->unp_addr;
176983f3198bSThomas Moestl else
17700d9ce3a1SRobert Watson sa = &sun_noname;
17710fac350cSGleb Smirnoff bcopy(sa, ret, sa->sa_len);
1772e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp);
1773e5aeaa0cSDag-Erling Smørgrav return (0);
1774df8bae1dSRodney W. Grimes }
1775a29f300eSGarrett Wollman
17760b36cd25SRobert Watson static int
uipc_ctloutput(struct socket * so,struct sockopt * sopt)1777892af6b9SRobert Watson uipc_ctloutput(struct socket *so, struct sockopt *sopt)
17780c1bb4fbSDima Dorfman {
177940f2ac28SRobert Watson struct unpcb *unp;
17800d9ce3a1SRobert Watson struct xucred xu;
17816a2989fdSMatthew N. Dodd int error, optval;
17826a2989fdSMatthew N. Dodd
17836e0c8e1aSKonstantin Belousov if (sopt->sopt_level != SOL_LOCAL)
178496a041b5SMatthew N. Dodd return (EINVAL);
178596a041b5SMatthew N. Dodd
17866a2989fdSMatthew N. Dodd unp = sotounpcb(so);
17874d4b555eSRobert Watson KASSERT(unp != NULL, ("uipc_ctloutput: unp == NULL"));
17886a2989fdSMatthew N. Dodd error = 0;
17890c1bb4fbSDima Dorfman switch (sopt->sopt_dir) {
17900c1bb4fbSDima Dorfman case SOPT_GET:
17910c1bb4fbSDima Dorfman switch (sopt->sopt_name) {
17920c1bb4fbSDima Dorfman case LOCAL_PEERCRED:
1793e7c33e29SRobert Watson UNP_PCB_LOCK(unp);
17940c1bb4fbSDima Dorfman if (unp->unp_flags & UNP_HAVEPC)
17950d9ce3a1SRobert Watson xu = unp->unp_peercred;
17960c1bb4fbSDima Dorfman else {
17971c933f46SMark Johnston if (so->so_proto->pr_flags & PR_CONNREQUIRED)
17980c1bb4fbSDima Dorfman error = ENOTCONN;
17990c1bb4fbSDima Dorfman else
18000c1bb4fbSDima Dorfman error = EINVAL;
18010c1bb4fbSDima Dorfman }
1802e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp);
18030d9ce3a1SRobert Watson if (error == 0)
18040d9ce3a1SRobert Watson error = sooptcopyout(sopt, &xu, sizeof(xu));
18050c1bb4fbSDima Dorfman break;
1806e7c33e29SRobert Watson
18076a2989fdSMatthew N. Dodd case LOCAL_CREDS:
1808a6357845SRobert Watson /* Unlocked read. */
18092de07e40SConrad Meyer optval = unp->unp_flags & UNP_WANTCRED_ONESHOT ? 1 : 0;
18102de07e40SConrad Meyer error = sooptcopyout(sopt, &optval, sizeof(optval));
18112de07e40SConrad Meyer break;
18122de07e40SConrad Meyer
18132de07e40SConrad Meyer case LOCAL_CREDS_PERSISTENT:
18142de07e40SConrad Meyer /* Unlocked read. */
18152de07e40SConrad Meyer optval = unp->unp_flags & UNP_WANTCRED_ALWAYS ? 1 : 0;
18166a2989fdSMatthew N. Dodd error = sooptcopyout(sopt, &optval, sizeof(optval));
18176a2989fdSMatthew N. Dodd break;
1818e7c33e29SRobert Watson
18190c1bb4fbSDima Dorfman default:
18200c1bb4fbSDima Dorfman error = EOPNOTSUPP;
18210c1bb4fbSDima Dorfman break;
18220c1bb4fbSDima Dorfman }
18230c1bb4fbSDima Dorfman break;
1824e7c33e29SRobert Watson
18250c1bb4fbSDima Dorfman case SOPT_SET:
18266a2989fdSMatthew N. Dodd switch (sopt->sopt_name) {
18276a2989fdSMatthew N. Dodd case LOCAL_CREDS:
18282de07e40SConrad Meyer case LOCAL_CREDS_PERSISTENT:
18296a2989fdSMatthew N. Dodd error = sooptcopyin(sopt, &optval, sizeof(optval),
18306a2989fdSMatthew N. Dodd sizeof(optval));
18316a2989fdSMatthew N. Dodd if (error)
18326a2989fdSMatthew N. Dodd break;
18336a2989fdSMatthew N. Dodd
18342de07e40SConrad Meyer #define OPTSET(bit, exclusive) do { \
1835e7c33e29SRobert Watson UNP_PCB_LOCK(unp); \
18362de07e40SConrad Meyer if (optval) { \
18372de07e40SConrad Meyer if ((unp->unp_flags & (exclusive)) != 0) { \
18382de07e40SConrad Meyer UNP_PCB_UNLOCK(unp); \
18392de07e40SConrad Meyer error = EINVAL; \
18402de07e40SConrad Meyer break; \
18412de07e40SConrad Meyer } \
18422de07e40SConrad Meyer unp->unp_flags |= (bit); \
18432de07e40SConrad Meyer } else \
18442de07e40SConrad Meyer unp->unp_flags &= ~(bit); \
1845e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp); \
1846e7c33e29SRobert Watson } while (0)
18476a2989fdSMatthew N. Dodd
18486a2989fdSMatthew N. Dodd switch (sopt->sopt_name) {
18496a2989fdSMatthew N. Dodd case LOCAL_CREDS:
18502de07e40SConrad Meyer OPTSET(UNP_WANTCRED_ONESHOT, UNP_WANTCRED_ALWAYS);
18512de07e40SConrad Meyer break;
18522de07e40SConrad Meyer
18532de07e40SConrad Meyer case LOCAL_CREDS_PERSISTENT:
18542de07e40SConrad Meyer OPTSET(UNP_WANTCRED_ALWAYS, UNP_WANTCRED_ONESHOT);
18556a2989fdSMatthew N. Dodd break;
1856e7c33e29SRobert Watson
18576a2989fdSMatthew N. Dodd default:
18586a2989fdSMatthew N. Dodd break;
18596a2989fdSMatthew N. Dodd }
18606a2989fdSMatthew N. Dodd break;
18616a2989fdSMatthew N. Dodd #undef OPTSET
18626a2989fdSMatthew N. Dodd default:
18636a2989fdSMatthew N. Dodd error = ENOPROTOOPT;
18646a2989fdSMatthew N. Dodd break;
18656a2989fdSMatthew N. Dodd }
1866abb886faSMatthew N. Dodd break;
1867e7c33e29SRobert Watson
18680c1bb4fbSDima Dorfman default:
18690c1bb4fbSDima Dorfman error = EOPNOTSUPP;
18700c1bb4fbSDima Dorfman break;
18710c1bb4fbSDima Dorfman }
18720c1bb4fbSDima Dorfman return (error);
18730c1bb4fbSDima Dorfman }
18740c1bb4fbSDima Dorfman
1875f708ef1bSPoul-Henning Kamp static int
unp_connect(struct socket * so,struct sockaddr * nam,struct thread * td)1876892af6b9SRobert Watson unp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
1877df8bae1dSRodney W. Grimes {
18787493f24eSPawel Jakub Dawidek
1879315167c0SGleb Smirnoff return (unp_connectat(AT_FDCWD, so, nam, td, false));
18807493f24eSPawel Jakub Dawidek }
18817493f24eSPawel Jakub Dawidek
18827493f24eSPawel Jakub Dawidek static int
unp_connectat(int fd,struct socket * so,struct sockaddr * nam,struct thread * td,bool return_locked)18837493f24eSPawel Jakub Dawidek unp_connectat(int fd, struct socket *so, struct sockaddr *nam,
1884315167c0SGleb Smirnoff struct thread *td, bool return_locked)
18857493f24eSPawel Jakub Dawidek {
1886ed92e1c7SMark Johnston struct mtx *vplock;
1887ed92e1c7SMark Johnston struct sockaddr_un *soun;
1888892af6b9SRobert Watson struct vnode *vp;
1889779f106aSGleb Smirnoff struct socket *so2;
1890b295bdcdSRobert Watson struct unpcb *unp, *unp2, *unp3;
1891df8bae1dSRodney W. Grimes struct nameidata nd;
189257bf258eSGarrett Wollman char buf[SOCK_MAXADDRLEN];
18930d9ce3a1SRobert Watson struct sockaddr *sa;
18947008be5bSPawel Jakub Dawidek cap_rights_t rights;
1895ccdadf1aSMark Johnston int error, len;
1896ed92e1c7SMark Johnston bool connreq;
18970d9ce3a1SRobert Watson
1898*4c0e435bSGleb Smirnoff CURVNET_ASSERT_SET();
1899*4c0e435bSGleb Smirnoff
1900cb7df69bSKevin Lo if (nam->sa_family != AF_UNIX)
1901cb7df69bSKevin Lo return (EAFNOSUPPORT);
1902a06534c3SBjoern A. Zeeb if (nam->sa_len > sizeof(struct sockaddr_un))
1903a06534c3SBjoern A. Zeeb return (EINVAL);
190457bf258eSGarrett Wollman len = nam->sa_len - offsetof(struct sockaddr_un, sun_path);
190557bf258eSGarrett Wollman if (len <= 0)
1906e5aeaa0cSDag-Erling Smørgrav return (EINVAL);
1907ed92e1c7SMark Johnston soun = (struct sockaddr_un *)nam;
19087928893dSEd Maste bcopy(soun->sun_path, buf, len);
19097928893dSEd Maste buf[len] = 0;
1910e7c33e29SRobert Watson
1911bd4a39ccSMark Johnston error = 0;
191275a67bf3SMatt Macy unp = sotounpcb(so);
1913e7c33e29SRobert Watson UNP_PCB_LOCK(unp);
1914ccdadf1aSMark Johnston for (;;) {
1915ccdadf1aSMark Johnston /*
1916ccdadf1aSMark Johnston * Wait for connection state to stabilize. If a connection
1917ccdadf1aSMark Johnston * already exists, give up. For datagram sockets, which permit
1918ccdadf1aSMark Johnston * multiple consecutive connect(2) calls, upper layers are
1919ccdadf1aSMark Johnston * responsible for disconnecting in advance of a subsequent
1920ccdadf1aSMark Johnston * connect(2), but this is not synchronized with PCB connection
1921ccdadf1aSMark Johnston * state.
1922ccdadf1aSMark Johnston *
1923ccdadf1aSMark Johnston * Also make sure that no threads are currently attempting to
1924ccdadf1aSMark Johnston * lock the peer socket, to ensure that unp_conn cannot
1925ccdadf1aSMark Johnston * transition between two valid sockets while locks are dropped.
1926ccdadf1aSMark Johnston */
1927bd4a39ccSMark Johnston if (SOLISTENING(so))
1928bd4a39ccSMark Johnston error = EOPNOTSUPP;
1929bd4a39ccSMark Johnston else if (unp->unp_conn != NULL)
1930bd4a39ccSMark Johnston error = EISCONN;
1931bd4a39ccSMark Johnston else if ((unp->unp_flags & UNP_CONNECTING) != 0) {
1932bd4a39ccSMark Johnston error = EALREADY;
1933ccdadf1aSMark Johnston }
1934bd4a39ccSMark Johnston if (error != 0) {
1935e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp);
1936bd4a39ccSMark Johnston return (error);
19374f1f0ef5SRobert Watson }
1938ccdadf1aSMark Johnston if (unp->unp_pairbusy > 0) {
1939ccdadf1aSMark Johnston unp->unp_flags |= UNP_WAITING;
1940ccdadf1aSMark Johnston mtx_sleep(unp, UNP_PCB_LOCKPTR(unp), 0, "unpeer", 0);
1941ccdadf1aSMark Johnston continue;
1942ccdadf1aSMark Johnston }
1943ccdadf1aSMark Johnston break;
1944ccdadf1aSMark Johnston }
194505102f04SRobert Watson unp->unp_flags |= UNP_CONNECTING;
1946e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp);
1947e7c33e29SRobert Watson
1948ed92e1c7SMark Johnston connreq = (so->so_proto->pr_flags & PR_CONNREQUIRED) != 0;
1949ed92e1c7SMark Johnston if (connreq)
19500d9ce3a1SRobert Watson sa = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK);
1951ed92e1c7SMark Johnston else
1952ed92e1c7SMark Johnston sa = NULL;
19537493f24eSPawel Jakub Dawidek NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF,
19547e1d3eefSMateusz Guzik UIO_SYSSPACE, buf, fd, cap_rights_init_one(&rights, CAP_CONNECTAT));
1955797f2d22SPoul-Henning Kamp error = namei(&nd);
1956797f2d22SPoul-Henning Kamp if (error)
19570d9ce3a1SRobert Watson vp = NULL;
19580d9ce3a1SRobert Watson else
1959df8bae1dSRodney W. Grimes vp = nd.ni_vp;
19600d9ce3a1SRobert Watson ASSERT_VOP_LOCKED(vp, "unp_connect");
19610d9ce3a1SRobert Watson if (error)
19620d9ce3a1SRobert Watson goto bad;
19635b5b7e2cSMateusz Guzik NDFREE_PNBUF(&nd);
19640d9ce3a1SRobert Watson
1965df8bae1dSRodney W. Grimes if (vp->v_type != VSOCK) {
1966df8bae1dSRodney W. Grimes error = ENOTSOCK;
1967df8bae1dSRodney W. Grimes goto bad;
1968df8bae1dSRodney W. Grimes }
19696fac927cSRobert Watson #ifdef MAC
197030d239bcSRobert Watson error = mac_vnode_check_open(td->td_ucred, vp, VWRITE | VREAD);
19716fac927cSRobert Watson if (error)
19726fac927cSRobert Watson goto bad;
19736fac927cSRobert Watson #endif
1974a854ed98SJohn Baldwin error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td);
1975797f2d22SPoul-Henning Kamp if (error)
1976df8bae1dSRodney W. Grimes goto bad;
1977e7c33e29SRobert Watson
1978b295bdcdSRobert Watson unp = sotounpcb(so);
19794d4b555eSRobert Watson KASSERT(unp != NULL, ("unp_connect: unp == NULL"));
1980e7c33e29SRobert Watson
1981c08e016fSJohn Baldwin vplock = mtx_pool_find(unp_vp_mtxpool, vp);
198275a67bf3SMatt Macy mtx_lock(vplock);
19830c3c207fSGleb Smirnoff VOP_UNP_CONNECT(vp, &unp2);
19840c3c207fSGleb Smirnoff if (unp2 == NULL) {
1985df8bae1dSRodney W. Grimes error = ECONNREFUSED;
19862260c03dSRobert Watson goto bad2;
1987df8bae1dSRodney W. Grimes }
19880c3c207fSGleb Smirnoff so2 = unp2->unp_socket;
1989df8bae1dSRodney W. Grimes if (so->so_type != so2->so_type) {
1990df8bae1dSRodney W. Grimes error = EPROTOTYPE;
19912260c03dSRobert Watson goto bad2;
1992df8bae1dSRodney W. Grimes }
1993ed92e1c7SMark Johnston if (connreq) {
1994*4c0e435bSGleb Smirnoff if (SOLISTENING(so2))
1995779f106aSGleb Smirnoff so2 = sonewconn(so2, 0);
1996*4c0e435bSGleb Smirnoff else
1997779f106aSGleb Smirnoff so2 = NULL;
1998779f106aSGleb Smirnoff if (so2 == NULL) {
1999df8bae1dSRodney W. Grimes error = ECONNREFUSED;
2000cb8f450bSMatt Macy goto bad2;
2001df8bae1dSRodney W. Grimes }
2002779f106aSGleb Smirnoff unp3 = sotounpcb(so2);
20034820bf6aSMark Johnston unp_pcb_lock_pair(unp2, unp3);
20040d9ce3a1SRobert Watson if (unp2->unp_addr != NULL) {
20050d9ce3a1SRobert Watson bcopy(unp2->unp_addr, sa, unp2->unp_addr->sun_len);
20060d9ce3a1SRobert Watson unp3->unp_addr = (struct sockaddr_un *) sa;
20070d9ce3a1SRobert Watson sa = NULL;
20080d9ce3a1SRobert Watson }
2009b523ec24SRobert Watson
2010da446550SAlan Somers unp_copy_peercred(td, unp3, unp, unp2);
2011b523ec24SRobert Watson
2012e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp2);
2013779f106aSGleb Smirnoff unp2 = unp3;
2014ccdadf1aSMark Johnston
2015ccdadf1aSMark Johnston /*
2016ccdadf1aSMark Johnston * It is safe to block on the PCB lock here since unp2 is
2017ccdadf1aSMark Johnston * nascent and cannot be connected to any other sockets.
2018ccdadf1aSMark Johnston */
2019ccdadf1aSMark Johnston UNP_PCB_LOCK(unp);
2020335654d7SRobert Watson #ifdef MAC
2021779f106aSGleb Smirnoff mac_socketpeer_set_from_socket(so, so2);
2022779f106aSGleb Smirnoff mac_socketpeer_set_from_socket(so2, so);
2023335654d7SRobert Watson #endif
2024a3a73490SMatt Macy } else {
20254820bf6aSMark Johnston unp_pcb_lock_pair(unp, unp2);
2026a3a73490SMatt Macy }
2027779f106aSGleb Smirnoff KASSERT(unp2 != NULL && so2 != NULL && unp2->unp_socket == so2 &&
2028779f106aSGleb Smirnoff sotounpcb(so2) == unp2,
2029779f106aSGleb Smirnoff ("%s: unp2 %p so2 %p", __func__, unp2, so2));
20300bd8eb3eSGleb Smirnoff unp_connect2(so, so2);
2031bb35a4e1SGleb Smirnoff KASSERT((unp->unp_flags & UNP_CONNECTING) != 0,
2032bb35a4e1SGleb Smirnoff ("%s: unp %p has UNP_CONNECTING clear", __func__, unp));
2033bb35a4e1SGleb Smirnoff unp->unp_flags &= ~UNP_CONNECTING;
2034315167c0SGleb Smirnoff if (!return_locked)
20354820bf6aSMark Johnston unp_pcb_unlock_pair(unp, unp2);
20360d9ce3a1SRobert Watson bad2:
203775a67bf3SMatt Macy mtx_unlock(vplock);
2038df8bae1dSRodney W. Grimes bad:
203975a67bf3SMatt Macy if (vp != NULL) {
2040315167c0SGleb Smirnoff /*
2041315167c0SGleb Smirnoff * If we are returning locked (called via uipc_sosend_dgram()),
2042315167c0SGleb Smirnoff * we need to be sure that vput() won't sleep. This is
2043315167c0SGleb Smirnoff * guaranteed by VOP_UNP_CONNECT() call above and unp2 lock.
2044315167c0SGleb Smirnoff * SOCK_STREAM/SEQPACKET can't request return_locked (yet).
2045315167c0SGleb Smirnoff */
2046315167c0SGleb Smirnoff MPASS(!(return_locked && connreq));
2047df8bae1dSRodney W. Grimes vput(vp);
204875a67bf3SMatt Macy }
20490d9ce3a1SRobert Watson free(sa, M_SONAME);
2050bb35a4e1SGleb Smirnoff if (__predict_false(error)) {
2051e7c33e29SRobert Watson UNP_PCB_LOCK(unp);
2052ccdadf1aSMark Johnston KASSERT((unp->unp_flags & UNP_CONNECTING) != 0,
2053ccdadf1aSMark Johnston ("%s: unp %p has UNP_CONNECTING clear", __func__, unp));
20544f1f0ef5SRobert Watson unp->unp_flags &= ~UNP_CONNECTING;
2055e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp);
2056bb35a4e1SGleb Smirnoff }
2057df8bae1dSRodney W. Grimes return (error);
2058df8bae1dSRodney W. Grimes }
2059df8bae1dSRodney W. Grimes
2060da446550SAlan Somers /*
2061da446550SAlan Somers * Set socket peer credentials at connection time.
2062da446550SAlan Somers *
2063da446550SAlan Somers * The client's PCB credentials are copied from its process structure. The
2064da446550SAlan Somers * server's PCB credentials are copied from the socket on which it called
2065da446550SAlan Somers * listen(2). uipc_listen cached that process's credentials at the time.
2066da446550SAlan Somers */
2067da446550SAlan Somers void
unp_copy_peercred(struct thread * td,struct unpcb * client_unp,struct unpcb * server_unp,struct unpcb * listen_unp)2068da446550SAlan Somers unp_copy_peercred(struct thread *td, struct unpcb *client_unp,
2069da446550SAlan Somers struct unpcb *server_unp, struct unpcb *listen_unp)
2070da446550SAlan Somers {
2071c5afec6eSDmitry Chagin cru2xt(td, &client_unp->unp_peercred);
2072da446550SAlan Somers client_unp->unp_flags |= UNP_HAVEPC;
2073da446550SAlan Somers
2074da446550SAlan Somers memcpy(&server_unp->unp_peercred, &listen_unp->unp_peercred,
2075da446550SAlan Somers sizeof(server_unp->unp_peercred));
2076da446550SAlan Somers server_unp->unp_flags |= UNP_HAVEPC;
20772de07e40SConrad Meyer client_unp->unp_flags |= (listen_unp->unp_flags & UNP_WANTCRED_MASK);
2078da446550SAlan Somers }
2079da446550SAlan Somers
208008f17d14SGleb Smirnoff static void
unp_connect2(struct socket * so,struct socket * so2)20810bd8eb3eSGleb Smirnoff unp_connect2(struct socket *so, struct socket *so2)
2082df8bae1dSRodney W. Grimes {
2083e7c33e29SRobert Watson struct unpcb *unp;
2084892af6b9SRobert Watson struct unpcb *unp2;
2085df8bae1dSRodney W. Grimes
208608f17d14SGleb Smirnoff MPASS(so2->so_type == so->so_type);
2087e7c33e29SRobert Watson unp = sotounpcb(so);
2088e7c33e29SRobert Watson KASSERT(unp != NULL, ("unp_connect2: unp == NULL"));
2089e7c33e29SRobert Watson unp2 = sotounpcb(so2);
2090e7c33e29SRobert Watson KASSERT(unp2 != NULL, ("unp_connect2: unp2 == NULL"));
2091e7c33e29SRobert Watson
2092e7c33e29SRobert Watson UNP_PCB_LOCK_ASSERT(unp);
2093e7c33e29SRobert Watson UNP_PCB_LOCK_ASSERT(unp2);
2094ccdadf1aSMark Johnston KASSERT(unp->unp_conn == NULL,
2095ccdadf1aSMark Johnston ("%s: socket %p is already connected", __func__, unp));
20960d9ce3a1SRobert Watson
2097df8bae1dSRodney W. Grimes unp->unp_conn = unp2;
209875a67bf3SMatt Macy unp_pcb_hold(unp2);
209975a67bf3SMatt Macy unp_pcb_hold(unp);
2100df8bae1dSRodney W. Grimes switch (so->so_type) {
2101df8bae1dSRodney W. Grimes case SOCK_DGRAM:
210275a67bf3SMatt Macy UNP_REF_LIST_LOCK();
210398271db4SGarrett Wollman LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink);
210475a67bf3SMatt Macy UNP_REF_LIST_UNLOCK();
2105df8bae1dSRodney W. Grimes soisconnected(so);
2106df8bae1dSRodney W. Grimes break;
2107df8bae1dSRodney W. Grimes
2108df8bae1dSRodney W. Grimes case SOCK_STREAM:
210984d61770SRobert Watson case SOCK_SEQPACKET:
2110ccdadf1aSMark Johnston KASSERT(unp2->unp_conn == NULL,
2111ccdadf1aSMark Johnston ("%s: socket %p is already connected", __func__, unp2));
2112df8bae1dSRodney W. Grimes unp2->unp_conn = unp;
2113df8bae1dSRodney W. Grimes soisconnected(so);
2114df8bae1dSRodney W. Grimes soisconnected(so2);
2115df8bae1dSRodney W. Grimes break;
2116df8bae1dSRodney W. Grimes
2117df8bae1dSRodney W. Grimes default:
2118df8bae1dSRodney W. Grimes panic("unp_connect2");
2119df8bae1dSRodney W. Grimes }
2120df8bae1dSRodney W. Grimes }
2121df8bae1dSRodney W. Grimes
2122f708ef1bSPoul-Henning Kamp static void
unp_disconnect(struct unpcb * unp,struct unpcb * unp2)2123e7c33e29SRobert Watson unp_disconnect(struct unpcb *unp, struct unpcb *unp2)
2124df8bae1dSRodney W. Grimes {
212575a67bf3SMatt Macy struct socket *so, *so2;
2126458f475dSGleb Smirnoff struct mbuf *m = NULL;
2127f0317f86SMark Johnston #ifdef INVARIANTS
2128f0317f86SMark Johnston struct unpcb *unptmp;
2129f0317f86SMark Johnston #endif
21300d9ce3a1SRobert Watson
2131e7c33e29SRobert Watson UNP_PCB_LOCK_ASSERT(unp);
2132e7c33e29SRobert Watson UNP_PCB_LOCK_ASSERT(unp2);
2133f0317f86SMark Johnston KASSERT(unp->unp_conn == unp2,
2134f0317f86SMark Johnston ("%s: unpcb %p is not connected to %p", __func__, unp, unp2));
2135e7c33e29SRobert Watson
2136fc3fcacfSRobert Watson unp->unp_conn = NULL;
213775a67bf3SMatt Macy so = unp->unp_socket;
213875a67bf3SMatt Macy so2 = unp2->unp_socket;
2139df8bae1dSRodney W. Grimes switch (unp->unp_socket->so_type) {
2140df8bae1dSRodney W. Grimes case SOCK_DGRAM:
2141458f475dSGleb Smirnoff /*
2142458f475dSGleb Smirnoff * Remove our send socket buffer from the peer's receive buffer.
2143458f475dSGleb Smirnoff * Move the data to the receive buffer only if it is empty.
2144458f475dSGleb Smirnoff * This is a protection against a scenario where a peer
2145458f475dSGleb Smirnoff * connects, floods and disconnects, effectively blocking
2146458f475dSGleb Smirnoff * sendto() from unconnected sockets.
2147458f475dSGleb Smirnoff */
2148458f475dSGleb Smirnoff SOCK_RECVBUF_LOCK(so2);
2149458f475dSGleb Smirnoff if (!STAILQ_EMPTY(&so->so_snd.uxdg_mb)) {
2150458f475dSGleb Smirnoff TAILQ_REMOVE(&so2->so_rcv.uxdg_conns, &so->so_snd,
2151458f475dSGleb Smirnoff uxdg_clist);
2152458f475dSGleb Smirnoff if (__predict_true((so2->so_rcv.sb_state &
2153458f475dSGleb Smirnoff SBS_CANTRCVMORE) == 0) &&
2154458f475dSGleb Smirnoff STAILQ_EMPTY(&so2->so_rcv.uxdg_mb)) {
2155458f475dSGleb Smirnoff STAILQ_CONCAT(&so2->so_rcv.uxdg_mb,
2156458f475dSGleb Smirnoff &so->so_snd.uxdg_mb);
2157458f475dSGleb Smirnoff so2->so_rcv.uxdg_cc += so->so_snd.uxdg_cc;
2158458f475dSGleb Smirnoff so2->so_rcv.uxdg_ctl += so->so_snd.uxdg_ctl;
2159458f475dSGleb Smirnoff so2->so_rcv.uxdg_mbcnt += so->so_snd.uxdg_mbcnt;
2160458f475dSGleb Smirnoff } else {
2161458f475dSGleb Smirnoff m = STAILQ_FIRST(&so->so_snd.uxdg_mb);
2162458f475dSGleb Smirnoff STAILQ_INIT(&so->so_snd.uxdg_mb);
2163458f475dSGleb Smirnoff so2->so_rcv.sb_acc -= so->so_snd.uxdg_cc;
2164458f475dSGleb Smirnoff so2->so_rcv.sb_ccc -= so->so_snd.uxdg_cc;
2165458f475dSGleb Smirnoff so2->so_rcv.sb_ctl -= so->so_snd.uxdg_ctl;
2166458f475dSGleb Smirnoff so2->so_rcv.sb_mbcnt -= so->so_snd.uxdg_mbcnt;
2167458f475dSGleb Smirnoff }
2168458f475dSGleb Smirnoff /* Note: so may reconnect. */
2169458f475dSGleb Smirnoff so->so_snd.uxdg_cc = 0;
2170458f475dSGleb Smirnoff so->so_snd.uxdg_ctl = 0;
2171458f475dSGleb Smirnoff so->so_snd.uxdg_mbcnt = 0;
2172458f475dSGleb Smirnoff }
2173458f475dSGleb Smirnoff SOCK_RECVBUF_UNLOCK(so2);
217475a67bf3SMatt Macy UNP_REF_LIST_LOCK();
2175f0317f86SMark Johnston #ifdef INVARIANTS
2176f0317f86SMark Johnston LIST_FOREACH(unptmp, &unp2->unp_refs, unp_reflink) {
2177f0317f86SMark Johnston if (unptmp == unp)
2178f0317f86SMark Johnston break;
2179f0317f86SMark Johnston }
2180f0317f86SMark Johnston KASSERT(unptmp != NULL,
2181f0317f86SMark Johnston ("%s: %p not found in reflist of %p", __func__, unp, unp2));
2182f0317f86SMark Johnston #endif
218398271db4SGarrett Wollman LIST_REMOVE(unp, unp_reflink);
218475a67bf3SMatt Macy UNP_REF_LIST_UNLOCK();
218575a67bf3SMatt Macy if (so) {
21861b2e3b4bSRobert Watson SOCK_LOCK(so);
21871b2e3b4bSRobert Watson so->so_state &= ~SS_ISCONNECTED;
21881b2e3b4bSRobert Watson SOCK_UNLOCK(so);
218975a67bf3SMatt Macy }
2190df8bae1dSRodney W. Grimes break;
2191df8bae1dSRodney W. Grimes
2192df8bae1dSRodney W. Grimes case SOCK_STREAM:
219384d61770SRobert Watson case SOCK_SEQPACKET:
21945716d902SGleb Smirnoff if (so)
21955716d902SGleb Smirnoff soisdisconnected(so);
219675a67bf3SMatt Macy MPASS(unp2->unp_conn == unp);
2197fc3fcacfSRobert Watson unp2->unp_conn = NULL;
21985716d902SGleb Smirnoff if (so2)
21995716d902SGleb Smirnoff soisdisconnected(so2);
2200df8bae1dSRodney W. Grimes break;
2201df8bae1dSRodney W. Grimes }
2202f0317f86SMark Johnston
2203f0317f86SMark Johnston if (unp == unp2) {
2204f0317f86SMark Johnston unp_pcb_rele_notlast(unp);
2205f0317f86SMark Johnston if (!unp_pcb_rele(unp))
2206f0317f86SMark Johnston UNP_PCB_UNLOCK(unp);
2207f0317f86SMark Johnston } else {
2208f0317f86SMark Johnston if (!unp_pcb_rele(unp))
2209f0317f86SMark Johnston UNP_PCB_UNLOCK(unp);
2210f0317f86SMark Johnston if (!unp_pcb_rele(unp2))
2211f0317f86SMark Johnston UNP_PCB_UNLOCK(unp2);
2212f0317f86SMark Johnston }
2213458f475dSGleb Smirnoff
2214458f475dSGleb Smirnoff if (m != NULL) {
2215458f475dSGleb Smirnoff unp_scan(m, unp_freerights);
2216a9b55a66SGleb Smirnoff m_freemp(m);
2217458f475dSGleb Smirnoff }
2218df8bae1dSRodney W. Grimes }
2219df8bae1dSRodney W. Grimes
22200d9ce3a1SRobert Watson /*
2221d7924b70SRobert Watson * unp_pcblist() walks the global list of struct unpcb's to generate a
2222d7924b70SRobert Watson * pointer list, bumping the refcount on each unpcb. It then copies them out
2223d7924b70SRobert Watson * sequentially, validating the generation number on each to see if it has
2224d7924b70SRobert Watson * been detached. All of this is necessary because copyout() may sleep on
2225d7924b70SRobert Watson * disk I/O.
22260d9ce3a1SRobert Watson */
222798271db4SGarrett Wollman static int
unp_pcblist(SYSCTL_HANDLER_ARGS)222882d9ae4eSPoul-Henning Kamp unp_pcblist(SYSCTL_HANDLER_ARGS)
222998271db4SGarrett Wollman {
223098271db4SGarrett Wollman struct unpcb *unp, **unp_list;
223198271db4SGarrett Wollman unp_gen_t gencnt;
22328f364875SJulian Elischer struct xunpgen *xug;
223398271db4SGarrett Wollman struct unp_head *head;
22348f364875SJulian Elischer struct xunpcb *xu;
2235d821d364SPedro F. Giffuni u_int i;
22365362170dSMark Johnston int error, n;
223798271db4SGarrett Wollman
223884d61770SRobert Watson switch ((intptr_t)arg1) {
223984d61770SRobert Watson case SOCK_STREAM:
224084d61770SRobert Watson head = &unp_shead;
224184d61770SRobert Watson break;
224284d61770SRobert Watson
224384d61770SRobert Watson case SOCK_DGRAM:
224484d61770SRobert Watson head = &unp_dhead;
224584d61770SRobert Watson break;
224684d61770SRobert Watson
224784d61770SRobert Watson case SOCK_SEQPACKET:
224884d61770SRobert Watson head = &unp_sphead;
224984d61770SRobert Watson break;
225084d61770SRobert Watson
225184d61770SRobert Watson default:
2252604f19c9SRobert Watson panic("unp_pcblist: arg1 %d", (int)(intptr_t)arg1);
225384d61770SRobert Watson }
225498271db4SGarrett Wollman
225598271db4SGarrett Wollman /*
225698271db4SGarrett Wollman * The process of preparing the PCB list is too time-consuming and
225798271db4SGarrett Wollman * resource-intensive to repeat twice on every request.
225898271db4SGarrett Wollman */
2259fc3fcacfSRobert Watson if (req->oldptr == NULL) {
226098271db4SGarrett Wollman n = unp_count;
22618f364875SJulian Elischer req->oldidx = 2 * (sizeof *xug)
226298271db4SGarrett Wollman + (n + n/8) * sizeof(struct xunpcb);
2263e5aeaa0cSDag-Erling Smørgrav return (0);
226498271db4SGarrett Wollman }
226598271db4SGarrett Wollman
2266fc3fcacfSRobert Watson if (req->newptr != NULL)
2267e5aeaa0cSDag-Erling Smørgrav return (EPERM);
226898271db4SGarrett Wollman
226998271db4SGarrett Wollman /*
227098271db4SGarrett Wollman * OK, now we're committed to doing something.
227198271db4SGarrett Wollman */
227279db6fe7SMark Johnston xug = malloc(sizeof(*xug), M_TEMP, M_WAITOK | M_ZERO);
2273779f106aSGleb Smirnoff UNP_LINK_RLOCK();
227498271db4SGarrett Wollman gencnt = unp_gencnt;
227598271db4SGarrett Wollman n = unp_count;
2276779f106aSGleb Smirnoff UNP_LINK_RUNLOCK();
227798271db4SGarrett Wollman
22788f364875SJulian Elischer xug->xug_len = sizeof *xug;
22798f364875SJulian Elischer xug->xug_count = n;
22808f364875SJulian Elischer xug->xug_gen = gencnt;
22818f364875SJulian Elischer xug->xug_sogen = so_gencnt;
22828f364875SJulian Elischer error = SYSCTL_OUT(req, xug, sizeof *xug);
22838f364875SJulian Elischer if (error) {
22848f364875SJulian Elischer free(xug, M_TEMP);
2285e5aeaa0cSDag-Erling Smørgrav return (error);
22868f364875SJulian Elischer }
228798271db4SGarrett Wollman
2288a163d034SWarner Losh unp_list = malloc(n * sizeof *unp_list, M_TEMP, M_WAITOK);
228998271db4SGarrett Wollman
2290779f106aSGleb Smirnoff UNP_LINK_RLOCK();
22912e3c8fcbSPoul-Henning Kamp for (unp = LIST_FIRST(head), i = 0; unp && i < n;
22922e3c8fcbSPoul-Henning Kamp unp = LIST_NEXT(unp, unp_link)) {
2293e7c33e29SRobert Watson UNP_PCB_LOCK(unp);
22948a7d8cc6SRobert Watson if (unp->unp_gencnt <= gencnt) {
2295a854ed98SJohn Baldwin if (cr_cansee(req->td->td_ucred,
2296e7c33e29SRobert Watson unp->unp_socket->so_cred)) {
2297e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp);
22984787fd37SPaul Saab continue;
2299e7c33e29SRobert Watson }
230098271db4SGarrett Wollman unp_list[i++] = unp;
230175a67bf3SMatt Macy unp_pcb_hold(unp);
230298271db4SGarrett Wollman }
2303e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp);
23044787fd37SPaul Saab }
2305779f106aSGleb Smirnoff UNP_LINK_RUNLOCK();
23061c381b19SRobert Watson n = i; /* In case we lost some during malloc. */
230798271db4SGarrett Wollman
230898271db4SGarrett Wollman error = 0;
2309fe2eee82SColin Percival xu = malloc(sizeof(*xu), M_TEMP, M_WAITOK | M_ZERO);
231098271db4SGarrett Wollman for (i = 0; i < n; i++) {
231198271db4SGarrett Wollman unp = unp_list[i];
2312e7c33e29SRobert Watson UNP_PCB_LOCK(unp);
23135362170dSMark Johnston if (unp_pcb_rele(unp))
23145362170dSMark Johnston continue;
231575a67bf3SMatt Macy
23165362170dSMark Johnston if (unp->unp_gencnt <= gencnt) {
23178f364875SJulian Elischer xu->xu_len = sizeof *xu;
23183a20f06aSBrooks Davis xu->xu_unpp = (uintptr_t)unp;
231998271db4SGarrett Wollman /*
232098271db4SGarrett Wollman * XXX - need more locking here to protect against
232198271db4SGarrett Wollman * connect/disconnect races for SMP.
232298271db4SGarrett Wollman */
2323fc3fcacfSRobert Watson if (unp->unp_addr != NULL)
23248f364875SJulian Elischer bcopy(unp->unp_addr, &xu->xu_addr,
232598271db4SGarrett Wollman unp->unp_addr->sun_len);
23260e229f34SGleb Smirnoff else
23270e229f34SGleb Smirnoff bzero(&xu->xu_addr, sizeof(xu->xu_addr));
2328fc3fcacfSRobert Watson if (unp->unp_conn != NULL &&
2329fc3fcacfSRobert Watson unp->unp_conn->unp_addr != NULL)
233098271db4SGarrett Wollman bcopy(unp->unp_conn->unp_addr,
23318f364875SJulian Elischer &xu->xu_caddr,
233298271db4SGarrett Wollman unp->unp_conn->unp_addr->sun_len);
23330e229f34SGleb Smirnoff else
23340e229f34SGleb Smirnoff bzero(&xu->xu_caddr, sizeof(xu->xu_caddr));
23353a20f06aSBrooks Davis xu->unp_vnode = (uintptr_t)unp->unp_vnode;
23363a20f06aSBrooks Davis xu->unp_conn = (uintptr_t)unp->unp_conn;
23373a20f06aSBrooks Davis xu->xu_firstref = (uintptr_t)LIST_FIRST(&unp->unp_refs);
23383a20f06aSBrooks Davis xu->xu_nextref = (uintptr_t)LIST_NEXT(unp, unp_reflink);
23390e229f34SGleb Smirnoff xu->unp_gencnt = unp->unp_gencnt;
23408f364875SJulian Elischer sotoxsocket(unp->unp_socket, &xu->xu_socket);
2341e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp);
23428f364875SJulian Elischer error = SYSCTL_OUT(req, xu, sizeof *xu);
23435362170dSMark Johnston } else {
2344e7c33e29SRobert Watson UNP_PCB_UNLOCK(unp);
2345e7c33e29SRobert Watson }
23465362170dSMark Johnston }
23478f364875SJulian Elischer free(xu, M_TEMP);
234898271db4SGarrett Wollman if (!error) {
234998271db4SGarrett Wollman /*
23501c381b19SRobert Watson * Give the user an updated idea of our state. If the
23511c381b19SRobert Watson * generation differs from what we told her before, she knows
23521c381b19SRobert Watson * that something happened while we were processing this
23531c381b19SRobert Watson * request, and it might be necessary to retry.
235498271db4SGarrett Wollman */
23558f364875SJulian Elischer xug->xug_gen = unp_gencnt;
23568f364875SJulian Elischer xug->xug_sogen = so_gencnt;
23578f364875SJulian Elischer xug->xug_count = unp_count;
23588f364875SJulian Elischer error = SYSCTL_OUT(req, xug, sizeof *xug);
235998271db4SGarrett Wollman }
236098271db4SGarrett Wollman free(unp_list, M_TEMP);
23618f364875SJulian Elischer free(xug, M_TEMP);
2362e5aeaa0cSDag-Erling Smørgrav return (error);
236398271db4SGarrett Wollman }
236498271db4SGarrett Wollman
23657029da5cSPawel Biernacki SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist,
23667029da5cSPawel Biernacki CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE,
23672fee06f0SMatthew D Fleming (void *)(intptr_t)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb",
236898271db4SGarrett Wollman "List of active local datagram sockets");
23697029da5cSPawel Biernacki SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist,
23707029da5cSPawel Biernacki CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE,
23712fee06f0SMatthew D Fleming (void *)(intptr_t)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb",
237298271db4SGarrett Wollman "List of active local stream sockets");
23732fee06f0SMatthew D Fleming SYSCTL_PROC(_net_local_seqpacket, OID_AUTO, pcblist,
23747029da5cSPawel Biernacki CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE,
23752fee06f0SMatthew D Fleming (void *)(intptr_t)SOCK_SEQPACKET, 0, unp_pcblist, "S,xunpcb",
237684d61770SRobert Watson "List of active local seqpacket sockets");
237798271db4SGarrett Wollman
2378f708ef1bSPoul-Henning Kamp static void
unp_shutdown(struct unpcb * unp)2379892af6b9SRobert Watson unp_shutdown(struct unpcb *unp)
2380df8bae1dSRodney W. Grimes {
2381e7c33e29SRobert Watson struct unpcb *unp2;
2382df8bae1dSRodney W. Grimes struct socket *so;
2383df8bae1dSRodney W. Grimes
2384e7c33e29SRobert Watson UNP_PCB_LOCK_ASSERT(unp);
23850d9ce3a1SRobert Watson
2386e7c33e29SRobert Watson unp2 = unp->unp_conn;
238784d61770SRobert Watson if ((unp->unp_socket->so_type == SOCK_STREAM ||
238884d61770SRobert Watson (unp->unp_socket->so_type == SOCK_SEQPACKET)) && unp2 != NULL) {
2389e7c33e29SRobert Watson so = unp2->unp_socket;
2390e7c33e29SRobert Watson if (so != NULL)
2391df8bae1dSRodney W. Grimes socantrcvmore(so);
2392df8bae1dSRodney W. Grimes }
2393e7c33e29SRobert Watson }
2394df8bae1dSRodney W. Grimes
2395f708ef1bSPoul-Henning Kamp static void
unp_drop(struct unpcb * unp)2396afc055d9SEd Schouten unp_drop(struct unpcb *unp)
2397df8bae1dSRodney W. Grimes {
239850b07c1fSMark Johnston struct socket *so;
2399e7c33e29SRobert Watson struct unpcb *unp2;
24000d9ce3a1SRobert Watson
2401afc055d9SEd Schouten /*
2402afc055d9SEd Schouten * Regardless of whether the socket's peer dropped the connection
2403afc055d9SEd Schouten * with this socket by aborting or disconnecting, POSIX requires
24045716d902SGleb Smirnoff * that ECONNRESET is returned.
2405afc055d9SEd Schouten */
24065716d902SGleb Smirnoff
240775a67bf3SMatt Macy UNP_PCB_LOCK(unp);
240850b07c1fSMark Johnston so = unp->unp_socket;
24095716d902SGleb Smirnoff if (so)
24105716d902SGleb Smirnoff so->so_error = ECONNRESET;
2411ccdadf1aSMark Johnston if ((unp2 = unp_pcb_lock_peer(unp)) != NULL) {
2412f0317f86SMark Johnston /* Last reference dropped in unp_disconnect(). */
2413f0317f86SMark Johnston unp_pcb_rele_notlast(unp);
2414e7c33e29SRobert Watson unp_disconnect(unp, unp2);
2415f0317f86SMark Johnston } else if (!unp_pcb_rele(unp)) {
241675a67bf3SMatt Macy UNP_PCB_UNLOCK(unp);
241775a67bf3SMatt Macy }
2418f0317f86SMark Johnston }
2419df8bae1dSRodney W. Grimes
24202bc21ed9SDavid Malone static void
unp_freerights(struct filedescent ** fdep,int fdcount)24218cb539f1SPawel Jakub Dawidek unp_freerights(struct filedescent **fdep, int fdcount)
2422df8bae1dSRodney W. Grimes {
24232bc21ed9SDavid Malone struct file *fp;
24242609222aSPawel Jakub Dawidek int i;
2425df8bae1dSRodney W. Grimes
242682e825c4SGleb Smirnoff KASSERT(fdcount > 0, ("%s: fdcount %d", __func__, fdcount));
242782e825c4SGleb Smirnoff
24288cb539f1SPawel Jakub Dawidek for (i = 0; i < fdcount; i++) {
24298cb539f1SPawel Jakub Dawidek fp = fdep[i]->fde_file;
24308cb539f1SPawel Jakub Dawidek filecaps_free(&fdep[i]->fde_caps);
24318692c025SYoshinobu Inoue unp_discard(fp);
2432df8bae1dSRodney W. Grimes }
24338cb539f1SPawel Jakub Dawidek free(fdep[0], M_FILECAPS);
24342bc21ed9SDavid Malone }
24352bc21ed9SDavid Malone
24360b36cd25SRobert Watson static int
unp_externalize(struct mbuf * control,struct mbuf ** controlp,int flags)2437c2e3c52eSJilles Tjoelker unp_externalize(struct mbuf *control, struct mbuf **controlp, int flags)
24382bc21ed9SDavid Malone {
24392bc21ed9SDavid Malone struct thread *td = curthread; /* XXX */
24402bc21ed9SDavid Malone struct cmsghdr *cm = mtod(control, struct cmsghdr *);
24412bc21ed9SDavid Malone int i;
24422bc21ed9SDavid Malone int *fdp;
24432609222aSPawel Jakub Dawidek struct filedesc *fdesc = td->td_proc->p_fd;
2444ea31808cSMateusz Guzik struct filedescent **fdep;
24452bc21ed9SDavid Malone void *data;
24462bc21ed9SDavid Malone socklen_t clen = control->m_len, datalen;
24472bc21ed9SDavid Malone int error, newfds;
24482bc21ed9SDavid Malone u_int newlen;
24492bc21ed9SDavid Malone
24503dab55bcSRobert Watson UNP_LINK_UNLOCK_ASSERT();
24514c5bc1caSRobert Watson
24522bc21ed9SDavid Malone error = 0;
24532bc21ed9SDavid Malone if (controlp != NULL) /* controlp == NULL => free control messages */
24542bc21ed9SDavid Malone *controlp = NULL;
24552bc21ed9SDavid Malone while (cm != NULL) {
245675e7e3ceSGleb Smirnoff MPASS(clen >= sizeof(*cm) && clen >= cm->cmsg_len);
24574682ac69SGleb Smirnoff
24582bc21ed9SDavid Malone data = CMSG_DATA(cm);
24592bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
24602bc21ed9SDavid Malone if (cm->cmsg_level == SOL_SOCKET
24612bc21ed9SDavid Malone && cm->cmsg_type == SCM_RIGHTS) {
24622609222aSPawel Jakub Dawidek newfds = datalen / sizeof(*fdep);
246382e825c4SGleb Smirnoff if (newfds == 0)
246482e825c4SGleb Smirnoff goto next;
24652609222aSPawel Jakub Dawidek fdep = data;
24662bc21ed9SDavid Malone
2467e2f9a08bSOlivier Houchard /* If we're not outputting the descriptors free them. */
24682bc21ed9SDavid Malone if (error || controlp == NULL) {
24692609222aSPawel Jakub Dawidek unp_freerights(fdep, newfds);
24702bc21ed9SDavid Malone goto next;
24712bc21ed9SDavid Malone }
24722609222aSPawel Jakub Dawidek FILEDESC_XLOCK(fdesc);
247360a5ef26SRobert Watson
2474ed5b7817SJulian Elischer /*
24751c381b19SRobert Watson * Now change each pointer to an fd in the global
24761c381b19SRobert Watson * table to an integer that is the index to the local
24771c381b19SRobert Watson * fd table entry that we set up to point to the
24781c381b19SRobert Watson * global one we are transferring.
2479ed5b7817SJulian Elischer */
24802bc21ed9SDavid Malone newlen = newfds * sizeof(int);
24812bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, newlen,
2482d64f2f42SGleb Smirnoff SCM_RIGHTS, SOL_SOCKET, M_WAITOK);
24832bc21ed9SDavid Malone
24842bc21ed9SDavid Malone fdp = (int *)
24852bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *));
248648a55bbfSGleb Smirnoff if ((error = fdallocn(td, 0, fdp, newfds))) {
24873331a33aSMateusz Guzik FILEDESC_XUNLOCK(fdesc);
2488db8f33fdSMateusz Guzik unp_freerights(fdep, newfds);
2489db8f33fdSMateusz Guzik m_freem(*controlp);
2490db8f33fdSMateusz Guzik *controlp = NULL;
2491db8f33fdSMateusz Guzik goto next;
2492db8f33fdSMateusz Guzik }
24938cb539f1SPawel Jakub Dawidek for (i = 0; i < newfds; i++, fdp++) {
2494ea31808cSMateusz Guzik _finstall(fdesc, fdep[i]->fde_file, *fdp,
24956ceacebdSAlex Richardson (flags & MSG_CMSG_CLOEXEC) != 0 ? O_CLOEXEC : 0,
2496ea31808cSMateusz Guzik &fdep[i]->fde_caps);
2497ea31808cSMateusz Guzik unp_externalize_fp(fdep[i]->fde_file);
2498df8bae1dSRodney W. Grimes }
2499c7902fbeSMark Johnston
2500c7902fbeSMark Johnston /*
2501c7902fbeSMark Johnston * The new type indicates that the mbuf data refers to
2502c7902fbeSMark Johnston * kernel resources that may need to be released before
2503c7902fbeSMark Johnston * the mbuf is freed.
2504c7902fbeSMark Johnston */
2505c7902fbeSMark Johnston m_chtype(*controlp, MT_EXTCONTROL);
25062609222aSPawel Jakub Dawidek FILEDESC_XUNLOCK(fdesc);
25078cb539f1SPawel Jakub Dawidek free(fdep[0], M_FILECAPS);
25081c381b19SRobert Watson } else {
25091c381b19SRobert Watson /* We can just copy anything else across. */
25102bc21ed9SDavid Malone if (error || controlp == NULL)
25112bc21ed9SDavid Malone goto next;
25122bc21ed9SDavid Malone *controlp = sbcreatecontrol(NULL, datalen,
2513d64f2f42SGleb Smirnoff cm->cmsg_type, cm->cmsg_level, M_WAITOK);
25142bc21ed9SDavid Malone bcopy(data,
25152bc21ed9SDavid Malone CMSG_DATA(mtod(*controlp, struct cmsghdr *)),
25162bc21ed9SDavid Malone datalen);
25172bc21ed9SDavid Malone }
25182bc21ed9SDavid Malone controlp = &(*controlp)->m_next;
25192bc21ed9SDavid Malone
25202bc21ed9SDavid Malone next:
25212bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) {
25222bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen);
25232bc21ed9SDavid Malone cm = (struct cmsghdr *)
25242bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen));
25258692c025SYoshinobu Inoue } else {
25262bc21ed9SDavid Malone clen = 0;
25272bc21ed9SDavid Malone cm = NULL;
25288692c025SYoshinobu Inoue }
25298692c025SYoshinobu Inoue }
25308692c025SYoshinobu Inoue
25312bc21ed9SDavid Malone m_freem(control);
25322bc21ed9SDavid Malone return (error);
2533df8bae1dSRodney W. Grimes }
2534df8bae1dSRodney W. Grimes
25354f590175SPaul Saab static void
unp_zone_change(void * tag)25364f590175SPaul Saab unp_zone_change(void *tag)
25374f590175SPaul Saab {
25384f590175SPaul Saab
25394f590175SPaul Saab uma_zone_set_max(unp_zone, maxsockets);
25404f590175SPaul Saab }
25414f590175SPaul Saab
25425362170dSMark Johnston #ifdef INVARIANTS
25435362170dSMark Johnston static void
unp_zdtor(void * mem,int size __unused,void * arg __unused)25445362170dSMark Johnston unp_zdtor(void *mem, int size __unused, void *arg __unused)
25455362170dSMark Johnston {
25465362170dSMark Johnston struct unpcb *unp;
25475362170dSMark Johnston
25485362170dSMark Johnston unp = mem;
25495362170dSMark Johnston
25505362170dSMark Johnston KASSERT(LIST_EMPTY(&unp->unp_refs),
25515362170dSMark Johnston ("%s: unpcb %p has lingering refs", __func__, unp));
25525362170dSMark Johnston KASSERT(unp->unp_socket == NULL,
25535362170dSMark Johnston ("%s: unpcb %p has socket backpointer", __func__, unp));
25545362170dSMark Johnston KASSERT(unp->unp_vnode == NULL,
25555362170dSMark Johnston ("%s: unpcb %p has vnode references", __func__, unp));
25565362170dSMark Johnston KASSERT(unp->unp_conn == NULL,
25575362170dSMark Johnston ("%s: unpcb %p is still connected", __func__, unp));
25585362170dSMark Johnston KASSERT(unp->unp_addr == NULL,
25595362170dSMark Johnston ("%s: unpcb %p has leaked addr", __func__, unp));
25605362170dSMark Johnston }
25615362170dSMark Johnston #endif
25625362170dSMark Johnston
25630b36cd25SRobert Watson static void
unp_init(void * arg __unused)256424e1c6aeSGleb Smirnoff unp_init(void *arg __unused)
256598271db4SGarrett Wollman {
25665362170dSMark Johnston uma_dtor dtor;
25671c381b19SRobert Watson
25685362170dSMark Johnston #ifdef INVARIANTS
25695362170dSMark Johnston dtor = unp_zdtor;
25705362170dSMark Johnston #else
25715362170dSMark Johnston dtor = NULL;
25725362170dSMark Johnston #endif
25735362170dSMark Johnston unp_zone = uma_zcreate("unpcb", sizeof(struct unpcb), NULL, dtor,
257475a67bf3SMatt Macy NULL, NULL, UMA_ALIGN_CACHE, 0);
25754f590175SPaul Saab uma_zone_set_max(unp_zone, maxsockets);
25766e0b6746SPawel Jakub Dawidek uma_zone_set_warning(unp_zone, "kern.ipc.maxsockets limit reached");
25774f590175SPaul Saab EVENTHANDLER_REGISTER(maxsockets_change, unp_zone_change,
25784f590175SPaul Saab NULL, EVENTHANDLER_PRI_ANY);
257998271db4SGarrett Wollman LIST_INIT(&unp_dhead);
258098271db4SGarrett Wollman LIST_INIT(&unp_shead);
258184d61770SRobert Watson LIST_INIT(&unp_sphead);
25820cb64678SKonstantin Belousov SLIST_INIT(&unp_defers);
2583daee0f0bSKonstantin Belousov TIMEOUT_TASK_INIT(taskqueue_thread, &unp_gc_task, 0, unp_gc, NULL);
25840cb64678SKonstantin Belousov TASK_INIT(&unp_defer_task, 0, unp_process_defers, NULL);
25853dab55bcSRobert Watson UNP_LINK_LOCK_INIT();
25860cb64678SKonstantin Belousov UNP_DEFERRED_LOCK_INIT();
2587c08e016fSJohn Baldwin unp_vp_mtxpool = mtx_pool_create("unp vp mtxpool", 32, MTX_DEF);
258898271db4SGarrett Wollman }
258924e1c6aeSGleb Smirnoff SYSINIT(unp_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_SECOND, unp_init, NULL);
259098271db4SGarrett Wollman
259147c3450eSKonstantin Belousov static void
unp_internalize_cleanup_rights(struct mbuf * control)259247c3450eSKonstantin Belousov unp_internalize_cleanup_rights(struct mbuf *control)
259347c3450eSKonstantin Belousov {
259447c3450eSKonstantin Belousov struct cmsghdr *cp;
259547c3450eSKonstantin Belousov struct mbuf *m;
259647c3450eSKonstantin Belousov void *data;
259747c3450eSKonstantin Belousov socklen_t datalen;
259847c3450eSKonstantin Belousov
259947c3450eSKonstantin Belousov for (m = control; m != NULL; m = m->m_next) {
260047c3450eSKonstantin Belousov cp = mtod(m, struct cmsghdr *);
260147c3450eSKonstantin Belousov if (cp->cmsg_level != SOL_SOCKET ||
260247c3450eSKonstantin Belousov cp->cmsg_type != SCM_RIGHTS)
260347c3450eSKonstantin Belousov continue;
260447c3450eSKonstantin Belousov data = CMSG_DATA(cp);
260547c3450eSKonstantin Belousov datalen = (caddr_t)cp + cp->cmsg_len - (caddr_t)data;
260647c3450eSKonstantin Belousov unp_freerights(data, datalen / sizeof(struct filedesc *));
260747c3450eSKonstantin Belousov }
260847c3450eSKonstantin Belousov }
260947c3450eSKonstantin Belousov
2610f708ef1bSPoul-Henning Kamp static int
unp_internalize(struct mbuf ** controlp,struct thread * td,struct mbuf ** clast,u_int * space,u_int * mbcnt)26115716d902SGleb Smirnoff unp_internalize(struct mbuf **controlp, struct thread *td,
26125716d902SGleb Smirnoff struct mbuf **clast, u_int *space, u_int *mbcnt)
2613df8bae1dSRodney W. Grimes {
26145716d902SGleb Smirnoff struct mbuf *control, **initial_controlp;
261547c3450eSKonstantin Belousov struct proc *p;
261647c3450eSKonstantin Belousov struct filedesc *fdesc;
2617ab15d803SSergey Kandaurov struct bintime *bt;
261847c3450eSKonstantin Belousov struct cmsghdr *cm;
26192bc21ed9SDavid Malone struct cmsgcred *cmcred;
26208cb539f1SPawel Jakub Dawidek struct filedescent *fde, **fdep, *fdev;
26212bc21ed9SDavid Malone struct file *fp;
26222bc21ed9SDavid Malone struct timeval *tv;
2623339efd75SMaxim Sobolev struct timespec *ts;
26242bc21ed9SDavid Malone void *data;
262547c3450eSKonstantin Belousov socklen_t clen, datalen;
2626f1cf2b9dSKonstantin Belousov int i, j, error, *fdp, oldfds;
26278692c025SYoshinobu Inoue u_int newlen;
2628df8bae1dSRodney W. Grimes
26295716d902SGleb Smirnoff MPASS((*controlp)->m_next == NULL); /* COMPAT_OLDSOCK may violate */
26303dab55bcSRobert Watson UNP_LINK_UNLOCK_ASSERT();
26314c5bc1caSRobert Watson
263247c3450eSKonstantin Belousov p = td->td_proc;
263347c3450eSKonstantin Belousov fdesc = p->p_fd;
26342bc21ed9SDavid Malone error = 0;
26355716d902SGleb Smirnoff control = *controlp;
26365716d902SGleb Smirnoff *controlp = NULL;
26375716d902SGleb Smirnoff initial_controlp = controlp;
2638d97922c6SGleb Smirnoff for (clen = control->m_len, cm = mtod(control, struct cmsghdr *),
26392bc21ed9SDavid Malone data = CMSG_DATA(cm);
26402bc21ed9SDavid Malone
2641d97922c6SGleb Smirnoff clen >= sizeof(*cm) && cm->cmsg_level == SOL_SOCKET &&
2642d97922c6SGleb Smirnoff clen >= cm->cmsg_len && cm->cmsg_len >= sizeof(*cm) &&
2643d97922c6SGleb Smirnoff (char *)cm + cm->cmsg_len >= (char *)data;
2644d97922c6SGleb Smirnoff
2645d97922c6SGleb Smirnoff clen -= min(CMSG_SPACE(datalen), clen),
2646d97922c6SGleb Smirnoff cm = (struct cmsghdr *) ((char *)cm + CMSG_SPACE(datalen)),
2647d97922c6SGleb Smirnoff data = CMSG_DATA(cm)) {
2648d97922c6SGleb Smirnoff datalen = (char *)cm + cm->cmsg_len - (char *)data;
26492bc21ed9SDavid Malone switch (cm->cmsg_type) {
26502bc21ed9SDavid Malone case SCM_CREDS:
26515716d902SGleb Smirnoff *controlp = sbcreatecontrol(NULL, sizeof(*cmcred),
26525716d902SGleb Smirnoff SCM_CREDS, SOL_SOCKET, M_WAITOK);
26532bc21ed9SDavid Malone cmcred = (struct cmsgcred *)
26545716d902SGleb Smirnoff CMSG_DATA(mtod(*controlp, struct cmsghdr *));
26550b788fa1SBill Paul cmcred->cmcred_pid = p->p_pid;
2656a854ed98SJohn Baldwin cmcred->cmcred_uid = td->td_ucred->cr_ruid;
2657a854ed98SJohn Baldwin cmcred->cmcred_gid = td->td_ucred->cr_rgid;
2658a854ed98SJohn Baldwin cmcred->cmcred_euid = td->td_ucred->cr_uid;
2659a854ed98SJohn Baldwin cmcred->cmcred_ngroups = MIN(td->td_ucred->cr_ngroups,
26600b788fa1SBill Paul CMGROUP_MAX);
26610b788fa1SBill Paul for (i = 0; i < cmcred->cmcred_ngroups; i++)
26622bc21ed9SDavid Malone cmcred->cmcred_groups[i] =
2663a854ed98SJohn Baldwin td->td_ucred->cr_groups[i];
26642bc21ed9SDavid Malone break;
26650b788fa1SBill Paul
26662bc21ed9SDavid Malone case SCM_RIGHTS:
26672bc21ed9SDavid Malone oldfds = datalen / sizeof (int);
266882e825c4SGleb Smirnoff if (oldfds == 0)
2669d97922c6SGleb Smirnoff continue;
2670579b45e2SGleb Smirnoff /* On some machines sizeof pointer is bigger than
2671579b45e2SGleb Smirnoff * sizeof int, so we need to check if data fits into
2672579b45e2SGleb Smirnoff * single mbuf. We could allocate several mbufs, and
2673579b45e2SGleb Smirnoff * unp_externalize() should even properly handle that.
2674579b45e2SGleb Smirnoff * But it is not worth to complicate the code for an
2675579b45e2SGleb Smirnoff * insane scenario of passing over 200 file descriptors
2676579b45e2SGleb Smirnoff * at once.
2677579b45e2SGleb Smirnoff */
2678579b45e2SGleb Smirnoff newlen = oldfds * sizeof(fdep[0]);
2679579b45e2SGleb Smirnoff if (CMSG_SPACE(newlen) > MCLBYTES) {
2680579b45e2SGleb Smirnoff error = EMSGSIZE;
2681579b45e2SGleb Smirnoff goto out;
2682579b45e2SGleb Smirnoff }
2683ed5b7817SJulian Elischer /*
26841c381b19SRobert Watson * Check that all the FDs passed in refer to legal
26851c381b19SRobert Watson * files. If not, reject the entire operation.
2686ed5b7817SJulian Elischer */
26872bc21ed9SDavid Malone fdp = data;
26882609222aSPawel Jakub Dawidek FILEDESC_SLOCK(fdesc);
26896a1cf96bSMateusz Guzik for (i = 0; i < oldfds; i++, fdp++) {
2690f17ef286SMateusz Guzik fp = fget_noref(fdesc, *fdp);
26916a1cf96bSMateusz Guzik if (fp == NULL) {
26922609222aSPawel Jakub Dawidek FILEDESC_SUNLOCK(fdesc);
26932bc21ed9SDavid Malone error = EBADF;
26942bc21ed9SDavid Malone goto out;
26952bc21ed9SDavid Malone }
2696e7d6662fSAlfred Perlstein if (!(fp->f_ops->fo_flags & DFLAG_PASSABLE)) {
26972609222aSPawel Jakub Dawidek FILEDESC_SUNLOCK(fdesc);
2698e7d6662fSAlfred Perlstein error = EOPNOTSUPP;
2699e7d6662fSAlfred Perlstein goto out;
2700e7d6662fSAlfred Perlstein }
2701df8bae1dSRodney W. Grimes }
27025e3f7694SRobert Watson
2703ed5b7817SJulian Elischer /*
27040b36cd25SRobert Watson * Now replace the integer FDs with pointers to the
27052609222aSPawel Jakub Dawidek * file structure and capability rights.
2706ed5b7817SJulian Elischer */
27075716d902SGleb Smirnoff *controlp = sbcreatecontrol(NULL, newlen,
27085716d902SGleb Smirnoff SCM_RIGHTS, SOL_SOCKET, M_WAITOK);
27092bc21ed9SDavid Malone fdp = data;
2710f1cf2b9dSKonstantin Belousov for (i = 0; i < oldfds; i++, fdp++) {
2711f1cf2b9dSKonstantin Belousov if (!fhold(fdesc->fd_ofiles[*fdp].fde_file)) {
2712f1cf2b9dSKonstantin Belousov fdp = data;
2713f1cf2b9dSKonstantin Belousov for (j = 0; j < i; j++, fdp++) {
2714f1cf2b9dSKonstantin Belousov fdrop(fdesc->fd_ofiles[*fdp].
2715f1cf2b9dSKonstantin Belousov fde_file, td);
2716f1cf2b9dSKonstantin Belousov }
2717f1cf2b9dSKonstantin Belousov FILEDESC_SUNLOCK(fdesc);
2718f1cf2b9dSKonstantin Belousov error = EBADF;
2719f1cf2b9dSKonstantin Belousov goto out;
2720f1cf2b9dSKonstantin Belousov }
2721f1cf2b9dSKonstantin Belousov }
2722f1cf2b9dSKonstantin Belousov fdp = data;
27238cb539f1SPawel Jakub Dawidek fdep = (struct filedescent **)
27245716d902SGleb Smirnoff CMSG_DATA(mtod(*controlp, struct cmsghdr *));
27258cb539f1SPawel Jakub Dawidek fdev = malloc(sizeof(*fdev) * oldfds, M_FILECAPS,
27268cb539f1SPawel Jakub Dawidek M_WAITOK);
27278cb539f1SPawel Jakub Dawidek for (i = 0; i < oldfds; i++, fdev++, fdp++) {
27282609222aSPawel Jakub Dawidek fde = &fdesc->fd_ofiles[*fdp];
27298cb539f1SPawel Jakub Dawidek fdep[i] = fdev;
27308cb539f1SPawel Jakub Dawidek fdep[i]->fde_file = fde->fde_file;
27318cb539f1SPawel Jakub Dawidek filecaps_copy(&fde->fde_caps,
2732d7832811SMateusz Guzik &fdep[i]->fde_caps, true);
27338cb539f1SPawel Jakub Dawidek unp_internalize_fp(fdep[i]->fde_file);
2734df8bae1dSRodney W. Grimes }
27352609222aSPawel Jakub Dawidek FILEDESC_SUNLOCK(fdesc);
27362bc21ed9SDavid Malone break;
27372bc21ed9SDavid Malone
27382bc21ed9SDavid Malone case SCM_TIMESTAMP:
27395716d902SGleb Smirnoff *controlp = sbcreatecontrol(NULL, sizeof(*tv),
27405716d902SGleb Smirnoff SCM_TIMESTAMP, SOL_SOCKET, M_WAITOK);
27412bc21ed9SDavid Malone tv = (struct timeval *)
27425716d902SGleb Smirnoff CMSG_DATA(mtod(*controlp, struct cmsghdr *));
27432bc21ed9SDavid Malone microtime(tv);
27442bc21ed9SDavid Malone break;
27452bc21ed9SDavid Malone
2746ab15d803SSergey Kandaurov case SCM_BINTIME:
27475716d902SGleb Smirnoff *controlp = sbcreatecontrol(NULL, sizeof(*bt),
27485716d902SGleb Smirnoff SCM_BINTIME, SOL_SOCKET, M_WAITOK);
2749ab15d803SSergey Kandaurov bt = (struct bintime *)
27505716d902SGleb Smirnoff CMSG_DATA(mtod(*controlp, struct cmsghdr *));
2751ab15d803SSergey Kandaurov bintime(bt);
2752ab15d803SSergey Kandaurov break;
2753ab15d803SSergey Kandaurov
2754339efd75SMaxim Sobolev case SCM_REALTIME:
27555716d902SGleb Smirnoff *controlp = sbcreatecontrol(NULL, sizeof(*ts),
27565716d902SGleb Smirnoff SCM_REALTIME, SOL_SOCKET, M_WAITOK);
2757339efd75SMaxim Sobolev ts = (struct timespec *)
27585716d902SGleb Smirnoff CMSG_DATA(mtod(*controlp, struct cmsghdr *));
2759339efd75SMaxim Sobolev nanotime(ts);
2760339efd75SMaxim Sobolev break;
2761339efd75SMaxim Sobolev
2762339efd75SMaxim Sobolev case SCM_MONOTONIC:
27635716d902SGleb Smirnoff *controlp = sbcreatecontrol(NULL, sizeof(*ts),
27645716d902SGleb Smirnoff SCM_MONOTONIC, SOL_SOCKET, M_WAITOK);
2765339efd75SMaxim Sobolev ts = (struct timespec *)
27665716d902SGleb Smirnoff CMSG_DATA(mtod(*controlp, struct cmsghdr *));
2767339efd75SMaxim Sobolev nanouptime(ts);
2768339efd75SMaxim Sobolev break;
2769339efd75SMaxim Sobolev
27702bc21ed9SDavid Malone default:
27712bc21ed9SDavid Malone error = EINVAL;
27722bc21ed9SDavid Malone goto out;
27732bc21ed9SDavid Malone }
27742bc21ed9SDavid Malone
27755716d902SGleb Smirnoff if (space != NULL) {
27765716d902SGleb Smirnoff *space += (*controlp)->m_len;
27775716d902SGleb Smirnoff *mbcnt += MSIZE;
27785716d902SGleb Smirnoff if ((*controlp)->m_flags & M_EXT)
27795716d902SGleb Smirnoff *mbcnt += (*controlp)->m_ext.ext_size;
27805716d902SGleb Smirnoff *clast = *controlp;
27815716d902SGleb Smirnoff }
27825716d902SGleb Smirnoff controlp = &(*controlp)->m_next;
27832bc21ed9SDavid Malone }
2784d97922c6SGleb Smirnoff if (clen > 0)
2785d97922c6SGleb Smirnoff error = EINVAL;
27862bc21ed9SDavid Malone
27872bc21ed9SDavid Malone out:
27885716d902SGleb Smirnoff if (error != 0 && initial_controlp != NULL)
27895716d902SGleb Smirnoff unp_internalize_cleanup_rights(*initial_controlp);
27902bc21ed9SDavid Malone m_freem(control);
27912bc21ed9SDavid Malone return (error);
2792df8bae1dSRodney W. Grimes }
2793df8bae1dSRodney W. Grimes
27945716d902SGleb Smirnoff static struct mbuf *
unp_addsockcred(struct thread * td,struct mbuf * control,int mode,struct mbuf ** clast,u_int * space,u_int * mbcnt)27955716d902SGleb Smirnoff unp_addsockcred(struct thread *td, struct mbuf *control, int mode,
27965716d902SGleb Smirnoff struct mbuf **clast, u_int *space, u_int *mbcnt)
27976a2989fdSMatthew N. Dodd {
279870df31f4SMaxim Konovalov struct mbuf *m, *n, *n_prev;
279970df31f4SMaxim Konovalov const struct cmsghdr *cm;
2800ede4af47SConrad Meyer int ngroups, i, cmsgtype;
2801ede4af47SConrad Meyer size_t ctrlsz;
28026a2989fdSMatthew N. Dodd
28036a2989fdSMatthew N. Dodd ngroups = MIN(td->td_ucred->cr_ngroups, CMGROUP_MAX);
2804ede4af47SConrad Meyer if (mode & UNP_WANTCRED_ALWAYS) {
2805ede4af47SConrad Meyer ctrlsz = SOCKCRED2SIZE(ngroups);
2806ede4af47SConrad Meyer cmsgtype = SCM_CREDS2;
2807ede4af47SConrad Meyer } else {
2808ede4af47SConrad Meyer ctrlsz = SOCKCREDSIZE(ngroups);
2809ede4af47SConrad Meyer cmsgtype = SCM_CREDS;
2810ede4af47SConrad Meyer }
2811ede4af47SConrad Meyer
2812b46667c6SGleb Smirnoff m = sbcreatecontrol(NULL, ctrlsz, cmsgtype, SOL_SOCKET, M_NOWAIT);
28136a2989fdSMatthew N. Dodd if (m == NULL)
28145716d902SGleb Smirnoff return (control);
28151093f164SGleb Smirnoff MPASS((m->m_flags & M_EXT) == 0 && m->m_next == NULL);
28166a2989fdSMatthew N. Dodd
2817ede4af47SConrad Meyer if (mode & UNP_WANTCRED_ALWAYS) {
2818ede4af47SConrad Meyer struct sockcred2 *sc;
2819ede4af47SConrad Meyer
2820ede4af47SConrad Meyer sc = (void *)CMSG_DATA(mtod(m, struct cmsghdr *));
2821ede4af47SConrad Meyer sc->sc_version = 0;
2822ede4af47SConrad Meyer sc->sc_pid = td->td_proc->p_pid;
28236a2989fdSMatthew N. Dodd sc->sc_uid = td->td_ucred->cr_ruid;
28246a2989fdSMatthew N. Dodd sc->sc_euid = td->td_ucred->cr_uid;
28256a2989fdSMatthew N. Dodd sc->sc_gid = td->td_ucred->cr_rgid;
28266a2989fdSMatthew N. Dodd sc->sc_egid = td->td_ucred->cr_gid;
28276a2989fdSMatthew N. Dodd sc->sc_ngroups = ngroups;
28286a2989fdSMatthew N. Dodd for (i = 0; i < sc->sc_ngroups; i++)
28296a2989fdSMatthew N. Dodd sc->sc_groups[i] = td->td_ucred->cr_groups[i];
2830ede4af47SConrad Meyer } else {
2831ede4af47SConrad Meyer struct sockcred *sc;
2832ede4af47SConrad Meyer
2833ede4af47SConrad Meyer sc = (void *)CMSG_DATA(mtod(m, struct cmsghdr *));
2834ede4af47SConrad Meyer sc->sc_uid = td->td_ucred->cr_ruid;
2835ede4af47SConrad Meyer sc->sc_euid = td->td_ucred->cr_uid;
2836ede4af47SConrad Meyer sc->sc_gid = td->td_ucred->cr_rgid;
2837ede4af47SConrad Meyer sc->sc_egid = td->td_ucred->cr_gid;
2838ede4af47SConrad Meyer sc->sc_ngroups = ngroups;
2839ede4af47SConrad Meyer for (i = 0; i < sc->sc_ngroups; i++)
2840ede4af47SConrad Meyer sc->sc_groups[i] = td->td_ucred->cr_groups[i];
2841ede4af47SConrad Meyer }
28426a2989fdSMatthew N. Dodd
28436a2989fdSMatthew N. Dodd /*
28441c381b19SRobert Watson * Unlink SCM_CREDS control messages (struct cmsgcred), since just
28451c381b19SRobert Watson * created SCM_CREDS control message (struct sockcred) has another
28461c381b19SRobert Watson * format.
28476a2989fdSMatthew N. Dodd */
28485716d902SGleb Smirnoff if (control != NULL && cmsgtype == SCM_CREDS)
28495716d902SGleb Smirnoff for (n = control, n_prev = NULL; n != NULL;) {
285070df31f4SMaxim Konovalov cm = mtod(n, struct cmsghdr *);
285170df31f4SMaxim Konovalov if (cm->cmsg_level == SOL_SOCKET &&
285270df31f4SMaxim Konovalov cm->cmsg_type == SCM_CREDS) {
28535716d902SGleb Smirnoff if (n_prev == NULL)
28545716d902SGleb Smirnoff control = n->m_next;
28555716d902SGleb Smirnoff else
28565716d902SGleb Smirnoff n_prev->m_next = n->m_next;
28575716d902SGleb Smirnoff if (space != NULL) {
28585716d902SGleb Smirnoff MPASS(*space >= n->m_len);
28595716d902SGleb Smirnoff *space -= n->m_len;
28605716d902SGleb Smirnoff MPASS(*mbcnt >= MSIZE);
28615716d902SGleb Smirnoff *mbcnt -= MSIZE;
28625716d902SGleb Smirnoff if (n->m_flags & M_EXT) {
28635716d902SGleb Smirnoff MPASS(*mbcnt >=
28645716d902SGleb Smirnoff n->m_ext.ext_size);
28655716d902SGleb Smirnoff *mbcnt -= n->m_ext.ext_size;
28665716d902SGleb Smirnoff }
28675716d902SGleb Smirnoff MPASS(clast);
28685716d902SGleb Smirnoff if (*clast == n) {
28695716d902SGleb Smirnoff MPASS(n->m_next == NULL);
28705716d902SGleb Smirnoff if (n_prev == NULL)
28715716d902SGleb Smirnoff *clast = m;
28725716d902SGleb Smirnoff else
28735716d902SGleb Smirnoff *clast = n_prev;
28745716d902SGleb Smirnoff }
28755716d902SGleb Smirnoff }
28765716d902SGleb Smirnoff n = m_free(n);
28775716d902SGleb Smirnoff } else {
28785716d902SGleb Smirnoff n_prev = n;
28795716d902SGleb Smirnoff n = n->m_next;
288070df31f4SMaxim Konovalov }
288170df31f4SMaxim Konovalov }
28826a2989fdSMatthew N. Dodd
288370df31f4SMaxim Konovalov /* Prepend it to the head. */
28845716d902SGleb Smirnoff m->m_next = control;
28855716d902SGleb Smirnoff if (space != NULL) {
28865716d902SGleb Smirnoff *space += m->m_len;
28875716d902SGleb Smirnoff *mbcnt += MSIZE;
28885716d902SGleb Smirnoff if (control == NULL)
28895716d902SGleb Smirnoff *clast = m;
28905716d902SGleb Smirnoff }
28915716d902SGleb Smirnoff return (m);
28926a2989fdSMatthew N. Dodd }
28936a2989fdSMatthew N. Dodd
2894397c19d1SJeff Roberson static struct unpcb *
fptounp(struct file * fp)2895397c19d1SJeff Roberson fptounp(struct file *fp)
2896397c19d1SJeff Roberson {
2897397c19d1SJeff Roberson struct socket *so;
2898397c19d1SJeff Roberson
2899397c19d1SJeff Roberson if (fp->f_type != DTYPE_SOCKET)
2900397c19d1SJeff Roberson return (NULL);
2901397c19d1SJeff Roberson if ((so = fp->f_data) == NULL)
2902397c19d1SJeff Roberson return (NULL);
2903397c19d1SJeff Roberson if (so->so_proto->pr_domain != &localdomain)
2904397c19d1SJeff Roberson return (NULL);
2905397c19d1SJeff Roberson return sotounpcb(so);
2906397c19d1SJeff Roberson }
2907397c19d1SJeff Roberson
2908397c19d1SJeff Roberson static void
unp_discard(struct file * fp)2909397c19d1SJeff Roberson unp_discard(struct file *fp)
2910397c19d1SJeff Roberson {
29110cb64678SKonstantin Belousov struct unp_defer *dr;
2912397c19d1SJeff Roberson
29130cb64678SKonstantin Belousov if (unp_externalize_fp(fp)) {
29140cb64678SKonstantin Belousov dr = malloc(sizeof(*dr), M_TEMP, M_WAITOK);
29150cb64678SKonstantin Belousov dr->ud_fp = fp;
29160cb64678SKonstantin Belousov UNP_DEFERRED_LOCK();
29170cb64678SKonstantin Belousov SLIST_INSERT_HEAD(&unp_defers, dr, ud_link);
29180cb64678SKonstantin Belousov UNP_DEFERRED_UNLOCK();
29190cb64678SKonstantin Belousov atomic_add_int(&unp_defers_count, 1);
29200cb64678SKonstantin Belousov taskqueue_enqueue(taskqueue_thread, &unp_defer_task);
29210cb64678SKonstantin Belousov } else
29224faa375cSMateusz Guzik closef_nothread(fp);
2923397c19d1SJeff Roberson }
2924397c19d1SJeff Roberson
2925397c19d1SJeff Roberson static void
unp_process_defers(void * arg __unused,int pending)29260cb64678SKonstantin Belousov unp_process_defers(void *arg __unused, int pending)
29270cb64678SKonstantin Belousov {
29280cb64678SKonstantin Belousov struct unp_defer *dr;
29290cb64678SKonstantin Belousov SLIST_HEAD(, unp_defer) drl;
29300cb64678SKonstantin Belousov int count;
29310cb64678SKonstantin Belousov
29320cb64678SKonstantin Belousov SLIST_INIT(&drl);
29330cb64678SKonstantin Belousov for (;;) {
29340cb64678SKonstantin Belousov UNP_DEFERRED_LOCK();
29350cb64678SKonstantin Belousov if (SLIST_FIRST(&unp_defers) == NULL) {
29360cb64678SKonstantin Belousov UNP_DEFERRED_UNLOCK();
29370cb64678SKonstantin Belousov break;
29380cb64678SKonstantin Belousov }
29390cb64678SKonstantin Belousov SLIST_SWAP(&unp_defers, &drl, unp_defer);
29400cb64678SKonstantin Belousov UNP_DEFERRED_UNLOCK();
29410cb64678SKonstantin Belousov count = 0;
29420cb64678SKonstantin Belousov while ((dr = SLIST_FIRST(&drl)) != NULL) {
29430cb64678SKonstantin Belousov SLIST_REMOVE_HEAD(&drl, ud_link);
29444faa375cSMateusz Guzik closef_nothread(dr->ud_fp);
29450cb64678SKonstantin Belousov free(dr, M_TEMP);
29460cb64678SKonstantin Belousov count++;
29470cb64678SKonstantin Belousov }
29480cb64678SKonstantin Belousov atomic_add_int(&unp_defers_count, -count);
29490cb64678SKonstantin Belousov }
29500cb64678SKonstantin Belousov }
29510cb64678SKonstantin Belousov
29520cb64678SKonstantin Belousov static void
unp_internalize_fp(struct file * fp)2953397c19d1SJeff Roberson unp_internalize_fp(struct file *fp)
2954397c19d1SJeff Roberson {
2955397c19d1SJeff Roberson struct unpcb *unp;
2956397c19d1SJeff Roberson
29573dab55bcSRobert Watson UNP_LINK_WLOCK();
2958397c19d1SJeff Roberson if ((unp = fptounp(fp)) != NULL) {
2959397c19d1SJeff Roberson unp->unp_file = fp;
2960397c19d1SJeff Roberson unp->unp_msgcount++;
2961397c19d1SJeff Roberson }
2962397c19d1SJeff Roberson unp_rights++;
29633dab55bcSRobert Watson UNP_LINK_WUNLOCK();
2964397c19d1SJeff Roberson }
2965397c19d1SJeff Roberson
29660cb64678SKonstantin Belousov static int
unp_externalize_fp(struct file * fp)2967397c19d1SJeff Roberson unp_externalize_fp(struct file *fp)
2968397c19d1SJeff Roberson {
2969397c19d1SJeff Roberson struct unpcb *unp;
29700cb64678SKonstantin Belousov int ret;
2971397c19d1SJeff Roberson
29723dab55bcSRobert Watson UNP_LINK_WLOCK();
29730cb64678SKonstantin Belousov if ((unp = fptounp(fp)) != NULL) {
2974397c19d1SJeff Roberson unp->unp_msgcount--;
29750cb64678SKonstantin Belousov ret = 1;
29760cb64678SKonstantin Belousov } else
29770cb64678SKonstantin Belousov ret = 0;
2978397c19d1SJeff Roberson unp_rights--;
29793dab55bcSRobert Watson UNP_LINK_WUNLOCK();
29800cb64678SKonstantin Belousov return (ret);
2981397c19d1SJeff Roberson }
2982397c19d1SJeff Roberson
2983161a0c7cSRobert Watson /*
2984a0ec558aSRobert Watson * unp_defer indicates whether additional work has been defered for a future
2985a0ec558aSRobert Watson * pass through unp_gc(). It is thread local and does not require explicit
2986a0ec558aSRobert Watson * synchronization.
2987161a0c7cSRobert Watson */
2988397c19d1SJeff Roberson static int unp_marked;
2989a0ec558aSRobert Watson
2990397c19d1SJeff Roberson static void
unp_remove_dead_ref(struct filedescent ** fdep,int fdcount)2991a9aa06f7SJason A. Harmening unp_remove_dead_ref(struct filedescent **fdep, int fdcount)
2992397c19d1SJeff Roberson {
2993397c19d1SJeff Roberson struct unpcb *unp;
2994be26ba7cSPawel Jakub Dawidek struct file *fp;
2995be26ba7cSPawel Jakub Dawidek int i;
2996397c19d1SJeff Roberson
2997a9aa06f7SJason A. Harmening /*
2998a9aa06f7SJason A. Harmening * This function can only be called from the gc task.
2999a9aa06f7SJason A. Harmening */
3000a9aa06f7SJason A. Harmening KASSERT(taskqueue_member(taskqueue_thread, curthread) != 0,
3001a9aa06f7SJason A. Harmening ("%s: not on gc callout", __func__));
3002a9aa06f7SJason A. Harmening UNP_LINK_LOCK_ASSERT();
3003a9aa06f7SJason A. Harmening
3004be26ba7cSPawel Jakub Dawidek for (i = 0; i < fdcount; i++) {
3005be26ba7cSPawel Jakub Dawidek fp = fdep[i]->fde_file;
30066f552cb0SJeff Roberson if ((unp = fptounp(fp)) == NULL)
3007be26ba7cSPawel Jakub Dawidek continue;
3008a9aa06f7SJason A. Harmening if ((unp->unp_gcflag & UNPGC_DEAD) == 0)
3009be26ba7cSPawel Jakub Dawidek continue;
3010a9aa06f7SJason A. Harmening unp->unp_gcrefs--;
3011a9aa06f7SJason A. Harmening }
3012a9aa06f7SJason A. Harmening }
3013a9aa06f7SJason A. Harmening
3014a9aa06f7SJason A. Harmening static void
unp_restore_undead_ref(struct filedescent ** fdep,int fdcount)3015a9aa06f7SJason A. Harmening unp_restore_undead_ref(struct filedescent **fdep, int fdcount)
3016a9aa06f7SJason A. Harmening {
3017a9aa06f7SJason A. Harmening struct unpcb *unp;
3018a9aa06f7SJason A. Harmening struct file *fp;
3019a9aa06f7SJason A. Harmening int i;
3020a9aa06f7SJason A. Harmening
3021a9aa06f7SJason A. Harmening /*
3022a9aa06f7SJason A. Harmening * This function can only be called from the gc task.
3023a9aa06f7SJason A. Harmening */
3024a9aa06f7SJason A. Harmening KASSERT(taskqueue_member(taskqueue_thread, curthread) != 0,
3025a9aa06f7SJason A. Harmening ("%s: not on gc callout", __func__));
3026a9aa06f7SJason A. Harmening UNP_LINK_LOCK_ASSERT();
3027a9aa06f7SJason A. Harmening
3028a9aa06f7SJason A. Harmening for (i = 0; i < fdcount; i++) {
3029a9aa06f7SJason A. Harmening fp = fdep[i]->fde_file;
3030a9aa06f7SJason A. Harmening if ((unp = fptounp(fp)) == NULL)
3031a9aa06f7SJason A. Harmening continue;
3032a9aa06f7SJason A. Harmening if ((unp->unp_gcflag & UNPGC_DEAD) == 0)
3033a9aa06f7SJason A. Harmening continue;
3034a9aa06f7SJason A. Harmening unp->unp_gcrefs++;
3035397c19d1SJeff Roberson unp_marked++;
3036397c19d1SJeff Roberson }
3037be26ba7cSPawel Jakub Dawidek }
3038397c19d1SJeff Roberson
3039397c19d1SJeff Roberson static void
unp_scan_socket(struct socket * so,void (* op)(struct filedescent **,int))3040a7444f80SGleb Smirnoff unp_scan_socket(struct socket *so, void (*op)(struct filedescent **, int))
3041a7444f80SGleb Smirnoff {
3042458f475dSGleb Smirnoff struct sockbuf *sb;
3043a7444f80SGleb Smirnoff
3044a7444f80SGleb Smirnoff SOCK_LOCK_ASSERT(so);
3045a7444f80SGleb Smirnoff
3046a7444f80SGleb Smirnoff if (sotounpcb(so)->unp_gcflag & UNPGC_IGNORE_RIGHTS)
3047a7444f80SGleb Smirnoff return;
3048a7444f80SGleb Smirnoff
3049a7444f80SGleb Smirnoff SOCK_RECVBUF_LOCK(so);
3050a7444f80SGleb Smirnoff switch (so->so_type) {
3051a7444f80SGleb Smirnoff case SOCK_DGRAM:
3052a7444f80SGleb Smirnoff unp_scan(STAILQ_FIRST(&so->so_rcv.uxdg_mb), op);
3053458f475dSGleb Smirnoff unp_scan(so->so_rcv.uxdg_peeked, op);
3054458f475dSGleb Smirnoff TAILQ_FOREACH(sb, &so->so_rcv.uxdg_conns, uxdg_clist)
3055458f475dSGleb Smirnoff unp_scan(STAILQ_FIRST(&sb->uxdg_mb), op);
3056a7444f80SGleb Smirnoff break;
3057a7444f80SGleb Smirnoff case SOCK_STREAM:
3058a7444f80SGleb Smirnoff case SOCK_SEQPACKET:
30595716d902SGleb Smirnoff unp_scan(so->so_rcv.sb_mb, op);
3060a7444f80SGleb Smirnoff break;
3061a7444f80SGleb Smirnoff }
3062a7444f80SGleb Smirnoff SOCK_RECVBUF_UNLOCK(so);
3063a7444f80SGleb Smirnoff }
3064a7444f80SGleb Smirnoff
3065a7444f80SGleb Smirnoff static void
unp_gc_scan(struct unpcb * unp,void (* op)(struct filedescent **,int))3066a9aa06f7SJason A. Harmening unp_gc_scan(struct unpcb *unp, void (*op)(struct filedescent **, int))
3067397c19d1SJeff Roberson {
3068779f106aSGleb Smirnoff struct socket *so, *soa;
306960a5ef26SRobert Watson
3070397c19d1SJeff Roberson so = unp->unp_socket;
3071779f106aSGleb Smirnoff SOCK_LOCK(so);
3072779f106aSGleb Smirnoff if (SOLISTENING(so)) {
3073397c19d1SJeff Roberson /*
3074397c19d1SJeff Roberson * Mark all sockets in our accept queue.
3075397c19d1SJeff Roberson */
3076a7444f80SGleb Smirnoff TAILQ_FOREACH(soa, &so->sol_comp, so_list)
3077a7444f80SGleb Smirnoff unp_scan_socket(soa, op);
3078779f106aSGleb Smirnoff } else {
3079779f106aSGleb Smirnoff /*
3080779f106aSGleb Smirnoff * Mark all sockets we reference with RIGHTS.
3081779f106aSGleb Smirnoff */
3082a7444f80SGleb Smirnoff unp_scan_socket(so, op);
3083779f106aSGleb Smirnoff }
3084779f106aSGleb Smirnoff SOCK_UNLOCK(so);
3085397c19d1SJeff Roberson }
3086a0ec558aSRobert Watson
3087a0ec558aSRobert Watson static int unp_recycled;
3088be6b1304STom Rhodes SYSCTL_INT(_net_local, OID_AUTO, recycled, CTLFLAG_RD, &unp_recycled, 0,
3089be6b1304STom Rhodes "Number of unreachable sockets claimed by the garbage collector.");
3090df8bae1dSRodney W. Grimes
3091397c19d1SJeff Roberson static int unp_taskcount;
3092be6b1304STom Rhodes SYSCTL_INT(_net_local, OID_AUTO, taskcount, CTLFLAG_RD, &unp_taskcount, 0,
3093be6b1304STom Rhodes "Number of times the garbage collector has run.");
3094397c19d1SJeff Roberson
3095a9aa06f7SJason A. Harmening SYSCTL_UINT(_net_local, OID_AUTO, sockcount, CTLFLAG_RD, &unp_count, 0,
3096a9aa06f7SJason A. Harmening "Number of active local sockets.");
3097a9aa06f7SJason A. Harmening
3098f708ef1bSPoul-Henning Kamp static void
unp_gc(__unused void * arg,int pending)3099a0ec558aSRobert Watson unp_gc(__unused void *arg, int pending)
3100df8bae1dSRodney W. Grimes {
310184d61770SRobert Watson struct unp_head *heads[] = { &unp_dhead, &unp_shead, &unp_sphead,
310284d61770SRobert Watson NULL };
3103397c19d1SJeff Roberson struct unp_head **head;
3104a9aa06f7SJason A. Harmening struct unp_head unp_deadhead; /* List of potentially-dead sockets. */
3105f7780c61SKonstantin Belousov struct file *f, **unref;
3106a9aa06f7SJason A. Harmening struct unpcb *unp, *unptmp;
3107a9aa06f7SJason A. Harmening int i, total, unp_unreachable;
3108df8bae1dSRodney W. Grimes
3109a9aa06f7SJason A. Harmening LIST_INIT(&unp_deadhead);
3110a0ec558aSRobert Watson unp_taskcount++;
3111779f106aSGleb Smirnoff UNP_LINK_RLOCK();
3112ed5b7817SJulian Elischer /*
3113a9aa06f7SJason A. Harmening * First determine which sockets may be in cycles.
3114ed5b7817SJulian Elischer */
3115a9aa06f7SJason A. Harmening unp_unreachable = 0;
3116a9aa06f7SJason A. Harmening
3117397c19d1SJeff Roberson for (head = heads; *head != NULL; head++)
3118a9aa06f7SJason A. Harmening LIST_FOREACH(unp, *head, unp_link) {
3119a9aa06f7SJason A. Harmening KASSERT((unp->unp_gcflag & ~UNPGC_IGNORE_RIGHTS) == 0,
3120a9aa06f7SJason A. Harmening ("%s: unp %p has unexpected gc flags 0x%x",
3121a9aa06f7SJason A. Harmening __func__, unp, (unsigned int)unp->unp_gcflag));
3122a9aa06f7SJason A. Harmening
3123a9aa06f7SJason A. Harmening f = unp->unp_file;
312460a5ef26SRobert Watson
3125397c19d1SJeff Roberson /*
3126a9aa06f7SJason A. Harmening * Check for an unreachable socket potentially in a
3127a9aa06f7SJason A. Harmening * cycle. It must be in a queue as indicated by
3128a9aa06f7SJason A. Harmening * msgcount, and this must equal the file reference
3129a9aa06f7SJason A. Harmening * count. Note that when msgcount is 0 the file is
3130a9aa06f7SJason A. Harmening * NULL.
3131a9aa06f7SJason A. Harmening */
3132a9aa06f7SJason A. Harmening if (f != NULL && unp->unp_msgcount != 0 &&
31333c50616fSMateusz Guzik refcount_load(&f->f_count) == unp->unp_msgcount) {
3134a9aa06f7SJason A. Harmening LIST_INSERT_HEAD(&unp_deadhead, unp, unp_dead);
3135a9aa06f7SJason A. Harmening unp->unp_gcflag |= UNPGC_DEAD;
3136a9aa06f7SJason A. Harmening unp->unp_gcrefs = unp->unp_msgcount;
3137a9aa06f7SJason A. Harmening unp_unreachable++;
3138a9aa06f7SJason A. Harmening }
3139a9aa06f7SJason A. Harmening }
3140a9aa06f7SJason A. Harmening
3141a9aa06f7SJason A. Harmening /*
3142a9aa06f7SJason A. Harmening * Scan all sockets previously marked as potentially being in a cycle
3143a9aa06f7SJason A. Harmening * and remove the references each socket holds on any UNPGC_DEAD
3144a9aa06f7SJason A. Harmening * sockets in its queue. After this step, all remaining references on
3145a9aa06f7SJason A. Harmening * sockets marked UNPGC_DEAD should not be part of any cycle.
3146a9aa06f7SJason A. Harmening */
3147a9aa06f7SJason A. Harmening LIST_FOREACH(unp, &unp_deadhead, unp_dead)
3148a9aa06f7SJason A. Harmening unp_gc_scan(unp, unp_remove_dead_ref);
3149a9aa06f7SJason A. Harmening
3150a9aa06f7SJason A. Harmening /*
3151a9aa06f7SJason A. Harmening * If a socket still has a non-negative refcount, it cannot be in a
3152a9aa06f7SJason A. Harmening * cycle. In this case increment refcount of all children iteratively.
3153397c19d1SJeff Roberson * Stop the scan once we do a complete loop without discovering
3154397c19d1SJeff Roberson * a new reachable socket.
3155397c19d1SJeff Roberson */
3156df8bae1dSRodney W. Grimes do {
3157397c19d1SJeff Roberson unp_marked = 0;
3158a9aa06f7SJason A. Harmening LIST_FOREACH_SAFE(unp, &unp_deadhead, unp_dead, unptmp)
3159a9aa06f7SJason A. Harmening if (unp->unp_gcrefs > 0) {
3160a9aa06f7SJason A. Harmening unp->unp_gcflag &= ~UNPGC_DEAD;
3161a9aa06f7SJason A. Harmening LIST_REMOVE(unp, unp_dead);
3162a9aa06f7SJason A. Harmening KASSERT(unp_unreachable > 0,
3163a9aa06f7SJason A. Harmening ("%s: unp_unreachable underflow.",
3164a9aa06f7SJason A. Harmening __func__));
3165a9aa06f7SJason A. Harmening unp_unreachable--;
3166a9aa06f7SJason A. Harmening unp_gc_scan(unp, unp_restore_undead_ref);
3167a9aa06f7SJason A. Harmening }
3168397c19d1SJeff Roberson } while (unp_marked);
3169a9aa06f7SJason A. Harmening
3170779f106aSGleb Smirnoff UNP_LINK_RUNLOCK();
3171a9aa06f7SJason A. Harmening
3172397c19d1SJeff Roberson if (unp_unreachable == 0)
3173397c19d1SJeff Roberson return;
317460a5ef26SRobert Watson
3175ed5b7817SJulian Elischer /*
3176a9aa06f7SJason A. Harmening * Allocate space for a local array of dead unpcbs.
3177a9aa06f7SJason A. Harmening * TODO: can this path be simplified by instead using the local
3178a9aa06f7SJason A. Harmening * dead list at unp_deadhead, after taking out references
3179a9aa06f7SJason A. Harmening * on the file object and/or unpcb and dropping the link lock?
3180ed5b7817SJulian Elischer */
3181397c19d1SJeff Roberson unref = malloc(unp_unreachable * sizeof(struct file *),
3182397c19d1SJeff Roberson M_TEMP, M_WAITOK);
318360a5ef26SRobert Watson
3184ed5b7817SJulian Elischer /*
3185397c19d1SJeff Roberson * Iterate looking for sockets which have been specifically marked
3186a9aa06f7SJason A. Harmening * as unreachable and store them locally.
3187ed5b7817SJulian Elischer */
3188f7780c61SKonstantin Belousov UNP_LINK_RLOCK();
3189a9aa06f7SJason A. Harmening total = 0;
3190a9aa06f7SJason A. Harmening LIST_FOREACH(unp, &unp_deadhead, unp_dead) {
3191a9aa06f7SJason A. Harmening KASSERT((unp->unp_gcflag & UNPGC_DEAD) != 0,
3192a9aa06f7SJason A. Harmening ("%s: unp %p not marked UNPGC_DEAD", __func__, unp));
3193a9aa06f7SJason A. Harmening unp->unp_gcflag &= ~UNPGC_DEAD;
3194f7780c61SKonstantin Belousov f = unp->unp_file;
3195f7780c61SKonstantin Belousov if (unp->unp_msgcount == 0 || f == NULL ||
31963c50616fSMateusz Guzik refcount_load(&f->f_count) != unp->unp_msgcount ||
3197f1cf2b9dSKonstantin Belousov !fhold(f))
3198f7780c61SKonstantin Belousov continue;
3199f7780c61SKonstantin Belousov unref[total++] = f;
3200f7780c61SKonstantin Belousov KASSERT(total <= unp_unreachable,
3201a9aa06f7SJason A. Harmening ("%s: incorrect unreachable count.", __func__));
3202397c19d1SJeff Roberson }
3203f7780c61SKonstantin Belousov UNP_LINK_RUNLOCK();
320460a5ef26SRobert Watson
3205ed5b7817SJulian Elischer /*
3206397c19d1SJeff Roberson * Now flush all sockets, free'ing rights. This will free the
3207397c19d1SJeff Roberson * struct files associated with these sockets but leave each socket
3208397c19d1SJeff Roberson * with one remaining ref.
3209ed5b7817SJulian Elischer */
32101fb51a12SBjoern A. Zeeb for (i = 0; i < total; i++) {
32111fb51a12SBjoern A. Zeeb struct socket *so;
32121fb51a12SBjoern A. Zeeb
32131fb51a12SBjoern A. Zeeb so = unref[i]->f_data;
32141fb51a12SBjoern A. Zeeb CURVNET_SET(so->so_vnet);
3215289bee16SGleb Smirnoff socantrcvmore(so);
3216289bee16SGleb Smirnoff unp_dispose(so);
32171fb51a12SBjoern A. Zeeb CURVNET_RESTORE();
32181fb51a12SBjoern A. Zeeb }
321960a5ef26SRobert Watson
3220ed5b7817SJulian Elischer /*
3221397c19d1SJeff Roberson * And finally release the sockets so they can be reclaimed.
3222ed5b7817SJulian Elischer */
3223f7780c61SKonstantin Belousov for (i = 0; i < total; i++)
3224397c19d1SJeff Roberson fdrop(unref[i], NULL);
3225f7780c61SKonstantin Belousov unp_recycled += total;
3226397c19d1SJeff Roberson free(unref, M_TEMP);
3227df8bae1dSRodney W. Grimes }
3228df8bae1dSRodney W. Grimes
32290c40f353SConrad Meyer /*
32300c40f353SConrad Meyer * Synchronize against unp_gc, which can trip over data as we are freeing it.
32310c40f353SConrad Meyer */
32320c40f353SConrad Meyer static void
unp_dispose(struct socket * so)323399ab95dbSMark Johnston unp_dispose(struct socket *so)
32340c40f353SConrad Meyer {
3235458f475dSGleb Smirnoff struct sockbuf *sb;
32360c40f353SConrad Meyer struct unpcb *unp;
3237a982ce04SGleb Smirnoff struct mbuf *m;
3238d682c1eaSGleb Smirnoff int error __diagused;
32390c40f353SConrad Meyer
324042f2fa99SGleb Smirnoff MPASS(!SOLISTENING(so));
324142f2fa99SGleb Smirnoff
32420c40f353SConrad Meyer unp = sotounpcb(so);
3243779f106aSGleb Smirnoff UNP_LINK_WLOCK();
32440c40f353SConrad Meyer unp->unp_gcflag |= UNPGC_IGNORE_RIGHTS;
3245779f106aSGleb Smirnoff UNP_LINK_WUNLOCK();
3246a982ce04SGleb Smirnoff
3247a982ce04SGleb Smirnoff /*
3248a982ce04SGleb Smirnoff * Grab our special mbufs before calling sbrelease().
3249a982ce04SGleb Smirnoff */
3250d682c1eaSGleb Smirnoff error = SOCK_IO_RECV_LOCK(so, SBL_WAIT | SBL_NOINTR);
3251d682c1eaSGleb Smirnoff MPASS(!error);
3252a982ce04SGleb Smirnoff SOCK_RECVBUF_LOCK(so);
3253a7444f80SGleb Smirnoff switch (so->so_type) {
3254a7444f80SGleb Smirnoff case SOCK_DGRAM:
3255458f475dSGleb Smirnoff while ((sb = TAILQ_FIRST(&so->so_rcv.uxdg_conns)) != NULL) {
3256458f475dSGleb Smirnoff STAILQ_CONCAT(&so->so_rcv.uxdg_mb, &sb->uxdg_mb);
3257458f475dSGleb Smirnoff TAILQ_REMOVE(&so->so_rcv.uxdg_conns, sb, uxdg_clist);
3258458f475dSGleb Smirnoff /* Note: socket of sb may reconnect. */
3259458f475dSGleb Smirnoff sb->uxdg_cc = sb->uxdg_ctl = sb->uxdg_mbcnt = 0;
3260458f475dSGleb Smirnoff }
3261458f475dSGleb Smirnoff sb = &so->so_rcv;
3262458f475dSGleb Smirnoff if (sb->uxdg_peeked != NULL) {
3263458f475dSGleb Smirnoff STAILQ_INSERT_HEAD(&sb->uxdg_mb, sb->uxdg_peeked,
3264458f475dSGleb Smirnoff m_stailqpkt);
3265458f475dSGleb Smirnoff sb->uxdg_peeked = NULL;
3266458f475dSGleb Smirnoff }
3267a7444f80SGleb Smirnoff m = STAILQ_FIRST(&sb->uxdg_mb);
3268a7444f80SGleb Smirnoff STAILQ_INIT(&sb->uxdg_mb);
32695716d902SGleb Smirnoff /* XXX: our shortened sbrelease() */
32705716d902SGleb Smirnoff (void)chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, 0,
32715716d902SGleb Smirnoff RLIM_INFINITY);
32725716d902SGleb Smirnoff /*
32735716d902SGleb Smirnoff * XXXGL Mark sb with SBS_CANTRCVMORE. This is needed to
32745716d902SGleb Smirnoff * prevent uipc_sosend_dgram() or unp_disconnect() adding more
32755716d902SGleb Smirnoff * data to the socket.
32765716d902SGleb Smirnoff * We came here either through shutdown(2) or from the final
32775716d902SGleb Smirnoff * sofree(). The sofree() case is simple as it guarantees
32785716d902SGleb Smirnoff * that no more sends will happen, however we can race with
32795716d902SGleb Smirnoff * unp_disconnect() from our peer. The shutdown(2) case is
32805716d902SGleb Smirnoff * more exotic. It would call into unp_dispose() only if
32815716d902SGleb Smirnoff * socket is SS_ISCONNECTED. This is possible if we did
32825716d902SGleb Smirnoff * connect(2) on this socket and we also had it bound with
32835716d902SGleb Smirnoff * bind(2) and receive connections from other sockets.
32845716d902SGleb Smirnoff * Because uipc_shutdown() violates POSIX (see comment
32855716d902SGleb Smirnoff * there) we will end up here shutting down our receive side.
32865716d902SGleb Smirnoff * Of course this will have affect not only on the peer we
32875716d902SGleb Smirnoff * connect(2)ed to, but also on all of the peers who had
32885716d902SGleb Smirnoff * connect(2)ed to us. Their sends would end up with ENOBUFS.
32895716d902SGleb Smirnoff */
32905716d902SGleb Smirnoff sb->sb_state |= SBS_CANTRCVMORE;
3291a7444f80SGleb Smirnoff break;
3292a7444f80SGleb Smirnoff case SOCK_STREAM:
3293a7444f80SGleb Smirnoff case SOCK_SEQPACKET:
3294458f475dSGleb Smirnoff sb = &so->so_rcv;
32955716d902SGleb Smirnoff m = sbcut_locked(sb, sb->sb_ccc);
32965716d902SGleb Smirnoff KASSERT(sb->sb_ccc == 0 && sb->sb_mb == 0 && sb->sb_mbcnt == 0,
32975716d902SGleb Smirnoff ("%s: ccc %u mb %p mbcnt %u", __func__,
32985716d902SGleb Smirnoff sb->sb_ccc, (void *)sb->sb_mb, sb->sb_mbcnt));
32995716d902SGleb Smirnoff sbrelease_locked(so, SO_RCV);
3300a7444f80SGleb Smirnoff break;
3301a7444f80SGleb Smirnoff }
3302a982ce04SGleb Smirnoff SOCK_RECVBUF_UNLOCK(so);
3303a982ce04SGleb Smirnoff SOCK_IO_RECV_UNLOCK(so);
3304a982ce04SGleb Smirnoff
33052e5bf7c4SGleb Smirnoff if (m != NULL) {
3306eac7f079SGleb Smirnoff unp_scan(m, unp_freerights);
3307a9b55a66SGleb Smirnoff m_freemp(m);
33082e5bf7c4SGleb Smirnoff }
33090c40f353SConrad Meyer }
33100c40f353SConrad Meyer
3311f708ef1bSPoul-Henning Kamp static void
unp_scan(struct mbuf * m0,void (* op)(struct filedescent **,int))3312be26ba7cSPawel Jakub Dawidek unp_scan(struct mbuf *m0, void (*op)(struct filedescent **, int))
3313df8bae1dSRodney W. Grimes {
33142bc21ed9SDavid Malone struct mbuf *m;
33152bc21ed9SDavid Malone struct cmsghdr *cm;
33162bc21ed9SDavid Malone void *data;
33172bc21ed9SDavid Malone socklen_t clen, datalen;
3318df8bae1dSRodney W. Grimes
3319fc3fcacfSRobert Watson while (m0 != NULL) {
33202bc21ed9SDavid Malone for (m = m0; m; m = m->m_next) {
332112396bdcSDavid Malone if (m->m_type != MT_CONTROL)
3322df8bae1dSRodney W. Grimes continue;
33232bc21ed9SDavid Malone
33242bc21ed9SDavid Malone cm = mtod(m, struct cmsghdr *);
33252bc21ed9SDavid Malone clen = m->m_len;
33262bc21ed9SDavid Malone
33272bc21ed9SDavid Malone while (cm != NULL) {
33282bc21ed9SDavid Malone if (sizeof(*cm) > clen || cm->cmsg_len > clen)
33292bc21ed9SDavid Malone break;
33302bc21ed9SDavid Malone
33312bc21ed9SDavid Malone data = CMSG_DATA(cm);
33322bc21ed9SDavid Malone datalen = (caddr_t)cm + cm->cmsg_len
33332bc21ed9SDavid Malone - (caddr_t)data;
33342bc21ed9SDavid Malone
33352bc21ed9SDavid Malone if (cm->cmsg_level == SOL_SOCKET &&
33362bc21ed9SDavid Malone cm->cmsg_type == SCM_RIGHTS) {
3337be26ba7cSPawel Jakub Dawidek (*op)(data, datalen /
3338be26ba7cSPawel Jakub Dawidek sizeof(struct filedescent *));
33392bc21ed9SDavid Malone }
33402bc21ed9SDavid Malone
33412bc21ed9SDavid Malone if (CMSG_SPACE(datalen) < clen) {
33422bc21ed9SDavid Malone clen -= CMSG_SPACE(datalen);
33432bc21ed9SDavid Malone cm = (struct cmsghdr *)
33442bc21ed9SDavid Malone ((caddr_t)cm + CMSG_SPACE(datalen));
33452bc21ed9SDavid Malone } else {
33462bc21ed9SDavid Malone clen = 0;
33472bc21ed9SDavid Malone cm = NULL;
33482bc21ed9SDavid Malone }
33492bc21ed9SDavid Malone }
3350df8bae1dSRodney W. Grimes }
3351c29a3321SKevin Lo m0 = m0->m_nextpkt;
3352df8bae1dSRodney W. Grimes }
3353df8bae1dSRodney W. Grimes }
3354df8bae1dSRodney W. Grimes
3355662c901cSMikolaj Golub /*
3356e7d02be1SGleb Smirnoff * Definitions of protocols supported in the LOCAL domain.
3357e7d02be1SGleb Smirnoff */
3358e7d02be1SGleb Smirnoff static struct protosw streamproto = {
3359e7d02be1SGleb Smirnoff .pr_type = SOCK_STREAM,
33605716d902SGleb Smirnoff .pr_flags = PR_CONNREQUIRED | PR_WANTRCVD | PR_CAPATTACH,
3361e7d02be1SGleb Smirnoff .pr_ctloutput = &uipc_ctloutput,
3362e7d02be1SGleb Smirnoff .pr_abort = uipc_abort,
33630fac350cSGleb Smirnoff .pr_accept = uipc_peeraddr,
3364e7d02be1SGleb Smirnoff .pr_attach = uipc_attach,
3365e7d02be1SGleb Smirnoff .pr_bind = uipc_bind,
3366e7d02be1SGleb Smirnoff .pr_bindat = uipc_bindat,
3367e7d02be1SGleb Smirnoff .pr_connect = uipc_connect,
3368e7d02be1SGleb Smirnoff .pr_connectat = uipc_connectat,
3369e7d02be1SGleb Smirnoff .pr_connect2 = uipc_connect2,
3370e7d02be1SGleb Smirnoff .pr_detach = uipc_detach,
3371e7d02be1SGleb Smirnoff .pr_disconnect = uipc_disconnect,
3372e7d02be1SGleb Smirnoff .pr_listen = uipc_listen,
3373e7d02be1SGleb Smirnoff .pr_peeraddr = uipc_peeraddr,
33745716d902SGleb Smirnoff .pr_rcvd = uipc_rcvd,
33755716d902SGleb Smirnoff .pr_send = uipc_send,
33765716d902SGleb Smirnoff .pr_ready = uipc_ready,
3377e7d02be1SGleb Smirnoff .pr_sense = uipc_sense,
3378e7d02be1SGleb Smirnoff .pr_shutdown = uipc_shutdown,
3379e7d02be1SGleb Smirnoff .pr_sockaddr = uipc_sockaddr,
33805716d902SGleb Smirnoff .pr_soreceive = soreceive_generic,
3381e7d02be1SGleb Smirnoff .pr_close = uipc_close,
3382bfd03046SMark Johnston .pr_chmod = uipc_chmod,
3383e7d02be1SGleb Smirnoff };
3384e7d02be1SGleb Smirnoff
3385e7d02be1SGleb Smirnoff static struct protosw dgramproto = {
3386e7d02be1SGleb Smirnoff .pr_type = SOCK_DGRAM,
3387289bee16SGleb Smirnoff .pr_flags = PR_ATOMIC | PR_ADDR | PR_CAPATTACH | PR_SOCKBUF,
3388e7d02be1SGleb Smirnoff .pr_ctloutput = &uipc_ctloutput,
3389e7d02be1SGleb Smirnoff .pr_abort = uipc_abort,
33900fac350cSGleb Smirnoff .pr_accept = uipc_peeraddr,
3391e7d02be1SGleb Smirnoff .pr_attach = uipc_attach,
3392e7d02be1SGleb Smirnoff .pr_bind = uipc_bind,
3393e7d02be1SGleb Smirnoff .pr_bindat = uipc_bindat,
3394e7d02be1SGleb Smirnoff .pr_connect = uipc_connect,
3395e7d02be1SGleb Smirnoff .pr_connectat = uipc_connectat,
3396e7d02be1SGleb Smirnoff .pr_connect2 = uipc_connect2,
3397e7d02be1SGleb Smirnoff .pr_detach = uipc_detach,
3398e7d02be1SGleb Smirnoff .pr_disconnect = uipc_disconnect,
3399e7d02be1SGleb Smirnoff .pr_peeraddr = uipc_peeraddr,
3400e7d02be1SGleb Smirnoff .pr_sosend = uipc_sosend_dgram,
3401e7d02be1SGleb Smirnoff .pr_sense = uipc_sense,
3402e7d02be1SGleb Smirnoff .pr_shutdown = uipc_shutdown,
3403e7d02be1SGleb Smirnoff .pr_sockaddr = uipc_sockaddr,
3404e7d02be1SGleb Smirnoff .pr_soreceive = uipc_soreceive_dgram,
3405e7d02be1SGleb Smirnoff .pr_close = uipc_close,
3406bfd03046SMark Johnston .pr_chmod = uipc_chmod,
3407e7d02be1SGleb Smirnoff };
3408e7d02be1SGleb Smirnoff
3409e7d02be1SGleb Smirnoff static struct protosw seqpacketproto = {
3410e7d02be1SGleb Smirnoff .pr_type = SOCK_SEQPACKET,
34115716d902SGleb Smirnoff /*
34125716d902SGleb Smirnoff * XXXRW: For now, PR_ADDR because soreceive will bump into them
34135716d902SGleb Smirnoff * due to our use of sbappendaddr. A new sbappend variants is needed
34145716d902SGleb Smirnoff * that supports both atomic record writes and control data.
34155716d902SGleb Smirnoff */
34165716d902SGleb Smirnoff .pr_flags = PR_ADDR | PR_ATOMIC | PR_CONNREQUIRED |
34175716d902SGleb Smirnoff PR_WANTRCVD | PR_CAPATTACH,
3418e7d02be1SGleb Smirnoff .pr_ctloutput = &uipc_ctloutput,
3419e7d02be1SGleb Smirnoff .pr_abort = uipc_abort,
34200fac350cSGleb Smirnoff .pr_accept = uipc_peeraddr,
3421e7d02be1SGleb Smirnoff .pr_attach = uipc_attach,
3422e7d02be1SGleb Smirnoff .pr_bind = uipc_bind,
3423e7d02be1SGleb Smirnoff .pr_bindat = uipc_bindat,
3424e7d02be1SGleb Smirnoff .pr_connect = uipc_connect,
3425e7d02be1SGleb Smirnoff .pr_connectat = uipc_connectat,
3426e7d02be1SGleb Smirnoff .pr_connect2 = uipc_connect2,
3427e7d02be1SGleb Smirnoff .pr_detach = uipc_detach,
3428e7d02be1SGleb Smirnoff .pr_disconnect = uipc_disconnect,
3429e7d02be1SGleb Smirnoff .pr_listen = uipc_listen,
3430e7d02be1SGleb Smirnoff .pr_peeraddr = uipc_peeraddr,
34315716d902SGleb Smirnoff .pr_rcvd = uipc_rcvd,
34325716d902SGleb Smirnoff .pr_send = uipc_send,
3433e7d02be1SGleb Smirnoff .pr_sense = uipc_sense,
3434e7d02be1SGleb Smirnoff .pr_shutdown = uipc_shutdown,
3435e7d02be1SGleb Smirnoff .pr_sockaddr = uipc_sockaddr,
34365716d902SGleb Smirnoff .pr_soreceive = soreceive_generic, /* XXX: or...? */
3437e7d02be1SGleb Smirnoff .pr_close = uipc_close,
3438bfd03046SMark Johnston .pr_chmod = uipc_chmod,
3439e7d02be1SGleb Smirnoff };
3440e7d02be1SGleb Smirnoff
3441e7d02be1SGleb Smirnoff static struct domain localdomain = {
3442e7d02be1SGleb Smirnoff .dom_family = AF_LOCAL,
3443e7d02be1SGleb Smirnoff .dom_name = "local",
3444e7d02be1SGleb Smirnoff .dom_externalize = unp_externalize,
3445e7d02be1SGleb Smirnoff .dom_nprotosw = 3,
3446e7d02be1SGleb Smirnoff .dom_protosw = {
3447e7d02be1SGleb Smirnoff &streamproto,
3448e7d02be1SGleb Smirnoff &dgramproto,
3449e7d02be1SGleb Smirnoff &seqpacketproto,
3450e7d02be1SGleb Smirnoff }
3451e7d02be1SGleb Smirnoff };
3452e7d02be1SGleb Smirnoff DOMAIN_SET(local);
3453e7d02be1SGleb Smirnoff
3454e7d02be1SGleb Smirnoff /*
3455662c901cSMikolaj Golub * A helper function called by VFS before socket-type vnode reclamation.
3456662c901cSMikolaj Golub * For an active vnode it clears unp_vnode pointer and decrements unp_vnode
3457662c901cSMikolaj Golub * use count.
3458662c901cSMikolaj Golub */
3459662c901cSMikolaj Golub void
vfs_unp_reclaim(struct vnode * vp)3460662c901cSMikolaj Golub vfs_unp_reclaim(struct vnode *vp)
3461662c901cSMikolaj Golub {
3462662c901cSMikolaj Golub struct unpcb *unp;
3463662c901cSMikolaj Golub int active;
346475a67bf3SMatt Macy struct mtx *vplock;
3465662c901cSMikolaj Golub
3466662c901cSMikolaj Golub ASSERT_VOP_ELOCKED(vp, "vfs_unp_reclaim");
3467662c901cSMikolaj Golub KASSERT(vp->v_type == VSOCK,
3468662c901cSMikolaj Golub ("vfs_unp_reclaim: vp->v_type != VSOCK"));
3469662c901cSMikolaj Golub
3470662c901cSMikolaj Golub active = 0;
3471c08e016fSJohn Baldwin vplock = mtx_pool_find(unp_vp_mtxpool, vp);
347275a67bf3SMatt Macy mtx_lock(vplock);
34730c3c207fSGleb Smirnoff VOP_UNP_CONNECT(vp, &unp);
3474662c901cSMikolaj Golub if (unp == NULL)
3475662c901cSMikolaj Golub goto done;
3476662c901cSMikolaj Golub UNP_PCB_LOCK(unp);
3477c7e41c8bSMikolaj Golub if (unp->unp_vnode == vp) {
3478c7e41c8bSMikolaj Golub VOP_UNP_DETACH(vp);
3479662c901cSMikolaj Golub unp->unp_vnode = NULL;
3480662c901cSMikolaj Golub active = 1;
3481662c901cSMikolaj Golub }
3482662c901cSMikolaj Golub UNP_PCB_UNLOCK(unp);
3483662c901cSMikolaj Golub done:
348475a67bf3SMatt Macy mtx_unlock(vplock);
3485662c901cSMikolaj Golub if (active)
3486662c901cSMikolaj Golub vunref(vp);
3487662c901cSMikolaj Golub }
3488662c901cSMikolaj Golub
348903c96c31SRobert Watson #ifdef DDB
349003c96c31SRobert Watson static void
db_print_indent(int indent)349103c96c31SRobert Watson db_print_indent(int indent)
349203c96c31SRobert Watson {
349303c96c31SRobert Watson int i;
349403c96c31SRobert Watson
349503c96c31SRobert Watson for (i = 0; i < indent; i++)
349603c96c31SRobert Watson db_printf(" ");
349703c96c31SRobert Watson }
349803c96c31SRobert Watson
349903c96c31SRobert Watson static void
db_print_unpflags(int unp_flags)350003c96c31SRobert Watson db_print_unpflags(int unp_flags)
350103c96c31SRobert Watson {
350203c96c31SRobert Watson int comma;
350303c96c31SRobert Watson
350403c96c31SRobert Watson comma = 0;
350503c96c31SRobert Watson if (unp_flags & UNP_HAVEPC) {
350603c96c31SRobert Watson db_printf("%sUNP_HAVEPC", comma ? ", " : "");
350703c96c31SRobert Watson comma = 1;
350803c96c31SRobert Watson }
35092de07e40SConrad Meyer if (unp_flags & UNP_WANTCRED_ALWAYS) {
35102de07e40SConrad Meyer db_printf("%sUNP_WANTCRED_ALWAYS", comma ? ", " : "");
35112de07e40SConrad Meyer comma = 1;
35122de07e40SConrad Meyer }
35132de07e40SConrad Meyer if (unp_flags & UNP_WANTCRED_ONESHOT) {
35142de07e40SConrad Meyer db_printf("%sUNP_WANTCRED_ONESHOT", comma ? ", " : "");
351503c96c31SRobert Watson comma = 1;
351603c96c31SRobert Watson }
351703c96c31SRobert Watson if (unp_flags & UNP_CONNECTING) {
351803c96c31SRobert Watson db_printf("%sUNP_CONNECTING", comma ? ", " : "");
351903c96c31SRobert Watson comma = 1;
352003c96c31SRobert Watson }
352103c96c31SRobert Watson if (unp_flags & UNP_BINDING) {
352203c96c31SRobert Watson db_printf("%sUNP_BINDING", comma ? ", " : "");
352303c96c31SRobert Watson comma = 1;
352403c96c31SRobert Watson }
352503c96c31SRobert Watson }
352603c96c31SRobert Watson
352703c96c31SRobert Watson static void
db_print_xucred(int indent,struct xucred * xu)352803c96c31SRobert Watson db_print_xucred(int indent, struct xucred *xu)
352903c96c31SRobert Watson {
353003c96c31SRobert Watson int comma, i;
353103c96c31SRobert Watson
353203c96c31SRobert Watson db_print_indent(indent);
3533c5afec6eSDmitry Chagin db_printf("cr_version: %u cr_uid: %u cr_pid: %d cr_ngroups: %d\n",
3534c5afec6eSDmitry Chagin xu->cr_version, xu->cr_uid, xu->cr_pid, xu->cr_ngroups);
353503c96c31SRobert Watson db_print_indent(indent);
353603c96c31SRobert Watson db_printf("cr_groups: ");
353703c96c31SRobert Watson comma = 0;
353803c96c31SRobert Watson for (i = 0; i < xu->cr_ngroups; i++) {
353903c96c31SRobert Watson db_printf("%s%u", comma ? ", " : "", xu->cr_groups[i]);
354003c96c31SRobert Watson comma = 1;
354103c96c31SRobert Watson }
354203c96c31SRobert Watson db_printf("\n");
354303c96c31SRobert Watson }
354403c96c31SRobert Watson
354503c96c31SRobert Watson static void
db_print_unprefs(int indent,struct unp_head * uh)354603c96c31SRobert Watson db_print_unprefs(int indent, struct unp_head *uh)
354703c96c31SRobert Watson {
354803c96c31SRobert Watson struct unpcb *unp;
354903c96c31SRobert Watson int counter;
355003c96c31SRobert Watson
355103c96c31SRobert Watson counter = 0;
355203c96c31SRobert Watson LIST_FOREACH(unp, uh, unp_reflink) {
355303c96c31SRobert Watson if (counter % 4 == 0)
355403c96c31SRobert Watson db_print_indent(indent);
355503c96c31SRobert Watson db_printf("%p ", unp);
355603c96c31SRobert Watson if (counter % 4 == 3)
355703c96c31SRobert Watson db_printf("\n");
355803c96c31SRobert Watson counter++;
355903c96c31SRobert Watson }
356003c96c31SRobert Watson if (counter != 0 && counter % 4 != 0)
356103c96c31SRobert Watson db_printf("\n");
356203c96c31SRobert Watson }
356303c96c31SRobert Watson
DB_SHOW_COMMAND(unpcb,db_show_unpcb)356403c96c31SRobert Watson DB_SHOW_COMMAND(unpcb, db_show_unpcb)
356503c96c31SRobert Watson {
356603c96c31SRobert Watson struct unpcb *unp;
356703c96c31SRobert Watson
356803c96c31SRobert Watson if (!have_addr) {
356903c96c31SRobert Watson db_printf("usage: show unpcb <addr>\n");
357003c96c31SRobert Watson return;
357103c96c31SRobert Watson }
357203c96c31SRobert Watson unp = (struct unpcb *)addr;
357303c96c31SRobert Watson
357403c96c31SRobert Watson db_printf("unp_socket: %p unp_vnode: %p\n", unp->unp_socket,
357503c96c31SRobert Watson unp->unp_vnode);
357603c96c31SRobert Watson
3577fc8fdae0SMatthew D Fleming db_printf("unp_ino: %ju unp_conn: %p\n", (uintmax_t)unp->unp_ino,
357803c96c31SRobert Watson unp->unp_conn);
357903c96c31SRobert Watson
358003c96c31SRobert Watson db_printf("unp_refs:\n");
358103c96c31SRobert Watson db_print_unprefs(2, &unp->unp_refs);
358203c96c31SRobert Watson
358303c96c31SRobert Watson /* XXXRW: Would be nice to print the full address, if any. */
358403c96c31SRobert Watson db_printf("unp_addr: %p\n", unp->unp_addr);
358503c96c31SRobert Watson
3586c2090e73SAlan Somers db_printf("unp_gencnt: %llu\n",
358703c96c31SRobert Watson (unsigned long long)unp->unp_gencnt);
358803c96c31SRobert Watson
358903c96c31SRobert Watson db_printf("unp_flags: %x (", unp->unp_flags);
359003c96c31SRobert Watson db_print_unpflags(unp->unp_flags);
359103c96c31SRobert Watson db_printf(")\n");
359203c96c31SRobert Watson
359303c96c31SRobert Watson db_printf("unp_peercred:\n");
359403c96c31SRobert Watson db_print_xucred(2, &unp->unp_peercred);
359503c96c31SRobert Watson
359603c96c31SRobert Watson db_printf("unp_refcount: %u\n", unp->unp_refcount);
359703c96c31SRobert Watson }
359803c96c31SRobert Watson #endif
3599