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