xref: /dragonfly/sys/kern/uipc_usrreq.c (revision 3f5e28f4)
1 /*
2  * Copyright (c) 1982, 1986, 1989, 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	From: @(#)uipc_usrreq.c	8.3 (Berkeley) 1/4/94
34  * $FreeBSD: src/sys/kern/uipc_usrreq.c,v 1.54.2.10 2003/03/04 17:28:09 nectar Exp $
35  * $DragonFly: src/sys/kern/uipc_usrreq.c,v 1.34 2007/04/22 01:13:10 dillon Exp $
36  */
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/domain.h>
42 #include <sys/fcntl.h>
43 #include <sys/malloc.h>		/* XXX must be before <sys/file.h> */
44 #include <sys/proc.h>
45 #include <sys/file.h>
46 #include <sys/filedesc.h>
47 #include <sys/mbuf.h>
48 #include <sys/nlookup.h>
49 #include <sys/protosw.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
52 #include <sys/resourcevar.h>
53 #include <sys/stat.h>
54 #include <sys/mount.h>
55 #include <sys/sysctl.h>
56 #include <sys/un.h>
57 #include <sys/unpcb.h>
58 #include <sys/vnode.h>
59 #include <sys/file2.h>
60 #include <sys/spinlock2.h>
61 
62 #include <vm/vm_zone.h>
63 
64 static	struct vm_zone *unp_zone;
65 static	unp_gen_t unp_gencnt;
66 static	u_int unp_count;
67 
68 static	struct unp_head unp_shead, unp_dhead;
69 
70 /*
71  * Unix communications domain.
72  *
73  * TODO:
74  *	SEQPACKET, RDM
75  *	rethink name space problems
76  *	need a proper out-of-band
77  *	lock pushdown
78  */
79 static struct	sockaddr sun_noname = { sizeof(sun_noname), AF_LOCAL };
80 static ino_t	unp_ino;		/* prototype for fake inode numbers */
81 
82 static int     unp_attach (struct socket *, struct pru_attach_info *);
83 static void    unp_detach (struct unpcb *);
84 static int     unp_bind (struct unpcb *,struct sockaddr *, struct thread *);
85 static int     unp_connect (struct socket *,struct sockaddr *,
86 				struct thread *);
87 static void    unp_disconnect (struct unpcb *);
88 static void    unp_shutdown (struct unpcb *);
89 static void    unp_drop (struct unpcb *, int);
90 static void    unp_gc (void);
91 static int     unp_gc_clearmarks(struct file *, void *);
92 static int     unp_gc_checkmarks(struct file *, void *);
93 static int     unp_gc_checkrefs(struct file *, void *);
94 static void    unp_scan (struct mbuf *, void (*)(struct file *, void *),
95 				void *data);
96 static void    unp_mark (struct file *, void *data);
97 static void    unp_discard (struct file *, void *);
98 static int     unp_internalize (struct mbuf *, struct thread *);
99 static int     unp_listen (struct unpcb *, struct thread *);
100 
101 static int
102 uipc_abort(struct socket *so)
103 {
104 	struct unpcb *unp = so->so_pcb;
105 
106 	if (unp == NULL)
107 		return EINVAL;
108 	unp_drop(unp, ECONNABORTED);
109 	unp_detach(unp);
110 	sofree(so);
111 	return 0;
112 }
113 
114 static int
115 uipc_accept(struct socket *so, struct sockaddr **nam)
116 {
117 	struct unpcb *unp = so->so_pcb;
118 
119 	if (unp == NULL)
120 		return EINVAL;
121 
122 	/*
123 	 * Pass back name of connected socket,
124 	 * if it was bound and we are still connected
125 	 * (our peer may have closed already!).
126 	 */
127 	if (unp->unp_conn && unp->unp_conn->unp_addr) {
128 		*nam = dup_sockaddr((struct sockaddr *)unp->unp_conn->unp_addr);
129 	} else {
130 		*nam = dup_sockaddr((struct sockaddr *)&sun_noname);
131 	}
132 	return 0;
133 }
134 
135 static int
136 uipc_attach(struct socket *so, int proto, struct pru_attach_info *ai)
137 {
138 	struct unpcb *unp = so->so_pcb;
139 
140 	if (unp != NULL)
141 		return EISCONN;
142 	return unp_attach(so, ai);
143 }
144 
145 static int
146 uipc_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
147 {
148 	struct unpcb *unp = so->so_pcb;
149 
150 	if (unp == NULL)
151 		return EINVAL;
152 	return unp_bind(unp, nam, td);
153 }
154 
155 static int
156 uipc_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
157 {
158 	struct unpcb *unp = so->so_pcb;
159 
160 	if (unp == NULL)
161 		return EINVAL;
162 	return unp_connect(so, nam, td);
163 }
164 
165 static int
166 uipc_connect2(struct socket *so1, struct socket *so2)
167 {
168 	struct unpcb *unp = so1->so_pcb;
169 
170 	if (unp == NULL)
171 		return EINVAL;
172 
173 	return unp_connect2(so1, so2);
174 }
175 
176 /* control is EOPNOTSUPP */
177 
178 static int
179 uipc_detach(struct socket *so)
180 {
181 	struct unpcb *unp = so->so_pcb;
182 
183 	if (unp == NULL)
184 		return EINVAL;
185 
186 	unp_detach(unp);
187 	return 0;
188 }
189 
190 static int
191 uipc_disconnect(struct socket *so)
192 {
193 	struct unpcb *unp = so->so_pcb;
194 
195 	if (unp == NULL)
196 		return EINVAL;
197 	unp_disconnect(unp);
198 	return 0;
199 }
200 
201 static int
202 uipc_listen(struct socket *so, struct thread *td)
203 {
204 	struct unpcb *unp = so->so_pcb;
205 
206 	if (unp == NULL || unp->unp_vnode == NULL)
207 		return EINVAL;
208 	return unp_listen(unp, td);
209 }
210 
211 static int
212 uipc_peeraddr(struct socket *so, struct sockaddr **nam)
213 {
214 	struct unpcb *unp = so->so_pcb;
215 
216 	if (unp == NULL)
217 		return EINVAL;
218 	if (unp->unp_conn && unp->unp_conn->unp_addr)
219 		*nam = dup_sockaddr((struct sockaddr *)unp->unp_conn->unp_addr);
220 	else {
221 		/*
222 		 * XXX: It seems that this test always fails even when
223 		 * connection is established.  So, this else clause is
224 		 * added as workaround to return PF_LOCAL sockaddr.
225 		 */
226 		*nam = dup_sockaddr((struct sockaddr *)&sun_noname);
227 	}
228 	return 0;
229 }
230 
231 static int
232 uipc_rcvd(struct socket *so, int flags)
233 {
234 	struct unpcb *unp = so->so_pcb;
235 	struct socket *so2;
236 	u_long newhiwat;
237 
238 	if (unp == NULL)
239 		return EINVAL;
240 	switch (so->so_type) {
241 	case SOCK_DGRAM:
242 		panic("uipc_rcvd DGRAM?");
243 		/*NOTREACHED*/
244 
245 	case SOCK_STREAM:
246 		if (unp->unp_conn == NULL)
247 			break;
248 		so2 = unp->unp_conn->unp_socket;
249 		/*
250 		 * Adjust backpressure on sender
251 		 * and wakeup any waiting to write.
252 		 */
253 		so2->so_snd.ssb_mbmax += unp->unp_mbcnt - so->so_rcv.ssb_mbcnt;
254 		unp->unp_mbcnt = so->so_rcv.ssb_mbcnt;
255 		newhiwat =
256 		    so2->so_snd.ssb_hiwat + unp->unp_cc - so->so_rcv.ssb_cc;
257 		chgsbsize(so2->so_cred->cr_uidinfo, &so2->so_snd.ssb_hiwat,
258 		    newhiwat, RLIM_INFINITY);
259 		unp->unp_cc = so->so_rcv.ssb_cc;
260 		sowwakeup(so2);
261 		break;
262 
263 	default:
264 		panic("uipc_rcvd unknown socktype");
265 	}
266 	return 0;
267 }
268 
269 /* pru_rcvoob is EOPNOTSUPP */
270 
271 static int
272 uipc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
273 	  struct mbuf *control, struct thread *td)
274 {
275 	int error = 0;
276 	struct unpcb *unp = so->so_pcb;
277 	struct socket *so2;
278 	u_long newhiwat;
279 
280 	if (unp == NULL) {
281 		error = EINVAL;
282 		goto release;
283 	}
284 	if (flags & PRUS_OOB) {
285 		error = EOPNOTSUPP;
286 		goto release;
287 	}
288 
289 	if (control && (error = unp_internalize(control, td)))
290 		goto release;
291 
292 	switch (so->so_type) {
293 	case SOCK_DGRAM:
294 	{
295 		struct sockaddr *from;
296 
297 		if (nam) {
298 			if (unp->unp_conn) {
299 				error = EISCONN;
300 				break;
301 			}
302 			error = unp_connect(so, nam, td);
303 			if (error)
304 				break;
305 		} else {
306 			if (unp->unp_conn == NULL) {
307 				error = ENOTCONN;
308 				break;
309 			}
310 		}
311 		so2 = unp->unp_conn->unp_socket;
312 		if (unp->unp_addr)
313 			from = (struct sockaddr *)unp->unp_addr;
314 		else
315 			from = &sun_noname;
316 		if (ssb_appendaddr(&so2->so_rcv, from, m, control)) {
317 			sorwakeup(so2);
318 			m = NULL;
319 			control = NULL;
320 		} else {
321 			error = ENOBUFS;
322 		}
323 		if (nam)
324 			unp_disconnect(unp);
325 		break;
326 	}
327 
328 	case SOCK_STREAM:
329 		/* Connect if not connected yet. */
330 		/*
331 		 * Note: A better implementation would complain
332 		 * if not equal to the peer's address.
333 		 */
334 		if (!(so->so_state & SS_ISCONNECTED)) {
335 			if (nam) {
336 				error = unp_connect(so, nam, td);
337 				if (error)
338 					break;	/* XXX */
339 			} else {
340 				error = ENOTCONN;
341 				break;
342 			}
343 		}
344 
345 		if (so->so_state & SS_CANTSENDMORE) {
346 			error = EPIPE;
347 			break;
348 		}
349 		if (unp->unp_conn == NULL)
350 			panic("uipc_send connected but no connection?");
351 		so2 = unp->unp_conn->unp_socket;
352 		/*
353 		 * Send to paired receive port, and then reduce
354 		 * send buffer hiwater marks to maintain backpressure.
355 		 * Wake up readers.
356 		 */
357 		if (control) {
358 			if (ssb_appendcontrol(&so2->so_rcv, m, control))
359 				control = NULL;
360 		} else {
361 			sbappend(&so2->so_rcv.sb, m);
362 		}
363 		so->so_snd.ssb_mbmax -=
364 			so2->so_rcv.ssb_mbcnt - unp->unp_conn->unp_mbcnt;
365 		unp->unp_conn->unp_mbcnt = so2->so_rcv.ssb_mbcnt;
366 		newhiwat = so->so_snd.ssb_hiwat -
367 		    (so2->so_rcv.ssb_cc - unp->unp_conn->unp_cc);
368 		chgsbsize(so->so_cred->cr_uidinfo, &so->so_snd.ssb_hiwat,
369 		    newhiwat, RLIM_INFINITY);
370 		unp->unp_conn->unp_cc = so2->so_rcv.ssb_cc;
371 		sorwakeup(so2);
372 		m = NULL;
373 		break;
374 
375 	default:
376 		panic("uipc_send unknown socktype");
377 	}
378 
379 	/*
380 	 * SEND_EOF is equivalent to a SEND followed by a SHUTDOWN.
381 	 */
382 	if (flags & PRUS_EOF) {
383 		socantsendmore(so);
384 		unp_shutdown(unp);
385 	}
386 
387 	if (control && error != 0)
388 		unp_dispose(control);
389 
390 release:
391 	if (control)
392 		m_freem(control);
393 	if (m)
394 		m_freem(m);
395 	return error;
396 }
397 
398 static int
399 uipc_sense(struct socket *so, struct stat *sb)
400 {
401 	struct unpcb *unp = so->so_pcb;
402 	struct socket *so2;
403 
404 	if (unp == NULL)
405 		return EINVAL;
406 	sb->st_blksize = so->so_snd.ssb_hiwat;
407 	if (so->so_type == SOCK_STREAM && unp->unp_conn != NULL) {
408 		so2 = unp->unp_conn->unp_socket;
409 		sb->st_blksize += so2->so_rcv.ssb_cc;
410 	}
411 	sb->st_dev = NOUDEV;
412 	if (unp->unp_ino == 0)		/* make up a non-zero inode number */
413 		unp->unp_ino = (++unp_ino == 0) ? ++unp_ino : unp_ino;
414 	sb->st_ino = unp->unp_ino;
415 	return (0);
416 }
417 
418 static int
419 uipc_shutdown(struct socket *so)
420 {
421 	struct unpcb *unp = so->so_pcb;
422 
423 	if (unp == NULL)
424 		return EINVAL;
425 	socantsendmore(so);
426 	unp_shutdown(unp);
427 	return 0;
428 }
429 
430 static int
431 uipc_sockaddr(struct socket *so, struct sockaddr **nam)
432 {
433 	struct unpcb *unp = so->so_pcb;
434 
435 	if (unp == NULL)
436 		return EINVAL;
437 	if (unp->unp_addr)
438 		*nam = dup_sockaddr((struct sockaddr *)unp->unp_addr);
439 	return 0;
440 }
441 
442 struct pr_usrreqs uipc_usrreqs = {
443 	.pru_abort = uipc_abort,
444 	.pru_accept = uipc_accept,
445 	.pru_attach = uipc_attach,
446 	.pru_bind = uipc_bind,
447 	.pru_connect = uipc_connect,
448 	.pru_connect2 = uipc_connect2,
449 	.pru_control = pru_control_notsupp,
450 	.pru_detach = uipc_detach,
451 	.pru_disconnect = uipc_disconnect,
452 	.pru_listen = uipc_listen,
453 	.pru_peeraddr = uipc_peeraddr,
454 	.pru_rcvd = uipc_rcvd,
455 	.pru_rcvoob = pru_rcvoob_notsupp,
456 	.pru_send = uipc_send,
457 	.pru_sense = uipc_sense,
458 	.pru_shutdown = uipc_shutdown,
459 	.pru_sockaddr = uipc_sockaddr,
460 	.pru_sosend = sosend,
461 	.pru_soreceive = soreceive,
462 	.pru_sopoll = sopoll
463 };
464 
465 int
466 uipc_ctloutput(struct socket *so, struct sockopt *sopt)
467 {
468 	struct unpcb *unp = so->so_pcb;
469 	int error;
470 
471 	switch (sopt->sopt_dir) {
472 	case SOPT_GET:
473 		switch (sopt->sopt_name) {
474 		case LOCAL_PEERCRED:
475 			if (unp->unp_flags & UNP_HAVEPC)
476 				error = sooptcopyout(sopt, &unp->unp_peercred,
477 				    sizeof(unp->unp_peercred));
478 			else {
479 				if (so->so_type == SOCK_STREAM)
480 					error = ENOTCONN;
481 				else
482 					error = EINVAL;
483 			}
484 			break;
485 		default:
486 			error = EOPNOTSUPP;
487 			break;
488 		}
489 		break;
490 	case SOPT_SET:
491 	default:
492 		error = EOPNOTSUPP;
493 		break;
494 	}
495 	return (error);
496 }
497 
498 /*
499  * Both send and receive buffers are allocated PIPSIZ bytes of buffering
500  * for stream sockets, although the total for sender and receiver is
501  * actually only PIPSIZ.
502  * Datagram sockets really use the sendspace as the maximum datagram size,
503  * and don't really want to reserve the sendspace.  Their recvspace should
504  * be large enough for at least one max-size datagram plus address.
505  */
506 #ifndef PIPSIZ
507 #define	PIPSIZ	8192
508 #endif
509 static u_long	unpst_sendspace = PIPSIZ;
510 static u_long	unpst_recvspace = PIPSIZ;
511 static u_long	unpdg_sendspace = 2*1024;	/* really max datagram size */
512 static u_long	unpdg_recvspace = 4*1024;
513 
514 static int	unp_rights;			/* file descriptors in flight */
515 static struct spinlock unp_spin = SPINLOCK_INITIALIZER(&unp_spin);
516 
517 SYSCTL_DECL(_net_local_stream);
518 SYSCTL_INT(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW,
519 	   &unpst_sendspace, 0, "");
520 SYSCTL_INT(_net_local_stream, OID_AUTO, recvspace, CTLFLAG_RW,
521 	   &unpst_recvspace, 0, "");
522 
523 SYSCTL_DECL(_net_local_dgram);
524 SYSCTL_INT(_net_local_dgram, OID_AUTO, maxdgram, CTLFLAG_RW,
525 	   &unpdg_sendspace, 0, "");
526 SYSCTL_INT(_net_local_dgram, OID_AUTO, recvspace, CTLFLAG_RW,
527 	   &unpdg_recvspace, 0, "");
528 
529 SYSCTL_DECL(_net_local);
530 SYSCTL_INT(_net_local, OID_AUTO, inflight, CTLFLAG_RD, &unp_rights, 0, "");
531 
532 static int
533 unp_attach(struct socket *so, struct pru_attach_info *ai)
534 {
535 	struct unpcb *unp;
536 	int error;
537 
538 	if (so->so_snd.ssb_hiwat == 0 || so->so_rcv.ssb_hiwat == 0) {
539 		switch (so->so_type) {
540 
541 		case SOCK_STREAM:
542 			error = soreserve(so, unpst_sendspace, unpst_recvspace,
543 					  ai->sb_rlimit);
544 			break;
545 
546 		case SOCK_DGRAM:
547 			error = soreserve(so, unpdg_sendspace, unpdg_recvspace,
548 					  ai->sb_rlimit);
549 			break;
550 
551 		default:
552 			panic("unp_attach");
553 		}
554 		if (error)
555 			return (error);
556 	}
557 	unp = zalloc(unp_zone);
558 	if (unp == NULL)
559 		return (ENOBUFS);
560 	bzero(unp, sizeof *unp);
561 	unp->unp_gencnt = ++unp_gencnt;
562 	unp_count++;
563 	LIST_INIT(&unp->unp_refs);
564 	unp->unp_socket = so;
565 	unp->unp_rvnode = ai->fd_rdir;		/* jail cruft XXX JH */
566 	LIST_INSERT_HEAD(so->so_type == SOCK_DGRAM ? &unp_dhead
567 			 : &unp_shead, unp, unp_link);
568 	so->so_pcb = (caddr_t)unp;
569 	return (0);
570 }
571 
572 static void
573 unp_detach(struct unpcb *unp)
574 {
575 	LIST_REMOVE(unp, unp_link);
576 	unp->unp_gencnt = ++unp_gencnt;
577 	--unp_count;
578 	if (unp->unp_vnode) {
579 		unp->unp_vnode->v_socket = NULL;
580 		vrele(unp->unp_vnode);
581 		unp->unp_vnode = NULL;
582 	}
583 	if (unp->unp_conn)
584 		unp_disconnect(unp);
585 	while (!LIST_EMPTY(&unp->unp_refs))
586 		unp_drop(LIST_FIRST(&unp->unp_refs), ECONNRESET);
587 	soisdisconnected(unp->unp_socket);
588 	unp->unp_socket->so_pcb = NULL;
589 	if (unp_rights) {
590 		/*
591 		 * Normally the receive buffer is flushed later,
592 		 * in sofree, but if our receive buffer holds references
593 		 * to descriptors that are now garbage, we will dispose
594 		 * of those descriptor references after the garbage collector
595 		 * gets them (resulting in a "panic: closef: count < 0").
596 		 */
597 		sorflush(unp->unp_socket);
598 		unp_gc();
599 	}
600 	if (unp->unp_addr)
601 		kfree(unp->unp_addr, M_SONAME);
602 	zfree(unp_zone, unp);
603 }
604 
605 static int
606 unp_bind(struct unpcb *unp, struct sockaddr *nam, struct thread *td)
607 {
608 	struct proc *p = td->td_proc;
609 	struct sockaddr_un *soun = (struct sockaddr_un *)nam;
610 	struct vnode *vp;
611 	struct vattr vattr;
612 	int error, namelen;
613 	struct nlookupdata nd;
614 	char buf[SOCK_MAXADDRLEN];
615 
616 	if (unp->unp_vnode != NULL)
617 		return (EINVAL);
618 	namelen = soun->sun_len - offsetof(struct sockaddr_un, sun_path);
619 	if (namelen <= 0)
620 		return (EINVAL);
621 	strncpy(buf, soun->sun_path, namelen);
622 	buf[namelen] = 0;	/* null-terminate the string */
623 	error = nlookup_init(&nd, buf, UIO_SYSSPACE, NLC_LOCKVP|NLC_CREATE);
624 	if (error == 0)
625 		error = nlookup(&nd);
626 	if (error == 0 && nd.nl_nch.ncp->nc_vp != NULL)
627 		error = EADDRINUSE;
628 	if (error)
629 		goto done;
630 
631 	VATTR_NULL(&vattr);
632 	vattr.va_type = VSOCK;
633 	vattr.va_mode = (ACCESSPERMS & ~p->p_fd->fd_cmask);
634 	error = VOP_NCREATE(&nd.nl_nch, &vp, nd.nl_cred, &vattr);
635 	if (error == 0) {
636 		vp->v_socket = unp->unp_socket;
637 		unp->unp_vnode = vp;
638 		unp->unp_addr = (struct sockaddr_un *)dup_sockaddr(nam);
639 		vn_unlock(vp);
640 	}
641 done:
642 	nlookup_done(&nd);
643 	return (error);
644 }
645 
646 static int
647 unp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
648 {
649 	struct proc *p = td->td_proc;
650 	struct sockaddr_un *soun = (struct sockaddr_un *)nam;
651 	struct vnode *vp;
652 	struct socket *so2, *so3;
653 	struct unpcb *unp, *unp2, *unp3;
654 	int error, len;
655 	struct nlookupdata nd;
656 	char buf[SOCK_MAXADDRLEN];
657 
658 	KKASSERT(p);
659 
660 	len = nam->sa_len - offsetof(struct sockaddr_un, sun_path);
661 	if (len <= 0)
662 		return EINVAL;
663 	strncpy(buf, soun->sun_path, len);
664 	buf[len] = 0;
665 
666 	vp = NULL;
667 	error = nlookup_init(&nd, buf, UIO_SYSSPACE, NLC_FOLLOW);
668 	if (error == 0)
669 		error = nlookup(&nd);
670 	if (error == 0)
671 		error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
672 	nlookup_done(&nd);
673 	if (error)
674 		return (error);
675 
676 	if (vp->v_type != VSOCK) {
677 		error = ENOTSOCK;
678 		goto bad;
679 	}
680 	error = VOP_ACCESS(vp, VWRITE, p->p_ucred);
681 	if (error)
682 		goto bad;
683 	so2 = vp->v_socket;
684 	if (so2 == NULL) {
685 		error = ECONNREFUSED;
686 		goto bad;
687 	}
688 	if (so->so_type != so2->so_type) {
689 		error = EPROTOTYPE;
690 		goto bad;
691 	}
692 	if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
693 		if (!(so2->so_options & SO_ACCEPTCONN) ||
694 		    (so3 = sonewconn(so2, 0)) == NULL) {
695 			error = ECONNREFUSED;
696 			goto bad;
697 		}
698 		unp = so->so_pcb;
699 		unp2 = so2->so_pcb;
700 		unp3 = so3->so_pcb;
701 		if (unp2->unp_addr)
702 			unp3->unp_addr = (struct sockaddr_un *)
703 				dup_sockaddr((struct sockaddr *)unp2->unp_addr);
704 
705 		/*
706 		 * unp_peercred management:
707 		 *
708 		 * The connecter's (client's) credentials are copied
709 		 * from its process structure at the time of connect()
710 		 * (which is now).
711 		 */
712 		cru2x(p->p_ucred, &unp3->unp_peercred);
713 		unp3->unp_flags |= UNP_HAVEPC;
714 		/*
715 		 * The receiver's (server's) credentials are copied
716 		 * from the unp_peercred member of socket on which the
717 		 * former called listen(); unp_listen() cached that
718 		 * process's credentials at that time so we can use
719 		 * them now.
720 		 */
721 		KASSERT(unp2->unp_flags & UNP_HAVEPCCACHED,
722 		    ("unp_connect: listener without cached peercred"));
723 		memcpy(&unp->unp_peercred, &unp2->unp_peercred,
724 		    sizeof(unp->unp_peercred));
725 		unp->unp_flags |= UNP_HAVEPC;
726 
727 		so2 = so3;
728 	}
729 	error = unp_connect2(so, so2);
730 bad:
731 	vput(vp);
732 	return (error);
733 }
734 
735 int
736 unp_connect2(struct socket *so, struct socket *so2)
737 {
738 	struct unpcb *unp = so->so_pcb;
739 	struct unpcb *unp2;
740 
741 	if (so2->so_type != so->so_type)
742 		return (EPROTOTYPE);
743 	unp2 = so2->so_pcb;
744 	unp->unp_conn = unp2;
745 	switch (so->so_type) {
746 
747 	case SOCK_DGRAM:
748 		LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink);
749 		soisconnected(so);
750 		break;
751 
752 	case SOCK_STREAM:
753 		unp2->unp_conn = unp;
754 		soisconnected(so);
755 		soisconnected(so2);
756 		break;
757 
758 	default:
759 		panic("unp_connect2");
760 	}
761 	return (0);
762 }
763 
764 static void
765 unp_disconnect(struct unpcb *unp)
766 {
767 	struct unpcb *unp2 = unp->unp_conn;
768 
769 	if (unp2 == NULL)
770 		return;
771 
772 	unp->unp_conn = NULL;
773 
774 	switch (unp->unp_socket->so_type) {
775 	case SOCK_DGRAM:
776 		LIST_REMOVE(unp, unp_reflink);
777 		unp->unp_socket->so_state &= ~SS_ISCONNECTED;
778 		break;
779 	case SOCK_STREAM:
780 		soisdisconnected(unp->unp_socket);
781 		unp2->unp_conn = NULL;
782 		soisdisconnected(unp2->unp_socket);
783 		break;
784 	}
785 }
786 
787 #ifdef notdef
788 void
789 unp_abort(struct unpcb *unp)
790 {
791 
792 	unp_detach(unp);
793 }
794 #endif
795 
796 static int
797 prison_unpcb(struct thread *td, struct unpcb *unp)
798 {
799 	struct proc *p;
800 
801 	if (td == NULL)
802 		return (0);
803 	if ((p = td->td_proc) == NULL)
804 		return (0);
805 	if (!p->p_ucred->cr_prison)
806 		return (0);
807 	if (p->p_fd->fd_rdir == unp->unp_rvnode)
808 		return (0);
809 	return (1);
810 }
811 
812 static int
813 unp_pcblist(SYSCTL_HANDLER_ARGS)
814 {
815 	int error, i, n;
816 	struct unpcb *unp, **unp_list;
817 	unp_gen_t gencnt;
818 	struct unp_head *head;
819 
820 	head = ((intptr_t)arg1 == SOCK_DGRAM ? &unp_dhead : &unp_shead);
821 
822 	KKASSERT(curproc != NULL);
823 
824 	/*
825 	 * The process of preparing the PCB list is too time-consuming and
826 	 * resource-intensive to repeat twice on every request.
827 	 */
828 	if (req->oldptr == NULL) {
829 		n = unp_count;
830 		req->oldidx = (n + n/8) * sizeof(struct xunpcb);
831 		return 0;
832 	}
833 
834 	if (req->newptr != NULL)
835 		return EPERM;
836 
837 	/*
838 	 * OK, now we're committed to doing something.
839 	 */
840 	gencnt = unp_gencnt;
841 	n = unp_count;
842 
843 	unp_list = kmalloc(n * sizeof *unp_list, M_TEMP, M_WAITOK);
844 	if (unp_list == NULL)
845 		return ENOMEM;
846 
847 	for (unp = LIST_FIRST(head), i = 0; unp && i < n;
848 	     unp = LIST_NEXT(unp, unp_link)) {
849 		if (unp->unp_gencnt <= gencnt && !prison_unpcb(req->td, unp))
850 			unp_list[i++] = unp;
851 	}
852 	n = i;			/* in case we lost some during malloc */
853 
854 	error = 0;
855 	for (i = 0; i < n; i++) {
856 		unp = unp_list[i];
857 		if (unp->unp_gencnt <= gencnt) {
858 			struct xunpcb xu;
859 			xu.xu_len = sizeof xu;
860 			xu.xu_unpp = unp;
861 			/*
862 			 * XXX - need more locking here to protect against
863 			 * connect/disconnect races for SMP.
864 			 */
865 			if (unp->unp_addr)
866 				bcopy(unp->unp_addr, &xu.xu_addr,
867 				      unp->unp_addr->sun_len);
868 			if (unp->unp_conn && unp->unp_conn->unp_addr)
869 				bcopy(unp->unp_conn->unp_addr,
870 				      &xu.xu_caddr,
871 				      unp->unp_conn->unp_addr->sun_len);
872 			bcopy(unp, &xu.xu_unp, sizeof *unp);
873 			sotoxsocket(unp->unp_socket, &xu.xu_socket);
874 			error = SYSCTL_OUT(req, &xu, sizeof xu);
875 		}
876 	}
877 	kfree(unp_list, M_TEMP);
878 	return error;
879 }
880 
881 SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLFLAG_RD,
882 	    (caddr_t)(long)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb",
883 	    "List of active local datagram sockets");
884 SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, CTLFLAG_RD,
885 	    (caddr_t)(long)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb",
886 	    "List of active local stream sockets");
887 
888 static void
889 unp_shutdown(struct unpcb *unp)
890 {
891 	struct socket *so;
892 
893 	if (unp->unp_socket->so_type == SOCK_STREAM && unp->unp_conn != NULL &&
894 	    (so = unp->unp_conn->unp_socket))
895 		socantrcvmore(so);
896 }
897 
898 static void
899 unp_drop(struct unpcb *unp, int err)
900 {
901 	struct socket *so = unp->unp_socket;
902 
903 	so->so_error = err;
904 	unp_disconnect(unp);
905 }
906 
907 #ifdef notdef
908 void
909 unp_drain(void)
910 {
911 
912 }
913 #endif
914 
915 int
916 unp_externalize(struct mbuf *rights)
917 {
918 	struct proc *p = curproc;		/* XXX */
919 	int i;
920 	struct cmsghdr *cm = mtod(rights, struct cmsghdr *);
921 	int *fdp;
922 	struct file **rp;
923 	struct file *fp;
924 	int newfds = (cm->cmsg_len - (CMSG_DATA(cm) - (u_char *)cm))
925 		/ sizeof (struct file *);
926 	int f;
927 
928 	/*
929 	 * if the new FD's will not fit, then we free them all
930 	 */
931 	if (!fdavail(p, newfds)) {
932 		rp = (struct file **)CMSG_DATA(cm);
933 		for (i = 0; i < newfds; i++) {
934 			fp = *rp;
935 			/*
936 			 * zero the pointer before calling unp_discard,
937 			 * since it may end up in unp_gc()..
938 			 */
939 			*rp++ = 0;
940 			unp_discard(fp, NULL);
941 		}
942 		return (EMSGSIZE);
943 	}
944 	/*
945 	 * now change each pointer to an fd in the global table to
946 	 * an integer that is the index to the local fd table entry
947 	 * that we set up to point to the global one we are transferring.
948 	 * If sizeof (struct file *) is bigger than or equal to sizeof int,
949 	 * then do it in forward order. In that case, an integer will
950 	 * always come in the same place or before its corresponding
951 	 * struct file pointer.
952 	 * If sizeof (struct file *) is smaller than sizeof int, then
953 	 * do it in reverse order.
954 	 */
955 	if (sizeof (struct file *) >= sizeof (int)) {
956 		fdp = (int *)(cm + 1);
957 		rp = (struct file **)CMSG_DATA(cm);
958 		for (i = 0; i < newfds; i++) {
959 			if (fdalloc(p, 0, &f))
960 				panic("unp_externalize");
961 			fp = *rp++;
962 			fsetfd(p, fp, f);
963 			fdrop(fp);
964 			spin_lock_wr(&unp_spin);
965 			fp->f_msgcount--;
966 			unp_rights--;
967 			spin_unlock_wr(&unp_spin);
968 			*fdp++ = f;
969 		}
970 	} else {
971 		fdp = (int *)(cm + 1) + newfds - 1;
972 		rp = (struct file **)CMSG_DATA(cm) + newfds - 1;
973 		for (i = 0; i < newfds; i++) {
974 			if (fdalloc(p, 0, &f))
975 				panic("unp_externalize");
976 			fp = *rp--;
977 			fsetfd(p, fp, f);
978 			fdrop(fp);
979 			spin_lock_wr(&unp_spin);
980 			fp->f_msgcount--;
981 			unp_rights--;
982 			spin_unlock_wr(&unp_spin);
983 			*fdp-- = f;
984 		}
985 	}
986 
987 	/*
988 	 * Adjust length, in case sizeof(struct file *) and sizeof(int)
989 	 * differs.
990 	 */
991 	cm->cmsg_len = CMSG_LEN(newfds * sizeof(int));
992 	rights->m_len = cm->cmsg_len;
993 	return (0);
994 }
995 
996 void
997 unp_init(void)
998 {
999 	unp_zone = zinit("unpcb", sizeof(struct unpcb), nmbclusters, 0, 0);
1000 	if (unp_zone == NULL)
1001 		panic("unp_init");
1002 	LIST_INIT(&unp_dhead);
1003 	LIST_INIT(&unp_shead);
1004 	spin_init(&unp_spin);
1005 }
1006 
1007 static int
1008 unp_internalize(struct mbuf *control, struct thread *td)
1009 {
1010 	struct proc *p = td->td_proc;
1011 	struct filedesc *fdescp;
1012 	struct cmsghdr *cm = mtod(control, struct cmsghdr *);
1013 	struct file **rp;
1014 	struct file *fp;
1015 	int i, fd, *fdp;
1016 	struct cmsgcred *cmcred;
1017 	int oldfds;
1018 	u_int newlen;
1019 
1020 	KKASSERT(p);
1021 	fdescp = p->p_fd;
1022 	if ((cm->cmsg_type != SCM_RIGHTS && cm->cmsg_type != SCM_CREDS) ||
1023 	    cm->cmsg_level != SOL_SOCKET || cm->cmsg_len != control->m_len)
1024 		return (EINVAL);
1025 
1026 	/*
1027 	 * Fill in credential information.
1028 	 */
1029 	if (cm->cmsg_type == SCM_CREDS) {
1030 		cmcred = (struct cmsgcred *)(cm + 1);
1031 		cmcred->cmcred_pid = p->p_pid;
1032 		cmcred->cmcred_uid = p->p_ucred->cr_ruid;
1033 		cmcred->cmcred_gid = p->p_ucred->cr_rgid;
1034 		cmcred->cmcred_euid = p->p_ucred->cr_uid;
1035 		cmcred->cmcred_ngroups = MIN(p->p_ucred->cr_ngroups,
1036 							CMGROUP_MAX);
1037 		for (i = 0; i < cmcred->cmcred_ngroups; i++)
1038 			cmcred->cmcred_groups[i] = p->p_ucred->cr_groups[i];
1039 		return(0);
1040 	}
1041 
1042 	oldfds = (cm->cmsg_len - sizeof (*cm)) / sizeof (int);
1043 	/*
1044 	 * check that all the FDs passed in refer to legal OPEN files
1045 	 * If not, reject the entire operation.
1046 	 */
1047 	fdp = (int *)(cm + 1);
1048 	for (i = 0; i < oldfds; i++) {
1049 		fd = *fdp++;
1050 		if ((unsigned)fd >= fdescp->fd_nfiles ||
1051 		    fdescp->fd_files[fd].fp == NULL)
1052 			return (EBADF);
1053 		if (fdescp->fd_files[fd].fp->f_type == DTYPE_KQUEUE)
1054 			return (EOPNOTSUPP);
1055 	}
1056 	/*
1057 	 * Now replace the integer FDs with pointers to
1058 	 * the associated global file table entry..
1059 	 * Allocate a bigger buffer as necessary. But if an cluster is not
1060 	 * enough, return E2BIG.
1061 	 */
1062 	newlen = CMSG_LEN(oldfds * sizeof(struct file *));
1063 	if (newlen > MCLBYTES)
1064 		return (E2BIG);
1065 	if (newlen - control->m_len > M_TRAILINGSPACE(control)) {
1066 		if (control->m_flags & M_EXT)
1067 			return (E2BIG);
1068 		MCLGET(control, MB_WAIT);
1069 		if (!(control->m_flags & M_EXT))
1070 			return (ENOBUFS);
1071 
1072 		/* copy the data to the cluster */
1073 		memcpy(mtod(control, char *), cm, cm->cmsg_len);
1074 		cm = mtod(control, struct cmsghdr *);
1075 	}
1076 
1077 	/*
1078 	 * Adjust length, in case sizeof(struct file *) and sizeof(int)
1079 	 * differs.
1080 	 */
1081 	control->m_len = cm->cmsg_len = newlen;
1082 
1083 	/*
1084 	 * Transform the file descriptors into struct file pointers.
1085 	 * If sizeof (struct file *) is bigger than or equal to sizeof int,
1086 	 * then do it in reverse order so that the int won't get until
1087 	 * we're done.
1088 	 * If sizeof (struct file *) is smaller than sizeof int, then
1089 	 * do it in forward order.
1090 	 */
1091 	if (sizeof (struct file *) >= sizeof (int)) {
1092 		fdp = (int *)(cm + 1) + oldfds - 1;
1093 		rp = (struct file **)CMSG_DATA(cm) + oldfds - 1;
1094 		for (i = 0; i < oldfds; i++) {
1095 			fp = fdescp->fd_files[*fdp--].fp;
1096 			*rp-- = fp;
1097 			fhold(fp);
1098 			spin_lock_wr(&unp_spin);
1099 			fp->f_msgcount++;
1100 			unp_rights++;
1101 			spin_unlock_wr(&unp_spin);
1102 		}
1103 	} else {
1104 		fdp = (int *)(cm + 1);
1105 		rp = (struct file **)CMSG_DATA(cm);
1106 		for (i = 0; i < oldfds; i++) {
1107 			fp = fdescp->fd_files[*fdp++].fp;
1108 			*rp++ = fp;
1109 			fhold(fp);
1110 			spin_lock_wr(&unp_spin);
1111 			fp->f_msgcount++;
1112 			unp_rights++;
1113 			spin_unlock_wr(&unp_spin);
1114 		}
1115 	}
1116 	return (0);
1117 }
1118 
1119 /*
1120  * Garbage collect in-transit file descriptors that get lost due to
1121  * loops (i.e. when a socket is sent to another process over itself,
1122  * and more complex situations).
1123  *
1124  * NOT MPSAFE - TODO socket flush code and maybe closef.  Rest is MPSAFE.
1125  */
1126 
1127 struct unp_gc_info {
1128 	struct file **extra_ref;
1129 	struct file *locked_fp;
1130 	int defer;
1131 	int index;
1132 	int maxindex;
1133 };
1134 
1135 static void
1136 unp_gc(void)
1137 {
1138 	struct unp_gc_info info;
1139 	static boolean_t unp_gcing;
1140 	struct file **fpp;
1141 	int i;
1142 
1143 	spin_lock_wr(&unp_spin);
1144 	if (unp_gcing) {
1145 		spin_unlock_wr(&unp_spin);
1146 		return;
1147 	}
1148 	unp_gcing = TRUE;
1149 	spin_unlock_wr(&unp_spin);
1150 
1151 	/*
1152 	 * before going through all this, set all FDs to
1153 	 * be NOT defered and NOT externally accessible
1154 	 */
1155 	info.defer = 0;
1156 	allfiles_scan_exclusive(unp_gc_clearmarks, NULL);
1157 	do {
1158 		allfiles_scan_exclusive(unp_gc_checkmarks, &info);
1159 	} while (info.defer);
1160 
1161 	/*
1162 	 * We grab an extra reference to each of the file table entries
1163 	 * that are not otherwise accessible and then free the rights
1164 	 * that are stored in messages on them.
1165 	 *
1166 	 * The bug in the orginal code is a little tricky, so I'll describe
1167 	 * what's wrong with it here.
1168 	 *
1169 	 * It is incorrect to simply unp_discard each entry for f_msgcount
1170 	 * times -- consider the case of sockets A and B that contain
1171 	 * references to each other.  On a last close of some other socket,
1172 	 * we trigger a gc since the number of outstanding rights (unp_rights)
1173 	 * is non-zero.  If during the sweep phase the gc code un_discards,
1174 	 * we end up doing a (full) closef on the descriptor.  A closef on A
1175 	 * results in the following chain.  Closef calls soo_close, which
1176 	 * calls soclose.   Soclose calls first (through the switch
1177 	 * uipc_usrreq) unp_detach, which re-invokes unp_gc.  Unp_gc simply
1178 	 * returns because the previous instance had set unp_gcing, and
1179 	 * we return all the way back to soclose, which marks the socket
1180 	 * with SS_NOFDREF, and then calls sofree.  Sofree calls sorflush
1181 	 * to free up the rights that are queued in messages on the socket A,
1182 	 * i.e., the reference on B.  The sorflush calls via the dom_dispose
1183 	 * switch unp_dispose, which unp_scans with unp_discard.  This second
1184 	 * instance of unp_discard just calls closef on B.
1185 	 *
1186 	 * Well, a similar chain occurs on B, resulting in a sorflush on B,
1187 	 * which results in another closef on A.  Unfortunately, A is already
1188 	 * being closed, and the descriptor has already been marked with
1189 	 * SS_NOFDREF, and soclose panics at this point.
1190 	 *
1191 	 * Here, we first take an extra reference to each inaccessible
1192 	 * descriptor.  Then, we call sorflush ourself, since we know
1193 	 * it is a Unix domain socket anyhow.  After we destroy all the
1194 	 * rights carried in messages, we do a last closef to get rid
1195 	 * of our extra reference.  This is the last close, and the
1196 	 * unp_detach etc will shut down the socket.
1197 	 *
1198 	 * 91/09/19, bsy@cs.cmu.edu
1199 	 */
1200 	info.extra_ref = kmalloc(256 * sizeof(struct file *), M_FILE, M_WAITOK);
1201 	info.maxindex = 256;
1202 
1203 	do {
1204 		/*
1205 		 * Look for matches
1206 		 */
1207 		info.index = 0;
1208 		allfiles_scan_exclusive(unp_gc_checkrefs, &info);
1209 
1210 		/*
1211 		 * For each FD on our hit list, do the following two things
1212 		 */
1213 		for (i = info.index, fpp = info.extra_ref; --i >= 0; ++fpp) {
1214 			struct file *tfp = *fpp;
1215 			if (tfp->f_type == DTYPE_SOCKET && tfp->f_data != NULL)
1216 				sorflush((struct socket *)(tfp->f_data));
1217 		}
1218 		for (i = info.index, fpp = info.extra_ref; --i >= 0; ++fpp)
1219 			closef(*fpp, NULL);
1220 	} while (info.index == info.maxindex);
1221 	kfree((caddr_t)info.extra_ref, M_FILE);
1222 	unp_gcing = FALSE;
1223 }
1224 
1225 /*
1226  * MPSAFE - NOTE: filehead list and file pointer spinlocked on entry
1227  */
1228 static int
1229 unp_gc_checkrefs(struct file *fp, void *data)
1230 {
1231 	struct unp_gc_info *info = data;
1232 
1233 	if (fp->f_count == 0)
1234 		return(0);
1235 	if (info->index == info->maxindex)
1236 		return(-1);
1237 
1238 	/*
1239 	 * If all refs are from msgs, and it's not marked accessible
1240 	 * then it must be referenced from some unreachable cycle
1241 	 * of (shut-down) FDs, so include it in our
1242 	 * list of FDs to remove
1243 	 */
1244 	if (fp->f_count == fp->f_msgcount && !(fp->f_flag & FMARK)) {
1245 		info->extra_ref[info->index++] = fp;
1246 		fhold(fp);
1247 	}
1248 	return(0);
1249 }
1250 
1251 /*
1252  * MPSAFE - NOTE: filehead list and file pointer spinlocked on entry
1253  */
1254 static int
1255 unp_gc_clearmarks(struct file *fp, void *data __unused)
1256 {
1257 	fp->f_flag &= ~(FMARK|FDEFER);
1258 	return(0);
1259 }
1260 
1261 /*
1262  * MPSAFE - NOTE: filehead list and file pointer spinlocked on entry
1263  */
1264 static int
1265 unp_gc_checkmarks(struct file *fp, void *data)
1266 {
1267 	struct unp_gc_info *info = data;
1268 	struct socket *so;
1269 
1270 	/*
1271 	 * If the file is not open, skip it
1272 	 */
1273 	if (fp->f_count == 0)
1274 		return(0);
1275 	/*
1276 	 * If we already marked it as 'defer'  in a
1277 	 * previous pass, then try process it this time
1278 	 * and un-mark it
1279 	 */
1280 	if (fp->f_flag & FDEFER) {
1281 		fp->f_flag &= ~FDEFER;
1282 		--info->defer;
1283 	} else {
1284 		/*
1285 		 * if it's not defered, then check if it's
1286 		 * already marked.. if so skip it
1287 		 */
1288 		if (fp->f_flag & FMARK)
1289 			return(0);
1290 		/*
1291 		 * If all references are from messages
1292 		 * in transit, then skip it. it's not
1293 		 * externally accessible.
1294 		 */
1295 		if (fp->f_count == fp->f_msgcount)
1296 			return(0);
1297 		/*
1298 		 * If it got this far then it must be
1299 		 * externally accessible.
1300 		 */
1301 		fp->f_flag |= FMARK;
1302 	}
1303 	/*
1304 	 * either it was defered, or it is externally
1305 	 * accessible and not already marked so.
1306 	 * Now check if it is possibly one of OUR sockets.
1307 	 */
1308 	if (fp->f_type != DTYPE_SOCKET ||
1309 	    (so = (struct socket *)fp->f_data) == NULL)
1310 		return(0);
1311 	if (so->so_proto->pr_domain != &localdomain ||
1312 	    !(so->so_proto->pr_flags & PR_RIGHTS))
1313 		return(0);
1314 #ifdef notdef
1315 	XXX note: exclusive fp->f_spin lock held
1316 	if (so->so_rcv.sb_flags & SB_LOCK) {
1317 		/*
1318 		 * This is problematical; it's not clear
1319 		 * we need to wait for the sockbuf to be
1320 		 * unlocked (on a uniprocessor, at least),
1321 		 * and it's also not clear what to do
1322 		 * if sbwait returns an error due to receipt
1323 		 * of a signal.  If sbwait does return
1324 		 * an error, we'll go into an infinite
1325 		 * loop.  Delete all of this for now.
1326 		 */
1327 		sbwait(&so->so_rcv);
1328 		goto restart;
1329 	}
1330 #endif
1331 	/*
1332 	 * So, Ok, it's one of our sockets and it IS externally
1333 	 * accessible (or was defered). Now we look
1334 	 * to see if we hold any file descriptors in its
1335 	 * message buffers. Follow those links and mark them
1336 	 * as accessible too.
1337 	 */
1338 	info->locked_fp = fp;
1339 /*	spin_lock_wr(&so->so_rcv.sb_spin); */
1340 	unp_scan(so->so_rcv.ssb_mb, unp_mark, info);
1341 /*	spin_unlock_wr(&so->so_rcv.sb_spin);*/
1342 	return (0);
1343 }
1344 
1345 void
1346 unp_dispose(struct mbuf *m)
1347 {
1348 	if (m)
1349 		unp_scan(m, unp_discard, NULL);
1350 }
1351 
1352 static int
1353 unp_listen(struct unpcb *unp, struct thread *td)
1354 {
1355 	struct proc *p = td->td_proc;
1356 
1357 	KKASSERT(p);
1358 	cru2x(p->p_ucred, &unp->unp_peercred);
1359 	unp->unp_flags |= UNP_HAVEPCCACHED;
1360 	return (0);
1361 }
1362 
1363 static void
1364 unp_scan(struct mbuf *m0, void (*op)(struct file *, void *), void *data)
1365 {
1366 	struct mbuf *m;
1367 	struct file **rp;
1368 	struct cmsghdr *cm;
1369 	int i;
1370 	int qfds;
1371 
1372 	while (m0) {
1373 		for (m = m0; m; m = m->m_next) {
1374 			if (m->m_type == MT_CONTROL &&
1375 			    m->m_len >= sizeof(*cm)) {
1376 				cm = mtod(m, struct cmsghdr *);
1377 				if (cm->cmsg_level != SOL_SOCKET ||
1378 				    cm->cmsg_type != SCM_RIGHTS)
1379 					continue;
1380 				qfds = (cm->cmsg_len -
1381 					(CMSG_DATA(cm) - (u_char *)cm))
1382 						/ sizeof (struct file *);
1383 				rp = (struct file **)CMSG_DATA(cm);
1384 				for (i = 0; i < qfds; i++)
1385 					(*op)(*rp++, data);
1386 				break;		/* XXX, but saves time */
1387 			}
1388 		}
1389 		m0 = m0->m_nextpkt;
1390 	}
1391 }
1392 
1393 static void
1394 unp_mark(struct file *fp, void *data)
1395 {
1396 	struct unp_gc_info *info = data;
1397 
1398 	if (info->locked_fp != fp)
1399 		spin_lock_wr(&fp->f_spin);
1400 	if ((fp->f_flag & FMARK) == 0) {
1401 		++info->defer;
1402 		fp->f_flag |= (FMARK|FDEFER);
1403 	}
1404 	if (info->locked_fp != fp)
1405 		spin_unlock_wr(&fp->f_spin);
1406 }
1407 
1408 static void
1409 unp_discard(struct file *fp, void *data __unused)
1410 {
1411 	spin_lock_wr(&unp_spin);
1412 	fp->f_msgcount--;
1413 	unp_rights--;
1414 	spin_unlock_wr(&unp_spin);
1415 	closef(fp, NULL);
1416 }
1417 
1418