xref: /netbsd-src/sys/kern/uipc_usrreq.c (revision 8ac07aec990b9d2e483062509d0a9fa5b4f57cf2)
1 /*	$NetBSD: uipc_usrreq.c,v 1.112 2008/04/24 11:38:36 ad Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998, 2000, 2004, 2008 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.
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  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the NetBSD
22  *	Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 /*
41  * Copyright (c) 1982, 1986, 1989, 1991, 1993
42  *	The Regents of the University of California.  All rights reserved.
43  *
44  * Redistribution and use in source and binary forms, with or without
45  * modification, are permitted provided that the following conditions
46  * are met:
47  * 1. Redistributions of source code must retain the above copyright
48  *    notice, this list of conditions and the following disclaimer.
49  * 2. Redistributions in binary form must reproduce the above copyright
50  *    notice, this list of conditions and the following disclaimer in the
51  *    documentation and/or other materials provided with the distribution.
52  * 3. Neither the name of the University nor the names of its contributors
53  *    may be used to endorse or promote products derived from this software
54  *    without specific prior written permission.
55  *
56  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
57  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
60  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66  * SUCH DAMAGE.
67  *
68  *	@(#)uipc_usrreq.c	8.9 (Berkeley) 5/14/95
69  */
70 
71 /*
72  * Copyright (c) 1997 Christopher G. Demetriou.  All rights reserved.
73  *
74  * Redistribution and use in source and binary forms, with or without
75  * modification, are permitted provided that the following conditions
76  * are met:
77  * 1. Redistributions of source code must retain the above copyright
78  *    notice, this list of conditions and the following disclaimer.
79  * 2. Redistributions in binary form must reproduce the above copyright
80  *    notice, this list of conditions and the following disclaimer in the
81  *    documentation and/or other materials provided with the distribution.
82  * 3. All advertising materials mentioning features or use of this software
83  *    must display the following acknowledgement:
84  *	This product includes software developed by the University of
85  *	California, Berkeley and its contributors.
86  * 4. Neither the name of the University nor the names of its contributors
87  *    may be used to endorse or promote products derived from this software
88  *    without specific prior written permission.
89  *
90  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
91  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
92  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
93  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
94  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
95  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
96  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
97  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
98  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
99  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
100  * SUCH DAMAGE.
101  *
102  *	@(#)uipc_usrreq.c	8.9 (Berkeley) 5/14/95
103  */
104 
105 #include <sys/cdefs.h>
106 __KERNEL_RCSID(0, "$NetBSD: uipc_usrreq.c,v 1.112 2008/04/24 11:38:36 ad Exp $");
107 
108 #include <sys/param.h>
109 #include <sys/systm.h>
110 #include <sys/proc.h>
111 #include <sys/filedesc.h>
112 #include <sys/domain.h>
113 #include <sys/protosw.h>
114 #include <sys/socket.h>
115 #include <sys/socketvar.h>
116 #include <sys/unpcb.h>
117 #include <sys/un.h>
118 #include <sys/namei.h>
119 #include <sys/vnode.h>
120 #include <sys/file.h>
121 #include <sys/stat.h>
122 #include <sys/mbuf.h>
123 #include <sys/kauth.h>
124 #include <sys/kmem.h>
125 #include <sys/atomic.h>
126 
127 /*
128  * Unix communications domain.
129  *
130  * TODO:
131  *	SEQPACKET, RDM
132  *	rethink name space problems
133  *	need a proper out-of-band
134  *
135  * Notes on locking:
136  *
137  * The generic rules noted in uipc_socket2.c apply.  In addition:
138  *
139  * o We have a global lock, uipc_lock.
140  *
141  * o All datagram sockets are locked by uipc_lock.
142  *
143  * o For stream socketpairs, the two endpoints are created sharing the same
144  *   independent lock.  Sockets presented to PRU_CONNECT2 must already have
145  *   matching locks.
146  *
147  * o Stream sockets created via socket() start life with their own
148  *   independent lock.
149  *
150  * o Stream connections to a named endpoint are slightly more complicated.
151  *   Sockets that have called listen() have their lock pointer mutated to
152  *   the global uipc_lock.  When establishing a connection, the connecting
153  *   socket also has its lock mutated to uipc_lock, which matches the head
154  *   (listening socket).  We create a new socket for accept() to return, and
155  *   that also shares the head's lock.  Until the connection is completely
156  *   done on both ends, all three sockets are locked by uipc_lock.  Once the
157  *   connection is complete, the association with the head's lock is broken.
158  *   The connecting socket and the socket returned from accept() have their
159  *   lock pointers mutated away from uipc_lock, and back to the connecting
160  *   socket's original, independent lock.  The head continues to be locked
161  *   by uipc_lock.
162  *
163  * o If uipc_lock is determined to be a significant source of contention,
164  *   it could easily be hashed out.  It is difficult to simply make it an
165  *   independent lock because of visibility / garbage collection issues:
166  *   if a socket has been associated with a lock at any point, that lock
167  *   must remain valid until the socket is no longer visible in the system.
168  *   The lock must not be freed or otherwise destroyed until any sockets
169  *   that had referenced it have also been destroyed.
170  */
171 const struct sockaddr_un sun_noname = {
172 	.sun_len = sizeof(sun_noname),
173 	.sun_family = AF_LOCAL,
174 };
175 ino_t	unp_ino;			/* prototype for fake inode numbers */
176 
177 struct mbuf *unp_addsockcred(struct lwp *, struct mbuf *);
178 static kmutex_t *uipc_lock;
179 
180 /*
181  * Initialize Unix protocols.
182  */
183 void
184 uipc_init(void)
185 {
186 
187 	uipc_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
188 }
189 
190 /*
191  * A connection succeeded: disassociate both endpoints from the head's
192  * lock, and make them share their own lock.  There is a race here: for
193  * a very brief time one endpoint will be locked by a different lock
194  * than the other end.  However, since the current thread holds the old
195  * lock (the listening socket's lock, the head) access can still only be
196  * made to one side of the connection.
197  */
198 static void
199 unp_setpeerlocks(struct socket *so, struct socket *so2)
200 {
201 	struct unpcb *unp;
202 	kmutex_t *lock;
203 
204 	KASSERT(solocked2(so, so2));
205 
206 	/*
207 	 * Bail out if either end of the socket is not yet fully
208 	 * connected or accepted.  We only break the lock association
209 	 * with the head when the pair of sockets stand completely
210 	 * on their own.
211 	 */
212 	if (so->so_head != NULL || so2->so_head != NULL)
213 		return;
214 
215 	/*
216 	 * Drop references to old lock.  A third reference (from the
217 	 * queue head) must be held as we still hold its lock.  Bonus:
218 	 * we don't need to worry about garbage collecting the lock.
219 	 */
220 	lock = so->so_lock;
221 	KASSERT(lock == uipc_lock);
222 	mutex_obj_free(lock);
223 	mutex_obj_free(lock);
224 
225 	/*
226 	 * Grab stream lock from the initiator and share between the two
227 	 * endpoints.  Issue memory barrier to ensure all modifications
228 	 * become globally visible before the lock change.  so2 is
229 	 * assumed not to have a stream lock, because it was created
230 	 * purely for the server side to accept this connection and
231 	 * started out life using the domain-wide lock.
232 	 */
233 	unp = sotounpcb(so);
234 	KASSERT(unp->unp_streamlock != NULL);
235 	KASSERT(sotounpcb(so2)->unp_streamlock == NULL);
236 	lock = unp->unp_streamlock;
237 	unp->unp_streamlock = NULL;
238 	mutex_obj_hold(lock);
239 	membar_exit();
240 	so->so_lock = lock;
241 	so2->so_lock = lock;
242 }
243 
244 /*
245  * Reset a socket's lock back to the domain-wide lock.
246  */
247 static void
248 unp_resetlock(struct socket *so)
249 {
250 	kmutex_t *olock, *nlock;
251 	struct unpcb *unp;
252 
253 	KASSERT(solocked(so));
254 
255 	olock = so->so_lock;
256 	nlock = uipc_lock;
257 	if (olock == nlock)
258 		return;
259 	unp = sotounpcb(so);
260 	KASSERT(unp->unp_streamlock == NULL);
261 	unp->unp_streamlock = olock;
262 	mutex_obj_hold(nlock);
263 	mutex_enter(nlock);
264 	so->so_lock = nlock;
265 	mutex_exit(olock);
266 }
267 
268 static void
269 unp_free(struct unpcb *unp)
270 {
271 
272 	if (unp->unp_addr)
273 		free(unp->unp_addr, M_SONAME);
274 	if (unp->unp_streamlock != NULL)
275 		mutex_obj_free(unp->unp_streamlock);
276 	free(unp, M_PCB);
277 }
278 
279 int
280 unp_output(struct mbuf *m, struct mbuf *control, struct unpcb *unp,
281 	struct lwp *l)
282 {
283 	struct socket *so2;
284 	const struct sockaddr_un *sun;
285 
286 	so2 = unp->unp_conn->unp_socket;
287 
288 	KASSERT(solocked(so2));
289 
290 	if (unp->unp_addr)
291 		sun = unp->unp_addr;
292 	else
293 		sun = &sun_noname;
294 	if (unp->unp_conn->unp_flags & UNP_WANTCRED)
295 		control = unp_addsockcred(l, control);
296 	if (sbappendaddr(&so2->so_rcv, (const struct sockaddr *)sun, m,
297 	    control) == 0) {
298 		so2->so_rcv.sb_overflowed++;
299 	    	sounlock(so2);
300 		unp_dispose(control);
301 		m_freem(control);
302 		m_freem(m);
303 	    	solock(so2);
304 		return (ENOBUFS);
305 	} else {
306 		sorwakeup(so2);
307 		return (0);
308 	}
309 }
310 
311 void
312 unp_setaddr(struct socket *so, struct mbuf *nam, bool peeraddr)
313 {
314 	const struct sockaddr_un *sun;
315 	struct unpcb *unp;
316 	bool ext;
317 
318 	unp = sotounpcb(so);
319 	ext = false;
320 
321 	for (;;) {
322 		sun = NULL;
323 		if (peeraddr) {
324 			if (unp->unp_conn && unp->unp_conn->unp_addr)
325 				sun = unp->unp_conn->unp_addr;
326 		} else {
327 			if (unp->unp_addr)
328 				sun = unp->unp_addr;
329 		}
330 		if (sun == NULL)
331 			sun = &sun_noname;
332 		nam->m_len = sun->sun_len;
333 		if (nam->m_len > MLEN && !ext) {
334 			sounlock(so);
335 			MEXTMALLOC(nam, MAXPATHLEN * 2, M_WAITOK);
336 			solock(so);
337 			ext = true;
338 		} else {
339 			KASSERT(nam->m_len <= MAXPATHLEN * 2);
340 			memcpy(mtod(nam, void *), sun, (size_t)nam->m_len);
341 			break;
342 		}
343 	}
344 }
345 
346 /*ARGSUSED*/
347 int
348 uipc_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
349 	struct mbuf *control, struct lwp *l)
350 {
351 	struct unpcb *unp = sotounpcb(so);
352 	struct socket *so2;
353 	struct proc *p;
354 	u_int newhiwat;
355 	int error = 0;
356 
357 	if (req == PRU_CONTROL)
358 		return (EOPNOTSUPP);
359 
360 #ifdef DIAGNOSTIC
361 	if (req != PRU_SEND && req != PRU_SENDOOB && control)
362 		panic("uipc_usrreq: unexpected control mbuf");
363 #endif
364 	p = l ? l->l_proc : NULL;
365 	if (req != PRU_ATTACH) {
366 		if (unp == 0) {
367 			error = EINVAL;
368 			goto release;
369 		}
370 		KASSERT(solocked(so));
371 	}
372 
373 	switch (req) {
374 
375 	case PRU_ATTACH:
376 		if (unp != 0) {
377 			error = EISCONN;
378 			break;
379 		}
380 		error = unp_attach(so);
381 		break;
382 
383 	case PRU_DETACH:
384 		unp_detach(unp);
385 		break;
386 
387 	case PRU_BIND:
388 		KASSERT(l != NULL);
389 		error = unp_bind(so, nam, l);
390 		break;
391 
392 	case PRU_LISTEN:
393 		/*
394 		 * If the socket can accept a connection, it must be
395 		 * locked by uipc_lock.
396 		 */
397 		unp_resetlock(so);
398 		if (unp->unp_vnode == 0)
399 			error = EINVAL;
400 		break;
401 
402 	case PRU_CONNECT:
403 		KASSERT(l != NULL);
404 		error = unp_connect(so, nam, l);
405 		break;
406 
407 	case PRU_CONNECT2:
408 		error = unp_connect2(so, (struct socket *)nam, PRU_CONNECT2);
409 		break;
410 
411 	case PRU_DISCONNECT:
412 		unp_disconnect(unp);
413 		break;
414 
415 	case PRU_ACCEPT:
416 		KASSERT(so->so_lock == uipc_lock);
417 		/*
418 		 * Mark the initiating STREAM socket as connected *ONLY*
419 		 * after it's been accepted.  This prevents a client from
420 		 * overrunning a server and receiving ECONNREFUSED.
421 		 */
422 		if (unp->unp_conn == NULL)
423 			break;
424 		so2 = unp->unp_conn->unp_socket;
425 		if (so2->so_state & SS_ISCONNECTING) {
426 			KASSERT(solocked2(so, so->so_head));
427 			KASSERT(solocked2(so2, so->so_head));
428 			soisconnected(so2);
429 		}
430 		/*
431 		 * If the connection is fully established, break the
432 		 * association with uipc_lock and give the connected
433 		 * pair a seperate lock to share.
434 		 */
435 		unp_setpeerlocks(so2, so);
436 		/*
437 		 * Only now return peer's address, as we may need to
438 		 * block in order to allocate memory.
439 		 *
440 		 * XXX Minor race: connection can be broken while
441 		 * lock is dropped in unp_setaddr().  We will return
442 		 * error == 0 and sun_noname as the peer address.
443 		 */
444 		unp_setaddr(so, nam, true);
445 		break;
446 
447 	case PRU_SHUTDOWN:
448 		socantsendmore(so);
449 		unp_shutdown(unp);
450 		break;
451 
452 	case PRU_RCVD:
453 		switch (so->so_type) {
454 
455 		case SOCK_DGRAM:
456 			panic("uipc 1");
457 			/*NOTREACHED*/
458 
459 		case SOCK_STREAM:
460 #define	rcv (&so->so_rcv)
461 #define snd (&so2->so_snd)
462 			if (unp->unp_conn == 0)
463 				break;
464 			so2 = unp->unp_conn->unp_socket;
465 			KASSERT(solocked2(so, so2));
466 			/*
467 			 * Adjust backpressure on sender
468 			 * and wakeup any waiting to write.
469 			 */
470 			snd->sb_mbmax += unp->unp_mbcnt - rcv->sb_mbcnt;
471 			unp->unp_mbcnt = rcv->sb_mbcnt;
472 			newhiwat = snd->sb_hiwat + unp->unp_cc - rcv->sb_cc;
473 			(void)chgsbsize(so2->so_uidinfo,
474 			    &snd->sb_hiwat, newhiwat, RLIM_INFINITY);
475 			unp->unp_cc = rcv->sb_cc;
476 			sowwakeup(so2);
477 #undef snd
478 #undef rcv
479 			break;
480 
481 		default:
482 			panic("uipc 2");
483 		}
484 		break;
485 
486 	case PRU_SEND:
487 		/*
488 		 * Note: unp_internalize() rejects any control message
489 		 * other than SCM_RIGHTS, and only allows one.  This
490 		 * has the side-effect of preventing a caller from
491 		 * forging SCM_CREDS.
492 		 */
493 		if (control) {
494 			sounlock(so);
495 			error = unp_internalize(&control);
496 			solock(so);
497 			if (error != 0) {
498 				m_freem(control);
499 				m_freem(m);
500 				break;
501 			}
502 		}
503 		switch (so->so_type) {
504 
505 		case SOCK_DGRAM: {
506 			KASSERT(so->so_lock == uipc_lock);
507 			if (nam) {
508 				if ((so->so_state & SS_ISCONNECTED) != 0)
509 					error = EISCONN;
510 				else {
511 					/*
512 					 * Note: once connected, the
513 					 * socket's lock must not be
514 					 * dropped until we have sent
515 					 * the message and disconnected.
516 					 * This is necessary to prevent
517 					 * intervening control ops, like
518 					 * another connection.
519 					 */
520 					error = unp_connect(so, nam, l);
521 				}
522 			} else {
523 				if ((so->so_state & SS_ISCONNECTED) == 0)
524 					error = ENOTCONN;
525 			}
526 			if (error) {
527 				sounlock(so);
528 				unp_dispose(control);
529 				m_freem(control);
530 				m_freem(m);
531 				solock(so);
532 				break;
533 			}
534 			KASSERT(p != NULL);
535 			error = unp_output(m, control, unp, l);
536 			if (nam)
537 				unp_disconnect(unp);
538 			break;
539 		}
540 
541 		case SOCK_STREAM:
542 #define	rcv (&so2->so_rcv)
543 #define	snd (&so->so_snd)
544 			if (unp->unp_conn == NULL) {
545 				error = ENOTCONN;
546 				break;
547 			}
548 			so2 = unp->unp_conn->unp_socket;
549 			KASSERT(solocked2(so, so2));
550 			if (unp->unp_conn->unp_flags & UNP_WANTCRED) {
551 				/*
552 				 * Credentials are passed only once on
553 				 * SOCK_STREAM.
554 				 */
555 				unp->unp_conn->unp_flags &= ~UNP_WANTCRED;
556 				control = unp_addsockcred(l, control);
557 			}
558 			/*
559 			 * Send to paired receive port, and then reduce
560 			 * send buffer hiwater marks to maintain backpressure.
561 			 * Wake up readers.
562 			 */
563 			if (control) {
564 				if (sbappendcontrol(rcv, m, control) != 0)
565 					control = NULL;
566 			} else
567 				sbappend(rcv, m);
568 			snd->sb_mbmax -=
569 			    rcv->sb_mbcnt - unp->unp_conn->unp_mbcnt;
570 			unp->unp_conn->unp_mbcnt = rcv->sb_mbcnt;
571 			newhiwat = snd->sb_hiwat -
572 			    (rcv->sb_cc - unp->unp_conn->unp_cc);
573 			(void)chgsbsize(so->so_uidinfo,
574 			    &snd->sb_hiwat, newhiwat, RLIM_INFINITY);
575 			unp->unp_conn->unp_cc = rcv->sb_cc;
576 			sorwakeup(so2);
577 #undef snd
578 #undef rcv
579 			if (control != NULL) {
580 				sounlock(so);
581 				unp_dispose(control);
582 				m_freem(control);
583 				solock(so);
584 			}
585 			break;
586 
587 		default:
588 			panic("uipc 4");
589 		}
590 		break;
591 
592 	case PRU_ABORT:
593 		(void)unp_drop(unp, ECONNABORTED);
594 
595 		KASSERT(so->so_head == NULL);
596 #ifdef DIAGNOSTIC
597 		if (so->so_pcb == 0)
598 			panic("uipc 5: drop killed pcb");
599 #endif
600 		unp_detach(unp);
601 		break;
602 
603 	case PRU_SENSE:
604 		((struct stat *) m)->st_blksize = so->so_snd.sb_hiwat;
605 		if (so->so_type == SOCK_STREAM && unp->unp_conn != 0) {
606 			so2 = unp->unp_conn->unp_socket;
607 			KASSERT(solocked2(so, so2));
608 			((struct stat *) m)->st_blksize += so2->so_rcv.sb_cc;
609 		}
610 		((struct stat *) m)->st_dev = NODEV;
611 		if (unp->unp_ino == 0)
612 			unp->unp_ino = unp_ino++;
613 		((struct stat *) m)->st_atimespec =
614 		    ((struct stat *) m)->st_mtimespec =
615 		    ((struct stat *) m)->st_ctimespec = unp->unp_ctime;
616 		((struct stat *) m)->st_ino = unp->unp_ino;
617 		return (0);
618 
619 	case PRU_RCVOOB:
620 		error = EOPNOTSUPP;
621 		break;
622 
623 	case PRU_SENDOOB:
624 		m_freem(control);
625 		m_freem(m);
626 		error = EOPNOTSUPP;
627 		break;
628 
629 	case PRU_SOCKADDR:
630 		unp_setaddr(so, nam, false);
631 		break;
632 
633 	case PRU_PEERADDR:
634 		unp_setaddr(so, nam, true);
635 		break;
636 
637 	default:
638 		panic("piusrreq");
639 	}
640 
641 release:
642 	return (error);
643 }
644 
645 /*
646  * Unix domain socket option processing.
647  */
648 int
649 uipc_ctloutput(int op, struct socket *so, int level, int optname,
650 	struct mbuf **mp)
651 {
652 	struct unpcb *unp = sotounpcb(so);
653 	struct mbuf *m = *mp;
654 	int optval = 0, error = 0;
655 
656 	KASSERT(solocked(so));
657 
658 	if (level != 0) {
659 		error = ENOPROTOOPT;
660 		if (op == PRCO_SETOPT && m)
661 			(void) m_free(m);
662 	} else switch (op) {
663 
664 	case PRCO_SETOPT:
665 		switch (optname) {
666 		case LOCAL_CREDS:
667 		case LOCAL_CONNWAIT:
668 			if (m == NULL || m->m_len != sizeof(int))
669 				error = EINVAL;
670 			else {
671 				optval = *mtod(m, int *);
672 				switch (optname) {
673 #define	OPTSET(bit) \
674 	if (optval) \
675 		unp->unp_flags |= (bit); \
676 	else \
677 		unp->unp_flags &= ~(bit);
678 
679 				case LOCAL_CREDS:
680 					OPTSET(UNP_WANTCRED);
681 					break;
682 				case LOCAL_CONNWAIT:
683 					OPTSET(UNP_CONNWAIT);
684 					break;
685 				}
686 			}
687 			break;
688 #undef OPTSET
689 
690 		default:
691 			error = ENOPROTOOPT;
692 			break;
693 		}
694 		if (m)
695 			(void) m_free(m);
696 		break;
697 
698 	case PRCO_GETOPT:
699 		sounlock(so);
700 		switch (optname) {
701 		case LOCAL_PEEREID:
702 			if (unp->unp_flags & UNP_EIDSVALID) {
703 				*mp = m = m_get(M_WAIT, MT_SOOPTS);
704 				m->m_len = sizeof(struct unpcbid);
705 				*mtod(m, struct unpcbid *) = unp->unp_connid;
706 			} else {
707 				error = EINVAL;
708 			}
709 			break;
710 		case LOCAL_CREDS:
711 			*mp = m = m_get(M_WAIT, MT_SOOPTS);
712 			m->m_len = sizeof(int);
713 
714 #define	OPTBIT(bit)	(unp->unp_flags & (bit) ? 1 : 0)
715 
716 			optval = OPTBIT(UNP_WANTCRED);
717 			*mtod(m, int *) = optval;
718 			break;
719 #undef OPTBIT
720 
721 		default:
722 			error = ENOPROTOOPT;
723 			break;
724 		}
725 		solock(so);
726 		break;
727 	}
728 	return (error);
729 }
730 
731 /*
732  * Both send and receive buffers are allocated PIPSIZ bytes of buffering
733  * for stream sockets, although the total for sender and receiver is
734  * actually only PIPSIZ.
735  * Datagram sockets really use the sendspace as the maximum datagram size,
736  * and don't really want to reserve the sendspace.  Their recvspace should
737  * be large enough for at least one max-size datagram plus address.
738  */
739 #define	PIPSIZ	4096
740 u_long	unpst_sendspace = PIPSIZ;
741 u_long	unpst_recvspace = PIPSIZ;
742 u_long	unpdg_sendspace = 2*1024;	/* really max datagram size */
743 u_long	unpdg_recvspace = 4*1024;
744 
745 u_int	unp_rights;			/* file descriptors in flight */
746 
747 int
748 unp_attach(struct socket *so)
749 {
750 	struct unpcb *unp;
751 	int error;
752 
753 	switch (so->so_type) {
754 	case SOCK_STREAM:
755 		if (so->so_lock == NULL) {
756 			/*
757 			 * XXX Assuming that no socket locks are held,
758 			 * as this call may sleep.
759 			 */
760 			so->so_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
761 			solock(so);
762 		}
763 		if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
764 			error = soreserve(so, unpst_sendspace, unpst_recvspace);
765 			if (error != 0)
766 				return (error);
767 		}
768 		break;
769 
770 	case SOCK_DGRAM:
771 		if (so->so_lock == NULL) {
772 			mutex_obj_hold(uipc_lock);
773 			so->so_lock = uipc_lock;
774 			solock(so);
775 		}
776 		if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
777 			error = soreserve(so, unpdg_sendspace, unpdg_recvspace);
778 			if (error != 0)
779 				return (error);
780 		}
781 		break;
782 
783 	default:
784 		panic("unp_attach");
785 	}
786 	KASSERT(solocked(so));
787 	unp = malloc(sizeof(*unp), M_PCB, M_NOWAIT);
788 	if (unp == NULL)
789 		return (ENOBUFS);
790 	memset((void *)unp, 0, sizeof(*unp));
791 	unp->unp_socket = so;
792 	so->so_pcb = unp;
793 	nanotime(&unp->unp_ctime);
794 	return (0);
795 }
796 
797 void
798 unp_detach(struct unpcb *unp)
799 {
800 	struct socket *so;
801 	vnode_t *vp;
802 
803 	so = unp->unp_socket;
804 
805  retry:
806 	if ((vp = unp->unp_vnode) != NULL) {
807 		sounlock(so);
808 		/* Acquire v_interlock to protect against unp_connect(). */
809 		mutex_enter(&vp->v_interlock);
810 		vp->v_socket = NULL;
811 		vrelel(vp, 0);
812 		solock(so);
813 		unp->unp_vnode = NULL;
814 	}
815 	if (unp->unp_conn)
816 		unp_disconnect(unp);
817 	while (unp->unp_refs) {
818 		KASSERT(solocked2(so, unp->unp_refs->unp_socket));
819 		if (unp_drop(unp->unp_refs, ECONNRESET)) {
820 			solock(so);
821 			goto retry;
822 		}
823 	}
824 	soisdisconnected(so);
825 	so->so_pcb = NULL;
826 	if (unp_rights) {
827 		/*
828 		 * Normally the receive buffer is flushed later,
829 		 * in sofree, but if our receive buffer holds references
830 		 * to descriptors that are now garbage, we will dispose
831 		 * of those descriptor references after the garbage collector
832 		 * gets them (resulting in a "panic: closef: count < 0").
833 		 */
834 		sorflush(so);
835 		unp_free(unp);
836 		sounlock(so);
837 		unp_gc();
838 		solock(so);
839 	} else
840 		unp_free(unp);
841 }
842 
843 int
844 unp_bind(struct socket *so, struct mbuf *nam, struct lwp *l)
845 {
846 	struct sockaddr_un *sun;
847 	struct unpcb *unp;
848 	vnode_t *vp;
849 	struct vattr vattr;
850 	size_t addrlen;
851 	int error;
852 	struct nameidata nd;
853 	proc_t *p;
854 
855 	unp = sotounpcb(so);
856 	if (unp->unp_vnode != NULL)
857 		return (EINVAL);
858 	if ((unp->unp_flags & UNP_BUSY) != 0) {
859 		/*
860 		 * EALREADY may not be strictly accurate, but since this
861 		 * is a major application error it's hardly a big deal.
862 		 */
863 		return (EALREADY);
864 	}
865 	unp->unp_flags |= UNP_BUSY;
866 	sounlock(so);
867 
868 	/*
869 	 * Allocate the new sockaddr.  We have to allocate one
870 	 * extra byte so that we can ensure that the pathname
871 	 * is nul-terminated.
872 	 */
873 	p = l->l_proc;
874 	addrlen = nam->m_len + 1;
875 	sun = malloc(addrlen, M_SONAME, M_WAITOK);
876 	m_copydata(nam, 0, nam->m_len, (void *)sun);
877 	*(((char *)sun) + nam->m_len) = '\0';
878 
879 	NDINIT(&nd, CREATE, FOLLOW | LOCKPARENT | TRYEMULROOT, UIO_SYSSPACE,
880 	    sun->sun_path);
881 
882 /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */
883 	if ((error = namei(&nd)) != 0)
884 		goto bad;
885 	vp = nd.ni_vp;
886 	if (vp != NULL) {
887 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
888 		if (nd.ni_dvp == vp)
889 			vrele(nd.ni_dvp);
890 		else
891 			vput(nd.ni_dvp);
892 		vrele(vp);
893 		error = EADDRINUSE;
894 		goto bad;
895 	}
896 	VATTR_NULL(&vattr);
897 	vattr.va_type = VSOCK;
898 	vattr.va_mode = ACCESSPERMS & ~(p->p_cwdi->cwdi_cmask);
899 	error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
900 	if (error)
901 		goto bad;
902 	vp = nd.ni_vp;
903 	solock(so);
904 	vp->v_socket = unp->unp_socket;
905 	unp->unp_vnode = vp;
906 	unp->unp_addrlen = addrlen;
907 	unp->unp_addr = sun;
908 	unp->unp_connid.unp_pid = p->p_pid;
909 	unp->unp_connid.unp_euid = kauth_cred_geteuid(l->l_cred);
910 	unp->unp_connid.unp_egid = kauth_cred_getegid(l->l_cred);
911 	unp->unp_flags |= UNP_EIDSBIND;
912 	VOP_UNLOCK(vp, 0);
913 	unp->unp_flags &= ~UNP_BUSY;
914 	return (0);
915 
916  bad:
917 	free(sun, M_SONAME);
918 	solock(so);
919 	unp->unp_flags &= ~UNP_BUSY;
920 	return (error);
921 }
922 
923 int
924 unp_connect(struct socket *so, struct mbuf *nam, struct lwp *l)
925 {
926 	struct sockaddr_un *sun;
927 	vnode_t *vp;
928 	struct socket *so2, *so3;
929 	struct unpcb *unp, *unp2, *unp3;
930 	size_t addrlen;
931 	int error;
932 	struct nameidata nd;
933 
934 	unp = sotounpcb(so);
935 	if ((unp->unp_flags & UNP_BUSY) != 0) {
936 		/*
937 		 * EALREADY may not be strictly accurate, but since this
938 		 * is a major application error it's hardly a big deal.
939 		 */
940 		return (EALREADY);
941 	}
942 	unp->unp_flags |= UNP_BUSY;
943 	sounlock(so);
944 
945 	/*
946 	 * Allocate a temporary sockaddr.  We have to allocate one extra
947 	 * byte so that we can ensure that the pathname is nul-terminated.
948 	 * When we establish the connection, we copy the other PCB's
949 	 * sockaddr to our own.
950 	 */
951 	addrlen = nam->m_len + 1;
952 	sun = malloc(addrlen, M_SONAME, M_WAITOK);
953 	m_copydata(nam, 0, nam->m_len, (void *)sun);
954 	*(((char *)sun) + nam->m_len) = '\0';
955 
956 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, UIO_SYSSPACE,
957 	    sun->sun_path);
958 
959 	if ((error = namei(&nd)) != 0)
960 		goto bad2;
961 	vp = nd.ni_vp;
962 	if (vp->v_type != VSOCK) {
963 		error = ENOTSOCK;
964 		goto bad;
965 	}
966 	if ((error = VOP_ACCESS(vp, VWRITE, l->l_cred)) != 0)
967 		goto bad;
968 	/* Acquire v_interlock to protect against unp_detach(). */
969 	mutex_enter(&vp->v_interlock);
970 	so2 = vp->v_socket;
971 	if (so2 == NULL) {
972 		mutex_exit(&vp->v_interlock);
973 		error = ECONNREFUSED;
974 		goto bad;
975 	}
976 	if (so->so_type != so2->so_type) {
977 		mutex_exit(&vp->v_interlock);
978 		error = EPROTOTYPE;
979 		goto bad;
980 	}
981 	solock(so);
982 	unp_resetlock(so);
983 	mutex_exit(&vp->v_interlock);
984 	if ((so->so_proto->pr_flags & PR_CONNREQUIRED) != 0) {
985 		/*
986 		 * This may seem somewhat fragile but is OK: if we can
987 		 * see SO_ACCEPTCONN set on the endpoint, then it must
988 		 * be locked by the domain-wide uipc_lock.
989 		 */
990 		KASSERT((so->so_options & SO_ACCEPTCONN) == 0 ||
991 		    so2->so_lock == uipc_lock);
992 		if ((so2->so_options & SO_ACCEPTCONN) == 0 ||
993 		    (so3 = sonewconn(so2, 0)) == 0) {
994 			error = ECONNREFUSED;
995 			sounlock(so);
996 			goto bad;
997 		}
998 		unp2 = sotounpcb(so2);
999 		unp3 = sotounpcb(so3);
1000 		if (unp2->unp_addr) {
1001 			unp3->unp_addr = malloc(unp2->unp_addrlen,
1002 			    M_SONAME, M_WAITOK);
1003 			memcpy(unp3->unp_addr, unp2->unp_addr,
1004 			    unp2->unp_addrlen);
1005 			unp3->unp_addrlen = unp2->unp_addrlen;
1006 		}
1007 		unp3->unp_flags = unp2->unp_flags;
1008 		unp3->unp_connid.unp_pid = l->l_proc->p_pid;
1009 		unp3->unp_connid.unp_euid = kauth_cred_geteuid(l->l_cred);
1010 		unp3->unp_connid.unp_egid = kauth_cred_getegid(l->l_cred);
1011 		unp3->unp_flags |= UNP_EIDSVALID;
1012 		if (unp2->unp_flags & UNP_EIDSBIND) {
1013 			unp->unp_connid = unp2->unp_connid;
1014 			unp->unp_flags |= UNP_EIDSVALID;
1015 		}
1016 		so2 = so3;
1017 	}
1018 	error = unp_connect2(so, so2, PRU_CONNECT);
1019 	sounlock(so);
1020  bad:
1021 	vput(vp);
1022  bad2:
1023 	free(sun, M_SONAME);
1024 	solock(so);
1025 	unp->unp_flags &= ~UNP_BUSY;
1026 	return (error);
1027 }
1028 
1029 int
1030 unp_connect2(struct socket *so, struct socket *so2, int req)
1031 {
1032 	struct unpcb *unp = sotounpcb(so);
1033 	struct unpcb *unp2;
1034 
1035 	if (so2->so_type != so->so_type)
1036 		return (EPROTOTYPE);
1037 
1038 	/*
1039 	 * All three sockets involved must be locked by same lock:
1040 	 *
1041 	 * local endpoint (so)
1042 	 * remote endpoint (so2)
1043 	 * queue head (so->so_head, only if PR_CONNREQUIRED)
1044 	 */
1045 	KASSERT(solocked2(so, so2));
1046 	if (so->so_head != NULL) {
1047 		KASSERT(so->so_lock == uipc_lock);
1048 		KASSERT(solocked2(so, so->so_head));
1049 	}
1050 
1051 	unp2 = sotounpcb(so2);
1052 	unp->unp_conn = unp2;
1053 	switch (so->so_type) {
1054 
1055 	case SOCK_DGRAM:
1056 		unp->unp_nextref = unp2->unp_refs;
1057 		unp2->unp_refs = unp;
1058 		soisconnected(so);
1059 		break;
1060 
1061 	case SOCK_STREAM:
1062 		unp2->unp_conn = unp;
1063 		if (req == PRU_CONNECT &&
1064 		    ((unp->unp_flags | unp2->unp_flags) & UNP_CONNWAIT))
1065 			soisconnecting(so);
1066 		else
1067 			soisconnected(so);
1068 		soisconnected(so2);
1069 		/*
1070 		 * If the connection is fully established, break the
1071 		 * association with uipc_lock and give the connected
1072 		 * pair a seperate lock to share.  For CONNECT2, we
1073 		 * require that the locks already match (the sockets
1074 		 * are created that way).
1075 		 */
1076 		if (req == PRU_CONNECT)
1077 			unp_setpeerlocks(so, so2);
1078 		break;
1079 
1080 	default:
1081 		panic("unp_connect2");
1082 	}
1083 	return (0);
1084 }
1085 
1086 void
1087 unp_disconnect(struct unpcb *unp)
1088 {
1089 	struct unpcb *unp2 = unp->unp_conn;
1090 	struct socket *so;
1091 
1092 	if (unp2 == 0)
1093 		return;
1094 	unp->unp_conn = 0;
1095 	so = unp->unp_socket;
1096 	switch (so->so_type) {
1097 	case SOCK_DGRAM:
1098 		if (unp2->unp_refs == unp)
1099 			unp2->unp_refs = unp->unp_nextref;
1100 		else {
1101 			unp2 = unp2->unp_refs;
1102 			for (;;) {
1103 				KASSERT(solocked2(so, unp2->unp_socket));
1104 				if (unp2 == 0)
1105 					panic("unp_disconnect");
1106 				if (unp2->unp_nextref == unp)
1107 					break;
1108 				unp2 = unp2->unp_nextref;
1109 			}
1110 			unp2->unp_nextref = unp->unp_nextref;
1111 		}
1112 		unp->unp_nextref = 0;
1113 		so->so_state &= ~SS_ISCONNECTED;
1114 		break;
1115 
1116 	case SOCK_STREAM:
1117 		KASSERT(solocked2(so, unp2->unp_socket));
1118 		soisdisconnected(so);
1119 		unp2->unp_conn = 0;
1120 		soisdisconnected(unp2->unp_socket);
1121 		break;
1122 	}
1123 }
1124 
1125 #ifdef notdef
1126 unp_abort(struct unpcb *unp)
1127 {
1128 	unp_detach(unp);
1129 }
1130 #endif
1131 
1132 void
1133 unp_shutdown(struct unpcb *unp)
1134 {
1135 	struct socket *so;
1136 
1137 	if (unp->unp_socket->so_type == SOCK_STREAM && unp->unp_conn &&
1138 	    (so = unp->unp_conn->unp_socket))
1139 		socantrcvmore(so);
1140 }
1141 
1142 bool
1143 unp_drop(struct unpcb *unp, int errno)
1144 {
1145 	struct socket *so = unp->unp_socket;
1146 
1147 	KASSERT(solocked(so));
1148 
1149 	so->so_error = errno;
1150 	unp_disconnect(unp);
1151 	if (so->so_head) {
1152 		so->so_pcb = NULL;
1153 		/* sofree() drops the socket lock */
1154 		sofree(so);
1155 		unp_free(unp);
1156 		return true;
1157 	}
1158 	return false;
1159 }
1160 
1161 #ifdef notdef
1162 unp_drain(void)
1163 {
1164 
1165 }
1166 #endif
1167 
1168 int
1169 unp_externalize(struct mbuf *rights, struct lwp *l)
1170 {
1171 	struct cmsghdr *cm = mtod(rights, struct cmsghdr *);
1172 	struct proc *p = l->l_proc;
1173 	int i, *fdp;
1174 	file_t **rp;
1175 	file_t *fp;
1176 	int nfds, error = 0;
1177 
1178 	nfds = (cm->cmsg_len - CMSG_ALIGN(sizeof(*cm))) /
1179 	    sizeof(file_t *);
1180 	rp = (file_t **)CMSG_DATA(cm);
1181 
1182 	fdp = malloc(nfds * sizeof(int), M_TEMP, M_WAITOK);
1183 	rw_enter(&p->p_cwdi->cwdi_lock, RW_READER);
1184 
1185 	/* Make sure the recipient should be able to see the descriptors.. */
1186 	if (p->p_cwdi->cwdi_rdir != NULL) {
1187 		rp = (file_t **)CMSG_DATA(cm);
1188 		for (i = 0; i < nfds; i++) {
1189 			fp = *rp++;
1190 			/*
1191 			 * If we are in a chroot'ed directory, and
1192 			 * someone wants to pass us a directory, make
1193 			 * sure it's inside the subtree we're allowed
1194 			 * to access.
1195 			 */
1196 			if (fp->f_type == DTYPE_VNODE) {
1197 				vnode_t *vp = (vnode_t *)fp->f_data;
1198 				if ((vp->v_type == VDIR) &&
1199 				    !vn_isunder(vp, p->p_cwdi->cwdi_rdir, l)) {
1200 					error = EPERM;
1201 					break;
1202 				}
1203 			}
1204 		}
1205 	}
1206 
1207  restart:
1208 	rp = (file_t **)CMSG_DATA(cm);
1209 	if (error != 0) {
1210 		for (i = 0; i < nfds; i++) {
1211 			fp = *rp;
1212 			/*
1213 			 * zero the pointer before calling unp_discard,
1214 			 * since it may end up in unp_gc()..
1215 			 */
1216 			*rp++ = 0;
1217 			unp_discard(fp);
1218 		}
1219 		goto out;
1220 	}
1221 
1222 	/*
1223 	 * First loop -- allocate file descriptor table slots for the
1224 	 * new descriptors.
1225 	 */
1226 	for (i = 0; i < nfds; i++) {
1227 		fp = *rp++;
1228 		if ((error = fd_alloc(p, 0, &fdp[i])) != 0) {
1229 			/*
1230 			 * Back out what we've done so far.
1231 			 */
1232 			for (--i; i >= 0; i--) {
1233 				fd_abort(p, NULL, fdp[i]);
1234 			}
1235 			if (error == ENOSPC) {
1236 				fd_tryexpand(p);
1237 				error = 0;
1238 			} else {
1239 				/*
1240 				 * This is the error that has historically
1241 				 * been returned, and some callers may
1242 				 * expect it.
1243 				 */
1244 				error = EMSGSIZE;
1245 			}
1246 			goto restart;
1247 		}
1248 	}
1249 
1250 	/*
1251 	 * Now that adding them has succeeded, update all of the
1252 	 * descriptor passing state.
1253 	 */
1254 	rp = (file_t **)CMSG_DATA(cm);
1255 	for (i = 0; i < nfds; i++) {
1256 		fp = *rp++;
1257 		atomic_dec_uint(&unp_rights);
1258 		fd_affix(p, fp, fdp[i]);
1259 		mutex_enter(&fp->f_lock);
1260 		fp->f_msgcount--;
1261 		mutex_exit(&fp->f_lock);
1262 		/*
1263 		 * Note that fd_affix() adds a reference to the file.
1264 		 * The file may already have been closed by another
1265 		 * LWP in the process, so we must drop the reference
1266 		 * added by unp_internalize() with closef().
1267 		 */
1268 		closef(fp);
1269 	}
1270 
1271 	/*
1272 	 * Copy temporary array to message and adjust length, in case of
1273 	 * transition from large file_t pointers to ints.
1274 	 */
1275 	memcpy(CMSG_DATA(cm), fdp, nfds * sizeof(int));
1276 	cm->cmsg_len = CMSG_LEN(nfds * sizeof(int));
1277 	rights->m_len = CMSG_SPACE(nfds * sizeof(int));
1278  out:
1279 	rw_exit(&p->p_cwdi->cwdi_lock);
1280 	free(fdp, M_TEMP);
1281 	return (error);
1282 }
1283 
1284 int
1285 unp_internalize(struct mbuf **controlp)
1286 {
1287 	struct filedesc *fdescp = curlwp->l_fd;
1288 	struct mbuf *control = *controlp;
1289 	struct cmsghdr *newcm, *cm = mtod(control, struct cmsghdr *);
1290 	file_t **rp, **files;
1291 	file_t *fp;
1292 	int i, fd, *fdp;
1293 	int nfds, error;
1294 
1295 	error = 0;
1296 	newcm = NULL;
1297 
1298 	/* Sanity check the control message header. */
1299 	if (cm->cmsg_type != SCM_RIGHTS || cm->cmsg_level != SOL_SOCKET ||
1300 	    cm->cmsg_len != control->m_len)
1301 		return (EINVAL);
1302 
1303 	/*
1304 	 * Verify that the file descriptors are valid, and acquire
1305 	 * a reference to each.
1306 	 */
1307 	nfds = (cm->cmsg_len - CMSG_ALIGN(sizeof(*cm))) / sizeof(int);
1308 	fdp = (int *)CMSG_DATA(cm);
1309 	for (i = 0; i < nfds; i++) {
1310 		fd = *fdp++;
1311 		if ((fp = fd_getfile(fd)) == NULL) {
1312 			nfds = i + 1;
1313 			error = EBADF;
1314 			goto out;
1315 		}
1316 	}
1317 
1318 	/* Allocate new space and copy header into it. */
1319 	newcm = malloc(CMSG_SPACE(nfds * sizeof(file_t *)), M_MBUF, M_WAITOK);
1320 	if (newcm == NULL) {
1321 		error = E2BIG;
1322 		goto out;
1323 	}
1324 	memcpy(newcm, cm, sizeof(struct cmsghdr));
1325 	files = (file_t **)CMSG_DATA(newcm);
1326 
1327 	/*
1328 	 * Transform the file descriptors into file_t pointers, in
1329 	 * reverse order so that if pointers are bigger than ints, the
1330 	 * int won't get until we're done.  No need to lock, as we have
1331 	 * already validated the descriptors with fd_getfile().
1332 	 */
1333 	fdp = (int *)CMSG_DATA(cm) + nfds;
1334 	rp = files + nfds;
1335 	for (i = 0; i < nfds; i++) {
1336 		fp = fdescp->fd_ofiles[*--fdp]->ff_file;
1337 		KASSERT(fp != NULL);
1338 		mutex_enter(&fp->f_lock);
1339 		*--rp = fp;
1340 		fp->f_count++;
1341 		fp->f_msgcount++;
1342 		mutex_exit(&fp->f_lock);
1343 		atomic_inc_uint(&unp_rights);
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 	}
1352 
1353 	if (error == 0) {
1354 		if (control->m_flags & M_EXT) {
1355 			m_freem(control);
1356 			*controlp = control = m_get(M_WAIT, MT_CONTROL);
1357 		}
1358 		MEXTADD(control, newcm, CMSG_SPACE(nfds * sizeof(file_t *)),
1359 		    M_MBUF, NULL, NULL);
1360 		cm = newcm;
1361 		/*
1362 		 * Adjust message & mbuf to note amount of space
1363 		 * actually used.
1364 		 */
1365 		cm->cmsg_len = CMSG_LEN(nfds * sizeof(file_t *));
1366 		control->m_len = CMSG_SPACE(nfds * sizeof(file_t *));
1367 	}
1368 
1369 	return error;
1370 }
1371 
1372 struct mbuf *
1373 unp_addsockcred(struct lwp *l, struct mbuf *control)
1374 {
1375 	struct cmsghdr *cmp;
1376 	struct sockcred *sc;
1377 	struct mbuf *m, *n;
1378 	int len, space, i;
1379 
1380 	len = CMSG_LEN(SOCKCREDSIZE(kauth_cred_ngroups(l->l_cred)));
1381 	space = CMSG_SPACE(SOCKCREDSIZE(kauth_cred_ngroups(l->l_cred)));
1382 
1383 	m = m_get(M_WAIT, MT_CONTROL);
1384 	if (space > MLEN) {
1385 		if (space > MCLBYTES)
1386 			MEXTMALLOC(m, space, M_WAITOK);
1387 		else
1388 			m_clget(m, M_WAIT);
1389 		if ((m->m_flags & M_EXT) == 0) {
1390 			m_free(m);
1391 			return (control);
1392 		}
1393 	}
1394 
1395 	m->m_len = space;
1396 	m->m_next = NULL;
1397 	cmp = mtod(m, struct cmsghdr *);
1398 	sc = (struct sockcred *)CMSG_DATA(cmp);
1399 	cmp->cmsg_len = len;
1400 	cmp->cmsg_level = SOL_SOCKET;
1401 	cmp->cmsg_type = SCM_CREDS;
1402 	sc->sc_uid = kauth_cred_getuid(l->l_cred);
1403 	sc->sc_euid = kauth_cred_geteuid(l->l_cred);
1404 	sc->sc_gid = kauth_cred_getgid(l->l_cred);
1405 	sc->sc_egid = kauth_cred_getegid(l->l_cred);
1406 	sc->sc_ngroups = kauth_cred_ngroups(l->l_cred);
1407 	for (i = 0; i < sc->sc_ngroups; i++)
1408 		sc->sc_groups[i] = kauth_cred_group(l->l_cred, i);
1409 
1410 	/*
1411 	 * If a control message already exists, append us to the end.
1412 	 */
1413 	if (control != NULL) {
1414 		for (n = control; n->m_next != NULL; n = n->m_next)
1415 			;
1416 		n->m_next = m;
1417 	} else
1418 		control = m;
1419 
1420 	return (control);
1421 }
1422 
1423 int	unp_defer, unp_gcing;
1424 extern	struct domain unixdomain;
1425 
1426 /*
1427  * Comment added long after the fact explaining what's going on here.
1428  * Do a mark-sweep GC of file descriptors on the system, to free up
1429  * any which are caught in flight to an about-to-be-closed socket.
1430  *
1431  * Traditional mark-sweep gc's start at the "root", and mark
1432  * everything reachable from the root (which, in our case would be the
1433  * process table).  The mark bits are cleared during the sweep.
1434  *
1435  * XXX For some inexplicable reason (perhaps because the file
1436  * descriptor tables used to live in the u area which could be swapped
1437  * out and thus hard to reach), we do multiple scans over the set of
1438  * descriptors, using use *two* mark bits per object (DEFER and MARK).
1439  * Whenever we find a descriptor which references other descriptors,
1440  * the ones it references are marked with both bits, and we iterate
1441  * over the whole file table until there are no more DEFER bits set.
1442  * We also make an extra pass *before* the GC to clear the mark bits,
1443  * which could have been cleared at almost no cost during the previous
1444  * sweep.
1445  */
1446 void
1447 unp_gc(void)
1448 {
1449 	file_t *fp, *nextfp;
1450 	struct socket *so, *so1;
1451 	file_t **extra_ref, **fpp;
1452 	int nunref, nslots, i;
1453 
1454 	if (atomic_swap_uint(&unp_gcing, 1) == 1)
1455 		return;
1456 
1457  restart:
1458  	nslots = nfiles * 2;
1459  	extra_ref = kmem_alloc(nslots * sizeof(file_t *), KM_SLEEP);
1460 
1461 	mutex_enter(&filelist_lock);
1462 	unp_defer = 0;
1463 
1464 	/* Clear mark bits */
1465 	LIST_FOREACH(fp, &filehead, f_list) {
1466 		atomic_and_uint(&fp->f_flag, ~(FMARK|FDEFER));
1467 	}
1468 
1469 	/*
1470 	 * Iterate over the set of descriptors, marking ones believed
1471 	 * (based on refcount) to be referenced from a process, and
1472 	 * marking for rescan descriptors which are queued on a socket.
1473 	 */
1474 	do {
1475 		LIST_FOREACH(fp, &filehead, f_list) {
1476 			mutex_enter(&fp->f_lock);
1477 			if (fp->f_flag & FDEFER) {
1478 				atomic_and_uint(&fp->f_flag, ~FDEFER);
1479 				unp_defer--;
1480 				KASSERT(fp->f_count != 0);
1481 			} else {
1482 				if (fp->f_count == 0 ||
1483 				    (fp->f_flag & FMARK) ||
1484 				    fp->f_count == fp->f_msgcount) {
1485 					mutex_exit(&fp->f_lock);
1486 					continue;
1487 				}
1488 			}
1489 			atomic_or_uint(&fp->f_flag, FMARK);
1490 
1491 			if (fp->f_type != DTYPE_SOCKET ||
1492 			    (so = fp->f_data) == NULL ||
1493 			    so->so_proto->pr_domain != &unixdomain ||
1494 			    (so->so_proto->pr_flags&PR_RIGHTS) == 0) {
1495 				mutex_exit(&fp->f_lock);
1496 				continue;
1497 			}
1498 #ifdef notdef
1499 			if (so->so_rcv.sb_flags & SB_LOCK) {
1500 				mutex_exit(&fp->f_lock);
1501 				mutex_exit(&filelist_lock);
1502 				kmem_free(extra_ref, nslots * sizeof(file_t *));
1503 				/*
1504 				 * This is problematical; it's not clear
1505 				 * we need to wait for the sockbuf to be
1506 				 * unlocked (on a uniprocessor, at least),
1507 				 * and it's also not clear what to do
1508 				 * if sbwait returns an error due to receipt
1509 				 * of a signal.  If sbwait does return
1510 				 * an error, we'll go into an infinite
1511 				 * loop.  Delete all of this for now.
1512 				 */
1513 				(void) sbwait(&so->so_rcv);
1514 				goto restart;
1515 			}
1516 #endif
1517 			mutex_exit(&fp->f_lock);
1518 
1519 			/*
1520 			 * XXX Locking a socket with filelist_lock held
1521 			 * is ugly.  filelist_lock can be taken by the
1522 			 * pagedaemon when reclaiming items from file_cache.
1523 			 * Socket activity could delay the pagedaemon.
1524 			 */
1525 			solock(so);
1526 			unp_scan(so->so_rcv.sb_mb, unp_mark, 0);
1527 			/*
1528 			 * Mark descriptors referenced from sockets queued
1529 			 * on the accept queue as well.
1530 			 */
1531 			if (so->so_options & SO_ACCEPTCONN) {
1532 				TAILQ_FOREACH(so1, &so->so_q0, so_qe) {
1533 					unp_scan(so1->so_rcv.sb_mb, unp_mark, 0);
1534 				}
1535 				TAILQ_FOREACH(so1, &so->so_q, so_qe) {
1536 					unp_scan(so1->so_rcv.sb_mb, unp_mark, 0);
1537 				}
1538 			}
1539 			sounlock(so);
1540 		}
1541 	} while (unp_defer);
1542 
1543 	/*
1544 	 * Sweep pass.  Find unmarked descriptors, and free them.
1545 	 *
1546 	 * We grab an extra reference to each of the file table entries
1547 	 * that are not otherwise accessible and then free the rights
1548 	 * that are stored in messages on them.
1549 	 *
1550 	 * The bug in the original code is a little tricky, so I'll describe
1551 	 * what's wrong with it here.
1552 	 *
1553 	 * It is incorrect to simply unp_discard each entry for f_msgcount
1554 	 * times -- consider the case of sockets A and B that contain
1555 	 * references to each other.  On a last close of some other socket,
1556 	 * we trigger a gc since the number of outstanding rights (unp_rights)
1557 	 * is non-zero.  If during the sweep phase the gc code un_discards,
1558 	 * we end up doing a (full) closef on the descriptor.  A closef on A
1559 	 * results in the following chain.  Closef calls soo_close, which
1560 	 * calls soclose.   Soclose calls first (through the switch
1561 	 * uipc_usrreq) unp_detach, which re-invokes unp_gc.  Unp_gc simply
1562 	 * returns because the previous instance had set unp_gcing, and
1563 	 * we return all the way back to soclose, which marks the socket
1564 	 * with SS_NOFDREF, and then calls sofree.  Sofree calls sorflush
1565 	 * to free up the rights that are queued in messages on the socket A,
1566 	 * i.e., the reference on B.  The sorflush calls via the dom_dispose
1567 	 * switch unp_dispose, which unp_scans with unp_discard.  This second
1568 	 * instance of unp_discard just calls closef on B.
1569 	 *
1570 	 * Well, a similar chain occurs on B, resulting in a sorflush on B,
1571 	 * which results in another closef on A.  Unfortunately, A is already
1572 	 * being closed, and the descriptor has already been marked with
1573 	 * SS_NOFDREF, and soclose panics at this point.
1574 	 *
1575 	 * Here, we first take an extra reference to each inaccessible
1576 	 * descriptor.  Then, if the inaccessible descriptor is a
1577 	 * socket, we call sorflush in case it is a Unix domain
1578 	 * socket.  After we destroy all the rights carried in
1579 	 * messages, we do a last closef to get rid of our extra
1580 	 * reference.  This is the last close, and the unp_detach etc
1581 	 * will shut down the socket.
1582 	 *
1583 	 * 91/09/19, bsy@cs.cmu.edu
1584 	 */
1585 	if (nslots < nfiles) {
1586 		mutex_exit(&filelist_lock);
1587 		kmem_free(extra_ref, nslots * sizeof(file_t *));
1588 		goto restart;
1589 	}
1590 	for (nunref = 0, fp = LIST_FIRST(&filehead), fpp = extra_ref; fp != 0;
1591 	    fp = nextfp) {
1592 		nextfp = LIST_NEXT(fp, f_list);
1593 		mutex_enter(&fp->f_lock);
1594 		if (fp->f_count != 0 &&
1595 		    fp->f_count == fp->f_msgcount && !(fp->f_flag & FMARK)) {
1596 			*fpp++ = fp;
1597 			nunref++;
1598 			fp->f_count++;
1599 		}
1600 		mutex_exit(&fp->f_lock);
1601 	}
1602 	mutex_exit(&filelist_lock);
1603 
1604 	for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) {
1605 		fp = *fpp;
1606 		if (fp->f_type == DTYPE_SOCKET) {
1607 			so = fp->f_data;
1608 			solock(so);
1609 			sorflush(fp->f_data);
1610 			sounlock(so);
1611 		}
1612 	}
1613 	for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) {
1614 		closef(*fpp);
1615 	}
1616 	kmem_free(extra_ref, nslots * sizeof(file_t *));
1617 	atomic_swap_uint(&unp_gcing, 0);
1618 }
1619 
1620 void
1621 unp_dispose(struct mbuf *m)
1622 {
1623 
1624 	if (m)
1625 		unp_scan(m, unp_discard, 1);
1626 }
1627 
1628 void
1629 unp_scan(struct mbuf *m0, void (*op)(file_t *), int discard)
1630 {
1631 	struct mbuf *m;
1632 	file_t **rp;
1633 	struct cmsghdr *cm;
1634 	int i;
1635 	int qfds;
1636 
1637 	while (m0) {
1638 		for (m = m0; m; m = m->m_next) {
1639 			if (m->m_type == MT_CONTROL &&
1640 			    m->m_len >= sizeof(*cm)) {
1641 				cm = mtod(m, struct cmsghdr *);
1642 				if (cm->cmsg_level != SOL_SOCKET ||
1643 				    cm->cmsg_type != SCM_RIGHTS)
1644 					continue;
1645 				qfds = (cm->cmsg_len - CMSG_ALIGN(sizeof(*cm)))
1646 				    / sizeof(file_t *);
1647 				rp = (file_t **)CMSG_DATA(cm);
1648 				for (i = 0; i < qfds; i++) {
1649 					file_t *fp = *rp;
1650 					if (discard)
1651 						*rp = 0;
1652 					(*op)(fp);
1653 					rp++;
1654 				}
1655 				break;		/* XXX, but saves time */
1656 			}
1657 		}
1658 		m0 = m0->m_nextpkt;
1659 	}
1660 }
1661 
1662 void
1663 unp_mark(file_t *fp)
1664 {
1665 
1666 	if (fp == NULL)
1667 		return;
1668 
1669 	/* If we're already deferred, don't screw up the defer count */
1670 	mutex_enter(&fp->f_lock);
1671 	if (fp->f_flag & (FMARK | FDEFER)) {
1672 		mutex_exit(&fp->f_lock);
1673 		return;
1674 	}
1675 
1676 	/*
1677 	 * Minimize the number of deferrals...  Sockets are the only
1678 	 * type of descriptor which can hold references to another
1679 	 * descriptor, so just mark other descriptors, and defer
1680 	 * unmarked sockets for the next pass.
1681 	 */
1682 	if (fp->f_type == DTYPE_SOCKET) {
1683 		unp_defer++;
1684 		KASSERT(fp->f_count != 0);
1685 		atomic_or_uint(&fp->f_flag, FDEFER);
1686 	} else {
1687 		atomic_or_uint(&fp->f_flag, FMARK);
1688 	}
1689 	mutex_exit(&fp->f_lock);
1690 	return;
1691 }
1692 
1693 void
1694 unp_discard(file_t *fp)
1695 {
1696 
1697 	if (fp == NULL)
1698 		return;
1699 
1700 	mutex_enter(&fp->f_lock);
1701 	KASSERT(fp->f_count > 0);
1702 	fp->f_msgcount--;
1703 	mutex_exit(&fp->f_lock);
1704 	atomic_dec_uint(&unp_rights);
1705 	(void)closef(fp);
1706 }
1707