xref: /netbsd-src/sys/kern/uipc_usrreq.c (revision 627f7eb200a4419d89b531d55fccd2ee3ffdcde0)
1 /*	$NetBSD: uipc_usrreq.c,v 1.200 2020/11/06 14:50:13 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998, 2000, 2004, 2008, 2009, 2020 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9  * NASA Ames Research Center, and by Andrew Doran.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*
34  * Copyright (c) 1982, 1986, 1989, 1991, 1993
35  *	The Regents of the University of California.  All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  * 1. Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  * 2. Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in the
44  *    documentation and/or other materials provided with the distribution.
45  * 3. Neither the name of the University nor the names of its contributors
46  *    may be used to endorse or promote products derived from this software
47  *    without specific prior written permission.
48  *
49  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59  * SUCH DAMAGE.
60  *
61  *	@(#)uipc_usrreq.c	8.9 (Berkeley) 5/14/95
62  */
63 
64 /*
65  * Copyright (c) 1997 Christopher G. Demetriou.  All rights reserved.
66  *
67  * Redistribution and use in source and binary forms, with or without
68  * modification, are permitted provided that the following conditions
69  * are met:
70  * 1. Redistributions of source code must retain the above copyright
71  *    notice, this list of conditions and the following disclaimer.
72  * 2. Redistributions in binary form must reproduce the above copyright
73  *    notice, this list of conditions and the following disclaimer in the
74  *    documentation and/or other materials provided with the distribution.
75  * 3. All advertising materials mentioning features or use of this software
76  *    must display the following acknowledgement:
77  *	This product includes software developed by the University of
78  *	California, Berkeley and its contributors.
79  * 4. Neither the name of the University nor the names of its contributors
80  *    may be used to endorse or promote products derived from this software
81  *    without specific prior written permission.
82  *
83  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
84  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
85  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
86  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
87  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
88  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
89  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
90  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
91  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
92  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
93  * SUCH DAMAGE.
94  *
95  *	@(#)uipc_usrreq.c	8.9 (Berkeley) 5/14/95
96  */
97 
98 #include <sys/cdefs.h>
99 __KERNEL_RCSID(0, "$NetBSD: uipc_usrreq.c,v 1.200 2020/11/06 14:50:13 christos Exp $");
100 
101 #ifdef _KERNEL_OPT
102 #include "opt_compat_netbsd.h"
103 #endif
104 
105 #include <sys/param.h>
106 #include <sys/systm.h>
107 #include <sys/proc.h>
108 #include <sys/filedesc.h>
109 #include <sys/domain.h>
110 #include <sys/protosw.h>
111 #include <sys/socket.h>
112 #include <sys/socketvar.h>
113 #include <sys/unpcb.h>
114 #include <sys/un.h>
115 #include <sys/namei.h>
116 #include <sys/vnode.h>
117 #include <sys/file.h>
118 #include <sys/stat.h>
119 #include <sys/mbuf.h>
120 #include <sys/kauth.h>
121 #include <sys/kmem.h>
122 #include <sys/atomic.h>
123 #include <sys/uidinfo.h>
124 #include <sys/kernel.h>
125 #include <sys/kthread.h>
126 #include <sys/compat_stub.h>
127 
128 #include <compat/sys/socket.h>
129 #include <compat/net/route_70.h>
130 
131 /*
132  * Unix communications domain.
133  *
134  * TODO:
135  *	RDM
136  *	rethink name space problems
137  *	need a proper out-of-band
138  *
139  * Notes on locking:
140  *
141  * The generic rules noted in uipc_socket2.c apply.  In addition:
142  *
143  * o We have a global lock, uipc_lock.
144  *
145  * o All datagram sockets are locked by uipc_lock.
146  *
147  * o For stream socketpairs, the two endpoints are created sharing the same
148  *   independent lock.  Sockets presented to PRU_CONNECT2 must already have
149  *   matching locks.
150  *
151  * o Stream sockets created via socket() start life with their own
152  *   independent lock.
153  *
154  * o Stream connections to a named endpoint are slightly more complicated.
155  *   Sockets that have called listen() have their lock pointer mutated to
156  *   the global uipc_lock.  When establishing a connection, the connecting
157  *   socket also has its lock mutated to uipc_lock, which matches the head
158  *   (listening socket).  We create a new socket for accept() to return, and
159  *   that also shares the head's lock.  Until the connection is completely
160  *   done on both ends, all three sockets are locked by uipc_lock.  Once the
161  *   connection is complete, the association with the head's lock is broken.
162  *   The connecting socket and the socket returned from accept() have their
163  *   lock pointers mutated away from uipc_lock, and back to the connecting
164  *   socket's original, independent lock.  The head continues to be locked
165  *   by uipc_lock.
166  *
167  * o If uipc_lock is determined to be a significant source of contention,
168  *   it could easily be hashed out.  It is difficult to simply make it an
169  *   independent lock because of visibility / garbage collection issues:
170  *   if a socket has been associated with a lock at any point, that lock
171  *   must remain valid until the socket is no longer visible in the system.
172  *   The lock must not be freed or otherwise destroyed until any sockets
173  *   that had referenced it have also been destroyed.
174  */
175 const struct sockaddr_un sun_noname = {
176 	.sun_len = offsetof(struct sockaddr_un, sun_path),
177 	.sun_family = AF_LOCAL,
178 };
179 ino_t	unp_ino;			/* prototype for fake inode numbers */
180 
181 static struct mbuf * unp_addsockcred(struct lwp *, struct mbuf *);
182 static void   unp_discard_later(file_t *);
183 static void   unp_discard_now(file_t *);
184 static void   unp_disconnect1(struct unpcb *);
185 static bool   unp_drop(struct unpcb *, int);
186 static int    unp_internalize(struct mbuf **);
187 static void   unp_mark(file_t *);
188 static void   unp_scan(struct mbuf *, void (*)(file_t *), int);
189 static void   unp_shutdown1(struct unpcb *);
190 static void   unp_thread(void *);
191 static void   unp_thread_kick(void);
192 
193 static kmutex_t *uipc_lock;
194 
195 static kcondvar_t unp_thread_cv;
196 static lwp_t *unp_thread_lwp;
197 static SLIST_HEAD(,file) unp_thread_discard;
198 static int unp_defer;
199 static struct sysctllog *usrreq_sysctllog;
200 static void unp_sysctl_create(void);
201 
202 /* Compat interface */
203 
204 struct mbuf * stub_compat_70_unp_addsockcred(lwp_t *, struct mbuf *);
205 
206 struct mbuf * stub_compat_70_unp_addsockcred(struct lwp *lwp,
207     struct mbuf *control)
208 {
209 
210 /* just copy our initial argument */
211 	return control;
212 }
213 
214 bool compat70_ocreds_valid = false;
215 
216 /*
217  * Initialize Unix protocols.
218  */
219 void
220 uipc_init(void)
221 {
222 	int error;
223 
224 	unp_sysctl_create();
225 
226 	uipc_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
227 	cv_init(&unp_thread_cv, "unpgc");
228 
229 	error = kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL, unp_thread,
230 	    NULL, &unp_thread_lwp, "unpgc");
231 	if (error != 0)
232 		panic("uipc_init %d", error);
233 }
234 
235 static void
236 unp_connid(struct lwp *l, struct unpcb *unp, int flags)
237 {
238 	unp->unp_connid.unp_pid = l->l_proc->p_pid;
239 	unp->unp_connid.unp_euid = kauth_cred_geteuid(l->l_cred);
240 	unp->unp_connid.unp_egid = kauth_cred_getegid(l->l_cred);
241 	unp->unp_flags |= flags;
242 }
243 
244 /*
245  * A connection succeeded: disassociate both endpoints from the head's
246  * lock, and make them share their own lock.  There is a race here: for
247  * a very brief time one endpoint will be locked by a different lock
248  * than the other end.  However, since the current thread holds the old
249  * lock (the listening socket's lock, the head) access can still only be
250  * made to one side of the connection.
251  */
252 static void
253 unp_setpeerlocks(struct socket *so, struct socket *so2)
254 {
255 	struct unpcb *unp;
256 	kmutex_t *lock;
257 
258 	KASSERT(solocked2(so, so2));
259 
260 	/*
261 	 * Bail out if either end of the socket is not yet fully
262 	 * connected or accepted.  We only break the lock association
263 	 * with the head when the pair of sockets stand completely
264 	 * on their own.
265 	 */
266 	KASSERT(so->so_head == NULL);
267 	if (so2->so_head != NULL)
268 		return;
269 
270 	/*
271 	 * Drop references to old lock.  A third reference (from the
272 	 * queue head) must be held as we still hold its lock.  Bonus:
273 	 * we don't need to worry about garbage collecting the lock.
274 	 */
275 	lock = so->so_lock;
276 	KASSERT(lock == uipc_lock);
277 	mutex_obj_free(lock);
278 	mutex_obj_free(lock);
279 
280 	/*
281 	 * Grab stream lock from the initiator and share between the two
282 	 * endpoints.  Issue memory barrier to ensure all modifications
283 	 * become globally visible before the lock change.  so2 is
284 	 * assumed not to have a stream lock, because it was created
285 	 * purely for the server side to accept this connection and
286 	 * started out life using the domain-wide lock.
287 	 */
288 	unp = sotounpcb(so);
289 	KASSERT(unp->unp_streamlock != NULL);
290 	KASSERT(sotounpcb(so2)->unp_streamlock == NULL);
291 	lock = unp->unp_streamlock;
292 	unp->unp_streamlock = NULL;
293 	mutex_obj_hold(lock);
294 	membar_exit();
295 	/*
296 	 * possible race if lock is not held - see comment in
297 	 * uipc_usrreq(PRU_ACCEPT).
298 	 */
299 	KASSERT(mutex_owned(lock));
300 	solockreset(so, lock);
301 	solockreset(so2, lock);
302 }
303 
304 /*
305  * Reset a socket's lock back to the domain-wide lock.
306  */
307 static void
308 unp_resetlock(struct socket *so)
309 {
310 	kmutex_t *olock, *nlock;
311 	struct unpcb *unp;
312 
313 	KASSERT(solocked(so));
314 
315 	olock = so->so_lock;
316 	nlock = uipc_lock;
317 	if (olock == nlock)
318 		return;
319 	unp = sotounpcb(so);
320 	KASSERT(unp->unp_streamlock == NULL);
321 	unp->unp_streamlock = olock;
322 	mutex_obj_hold(nlock);
323 	mutex_enter(nlock);
324 	solockreset(so, nlock);
325 	mutex_exit(olock);
326 }
327 
328 static void
329 unp_free(struct unpcb *unp)
330 {
331 	if (unp->unp_addr)
332 		free(unp->unp_addr, M_SONAME);
333 	if (unp->unp_streamlock != NULL)
334 		mutex_obj_free(unp->unp_streamlock);
335 	kmem_free(unp, sizeof(*unp));
336 }
337 
338 static int
339 unp_output(struct mbuf *m, struct mbuf *control, struct unpcb *unp)
340 {
341 	struct socket *so2;
342 	const struct sockaddr_un *sun;
343 
344 	/* XXX: server side closed the socket */
345 	if (unp->unp_conn == NULL)
346 		return ECONNREFUSED;
347 	so2 = unp->unp_conn->unp_socket;
348 
349 	KASSERT(solocked(so2));
350 
351 	if (unp->unp_addr)
352 		sun = unp->unp_addr;
353 	else
354 		sun = &sun_noname;
355 	if (unp->unp_conn->unp_flags & UNP_WANTCRED)
356 		control = unp_addsockcred(curlwp, control);
357 	if (unp->unp_conn->unp_flags & UNP_OWANTCRED)
358 		MODULE_HOOK_CALL(uipc_unp_70_hook, (curlwp, control),
359 		    stub_compat_70_unp_addsockcred(curlwp, control), control);
360 	if (sbappendaddr(&so2->so_rcv, (const struct sockaddr *)sun, m,
361 	    control) == 0) {
362 		unp_dispose(control);
363 		m_freem(control);
364 		m_freem(m);
365 		/* Don't call soroverflow because we're returning this
366 		 * error directly to the sender. */
367 		so2->so_rcv.sb_overflowed++;
368 		return ENOBUFS;
369 	} else {
370 		sorwakeup(so2);
371 		return 0;
372 	}
373 }
374 
375 static void
376 unp_setaddr(struct socket *so, struct sockaddr *nam, bool peeraddr)
377 {
378 	const struct sockaddr_un *sun = NULL;
379 	struct unpcb *unp;
380 
381 	KASSERT(solocked(so));
382 	unp = sotounpcb(so);
383 
384 	if (peeraddr) {
385 		if (unp->unp_conn && unp->unp_conn->unp_addr)
386 			sun = unp->unp_conn->unp_addr;
387 	} else {
388 		if (unp->unp_addr)
389 			sun = unp->unp_addr;
390 	}
391 	if (sun == NULL)
392 		sun = &sun_noname;
393 
394 	memcpy(nam, sun, sun->sun_len);
395 }
396 
397 static int
398 unp_rcvd(struct socket *so, int flags, struct lwp *l)
399 {
400 	struct unpcb *unp = sotounpcb(so);
401 	struct socket *so2;
402 	u_int newhiwat;
403 
404 	KASSERT(solocked(so));
405 	KASSERT(unp != NULL);
406 
407 	switch (so->so_type) {
408 
409 	case SOCK_DGRAM:
410 		panic("uipc 1");
411 		/*NOTREACHED*/
412 
413 	case SOCK_SEQPACKET: /* FALLTHROUGH */
414 	case SOCK_STREAM:
415 #define	rcv (&so->so_rcv)
416 #define snd (&so2->so_snd)
417 		if (unp->unp_conn == 0)
418 			break;
419 		so2 = unp->unp_conn->unp_socket;
420 		KASSERT(solocked2(so, so2));
421 		/*
422 		 * Adjust backpressure on sender
423 		 * and wakeup any waiting to write.
424 		 */
425 		snd->sb_mbmax += unp->unp_mbcnt - rcv->sb_mbcnt;
426 		unp->unp_mbcnt = rcv->sb_mbcnt;
427 		newhiwat = snd->sb_hiwat + unp->unp_cc - rcv->sb_cc;
428 		(void)chgsbsize(so2->so_uidinfo,
429 		    &snd->sb_hiwat, newhiwat, RLIM_INFINITY);
430 		unp->unp_cc = rcv->sb_cc;
431 		sowwakeup(so2);
432 #undef snd
433 #undef rcv
434 		break;
435 
436 	default:
437 		panic("uipc 2");
438 	}
439 
440 	return 0;
441 }
442 
443 static int
444 unp_recvoob(struct socket *so, struct mbuf *m, int flags)
445 {
446 	KASSERT(solocked(so));
447 
448 	return EOPNOTSUPP;
449 }
450 
451 static int
452 unp_send(struct socket *so, struct mbuf *m, struct sockaddr *nam,
453     struct mbuf *control, struct lwp *l)
454 {
455 	struct unpcb *unp = sotounpcb(so);
456 	int error = 0;
457 	u_int newhiwat;
458 	struct socket *so2;
459 
460 	KASSERT(solocked(so));
461 	KASSERT(unp != NULL);
462 	KASSERT(m != NULL);
463 
464 	/*
465 	 * Note: unp_internalize() rejects any control message
466 	 * other than SCM_RIGHTS, and only allows one.  This
467 	 * has the side-effect of preventing a caller from
468 	 * forging SCM_CREDS.
469 	 */
470 	if (control) {
471 		sounlock(so);
472 		error = unp_internalize(&control);
473 		solock(so);
474 		if (error != 0) {
475 			m_freem(control);
476 			m_freem(m);
477 			return error;
478 		}
479 	}
480 
481 	switch (so->so_type) {
482 
483 	case SOCK_DGRAM: {
484 		KASSERT(so->so_lock == uipc_lock);
485 		if (nam) {
486 			if ((so->so_state & SS_ISCONNECTED) != 0)
487 				error = EISCONN;
488 			else {
489 				/*
490 				 * Note: once connected, the
491 				 * socket's lock must not be
492 				 * dropped until we have sent
493 				 * the message and disconnected.
494 				 * This is necessary to prevent
495 				 * intervening control ops, like
496 				 * another connection.
497 				 */
498 				error = unp_connect(so, nam, l);
499 			}
500 		} else {
501 			if ((so->so_state & SS_ISCONNECTED) == 0)
502 				error = ENOTCONN;
503 		}
504 		if (error) {
505 			unp_dispose(control);
506 			m_freem(control);
507 			m_freem(m);
508 			return error;
509 		}
510 		error = unp_output(m, control, unp);
511 		if (nam)
512 			unp_disconnect1(unp);
513 		break;
514 	}
515 
516 	case SOCK_SEQPACKET: /* FALLTHROUGH */
517 	case SOCK_STREAM:
518 #define	rcv (&so2->so_rcv)
519 #define	snd (&so->so_snd)
520 		if (unp->unp_conn == NULL) {
521 			error = ENOTCONN;
522 			break;
523 		}
524 		so2 = unp->unp_conn->unp_socket;
525 		KASSERT(solocked2(so, so2));
526 		if (unp->unp_conn->unp_flags & UNP_WANTCRED) {
527 			/*
528 			 * Credentials are passed only once on
529 			 * SOCK_STREAM and SOCK_SEQPACKET.
530 			 */
531 			unp->unp_conn->unp_flags &= ~UNP_WANTCRED;
532 			control = unp_addsockcred(l, control);
533 		}
534 		if (unp->unp_conn->unp_flags & UNP_OWANTCRED) {
535 			/*
536 			 * Credentials are passed only once on
537 			 * SOCK_STREAM and SOCK_SEQPACKET.
538 			 */
539 			unp->unp_conn->unp_flags &= ~UNP_OWANTCRED;
540 			MODULE_HOOK_CALL(uipc_unp_70_hook, (curlwp, control),
541 			    stub_compat_70_unp_addsockcred(curlwp, control),
542 			    control);
543 		}
544 		/*
545 		 * Send to paired receive port, and then reduce
546 		 * send buffer hiwater marks to maintain backpressure.
547 		 * Wake up readers.
548 		 */
549 		if (control) {
550 			if (sbappendcontrol(rcv, m, control) != 0)
551 				control = NULL;
552 		} else {
553 			switch(so->so_type) {
554 			case SOCK_SEQPACKET:
555 				sbappendrecord(rcv, m);
556 				break;
557 			case SOCK_STREAM:
558 				sbappend(rcv, m);
559 				break;
560 			default:
561 				panic("uipc_usrreq");
562 				break;
563 			}
564 		}
565 		snd->sb_mbmax -=
566 		    rcv->sb_mbcnt - unp->unp_conn->unp_mbcnt;
567 		unp->unp_conn->unp_mbcnt = rcv->sb_mbcnt;
568 		newhiwat = snd->sb_hiwat -
569 		    (rcv->sb_cc - unp->unp_conn->unp_cc);
570 		(void)chgsbsize(so->so_uidinfo,
571 		    &snd->sb_hiwat, newhiwat, RLIM_INFINITY);
572 		unp->unp_conn->unp_cc = rcv->sb_cc;
573 		sorwakeup(so2);
574 #undef snd
575 #undef rcv
576 		if (control != NULL) {
577 			unp_dispose(control);
578 			m_freem(control);
579 		}
580 		break;
581 
582 	default:
583 		panic("uipc 4");
584 	}
585 
586 	return error;
587 }
588 
589 static int
590 unp_sendoob(struct socket *so, struct mbuf *m, struct mbuf * control)
591 {
592 	KASSERT(solocked(so));
593 
594 	m_freem(m);
595 	m_freem(control);
596 
597 	return EOPNOTSUPP;
598 }
599 
600 /*
601  * Unix domain socket option processing.
602  */
603 int
604 uipc_ctloutput(int op, struct socket *so, struct sockopt *sopt)
605 {
606 	struct unpcb *unp = sotounpcb(so);
607 	int optval = 0, error = 0;
608 
609 	KASSERT(solocked(so));
610 
611 	if (sopt->sopt_level != 0) {
612 		error = ENOPROTOOPT;
613 	} else switch (op) {
614 
615 	case PRCO_SETOPT:
616 		switch (sopt->sopt_name) {
617 		case LOCAL_OCREDS:
618 			if (!compat70_ocreds_valid)  {
619 				error = ENOPROTOOPT;
620 				break;
621 			}
622 			/* FALLTHROUGH */
623 		case LOCAL_CREDS:
624 		case LOCAL_CONNWAIT:
625 			error = sockopt_getint(sopt, &optval);
626 			if (error)
627 				break;
628 			switch (sopt->sopt_name) {
629 #define	OPTSET(bit) \
630 	if (optval) \
631 		unp->unp_flags |= (bit); \
632 	else \
633 		unp->unp_flags &= ~(bit);
634 
635 			case LOCAL_CREDS:
636 				OPTSET(UNP_WANTCRED);
637 				break;
638 			case LOCAL_CONNWAIT:
639 				OPTSET(UNP_CONNWAIT);
640 				break;
641 			case LOCAL_OCREDS:
642 				OPTSET(UNP_OWANTCRED);
643 				break;
644 			}
645 			break;
646 #undef OPTSET
647 
648 		default:
649 			error = ENOPROTOOPT;
650 			break;
651 		}
652 		break;
653 
654 	case PRCO_GETOPT:
655 		sounlock(so);
656 		switch (sopt->sopt_name) {
657 		case LOCAL_PEEREID:
658 			if (unp->unp_flags & UNP_EIDSVALID) {
659 				error = sockopt_set(sopt, &unp->unp_connid,
660 				    sizeof(unp->unp_connid));
661 			} else {
662 				error = EINVAL;
663 			}
664 			break;
665 		case LOCAL_CREDS:
666 #define	OPTBIT(bit)	(unp->unp_flags & (bit) ? 1 : 0)
667 
668 			optval = OPTBIT(UNP_WANTCRED);
669 			error = sockopt_setint(sopt, optval);
670 			break;
671 		case LOCAL_OCREDS:
672 			if (compat70_ocreds_valid) {
673 				optval = OPTBIT(UNP_OWANTCRED);
674 				error = sockopt_setint(sopt, optval);
675 				break;
676 			}
677 #undef OPTBIT
678 			/* FALLTHROUGH */
679 		default:
680 			error = ENOPROTOOPT;
681 			break;
682 		}
683 		solock(so);
684 		break;
685 	}
686 	return (error);
687 }
688 
689 /*
690  * Both send and receive buffers are allocated PIPSIZ bytes of buffering
691  * for stream sockets, although the total for sender and receiver is
692  * actually only PIPSIZ.
693  * Datagram sockets really use the sendspace as the maximum datagram size,
694  * and don't really want to reserve the sendspace.  Their recvspace should
695  * be large enough for at least one max-size datagram plus address.
696  */
697 #ifndef PIPSIZ
698 #define	PIPSIZ	8192
699 #endif
700 u_long	unpst_sendspace = PIPSIZ;
701 u_long	unpst_recvspace = PIPSIZ;
702 u_long	unpdg_sendspace = 2*1024;	/* really max datagram size */
703 u_long	unpdg_recvspace = 16*1024;
704 
705 u_int	unp_rights;			/* files in flight */
706 u_int	unp_rights_ratio = 2;		/* limit, fraction of maxfiles */
707 
708 static int
709 unp_attach(struct socket *so, int proto)
710 {
711 	struct unpcb *unp = sotounpcb(so);
712 	u_long sndspc, rcvspc;
713 	int error;
714 
715 	KASSERT(unp == NULL);
716 
717 	switch (so->so_type) {
718 	case SOCK_SEQPACKET:
719 		/* FALLTHROUGH */
720 	case SOCK_STREAM:
721 		if (so->so_lock == NULL) {
722 			so->so_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
723 			solock(so);
724 		}
725 		sndspc = unpst_sendspace;
726 		rcvspc = unpst_recvspace;
727 		break;
728 
729 	case SOCK_DGRAM:
730 		if (so->so_lock == NULL) {
731 			mutex_obj_hold(uipc_lock);
732 			so->so_lock = uipc_lock;
733 			solock(so);
734 		}
735 		sndspc = unpdg_sendspace;
736 		rcvspc = unpdg_recvspace;
737 		break;
738 
739 	default:
740 		panic("unp_attach");
741 	}
742 
743 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
744 		error = soreserve(so, sndspc, rcvspc);
745 		if (error) {
746 			return error;
747 		}
748 	}
749 
750 	unp = kmem_zalloc(sizeof(*unp), KM_SLEEP);
751 	nanotime(&unp->unp_ctime);
752 	unp->unp_socket = so;
753 	so->so_pcb = unp;
754 
755 	KASSERT(solocked(so));
756 	return 0;
757 }
758 
759 static void
760 unp_detach(struct socket *so)
761 {
762 	struct unpcb *unp;
763 	vnode_t *vp;
764 
765 	unp = sotounpcb(so);
766 	KASSERT(unp != NULL);
767 	KASSERT(solocked(so));
768  retry:
769 	if ((vp = unp->unp_vnode) != NULL) {
770 		sounlock(so);
771 		/* Acquire v_interlock to protect against unp_connect(). */
772 		/* XXXAD racy */
773 		mutex_enter(vp->v_interlock);
774 		vp->v_socket = NULL;
775 		mutex_exit(vp->v_interlock);
776 		vrele(vp);
777 		solock(so);
778 		unp->unp_vnode = NULL;
779 	}
780 	if (unp->unp_conn)
781 		unp_disconnect1(unp);
782 	while (unp->unp_refs) {
783 		KASSERT(solocked2(so, unp->unp_refs->unp_socket));
784 		if (unp_drop(unp->unp_refs, ECONNRESET)) {
785 			solock(so);
786 			goto retry;
787 		}
788 	}
789 	soisdisconnected(so);
790 	so->so_pcb = NULL;
791 	if (unp_rights) {
792 		/*
793 		 * Normally the receive buffer is flushed later, in sofree,
794 		 * but if our receive buffer holds references to files that
795 		 * are now garbage, we will enqueue those file references to
796 		 * the garbage collector and kick it into action.
797 		 */
798 		sorflush(so);
799 		unp_free(unp);
800 		unp_thread_kick();
801 	} else
802 		unp_free(unp);
803 }
804 
805 static int
806 unp_accept(struct socket *so, struct sockaddr *nam)
807 {
808 	struct unpcb *unp = sotounpcb(so);
809 	struct socket *so2;
810 
811 	KASSERT(solocked(so));
812 	KASSERT(nam != NULL);
813 
814 	/* XXX code review required to determine if unp can ever be NULL */
815 	if (unp == NULL)
816 		return EINVAL;
817 
818 	KASSERT(so->so_lock == uipc_lock);
819 	/*
820 	 * Mark the initiating STREAM socket as connected *ONLY*
821 	 * after it's been accepted.  This prevents a client from
822 	 * overrunning a server and receiving ECONNREFUSED.
823 	 */
824 	if (unp->unp_conn == NULL) {
825 		/*
826 		 * This will use the empty socket and will not
827 		 * allocate.
828 		 */
829 		unp_setaddr(so, nam, true);
830 		return 0;
831 	}
832 	so2 = unp->unp_conn->unp_socket;
833 	if (so2->so_state & SS_ISCONNECTING) {
834 		KASSERT(solocked2(so, so->so_head));
835 		KASSERT(solocked2(so2, so->so_head));
836 		soisconnected(so2);
837 	}
838 	/*
839 	 * If the connection is fully established, break the
840 	 * association with uipc_lock and give the connected
841 	 * pair a separate lock to share.
842 	 * There is a race here: sotounpcb(so2)->unp_streamlock
843 	 * is not locked, so when changing so2->so_lock
844 	 * another thread can grab it while so->so_lock is still
845 	 * pointing to the (locked) uipc_lock.
846 	 * this should be harmless, except that this makes
847 	 * solocked2() and solocked() unreliable.
848 	 * Another problem is that unp_setaddr() expects the
849 	 * the socket locked. Grabing sotounpcb(so2)->unp_streamlock
850 	 * fixes both issues.
851 	 */
852 	mutex_enter(sotounpcb(so2)->unp_streamlock);
853 	unp_setpeerlocks(so2, so);
854 	/*
855 	 * Only now return peer's address, as we may need to
856 	 * block in order to allocate memory.
857 	 *
858 	 * XXX Minor race: connection can be broken while
859 	 * lock is dropped in unp_setaddr().  We will return
860 	 * error == 0 and sun_noname as the peer address.
861 	 */
862 	unp_setaddr(so, nam, true);
863 	/* so_lock now points to unp_streamlock */
864 	mutex_exit(so2->so_lock);
865 	return 0;
866 }
867 
868 static int
869 unp_ioctl(struct socket *so, u_long cmd, void *nam, struct ifnet *ifp)
870 {
871 	return EOPNOTSUPP;
872 }
873 
874 static int
875 unp_stat(struct socket *so, struct stat *ub)
876 {
877 	struct unpcb *unp;
878 	struct socket *so2;
879 
880 	KASSERT(solocked(so));
881 
882 	unp = sotounpcb(so);
883 	if (unp == NULL)
884 		return EINVAL;
885 
886 	ub->st_blksize = so->so_snd.sb_hiwat;
887 	switch (so->so_type) {
888 	case SOCK_SEQPACKET: /* FALLTHROUGH */
889 	case SOCK_STREAM:
890 		if (unp->unp_conn == 0)
891 			break;
892 
893 		so2 = unp->unp_conn->unp_socket;
894 		KASSERT(solocked2(so, so2));
895 		ub->st_blksize += so2->so_rcv.sb_cc;
896 		break;
897 	default:
898 		break;
899 	}
900 	ub->st_dev = NODEV;
901 	if (unp->unp_ino == 0)
902 		unp->unp_ino = unp_ino++;
903 	ub->st_atimespec = ub->st_mtimespec = ub->st_ctimespec = unp->unp_ctime;
904 	ub->st_ino = unp->unp_ino;
905 	ub->st_uid = so->so_uidinfo->ui_uid;
906 	ub->st_gid = so->so_egid;
907 	return (0);
908 }
909 
910 static int
911 unp_peeraddr(struct socket *so, struct sockaddr *nam)
912 {
913 	KASSERT(solocked(so));
914 	KASSERT(sotounpcb(so) != NULL);
915 	KASSERT(nam != NULL);
916 
917 	unp_setaddr(so, nam, true);
918 	return 0;
919 }
920 
921 static int
922 unp_sockaddr(struct socket *so, struct sockaddr *nam)
923 {
924 	KASSERT(solocked(so));
925 	KASSERT(sotounpcb(so) != NULL);
926 	KASSERT(nam != NULL);
927 
928 	unp_setaddr(so, nam, false);
929 	return 0;
930 }
931 
932 /*
933  * we only need to perform this allocation until syscalls other than
934  * bind are adjusted to use sockaddr_big.
935  */
936 static struct sockaddr_un *
937 makeun_sb(struct sockaddr *nam, size_t *addrlen)
938 {
939 	struct sockaddr_un *sun;
940 
941 	*addrlen = nam->sa_len + 1;
942 	sun = malloc(*addrlen, M_SONAME, M_WAITOK);
943 	memcpy(sun, nam, nam->sa_len);
944 	*(((char *)sun) + nam->sa_len) = '\0';
945 	return sun;
946 }
947 
948 static int
949 unp_bind(struct socket *so, struct sockaddr *nam, struct lwp *l)
950 {
951 	struct sockaddr_un *sun;
952 	struct unpcb *unp;
953 	vnode_t *vp;
954 	struct vattr vattr;
955 	size_t addrlen;
956 	int error;
957 	struct pathbuf *pb;
958 	struct nameidata nd;
959 	proc_t *p;
960 
961 	unp = sotounpcb(so);
962 
963 	KASSERT(solocked(so));
964 	KASSERT(unp != NULL);
965 	KASSERT(nam != NULL);
966 
967 	if (unp->unp_vnode != NULL)
968 		return (EINVAL);
969 	if ((unp->unp_flags & UNP_BUSY) != 0) {
970 		/*
971 		 * EALREADY may not be strictly accurate, but since this
972 		 * is a major application error it's hardly a big deal.
973 		 */
974 		return (EALREADY);
975 	}
976 	unp->unp_flags |= UNP_BUSY;
977 	sounlock(so);
978 
979 	p = l->l_proc;
980 	sun = makeun_sb(nam, &addrlen);
981 
982 	pb = pathbuf_create(sun->sun_path);
983 	if (pb == NULL) {
984 		error = ENOMEM;
985 		goto bad;
986 	}
987 	NDINIT(&nd, CREATE, FOLLOW | LOCKPARENT | TRYEMULROOT, pb);
988 
989 /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */
990 	if ((error = namei(&nd)) != 0) {
991 		pathbuf_destroy(pb);
992 		goto bad;
993 	}
994 	vp = nd.ni_vp;
995 	if (vp != NULL) {
996 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
997 		if (nd.ni_dvp == vp)
998 			vrele(nd.ni_dvp);
999 		else
1000 			vput(nd.ni_dvp);
1001 		vrele(vp);
1002 		pathbuf_destroy(pb);
1003 		error = EADDRINUSE;
1004 		goto bad;
1005 	}
1006 	vattr_null(&vattr);
1007 	vattr.va_type = VSOCK;
1008 	vattr.va_mode = ACCESSPERMS & ~(p->p_cwdi->cwdi_cmask);
1009 	error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1010 	if (error) {
1011 		vput(nd.ni_dvp);
1012 		pathbuf_destroy(pb);
1013 		goto bad;
1014 	}
1015 	vp = nd.ni_vp;
1016 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1017 	solock(so);
1018 	vp->v_socket = unp->unp_socket;
1019 	unp->unp_vnode = vp;
1020 	unp->unp_addrlen = addrlen;
1021 	unp->unp_addr = sun;
1022 	VOP_UNLOCK(vp);
1023 	vput(nd.ni_dvp);
1024 	unp->unp_flags &= ~UNP_BUSY;
1025 	pathbuf_destroy(pb);
1026 	return (0);
1027 
1028  bad:
1029 	free(sun, M_SONAME);
1030 	solock(so);
1031 	unp->unp_flags &= ~UNP_BUSY;
1032 	return (error);
1033 }
1034 
1035 static int
1036 unp_listen(struct socket *so, struct lwp *l)
1037 {
1038 	struct unpcb *unp = sotounpcb(so);
1039 
1040 	KASSERT(solocked(so));
1041 	KASSERT(unp != NULL);
1042 
1043 	/*
1044 	 * If the socket can accept a connection, it must be
1045 	 * locked by uipc_lock.
1046 	 */
1047 	unp_resetlock(so);
1048 	if (unp->unp_vnode == NULL)
1049 		return EINVAL;
1050 
1051 	unp_connid(l, unp, UNP_EIDSBIND);
1052 	return 0;
1053 }
1054 
1055 static int
1056 unp_disconnect(struct socket *so)
1057 {
1058 	KASSERT(solocked(so));
1059 	KASSERT(sotounpcb(so) != NULL);
1060 
1061 	unp_disconnect1(sotounpcb(so));
1062 	return 0;
1063 }
1064 
1065 static int
1066 unp_shutdown(struct socket *so)
1067 {
1068 	KASSERT(solocked(so));
1069 	KASSERT(sotounpcb(so) != NULL);
1070 
1071 	socantsendmore(so);
1072 	unp_shutdown1(sotounpcb(so));
1073 	return 0;
1074 }
1075 
1076 static int
1077 unp_abort(struct socket *so)
1078 {
1079 	KASSERT(solocked(so));
1080 	KASSERT(sotounpcb(so) != NULL);
1081 
1082 	(void)unp_drop(sotounpcb(so), ECONNABORTED);
1083 	KASSERT(so->so_head == NULL);
1084 	KASSERT(so->so_pcb != NULL);
1085 	unp_detach(so);
1086 	return 0;
1087 }
1088 
1089 static int
1090 unp_connect1(struct socket *so, struct socket *so2, struct lwp *l)
1091 {
1092 	struct unpcb *unp = sotounpcb(so);
1093 	struct unpcb *unp2;
1094 
1095 	if (so2->so_type != so->so_type)
1096 		return EPROTOTYPE;
1097 
1098 	/*
1099 	 * All three sockets involved must be locked by same lock:
1100 	 *
1101 	 * local endpoint (so)
1102 	 * remote endpoint (so2)
1103 	 * queue head (so2->so_head, only if PR_CONNREQUIRED)
1104 	 */
1105 	KASSERT(solocked2(so, so2));
1106 	KASSERT(so->so_head == NULL);
1107 	if (so2->so_head != NULL) {
1108 		KASSERT(so2->so_lock == uipc_lock);
1109 		KASSERT(solocked2(so2, so2->so_head));
1110 	}
1111 
1112 	unp2 = sotounpcb(so2);
1113 	unp->unp_conn = unp2;
1114 
1115 	switch (so->so_type) {
1116 
1117 	case SOCK_DGRAM:
1118 		unp->unp_nextref = unp2->unp_refs;
1119 		unp2->unp_refs = unp;
1120 		soisconnected(so);
1121 		break;
1122 
1123 	case SOCK_SEQPACKET: /* FALLTHROUGH */
1124 	case SOCK_STREAM:
1125 
1126 		/*
1127 		 * SOCK_SEQPACKET and SOCK_STREAM cases are handled by callers
1128 		 * which are unp_connect() or unp_connect2().
1129 		 */
1130 
1131 		break;
1132 
1133 	default:
1134 		panic("unp_connect1");
1135 	}
1136 
1137 	return 0;
1138 }
1139 
1140 int
1141 unp_connect(struct socket *so, struct sockaddr *nam, struct lwp *l)
1142 {
1143 	struct sockaddr_un *sun;
1144 	vnode_t *vp;
1145 	struct socket *so2, *so3;
1146 	struct unpcb *unp, *unp2, *unp3;
1147 	size_t addrlen;
1148 	int error;
1149 	struct pathbuf *pb;
1150 	struct nameidata nd;
1151 
1152 	unp = sotounpcb(so);
1153 	if ((unp->unp_flags & UNP_BUSY) != 0) {
1154 		/*
1155 		 * EALREADY may not be strictly accurate, but since this
1156 		 * is a major application error it's hardly a big deal.
1157 		 */
1158 		return (EALREADY);
1159 	}
1160 	unp->unp_flags |= UNP_BUSY;
1161 	sounlock(so);
1162 
1163 	sun = makeun_sb(nam, &addrlen);
1164 	pb = pathbuf_create(sun->sun_path);
1165 	if (pb == NULL) {
1166 		error = ENOMEM;
1167 		goto bad2;
1168 	}
1169 
1170 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, pb);
1171 
1172 	if ((error = namei(&nd)) != 0) {
1173 		pathbuf_destroy(pb);
1174 		goto bad2;
1175 	}
1176 	vp = nd.ni_vp;
1177 	pathbuf_destroy(pb);
1178 	if (vp->v_type != VSOCK) {
1179 		error = ENOTSOCK;
1180 		goto bad;
1181 	}
1182 	if ((error = VOP_ACCESS(vp, VWRITE, l->l_cred)) != 0)
1183 		goto bad;
1184 	/* Acquire v_interlock to protect against unp_detach(). */
1185 	mutex_enter(vp->v_interlock);
1186 	so2 = vp->v_socket;
1187 	if (so2 == NULL) {
1188 		mutex_exit(vp->v_interlock);
1189 		error = ECONNREFUSED;
1190 		goto bad;
1191 	}
1192 	if (so->so_type != so2->so_type) {
1193 		mutex_exit(vp->v_interlock);
1194 		error = EPROTOTYPE;
1195 		goto bad;
1196 	}
1197 	solock(so);
1198 	unp_resetlock(so);
1199 	mutex_exit(vp->v_interlock);
1200 	if ((so->so_proto->pr_flags & PR_CONNREQUIRED) != 0) {
1201 		/*
1202 		 * This may seem somewhat fragile but is OK: if we can
1203 		 * see SO_ACCEPTCONN set on the endpoint, then it must
1204 		 * be locked by the domain-wide uipc_lock.
1205 		 */
1206 		KASSERT((so2->so_options & SO_ACCEPTCONN) == 0 ||
1207 		    so2->so_lock == uipc_lock);
1208 		if ((so2->so_options & SO_ACCEPTCONN) == 0 ||
1209 		    (so3 = sonewconn(so2, false)) == NULL) {
1210 			error = ECONNREFUSED;
1211 			sounlock(so);
1212 			goto bad;
1213 		}
1214 		unp2 = sotounpcb(so2);
1215 		unp3 = sotounpcb(so3);
1216 		if (unp2->unp_addr) {
1217 			unp3->unp_addr = malloc(unp2->unp_addrlen,
1218 			    M_SONAME, M_WAITOK);
1219 			memcpy(unp3->unp_addr, unp2->unp_addr,
1220 			    unp2->unp_addrlen);
1221 			unp3->unp_addrlen = unp2->unp_addrlen;
1222 		}
1223 		unp3->unp_flags = unp2->unp_flags;
1224 		so2 = so3;
1225 		/*
1226 		 * The connector's (client's) credentials are copied from its
1227 		 * process structure at the time of connect() (which is now).
1228 		 */
1229 		unp_connid(l, unp3, UNP_EIDSVALID);
1230 		 /*
1231 		  * The receiver's (server's) credentials are copied from the
1232 		  * unp_peercred member of socket on which the former called
1233 		  * listen(); unp_listen() cached that process's credentials
1234 		  * at that time so we can use them now.
1235 		  */
1236 		if (unp2->unp_flags & UNP_EIDSBIND) {
1237 			memcpy(&unp->unp_connid, &unp2->unp_connid,
1238 			    sizeof(unp->unp_connid));
1239 			unp->unp_flags |= UNP_EIDSVALID;
1240 		}
1241 	}
1242 	error = unp_connect1(so, so2, l);
1243 	if (error) {
1244 		sounlock(so);
1245 		goto bad;
1246 	}
1247 	unp2 = sotounpcb(so2);
1248 	switch (so->so_type) {
1249 
1250 	/*
1251 	 * SOCK_DGRAM and default cases are handled in prior call to
1252 	 * unp_connect1(), do not add a default case without fixing
1253 	 * unp_connect1().
1254 	 */
1255 
1256 	case SOCK_SEQPACKET: /* FALLTHROUGH */
1257 	case SOCK_STREAM:
1258 		unp2->unp_conn = unp;
1259 		if ((unp->unp_flags | unp2->unp_flags) & UNP_CONNWAIT)
1260 			soisconnecting(so);
1261 		else
1262 			soisconnected(so);
1263 		soisconnected(so2);
1264 		/*
1265 		 * If the connection is fully established, break the
1266 		 * association with uipc_lock and give the connected
1267 		 * pair a separate lock to share.
1268 		 */
1269 		KASSERT(so2->so_head != NULL);
1270 		unp_setpeerlocks(so, so2);
1271 		break;
1272 
1273 	}
1274 	sounlock(so);
1275  bad:
1276 	vput(vp);
1277  bad2:
1278 	free(sun, M_SONAME);
1279 	solock(so);
1280 	unp->unp_flags &= ~UNP_BUSY;
1281 	return (error);
1282 }
1283 
1284 int
1285 unp_connect2(struct socket *so, struct socket *so2)
1286 {
1287 	struct unpcb *unp = sotounpcb(so);
1288 	struct unpcb *unp2;
1289 	int error = 0;
1290 
1291 	KASSERT(solocked2(so, so2));
1292 
1293 	error = unp_connect1(so, so2, curlwp);
1294 	if (error)
1295 		return error;
1296 
1297 	unp2 = sotounpcb(so2);
1298 	switch (so->so_type) {
1299 
1300 	/*
1301 	 * SOCK_DGRAM and default cases are handled in prior call to
1302 	 * unp_connect1(), do not add a default case without fixing
1303 	 * unp_connect1().
1304 	 */
1305 
1306 	case SOCK_SEQPACKET: /* FALLTHROUGH */
1307 	case SOCK_STREAM:
1308 		unp2->unp_conn = unp;
1309 		soisconnected(so);
1310 		soisconnected(so2);
1311 		break;
1312 
1313 	}
1314 	return error;
1315 }
1316 
1317 static void
1318 unp_disconnect1(struct unpcb *unp)
1319 {
1320 	struct unpcb *unp2 = unp->unp_conn;
1321 	struct socket *so;
1322 
1323 	if (unp2 == 0)
1324 		return;
1325 	unp->unp_conn = 0;
1326 	so = unp->unp_socket;
1327 	switch (so->so_type) {
1328 	case SOCK_DGRAM:
1329 		if (unp2->unp_refs == unp)
1330 			unp2->unp_refs = unp->unp_nextref;
1331 		else {
1332 			unp2 = unp2->unp_refs;
1333 			for (;;) {
1334 				KASSERT(solocked2(so, unp2->unp_socket));
1335 				if (unp2 == 0)
1336 					panic("unp_disconnect1");
1337 				if (unp2->unp_nextref == unp)
1338 					break;
1339 				unp2 = unp2->unp_nextref;
1340 			}
1341 			unp2->unp_nextref = unp->unp_nextref;
1342 		}
1343 		unp->unp_nextref = 0;
1344 		so->so_state &= ~SS_ISCONNECTED;
1345 		break;
1346 
1347 	case SOCK_SEQPACKET: /* FALLTHROUGH */
1348 	case SOCK_STREAM:
1349 		KASSERT(solocked2(so, unp2->unp_socket));
1350 		soisdisconnected(so);
1351 		unp2->unp_conn = 0;
1352 		soisdisconnected(unp2->unp_socket);
1353 		break;
1354 	}
1355 }
1356 
1357 static void
1358 unp_shutdown1(struct unpcb *unp)
1359 {
1360 	struct socket *so;
1361 
1362 	switch(unp->unp_socket->so_type) {
1363 	case SOCK_SEQPACKET: /* FALLTHROUGH */
1364 	case SOCK_STREAM:
1365 		if (unp->unp_conn && (so = unp->unp_conn->unp_socket))
1366 			socantrcvmore(so);
1367 		break;
1368 	default:
1369 		break;
1370 	}
1371 }
1372 
1373 static bool
1374 unp_drop(struct unpcb *unp, int errno)
1375 {
1376 	struct socket *so = unp->unp_socket;
1377 
1378 	KASSERT(solocked(so));
1379 
1380 	so->so_error = errno;
1381 	unp_disconnect1(unp);
1382 	if (so->so_head) {
1383 		so->so_pcb = NULL;
1384 		/* sofree() drops the socket lock */
1385 		sofree(so);
1386 		unp_free(unp);
1387 		return true;
1388 	}
1389 	return false;
1390 }
1391 
1392 #ifdef notdef
1393 unp_drain(void)
1394 {
1395 
1396 }
1397 #endif
1398 
1399 int
1400 unp_externalize(struct mbuf *rights, struct lwp *l, int flags)
1401 {
1402 	struct cmsghdr * const cm = mtod(rights, struct cmsghdr *);
1403 	struct proc * const p = l->l_proc;
1404 	file_t **rp;
1405 	int error = 0;
1406 
1407 	const size_t nfds = (cm->cmsg_len - CMSG_ALIGN(sizeof(*cm))) /
1408 	    sizeof(file_t *);
1409 	if (nfds == 0)
1410 		goto noop;
1411 
1412 	int * const fdp = kmem_alloc(nfds * sizeof(int), KM_SLEEP);
1413 	rw_enter(&p->p_cwdi->cwdi_lock, RW_READER);
1414 
1415 	/* Make sure the recipient should be able to see the files.. */
1416 	rp = (file_t **)CMSG_DATA(cm);
1417 	for (size_t i = 0; i < nfds; i++) {
1418 		file_t * const fp = *rp++;
1419 		if (fp == NULL) {
1420 			error = EINVAL;
1421 			goto out;
1422 		}
1423 		/*
1424 		 * If we are in a chroot'ed directory, and
1425 		 * someone wants to pass us a directory, make
1426 		 * sure it's inside the subtree we're allowed
1427 		 * to access.
1428 		 */
1429 		if (p->p_cwdi->cwdi_rdir != NULL && fp->f_type == DTYPE_VNODE) {
1430 			vnode_t *vp = fp->f_vnode;
1431 			if ((vp->v_type == VDIR) &&
1432 			    !vn_isunder(vp, p->p_cwdi->cwdi_rdir, l)) {
1433 				error = EPERM;
1434 				goto out;
1435 			}
1436 		}
1437 	}
1438 
1439  restart:
1440 	/*
1441 	 * First loop -- allocate file descriptor table slots for the
1442 	 * new files.
1443 	 */
1444 	for (size_t i = 0; i < nfds; i++) {
1445 		if ((error = fd_alloc(p, 0, &fdp[i])) != 0) {
1446 			/*
1447 			 * Back out what we've done so far.
1448 			 */
1449 			while (i-- > 0) {
1450 				fd_abort(p, NULL, fdp[i]);
1451 			}
1452 			if (error == ENOSPC) {
1453 				fd_tryexpand(p);
1454 				error = 0;
1455 				goto restart;
1456 			}
1457 			/*
1458 			 * This is the error that has historically
1459 			 * been returned, and some callers may
1460 			 * expect it.
1461 			 */
1462 			error = EMSGSIZE;
1463 			goto out;
1464 		}
1465 	}
1466 
1467 	/*
1468 	 * Now that adding them has succeeded, update all of the
1469 	 * file passing state and affix the descriptors.
1470 	 */
1471 	rp = (file_t **)CMSG_DATA(cm);
1472 	int *ofdp = (int *)CMSG_DATA(cm);
1473 	for (size_t i = 0; i < nfds; i++) {
1474 		file_t * const fp = *rp++;
1475 		const int fd = fdp[i];
1476 		atomic_dec_uint(&unp_rights);
1477 		fd_set_exclose(l, fd, (flags & O_CLOEXEC) != 0);
1478 		fd_affix(p, fp, fd);
1479 		/*
1480 		 * Done with this file pointer, replace it with a fd;
1481 		 */
1482 		*ofdp++ = fd;
1483 		mutex_enter(&fp->f_lock);
1484 		fp->f_msgcount--;
1485 		mutex_exit(&fp->f_lock);
1486 		/*
1487 		 * Note that fd_affix() adds a reference to the file.
1488 		 * The file may already have been closed by another
1489 		 * LWP in the process, so we must drop the reference
1490 		 * added by unp_internalize() with closef().
1491 		 */
1492 		closef(fp);
1493 	}
1494 
1495 	/*
1496 	 * Adjust length, in case of transition from large file_t
1497 	 * pointers to ints.
1498 	 */
1499 	if (sizeof(file_t *) != sizeof(int)) {
1500 		cm->cmsg_len = CMSG_LEN(nfds * sizeof(int));
1501 		rights->m_len = CMSG_SPACE(nfds * sizeof(int));
1502 	}
1503  out:
1504 	if (__predict_false(error != 0)) {
1505 		file_t **const fpp = (file_t **)CMSG_DATA(cm);
1506 		for (size_t i = 0; i < nfds; i++)
1507 			unp_discard_now(fpp[i]);
1508 		/*
1509 		 * Truncate the array so that nobody will try to interpret
1510 		 * what is now garbage in it.
1511 		 */
1512 		cm->cmsg_len = CMSG_LEN(0);
1513 		rights->m_len = CMSG_SPACE(0);
1514 	}
1515 	rw_exit(&p->p_cwdi->cwdi_lock);
1516 	kmem_free(fdp, nfds * sizeof(int));
1517 
1518  noop:
1519 	/*
1520 	 * Don't disclose kernel memory in the alignment space.
1521 	 */
1522 	KASSERT(cm->cmsg_len <= rights->m_len);
1523 	memset(&mtod(rights, char *)[cm->cmsg_len], 0, rights->m_len -
1524 	    cm->cmsg_len);
1525 	return error;
1526 }
1527 
1528 static int
1529 unp_internalize(struct mbuf **controlp)
1530 {
1531 	filedesc_t *fdescp = curlwp->l_fd;
1532 	fdtab_t *dt;
1533 	struct mbuf *control = *controlp;
1534 	struct cmsghdr *newcm, *cm = mtod(control, struct cmsghdr *);
1535 	file_t **rp, **files;
1536 	file_t *fp;
1537 	int i, fd, *fdp;
1538 	int nfds, error;
1539 	u_int maxmsg;
1540 
1541 	error = 0;
1542 	newcm = NULL;
1543 
1544 	/* Sanity check the control message header. */
1545 	if (cm->cmsg_type != SCM_RIGHTS || cm->cmsg_level != SOL_SOCKET ||
1546 	    cm->cmsg_len > control->m_len ||
1547 	    cm->cmsg_len < CMSG_ALIGN(sizeof(*cm)))
1548 		return (EINVAL);
1549 
1550 	/*
1551 	 * Verify that the file descriptors are valid, and acquire
1552 	 * a reference to each.
1553 	 */
1554 	nfds = (cm->cmsg_len - CMSG_ALIGN(sizeof(*cm))) / sizeof(int);
1555 	fdp = (int *)CMSG_DATA(cm);
1556 	maxmsg = maxfiles / unp_rights_ratio;
1557 	for (i = 0; i < nfds; i++) {
1558 		fd = *fdp++;
1559 		if (atomic_inc_uint_nv(&unp_rights) > maxmsg) {
1560 			atomic_dec_uint(&unp_rights);
1561 			nfds = i;
1562 			error = EAGAIN;
1563 			goto out;
1564 		}
1565 		if ((fp = fd_getfile(fd)) == NULL
1566 		    || fp->f_type == DTYPE_KQUEUE) {
1567 		    	if (fp)
1568 		    		fd_putfile(fd);
1569 			atomic_dec_uint(&unp_rights);
1570 			nfds = i;
1571 			error = EBADF;
1572 			goto out;
1573 		}
1574 	}
1575 
1576 	/* Allocate new space and copy header into it. */
1577 	newcm = malloc(CMSG_SPACE(nfds * sizeof(file_t *)), M_MBUF, M_WAITOK);
1578 	if (newcm == NULL) {
1579 		error = E2BIG;
1580 		goto out;
1581 	}
1582 	memcpy(newcm, cm, sizeof(struct cmsghdr));
1583 	memset(newcm + 1, 0, CMSG_LEN(0) - sizeof(struct cmsghdr));
1584 	files = (file_t **)CMSG_DATA(newcm);
1585 
1586 	/*
1587 	 * Transform the file descriptors into file_t pointers, in
1588 	 * reverse order so that if pointers are bigger than ints, the
1589 	 * int won't get until we're done.  No need to lock, as we have
1590 	 * already validated the descriptors with fd_getfile().
1591 	 */
1592 	fdp = (int *)CMSG_DATA(cm) + nfds;
1593 	rp = files + nfds;
1594 	for (i = 0; i < nfds; i++) {
1595 		dt = atomic_load_consume(&fdescp->fd_dt);
1596 		fp = atomic_load_consume(&dt->dt_ff[*--fdp]->ff_file);
1597 		KASSERT(fp != NULL);
1598 		mutex_enter(&fp->f_lock);
1599 		*--rp = fp;
1600 		fp->f_count++;
1601 		fp->f_msgcount++;
1602 		mutex_exit(&fp->f_lock);
1603 	}
1604 
1605  out:
1606  	/* Release descriptor references. */
1607 	fdp = (int *)CMSG_DATA(cm);
1608 	for (i = 0; i < nfds; i++) {
1609 		fd_putfile(*fdp++);
1610 		if (error != 0) {
1611 			atomic_dec_uint(&unp_rights);
1612 		}
1613 	}
1614 
1615 	if (error == 0) {
1616 		if (control->m_flags & M_EXT) {
1617 			m_freem(control);
1618 			*controlp = control = m_get(M_WAIT, MT_CONTROL);
1619 		}
1620 		MEXTADD(control, newcm, CMSG_SPACE(nfds * sizeof(file_t *)),
1621 		    M_MBUF, NULL, NULL);
1622 		cm = newcm;
1623 		/*
1624 		 * Adjust message & mbuf to note amount of space
1625 		 * actually used.
1626 		 */
1627 		cm->cmsg_len = CMSG_LEN(nfds * sizeof(file_t *));
1628 		control->m_len = CMSG_SPACE(nfds * sizeof(file_t *));
1629 	}
1630 
1631 	return error;
1632 }
1633 
1634 struct mbuf *
1635 unp_addsockcred(struct lwp *l, struct mbuf *control)
1636 {
1637 	struct sockcred *sc;
1638 	struct mbuf *m;
1639 	void *p;
1640 
1641 	m = sbcreatecontrol1(&p, SOCKCREDSIZE(kauth_cred_ngroups(l->l_cred)),
1642 		SCM_CREDS, SOL_SOCKET, M_WAITOK);
1643 	if (m == NULL)
1644 		return control;
1645 
1646 	sc = p;
1647 	sc->sc_pid = l->l_proc->p_pid;
1648 	sc->sc_uid = kauth_cred_getuid(l->l_cred);
1649 	sc->sc_euid = kauth_cred_geteuid(l->l_cred);
1650 	sc->sc_gid = kauth_cred_getgid(l->l_cred);
1651 	sc->sc_egid = kauth_cred_getegid(l->l_cred);
1652 	sc->sc_ngroups = kauth_cred_ngroups(l->l_cred);
1653 
1654 	for (int i = 0; i < sc->sc_ngroups; i++)
1655 		sc->sc_groups[i] = kauth_cred_group(l->l_cred, i);
1656 
1657 	return m_add(control, m);
1658 }
1659 
1660 /*
1661  * Do a mark-sweep GC of files in the system, to free up any which are
1662  * caught in flight to an about-to-be-closed socket.  Additionally,
1663  * process deferred file closures.
1664  */
1665 static void
1666 unp_gc(file_t *dp)
1667 {
1668 	extern	struct domain unixdomain;
1669 	file_t *fp, *np;
1670 	struct socket *so, *so1;
1671 	u_int i, oflags, rflags;
1672 	bool didwork;
1673 
1674 	KASSERT(curlwp == unp_thread_lwp);
1675 	KASSERT(mutex_owned(&filelist_lock));
1676 
1677 	/*
1678 	 * First, process deferred file closures.
1679 	 */
1680 	while (!SLIST_EMPTY(&unp_thread_discard)) {
1681 		fp = SLIST_FIRST(&unp_thread_discard);
1682 		KASSERT(fp->f_unpcount > 0);
1683 		KASSERT(fp->f_count > 0);
1684 		KASSERT(fp->f_msgcount > 0);
1685 		KASSERT(fp->f_count >= fp->f_unpcount);
1686 		KASSERT(fp->f_count >= fp->f_msgcount);
1687 		KASSERT(fp->f_msgcount >= fp->f_unpcount);
1688 		SLIST_REMOVE_HEAD(&unp_thread_discard, f_unplist);
1689 		i = fp->f_unpcount;
1690 		fp->f_unpcount = 0;
1691 		mutex_exit(&filelist_lock);
1692 		for (; i != 0; i--) {
1693 			unp_discard_now(fp);
1694 		}
1695 		mutex_enter(&filelist_lock);
1696 	}
1697 
1698 	/*
1699 	 * Clear mark bits.  Ensure that we don't consider new files
1700 	 * entering the file table during this loop (they will not have
1701 	 * FSCAN set).
1702 	 */
1703 	unp_defer = 0;
1704 	LIST_FOREACH(fp, &filehead, f_list) {
1705 		for (oflags = fp->f_flag;; oflags = rflags) {
1706 			rflags = atomic_cas_uint(&fp->f_flag, oflags,
1707 			    (oflags | FSCAN) & ~(FMARK|FDEFER));
1708 			if (__predict_true(oflags == rflags)) {
1709 				break;
1710 			}
1711 		}
1712 	}
1713 
1714 	/*
1715 	 * Iterate over the set of sockets, marking ones believed (based on
1716 	 * refcount) to be referenced from a process, and marking for rescan
1717 	 * sockets which are queued on a socket.  Recan continues descending
1718 	 * and searching for sockets referenced by sockets (FDEFER), until
1719 	 * there are no more socket->socket references to be discovered.
1720 	 */
1721 	do {
1722 		didwork = false;
1723 		for (fp = LIST_FIRST(&filehead); fp != NULL; fp = np) {
1724 			KASSERT(mutex_owned(&filelist_lock));
1725 			np = LIST_NEXT(fp, f_list);
1726 			mutex_enter(&fp->f_lock);
1727 			if ((fp->f_flag & FDEFER) != 0) {
1728 				atomic_and_uint(&fp->f_flag, ~FDEFER);
1729 				unp_defer--;
1730 				if (fp->f_count == 0) {
1731 					/*
1732 					 * XXX: closef() doesn't pay attention
1733 					 * to FDEFER
1734 					 */
1735 					mutex_exit(&fp->f_lock);
1736 					continue;
1737 				}
1738 			} else {
1739 				if (fp->f_count == 0 ||
1740 				    (fp->f_flag & FMARK) != 0 ||
1741 				    fp->f_count == fp->f_msgcount ||
1742 				    fp->f_unpcount != 0) {
1743 					mutex_exit(&fp->f_lock);
1744 					continue;
1745 				}
1746 			}
1747 			atomic_or_uint(&fp->f_flag, FMARK);
1748 
1749 			if (fp->f_type != DTYPE_SOCKET ||
1750 			    (so = fp->f_socket) == NULL ||
1751 			    so->so_proto->pr_domain != &unixdomain ||
1752 			    (so->so_proto->pr_flags & PR_RIGHTS) == 0) {
1753 				mutex_exit(&fp->f_lock);
1754 				continue;
1755 			}
1756 
1757 			/* Gain file ref, mark our position, and unlock. */
1758 			didwork = true;
1759 			LIST_INSERT_AFTER(fp, dp, f_list);
1760 			fp->f_count++;
1761 			mutex_exit(&fp->f_lock);
1762 			mutex_exit(&filelist_lock);
1763 
1764 			/*
1765 			 * Mark files referenced from sockets queued on the
1766 			 * accept queue as well.
1767 			 */
1768 			solock(so);
1769 			unp_scan(so->so_rcv.sb_mb, unp_mark, 0);
1770 			if ((so->so_options & SO_ACCEPTCONN) != 0) {
1771 				TAILQ_FOREACH(so1, &so->so_q0, so_qe) {
1772 					unp_scan(so1->so_rcv.sb_mb, unp_mark, 0);
1773 				}
1774 				TAILQ_FOREACH(so1, &so->so_q, so_qe) {
1775 					unp_scan(so1->so_rcv.sb_mb, unp_mark, 0);
1776 				}
1777 			}
1778 			sounlock(so);
1779 
1780 			/* Re-lock and restart from where we left off. */
1781 			closef(fp);
1782 			mutex_enter(&filelist_lock);
1783 			np = LIST_NEXT(dp, f_list);
1784 			LIST_REMOVE(dp, f_list);
1785 		}
1786 		/*
1787 		 * Bail early if we did nothing in the loop above.  Could
1788 		 * happen because of concurrent activity causing unp_defer
1789 		 * to get out of sync.
1790 		 */
1791 	} while (unp_defer != 0 && didwork);
1792 
1793 	/*
1794 	 * Sweep pass.
1795 	 *
1796 	 * We grab an extra reference to each of the files that are
1797 	 * not otherwise accessible and then free the rights that are
1798 	 * stored in messages on them.
1799 	 */
1800 	for (fp = LIST_FIRST(&filehead); fp != NULL; fp = np) {
1801 		KASSERT(mutex_owned(&filelist_lock));
1802 		np = LIST_NEXT(fp, f_list);
1803 		mutex_enter(&fp->f_lock);
1804 
1805 		/*
1806 		 * Ignore non-sockets.
1807 		 * Ignore dead sockets, or sockets with pending close.
1808 		 * Ignore sockets obviously referenced elsewhere.
1809 		 * Ignore sockets marked as referenced by our scan.
1810 		 * Ignore new sockets that did not exist during the scan.
1811 		 */
1812 		if (fp->f_type != DTYPE_SOCKET ||
1813 		    fp->f_count == 0 || fp->f_unpcount != 0 ||
1814 		    fp->f_count != fp->f_msgcount ||
1815 		    (fp->f_flag & (FMARK | FSCAN)) != FSCAN) {
1816 			mutex_exit(&fp->f_lock);
1817 			continue;
1818 		}
1819 
1820 		/* Gain file ref, mark our position, and unlock. */
1821 		LIST_INSERT_AFTER(fp, dp, f_list);
1822 		fp->f_count++;
1823 		mutex_exit(&fp->f_lock);
1824 		mutex_exit(&filelist_lock);
1825 
1826 		/*
1827 		 * Flush all data from the socket's receive buffer.
1828 		 * This will cause files referenced only by the
1829 		 * socket to be queued for close.
1830 		 */
1831 		so = fp->f_socket;
1832 		solock(so);
1833 		sorflush(so);
1834 		sounlock(so);
1835 
1836 		/* Re-lock and restart from where we left off. */
1837 		closef(fp);
1838 		mutex_enter(&filelist_lock);
1839 		np = LIST_NEXT(dp, f_list);
1840 		LIST_REMOVE(dp, f_list);
1841 	}
1842 }
1843 
1844 /*
1845  * Garbage collector thread.  While SCM_RIGHTS messages are in transit,
1846  * wake once per second to garbage collect.  Run continually while we
1847  * have deferred closes to process.
1848  */
1849 static void
1850 unp_thread(void *cookie)
1851 {
1852 	file_t *dp;
1853 
1854 	/* Allocate a dummy file for our scans. */
1855 	if ((dp = fgetdummy()) == NULL) {
1856 		panic("unp_thread");
1857 	}
1858 
1859 	mutex_enter(&filelist_lock);
1860 	for (;;) {
1861 		KASSERT(mutex_owned(&filelist_lock));
1862 		if (SLIST_EMPTY(&unp_thread_discard)) {
1863 			if (unp_rights != 0) {
1864 				(void)cv_timedwait(&unp_thread_cv,
1865 				    &filelist_lock, hz);
1866 			} else {
1867 				cv_wait(&unp_thread_cv, &filelist_lock);
1868 			}
1869 		}
1870 		unp_gc(dp);
1871 	}
1872 	/* NOTREACHED */
1873 }
1874 
1875 /*
1876  * Kick the garbage collector into action if there is something for
1877  * it to process.
1878  */
1879 static void
1880 unp_thread_kick(void)
1881 {
1882 
1883 	if (!SLIST_EMPTY(&unp_thread_discard) || unp_rights != 0) {
1884 		mutex_enter(&filelist_lock);
1885 		cv_signal(&unp_thread_cv);
1886 		mutex_exit(&filelist_lock);
1887 	}
1888 }
1889 
1890 void
1891 unp_dispose(struct mbuf *m)
1892 {
1893 
1894 	if (m)
1895 		unp_scan(m, unp_discard_later, 1);
1896 }
1897 
1898 void
1899 unp_scan(struct mbuf *m0, void (*op)(file_t *), int discard)
1900 {
1901 	struct mbuf *m;
1902 	file_t **rp, *fp;
1903 	struct cmsghdr *cm;
1904 	int i, qfds;
1905 
1906 	while (m0) {
1907 		for (m = m0; m; m = m->m_next) {
1908 			if (m->m_type != MT_CONTROL ||
1909 			    m->m_len < sizeof(*cm)) {
1910 			    	continue;
1911 			}
1912 			cm = mtod(m, struct cmsghdr *);
1913 			if (cm->cmsg_level != SOL_SOCKET ||
1914 			    cm->cmsg_type != SCM_RIGHTS)
1915 				continue;
1916 			qfds = (cm->cmsg_len - CMSG_ALIGN(sizeof(*cm)))
1917 			    / sizeof(file_t *);
1918 			rp = (file_t **)CMSG_DATA(cm);
1919 			for (i = 0; i < qfds; i++) {
1920 				fp = *rp;
1921 				if (discard) {
1922 					*rp = 0;
1923 				}
1924 				(*op)(fp);
1925 				rp++;
1926 			}
1927 		}
1928 		m0 = m0->m_nextpkt;
1929 	}
1930 }
1931 
1932 void
1933 unp_mark(file_t *fp)
1934 {
1935 
1936 	if (fp == NULL)
1937 		return;
1938 
1939 	/* If we're already deferred, don't screw up the defer count */
1940 	mutex_enter(&fp->f_lock);
1941 	if (fp->f_flag & (FMARK | FDEFER)) {
1942 		mutex_exit(&fp->f_lock);
1943 		return;
1944 	}
1945 
1946 	/*
1947 	 * Minimize the number of deferrals...  Sockets are the only type of
1948 	 * file which can hold references to another file, so just mark
1949 	 * other files, and defer unmarked sockets for the next pass.
1950 	 */
1951 	if (fp->f_type == DTYPE_SOCKET) {
1952 		unp_defer++;
1953 		KASSERT(fp->f_count != 0);
1954 		atomic_or_uint(&fp->f_flag, FDEFER);
1955 	} else {
1956 		atomic_or_uint(&fp->f_flag, FMARK);
1957 	}
1958 	mutex_exit(&fp->f_lock);
1959 }
1960 
1961 static void
1962 unp_discard_now(file_t *fp)
1963 {
1964 
1965 	if (fp == NULL)
1966 		return;
1967 
1968 	KASSERT(fp->f_count > 0);
1969 	KASSERT(fp->f_msgcount > 0);
1970 
1971 	mutex_enter(&fp->f_lock);
1972 	fp->f_msgcount--;
1973 	mutex_exit(&fp->f_lock);
1974 	atomic_dec_uint(&unp_rights);
1975 	(void)closef(fp);
1976 }
1977 
1978 static void
1979 unp_discard_later(file_t *fp)
1980 {
1981 
1982 	if (fp == NULL)
1983 		return;
1984 
1985 	KASSERT(fp->f_count > 0);
1986 	KASSERT(fp->f_msgcount > 0);
1987 
1988 	mutex_enter(&filelist_lock);
1989 	if (fp->f_unpcount++ == 0) {
1990 		SLIST_INSERT_HEAD(&unp_thread_discard, fp, f_unplist);
1991 	}
1992 	mutex_exit(&filelist_lock);
1993 }
1994 
1995 static void
1996 unp_sysctl_create(void)
1997 {
1998 
1999 	KASSERT(usrreq_sysctllog == NULL);
2000 	sysctl_createv(&usrreq_sysctllog, 0, NULL, NULL,
2001 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2002 		       CTLTYPE_LONG, "sendspace",
2003 		       SYSCTL_DESCR("Default stream send space"),
2004 		       NULL, 0, &unpst_sendspace, 0,
2005 		       CTL_NET, PF_LOCAL, SOCK_STREAM, CTL_CREATE, CTL_EOL);
2006 	sysctl_createv(&usrreq_sysctllog, 0, NULL, NULL,
2007 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2008 		       CTLTYPE_LONG, "recvspace",
2009 		       SYSCTL_DESCR("Default stream recv space"),
2010 		       NULL, 0, &unpst_recvspace, 0,
2011 		       CTL_NET, PF_LOCAL, SOCK_STREAM, CTL_CREATE, CTL_EOL);
2012 	sysctl_createv(&usrreq_sysctllog, 0, NULL, NULL,
2013 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2014 		       CTLTYPE_LONG, "sendspace",
2015 		       SYSCTL_DESCR("Default datagram send space"),
2016 		       NULL, 0, &unpdg_sendspace, 0,
2017 		       CTL_NET, PF_LOCAL, SOCK_DGRAM, CTL_CREATE, CTL_EOL);
2018 	sysctl_createv(&usrreq_sysctllog, 0, NULL, NULL,
2019 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2020 		       CTLTYPE_LONG, "recvspace",
2021 		       SYSCTL_DESCR("Default datagram recv space"),
2022 		       NULL, 0, &unpdg_recvspace, 0,
2023 		       CTL_NET, PF_LOCAL, SOCK_DGRAM, CTL_CREATE, CTL_EOL);
2024 	sysctl_createv(&usrreq_sysctllog, 0, NULL, NULL,
2025 		       CTLFLAG_PERMANENT|CTLFLAG_READONLY,
2026 		       CTLTYPE_INT, "inflight",
2027 		       SYSCTL_DESCR("File descriptors in flight"),
2028 		       NULL, 0, &unp_rights, 0,
2029 		       CTL_NET, PF_LOCAL, CTL_CREATE, CTL_EOL);
2030 	sysctl_createv(&usrreq_sysctllog, 0, NULL, NULL,
2031 		       CTLFLAG_PERMANENT|CTLFLAG_READONLY,
2032 		       CTLTYPE_INT, "deferred",
2033 		       SYSCTL_DESCR("File descriptors deferred for close"),
2034 		       NULL, 0, &unp_defer, 0,
2035 		       CTL_NET, PF_LOCAL, CTL_CREATE, CTL_EOL);
2036 }
2037 
2038 const struct pr_usrreqs unp_usrreqs = {
2039 	.pr_attach	= unp_attach,
2040 	.pr_detach	= unp_detach,
2041 	.pr_accept	= unp_accept,
2042 	.pr_bind	= unp_bind,
2043 	.pr_listen	= unp_listen,
2044 	.pr_connect	= unp_connect,
2045 	.pr_connect2	= unp_connect2,
2046 	.pr_disconnect	= unp_disconnect,
2047 	.pr_shutdown	= unp_shutdown,
2048 	.pr_abort	= unp_abort,
2049 	.pr_ioctl	= unp_ioctl,
2050 	.pr_stat	= unp_stat,
2051 	.pr_peeraddr	= unp_peeraddr,
2052 	.pr_sockaddr	= unp_sockaddr,
2053 	.pr_rcvd	= unp_rcvd,
2054 	.pr_recvoob	= unp_recvoob,
2055 	.pr_send	= unp_send,
2056 	.pr_sendoob	= unp_sendoob,
2057 };
2058