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