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