xref: /freebsd/sys/kern/uipc_usrreq.c (revision 3fe92528afe8313fecf48822dde74bad5e380f48)
1 /*-
2  * Copyright (c) 1982, 1986, 1989, 1991, 1993
3  *	The Regents of the University of California.
4  * Copyright 2004-2006 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.  The implementation is substantially
45  * complicated by the fact that "ancillary data", such as file descriptors or
46  * credentials, may be passed across UNIX domain sockets.  The potential for
47  * passing UNIX domain sockets over other UNIX domain sockets requires the
48  * implementation of a simple garbage collector to find and tear down cycles
49  * of disconnected sockets.
50  */
51 
52 #include <sys/cdefs.h>
53 __FBSDID("$FreeBSD$");
54 
55 #include "opt_mac.h"
56 
57 #include <sys/param.h>
58 #include <sys/domain.h>
59 #include <sys/fcntl.h>
60 #include <sys/malloc.h>		/* XXX must be before <sys/file.h> */
61 #include <sys/eventhandler.h>
62 #include <sys/file.h>
63 #include <sys/filedesc.h>
64 #include <sys/jail.h>
65 #include <sys/kernel.h>
66 #include <sys/lock.h>
67 #include <sys/mac.h>
68 #include <sys/mbuf.h>
69 #include <sys/mount.h>
70 #include <sys/mutex.h>
71 #include <sys/namei.h>
72 #include <sys/proc.h>
73 #include <sys/protosw.h>
74 #include <sys/resourcevar.h>
75 #include <sys/socket.h>
76 #include <sys/socketvar.h>
77 #include <sys/signalvar.h>
78 #include <sys/stat.h>
79 #include <sys/sx.h>
80 #include <sys/sysctl.h>
81 #include <sys/systm.h>
82 #include <sys/taskqueue.h>
83 #include <sys/un.h>
84 #include <sys/unpcb.h>
85 #include <sys/vnode.h>
86 
87 #include <vm/uma.h>
88 
89 static uma_zone_t unp_zone;
90 static	unp_gen_t unp_gencnt;
91 static	u_int unp_count;
92 
93 static	struct unp_head unp_shead, unp_dhead;
94 
95 /*
96  * Unix communications domain.
97  *
98  * TODO:
99  *	SEQPACKET, RDM
100  *	rethink name space problems
101  *	need a proper out-of-band
102  *	lock pushdown
103  */
104 static const struct	sockaddr sun_noname = { sizeof(sun_noname), AF_LOCAL };
105 static ino_t	unp_ino;		/* prototype for fake inode numbers */
106 struct mbuf *unp_addsockcred(struct thread *, struct mbuf *);
107 
108 /*
109  * Both send and receive buffers are allocated PIPSIZ bytes of buffering for
110  * stream sockets, although the total for sender and receiver is actually
111  * only PIPSIZ.
112  *
113  * Datagram sockets really use the sendspace as the maximum datagram size,
114  * and don't really want to reserve the sendspace.  Their recvspace should be
115  * large enough for at least one max-size datagram plus address.
116  */
117 #ifndef PIPSIZ
118 #define	PIPSIZ	8192
119 #endif
120 static u_long	unpst_sendspace = PIPSIZ;
121 static u_long	unpst_recvspace = PIPSIZ;
122 static u_long	unpdg_sendspace = 2*1024;	/* really max datagram size */
123 static u_long	unpdg_recvspace = 4*1024;
124 
125 static int	unp_rights;			/* file descriptors in flight */
126 
127 SYSCTL_NODE(_net, PF_LOCAL, local, CTLFLAG_RW, 0, "Local domain");
128 SYSCTL_NODE(_net_local, SOCK_STREAM, stream, CTLFLAG_RW, 0, "SOCK_STREAM");
129 SYSCTL_NODE(_net_local, SOCK_DGRAM, dgram, CTLFLAG_RW, 0, "SOCK_DGRAM");
130 
131 SYSCTL_ULONG(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW,
132 	   &unpst_sendspace, 0, "");
133 SYSCTL_ULONG(_net_local_stream, OID_AUTO, recvspace, CTLFLAG_RW,
134 	   &unpst_recvspace, 0, "");
135 SYSCTL_ULONG(_net_local_dgram, OID_AUTO, maxdgram, CTLFLAG_RW,
136 	   &unpdg_sendspace, 0, "");
137 SYSCTL_ULONG(_net_local_dgram, OID_AUTO, recvspace, CTLFLAG_RW,
138 	   &unpdg_recvspace, 0, "");
139 SYSCTL_INT(_net_local, OID_AUTO, inflight, CTLFLAG_RD, &unp_rights, 0, "");
140 
141 /*
142  * Currently, UNIX domain sockets are protected by a single subsystem lock,
143  * which covers global data structures and variables, the contents of each
144  * per-socket unpcb structure, and the so_pcb field in sockets attached to
145  * the UNIX domain.  This provides for a moderate degree of paralellism, as
146  * receive operations on UNIX domain sockets do not need to acquire the
147  * subsystem lock.  Finer grained locking to permit send() without acquiring
148  * a global lock would be a logical next step.
149  *
150  * The UNIX domain socket lock preceds all socket layer locks, including the
151  * socket lock and socket buffer lock, permitting UNIX domain socket code to
152  * call into socket support routines without releasing its locks.
153  *
154  * Some caution is required in areas where the UNIX domain socket code enters
155  * VFS in order to create or find rendezvous points.  This results in
156  * dropping of the UNIX domain socket subsystem lock, acquisition of the
157  * Giant lock, and potential sleeping.  This increases the chances of races,
158  * and exposes weaknesses in the socket->protocol API by offering poor
159  * failure modes.
160  */
161 static struct mtx unp_mtx;
162 #define	UNP_LOCK_INIT() \
163 	mtx_init(&unp_mtx, "unp", NULL, MTX_DEF)
164 #define	UNP_LOCK()		mtx_lock(&unp_mtx)
165 #define	UNP_UNLOCK()		mtx_unlock(&unp_mtx)
166 #define	UNP_LOCK_ASSERT()	mtx_assert(&unp_mtx, MA_OWNED)
167 #define	UNP_UNLOCK_ASSERT()	mtx_assert(&unp_mtx, MA_NOTOWNED)
168 
169 /*
170  * Garbage collection of cyclic file descriptor/socket references occurs
171  * asynchronously in a taskqueue context in order to avoid recursion and
172  * reentrance in the UNIX domain socket, file descriptor, and socket layer
173  * code.  See unp_gc() for a full description.
174  */
175 static struct task	unp_gc_task;
176 
177 static int     unp_connect(struct socket *,struct sockaddr *, struct thread *);
178 static int     unp_connect2(struct socket *so, struct socket *so2, int);
179 static void    unp_disconnect(struct unpcb *);
180 static void    unp_shutdown(struct unpcb *);
181 static void    unp_drop(struct unpcb *, int);
182 static void    unp_gc(__unused void *, int);
183 static void    unp_scan(struct mbuf *, void (*)(struct file *));
184 static void    unp_mark(struct file *);
185 static void    unp_discard(struct file *);
186 static void    unp_freerights(struct file **, int);
187 static int     unp_internalize(struct mbuf **, struct thread *);
188 static int     unp_listen(struct socket *, struct unpcb *, int,
189 		   struct thread *);
190 
191 /*
192  * Definitions of protocols supported in the LOCAL domain.
193  */
194 static struct domain localdomain;
195 static struct protosw localsw[] = {
196 {
197 	.pr_type =		SOCK_STREAM,
198 	.pr_domain =		&localdomain,
199 	.pr_flags =		PR_CONNREQUIRED|PR_WANTRCVD|PR_RIGHTS,
200 	.pr_ctloutput =		&uipc_ctloutput,
201 	.pr_usrreqs =		&uipc_usrreqs
202 },
203 {
204 	.pr_type =		SOCK_DGRAM,
205 	.pr_domain =		&localdomain,
206 	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_RIGHTS,
207 	.pr_usrreqs =		&uipc_usrreqs
208 },
209 };
210 
211 static struct domain localdomain = {
212 	.dom_family =		AF_LOCAL,
213 	.dom_name =		"local",
214 	.dom_init =		unp_init,
215 	.dom_externalize =	unp_externalize,
216 	.dom_dispose =		unp_dispose,
217 	.dom_protosw =		localsw,
218 	.dom_protoswNPROTOSW =	&localsw[sizeof(localsw)/sizeof(localsw[0])]
219 };
220 DOMAIN_SET(local);
221 
222 static void
223 uipc_abort(struct socket *so)
224 {
225 	struct unpcb *unp;
226 
227 	unp = sotounpcb(so);
228 	KASSERT(unp != NULL, ("uipc_abort: unp == NULL"));
229 	UNP_LOCK();
230 	unp_drop(unp, ECONNABORTED);
231 	UNP_UNLOCK();
232 }
233 
234 static int
235 uipc_accept(struct socket *so, struct sockaddr **nam)
236 {
237 	struct unpcb *unp;
238 	const struct sockaddr *sa;
239 
240 	/*
241 	 * Pass back name of connected socket, if it was bound and we are
242 	 * still connected (our peer may have closed already!).
243 	 */
244 	unp = sotounpcb(so);
245 	KASSERT(unp != NULL, ("uipc_accept: unp == NULL"));
246 	*nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK);
247 	UNP_LOCK();
248 	if (unp->unp_conn != NULL && unp->unp_conn->unp_addr != NULL)
249 		sa = (struct sockaddr *) unp->unp_conn->unp_addr;
250 	else
251 		sa = &sun_noname;
252 	bcopy(sa, *nam, sa->sa_len);
253 	UNP_UNLOCK();
254 	return (0);
255 }
256 
257 static int
258 uipc_attach(struct socket *so, int proto, struct thread *td)
259 {
260 	struct unpcb *unp;
261 	int error;
262 
263 	KASSERT(so->so_pcb == NULL, ("uipc_attach: so_pcb != NULL"));
264 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
265 		switch (so->so_type) {
266 		case SOCK_STREAM:
267 			error = soreserve(so, unpst_sendspace, unpst_recvspace);
268 			break;
269 
270 		case SOCK_DGRAM:
271 			error = soreserve(so, unpdg_sendspace, unpdg_recvspace);
272 			break;
273 
274 		default:
275 			panic("unp_attach");
276 		}
277 		if (error)
278 			return (error);
279 	}
280 	unp = uma_zalloc(unp_zone, M_WAITOK | M_ZERO);
281 	if (unp == NULL)
282 		return (ENOBUFS);
283 	LIST_INIT(&unp->unp_refs);
284 	unp->unp_socket = so;
285 	so->so_pcb = unp;
286 
287 	UNP_LOCK();
288 	unp->unp_gencnt = ++unp_gencnt;
289 	unp_count++;
290 	LIST_INSERT_HEAD(so->so_type == SOCK_DGRAM ? &unp_dhead : &unp_shead,
291 	    unp, unp_link);
292 	UNP_UNLOCK();
293 
294 	return (0);
295 }
296 
297 static int
298 uipc_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
299 {
300 	struct sockaddr_un *soun = (struct sockaddr_un *)nam;
301 	struct vattr vattr;
302 	int error, namelen;
303 	struct nameidata nd;
304 	struct unpcb *unp;
305 	struct vnode *vp;
306 	struct mount *mp;
307 	char *buf;
308 
309 	unp = sotounpcb(so);
310 	KASSERT(unp != NULL, ("uipc_bind: unp == NULL"));
311 
312 	namelen = soun->sun_len - offsetof(struct sockaddr_un, sun_path);
313 	if (namelen <= 0)
314 		return (EINVAL);
315 
316 	/*
317 	 * We don't allow simultaneous bind() calls on a single UNIX domain
318 	 * socket, so flag in-progress operations, and return an error if an
319 	 * operation is already in progress.
320 	 *
321 	 * Historically, we have not allowed a socket to be rebound, so this
322 	 * also returns an error.  Not allowing re-binding certainly
323 	 * simplifies the implementation and avoids a great many possible
324 	 * failure modes.
325 	 */
326 	UNP_LOCK();
327 	if (unp->unp_vnode != NULL) {
328 		UNP_UNLOCK();
329 		return (EINVAL);
330 	}
331 	if (unp->unp_flags & UNP_BINDING) {
332 		UNP_UNLOCK();
333 		return (EALREADY);
334 	}
335 	unp->unp_flags |= UNP_BINDING;
336 	UNP_UNLOCK();
337 
338 	buf = malloc(namelen + 1, M_TEMP, M_WAITOK);
339 	strlcpy(buf, soun->sun_path, namelen + 1);
340 
341 	mtx_lock(&Giant);
342 restart:
343 	mtx_assert(&Giant, MA_OWNED);
344 	NDINIT(&nd, CREATE, NOFOLLOW | LOCKPARENT | SAVENAME, UIO_SYSSPACE,
345 	    buf, td);
346 /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */
347 	error = namei(&nd);
348 	if (error)
349 		goto error;
350 	vp = nd.ni_vp;
351 	if (vp != NULL || vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
352 		NDFREE(&nd, NDF_ONLY_PNBUF);
353 		if (nd.ni_dvp == vp)
354 			vrele(nd.ni_dvp);
355 		else
356 			vput(nd.ni_dvp);
357 		if (vp != NULL) {
358 			vrele(vp);
359 			error = EADDRINUSE;
360 			goto error;
361 		}
362 		error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH);
363 		if (error)
364 			goto error;
365 		goto restart;
366 	}
367 	VATTR_NULL(&vattr);
368 	vattr.va_type = VSOCK;
369 	vattr.va_mode = (ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask);
370 #ifdef MAC
371 	error = mac_check_vnode_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
372 	    &vattr);
373 #endif
374 	if (error == 0) {
375 		VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
376 		error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
377 	}
378 	NDFREE(&nd, NDF_ONLY_PNBUF);
379 	vput(nd.ni_dvp);
380 	if (error) {
381 		vn_finished_write(mp);
382 		goto error;
383 	}
384 	vp = nd.ni_vp;
385 	ASSERT_VOP_LOCKED(vp, "uipc_bind");
386 	soun = (struct sockaddr_un *)sodupsockaddr(nam, M_WAITOK);
387 	UNP_LOCK();
388 	vp->v_socket = unp->unp_socket;
389 	unp->unp_vnode = vp;
390 	unp->unp_addr = soun;
391 	unp->unp_flags &= ~UNP_BINDING;
392 	UNP_UNLOCK();
393 	VOP_UNLOCK(vp, 0, td);
394 	vn_finished_write(mp);
395 	mtx_unlock(&Giant);
396 	free(buf, M_TEMP);
397 	return (0);
398 error:
399 	UNP_LOCK();
400 	unp->unp_flags &= ~UNP_BINDING;
401 	UNP_UNLOCK();
402 	mtx_unlock(&Giant);
403 	free(buf, M_TEMP);
404 	return (error);
405 }
406 
407 static int
408 uipc_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
409 {
410 	int error;
411 
412 	KASSERT(td == curthread, ("uipc_connect: td != curthread"));
413 	UNP_LOCK();
414 	error = unp_connect(so, nam, td);
415 	UNP_UNLOCK();
416 	return (error);
417 }
418 
419 /*
420  * XXXRW: Should also unbind?
421  */
422 static void
423 uipc_close(struct socket *so)
424 {
425 	struct unpcb *unp;
426 
427 	unp = sotounpcb(so);
428 	KASSERT(unp != NULL, ("uipc_close: unp == NULL"));
429 	UNP_LOCK();
430 	unp_disconnect(unp);
431 	UNP_UNLOCK();
432 }
433 
434 int
435 uipc_connect2(struct socket *so1, struct socket *so2)
436 {
437 	struct unpcb *unp;
438 	int error;
439 
440 	unp = sotounpcb(so1);
441 	KASSERT(unp != NULL, ("uipc_connect2: unp == NULL"));
442 	UNP_LOCK();
443 	error = unp_connect2(so1, so2, PRU_CONNECT2);
444 	UNP_UNLOCK();
445 	return (error);
446 }
447 
448 /* control is EOPNOTSUPP */
449 
450 static void
451 uipc_detach(struct socket *so)
452 {
453 	int local_unp_rights;
454 	struct unpcb *unp;
455 	struct vnode *vp;
456 
457 	unp = sotounpcb(so);
458 	KASSERT(unp != NULL, ("uipc_detach: unp == NULL"));
459 	UNP_LOCK();
460 	LIST_REMOVE(unp, unp_link);
461 	unp->unp_gencnt = ++unp_gencnt;
462 	--unp_count;
463 	if ((vp = unp->unp_vnode) != NULL) {
464 		/*
465 		 * XXXRW: should v_socket be frobbed only while holding
466 		 * Giant?
467 		 */
468 		unp->unp_vnode->v_socket = NULL;
469 		unp->unp_vnode = NULL;
470 	}
471 	if (unp->unp_conn != NULL)
472 		unp_disconnect(unp);
473 	while (!LIST_EMPTY(&unp->unp_refs)) {
474 		struct unpcb *ref = LIST_FIRST(&unp->unp_refs);
475 		unp_drop(ref, ECONNRESET);
476 	}
477 	unp->unp_socket->so_pcb = NULL;
478 	local_unp_rights = unp_rights;
479 	UNP_UNLOCK();
480 	if (unp->unp_addr != NULL)
481 		FREE(unp->unp_addr, M_SONAME);
482 	uma_zfree(unp_zone, unp);
483 	if (vp) {
484 		int vfslocked;
485 
486 		vfslocked = VFS_LOCK_GIANT(vp->v_mount);
487 		vrele(vp);
488 		VFS_UNLOCK_GIANT(vfslocked);
489 	}
490 	if (local_unp_rights)
491 		taskqueue_enqueue(taskqueue_thread, &unp_gc_task);
492 }
493 
494 static int
495 uipc_disconnect(struct socket *so)
496 {
497 	struct unpcb *unp;
498 
499 	unp = sotounpcb(so);
500 	KASSERT(unp != NULL, ("uipc_disconnect: unp == NULL"));
501 	UNP_LOCK();
502 	unp_disconnect(unp);
503 	UNP_UNLOCK();
504 	return (0);
505 }
506 
507 static int
508 uipc_listen(struct socket *so, int backlog, struct thread *td)
509 {
510 	struct unpcb *unp;
511 	int error;
512 
513 	unp = sotounpcb(so);
514 	KASSERT(unp != NULL, ("uipc_listen: unp == NULL"));
515 	UNP_LOCK();
516 	if (unp->unp_vnode == NULL) {
517 		UNP_UNLOCK();
518 		return (EINVAL);
519 	}
520 	error = unp_listen(so, unp, backlog, td);
521 	UNP_UNLOCK();
522 	return (error);
523 }
524 
525 static int
526 uipc_peeraddr(struct socket *so, struct sockaddr **nam)
527 {
528 	struct unpcb *unp;
529 	const struct sockaddr *sa;
530 
531 	unp = sotounpcb(so);
532 	KASSERT(unp != NULL, ("uipc_peeraddr: unp == NULL"));
533 	*nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK);
534 	UNP_LOCK();
535 	if (unp->unp_conn != NULL && unp->unp_conn->unp_addr!= NULL)
536 		sa = (struct sockaddr *) unp->unp_conn->unp_addr;
537 	else {
538 		/*
539 		 * XXX: It seems that this test always fails even when
540 		 * connection is established.  So, this else clause is
541 		 * added as workaround to return PF_LOCAL sockaddr.
542 		 */
543 		sa = &sun_noname;
544 	}
545 	bcopy(sa, *nam, sa->sa_len);
546 	UNP_UNLOCK();
547 	return (0);
548 }
549 
550 static int
551 uipc_rcvd(struct socket *so, int flags)
552 {
553 	struct unpcb *unp;
554 	struct socket *so2;
555 	u_int mbcnt, sbcc;
556 	u_long newhiwat;
557 
558 	unp = sotounpcb(so);
559 	KASSERT(unp != NULL, ("uipc_rcvd: unp == NULL"));
560 	switch (so->so_type) {
561 	case SOCK_DGRAM:
562 		panic("uipc_rcvd DGRAM?");
563 		/*NOTREACHED*/
564 
565 	case SOCK_STREAM:
566 		/*
567 		 * Adjust backpressure on sender and wakeup any waiting to
568 		 * write.
569 		 */
570 		SOCKBUF_LOCK(&so->so_rcv);
571 		mbcnt = so->so_rcv.sb_mbcnt;
572 		sbcc = so->so_rcv.sb_cc;
573 		SOCKBUF_UNLOCK(&so->so_rcv);
574 		UNP_LOCK();
575 		if (unp->unp_conn == NULL) {
576 			UNP_UNLOCK();
577 			break;
578 		}
579 		so2 = unp->unp_conn->unp_socket;
580 		SOCKBUF_LOCK(&so2->so_snd);
581 		so2->so_snd.sb_mbmax += unp->unp_mbcnt - mbcnt;
582 		newhiwat = so2->so_snd.sb_hiwat + unp->unp_cc - sbcc;
583 		(void)chgsbsize(so2->so_cred->cr_uidinfo, &so2->so_snd.sb_hiwat,
584 		    newhiwat, RLIM_INFINITY);
585 		sowwakeup_locked(so2);
586 		unp->unp_mbcnt = mbcnt;
587 		unp->unp_cc = sbcc;
588 		UNP_UNLOCK();
589 		break;
590 
591 	default:
592 		panic("uipc_rcvd unknown socktype");
593 	}
594 	return (0);
595 }
596 
597 /* pru_rcvoob is EOPNOTSUPP */
598 
599 static int
600 uipc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
601     struct mbuf *control, struct thread *td)
602 {
603 	struct unpcb *unp, *unp2;
604 	struct socket *so2;
605 	u_int mbcnt, sbcc;
606 	u_long newhiwat;
607 	int error = 0;
608 
609 	unp = sotounpcb(so);
610 	KASSERT(unp != NULL, ("uipc_send: unp == NULL"));
611 	if (flags & PRUS_OOB) {
612 		error = EOPNOTSUPP;
613 		goto release;
614 	}
615 
616 	if (control != NULL && (error = unp_internalize(&control, td)))
617 		goto release;
618 
619 	UNP_LOCK();
620 	switch (so->so_type) {
621 	case SOCK_DGRAM:
622 	{
623 		const struct sockaddr *from;
624 
625 		if (nam != NULL) {
626 			if (unp->unp_conn != NULL) {
627 				error = EISCONN;
628 				break;
629 			}
630 			error = unp_connect(so, nam, td);
631 			if (error)
632 				break;
633 		}
634 		/*
635 		 * Because connect() and send() are non-atomic in a sendto()
636 		 * with a target address, it's possible that the socket will
637 		 * have disconnected before the send() can run.  In that case
638 		 * return the slightly counter-intuitive but otherwise
639 		 * correct error that the socket is not connected.
640 		 */
641 		unp2 = unp->unp_conn;
642 		if (unp2 == NULL) {
643 			error = ENOTCONN;
644 			break;
645 		}
646 		so2 = unp2->unp_socket;
647 		if (unp->unp_addr != NULL)
648 			from = (struct sockaddr *)unp->unp_addr;
649 		else
650 			from = &sun_noname;
651 		if (unp2->unp_flags & UNP_WANTCRED)
652 			control = unp_addsockcred(td, control);
653 		SOCKBUF_LOCK(&so2->so_rcv);
654 		if (sbappendaddr_locked(&so2->so_rcv, from, m, control)) {
655 			sorwakeup_locked(so2);
656 			m = NULL;
657 			control = NULL;
658 		} else {
659 			SOCKBUF_UNLOCK(&so2->so_rcv);
660 			error = ENOBUFS;
661 		}
662 		if (nam != NULL)
663 			unp_disconnect(unp);
664 		break;
665 	}
666 
667 	case SOCK_STREAM:
668 		/*
669 		 * Connect if not connected yet.
670 		 *
671 		 * Note: A better implementation would complain if not equal
672 		 * to the peer's address.
673 		 */
674 		if ((so->so_state & SS_ISCONNECTED) == 0) {
675 			if (nam != NULL) {
676 				error = unp_connect(so, nam, td);
677 				if (error)
678 					break;	/* XXX */
679 			} else {
680 				error = ENOTCONN;
681 				break;
682 			}
683 		}
684 
685 		/* Lockless read. */
686 		if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
687 			error = EPIPE;
688 			break;
689 		}
690 		/*
691 		 * Because connect() and send() are non-atomic in a sendto()
692 		 * with a target address, it's possible that the socket will
693 		 * have disconnected before the send() can run.  In that case
694 		 * return the slightly counter-intuitive but otherwise
695 		 * correct error that the socket is not connected.
696 		 */
697 		unp2 = unp->unp_conn;
698 		if (unp2 == NULL) {
699 			error = ENOTCONN;
700 			break;
701 		}
702 		so2 = unp2->unp_socket;
703 		SOCKBUF_LOCK(&so2->so_rcv);
704 		if (unp2->unp_flags & UNP_WANTCRED) {
705 			/*
706 			 * Credentials are passed only once on
707 			 * SOCK_STREAM.
708 			 */
709 			unp2->unp_flags &= ~UNP_WANTCRED;
710 			control = unp_addsockcred(td, control);
711 		}
712 		/*
713 		 * Send to paired receive port, and then reduce send buffer
714 		 * hiwater marks to maintain backpressure.  Wake up readers.
715 		 */
716 		if (control != NULL) {
717 			if (sbappendcontrol_locked(&so2->so_rcv, m, control))
718 				control = NULL;
719 		} else {
720 			sbappend_locked(&so2->so_rcv, m);
721 		}
722 		mbcnt = so2->so_rcv.sb_mbcnt - unp2->unp_mbcnt;
723 		unp2->unp_mbcnt = so2->so_rcv.sb_mbcnt;
724 		sbcc = so2->so_rcv.sb_cc;
725 		sorwakeup_locked(so2);
726 
727 		SOCKBUF_LOCK(&so->so_snd);
728 		newhiwat = so->so_snd.sb_hiwat - (sbcc - unp2->unp_cc);
729 		(void)chgsbsize(so->so_cred->cr_uidinfo, &so->so_snd.sb_hiwat,
730 		    newhiwat, RLIM_INFINITY);
731 		so->so_snd.sb_mbmax -= mbcnt;
732 		SOCKBUF_UNLOCK(&so->so_snd);
733 
734 		unp2->unp_cc = sbcc;
735 		m = NULL;
736 		break;
737 
738 	default:
739 		panic("uipc_send unknown socktype");
740 	}
741 
742 	/*
743 	 * SEND_EOF is equivalent to a SEND followed by
744 	 * a SHUTDOWN.
745 	 */
746 	if (flags & PRUS_EOF) {
747 		socantsendmore(so);
748 		unp_shutdown(unp);
749 	}
750 	UNP_UNLOCK();
751 
752 	if (control != NULL && error != 0)
753 		unp_dispose(control);
754 
755 release:
756 	if (control != NULL)
757 		m_freem(control);
758 	if (m != NULL)
759 		m_freem(m);
760 	return (error);
761 }
762 
763 static int
764 uipc_sense(struct socket *so, struct stat *sb)
765 {
766 	struct unpcb *unp;
767 	struct socket *so2;
768 
769 	unp = sotounpcb(so);
770 	KASSERT(unp != NULL, ("uipc_sense: unp == NULL"));
771 	UNP_LOCK();
772 	sb->st_blksize = so->so_snd.sb_hiwat;
773 	if (so->so_type == SOCK_STREAM && unp->unp_conn != NULL) {
774 		so2 = unp->unp_conn->unp_socket;
775 		sb->st_blksize += so2->so_rcv.sb_cc;
776 	}
777 	sb->st_dev = NODEV;
778 	if (unp->unp_ino == 0)
779 		unp->unp_ino = (++unp_ino == 0) ? ++unp_ino : unp_ino;
780 	sb->st_ino = unp->unp_ino;
781 	UNP_UNLOCK();
782 	return (0);
783 }
784 
785 static int
786 uipc_shutdown(struct socket *so)
787 {
788 	struct unpcb *unp;
789 
790 	unp = sotounpcb(so);
791 	KASSERT(unp != NULL, ("uipc_shutdown: unp == NULL"));
792 	UNP_LOCK();
793 	socantsendmore(so);
794 	unp_shutdown(unp);
795 	UNP_UNLOCK();
796 	return (0);
797 }
798 
799 static int
800 uipc_sockaddr(struct socket *so, struct sockaddr **nam)
801 {
802 	struct unpcb *unp;
803 	const struct sockaddr *sa;
804 
805 	unp = sotounpcb(so);
806 	KASSERT(unp != NULL, ("uipc_sockaddr: unp == NULL"));
807 	*nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK);
808 	UNP_LOCK();
809 	if (unp->unp_addr != NULL)
810 		sa = (struct sockaddr *) unp->unp_addr;
811 	else
812 		sa = &sun_noname;
813 	bcopy(sa, *nam, sa->sa_len);
814 	UNP_UNLOCK();
815 	return (0);
816 }
817 
818 struct pr_usrreqs uipc_usrreqs = {
819 	.pru_abort = 		uipc_abort,
820 	.pru_accept =		uipc_accept,
821 	.pru_attach =		uipc_attach,
822 	.pru_bind =		uipc_bind,
823 	.pru_connect =		uipc_connect,
824 	.pru_connect2 =		uipc_connect2,
825 	.pru_detach =		uipc_detach,
826 	.pru_disconnect =	uipc_disconnect,
827 	.pru_listen =		uipc_listen,
828 	.pru_peeraddr =		uipc_peeraddr,
829 	.pru_rcvd =		uipc_rcvd,
830 	.pru_send =		uipc_send,
831 	.pru_sense =		uipc_sense,
832 	.pru_shutdown =		uipc_shutdown,
833 	.pru_sockaddr =		uipc_sockaddr,
834 	.pru_close =		uipc_close,
835 };
836 
837 int
838 uipc_ctloutput(struct socket *so, struct sockopt *sopt)
839 {
840 	struct unpcb *unp;
841 	struct xucred xu;
842 	int error, optval;
843 
844 	if (sopt->sopt_level != 0)
845 		return (EINVAL);
846 
847 	unp = sotounpcb(so);
848 	KASSERT(unp != NULL, ("uipc_ctloutput: unp == NULL"));
849 	UNP_LOCK();
850 	error = 0;
851 	switch (sopt->sopt_dir) {
852 	case SOPT_GET:
853 		switch (sopt->sopt_name) {
854 		case LOCAL_PEERCRED:
855 			if (unp->unp_flags & UNP_HAVEPC)
856 				xu = unp->unp_peercred;
857 			else {
858 				if (so->so_type == SOCK_STREAM)
859 					error = ENOTCONN;
860 				else
861 					error = EINVAL;
862 			}
863 			if (error == 0)
864 				error = sooptcopyout(sopt, &xu, sizeof(xu));
865 			break;
866 		case LOCAL_CREDS:
867 			optval = unp->unp_flags & UNP_WANTCRED ? 1 : 0;
868 			error = sooptcopyout(sopt, &optval, sizeof(optval));
869 			break;
870 		case LOCAL_CONNWAIT:
871 			optval = unp->unp_flags & UNP_CONNWAIT ? 1 : 0;
872 			error = sooptcopyout(sopt, &optval, sizeof(optval));
873 			break;
874 		default:
875 			error = EOPNOTSUPP;
876 			break;
877 		}
878 		break;
879 	case SOPT_SET:
880 		switch (sopt->sopt_name) {
881 		case LOCAL_CREDS:
882 		case LOCAL_CONNWAIT:
883 			error = sooptcopyin(sopt, &optval, sizeof(optval),
884 					    sizeof(optval));
885 			if (error)
886 				break;
887 
888 #define	OPTSET(bit) \
889 	if (optval) \
890 		unp->unp_flags |= bit; \
891 	else \
892 		unp->unp_flags &= ~bit;
893 
894 			switch (sopt->sopt_name) {
895 			case LOCAL_CREDS:
896 				OPTSET(UNP_WANTCRED);
897 				break;
898 			case LOCAL_CONNWAIT:
899 				OPTSET(UNP_CONNWAIT);
900 				break;
901 			default:
902 				break;
903 			}
904 			break;
905 #undef	OPTSET
906 		default:
907 			error = ENOPROTOOPT;
908 			break;
909 		}
910 		break;
911 	default:
912 		error = EOPNOTSUPP;
913 		break;
914 	}
915 	UNP_UNLOCK();
916 	return (error);
917 }
918 
919 static int
920 unp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
921 {
922 	struct sockaddr_un *soun = (struct sockaddr_un *)nam;
923 	struct vnode *vp;
924 	struct socket *so2, *so3;
925 	struct unpcb *unp, *unp2, *unp3;
926 	int error, len;
927 	struct nameidata nd;
928 	char buf[SOCK_MAXADDRLEN];
929 	struct sockaddr *sa;
930 
931 	UNP_LOCK_ASSERT();
932 
933 	unp = sotounpcb(so);
934 	KASSERT(unp != NULL, ("unp_connect: unp == NULL"));
935 	len = nam->sa_len - offsetof(struct sockaddr_un, sun_path);
936 	if (len <= 0)
937 		return (EINVAL);
938 	strlcpy(buf, soun->sun_path, len + 1);
939 	if (unp->unp_flags & UNP_CONNECTING) {
940 		UNP_UNLOCK();
941 		return (EALREADY);
942 	}
943 	UNP_UNLOCK();
944 	sa = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK);
945 	mtx_lock(&Giant);
946 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, buf, td);
947 	error = namei(&nd);
948 	if (error)
949 		vp = NULL;
950 	else
951 		vp = nd.ni_vp;
952 	ASSERT_VOP_LOCKED(vp, "unp_connect");
953 	NDFREE(&nd, NDF_ONLY_PNBUF);
954 	if (error)
955 		goto bad;
956 
957 	if (vp->v_type != VSOCK) {
958 		error = ENOTSOCK;
959 		goto bad;
960 	}
961 	error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td);
962 	if (error)
963 		goto bad;
964 	mtx_unlock(&Giant);
965 	UNP_LOCK();
966 	unp = sotounpcb(so);
967 	KASSERT(unp != NULL, ("unp_connect: unp == NULL"));
968 	so2 = vp->v_socket;
969 	if (so2 == NULL) {
970 		error = ECONNREFUSED;
971 		goto bad2;
972 	}
973 	if (so->so_type != so2->so_type) {
974 		error = EPROTOTYPE;
975 		goto bad2;
976 	}
977 	if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
978 		if (so2->so_options & SO_ACCEPTCONN) {
979 			/*
980 			 * NB: drop locks here so unp_attach is entered w/o
981 			 * locks; this avoids a recursive lock of the head
982 			 * and holding sleep locks across a (potentially)
983 			 * blocking malloc.
984 			 */
985 			UNP_UNLOCK();
986 			so3 = sonewconn(so2, 0);
987 			UNP_LOCK();
988 		} else
989 			so3 = NULL;
990 		if (so3 == NULL) {
991 			error = ECONNREFUSED;
992 			goto bad2;
993 		}
994 		unp = sotounpcb(so);
995 		unp2 = sotounpcb(so2);
996 		unp3 = sotounpcb(so3);
997 		if (unp2->unp_addr != NULL) {
998 			bcopy(unp2->unp_addr, sa, unp2->unp_addr->sun_len);
999 			unp3->unp_addr = (struct sockaddr_un *) sa;
1000 			sa = NULL;
1001 		}
1002 		/*
1003 		 * unp_peercred management:
1004 		 *
1005 		 * The connecter's (client's) credentials are copied from its
1006 		 * process structure at the time of connect() (which is now).
1007 		 */
1008 		cru2x(td->td_ucred, &unp3->unp_peercred);
1009 		unp3->unp_flags |= UNP_HAVEPC;
1010 		/*
1011 		 * The receiver's (server's) credentials are copied from the
1012 		 * unp_peercred member of socket on which the former called
1013 		 * listen(); unp_listen() cached that process's credentials
1014 		 * at that time so we can use them now.
1015 		 */
1016 		KASSERT(unp2->unp_flags & UNP_HAVEPCCACHED,
1017 		    ("unp_connect: listener without cached peercred"));
1018 		memcpy(&unp->unp_peercred, &unp2->unp_peercred,
1019 		    sizeof(unp->unp_peercred));
1020 		unp->unp_flags |= UNP_HAVEPC;
1021 		if (unp2->unp_flags & UNP_WANTCRED)
1022 			unp3->unp_flags |= UNP_WANTCRED;
1023 #ifdef MAC
1024 		SOCK_LOCK(so);
1025 		mac_set_socket_peer_from_socket(so, so3);
1026 		mac_set_socket_peer_from_socket(so3, so);
1027 		SOCK_UNLOCK(so);
1028 #endif
1029 
1030 		so2 = so3;
1031 	}
1032 	error = unp_connect2(so, so2, PRU_CONNECT);
1033 bad2:
1034 	UNP_UNLOCK();
1035 	mtx_lock(&Giant);
1036 bad:
1037 	mtx_assert(&Giant, MA_OWNED);
1038 	if (vp != NULL)
1039 		vput(vp);
1040 	mtx_unlock(&Giant);
1041 	free(sa, M_SONAME);
1042 	UNP_LOCK();
1043 	unp->unp_flags &= ~UNP_CONNECTING;
1044 	return (error);
1045 }
1046 
1047 static int
1048 unp_connect2(struct socket *so, struct socket *so2, int req)
1049 {
1050 	struct unpcb *unp = sotounpcb(so);
1051 	struct unpcb *unp2;
1052 
1053 	UNP_LOCK_ASSERT();
1054 
1055 	if (so2->so_type != so->so_type)
1056 		return (EPROTOTYPE);
1057 	unp2 = sotounpcb(so2);
1058 	KASSERT(unp2 != NULL, ("unp_connect2: unp2 == NULL"));
1059 	unp->unp_conn = unp2;
1060 	switch (so->so_type) {
1061 	case SOCK_DGRAM:
1062 		LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink);
1063 		soisconnected(so);
1064 		break;
1065 
1066 	case SOCK_STREAM:
1067 		unp2->unp_conn = unp;
1068 		if (req == PRU_CONNECT &&
1069 		    ((unp->unp_flags | unp2->unp_flags) & UNP_CONNWAIT))
1070 			soisconnecting(so);
1071 		else
1072 			soisconnected(so);
1073 		soisconnected(so2);
1074 		break;
1075 
1076 	default:
1077 		panic("unp_connect2");
1078 	}
1079 	return (0);
1080 }
1081 
1082 static void
1083 unp_disconnect(struct unpcb *unp)
1084 {
1085 	struct unpcb *unp2 = unp->unp_conn;
1086 	struct socket *so;
1087 
1088 	UNP_LOCK_ASSERT();
1089 
1090 	if (unp2 == NULL)
1091 		return;
1092 	unp->unp_conn = NULL;
1093 	switch (unp->unp_socket->so_type) {
1094 	case SOCK_DGRAM:
1095 		LIST_REMOVE(unp, unp_reflink);
1096 		so = unp->unp_socket;
1097 		SOCK_LOCK(so);
1098 		so->so_state &= ~SS_ISCONNECTED;
1099 		SOCK_UNLOCK(so);
1100 		break;
1101 
1102 	case SOCK_STREAM:
1103 		soisdisconnected(unp->unp_socket);
1104 		unp2->unp_conn = NULL;
1105 		soisdisconnected(unp2->unp_socket);
1106 		break;
1107 	}
1108 }
1109 
1110 /*
1111  * unp_pcblist() assumes that UNIX domain socket memory is never reclaimed by
1112  * the zone (UMA_ZONE_NOFREE), and as such potentially stale pointers are
1113  * safe to reference.  It first scans the list of struct unpcb's to generate
1114  * a pointer list, then it rescans its list one entry at a time to
1115  * externalize and copyout.  It checks the generation number to see if a
1116  * struct unpcb has been reused, and will skip it if so.
1117  */
1118 static int
1119 unp_pcblist(SYSCTL_HANDLER_ARGS)
1120 {
1121 	int error, i, n;
1122 	struct unpcb *unp, **unp_list;
1123 	unp_gen_t gencnt;
1124 	struct xunpgen *xug;
1125 	struct unp_head *head;
1126 	struct xunpcb *xu;
1127 
1128 	head = ((intptr_t)arg1 == SOCK_DGRAM ? &unp_dhead : &unp_shead);
1129 
1130 	/*
1131 	 * The process of preparing the PCB list is too time-consuming and
1132 	 * resource-intensive to repeat twice on every request.
1133 	 */
1134 	if (req->oldptr == NULL) {
1135 		n = unp_count;
1136 		req->oldidx = 2 * (sizeof *xug)
1137 			+ (n + n/8) * sizeof(struct xunpcb);
1138 		return (0);
1139 	}
1140 
1141 	if (req->newptr != NULL)
1142 		return (EPERM);
1143 
1144 	/*
1145 	 * OK, now we're committed to doing something.
1146 	 */
1147 	xug = malloc(sizeof(*xug), M_TEMP, M_WAITOK);
1148 	UNP_LOCK();
1149 	gencnt = unp_gencnt;
1150 	n = unp_count;
1151 	UNP_UNLOCK();
1152 
1153 	xug->xug_len = sizeof *xug;
1154 	xug->xug_count = n;
1155 	xug->xug_gen = gencnt;
1156 	xug->xug_sogen = so_gencnt;
1157 	error = SYSCTL_OUT(req, xug, sizeof *xug);
1158 	if (error) {
1159 		free(xug, M_TEMP);
1160 		return (error);
1161 	}
1162 
1163 	unp_list = malloc(n * sizeof *unp_list, M_TEMP, M_WAITOK);
1164 
1165 	UNP_LOCK();
1166 	for (unp = LIST_FIRST(head), i = 0; unp && i < n;
1167 	     unp = LIST_NEXT(unp, unp_link)) {
1168 		if (unp->unp_gencnt <= gencnt) {
1169 			if (cr_cansee(req->td->td_ucred,
1170 			    unp->unp_socket->so_cred))
1171 				continue;
1172 			unp_list[i++] = unp;
1173 		}
1174 	}
1175 	UNP_UNLOCK();
1176 	n = i;			/* In case we lost some during malloc. */
1177 
1178 	error = 0;
1179 	xu = malloc(sizeof(*xu), M_TEMP, M_WAITOK | M_ZERO);
1180 	for (i = 0; i < n; i++) {
1181 		unp = unp_list[i];
1182 		if (unp->unp_gencnt <= gencnt) {
1183 			xu->xu_len = sizeof *xu;
1184 			xu->xu_unpp = unp;
1185 			/*
1186 			 * XXX - need more locking here to protect against
1187 			 * connect/disconnect races for SMP.
1188 			 */
1189 			if (unp->unp_addr != NULL)
1190 				bcopy(unp->unp_addr, &xu->xu_addr,
1191 				      unp->unp_addr->sun_len);
1192 			if (unp->unp_conn != NULL &&
1193 			    unp->unp_conn->unp_addr != NULL)
1194 				bcopy(unp->unp_conn->unp_addr,
1195 				      &xu->xu_caddr,
1196 				      unp->unp_conn->unp_addr->sun_len);
1197 			bcopy(unp, &xu->xu_unp, sizeof *unp);
1198 			sotoxsocket(unp->unp_socket, &xu->xu_socket);
1199 			error = SYSCTL_OUT(req, xu, sizeof *xu);
1200 		}
1201 	}
1202 	free(xu, M_TEMP);
1203 	if (!error) {
1204 		/*
1205 		 * Give the user an updated idea of our state.  If the
1206 		 * generation differs from what we told her before, she knows
1207 		 * that something happened while we were processing this
1208 		 * request, and it might be necessary to retry.
1209 		 */
1210 		xug->xug_gen = unp_gencnt;
1211 		xug->xug_sogen = so_gencnt;
1212 		xug->xug_count = unp_count;
1213 		error = SYSCTL_OUT(req, xug, sizeof *xug);
1214 	}
1215 	free(unp_list, M_TEMP);
1216 	free(xug, M_TEMP);
1217 	return (error);
1218 }
1219 
1220 SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLFLAG_RD,
1221 	    (caddr_t)(long)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb",
1222 	    "List of active local datagram sockets");
1223 SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, CTLFLAG_RD,
1224 	    (caddr_t)(long)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb",
1225 	    "List of active local stream sockets");
1226 
1227 static void
1228 unp_shutdown(struct unpcb *unp)
1229 {
1230 	struct socket *so;
1231 
1232 	UNP_LOCK_ASSERT();
1233 
1234 	if (unp->unp_socket->so_type == SOCK_STREAM && unp->unp_conn &&
1235 	    (so = unp->unp_conn->unp_socket))
1236 		socantrcvmore(so);
1237 }
1238 
1239 static void
1240 unp_drop(struct unpcb *unp, int errno)
1241 {
1242 	struct socket *so = unp->unp_socket;
1243 
1244 	UNP_LOCK_ASSERT();
1245 
1246 	so->so_error = errno;
1247 	unp_disconnect(unp);
1248 }
1249 
1250 static void
1251 unp_freerights(struct file **rp, int fdcount)
1252 {
1253 	int i;
1254 	struct file *fp;
1255 
1256 	for (i = 0; i < fdcount; i++) {
1257 		fp = *rp;
1258 		/*
1259 		 * Zero the pointer before calling unp_discard since it may
1260 		 * end up in unp_gc()..
1261 		 *
1262 		 * XXXRW: This is less true than it used to be.
1263 		 */
1264 		*rp++ = 0;
1265 		unp_discard(fp);
1266 	}
1267 }
1268 
1269 int
1270 unp_externalize(struct mbuf *control, struct mbuf **controlp)
1271 {
1272 	struct thread *td = curthread;		/* XXX */
1273 	struct cmsghdr *cm = mtod(control, struct cmsghdr *);
1274 	int i;
1275 	int *fdp;
1276 	struct file **rp;
1277 	struct file *fp;
1278 	void *data;
1279 	socklen_t clen = control->m_len, datalen;
1280 	int error, newfds;
1281 	int f;
1282 	u_int newlen;
1283 
1284 	UNP_UNLOCK_ASSERT();
1285 
1286 	error = 0;
1287 	if (controlp != NULL) /* controlp == NULL => free control messages */
1288 		*controlp = NULL;
1289 
1290 	while (cm != NULL) {
1291 		if (sizeof(*cm) > clen || cm->cmsg_len > clen) {
1292 			error = EINVAL;
1293 			break;
1294 		}
1295 
1296 		data = CMSG_DATA(cm);
1297 		datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
1298 
1299 		if (cm->cmsg_level == SOL_SOCKET
1300 		    && cm->cmsg_type == SCM_RIGHTS) {
1301 			newfds = datalen / sizeof(struct file *);
1302 			rp = data;
1303 
1304 			/* If we're not outputting the descriptors free them. */
1305 			if (error || controlp == NULL) {
1306 				unp_freerights(rp, newfds);
1307 				goto next;
1308 			}
1309 			FILEDESC_LOCK(td->td_proc->p_fd);
1310 			/* if the new FD's will not fit free them.  */
1311 			if (!fdavail(td, newfds)) {
1312 				FILEDESC_UNLOCK(td->td_proc->p_fd);
1313 				error = EMSGSIZE;
1314 				unp_freerights(rp, newfds);
1315 				goto next;
1316 			}
1317 			/*
1318 			 * Now change each pointer to an fd in the global
1319 			 * table to an integer that is the index to the local
1320 			 * fd table entry that we set up to point to the
1321 			 * global one we are transferring.
1322 			 */
1323 			newlen = newfds * sizeof(int);
1324 			*controlp = sbcreatecontrol(NULL, newlen,
1325 			    SCM_RIGHTS, SOL_SOCKET);
1326 			if (*controlp == NULL) {
1327 				FILEDESC_UNLOCK(td->td_proc->p_fd);
1328 				error = E2BIG;
1329 				unp_freerights(rp, newfds);
1330 				goto next;
1331 			}
1332 
1333 			fdp = (int *)
1334 			    CMSG_DATA(mtod(*controlp, struct cmsghdr *));
1335 			for (i = 0; i < newfds; i++) {
1336 				if (fdalloc(td, 0, &f))
1337 					panic("unp_externalize fdalloc failed");
1338 				fp = *rp++;
1339 				td->td_proc->p_fd->fd_ofiles[f] = fp;
1340 				FILE_LOCK(fp);
1341 				fp->f_msgcount--;
1342 				FILE_UNLOCK(fp);
1343 				unp_rights--;
1344 				*fdp++ = f;
1345 			}
1346 			FILEDESC_UNLOCK(td->td_proc->p_fd);
1347 		} else {
1348 			/* We can just copy anything else across. */
1349 			if (error || controlp == NULL)
1350 				goto next;
1351 			*controlp = sbcreatecontrol(NULL, datalen,
1352 			    cm->cmsg_type, cm->cmsg_level);
1353 			if (*controlp == NULL) {
1354 				error = ENOBUFS;
1355 				goto next;
1356 			}
1357 			bcopy(data,
1358 			    CMSG_DATA(mtod(*controlp, struct cmsghdr *)),
1359 			    datalen);
1360 		}
1361 
1362 		controlp = &(*controlp)->m_next;
1363 
1364 next:
1365 		if (CMSG_SPACE(datalen) < clen) {
1366 			clen -= CMSG_SPACE(datalen);
1367 			cm = (struct cmsghdr *)
1368 			    ((caddr_t)cm + CMSG_SPACE(datalen));
1369 		} else {
1370 			clen = 0;
1371 			cm = NULL;
1372 		}
1373 	}
1374 
1375 	m_freem(control);
1376 
1377 	return (error);
1378 }
1379 
1380 static void
1381 unp_zone_change(void *tag)
1382 {
1383 
1384 	uma_zone_set_max(unp_zone, maxsockets);
1385 }
1386 
1387 void
1388 unp_init(void)
1389 {
1390 
1391 	unp_zone = uma_zcreate("unpcb", sizeof(struct unpcb), NULL, NULL,
1392 	    NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
1393 	if (unp_zone == NULL)
1394 		panic("unp_init");
1395 	uma_zone_set_max(unp_zone, maxsockets);
1396 	EVENTHANDLER_REGISTER(maxsockets_change, unp_zone_change,
1397 	    NULL, EVENTHANDLER_PRI_ANY);
1398 	LIST_INIT(&unp_dhead);
1399 	LIST_INIT(&unp_shead);
1400 	TASK_INIT(&unp_gc_task, 0, unp_gc, NULL);
1401 	UNP_LOCK_INIT();
1402 }
1403 
1404 static int
1405 unp_internalize(struct mbuf **controlp, struct thread *td)
1406 {
1407 	struct mbuf *control = *controlp;
1408 	struct proc *p = td->td_proc;
1409 	struct filedesc *fdescp = p->p_fd;
1410 	struct cmsghdr *cm = mtod(control, struct cmsghdr *);
1411 	struct cmsgcred *cmcred;
1412 	struct file **rp;
1413 	struct file *fp;
1414 	struct timeval *tv;
1415 	int i, fd, *fdp;
1416 	void *data;
1417 	socklen_t clen = control->m_len, datalen;
1418 	int error, oldfds;
1419 	u_int newlen;
1420 
1421 	UNP_UNLOCK_ASSERT();
1422 
1423 	error = 0;
1424 	*controlp = NULL;
1425 
1426 	while (cm != NULL) {
1427 		if (sizeof(*cm) > clen || cm->cmsg_level != SOL_SOCKET
1428 		    || cm->cmsg_len > clen) {
1429 			error = EINVAL;
1430 			goto out;
1431 		}
1432 
1433 		data = CMSG_DATA(cm);
1434 		datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
1435 
1436 		switch (cm->cmsg_type) {
1437 		/*
1438 		 * Fill in credential information.
1439 		 */
1440 		case SCM_CREDS:
1441 			*controlp = sbcreatecontrol(NULL, sizeof(*cmcred),
1442 			    SCM_CREDS, SOL_SOCKET);
1443 			if (*controlp == NULL) {
1444 				error = ENOBUFS;
1445 				goto out;
1446 			}
1447 
1448 			cmcred = (struct cmsgcred *)
1449 			    CMSG_DATA(mtod(*controlp, struct cmsghdr *));
1450 			cmcred->cmcred_pid = p->p_pid;
1451 			cmcred->cmcred_uid = td->td_ucred->cr_ruid;
1452 			cmcred->cmcred_gid = td->td_ucred->cr_rgid;
1453 			cmcred->cmcred_euid = td->td_ucred->cr_uid;
1454 			cmcred->cmcred_ngroups = MIN(td->td_ucred->cr_ngroups,
1455 							CMGROUP_MAX);
1456 			for (i = 0; i < cmcred->cmcred_ngroups; i++)
1457 				cmcred->cmcred_groups[i] =
1458 				    td->td_ucred->cr_groups[i];
1459 			break;
1460 
1461 		case SCM_RIGHTS:
1462 			oldfds = datalen / sizeof (int);
1463 			/*
1464 			 * Check that all the FDs passed in refer to legal
1465 			 * files.  If not, reject the entire operation.
1466 			 */
1467 			fdp = data;
1468 			FILEDESC_LOCK(fdescp);
1469 			for (i = 0; i < oldfds; i++) {
1470 				fd = *fdp++;
1471 				if ((unsigned)fd >= fdescp->fd_nfiles ||
1472 				    fdescp->fd_ofiles[fd] == NULL) {
1473 					FILEDESC_UNLOCK(fdescp);
1474 					error = EBADF;
1475 					goto out;
1476 				}
1477 				fp = fdescp->fd_ofiles[fd];
1478 				if (!(fp->f_ops->fo_flags & DFLAG_PASSABLE)) {
1479 					FILEDESC_UNLOCK(fdescp);
1480 					error = EOPNOTSUPP;
1481 					goto out;
1482 				}
1483 
1484 			}
1485 			/*
1486 			 * Now replace the integer FDs with pointers to the
1487 			 * associated global file table entry..
1488 			 */
1489 			newlen = oldfds * sizeof(struct file *);
1490 			*controlp = sbcreatecontrol(NULL, newlen,
1491 			    SCM_RIGHTS, SOL_SOCKET);
1492 			if (*controlp == NULL) {
1493 				FILEDESC_UNLOCK(fdescp);
1494 				error = E2BIG;
1495 				goto out;
1496 			}
1497 
1498 			fdp = data;
1499 			rp = (struct file **)
1500 			    CMSG_DATA(mtod(*controlp, struct cmsghdr *));
1501 			for (i = 0; i < oldfds; i++) {
1502 				fp = fdescp->fd_ofiles[*fdp++];
1503 				*rp++ = fp;
1504 				FILE_LOCK(fp);
1505 				fp->f_count++;
1506 				fp->f_msgcount++;
1507 				FILE_UNLOCK(fp);
1508 				unp_rights++;
1509 			}
1510 			FILEDESC_UNLOCK(fdescp);
1511 			break;
1512 
1513 		case SCM_TIMESTAMP:
1514 			*controlp = sbcreatecontrol(NULL, sizeof(*tv),
1515 			    SCM_TIMESTAMP, SOL_SOCKET);
1516 			if (*controlp == NULL) {
1517 				error = ENOBUFS;
1518 				goto out;
1519 			}
1520 			tv = (struct timeval *)
1521 			    CMSG_DATA(mtod(*controlp, struct cmsghdr *));
1522 			microtime(tv);
1523 			break;
1524 
1525 		default:
1526 			error = EINVAL;
1527 			goto out;
1528 		}
1529 
1530 		controlp = &(*controlp)->m_next;
1531 
1532 		if (CMSG_SPACE(datalen) < clen) {
1533 			clen -= CMSG_SPACE(datalen);
1534 			cm = (struct cmsghdr *)
1535 			    ((caddr_t)cm + CMSG_SPACE(datalen));
1536 		} else {
1537 			clen = 0;
1538 			cm = NULL;
1539 		}
1540 	}
1541 
1542 out:
1543 	m_freem(control);
1544 
1545 	return (error);
1546 }
1547 
1548 struct mbuf *
1549 unp_addsockcred(struct thread *td, struct mbuf *control)
1550 {
1551 	struct mbuf *m, *n, *n_prev;
1552 	struct sockcred *sc;
1553 	const struct cmsghdr *cm;
1554 	int ngroups;
1555 	int i;
1556 
1557 	ngroups = MIN(td->td_ucred->cr_ngroups, CMGROUP_MAX);
1558 
1559 	m = sbcreatecontrol(NULL, SOCKCREDSIZE(ngroups), SCM_CREDS, SOL_SOCKET);
1560 	if (m == NULL)
1561 		return (control);
1562 
1563 	sc = (struct sockcred *) CMSG_DATA(mtod(m, struct cmsghdr *));
1564 	sc->sc_uid = td->td_ucred->cr_ruid;
1565 	sc->sc_euid = td->td_ucred->cr_uid;
1566 	sc->sc_gid = td->td_ucred->cr_rgid;
1567 	sc->sc_egid = td->td_ucred->cr_gid;
1568 	sc->sc_ngroups = ngroups;
1569 	for (i = 0; i < sc->sc_ngroups; i++)
1570 		sc->sc_groups[i] = td->td_ucred->cr_groups[i];
1571 
1572 	/*
1573 	 * Unlink SCM_CREDS control messages (struct cmsgcred), since just
1574 	 * created SCM_CREDS control message (struct sockcred) has another
1575 	 * format.
1576 	 */
1577 	if (control != NULL)
1578 		for (n = control, n_prev = NULL; n != NULL;) {
1579 			cm = mtod(n, struct cmsghdr *);
1580     			if (cm->cmsg_level == SOL_SOCKET &&
1581 			    cm->cmsg_type == SCM_CREDS) {
1582     				if (n_prev == NULL)
1583 					control = n->m_next;
1584 				else
1585 					n_prev->m_next = n->m_next;
1586 				n = m_free(n);
1587 			} else {
1588 				n_prev = n;
1589 				n = n->m_next;
1590 			}
1591 		}
1592 
1593 	/* Prepend it to the head. */
1594 	m->m_next = control;
1595 
1596 	return (m);
1597 }
1598 
1599 /*
1600  * unp_defer indicates whether additional work has been defered for a future
1601  * pass through unp_gc().  It is thread local and does not require explicit
1602  * synchronization.
1603  */
1604 static int	unp_defer;
1605 
1606 static int unp_taskcount;
1607 SYSCTL_INT(_net_local, OID_AUTO, taskcount, CTLFLAG_RD, &unp_taskcount, 0, "");
1608 
1609 static int unp_recycled;
1610 SYSCTL_INT(_net_local, OID_AUTO, recycled, CTLFLAG_RD, &unp_recycled, 0, "");
1611 
1612 static void
1613 unp_gc(__unused void *arg, int pending)
1614 {
1615 	struct file *fp, *nextfp;
1616 	struct socket *so;
1617 	struct file **extra_ref, **fpp;
1618 	int nunref, i;
1619 	int nfiles_snap;
1620 	int nfiles_slack = 20;
1621 
1622 	unp_taskcount++;
1623 	unp_defer = 0;
1624 	/*
1625 	 * Before going through all this, set all FDs to be NOT defered and
1626 	 * NOT externally accessible.
1627 	 */
1628 	sx_slock(&filelist_lock);
1629 	LIST_FOREACH(fp, &filehead, f_list)
1630 		fp->f_gcflag &= ~(FMARK|FDEFER);
1631 	do {
1632 		KASSERT(unp_defer >= 0, ("unp_gc: unp_defer %d", unp_defer));
1633 		LIST_FOREACH(fp, &filehead, f_list) {
1634 			FILE_LOCK(fp);
1635 			/*
1636 			 * If the file is not open, skip it -- could be a
1637 			 * file in the process of being opened, or in the
1638 			 * process of being closed.  If the file is
1639 			 * "closing", it may have been marked for deferred
1640 			 * consideration.  Clear the flag now if so.
1641 			 */
1642 			if (fp->f_count == 0) {
1643 				if (fp->f_gcflag & FDEFER)
1644 					unp_defer--;
1645 				fp->f_gcflag &= ~(FMARK|FDEFER);
1646 				FILE_UNLOCK(fp);
1647 				continue;
1648 			}
1649 			/*
1650 			 * If we already marked it as 'defer' in a previous
1651 			 * pass, then try process it this time and un-mark
1652 			 * it.
1653 			 */
1654 			if (fp->f_gcflag & FDEFER) {
1655 				fp->f_gcflag &= ~FDEFER;
1656 				unp_defer--;
1657 			} else {
1658 				/*
1659 				 * if it's not defered, then check if it's
1660 				 * already marked.. if so skip it
1661 				 */
1662 				if (fp->f_gcflag & FMARK) {
1663 					FILE_UNLOCK(fp);
1664 					continue;
1665 				}
1666 				/*
1667 				 * If all references are from messages in
1668 				 * transit, then skip it. it's not externally
1669 				 * accessible.
1670 				 */
1671 				if (fp->f_count == fp->f_msgcount) {
1672 					FILE_UNLOCK(fp);
1673 					continue;
1674 				}
1675 				/*
1676 				 * If it got this far then it must be
1677 				 * externally accessible.
1678 				 */
1679 				fp->f_gcflag |= FMARK;
1680 			}
1681 			/*
1682 			 * Either it was defered, or it is externally
1683 			 * accessible and not already marked so.  Now check
1684 			 * if it is possibly one of OUR sockets.
1685 			 */
1686 			if (fp->f_type != DTYPE_SOCKET ||
1687 			    (so = fp->f_data) == NULL) {
1688 				FILE_UNLOCK(fp);
1689 				continue;
1690 			}
1691 			FILE_UNLOCK(fp);
1692 			if (so->so_proto->pr_domain != &localdomain ||
1693 			    (so->so_proto->pr_flags&PR_RIGHTS) == 0)
1694 				continue;
1695 			/*
1696 			 * So, Ok, it's one of our sockets and it IS
1697 			 * externally accessible (or was defered).  Now we
1698 			 * look to see if we hold any file descriptors in its
1699 			 * message buffers. Follow those links and mark them
1700 			 * as accessible too.
1701 			 */
1702 			SOCKBUF_LOCK(&so->so_rcv);
1703 			unp_scan(so->so_rcv.sb_mb, unp_mark);
1704 			SOCKBUF_UNLOCK(&so->so_rcv);
1705 		}
1706 	} while (unp_defer);
1707 	sx_sunlock(&filelist_lock);
1708 	/*
1709 	 * XXXRW: The following comments need updating for a post-SMPng and
1710 	 * deferred unp_gc() world, but are still generally accurate.
1711 	 *
1712 	 * We grab an extra reference to each of the file table entries that
1713 	 * are not otherwise accessible and then free the rights that are
1714 	 * stored in messages on them.
1715 	 *
1716 	 * The bug in the orginal code is a little tricky, so I'll describe
1717 	 * what's wrong with it here.
1718 	 *
1719 	 * It is incorrect to simply unp_discard each entry for f_msgcount
1720 	 * times -- consider the case of sockets A and B that contain
1721 	 * references to each other.  On a last close of some other socket,
1722 	 * we trigger a gc since the number of outstanding rights (unp_rights)
1723 	 * is non-zero.  If during the sweep phase the gc code unp_discards,
1724 	 * we end up doing a (full) closef on the descriptor.  A closef on A
1725 	 * results in the following chain.  Closef calls soo_close, which
1726 	 * calls soclose.   Soclose calls first (through the switch
1727 	 * uipc_usrreq) unp_detach, which re-invokes unp_gc.  Unp_gc simply
1728 	 * returns because the previous instance had set unp_gcing, and we
1729 	 * return all the way back to soclose, which marks the socket with
1730 	 * SS_NOFDREF, and then calls sofree.  Sofree calls sorflush to free
1731 	 * up the rights that are queued in messages on the socket A, i.e.,
1732 	 * the reference on B.  The sorflush calls via the dom_dispose switch
1733 	 * unp_dispose, which unp_scans with unp_discard.  This second
1734 	 * instance of unp_discard just calls closef on B.
1735 	 *
1736 	 * Well, a similar chain occurs on B, resulting in a sorflush on B,
1737 	 * which results in another closef on A.  Unfortunately, A is already
1738 	 * being closed, and the descriptor has already been marked with
1739 	 * SS_NOFDREF, and soclose panics at this point.
1740 	 *
1741 	 * Here, we first take an extra reference to each inaccessible
1742 	 * descriptor.  Then, we call sorflush ourself, since we know it is a
1743 	 * Unix domain socket anyhow.  After we destroy all the rights
1744 	 * carried in messages, we do a last closef to get rid of our extra
1745 	 * reference.  This is the last close, and the unp_detach etc will
1746 	 * shut down the socket.
1747 	 *
1748 	 * 91/09/19, bsy@cs.cmu.edu
1749 	 */
1750 again:
1751 	nfiles_snap = openfiles + nfiles_slack;	/* some slack */
1752 	extra_ref = malloc(nfiles_snap * sizeof(struct file *), M_TEMP,
1753 	    M_WAITOK);
1754 	sx_slock(&filelist_lock);
1755 	if (nfiles_snap < openfiles) {
1756 		sx_sunlock(&filelist_lock);
1757 		free(extra_ref, M_TEMP);
1758 		nfiles_slack += 20;
1759 		goto again;
1760 	}
1761 	for (nunref = 0, fp = LIST_FIRST(&filehead), fpp = extra_ref;
1762 	    fp != NULL; fp = nextfp) {
1763 		nextfp = LIST_NEXT(fp, f_list);
1764 		FILE_LOCK(fp);
1765 		/*
1766 		 * If it's not open, skip it
1767 		 */
1768 		if (fp->f_count == 0) {
1769 			FILE_UNLOCK(fp);
1770 			continue;
1771 		}
1772 		/*
1773 		 * If all refs are from msgs, and it's not marked accessible
1774 		 * then it must be referenced from some unreachable cycle of
1775 		 * (shut-down) FDs, so include it in our list of FDs to
1776 		 * remove.
1777 		 */
1778 		if (fp->f_count == fp->f_msgcount && !(fp->f_gcflag & FMARK)) {
1779 			*fpp++ = fp;
1780 			nunref++;
1781 			fp->f_count++;
1782 		}
1783 		FILE_UNLOCK(fp);
1784 	}
1785 	sx_sunlock(&filelist_lock);
1786 	/*
1787 	 * For each FD on our hit list, do the following two things:
1788 	 */
1789 	for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) {
1790 		struct file *tfp = *fpp;
1791 		FILE_LOCK(tfp);
1792 		if (tfp->f_type == DTYPE_SOCKET &&
1793 		    tfp->f_data != NULL) {
1794 			FILE_UNLOCK(tfp);
1795 			sorflush(tfp->f_data);
1796 		} else {
1797 			FILE_UNLOCK(tfp);
1798 		}
1799 	}
1800 	for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) {
1801 		closef(*fpp, (struct thread *) NULL);
1802 		unp_recycled++;
1803 	}
1804 	free(extra_ref, M_TEMP);
1805 }
1806 
1807 void
1808 unp_dispose(struct mbuf *m)
1809 {
1810 
1811 	if (m)
1812 		unp_scan(m, unp_discard);
1813 }
1814 
1815 static int
1816 unp_listen(struct socket *so, struct unpcb *unp, int backlog,
1817     struct thread *td)
1818 {
1819 	int error;
1820 
1821 	UNP_LOCK_ASSERT();
1822 
1823 	SOCK_LOCK(so);
1824 	error = solisten_proto_check(so);
1825 	if (error == 0) {
1826 		cru2x(td->td_ucred, &unp->unp_peercred);
1827 		unp->unp_flags |= UNP_HAVEPCCACHED;
1828 		solisten_proto(so, backlog);
1829 	}
1830 	SOCK_UNLOCK(so);
1831 	return (error);
1832 }
1833 
1834 static void
1835 unp_scan(struct mbuf *m0, void (*op)(struct file *))
1836 {
1837 	struct mbuf *m;
1838 	struct file **rp;
1839 	struct cmsghdr *cm;
1840 	void *data;
1841 	int i;
1842 	socklen_t clen, datalen;
1843 	int qfds;
1844 
1845 	while (m0 != NULL) {
1846 		for (m = m0; m; m = m->m_next) {
1847 			if (m->m_type != MT_CONTROL)
1848 				continue;
1849 
1850 			cm = mtod(m, struct cmsghdr *);
1851 			clen = m->m_len;
1852 
1853 			while (cm != NULL) {
1854 				if (sizeof(*cm) > clen || cm->cmsg_len > clen)
1855 					break;
1856 
1857 				data = CMSG_DATA(cm);
1858 				datalen = (caddr_t)cm + cm->cmsg_len
1859 				    - (caddr_t)data;
1860 
1861 				if (cm->cmsg_level == SOL_SOCKET &&
1862 				    cm->cmsg_type == SCM_RIGHTS) {
1863 					qfds = datalen / sizeof (struct file *);
1864 					rp = data;
1865 					for (i = 0; i < qfds; i++)
1866 						(*op)(*rp++);
1867 				}
1868 
1869 				if (CMSG_SPACE(datalen) < clen) {
1870 					clen -= CMSG_SPACE(datalen);
1871 					cm = (struct cmsghdr *)
1872 					    ((caddr_t)cm + CMSG_SPACE(datalen));
1873 				} else {
1874 					clen = 0;
1875 					cm = NULL;
1876 				}
1877 			}
1878 		}
1879 		m0 = m0->m_act;
1880 	}
1881 }
1882 
1883 static void
1884 unp_mark(struct file *fp)
1885 {
1886 	if (fp->f_gcflag & FMARK)
1887 		return;
1888 	unp_defer++;
1889 	fp->f_gcflag |= (FMARK|FDEFER);
1890 }
1891 
1892 static void
1893 unp_discard(struct file *fp)
1894 {
1895 	UNP_LOCK();
1896 	FILE_LOCK(fp);
1897 	fp->f_msgcount--;
1898 	unp_rights--;
1899 	FILE_UNLOCK(fp);
1900 	UNP_UNLOCK();
1901 	(void) closef(fp, (struct thread *)NULL);
1902 }
1903