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