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