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