xref: /freebsd/sys/kern/uipc_usrreq.c (revision 162ae9c834f6d9f9cb443bd62cceb23e0b5fef48)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1982, 1986, 1989, 1991, 1993
5  *	The Regents of the University of California. All Rights Reserved.
6  * Copyright (c) 2004-2009 Robert N. M. Watson All Rights Reserved.
7  * Copyright (c) 2018 Matthew Macy
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	From: @(#)uipc_usrreq.c	8.3 (Berkeley) 1/4/94
34  */
35 
36 /*
37  * UNIX Domain (Local) Sockets
38  *
39  * This is an implementation of UNIX (local) domain sockets.  Each socket has
40  * an associated struct unpcb (UNIX protocol control block).  Stream sockets
41  * may be connected to 0 or 1 other socket.  Datagram sockets may be
42  * connected to 0, 1, or many other sockets.  Sockets may be created and
43  * connected in pairs (socketpair(2)), or bound/connected to using the file
44  * system name space.  For most purposes, only the receive socket buffer is
45  * used, as sending on one socket delivers directly to the receive socket
46  * buffer of a second socket.
47  *
48  * The implementation is substantially complicated by the fact that
49  * "ancillary data", such as file descriptors or credentials, may be passed
50  * across UNIX domain sockets.  The potential for passing UNIX domain sockets
51  * over other UNIX domain sockets requires the implementation of a simple
52  * garbage collector to find and tear down cycles of disconnected sockets.
53  *
54  * TODO:
55  *	RDM
56  *	rethink name space problems
57  *	need a proper out-of-band
58  */
59 
60 #include <sys/cdefs.h>
61 __FBSDID("$FreeBSD$");
62 
63 #include "opt_ddb.h"
64 
65 #include <sys/param.h>
66 #include <sys/capsicum.h>
67 #include <sys/domain.h>
68 #include <sys/fcntl.h>
69 #include <sys/malloc.h>		/* XXX must be before <sys/file.h> */
70 #include <sys/eventhandler.h>
71 #include <sys/file.h>
72 #include <sys/filedesc.h>
73 #include <sys/kernel.h>
74 #include <sys/lock.h>
75 #include <sys/mbuf.h>
76 #include <sys/mount.h>
77 #include <sys/mutex.h>
78 #include <sys/namei.h>
79 #include <sys/proc.h>
80 #include <sys/protosw.h>
81 #include <sys/queue.h>
82 #include <sys/resourcevar.h>
83 #include <sys/rwlock.h>
84 #include <sys/socket.h>
85 #include <sys/socketvar.h>
86 #include <sys/signalvar.h>
87 #include <sys/stat.h>
88 #include <sys/sx.h>
89 #include <sys/sysctl.h>
90 #include <sys/systm.h>
91 #include <sys/taskqueue.h>
92 #include <sys/un.h>
93 #include <sys/unpcb.h>
94 #include <sys/vnode.h>
95 
96 #include <net/vnet.h>
97 
98 #ifdef DDB
99 #include <ddb/ddb.h>
100 #endif
101 
102 #include <security/mac/mac_framework.h>
103 
104 #include <vm/uma.h>
105 
106 MALLOC_DECLARE(M_FILECAPS);
107 
108 /*
109  * Locking key:
110  * (l)	Locked using list lock
111  * (g)	Locked using linkage lock
112  */
113 
114 static uma_zone_t	unp_zone;
115 static unp_gen_t	unp_gencnt;	/* (l) */
116 static u_int		unp_count;	/* (l) Count of local sockets. */
117 static ino_t		unp_ino;	/* Prototype for fake inode numbers. */
118 static int		unp_rights;	/* (g) File descriptors in flight. */
119 static struct unp_head	unp_shead;	/* (l) List of stream sockets. */
120 static struct unp_head	unp_dhead;	/* (l) List of datagram sockets. */
121 static struct unp_head	unp_sphead;	/* (l) List of seqpacket sockets. */
122 
123 struct unp_defer {
124 	SLIST_ENTRY(unp_defer) ud_link;
125 	struct file *ud_fp;
126 };
127 static SLIST_HEAD(, unp_defer) unp_defers;
128 static int unp_defers_count;
129 
130 static const struct sockaddr	sun_noname = { sizeof(sun_noname), AF_LOCAL };
131 
132 /*
133  * Garbage collection of cyclic file descriptor/socket references occurs
134  * asynchronously in a taskqueue context in order to avoid recursion and
135  * reentrance in the UNIX domain socket, file descriptor, and socket layer
136  * code.  See unp_gc() for a full description.
137  */
138 static struct timeout_task unp_gc_task;
139 
140 /*
141  * The close of unix domain sockets attached as SCM_RIGHTS is
142  * postponed to the taskqueue, to avoid arbitrary recursion depth.
143  * The attached sockets might have another sockets attached.
144  */
145 static struct task	unp_defer_task;
146 
147 /*
148  * Both send and receive buffers are allocated PIPSIZ bytes of buffering for
149  * stream sockets, although the total for sender and receiver is actually
150  * only PIPSIZ.
151  *
152  * Datagram sockets really use the sendspace as the maximum datagram size,
153  * and don't really want to reserve the sendspace.  Their recvspace should be
154  * large enough for at least one max-size datagram plus address.
155  */
156 #ifndef PIPSIZ
157 #define	PIPSIZ	8192
158 #endif
159 static u_long	unpst_sendspace = PIPSIZ;
160 static u_long	unpst_recvspace = PIPSIZ;
161 static u_long	unpdg_sendspace = 2*1024;	/* really max datagram size */
162 static u_long	unpdg_recvspace = 4*1024;
163 static u_long	unpsp_sendspace = PIPSIZ;	/* really max datagram size */
164 static u_long	unpsp_recvspace = PIPSIZ;
165 
166 static SYSCTL_NODE(_net, PF_LOCAL, local, CTLFLAG_RW, 0, "Local domain");
167 static SYSCTL_NODE(_net_local, SOCK_STREAM, stream, CTLFLAG_RW, 0,
168     "SOCK_STREAM");
169 static SYSCTL_NODE(_net_local, SOCK_DGRAM, dgram, CTLFLAG_RW, 0, "SOCK_DGRAM");
170 static SYSCTL_NODE(_net_local, SOCK_SEQPACKET, seqpacket, CTLFLAG_RW, 0,
171     "SOCK_SEQPACKET");
172 
173 SYSCTL_ULONG(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW,
174 	   &unpst_sendspace, 0, "Default stream send space.");
175 SYSCTL_ULONG(_net_local_stream, OID_AUTO, recvspace, CTLFLAG_RW,
176 	   &unpst_recvspace, 0, "Default stream receive space.");
177 SYSCTL_ULONG(_net_local_dgram, OID_AUTO, maxdgram, CTLFLAG_RW,
178 	   &unpdg_sendspace, 0, "Default datagram send space.");
179 SYSCTL_ULONG(_net_local_dgram, OID_AUTO, recvspace, CTLFLAG_RW,
180 	   &unpdg_recvspace, 0, "Default datagram receive space.");
181 SYSCTL_ULONG(_net_local_seqpacket, OID_AUTO, maxseqpacket, CTLFLAG_RW,
182 	   &unpsp_sendspace, 0, "Default seqpacket send space.");
183 SYSCTL_ULONG(_net_local_seqpacket, OID_AUTO, recvspace, CTLFLAG_RW,
184 	   &unpsp_recvspace, 0, "Default seqpacket receive space.");
185 SYSCTL_INT(_net_local, OID_AUTO, inflight, CTLFLAG_RD, &unp_rights, 0,
186     "File descriptors in flight.");
187 SYSCTL_INT(_net_local, OID_AUTO, deferred, CTLFLAG_RD,
188     &unp_defers_count, 0,
189     "File descriptors deferred to taskqueue for close.");
190 
191 /*
192  * Locking and synchronization:
193  *
194  * Three types of locks exist in the local domain socket implementation: a
195  * a global linkage rwlock, the mtxpool lock, and per-unpcb mutexes.
196  * The linkage lock protects the socket count, global generation number,
197  * and stream/datagram global lists.
198  *
199  * The mtxpool lock protects the vnode from being modified while referenced.
200  * Lock ordering requires that it be acquired before any unpcb locks.
201  *
202  * The unpcb lock (unp_mtx) protects all fields in the unpcb. Of particular
203  * note is that this includes the unp_conn field. So long as the unpcb lock
204  * is held the reference to the unpcb pointed to by unp_conn is valid. If we
205  * require that the unpcb pointed to by unp_conn remain live in cases where
206  * we need to drop the unp_mtx as when we need to acquire the lock for a
207  * second unpcb the caller must first acquire an additional reference on the
208  * second unpcb and then revalidate any state (typically check that unp_conn
209  * is non-NULL) upon requiring the initial unpcb lock. The lock ordering
210  * between unpcbs is the conventional ascending address order. Two helper
211  * routines exist for this:
212  *
213  *   - unp_pcb_lock2(unp, unp2) - which just acquires the two locks in the
214  *     safe ordering.
215  *
216  *   - unp_pcb_owned_lock2(unp, unp2, freed) - the lock for unp is held
217  *     when called. If unp is unlocked and unp2 is subsequently freed
218  *     freed will be set to 1.
219  *
220  * The helper routines for references are:
221  *
222  *   - unp_pcb_hold(unp): Can be called any time we currently hold a valid
223  *     reference to unp.
224  *
225  *    - unp_pcb_rele(unp): The caller must hold the unp lock. If we are
226  *      releasing the last reference, detach must have been called thus
227  *      unp->unp_socket be NULL.
228  *
229  * UNIX domain sockets each have an unpcb hung off of their so_pcb pointer,
230  * allocated in pru_attach() and freed in pru_detach().  The validity of that
231  * pointer is an invariant, so no lock is required to dereference the so_pcb
232  * pointer if a valid socket reference is held by the caller.  In practice,
233  * this is always true during operations performed on a socket.  Each unpcb
234  * has a back-pointer to its socket, unp_socket, which will be stable under
235  * the same circumstances.
236  *
237  * This pointer may only be safely dereferenced as long as a valid reference
238  * to the unpcb is held.  Typically, this reference will be from the socket,
239  * or from another unpcb when the referring unpcb's lock is held (in order
240  * that the reference not be invalidated during use).  For example, to follow
241  * unp->unp_conn->unp_socket, you need to hold a lock on unp_conn to guarantee
242  * that detach is not run clearing unp_socket.
243  *
244  * Blocking with UNIX domain sockets is a tricky issue: unlike most network
245  * protocols, bind() is a non-atomic operation, and connect() requires
246  * potential sleeping in the protocol, due to potentially waiting on local or
247  * distributed file systems.  We try to separate "lookup" operations, which
248  * may sleep, and the IPC operations themselves, which typically can occur
249  * with relative atomicity as locks can be held over the entire operation.
250  *
251  * Another tricky issue is simultaneous multi-threaded or multi-process
252  * access to a single UNIX domain socket.  These are handled by the flags
253  * UNP_CONNECTING and UNP_BINDING, which prevent concurrent connecting or
254  * binding, both of which involve dropping UNIX domain socket locks in order
255  * to perform namei() and other file system operations.
256  */
257 static struct rwlock	unp_link_rwlock;
258 static struct mtx	unp_defers_lock;
259 
260 #define	UNP_LINK_LOCK_INIT()		rw_init(&unp_link_rwlock,	\
261 					    "unp_link_rwlock")
262 
263 #define	UNP_LINK_LOCK_ASSERT()		rw_assert(&unp_link_rwlock,	\
264 					    RA_LOCKED)
265 #define	UNP_LINK_UNLOCK_ASSERT()	rw_assert(&unp_link_rwlock,	\
266 					    RA_UNLOCKED)
267 
268 #define	UNP_LINK_RLOCK()		rw_rlock(&unp_link_rwlock)
269 #define	UNP_LINK_RUNLOCK()		rw_runlock(&unp_link_rwlock)
270 #define	UNP_LINK_WLOCK()		rw_wlock(&unp_link_rwlock)
271 #define	UNP_LINK_WUNLOCK()		rw_wunlock(&unp_link_rwlock)
272 #define	UNP_LINK_WLOCK_ASSERT()		rw_assert(&unp_link_rwlock,	\
273 					    RA_WLOCKED)
274 #define	UNP_LINK_WOWNED()		rw_wowned(&unp_link_rwlock)
275 
276 #define	UNP_DEFERRED_LOCK_INIT()	mtx_init(&unp_defers_lock, \
277 					    "unp_defer", NULL, MTX_DEF)
278 #define	UNP_DEFERRED_LOCK()		mtx_lock(&unp_defers_lock)
279 #define	UNP_DEFERRED_UNLOCK()		mtx_unlock(&unp_defers_lock)
280 
281 #define UNP_REF_LIST_LOCK()		UNP_DEFERRED_LOCK();
282 #define UNP_REF_LIST_UNLOCK()		UNP_DEFERRED_UNLOCK();
283 
284 #define UNP_PCB_LOCK_INIT(unp)		mtx_init(&(unp)->unp_mtx,	\
285 					    "unp", "unp",	\
286 					    MTX_DUPOK|MTX_DEF)
287 #define	UNP_PCB_LOCK_DESTROY(unp)	mtx_destroy(&(unp)->unp_mtx)
288 #define	UNP_PCB_LOCK(unp)		mtx_lock(&(unp)->unp_mtx)
289 #define	UNP_PCB_TRYLOCK(unp)		mtx_trylock(&(unp)->unp_mtx)
290 #define	UNP_PCB_UNLOCK(unp)		mtx_unlock(&(unp)->unp_mtx)
291 #define	UNP_PCB_OWNED(unp)		mtx_owned(&(unp)->unp_mtx)
292 #define	UNP_PCB_LOCK_ASSERT(unp)	mtx_assert(&(unp)->unp_mtx, MA_OWNED)
293 #define	UNP_PCB_UNLOCK_ASSERT(unp)	mtx_assert(&(unp)->unp_mtx, MA_NOTOWNED)
294 
295 static int	uipc_connect2(struct socket *, struct socket *);
296 static int	uipc_ctloutput(struct socket *, struct sockopt *);
297 static int	unp_connect(struct socket *, struct sockaddr *,
298 		    struct thread *);
299 static int	unp_connectat(int, struct socket *, struct sockaddr *,
300 		    struct thread *);
301 static int	unp_connect2(struct socket *so, struct socket *so2, int);
302 static void	unp_disconnect(struct unpcb *unp, struct unpcb *unp2);
303 static void	unp_dispose(struct socket *so);
304 static void	unp_dispose_mbuf(struct mbuf *);
305 static void	unp_shutdown(struct unpcb *);
306 static void	unp_drop(struct unpcb *);
307 static void	unp_gc(__unused void *, int);
308 static void	unp_scan(struct mbuf *, void (*)(struct filedescent **, int));
309 static void	unp_discard(struct file *);
310 static void	unp_freerights(struct filedescent **, int);
311 static void	unp_init(void);
312 static int	unp_internalize(struct mbuf **, struct thread *);
313 static void	unp_internalize_fp(struct file *);
314 static int	unp_externalize(struct mbuf *, struct mbuf **, int);
315 static int	unp_externalize_fp(struct file *);
316 static struct mbuf	*unp_addsockcred(struct thread *, struct mbuf *);
317 static void	unp_process_defers(void * __unused, int);
318 
319 
320 static void
321 unp_pcb_hold(struct unpcb *unp)
322 {
323 	MPASS(unp->unp_refcount);
324 	refcount_acquire(&unp->unp_refcount);
325 }
326 
327 static int
328 unp_pcb_rele(struct unpcb *unp)
329 {
330 	int freed;
331 
332 	UNP_PCB_LOCK_ASSERT(unp);
333 	MPASS(unp->unp_refcount);
334 	if ((freed = refcount_release(&unp->unp_refcount))) {
335 		/* we got here with having detached? */
336 		MPASS(unp->unp_socket == NULL);
337 		UNP_PCB_UNLOCK(unp);
338 		UNP_PCB_LOCK_DESTROY(unp);
339 		uma_zfree(unp_zone, unp);
340 	}
341 	return (freed);
342 }
343 
344 static void
345 unp_pcb_lock2(struct unpcb *unp, struct unpcb *unp2)
346 {
347 	MPASS(unp != unp2);
348 	UNP_PCB_UNLOCK_ASSERT(unp);
349 	UNP_PCB_UNLOCK_ASSERT(unp2);
350 	if ((uintptr_t)unp2 > (uintptr_t)unp) {
351 		UNP_PCB_LOCK(unp);
352 		UNP_PCB_LOCK(unp2);
353 	} else {
354 		UNP_PCB_LOCK(unp2);
355 		UNP_PCB_LOCK(unp);
356 	}
357 }
358 
359 static __noinline void
360 unp_pcb_owned_lock2_slowpath(struct unpcb *unp, struct unpcb **unp2p,
361     int *freed)
362 {
363 	struct unpcb *unp2;
364 
365 	unp2 = *unp2p;
366 	unp_pcb_hold(unp2);
367 	UNP_PCB_UNLOCK(unp);
368 	UNP_PCB_LOCK(unp2);
369 	UNP_PCB_LOCK(unp);
370 	*freed = unp_pcb_rele(unp2);
371 	if (*freed)
372 		*unp2p = NULL;
373 }
374 
375 #define unp_pcb_owned_lock2(unp, unp2, freed) do {			\
376 	freed = 0;							\
377 	UNP_PCB_LOCK_ASSERT(unp);					\
378 	UNP_PCB_UNLOCK_ASSERT(unp2);					\
379 	MPASS((unp) != (unp2));						\
380 	if (__predict_true(UNP_PCB_TRYLOCK(unp2)))			\
381 		break;							\
382 	else if ((uintptr_t)(unp2) > (uintptr_t)(unp))			\
383 		UNP_PCB_LOCK(unp2);					\
384 	else								\
385 		unp_pcb_owned_lock2_slowpath((unp), &(unp2), &freed);	\
386 } while (0)
387 
388 
389 /*
390  * Definitions of protocols supported in the LOCAL domain.
391  */
392 static struct domain localdomain;
393 static struct pr_usrreqs uipc_usrreqs_dgram, uipc_usrreqs_stream;
394 static struct pr_usrreqs uipc_usrreqs_seqpacket;
395 static struct protosw localsw[] = {
396 {
397 	.pr_type =		SOCK_STREAM,
398 	.pr_domain =		&localdomain,
399 	.pr_flags =		PR_CONNREQUIRED|PR_WANTRCVD|PR_RIGHTS,
400 	.pr_ctloutput =		&uipc_ctloutput,
401 	.pr_usrreqs =		&uipc_usrreqs_stream
402 },
403 {
404 	.pr_type =		SOCK_DGRAM,
405 	.pr_domain =		&localdomain,
406 	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_RIGHTS,
407 	.pr_ctloutput =		&uipc_ctloutput,
408 	.pr_usrreqs =		&uipc_usrreqs_dgram
409 },
410 {
411 	.pr_type =		SOCK_SEQPACKET,
412 	.pr_domain =		&localdomain,
413 
414 	/*
415 	 * XXXRW: For now, PR_ADDR because soreceive will bump into them
416 	 * due to our use of sbappendaddr.  A new sbappend variants is needed
417 	 * that supports both atomic record writes and control data.
418 	 */
419 	.pr_flags =		PR_ADDR|PR_ATOMIC|PR_CONNREQUIRED|PR_WANTRCVD|
420 				    PR_RIGHTS,
421 	.pr_ctloutput =		&uipc_ctloutput,
422 	.pr_usrreqs =		&uipc_usrreqs_seqpacket,
423 },
424 };
425 
426 static struct domain localdomain = {
427 	.dom_family =		AF_LOCAL,
428 	.dom_name =		"local",
429 	.dom_init =		unp_init,
430 	.dom_externalize =	unp_externalize,
431 	.dom_dispose =		unp_dispose,
432 	.dom_protosw =		localsw,
433 	.dom_protoswNPROTOSW =	&localsw[nitems(localsw)]
434 };
435 DOMAIN_SET(local);
436 
437 static void
438 uipc_abort(struct socket *so)
439 {
440 	struct unpcb *unp, *unp2;
441 
442 	unp = sotounpcb(so);
443 	KASSERT(unp != NULL, ("uipc_abort: unp == NULL"));
444 	UNP_PCB_UNLOCK_ASSERT(unp);
445 
446 	UNP_PCB_LOCK(unp);
447 	unp2 = unp->unp_conn;
448 	if (unp2 != NULL) {
449 		unp_pcb_hold(unp2);
450 		UNP_PCB_UNLOCK(unp);
451 		unp_drop(unp2);
452 	} else
453 		UNP_PCB_UNLOCK(unp);
454 }
455 
456 static int
457 uipc_accept(struct socket *so, struct sockaddr **nam)
458 {
459 	struct unpcb *unp, *unp2;
460 	const struct sockaddr *sa;
461 
462 	/*
463 	 * Pass back name of connected socket, if it was bound and we are
464 	 * still connected (our peer may have closed already!).
465 	 */
466 	unp = sotounpcb(so);
467 	KASSERT(unp != NULL, ("uipc_accept: unp == NULL"));
468 
469 	*nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK);
470 	UNP_LINK_RLOCK();
471 	unp2 = unp->unp_conn;
472 	if (unp2 != NULL && unp2->unp_addr != NULL) {
473 		UNP_PCB_LOCK(unp2);
474 		sa = (struct sockaddr *) unp2->unp_addr;
475 		bcopy(sa, *nam, sa->sa_len);
476 		UNP_PCB_UNLOCK(unp2);
477 	} else {
478 		sa = &sun_noname;
479 		bcopy(sa, *nam, sa->sa_len);
480 	}
481 	UNP_LINK_RUNLOCK();
482 	return (0);
483 }
484 
485 static int
486 uipc_attach(struct socket *so, int proto, struct thread *td)
487 {
488 	u_long sendspace, recvspace;
489 	struct unpcb *unp;
490 	int error;
491 	bool locked;
492 
493 	KASSERT(so->so_pcb == NULL, ("uipc_attach: so_pcb != NULL"));
494 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
495 		switch (so->so_type) {
496 		case SOCK_STREAM:
497 			sendspace = unpst_sendspace;
498 			recvspace = unpst_recvspace;
499 			break;
500 
501 		case SOCK_DGRAM:
502 			sendspace = unpdg_sendspace;
503 			recvspace = unpdg_recvspace;
504 			break;
505 
506 		case SOCK_SEQPACKET:
507 			sendspace = unpsp_sendspace;
508 			recvspace = unpsp_recvspace;
509 			break;
510 
511 		default:
512 			panic("uipc_attach");
513 		}
514 		error = soreserve(so, sendspace, recvspace);
515 		if (error)
516 			return (error);
517 	}
518 	unp = uma_zalloc(unp_zone, M_NOWAIT | M_ZERO);
519 	if (unp == NULL)
520 		return (ENOBUFS);
521 	LIST_INIT(&unp->unp_refs);
522 	UNP_PCB_LOCK_INIT(unp);
523 	unp->unp_socket = so;
524 	so->so_pcb = unp;
525 	unp->unp_refcount = 1;
526 	if (so->so_listen != NULL)
527 		unp->unp_flags |= UNP_NASCENT;
528 
529 	if ((locked = UNP_LINK_WOWNED()) == false)
530 		UNP_LINK_WLOCK();
531 
532 	unp->unp_gencnt = ++unp_gencnt;
533 	unp->unp_ino = ++unp_ino;
534 	unp_count++;
535 	switch (so->so_type) {
536 	case SOCK_STREAM:
537 		LIST_INSERT_HEAD(&unp_shead, unp, unp_link);
538 		break;
539 
540 	case SOCK_DGRAM:
541 		LIST_INSERT_HEAD(&unp_dhead, unp, unp_link);
542 		break;
543 
544 	case SOCK_SEQPACKET:
545 		LIST_INSERT_HEAD(&unp_sphead, unp, unp_link);
546 		break;
547 
548 	default:
549 		panic("uipc_attach");
550 	}
551 
552 	if (locked == false)
553 		UNP_LINK_WUNLOCK();
554 
555 	return (0);
556 }
557 
558 static int
559 uipc_bindat(int fd, struct socket *so, struct sockaddr *nam, struct thread *td)
560 {
561 	struct sockaddr_un *soun = (struct sockaddr_un *)nam;
562 	struct vattr vattr;
563 	int error, namelen;
564 	struct nameidata nd;
565 	struct unpcb *unp;
566 	struct vnode *vp;
567 	struct mount *mp;
568 	cap_rights_t rights;
569 	char *buf;
570 
571 	if (nam->sa_family != AF_UNIX)
572 		return (EAFNOSUPPORT);
573 
574 	unp = sotounpcb(so);
575 	KASSERT(unp != NULL, ("uipc_bind: unp == NULL"));
576 
577 	if (soun->sun_len > sizeof(struct sockaddr_un))
578 		return (EINVAL);
579 	namelen = soun->sun_len - offsetof(struct sockaddr_un, sun_path);
580 	if (namelen <= 0)
581 		return (EINVAL);
582 
583 	/*
584 	 * We don't allow simultaneous bind() calls on a single UNIX domain
585 	 * socket, so flag in-progress operations, and return an error if an
586 	 * operation is already in progress.
587 	 *
588 	 * Historically, we have not allowed a socket to be rebound, so this
589 	 * also returns an error.  Not allowing re-binding simplifies the
590 	 * implementation and avoids a great many possible failure modes.
591 	 */
592 	UNP_PCB_LOCK(unp);
593 	if (unp->unp_vnode != NULL) {
594 		UNP_PCB_UNLOCK(unp);
595 		return (EINVAL);
596 	}
597 	if (unp->unp_flags & UNP_BINDING) {
598 		UNP_PCB_UNLOCK(unp);
599 		return (EALREADY);
600 	}
601 	unp->unp_flags |= UNP_BINDING;
602 	UNP_PCB_UNLOCK(unp);
603 
604 	buf = malloc(namelen + 1, M_TEMP, M_WAITOK);
605 	bcopy(soun->sun_path, buf, namelen);
606 	buf[namelen] = 0;
607 
608 restart:
609 	NDINIT_ATRIGHTS(&nd, CREATE, NOFOLLOW | LOCKPARENT | SAVENAME | NOCACHE,
610 	    UIO_SYSSPACE, buf, fd, cap_rights_init(&rights, CAP_BINDAT), td);
611 /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */
612 	error = namei(&nd);
613 	if (error)
614 		goto error;
615 	vp = nd.ni_vp;
616 	if (vp != NULL || vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
617 		NDFREE(&nd, NDF_ONLY_PNBUF);
618 		if (nd.ni_dvp == vp)
619 			vrele(nd.ni_dvp);
620 		else
621 			vput(nd.ni_dvp);
622 		if (vp != NULL) {
623 			vrele(vp);
624 			error = EADDRINUSE;
625 			goto error;
626 		}
627 		error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH);
628 		if (error)
629 			goto error;
630 		goto restart;
631 	}
632 	VATTR_NULL(&vattr);
633 	vattr.va_type = VSOCK;
634 	vattr.va_mode = (ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask);
635 #ifdef MAC
636 	error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
637 	    &vattr);
638 #endif
639 	if (error == 0)
640 		error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
641 	NDFREE(&nd, NDF_ONLY_PNBUF);
642 	vput(nd.ni_dvp);
643 	if (error) {
644 		vn_finished_write(mp);
645 		goto error;
646 	}
647 	vp = nd.ni_vp;
648 	ASSERT_VOP_ELOCKED(vp, "uipc_bind");
649 	soun = (struct sockaddr_un *)sodupsockaddr(nam, M_WAITOK);
650 
651 	UNP_PCB_LOCK(unp);
652 	VOP_UNP_BIND(vp, unp);
653 	unp->unp_vnode = vp;
654 	unp->unp_addr = soun;
655 	unp->unp_flags &= ~UNP_BINDING;
656 	UNP_PCB_UNLOCK(unp);
657 	VOP_UNLOCK(vp);
658 	vn_finished_write(mp);
659 	free(buf, M_TEMP);
660 	return (0);
661 
662 error:
663 	UNP_PCB_LOCK(unp);
664 	unp->unp_flags &= ~UNP_BINDING;
665 	UNP_PCB_UNLOCK(unp);
666 	free(buf, M_TEMP);
667 	return (error);
668 }
669 
670 static int
671 uipc_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
672 {
673 
674 	return (uipc_bindat(AT_FDCWD, so, nam, td));
675 }
676 
677 static int
678 uipc_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
679 {
680 	int error;
681 
682 	KASSERT(td == curthread, ("uipc_connect: td != curthread"));
683 	error = unp_connect(so, nam, td);
684 	return (error);
685 }
686 
687 static int
688 uipc_connectat(int fd, struct socket *so, struct sockaddr *nam,
689     struct thread *td)
690 {
691 	int error;
692 
693 	KASSERT(td == curthread, ("uipc_connectat: td != curthread"));
694 	error = unp_connectat(fd, so, nam, td);
695 	return (error);
696 }
697 
698 static void
699 uipc_close(struct socket *so)
700 {
701 	struct unpcb *unp, *unp2;
702 	struct vnode *vp = NULL;
703 	struct mtx *vplock;
704 	int freed;
705 	unp = sotounpcb(so);
706 	KASSERT(unp != NULL, ("uipc_close: unp == NULL"));
707 
708 
709 	vplock = NULL;
710 	if ((vp = unp->unp_vnode) != NULL) {
711 		vplock = mtx_pool_find(mtxpool_sleep, vp);
712 		mtx_lock(vplock);
713 	}
714 	UNP_PCB_LOCK(unp);
715 	if (vp && unp->unp_vnode == NULL) {
716 		mtx_unlock(vplock);
717 		vp = NULL;
718 	}
719 	if (vp != NULL) {
720 		VOP_UNP_DETACH(vp);
721 		unp->unp_vnode = NULL;
722 	}
723 	unp2 = unp->unp_conn;
724 	unp_pcb_hold(unp);
725 	if (__predict_false(unp == unp2)) {
726 		unp_disconnect(unp, unp2);
727 	} else if (unp2 != NULL) {
728 		unp_pcb_hold(unp2);
729 		unp_pcb_owned_lock2(unp, unp2, freed);
730 		unp_disconnect(unp, unp2);
731 		if (unp_pcb_rele(unp2) == 0)
732 			UNP_PCB_UNLOCK(unp2);
733 	}
734 	if (unp_pcb_rele(unp) == 0)
735 		UNP_PCB_UNLOCK(unp);
736 	if (vp) {
737 		mtx_unlock(vplock);
738 		vrele(vp);
739 	}
740 }
741 
742 static int
743 uipc_connect2(struct socket *so1, struct socket *so2)
744 {
745 	struct unpcb *unp, *unp2;
746 	int error;
747 
748 	unp = so1->so_pcb;
749 	KASSERT(unp != NULL, ("uipc_connect2: unp == NULL"));
750 	unp2 = so2->so_pcb;
751 	KASSERT(unp2 != NULL, ("uipc_connect2: unp2 == NULL"));
752 	if (unp != unp2)
753 		unp_pcb_lock2(unp, unp2);
754 	else
755 		UNP_PCB_LOCK(unp);
756 	error = unp_connect2(so1, so2, PRU_CONNECT2);
757 	if (unp != unp2)
758 		UNP_PCB_UNLOCK(unp2);
759 	UNP_PCB_UNLOCK(unp);
760 	return (error);
761 }
762 
763 static void
764 uipc_detach(struct socket *so)
765 {
766 	struct unpcb *unp, *unp2;
767 	struct mtx *vplock;
768 	struct sockaddr_un *saved_unp_addr;
769 	struct vnode *vp;
770 	int freeunp, local_unp_rights;
771 
772 	unp = sotounpcb(so);
773 	KASSERT(unp != NULL, ("uipc_detach: unp == NULL"));
774 
775 	vp = NULL;
776 	vplock = NULL;
777 	local_unp_rights = 0;
778 
779 	UNP_LINK_WLOCK();
780 	LIST_REMOVE(unp, unp_link);
781 	if (unp->unp_gcflag & UNPGC_DEAD)
782 		LIST_REMOVE(unp, unp_dead);
783 	unp->unp_gencnt = ++unp_gencnt;
784 	--unp_count;
785 	UNP_LINK_WUNLOCK();
786 
787 	UNP_PCB_UNLOCK_ASSERT(unp);
788  restart:
789 	if ((vp = unp->unp_vnode) != NULL) {
790 		vplock = mtx_pool_find(mtxpool_sleep, vp);
791 		mtx_lock(vplock);
792 	}
793 	UNP_PCB_LOCK(unp);
794 	if (unp->unp_vnode != vp &&
795 		unp->unp_vnode != NULL) {
796 		if (vplock)
797 			mtx_unlock(vplock);
798 		UNP_PCB_UNLOCK(unp);
799 		goto restart;
800 	}
801 	if ((unp->unp_flags & UNP_NASCENT) != 0) {
802 		goto teardown;
803 	}
804 	if ((vp = unp->unp_vnode) != NULL) {
805 		VOP_UNP_DETACH(vp);
806 		unp->unp_vnode = NULL;
807 	}
808 	if (__predict_false(unp == unp->unp_conn)) {
809 		unp_disconnect(unp, unp);
810 		unp2 = NULL;
811 		goto connect_self;
812 	}
813 	if ((unp2 = unp->unp_conn) != NULL) {
814 		unp_pcb_owned_lock2(unp, unp2, freeunp);
815 		if (freeunp)
816 			unp2 = NULL;
817 	}
818 	unp_pcb_hold(unp);
819 	if (unp2 != NULL) {
820 		unp_pcb_hold(unp2);
821 		unp_disconnect(unp, unp2);
822 		if (unp_pcb_rele(unp2) == 0)
823 			UNP_PCB_UNLOCK(unp2);
824 	}
825  connect_self:
826 	UNP_PCB_UNLOCK(unp);
827 	UNP_REF_LIST_LOCK();
828 	while (!LIST_EMPTY(&unp->unp_refs)) {
829 		struct unpcb *ref = LIST_FIRST(&unp->unp_refs);
830 
831 		unp_pcb_hold(ref);
832 		UNP_REF_LIST_UNLOCK();
833 
834 		MPASS(ref != unp);
835 		UNP_PCB_UNLOCK_ASSERT(ref);
836 		unp_drop(ref);
837 		UNP_REF_LIST_LOCK();
838 	}
839 
840 	UNP_REF_LIST_UNLOCK();
841 	UNP_PCB_LOCK(unp);
842 	freeunp = unp_pcb_rele(unp);
843 	MPASS(freeunp == 0);
844 	local_unp_rights = unp_rights;
845 teardown:
846 	unp->unp_socket->so_pcb = NULL;
847 	saved_unp_addr = unp->unp_addr;
848 	unp->unp_addr = NULL;
849 	unp->unp_socket = NULL;
850 	freeunp = unp_pcb_rele(unp);
851 	if (saved_unp_addr != NULL)
852 		free(saved_unp_addr, M_SONAME);
853 	if (!freeunp)
854 		UNP_PCB_UNLOCK(unp);
855 	if (vp) {
856 		mtx_unlock(vplock);
857 		vrele(vp);
858 	}
859 	if (local_unp_rights)
860 		taskqueue_enqueue_timeout(taskqueue_thread, &unp_gc_task, -1);
861 }
862 
863 static int
864 uipc_disconnect(struct socket *so)
865 {
866 	struct unpcb *unp, *unp2;
867 	int freed;
868 
869 	unp = sotounpcb(so);
870 	KASSERT(unp != NULL, ("uipc_disconnect: unp == NULL"));
871 
872 	UNP_PCB_LOCK(unp);
873 	if ((unp2 = unp->unp_conn) == NULL) {
874 		UNP_PCB_UNLOCK(unp);
875 		return (0);
876 	}
877 	if (__predict_true(unp != unp2)) {
878 		unp_pcb_owned_lock2(unp, unp2, freed);
879 		if (__predict_false(freed)) {
880 			UNP_PCB_UNLOCK(unp);
881 			return (0);
882 		}
883 		unp_pcb_hold(unp2);
884 	}
885 	unp_pcb_hold(unp);
886 	unp_disconnect(unp, unp2);
887 	if (unp_pcb_rele(unp) == 0)
888 		UNP_PCB_UNLOCK(unp);
889 	if ((unp != unp2) && unp_pcb_rele(unp2) == 0)
890 		UNP_PCB_UNLOCK(unp2);
891 	return (0);
892 }
893 
894 static int
895 uipc_listen(struct socket *so, int backlog, struct thread *td)
896 {
897 	struct unpcb *unp;
898 	int error;
899 
900 	if (so->so_type != SOCK_STREAM && so->so_type != SOCK_SEQPACKET)
901 		return (EOPNOTSUPP);
902 
903 	unp = sotounpcb(so);
904 	KASSERT(unp != NULL, ("uipc_listen: unp == NULL"));
905 
906 	UNP_PCB_LOCK(unp);
907 	if (unp->unp_vnode == NULL) {
908 		/* Already connected or not bound to an address. */
909 		error = unp->unp_conn != NULL ? EINVAL : EDESTADDRREQ;
910 		UNP_PCB_UNLOCK(unp);
911 		return (error);
912 	}
913 
914 	SOCK_LOCK(so);
915 	error = solisten_proto_check(so);
916 	if (error == 0) {
917 		cru2xt(td, &unp->unp_peercred);
918 		solisten_proto(so, backlog);
919 	}
920 	SOCK_UNLOCK(so);
921 	UNP_PCB_UNLOCK(unp);
922 	return (error);
923 }
924 
925 static int
926 uipc_peeraddr(struct socket *so, struct sockaddr **nam)
927 {
928 	struct unpcb *unp, *unp2;
929 	const struct sockaddr *sa;
930 
931 	unp = sotounpcb(so);
932 	KASSERT(unp != NULL, ("uipc_peeraddr: unp == NULL"));
933 
934 	*nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK);
935 	UNP_LINK_RLOCK();
936 	/*
937 	 * XXX: It seems that this test always fails even when connection is
938 	 * established.  So, this else clause is added as workaround to
939 	 * return PF_LOCAL sockaddr.
940 	 */
941 	unp2 = unp->unp_conn;
942 	if (unp2 != NULL) {
943 		UNP_PCB_LOCK(unp2);
944 		if (unp2->unp_addr != NULL)
945 			sa = (struct sockaddr *) unp2->unp_addr;
946 		else
947 			sa = &sun_noname;
948 		bcopy(sa, *nam, sa->sa_len);
949 		UNP_PCB_UNLOCK(unp2);
950 	} else {
951 		sa = &sun_noname;
952 		bcopy(sa, *nam, sa->sa_len);
953 	}
954 	UNP_LINK_RUNLOCK();
955 	return (0);
956 }
957 
958 static int
959 uipc_rcvd(struct socket *so, int flags)
960 {
961 	struct unpcb *unp, *unp2;
962 	struct socket *so2;
963 	u_int mbcnt, sbcc;
964 
965 	unp = sotounpcb(so);
966 	KASSERT(unp != NULL, ("%s: unp == NULL", __func__));
967 	KASSERT(so->so_type == SOCK_STREAM || so->so_type == SOCK_SEQPACKET,
968 	    ("%s: socktype %d", __func__, so->so_type));
969 
970 	/*
971 	 * Adjust backpressure on sender and wakeup any waiting to write.
972 	 *
973 	 * The unp lock is acquired to maintain the validity of the unp_conn
974 	 * pointer; no lock on unp2 is required as unp2->unp_socket will be
975 	 * static as long as we don't permit unp2 to disconnect from unp,
976 	 * which is prevented by the lock on unp.  We cache values from
977 	 * so_rcv to avoid holding the so_rcv lock over the entire
978 	 * transaction on the remote so_snd.
979 	 */
980 	SOCKBUF_LOCK(&so->so_rcv);
981 	mbcnt = so->so_rcv.sb_mbcnt;
982 	sbcc = sbavail(&so->so_rcv);
983 	SOCKBUF_UNLOCK(&so->so_rcv);
984 	/*
985 	 * There is a benign race condition at this point.  If we're planning to
986 	 * clear SB_STOP, but uipc_send is called on the connected socket at
987 	 * this instant, it might add data to the sockbuf and set SB_STOP.  Then
988 	 * we would erroneously clear SB_STOP below, even though the sockbuf is
989 	 * full.  The race is benign because the only ill effect is to allow the
990 	 * sockbuf to exceed its size limit, and the size limits are not
991 	 * strictly guaranteed anyway.
992 	 */
993 	UNP_PCB_LOCK(unp);
994 	unp2 = unp->unp_conn;
995 	if (unp2 == NULL) {
996 		UNP_PCB_UNLOCK(unp);
997 		return (0);
998 	}
999 	so2 = unp2->unp_socket;
1000 	SOCKBUF_LOCK(&so2->so_snd);
1001 	if (sbcc < so2->so_snd.sb_hiwat && mbcnt < so2->so_snd.sb_mbmax)
1002 		so2->so_snd.sb_flags &= ~SB_STOP;
1003 	sowwakeup_locked(so2);
1004 	UNP_PCB_UNLOCK(unp);
1005 	return (0);
1006 }
1007 
1008 static int
1009 connect_internal(struct socket *so, struct sockaddr *nam, struct thread *td)
1010 {
1011 	int error;
1012 	struct unpcb *unp;
1013 
1014 	unp = so->so_pcb;
1015 	if (unp->unp_conn != NULL)
1016 		return (EISCONN);
1017 	error = unp_connect(so, nam, td);
1018 	if (error)
1019 		return (error);
1020 	UNP_PCB_LOCK(unp);
1021 	if (unp->unp_conn == NULL) {
1022 		UNP_PCB_UNLOCK(unp);
1023 		if (error == 0)
1024 			error = ENOTCONN;
1025 	}
1026 	return (error);
1027 }
1028 
1029 
1030 static int
1031 uipc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
1032     struct mbuf *control, struct thread *td)
1033 {
1034 	struct unpcb *unp, *unp2;
1035 	struct socket *so2;
1036 	u_int mbcnt, sbcc;
1037 	int freed, error;
1038 
1039 	unp = sotounpcb(so);
1040 	KASSERT(unp != NULL, ("%s: unp == NULL", __func__));
1041 	KASSERT(so->so_type == SOCK_STREAM || so->so_type == SOCK_DGRAM ||
1042 	    so->so_type == SOCK_SEQPACKET,
1043 	    ("%s: socktype %d", __func__, so->so_type));
1044 
1045 	freed = error = 0;
1046 	if (flags & PRUS_OOB) {
1047 		error = EOPNOTSUPP;
1048 		goto release;
1049 	}
1050 	if (control != NULL && (error = unp_internalize(&control, td)))
1051 		goto release;
1052 
1053 	unp2 = NULL;
1054 	switch (so->so_type) {
1055 	case SOCK_DGRAM:
1056 	{
1057 		const struct sockaddr *from;
1058 
1059 		if (nam != NULL) {
1060 			/*
1061 			 * We return with UNP_PCB_LOCK_HELD so we know that
1062 			 * the reference is live if the pointer is valid.
1063 			 */
1064 			if ((error = connect_internal(so, nam, td)))
1065 				break;
1066 			MPASS(unp->unp_conn != NULL);
1067 			unp2 = unp->unp_conn;
1068 		} else  {
1069 			UNP_PCB_LOCK(unp);
1070 
1071 			/*
1072 			 * Because connect() and send() are non-atomic in a sendto()
1073 			 * with a target address, it's possible that the socket will
1074 			 * have disconnected before the send() can run.  In that case
1075 			 * return the slightly counter-intuitive but otherwise
1076 			 * correct error that the socket is not connected.
1077 			 */
1078 			if ((unp2 = unp->unp_conn)  == NULL) {
1079 				UNP_PCB_UNLOCK(unp);
1080 				error = ENOTCONN;
1081 				break;
1082 			}
1083 		}
1084 		if (__predict_false(unp == unp2)) {
1085 			if (unp->unp_socket == NULL) {
1086 				error = ENOTCONN;
1087 				break;
1088 			}
1089 			goto connect_self;
1090 		}
1091 		unp_pcb_owned_lock2(unp, unp2, freed);
1092 		if (__predict_false(freed)) {
1093 			UNP_PCB_UNLOCK(unp);
1094 			error = ENOTCONN;
1095 			break;
1096 		}
1097 		/*
1098 		 * The socket referencing unp2 may have been closed
1099 		 * or unp may have been disconnected if the unp lock
1100 		 * was dropped to acquire unp2.
1101 		 */
1102 		if (__predict_false(unp->unp_conn == NULL) ||
1103 			unp2->unp_socket == NULL) {
1104 			UNP_PCB_UNLOCK(unp);
1105 			if (unp_pcb_rele(unp2) == 0)
1106 				UNP_PCB_UNLOCK(unp2);
1107 			error = ENOTCONN;
1108 			break;
1109 		}
1110 	connect_self:
1111 		if (unp2->unp_flags & UNP_WANTCRED)
1112 			control = unp_addsockcred(td, control);
1113 		if (unp->unp_addr != NULL)
1114 			from = (struct sockaddr *)unp->unp_addr;
1115 		else
1116 			from = &sun_noname;
1117 		so2 = unp2->unp_socket;
1118 		SOCKBUF_LOCK(&so2->so_rcv);
1119 		if (sbappendaddr_locked(&so2->so_rcv, from, m,
1120 		    control)) {
1121 			sorwakeup_locked(so2);
1122 			m = NULL;
1123 			control = NULL;
1124 		} else {
1125 			SOCKBUF_UNLOCK(&so2->so_rcv);
1126 			error = ENOBUFS;
1127 		}
1128 		if (nam != NULL)
1129 			unp_disconnect(unp, unp2);
1130 		if (__predict_true(unp != unp2))
1131 			UNP_PCB_UNLOCK(unp2);
1132 		UNP_PCB_UNLOCK(unp);
1133 		break;
1134 	}
1135 
1136 	case SOCK_SEQPACKET:
1137 	case SOCK_STREAM:
1138 		if ((so->so_state & SS_ISCONNECTED) == 0) {
1139 			if (nam != NULL) {
1140 				if ((error = connect_internal(so, nam, td)))
1141 					break;
1142 			} else  {
1143 				error = ENOTCONN;
1144 				break;
1145 			}
1146 		} else if ((unp2 = unp->unp_conn) == NULL) {
1147 			error = ENOTCONN;
1148 			break;
1149 		} else if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
1150 			error = EPIPE;
1151 			break;
1152 		} else {
1153 			UNP_PCB_LOCK(unp);
1154 			if ((unp2 = unp->unp_conn) == NULL) {
1155 				UNP_PCB_UNLOCK(unp);
1156 				error = ENOTCONN;
1157 				break;
1158 			}
1159 		}
1160 		unp_pcb_owned_lock2(unp, unp2, freed);
1161 		UNP_PCB_UNLOCK(unp);
1162 		if (__predict_false(freed)) {
1163 			error = ENOTCONN;
1164 			break;
1165 		}
1166 		if ((so2 = unp2->unp_socket) == NULL) {
1167 			UNP_PCB_UNLOCK(unp2);
1168 			error = ENOTCONN;
1169 			break;
1170 		}
1171 		SOCKBUF_LOCK(&so2->so_rcv);
1172 		if (unp2->unp_flags & UNP_WANTCRED) {
1173 			/*
1174 			 * Credentials are passed only once on SOCK_STREAM
1175 			 * and SOCK_SEQPACKET.
1176 			 */
1177 			unp2->unp_flags &= ~UNP_WANTCRED;
1178 			control = unp_addsockcred(td, control);
1179 		}
1180 
1181 		/*
1182 		 * Send to paired receive port and wake up readers.  Don't
1183 		 * check for space available in the receive buffer if we're
1184 		 * attaching ancillary data; Unix domain sockets only check
1185 		 * for space in the sending sockbuf, and that check is
1186 		 * performed one level up the stack.  At that level we cannot
1187 		 * precisely account for the amount of buffer space used
1188 		 * (e.g., because control messages are not yet internalized).
1189 		 */
1190 		switch (so->so_type) {
1191 		case SOCK_STREAM:
1192 			if (control != NULL) {
1193 				sbappendcontrol_locked(&so2->so_rcv, m,
1194 				    control);
1195 				control = NULL;
1196 			} else
1197 				sbappend_locked(&so2->so_rcv, m, flags);
1198 			break;
1199 
1200 		case SOCK_SEQPACKET: {
1201 			const struct sockaddr *from;
1202 
1203 			from = &sun_noname;
1204 			if (sbappendaddr_nospacecheck_locked(&so2->so_rcv,
1205 			    from, m, control))
1206 				control = NULL;
1207 			break;
1208 			}
1209 		}
1210 
1211 		mbcnt = so2->so_rcv.sb_mbcnt;
1212 		sbcc = sbavail(&so2->so_rcv);
1213 		if (sbcc)
1214 			sorwakeup_locked(so2);
1215 		else
1216 			SOCKBUF_UNLOCK(&so2->so_rcv);
1217 
1218 		/*
1219 		 * The PCB lock on unp2 protects the SB_STOP flag.  Without it,
1220 		 * it would be possible for uipc_rcvd to be called at this
1221 		 * point, drain the receiving sockbuf, clear SB_STOP, and then
1222 		 * we would set SB_STOP below.  That could lead to an empty
1223 		 * sockbuf having SB_STOP set
1224 		 */
1225 		SOCKBUF_LOCK(&so->so_snd);
1226 		if (sbcc >= so->so_snd.sb_hiwat || mbcnt >= so->so_snd.sb_mbmax)
1227 			so->so_snd.sb_flags |= SB_STOP;
1228 		SOCKBUF_UNLOCK(&so->so_snd);
1229 		UNP_PCB_UNLOCK(unp2);
1230 		m = NULL;
1231 		break;
1232 	}
1233 
1234 	/*
1235 	 * PRUS_EOF is equivalent to pru_send followed by pru_shutdown.
1236 	 */
1237 	if (flags & PRUS_EOF) {
1238 		UNP_PCB_LOCK(unp);
1239 		socantsendmore(so);
1240 		unp_shutdown(unp);
1241 		UNP_PCB_UNLOCK(unp);
1242 	}
1243 	if (control != NULL && error != 0)
1244 		unp_dispose_mbuf(control);
1245 
1246 release:
1247 	if (control != NULL)
1248 		m_freem(control);
1249 	/*
1250 	 * In case of PRUS_NOTREADY, uipc_ready() is responsible
1251 	 * for freeing memory.
1252 	 */
1253 	if (m != NULL && (flags & PRUS_NOTREADY) == 0)
1254 		m_freem(m);
1255 	return (error);
1256 }
1257 
1258 static int
1259 uipc_ready(struct socket *so, struct mbuf *m, int count)
1260 {
1261 	struct unpcb *unp, *unp2;
1262 	struct socket *so2;
1263 	int error;
1264 
1265 	unp = sotounpcb(so);
1266 
1267 	UNP_PCB_LOCK(unp);
1268 	if ((unp2 = unp->unp_conn) == NULL) {
1269 		UNP_PCB_UNLOCK(unp);
1270 		goto error;
1271 	}
1272 	if (unp != unp2) {
1273 		if (UNP_PCB_TRYLOCK(unp2) == 0) {
1274 			unp_pcb_hold(unp2);
1275 			UNP_PCB_UNLOCK(unp);
1276 			UNP_PCB_LOCK(unp2);
1277 			if (unp_pcb_rele(unp2))
1278 				goto error;
1279 		} else
1280 			UNP_PCB_UNLOCK(unp);
1281 	}
1282 	so2 = unp2->unp_socket;
1283 
1284 	SOCKBUF_LOCK(&so2->so_rcv);
1285 	if ((error = sbready(&so2->so_rcv, m, count)) == 0)
1286 		sorwakeup_locked(so2);
1287 	else
1288 		SOCKBUF_UNLOCK(&so2->so_rcv);
1289 
1290 	UNP_PCB_UNLOCK(unp2);
1291 
1292 	return (error);
1293  error:
1294 	for (int i = 0; i < count; i++)
1295 		m = m_free(m);
1296 	return (ECONNRESET);
1297 }
1298 
1299 static int
1300 uipc_sense(struct socket *so, struct stat *sb)
1301 {
1302 	struct unpcb *unp;
1303 
1304 	unp = sotounpcb(so);
1305 	KASSERT(unp != NULL, ("uipc_sense: unp == NULL"));
1306 
1307 	sb->st_blksize = so->so_snd.sb_hiwat;
1308 	sb->st_dev = NODEV;
1309 	sb->st_ino = unp->unp_ino;
1310 	return (0);
1311 }
1312 
1313 static int
1314 uipc_shutdown(struct socket *so)
1315 {
1316 	struct unpcb *unp;
1317 
1318 	unp = sotounpcb(so);
1319 	KASSERT(unp != NULL, ("uipc_shutdown: unp == NULL"));
1320 
1321 	UNP_PCB_LOCK(unp);
1322 	socantsendmore(so);
1323 	unp_shutdown(unp);
1324 	UNP_PCB_UNLOCK(unp);
1325 	return (0);
1326 }
1327 
1328 static int
1329 uipc_sockaddr(struct socket *so, struct sockaddr **nam)
1330 {
1331 	struct unpcb *unp;
1332 	const struct sockaddr *sa;
1333 
1334 	unp = sotounpcb(so);
1335 	KASSERT(unp != NULL, ("uipc_sockaddr: unp == NULL"));
1336 
1337 	*nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK);
1338 	UNP_PCB_LOCK(unp);
1339 	if (unp->unp_addr != NULL)
1340 		sa = (struct sockaddr *) unp->unp_addr;
1341 	else
1342 		sa = &sun_noname;
1343 	bcopy(sa, *nam, sa->sa_len);
1344 	UNP_PCB_UNLOCK(unp);
1345 	return (0);
1346 }
1347 
1348 static struct pr_usrreqs uipc_usrreqs_dgram = {
1349 	.pru_abort = 		uipc_abort,
1350 	.pru_accept =		uipc_accept,
1351 	.pru_attach =		uipc_attach,
1352 	.pru_bind =		uipc_bind,
1353 	.pru_bindat =		uipc_bindat,
1354 	.pru_connect =		uipc_connect,
1355 	.pru_connectat =	uipc_connectat,
1356 	.pru_connect2 =		uipc_connect2,
1357 	.pru_detach =		uipc_detach,
1358 	.pru_disconnect =	uipc_disconnect,
1359 	.pru_listen =		uipc_listen,
1360 	.pru_peeraddr =		uipc_peeraddr,
1361 	.pru_rcvd =		uipc_rcvd,
1362 	.pru_send =		uipc_send,
1363 	.pru_sense =		uipc_sense,
1364 	.pru_shutdown =		uipc_shutdown,
1365 	.pru_sockaddr =		uipc_sockaddr,
1366 	.pru_soreceive =	soreceive_dgram,
1367 	.pru_close =		uipc_close,
1368 };
1369 
1370 static struct pr_usrreqs uipc_usrreqs_seqpacket = {
1371 	.pru_abort =		uipc_abort,
1372 	.pru_accept =		uipc_accept,
1373 	.pru_attach =		uipc_attach,
1374 	.pru_bind =		uipc_bind,
1375 	.pru_bindat =		uipc_bindat,
1376 	.pru_connect =		uipc_connect,
1377 	.pru_connectat =	uipc_connectat,
1378 	.pru_connect2 =		uipc_connect2,
1379 	.pru_detach =		uipc_detach,
1380 	.pru_disconnect =	uipc_disconnect,
1381 	.pru_listen =		uipc_listen,
1382 	.pru_peeraddr =		uipc_peeraddr,
1383 	.pru_rcvd =		uipc_rcvd,
1384 	.pru_send =		uipc_send,
1385 	.pru_sense =		uipc_sense,
1386 	.pru_shutdown =		uipc_shutdown,
1387 	.pru_sockaddr =		uipc_sockaddr,
1388 	.pru_soreceive =	soreceive_generic,	/* XXX: or...? */
1389 	.pru_close =		uipc_close,
1390 };
1391 
1392 static struct pr_usrreqs uipc_usrreqs_stream = {
1393 	.pru_abort = 		uipc_abort,
1394 	.pru_accept =		uipc_accept,
1395 	.pru_attach =		uipc_attach,
1396 	.pru_bind =		uipc_bind,
1397 	.pru_bindat =		uipc_bindat,
1398 	.pru_connect =		uipc_connect,
1399 	.pru_connectat =	uipc_connectat,
1400 	.pru_connect2 =		uipc_connect2,
1401 	.pru_detach =		uipc_detach,
1402 	.pru_disconnect =	uipc_disconnect,
1403 	.pru_listen =		uipc_listen,
1404 	.pru_peeraddr =		uipc_peeraddr,
1405 	.pru_rcvd =		uipc_rcvd,
1406 	.pru_send =		uipc_send,
1407 	.pru_ready =		uipc_ready,
1408 	.pru_sense =		uipc_sense,
1409 	.pru_shutdown =		uipc_shutdown,
1410 	.pru_sockaddr =		uipc_sockaddr,
1411 	.pru_soreceive =	soreceive_generic,
1412 	.pru_close =		uipc_close,
1413 };
1414 
1415 static int
1416 uipc_ctloutput(struct socket *so, struct sockopt *sopt)
1417 {
1418 	struct unpcb *unp;
1419 	struct xucred xu;
1420 	int error, optval;
1421 
1422 	if (sopt->sopt_level != 0)
1423 		return (EINVAL);
1424 
1425 	unp = sotounpcb(so);
1426 	KASSERT(unp != NULL, ("uipc_ctloutput: unp == NULL"));
1427 	error = 0;
1428 	switch (sopt->sopt_dir) {
1429 	case SOPT_GET:
1430 		switch (sopt->sopt_name) {
1431 		case LOCAL_PEERCRED:
1432 			UNP_PCB_LOCK(unp);
1433 			if (unp->unp_flags & UNP_HAVEPC)
1434 				xu = unp->unp_peercred;
1435 			else {
1436 				if (so->so_type == SOCK_STREAM)
1437 					error = ENOTCONN;
1438 				else
1439 					error = EINVAL;
1440 			}
1441 			UNP_PCB_UNLOCK(unp);
1442 			if (error == 0)
1443 				error = sooptcopyout(sopt, &xu, sizeof(xu));
1444 			break;
1445 
1446 		case LOCAL_CREDS:
1447 			/* Unlocked read. */
1448 			optval = unp->unp_flags & UNP_WANTCRED ? 1 : 0;
1449 			error = sooptcopyout(sopt, &optval, sizeof(optval));
1450 			break;
1451 
1452 		case LOCAL_CONNWAIT:
1453 			/* Unlocked read. */
1454 			optval = unp->unp_flags & UNP_CONNWAIT ? 1 : 0;
1455 			error = sooptcopyout(sopt, &optval, sizeof(optval));
1456 			break;
1457 
1458 		default:
1459 			error = EOPNOTSUPP;
1460 			break;
1461 		}
1462 		break;
1463 
1464 	case SOPT_SET:
1465 		switch (sopt->sopt_name) {
1466 		case LOCAL_CREDS:
1467 		case LOCAL_CONNWAIT:
1468 			error = sooptcopyin(sopt, &optval, sizeof(optval),
1469 					    sizeof(optval));
1470 			if (error)
1471 				break;
1472 
1473 #define	OPTSET(bit) do {						\
1474 	UNP_PCB_LOCK(unp);						\
1475 	if (optval)							\
1476 		unp->unp_flags |= bit;					\
1477 	else								\
1478 		unp->unp_flags &= ~bit;					\
1479 	UNP_PCB_UNLOCK(unp);						\
1480 } while (0)
1481 
1482 			switch (sopt->sopt_name) {
1483 			case LOCAL_CREDS:
1484 				OPTSET(UNP_WANTCRED);
1485 				break;
1486 
1487 			case LOCAL_CONNWAIT:
1488 				OPTSET(UNP_CONNWAIT);
1489 				break;
1490 
1491 			default:
1492 				break;
1493 			}
1494 			break;
1495 #undef	OPTSET
1496 		default:
1497 			error = ENOPROTOOPT;
1498 			break;
1499 		}
1500 		break;
1501 
1502 	default:
1503 		error = EOPNOTSUPP;
1504 		break;
1505 	}
1506 	return (error);
1507 }
1508 
1509 static int
1510 unp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
1511 {
1512 
1513 	return (unp_connectat(AT_FDCWD, so, nam, td));
1514 }
1515 
1516 static int
1517 unp_connectat(int fd, struct socket *so, struct sockaddr *nam,
1518     struct thread *td)
1519 {
1520 	struct sockaddr_un *soun = (struct sockaddr_un *)nam;
1521 	struct vnode *vp;
1522 	struct socket *so2;
1523 	struct unpcb *unp, *unp2, *unp3;
1524 	struct nameidata nd;
1525 	char buf[SOCK_MAXADDRLEN];
1526 	struct sockaddr *sa;
1527 	cap_rights_t rights;
1528 	int error, len, freed;
1529 	struct mtx *vplock;
1530 
1531 	if (nam->sa_family != AF_UNIX)
1532 		return (EAFNOSUPPORT);
1533 	if (nam->sa_len > sizeof(struct sockaddr_un))
1534 		return (EINVAL);
1535 	len = nam->sa_len - offsetof(struct sockaddr_un, sun_path);
1536 	if (len <= 0)
1537 		return (EINVAL);
1538 	bcopy(soun->sun_path, buf, len);
1539 	buf[len] = 0;
1540 
1541 	unp = sotounpcb(so);
1542 	UNP_PCB_LOCK(unp);
1543 	if (unp->unp_flags & UNP_CONNECTING) {
1544 		UNP_PCB_UNLOCK(unp);
1545 		return (EALREADY);
1546 	}
1547 	unp->unp_flags |= UNP_CONNECTING;
1548 	UNP_PCB_UNLOCK(unp);
1549 
1550 	sa = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK);
1551 	NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF,
1552 	    UIO_SYSSPACE, buf, fd, cap_rights_init(&rights, CAP_CONNECTAT), td);
1553 	error = namei(&nd);
1554 	if (error)
1555 		vp = NULL;
1556 	else
1557 		vp = nd.ni_vp;
1558 	ASSERT_VOP_LOCKED(vp, "unp_connect");
1559 	NDFREE(&nd, NDF_ONLY_PNBUF);
1560 	if (error)
1561 		goto bad;
1562 
1563 	if (vp->v_type != VSOCK) {
1564 		error = ENOTSOCK;
1565 		goto bad;
1566 	}
1567 #ifdef MAC
1568 	error = mac_vnode_check_open(td->td_ucred, vp, VWRITE | VREAD);
1569 	if (error)
1570 		goto bad;
1571 #endif
1572 	error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td);
1573 	if (error)
1574 		goto bad;
1575 
1576 	unp = sotounpcb(so);
1577 	KASSERT(unp != NULL, ("unp_connect: unp == NULL"));
1578 
1579 	vplock = mtx_pool_find(mtxpool_sleep, vp);
1580 	mtx_lock(vplock);
1581 	VOP_UNP_CONNECT(vp, &unp2);
1582 	if (unp2 == NULL) {
1583 		error = ECONNREFUSED;
1584 		goto bad2;
1585 	}
1586 	so2 = unp2->unp_socket;
1587 	if (so->so_type != so2->so_type) {
1588 		error = EPROTOTYPE;
1589 		goto bad2;
1590 	}
1591 	if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
1592 		if (so2->so_options & SO_ACCEPTCONN) {
1593 			CURVNET_SET(so2->so_vnet);
1594 			so2 = sonewconn(so2, 0);
1595 			CURVNET_RESTORE();
1596 		} else
1597 			so2 = NULL;
1598 		if (so2 == NULL) {
1599 			error = ECONNREFUSED;
1600 			goto bad2;
1601 		}
1602 		unp3 = sotounpcb(so2);
1603 		unp_pcb_lock2(unp2, unp3);
1604 		if (unp2->unp_addr != NULL) {
1605 			bcopy(unp2->unp_addr, sa, unp2->unp_addr->sun_len);
1606 			unp3->unp_addr = (struct sockaddr_un *) sa;
1607 			sa = NULL;
1608 		}
1609 
1610 		unp_copy_peercred(td, unp3, unp, unp2);
1611 
1612 		UNP_PCB_UNLOCK(unp2);
1613 		unp2 = unp3;
1614 		unp_pcb_owned_lock2(unp2, unp, freed);
1615 		if (__predict_false(freed)) {
1616 			UNP_PCB_UNLOCK(unp2);
1617 			error = ECONNREFUSED;
1618 			goto bad2;
1619 		}
1620 #ifdef MAC
1621 		mac_socketpeer_set_from_socket(so, so2);
1622 		mac_socketpeer_set_from_socket(so2, so);
1623 #endif
1624 	} else {
1625 		if (unp == unp2)
1626 			UNP_PCB_LOCK(unp);
1627 		else
1628 			unp_pcb_lock2(unp, unp2);
1629 	}
1630 	KASSERT(unp2 != NULL && so2 != NULL && unp2->unp_socket == so2 &&
1631 	    sotounpcb(so2) == unp2,
1632 	    ("%s: unp2 %p so2 %p", __func__, unp2, so2));
1633 	error = unp_connect2(so, so2, PRU_CONNECT);
1634 	if (unp != unp2)
1635 		UNP_PCB_UNLOCK(unp2);
1636 	UNP_PCB_UNLOCK(unp);
1637 bad2:
1638 	mtx_unlock(vplock);
1639 bad:
1640 	if (vp != NULL) {
1641 		vput(vp);
1642 	}
1643 	free(sa, M_SONAME);
1644 	UNP_PCB_LOCK(unp);
1645 	unp->unp_flags &= ~UNP_CONNECTING;
1646 	UNP_PCB_UNLOCK(unp);
1647 	return (error);
1648 }
1649 
1650 /*
1651  * Set socket peer credentials at connection time.
1652  *
1653  * The client's PCB credentials are copied from its process structure.  The
1654  * server's PCB credentials are copied from the socket on which it called
1655  * listen(2).  uipc_listen cached that process's credentials at the time.
1656  */
1657 void
1658 unp_copy_peercred(struct thread *td, struct unpcb *client_unp,
1659     struct unpcb *server_unp, struct unpcb *listen_unp)
1660 {
1661 	cru2xt(td, &client_unp->unp_peercred);
1662 	client_unp->unp_flags |= UNP_HAVEPC;
1663 
1664 	memcpy(&server_unp->unp_peercred, &listen_unp->unp_peercred,
1665 	    sizeof(server_unp->unp_peercred));
1666 	server_unp->unp_flags |= UNP_HAVEPC;
1667 	if (listen_unp->unp_flags & UNP_WANTCRED)
1668 		client_unp->unp_flags |= UNP_WANTCRED;
1669 }
1670 
1671 static int
1672 unp_connect2(struct socket *so, struct socket *so2, int req)
1673 {
1674 	struct unpcb *unp;
1675 	struct unpcb *unp2;
1676 
1677 	unp = sotounpcb(so);
1678 	KASSERT(unp != NULL, ("unp_connect2: unp == NULL"));
1679 	unp2 = sotounpcb(so2);
1680 	KASSERT(unp2 != NULL, ("unp_connect2: unp2 == NULL"));
1681 
1682 	UNP_PCB_LOCK_ASSERT(unp);
1683 	UNP_PCB_LOCK_ASSERT(unp2);
1684 
1685 	if (so2->so_type != so->so_type)
1686 		return (EPROTOTYPE);
1687 	unp2->unp_flags &= ~UNP_NASCENT;
1688 	unp->unp_conn = unp2;
1689 	unp_pcb_hold(unp2);
1690 	unp_pcb_hold(unp);
1691 	switch (so->so_type) {
1692 	case SOCK_DGRAM:
1693 		UNP_REF_LIST_LOCK();
1694 		LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink);
1695 		UNP_REF_LIST_UNLOCK();
1696 		soisconnected(so);
1697 		break;
1698 
1699 	case SOCK_STREAM:
1700 	case SOCK_SEQPACKET:
1701 		unp2->unp_conn = unp;
1702 		if (req == PRU_CONNECT &&
1703 		    ((unp->unp_flags | unp2->unp_flags) & UNP_CONNWAIT))
1704 			soisconnecting(so);
1705 		else
1706 			soisconnected(so);
1707 		soisconnected(so2);
1708 		break;
1709 
1710 	default:
1711 		panic("unp_connect2");
1712 	}
1713 	return (0);
1714 }
1715 
1716 static void
1717 unp_disconnect(struct unpcb *unp, struct unpcb *unp2)
1718 {
1719 	struct socket *so, *so2;
1720 	int freed __unused;
1721 
1722 	KASSERT(unp2 != NULL, ("unp_disconnect: unp2 == NULL"));
1723 
1724 	UNP_PCB_LOCK_ASSERT(unp);
1725 	UNP_PCB_LOCK_ASSERT(unp2);
1726 
1727 	if (unp->unp_conn == NULL && unp2->unp_conn == NULL)
1728 		return;
1729 
1730 	MPASS(unp->unp_conn == unp2);
1731 	unp->unp_conn = NULL;
1732 	so = unp->unp_socket;
1733 	so2 = unp2->unp_socket;
1734 	switch (unp->unp_socket->so_type) {
1735 	case SOCK_DGRAM:
1736 		UNP_REF_LIST_LOCK();
1737 		LIST_REMOVE(unp, unp_reflink);
1738 		UNP_REF_LIST_UNLOCK();
1739 		if (so) {
1740 			SOCK_LOCK(so);
1741 			so->so_state &= ~SS_ISCONNECTED;
1742 			SOCK_UNLOCK(so);
1743 		}
1744 		break;
1745 
1746 	case SOCK_STREAM:
1747 	case SOCK_SEQPACKET:
1748 		if (so)
1749 			soisdisconnected(so);
1750 		MPASS(unp2->unp_conn == unp);
1751 		unp2->unp_conn = NULL;
1752 		if (so2)
1753 			soisdisconnected(so2);
1754 		break;
1755 	}
1756 	freed = unp_pcb_rele(unp);
1757 	MPASS(freed == 0);
1758 	freed = unp_pcb_rele(unp2);
1759 	MPASS(freed == 0);
1760 }
1761 
1762 /*
1763  * unp_pcblist() walks the global list of struct unpcb's to generate a
1764  * pointer list, bumping the refcount on each unpcb.  It then copies them out
1765  * sequentially, validating the generation number on each to see if it has
1766  * been detached.  All of this is necessary because copyout() may sleep on
1767  * disk I/O.
1768  */
1769 static int
1770 unp_pcblist(SYSCTL_HANDLER_ARGS)
1771 {
1772 	struct unpcb *unp, **unp_list;
1773 	unp_gen_t gencnt;
1774 	struct xunpgen *xug;
1775 	struct unp_head *head;
1776 	struct xunpcb *xu;
1777 	u_int i;
1778 	int error, freeunp, n;
1779 
1780 	switch ((intptr_t)arg1) {
1781 	case SOCK_STREAM:
1782 		head = &unp_shead;
1783 		break;
1784 
1785 	case SOCK_DGRAM:
1786 		head = &unp_dhead;
1787 		break;
1788 
1789 	case SOCK_SEQPACKET:
1790 		head = &unp_sphead;
1791 		break;
1792 
1793 	default:
1794 		panic("unp_pcblist: arg1 %d", (int)(intptr_t)arg1);
1795 	}
1796 
1797 	/*
1798 	 * The process of preparing the PCB list is too time-consuming and
1799 	 * resource-intensive to repeat twice on every request.
1800 	 */
1801 	if (req->oldptr == NULL) {
1802 		n = unp_count;
1803 		req->oldidx = 2 * (sizeof *xug)
1804 			+ (n + n/8) * sizeof(struct xunpcb);
1805 		return (0);
1806 	}
1807 
1808 	if (req->newptr != NULL)
1809 		return (EPERM);
1810 
1811 	/*
1812 	 * OK, now we're committed to doing something.
1813 	 */
1814 	xug = malloc(sizeof(*xug), M_TEMP, M_WAITOK | M_ZERO);
1815 	UNP_LINK_RLOCK();
1816 	gencnt = unp_gencnt;
1817 	n = unp_count;
1818 	UNP_LINK_RUNLOCK();
1819 
1820 	xug->xug_len = sizeof *xug;
1821 	xug->xug_count = n;
1822 	xug->xug_gen = gencnt;
1823 	xug->xug_sogen = so_gencnt;
1824 	error = SYSCTL_OUT(req, xug, sizeof *xug);
1825 	if (error) {
1826 		free(xug, M_TEMP);
1827 		return (error);
1828 	}
1829 
1830 	unp_list = malloc(n * sizeof *unp_list, M_TEMP, M_WAITOK);
1831 
1832 	UNP_LINK_RLOCK();
1833 	for (unp = LIST_FIRST(head), i = 0; unp && i < n;
1834 	     unp = LIST_NEXT(unp, unp_link)) {
1835 		UNP_PCB_LOCK(unp);
1836 		if (unp->unp_gencnt <= gencnt) {
1837 			if (cr_cansee(req->td->td_ucred,
1838 			    unp->unp_socket->so_cred)) {
1839 				UNP_PCB_UNLOCK(unp);
1840 				continue;
1841 			}
1842 			unp_list[i++] = unp;
1843 			unp_pcb_hold(unp);
1844 		}
1845 		UNP_PCB_UNLOCK(unp);
1846 	}
1847 	UNP_LINK_RUNLOCK();
1848 	n = i;			/* In case we lost some during malloc. */
1849 
1850 	error = 0;
1851 	xu = malloc(sizeof(*xu), M_TEMP, M_WAITOK | M_ZERO);
1852 	for (i = 0; i < n; i++) {
1853 		unp = unp_list[i];
1854 		UNP_PCB_LOCK(unp);
1855 		freeunp = unp_pcb_rele(unp);
1856 
1857 		if (freeunp == 0 && unp->unp_gencnt <= gencnt) {
1858 			xu->xu_len = sizeof *xu;
1859 			xu->xu_unpp = (uintptr_t)unp;
1860 			/*
1861 			 * XXX - need more locking here to protect against
1862 			 * connect/disconnect races for SMP.
1863 			 */
1864 			if (unp->unp_addr != NULL)
1865 				bcopy(unp->unp_addr, &xu->xu_addr,
1866 				      unp->unp_addr->sun_len);
1867 			else
1868 				bzero(&xu->xu_addr, sizeof(xu->xu_addr));
1869 			if (unp->unp_conn != NULL &&
1870 			    unp->unp_conn->unp_addr != NULL)
1871 				bcopy(unp->unp_conn->unp_addr,
1872 				      &xu->xu_caddr,
1873 				      unp->unp_conn->unp_addr->sun_len);
1874 			else
1875 				bzero(&xu->xu_caddr, sizeof(xu->xu_caddr));
1876 			xu->unp_vnode = (uintptr_t)unp->unp_vnode;
1877 			xu->unp_conn = (uintptr_t)unp->unp_conn;
1878 			xu->xu_firstref = (uintptr_t)LIST_FIRST(&unp->unp_refs);
1879 			xu->xu_nextref = (uintptr_t)LIST_NEXT(unp, unp_reflink);
1880 			xu->unp_gencnt = unp->unp_gencnt;
1881 			sotoxsocket(unp->unp_socket, &xu->xu_socket);
1882 			UNP_PCB_UNLOCK(unp);
1883 			error = SYSCTL_OUT(req, xu, sizeof *xu);
1884 		} else  if (freeunp == 0)
1885 			UNP_PCB_UNLOCK(unp);
1886 	}
1887 	free(xu, M_TEMP);
1888 	if (!error) {
1889 		/*
1890 		 * Give the user an updated idea of our state.  If the
1891 		 * generation differs from what we told her before, she knows
1892 		 * that something happened while we were processing this
1893 		 * request, and it might be necessary to retry.
1894 		 */
1895 		xug->xug_gen = unp_gencnt;
1896 		xug->xug_sogen = so_gencnt;
1897 		xug->xug_count = unp_count;
1898 		error = SYSCTL_OUT(req, xug, sizeof *xug);
1899 	}
1900 	free(unp_list, M_TEMP);
1901 	free(xug, M_TEMP);
1902 	return (error);
1903 }
1904 
1905 SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLTYPE_OPAQUE | CTLFLAG_RD,
1906     (void *)(intptr_t)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb",
1907     "List of active local datagram sockets");
1908 SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, CTLTYPE_OPAQUE | CTLFLAG_RD,
1909     (void *)(intptr_t)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb",
1910     "List of active local stream sockets");
1911 SYSCTL_PROC(_net_local_seqpacket, OID_AUTO, pcblist,
1912     CTLTYPE_OPAQUE | CTLFLAG_RD,
1913     (void *)(intptr_t)SOCK_SEQPACKET, 0, unp_pcblist, "S,xunpcb",
1914     "List of active local seqpacket sockets");
1915 
1916 static void
1917 unp_shutdown(struct unpcb *unp)
1918 {
1919 	struct unpcb *unp2;
1920 	struct socket *so;
1921 
1922 	UNP_PCB_LOCK_ASSERT(unp);
1923 
1924 	unp2 = unp->unp_conn;
1925 	if ((unp->unp_socket->so_type == SOCK_STREAM ||
1926 	    (unp->unp_socket->so_type == SOCK_SEQPACKET)) && unp2 != NULL) {
1927 		so = unp2->unp_socket;
1928 		if (so != NULL)
1929 			socantrcvmore(so);
1930 	}
1931 }
1932 
1933 static void
1934 unp_drop(struct unpcb *unp)
1935 {
1936 	struct socket *so = unp->unp_socket;
1937 	struct unpcb *unp2;
1938 	int freed;
1939 
1940 	/*
1941 	 * Regardless of whether the socket's peer dropped the connection
1942 	 * with this socket by aborting or disconnecting, POSIX requires
1943 	 * that ECONNRESET is returned.
1944 	 */
1945 	/* acquire a reference so that unp isn't freed from underneath us */
1946 
1947 	UNP_PCB_LOCK(unp);
1948 	if (so)
1949 		so->so_error = ECONNRESET;
1950 	unp2 = unp->unp_conn;
1951 	if (unp2 == unp) {
1952 		unp_disconnect(unp, unp2);
1953 	} else if (unp2 != NULL) {
1954 		unp_pcb_hold(unp2);
1955 		unp_pcb_owned_lock2(unp, unp2, freed);
1956 		unp_disconnect(unp, unp2);
1957 		if (unp_pcb_rele(unp2) == 0)
1958 			UNP_PCB_UNLOCK(unp2);
1959 	}
1960 	if (unp_pcb_rele(unp) == 0)
1961 		UNP_PCB_UNLOCK(unp);
1962 }
1963 
1964 static void
1965 unp_freerights(struct filedescent **fdep, int fdcount)
1966 {
1967 	struct file *fp;
1968 	int i;
1969 
1970 	KASSERT(fdcount > 0, ("%s: fdcount %d", __func__, fdcount));
1971 
1972 	for (i = 0; i < fdcount; i++) {
1973 		fp = fdep[i]->fde_file;
1974 		filecaps_free(&fdep[i]->fde_caps);
1975 		unp_discard(fp);
1976 	}
1977 	free(fdep[0], M_FILECAPS);
1978 }
1979 
1980 static int
1981 unp_externalize(struct mbuf *control, struct mbuf **controlp, int flags)
1982 {
1983 	struct thread *td = curthread;		/* XXX */
1984 	struct cmsghdr *cm = mtod(control, struct cmsghdr *);
1985 	int i;
1986 	int *fdp;
1987 	struct filedesc *fdesc = td->td_proc->p_fd;
1988 	struct filedescent **fdep;
1989 	void *data;
1990 	socklen_t clen = control->m_len, datalen;
1991 	int error, newfds;
1992 	u_int newlen;
1993 
1994 	UNP_LINK_UNLOCK_ASSERT();
1995 
1996 	error = 0;
1997 	if (controlp != NULL) /* controlp == NULL => free control messages */
1998 		*controlp = NULL;
1999 	while (cm != NULL) {
2000 		if (sizeof(*cm) > clen || cm->cmsg_len > clen) {
2001 			error = EINVAL;
2002 			break;
2003 		}
2004 		data = CMSG_DATA(cm);
2005 		datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
2006 		if (cm->cmsg_level == SOL_SOCKET
2007 		    && cm->cmsg_type == SCM_RIGHTS) {
2008 			newfds = datalen / sizeof(*fdep);
2009 			if (newfds == 0)
2010 				goto next;
2011 			fdep = data;
2012 
2013 			/* If we're not outputting the descriptors free them. */
2014 			if (error || controlp == NULL) {
2015 				unp_freerights(fdep, newfds);
2016 				goto next;
2017 			}
2018 			FILEDESC_XLOCK(fdesc);
2019 
2020 			/*
2021 			 * Now change each pointer to an fd in the global
2022 			 * table to an integer that is the index to the local
2023 			 * fd table entry that we set up to point to the
2024 			 * global one we are transferring.
2025 			 */
2026 			newlen = newfds * sizeof(int);
2027 			*controlp = sbcreatecontrol(NULL, newlen,
2028 			    SCM_RIGHTS, SOL_SOCKET);
2029 			if (*controlp == NULL) {
2030 				FILEDESC_XUNLOCK(fdesc);
2031 				error = E2BIG;
2032 				unp_freerights(fdep, newfds);
2033 				goto next;
2034 			}
2035 
2036 			fdp = (int *)
2037 			    CMSG_DATA(mtod(*controlp, struct cmsghdr *));
2038 			if (fdallocn(td, 0, fdp, newfds) != 0) {
2039 				FILEDESC_XUNLOCK(fdesc);
2040 				error = EMSGSIZE;
2041 				unp_freerights(fdep, newfds);
2042 				m_freem(*controlp);
2043 				*controlp = NULL;
2044 				goto next;
2045 			}
2046 			for (i = 0; i < newfds; i++, fdp++) {
2047 				_finstall(fdesc, fdep[i]->fde_file, *fdp,
2048 				    (flags & MSG_CMSG_CLOEXEC) != 0 ? UF_EXCLOSE : 0,
2049 				    &fdep[i]->fde_caps);
2050 				unp_externalize_fp(fdep[i]->fde_file);
2051 			}
2052 
2053 			/*
2054 			 * The new type indicates that the mbuf data refers to
2055 			 * kernel resources that may need to be released before
2056 			 * the mbuf is freed.
2057 			 */
2058 			m_chtype(*controlp, MT_EXTCONTROL);
2059 			FILEDESC_XUNLOCK(fdesc);
2060 			free(fdep[0], M_FILECAPS);
2061 		} else {
2062 			/* We can just copy anything else across. */
2063 			if (error || controlp == NULL)
2064 				goto next;
2065 			*controlp = sbcreatecontrol(NULL, datalen,
2066 			    cm->cmsg_type, cm->cmsg_level);
2067 			if (*controlp == NULL) {
2068 				error = ENOBUFS;
2069 				goto next;
2070 			}
2071 			bcopy(data,
2072 			    CMSG_DATA(mtod(*controlp, struct cmsghdr *)),
2073 			    datalen);
2074 		}
2075 		controlp = &(*controlp)->m_next;
2076 
2077 next:
2078 		if (CMSG_SPACE(datalen) < clen) {
2079 			clen -= CMSG_SPACE(datalen);
2080 			cm = (struct cmsghdr *)
2081 			    ((caddr_t)cm + CMSG_SPACE(datalen));
2082 		} else {
2083 			clen = 0;
2084 			cm = NULL;
2085 		}
2086 	}
2087 
2088 	m_freem(control);
2089 	return (error);
2090 }
2091 
2092 static void
2093 unp_zone_change(void *tag)
2094 {
2095 
2096 	uma_zone_set_max(unp_zone, maxsockets);
2097 }
2098 
2099 static void
2100 unp_init(void)
2101 {
2102 
2103 #ifdef VIMAGE
2104 	if (!IS_DEFAULT_VNET(curvnet))
2105 		return;
2106 #endif
2107 	unp_zone = uma_zcreate("unpcb", sizeof(struct unpcb), NULL, NULL,
2108 	    NULL, NULL, UMA_ALIGN_CACHE, 0);
2109 	if (unp_zone == NULL)
2110 		panic("unp_init");
2111 	uma_zone_set_max(unp_zone, maxsockets);
2112 	uma_zone_set_warning(unp_zone, "kern.ipc.maxsockets limit reached");
2113 	EVENTHANDLER_REGISTER(maxsockets_change, unp_zone_change,
2114 	    NULL, EVENTHANDLER_PRI_ANY);
2115 	LIST_INIT(&unp_dhead);
2116 	LIST_INIT(&unp_shead);
2117 	LIST_INIT(&unp_sphead);
2118 	SLIST_INIT(&unp_defers);
2119 	TIMEOUT_TASK_INIT(taskqueue_thread, &unp_gc_task, 0, unp_gc, NULL);
2120 	TASK_INIT(&unp_defer_task, 0, unp_process_defers, NULL);
2121 	UNP_LINK_LOCK_INIT();
2122 	UNP_DEFERRED_LOCK_INIT();
2123 }
2124 
2125 static void
2126 unp_internalize_cleanup_rights(struct mbuf *control)
2127 {
2128 	struct cmsghdr *cp;
2129 	struct mbuf *m;
2130 	void *data;
2131 	socklen_t datalen;
2132 
2133 	for (m = control; m != NULL; m = m->m_next) {
2134 		cp = mtod(m, struct cmsghdr *);
2135 		if (cp->cmsg_level != SOL_SOCKET ||
2136 		    cp->cmsg_type != SCM_RIGHTS)
2137 			continue;
2138 		data = CMSG_DATA(cp);
2139 		datalen = (caddr_t)cp + cp->cmsg_len - (caddr_t)data;
2140 		unp_freerights(data, datalen / sizeof(struct filedesc *));
2141 	}
2142 }
2143 
2144 static int
2145 unp_internalize(struct mbuf **controlp, struct thread *td)
2146 {
2147 	struct mbuf *control, **initial_controlp;
2148 	struct proc *p;
2149 	struct filedesc *fdesc;
2150 	struct bintime *bt;
2151 	struct cmsghdr *cm;
2152 	struct cmsgcred *cmcred;
2153 	struct filedescent *fde, **fdep, *fdev;
2154 	struct file *fp;
2155 	struct timeval *tv;
2156 	struct timespec *ts;
2157 	void *data;
2158 	socklen_t clen, datalen;
2159 	int i, j, error, *fdp, oldfds;
2160 	u_int newlen;
2161 
2162 	UNP_LINK_UNLOCK_ASSERT();
2163 
2164 	p = td->td_proc;
2165 	fdesc = p->p_fd;
2166 	error = 0;
2167 	control = *controlp;
2168 	clen = control->m_len;
2169 	*controlp = NULL;
2170 	initial_controlp = controlp;
2171 	for (cm = mtod(control, struct cmsghdr *); cm != NULL;) {
2172 		if (sizeof(*cm) > clen || cm->cmsg_level != SOL_SOCKET
2173 		    || cm->cmsg_len > clen || cm->cmsg_len < sizeof(*cm)) {
2174 			error = EINVAL;
2175 			goto out;
2176 		}
2177 		data = CMSG_DATA(cm);
2178 		datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
2179 
2180 		switch (cm->cmsg_type) {
2181 		/*
2182 		 * Fill in credential information.
2183 		 */
2184 		case SCM_CREDS:
2185 			*controlp = sbcreatecontrol(NULL, sizeof(*cmcred),
2186 			    SCM_CREDS, SOL_SOCKET);
2187 			if (*controlp == NULL) {
2188 				error = ENOBUFS;
2189 				goto out;
2190 			}
2191 			cmcred = (struct cmsgcred *)
2192 			    CMSG_DATA(mtod(*controlp, struct cmsghdr *));
2193 			cmcred->cmcred_pid = p->p_pid;
2194 			cmcred->cmcred_uid = td->td_ucred->cr_ruid;
2195 			cmcred->cmcred_gid = td->td_ucred->cr_rgid;
2196 			cmcred->cmcred_euid = td->td_ucred->cr_uid;
2197 			cmcred->cmcred_ngroups = MIN(td->td_ucred->cr_ngroups,
2198 			    CMGROUP_MAX);
2199 			for (i = 0; i < cmcred->cmcred_ngroups; i++)
2200 				cmcred->cmcred_groups[i] =
2201 				    td->td_ucred->cr_groups[i];
2202 			break;
2203 
2204 		case SCM_RIGHTS:
2205 			oldfds = datalen / sizeof (int);
2206 			if (oldfds == 0)
2207 				break;
2208 			/*
2209 			 * Check that all the FDs passed in refer to legal
2210 			 * files.  If not, reject the entire operation.
2211 			 */
2212 			fdp = data;
2213 			FILEDESC_SLOCK(fdesc);
2214 			for (i = 0; i < oldfds; i++, fdp++) {
2215 				fp = fget_locked(fdesc, *fdp);
2216 				if (fp == NULL) {
2217 					FILEDESC_SUNLOCK(fdesc);
2218 					error = EBADF;
2219 					goto out;
2220 				}
2221 				if (!(fp->f_ops->fo_flags & DFLAG_PASSABLE)) {
2222 					FILEDESC_SUNLOCK(fdesc);
2223 					error = EOPNOTSUPP;
2224 					goto out;
2225 				}
2226 
2227 			}
2228 
2229 			/*
2230 			 * Now replace the integer FDs with pointers to the
2231 			 * file structure and capability rights.
2232 			 */
2233 			newlen = oldfds * sizeof(fdep[0]);
2234 			*controlp = sbcreatecontrol(NULL, newlen,
2235 			    SCM_RIGHTS, SOL_SOCKET);
2236 			if (*controlp == NULL) {
2237 				FILEDESC_SUNLOCK(fdesc);
2238 				error = E2BIG;
2239 				goto out;
2240 			}
2241 			fdp = data;
2242 			for (i = 0; i < oldfds; i++, fdp++) {
2243 				if (!fhold(fdesc->fd_ofiles[*fdp].fde_file)) {
2244 					fdp = data;
2245 					for (j = 0; j < i; j++, fdp++) {
2246 						fdrop(fdesc->fd_ofiles[*fdp].
2247 						    fde_file, td);
2248 					}
2249 					FILEDESC_SUNLOCK(fdesc);
2250 					error = EBADF;
2251 					goto out;
2252 				}
2253 			}
2254 			fdp = data;
2255 			fdep = (struct filedescent **)
2256 			    CMSG_DATA(mtod(*controlp, struct cmsghdr *));
2257 			fdev = malloc(sizeof(*fdev) * oldfds, M_FILECAPS,
2258 			    M_WAITOK);
2259 			for (i = 0; i < oldfds; i++, fdev++, fdp++) {
2260 				fde = &fdesc->fd_ofiles[*fdp];
2261 				fdep[i] = fdev;
2262 				fdep[i]->fde_file = fde->fde_file;
2263 				filecaps_copy(&fde->fde_caps,
2264 				    &fdep[i]->fde_caps, true);
2265 				unp_internalize_fp(fdep[i]->fde_file);
2266 			}
2267 			FILEDESC_SUNLOCK(fdesc);
2268 			break;
2269 
2270 		case SCM_TIMESTAMP:
2271 			*controlp = sbcreatecontrol(NULL, sizeof(*tv),
2272 			    SCM_TIMESTAMP, SOL_SOCKET);
2273 			if (*controlp == NULL) {
2274 				error = ENOBUFS;
2275 				goto out;
2276 			}
2277 			tv = (struct timeval *)
2278 			    CMSG_DATA(mtod(*controlp, struct cmsghdr *));
2279 			microtime(tv);
2280 			break;
2281 
2282 		case SCM_BINTIME:
2283 			*controlp = sbcreatecontrol(NULL, sizeof(*bt),
2284 			    SCM_BINTIME, SOL_SOCKET);
2285 			if (*controlp == NULL) {
2286 				error = ENOBUFS;
2287 				goto out;
2288 			}
2289 			bt = (struct bintime *)
2290 			    CMSG_DATA(mtod(*controlp, struct cmsghdr *));
2291 			bintime(bt);
2292 			break;
2293 
2294 		case SCM_REALTIME:
2295 			*controlp = sbcreatecontrol(NULL, sizeof(*ts),
2296 			    SCM_REALTIME, SOL_SOCKET);
2297 			if (*controlp == NULL) {
2298 				error = ENOBUFS;
2299 				goto out;
2300 			}
2301 			ts = (struct timespec *)
2302 			    CMSG_DATA(mtod(*controlp, struct cmsghdr *));
2303 			nanotime(ts);
2304 			break;
2305 
2306 		case SCM_MONOTONIC:
2307 			*controlp = sbcreatecontrol(NULL, sizeof(*ts),
2308 			    SCM_MONOTONIC, SOL_SOCKET);
2309 			if (*controlp == NULL) {
2310 				error = ENOBUFS;
2311 				goto out;
2312 			}
2313 			ts = (struct timespec *)
2314 			    CMSG_DATA(mtod(*controlp, struct cmsghdr *));
2315 			nanouptime(ts);
2316 			break;
2317 
2318 		default:
2319 			error = EINVAL;
2320 			goto out;
2321 		}
2322 
2323 		if (*controlp != NULL)
2324 			controlp = &(*controlp)->m_next;
2325 		if (CMSG_SPACE(datalen) < clen) {
2326 			clen -= CMSG_SPACE(datalen);
2327 			cm = (struct cmsghdr *)
2328 			    ((caddr_t)cm + CMSG_SPACE(datalen));
2329 		} else {
2330 			clen = 0;
2331 			cm = NULL;
2332 		}
2333 	}
2334 
2335 out:
2336 	if (error != 0 && initial_controlp != NULL)
2337 		unp_internalize_cleanup_rights(*initial_controlp);
2338 	m_freem(control);
2339 	return (error);
2340 }
2341 
2342 static struct mbuf *
2343 unp_addsockcred(struct thread *td, struct mbuf *control)
2344 {
2345 	struct mbuf *m, *n, *n_prev;
2346 	struct sockcred *sc;
2347 	const struct cmsghdr *cm;
2348 	int ngroups;
2349 	int i;
2350 
2351 	ngroups = MIN(td->td_ucred->cr_ngroups, CMGROUP_MAX);
2352 	m = sbcreatecontrol(NULL, SOCKCREDSIZE(ngroups), SCM_CREDS, SOL_SOCKET);
2353 	if (m == NULL)
2354 		return (control);
2355 
2356 	sc = (struct sockcred *) CMSG_DATA(mtod(m, struct cmsghdr *));
2357 	sc->sc_uid = td->td_ucred->cr_ruid;
2358 	sc->sc_euid = td->td_ucred->cr_uid;
2359 	sc->sc_gid = td->td_ucred->cr_rgid;
2360 	sc->sc_egid = td->td_ucred->cr_gid;
2361 	sc->sc_ngroups = ngroups;
2362 	for (i = 0; i < sc->sc_ngroups; i++)
2363 		sc->sc_groups[i] = td->td_ucred->cr_groups[i];
2364 
2365 	/*
2366 	 * Unlink SCM_CREDS control messages (struct cmsgcred), since just
2367 	 * created SCM_CREDS control message (struct sockcred) has another
2368 	 * format.
2369 	 */
2370 	if (control != NULL)
2371 		for (n = control, n_prev = NULL; n != NULL;) {
2372 			cm = mtod(n, struct cmsghdr *);
2373     			if (cm->cmsg_level == SOL_SOCKET &&
2374 			    cm->cmsg_type == SCM_CREDS) {
2375     				if (n_prev == NULL)
2376 					control = n->m_next;
2377 				else
2378 					n_prev->m_next = n->m_next;
2379 				n = m_free(n);
2380 			} else {
2381 				n_prev = n;
2382 				n = n->m_next;
2383 			}
2384 		}
2385 
2386 	/* Prepend it to the head. */
2387 	m->m_next = control;
2388 	return (m);
2389 }
2390 
2391 static struct unpcb *
2392 fptounp(struct file *fp)
2393 {
2394 	struct socket *so;
2395 
2396 	if (fp->f_type != DTYPE_SOCKET)
2397 		return (NULL);
2398 	if ((so = fp->f_data) == NULL)
2399 		return (NULL);
2400 	if (so->so_proto->pr_domain != &localdomain)
2401 		return (NULL);
2402 	return sotounpcb(so);
2403 }
2404 
2405 static void
2406 unp_discard(struct file *fp)
2407 {
2408 	struct unp_defer *dr;
2409 
2410 	if (unp_externalize_fp(fp)) {
2411 		dr = malloc(sizeof(*dr), M_TEMP, M_WAITOK);
2412 		dr->ud_fp = fp;
2413 		UNP_DEFERRED_LOCK();
2414 		SLIST_INSERT_HEAD(&unp_defers, dr, ud_link);
2415 		UNP_DEFERRED_UNLOCK();
2416 		atomic_add_int(&unp_defers_count, 1);
2417 		taskqueue_enqueue(taskqueue_thread, &unp_defer_task);
2418 	} else
2419 		(void) closef(fp, (struct thread *)NULL);
2420 }
2421 
2422 static void
2423 unp_process_defers(void *arg __unused, int pending)
2424 {
2425 	struct unp_defer *dr;
2426 	SLIST_HEAD(, unp_defer) drl;
2427 	int count;
2428 
2429 	SLIST_INIT(&drl);
2430 	for (;;) {
2431 		UNP_DEFERRED_LOCK();
2432 		if (SLIST_FIRST(&unp_defers) == NULL) {
2433 			UNP_DEFERRED_UNLOCK();
2434 			break;
2435 		}
2436 		SLIST_SWAP(&unp_defers, &drl, unp_defer);
2437 		UNP_DEFERRED_UNLOCK();
2438 		count = 0;
2439 		while ((dr = SLIST_FIRST(&drl)) != NULL) {
2440 			SLIST_REMOVE_HEAD(&drl, ud_link);
2441 			closef(dr->ud_fp, NULL);
2442 			free(dr, M_TEMP);
2443 			count++;
2444 		}
2445 		atomic_add_int(&unp_defers_count, -count);
2446 	}
2447 }
2448 
2449 static void
2450 unp_internalize_fp(struct file *fp)
2451 {
2452 	struct unpcb *unp;
2453 
2454 	UNP_LINK_WLOCK();
2455 	if ((unp = fptounp(fp)) != NULL) {
2456 		unp->unp_file = fp;
2457 		unp->unp_msgcount++;
2458 	}
2459 	unp_rights++;
2460 	UNP_LINK_WUNLOCK();
2461 }
2462 
2463 static int
2464 unp_externalize_fp(struct file *fp)
2465 {
2466 	struct unpcb *unp;
2467 	int ret;
2468 
2469 	UNP_LINK_WLOCK();
2470 	if ((unp = fptounp(fp)) != NULL) {
2471 		unp->unp_msgcount--;
2472 		ret = 1;
2473 	} else
2474 		ret = 0;
2475 	unp_rights--;
2476 	UNP_LINK_WUNLOCK();
2477 	return (ret);
2478 }
2479 
2480 /*
2481  * unp_defer indicates whether additional work has been defered for a future
2482  * pass through unp_gc().  It is thread local and does not require explicit
2483  * synchronization.
2484  */
2485 static int	unp_marked;
2486 
2487 static void
2488 unp_remove_dead_ref(struct filedescent **fdep, int fdcount)
2489 {
2490 	struct unpcb *unp;
2491 	struct file *fp;
2492 	int i;
2493 
2494 	/*
2495 	 * This function can only be called from the gc task.
2496 	 */
2497 	KASSERT(taskqueue_member(taskqueue_thread, curthread) != 0,
2498 	    ("%s: not on gc callout", __func__));
2499 	UNP_LINK_LOCK_ASSERT();
2500 
2501 	for (i = 0; i < fdcount; i++) {
2502 		fp = fdep[i]->fde_file;
2503 		if ((unp = fptounp(fp)) == NULL)
2504 			continue;
2505 		if ((unp->unp_gcflag & UNPGC_DEAD) == 0)
2506 			continue;
2507 		unp->unp_gcrefs--;
2508 	}
2509 }
2510 
2511 static void
2512 unp_restore_undead_ref(struct filedescent **fdep, int fdcount)
2513 {
2514 	struct unpcb *unp;
2515 	struct file *fp;
2516 	int i;
2517 
2518 	/*
2519 	 * This function can only be called from the gc task.
2520 	 */
2521 	KASSERT(taskqueue_member(taskqueue_thread, curthread) != 0,
2522 	    ("%s: not on gc callout", __func__));
2523 	UNP_LINK_LOCK_ASSERT();
2524 
2525 	for (i = 0; i < fdcount; i++) {
2526 		fp = fdep[i]->fde_file;
2527 		if ((unp = fptounp(fp)) == NULL)
2528 			continue;
2529 		if ((unp->unp_gcflag & UNPGC_DEAD) == 0)
2530 			continue;
2531 		unp->unp_gcrefs++;
2532 		unp_marked++;
2533 	}
2534 }
2535 
2536 static void
2537 unp_gc_scan(struct unpcb *unp, void (*op)(struct filedescent **, int))
2538 {
2539 	struct socket *so, *soa;
2540 
2541 	so = unp->unp_socket;
2542 	SOCK_LOCK(so);
2543 	if (SOLISTENING(so)) {
2544 		/*
2545 		 * Mark all sockets in our accept queue.
2546 		 */
2547 		TAILQ_FOREACH(soa, &so->sol_comp, so_list) {
2548 			if (sotounpcb(soa)->unp_gcflag & UNPGC_IGNORE_RIGHTS)
2549 				continue;
2550 			SOCKBUF_LOCK(&soa->so_rcv);
2551 			unp_scan(soa->so_rcv.sb_mb, op);
2552 			SOCKBUF_UNLOCK(&soa->so_rcv);
2553 		}
2554 	} else {
2555 		/*
2556 		 * Mark all sockets we reference with RIGHTS.
2557 		 */
2558 		if ((unp->unp_gcflag & UNPGC_IGNORE_RIGHTS) == 0) {
2559 			SOCKBUF_LOCK(&so->so_rcv);
2560 			unp_scan(so->so_rcv.sb_mb, op);
2561 			SOCKBUF_UNLOCK(&so->so_rcv);
2562 		}
2563 	}
2564 	SOCK_UNLOCK(so);
2565 }
2566 
2567 static int unp_recycled;
2568 SYSCTL_INT(_net_local, OID_AUTO, recycled, CTLFLAG_RD, &unp_recycled, 0,
2569     "Number of unreachable sockets claimed by the garbage collector.");
2570 
2571 static int unp_taskcount;
2572 SYSCTL_INT(_net_local, OID_AUTO, taskcount, CTLFLAG_RD, &unp_taskcount, 0,
2573     "Number of times the garbage collector has run.");
2574 
2575 SYSCTL_UINT(_net_local, OID_AUTO, sockcount, CTLFLAG_RD, &unp_count, 0,
2576     "Number of active local sockets.");
2577 
2578 static void
2579 unp_gc(__unused void *arg, int pending)
2580 {
2581 	struct unp_head *heads[] = { &unp_dhead, &unp_shead, &unp_sphead,
2582 				    NULL };
2583 	struct unp_head **head;
2584 	struct unp_head unp_deadhead;	/* List of potentially-dead sockets. */
2585 	struct file *f, **unref;
2586 	struct unpcb *unp, *unptmp;
2587 	int i, total, unp_unreachable;
2588 
2589 	LIST_INIT(&unp_deadhead);
2590 	unp_taskcount++;
2591 	UNP_LINK_RLOCK();
2592 	/*
2593 	 * First determine which sockets may be in cycles.
2594 	 */
2595 	unp_unreachable = 0;
2596 
2597 	for (head = heads; *head != NULL; head++)
2598 		LIST_FOREACH(unp, *head, unp_link) {
2599 
2600 			KASSERT((unp->unp_gcflag & ~UNPGC_IGNORE_RIGHTS) == 0,
2601 			    ("%s: unp %p has unexpected gc flags 0x%x",
2602 			    __func__, unp, (unsigned int)unp->unp_gcflag));
2603 
2604 			f = unp->unp_file;
2605 
2606 			/*
2607 			 * Check for an unreachable socket potentially in a
2608 			 * cycle.  It must be in a queue as indicated by
2609 			 * msgcount, and this must equal the file reference
2610 			 * count.  Note that when msgcount is 0 the file is
2611 			 * NULL.
2612 			 */
2613 			if (f != NULL && unp->unp_msgcount != 0 &&
2614 			    f->f_count == unp->unp_msgcount) {
2615 				LIST_INSERT_HEAD(&unp_deadhead, unp, unp_dead);
2616 				unp->unp_gcflag |= UNPGC_DEAD;
2617 				unp->unp_gcrefs = unp->unp_msgcount;
2618 				unp_unreachable++;
2619 			}
2620 		}
2621 
2622 	/*
2623 	 * Scan all sockets previously marked as potentially being in a cycle
2624 	 * and remove the references each socket holds on any UNPGC_DEAD
2625 	 * sockets in its queue.  After this step, all remaining references on
2626 	 * sockets marked UNPGC_DEAD should not be part of any cycle.
2627 	 */
2628 	LIST_FOREACH(unp, &unp_deadhead, unp_dead)
2629 		unp_gc_scan(unp, unp_remove_dead_ref);
2630 
2631 	/*
2632 	 * If a socket still has a non-negative refcount, it cannot be in a
2633 	 * cycle.  In this case increment refcount of all children iteratively.
2634 	 * Stop the scan once we do a complete loop without discovering
2635 	 * a new reachable socket.
2636 	 */
2637 	do {
2638 		unp_marked = 0;
2639 		LIST_FOREACH_SAFE(unp, &unp_deadhead, unp_dead, unptmp)
2640 			if (unp->unp_gcrefs > 0) {
2641 				unp->unp_gcflag &= ~UNPGC_DEAD;
2642 				LIST_REMOVE(unp, unp_dead);
2643 				KASSERT(unp_unreachable > 0,
2644 				    ("%s: unp_unreachable underflow.",
2645 				    __func__));
2646 				unp_unreachable--;
2647 				unp_gc_scan(unp, unp_restore_undead_ref);
2648 			}
2649 	} while (unp_marked);
2650 
2651 	UNP_LINK_RUNLOCK();
2652 
2653 	if (unp_unreachable == 0)
2654 		return;
2655 
2656 	/*
2657 	 * Allocate space for a local array of dead unpcbs.
2658 	 * TODO: can this path be simplified by instead using the local
2659 	 * dead list at unp_deadhead, after taking out references
2660 	 * on the file object and/or unpcb and dropping the link lock?
2661 	 */
2662 	unref = malloc(unp_unreachable * sizeof(struct file *),
2663 	    M_TEMP, M_WAITOK);
2664 
2665 	/*
2666 	 * Iterate looking for sockets which have been specifically marked
2667 	 * as unreachable and store them locally.
2668 	 */
2669 	UNP_LINK_RLOCK();
2670 	total = 0;
2671 	LIST_FOREACH(unp, &unp_deadhead, unp_dead) {
2672 		KASSERT((unp->unp_gcflag & UNPGC_DEAD) != 0,
2673 		    ("%s: unp %p not marked UNPGC_DEAD", __func__, unp));
2674 		unp->unp_gcflag &= ~UNPGC_DEAD;
2675 		f = unp->unp_file;
2676 		if (unp->unp_msgcount == 0 || f == NULL ||
2677 		    f->f_count != unp->unp_msgcount ||
2678 		    !fhold(f))
2679 			continue;
2680 		unref[total++] = f;
2681 		KASSERT(total <= unp_unreachable,
2682 		    ("%s: incorrect unreachable count.", __func__));
2683 	}
2684 	UNP_LINK_RUNLOCK();
2685 
2686 	/*
2687 	 * Now flush all sockets, free'ing rights.  This will free the
2688 	 * struct files associated with these sockets but leave each socket
2689 	 * with one remaining ref.
2690 	 */
2691 	for (i = 0; i < total; i++) {
2692 		struct socket *so;
2693 
2694 		so = unref[i]->f_data;
2695 		CURVNET_SET(so->so_vnet);
2696 		sorflush(so);
2697 		CURVNET_RESTORE();
2698 	}
2699 
2700 	/*
2701 	 * And finally release the sockets so they can be reclaimed.
2702 	 */
2703 	for (i = 0; i < total; i++)
2704 		fdrop(unref[i], NULL);
2705 	unp_recycled += total;
2706 	free(unref, M_TEMP);
2707 }
2708 
2709 static void
2710 unp_dispose_mbuf(struct mbuf *m)
2711 {
2712 
2713 	if (m)
2714 		unp_scan(m, unp_freerights);
2715 }
2716 
2717 /*
2718  * Synchronize against unp_gc, which can trip over data as we are freeing it.
2719  */
2720 static void
2721 unp_dispose(struct socket *so)
2722 {
2723 	struct unpcb *unp;
2724 
2725 	unp = sotounpcb(so);
2726 	UNP_LINK_WLOCK();
2727 	unp->unp_gcflag |= UNPGC_IGNORE_RIGHTS;
2728 	UNP_LINK_WUNLOCK();
2729 	if (!SOLISTENING(so))
2730 		unp_dispose_mbuf(so->so_rcv.sb_mb);
2731 }
2732 
2733 static void
2734 unp_scan(struct mbuf *m0, void (*op)(struct filedescent **, int))
2735 {
2736 	struct mbuf *m;
2737 	struct cmsghdr *cm;
2738 	void *data;
2739 	socklen_t clen, datalen;
2740 
2741 	while (m0 != NULL) {
2742 		for (m = m0; m; m = m->m_next) {
2743 			if (m->m_type != MT_CONTROL)
2744 				continue;
2745 
2746 			cm = mtod(m, struct cmsghdr *);
2747 			clen = m->m_len;
2748 
2749 			while (cm != NULL) {
2750 				if (sizeof(*cm) > clen || cm->cmsg_len > clen)
2751 					break;
2752 
2753 				data = CMSG_DATA(cm);
2754 				datalen = (caddr_t)cm + cm->cmsg_len
2755 				    - (caddr_t)data;
2756 
2757 				if (cm->cmsg_level == SOL_SOCKET &&
2758 				    cm->cmsg_type == SCM_RIGHTS) {
2759 					(*op)(data, datalen /
2760 					    sizeof(struct filedescent *));
2761 				}
2762 
2763 				if (CMSG_SPACE(datalen) < clen) {
2764 					clen -= CMSG_SPACE(datalen);
2765 					cm = (struct cmsghdr *)
2766 					    ((caddr_t)cm + CMSG_SPACE(datalen));
2767 				} else {
2768 					clen = 0;
2769 					cm = NULL;
2770 				}
2771 			}
2772 		}
2773 		m0 = m0->m_nextpkt;
2774 	}
2775 }
2776 
2777 /*
2778  * A helper function called by VFS before socket-type vnode reclamation.
2779  * For an active vnode it clears unp_vnode pointer and decrements unp_vnode
2780  * use count.
2781  */
2782 void
2783 vfs_unp_reclaim(struct vnode *vp)
2784 {
2785 	struct unpcb *unp;
2786 	int active;
2787 	struct mtx *vplock;
2788 
2789 	ASSERT_VOP_ELOCKED(vp, "vfs_unp_reclaim");
2790 	KASSERT(vp->v_type == VSOCK,
2791 	    ("vfs_unp_reclaim: vp->v_type != VSOCK"));
2792 
2793 	active = 0;
2794 	vplock = mtx_pool_find(mtxpool_sleep, vp);
2795 	mtx_lock(vplock);
2796 	VOP_UNP_CONNECT(vp, &unp);
2797 	if (unp == NULL)
2798 		goto done;
2799 	UNP_PCB_LOCK(unp);
2800 	if (unp->unp_vnode == vp) {
2801 		VOP_UNP_DETACH(vp);
2802 		unp->unp_vnode = NULL;
2803 		active = 1;
2804 	}
2805 	UNP_PCB_UNLOCK(unp);
2806  done:
2807 	mtx_unlock(vplock);
2808 	if (active)
2809 		vunref(vp);
2810 }
2811 
2812 #ifdef DDB
2813 static void
2814 db_print_indent(int indent)
2815 {
2816 	int i;
2817 
2818 	for (i = 0; i < indent; i++)
2819 		db_printf(" ");
2820 }
2821 
2822 static void
2823 db_print_unpflags(int unp_flags)
2824 {
2825 	int comma;
2826 
2827 	comma = 0;
2828 	if (unp_flags & UNP_HAVEPC) {
2829 		db_printf("%sUNP_HAVEPC", comma ? ", " : "");
2830 		comma = 1;
2831 	}
2832 	if (unp_flags & UNP_WANTCRED) {
2833 		db_printf("%sUNP_WANTCRED", comma ? ", " : "");
2834 		comma = 1;
2835 	}
2836 	if (unp_flags & UNP_CONNWAIT) {
2837 		db_printf("%sUNP_CONNWAIT", comma ? ", " : "");
2838 		comma = 1;
2839 	}
2840 	if (unp_flags & UNP_CONNECTING) {
2841 		db_printf("%sUNP_CONNECTING", comma ? ", " : "");
2842 		comma = 1;
2843 	}
2844 	if (unp_flags & UNP_BINDING) {
2845 		db_printf("%sUNP_BINDING", comma ? ", " : "");
2846 		comma = 1;
2847 	}
2848 }
2849 
2850 static void
2851 db_print_xucred(int indent, struct xucred *xu)
2852 {
2853 	int comma, i;
2854 
2855 	db_print_indent(indent);
2856 	db_printf("cr_version: %u   cr_uid: %u   cr_pid: %d   cr_ngroups: %d\n",
2857 	    xu->cr_version, xu->cr_uid, xu->cr_pid, xu->cr_ngroups);
2858 	db_print_indent(indent);
2859 	db_printf("cr_groups: ");
2860 	comma = 0;
2861 	for (i = 0; i < xu->cr_ngroups; i++) {
2862 		db_printf("%s%u", comma ? ", " : "", xu->cr_groups[i]);
2863 		comma = 1;
2864 	}
2865 	db_printf("\n");
2866 }
2867 
2868 static void
2869 db_print_unprefs(int indent, struct unp_head *uh)
2870 {
2871 	struct unpcb *unp;
2872 	int counter;
2873 
2874 	counter = 0;
2875 	LIST_FOREACH(unp, uh, unp_reflink) {
2876 		if (counter % 4 == 0)
2877 			db_print_indent(indent);
2878 		db_printf("%p  ", unp);
2879 		if (counter % 4 == 3)
2880 			db_printf("\n");
2881 		counter++;
2882 	}
2883 	if (counter != 0 && counter % 4 != 0)
2884 		db_printf("\n");
2885 }
2886 
2887 DB_SHOW_COMMAND(unpcb, db_show_unpcb)
2888 {
2889 	struct unpcb *unp;
2890 
2891         if (!have_addr) {
2892                 db_printf("usage: show unpcb <addr>\n");
2893                 return;
2894         }
2895         unp = (struct unpcb *)addr;
2896 
2897 	db_printf("unp_socket: %p   unp_vnode: %p\n", unp->unp_socket,
2898 	    unp->unp_vnode);
2899 
2900 	db_printf("unp_ino: %ju   unp_conn: %p\n", (uintmax_t)unp->unp_ino,
2901 	    unp->unp_conn);
2902 
2903 	db_printf("unp_refs:\n");
2904 	db_print_unprefs(2, &unp->unp_refs);
2905 
2906 	/* XXXRW: Would be nice to print the full address, if any. */
2907 	db_printf("unp_addr: %p\n", unp->unp_addr);
2908 
2909 	db_printf("unp_gencnt: %llu\n",
2910 	    (unsigned long long)unp->unp_gencnt);
2911 
2912 	db_printf("unp_flags: %x (", unp->unp_flags);
2913 	db_print_unpflags(unp->unp_flags);
2914 	db_printf(")\n");
2915 
2916 	db_printf("unp_peercred:\n");
2917 	db_print_xucred(2, &unp->unp_peercred);
2918 
2919 	db_printf("unp_refcount: %u\n", unp->unp_refcount);
2920 }
2921 #endif
2922