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