uipc_usrreq.c revision 3308
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 *	@(#)uipc_usrreq.c	8.3 (Berkeley) 1/4/94
34 * $Id: uipc_usrreq.c,v 1.4 1994/09/28 19:55:10 phk Exp $
35 */
36
37#include <sys/param.h>
38#include <sys/systm.h>
39#include <sys/proc.h>
40#include <sys/filedesc.h>
41#include <sys/domain.h>
42#include <sys/protosw.h>
43#include <sys/stat.h>
44#include <sys/socket.h>
45#include <sys/socketvar.h>
46#include <sys/unpcb.h>
47#include <sys/un.h>
48#include <sys/namei.h>
49#include <sys/vnode.h>
50#include <sys/file.h>
51#include <sys/stat.h>
52#include <sys/mbuf.h>
53
54/*
55 * Unix communications domain.
56 *
57 * TODO:
58 *	SEQPACKET, RDM
59 *	rethink name space problems
60 *	need a proper out-of-band
61 */
62struct	sockaddr sun_noname = { sizeof(sun_noname), AF_UNIX };
63ino_t	unp_ino;			/* prototype for fake inode numbers */
64
65/*ARGSUSED*/
66int
67uipc_usrreq(so, req, m, nam, control)
68	struct socket *so;
69	int req;
70	struct mbuf *m, *nam, *control;
71{
72	struct unpcb *unp = sotounpcb(so);
73	register struct socket *so2;
74	register int error = 0;
75	struct proc *p = curproc;	/* XXX */
76
77	if (req == PRU_CONTROL)
78		return (EOPNOTSUPP);
79	if (req != PRU_SEND && control && control->m_len) {
80		error = EOPNOTSUPP;
81		goto release;
82	}
83	if (unp == 0 && req != PRU_ATTACH) {
84		error = EINVAL;
85		goto release;
86	}
87	switch (req) {
88
89	case PRU_ATTACH:
90		if (unp) {
91			error = EISCONN;
92			break;
93		}
94		error = unp_attach(so);
95		break;
96
97	case PRU_DETACH:
98		unp_detach(unp);
99		break;
100
101	case PRU_BIND:
102		error = unp_bind(unp, nam, p);
103		break;
104
105	case PRU_LISTEN:
106		if (unp->unp_vnode == 0)
107			error = EINVAL;
108		break;
109
110	case PRU_CONNECT:
111		error = unp_connect(so, nam, p);
112		break;
113
114	case PRU_CONNECT2:
115		error = unp_connect2(so, (struct socket *)nam);
116		break;
117
118	case PRU_DISCONNECT:
119		unp_disconnect(unp);
120		break;
121
122	case PRU_ACCEPT:
123		/*
124		 * Pass back name of connected socket,
125		 * if it was bound and we are still connected
126		 * (our peer may have closed already!).
127		 */
128		if (unp->unp_conn && unp->unp_conn->unp_addr) {
129			nam->m_len = unp->unp_conn->unp_addr->m_len;
130			bcopy(mtod(unp->unp_conn->unp_addr, caddr_t),
131			    mtod(nam, caddr_t), (unsigned)nam->m_len);
132		} else {
133			nam->m_len = sizeof(sun_noname);
134			*(mtod(nam, struct sockaddr *)) = sun_noname;
135		}
136		break;
137
138	case PRU_SHUTDOWN:
139		socantsendmore(so);
140		unp_shutdown(unp);
141		break;
142
143	case PRU_RCVD:
144		switch (so->so_type) {
145
146		case SOCK_DGRAM:
147			panic("uipc 1");
148			/*NOTREACHED*/
149
150		case SOCK_STREAM:
151#define	rcv (&so->so_rcv)
152#define snd (&so2->so_snd)
153			if (unp->unp_conn == 0)
154				break;
155			so2 = unp->unp_conn->unp_socket;
156			/*
157			 * Adjust backpressure on sender
158			 * and wakeup any waiting to write.
159			 */
160			snd->sb_mbmax += unp->unp_mbcnt - rcv->sb_mbcnt;
161			unp->unp_mbcnt = rcv->sb_mbcnt;
162			snd->sb_hiwat += unp->unp_cc - rcv->sb_cc;
163			unp->unp_cc = rcv->sb_cc;
164			sowwakeup(so2);
165#undef snd
166#undef rcv
167			break;
168
169		default:
170			panic("uipc 2");
171		}
172		break;
173
174	case PRU_SEND:
175		if (control && (error = unp_internalize(control, p)))
176			break;
177		switch (so->so_type) {
178
179		case SOCK_DGRAM: {
180			struct sockaddr *from;
181
182			if (nam) {
183				if (unp->unp_conn) {
184					error = EISCONN;
185					break;
186				}
187				error = unp_connect(so, nam, p);
188				if (error)
189					break;
190			} else {
191				if (unp->unp_conn == 0) {
192					error = ENOTCONN;
193					break;
194				}
195			}
196			so2 = unp->unp_conn->unp_socket;
197			if (unp->unp_addr)
198				from = mtod(unp->unp_addr, struct sockaddr *);
199			else
200				from = &sun_noname;
201			if (sbappendaddr(&so2->so_rcv, from, m, control)) {
202				sorwakeup(so2);
203				m = 0;
204				control = 0;
205			} else
206				error = ENOBUFS;
207			if (nam)
208				unp_disconnect(unp);
209			break;
210		}
211
212		case SOCK_STREAM:
213#define	rcv (&so2->so_rcv)
214#define	snd (&so->so_snd)
215			if (so->so_state & SS_CANTSENDMORE) {
216				error = EPIPE;
217				break;
218			}
219			if (unp->unp_conn == 0)
220				panic("uipc 3");
221			so2 = unp->unp_conn->unp_socket;
222			/*
223			 * Send to paired receive port, and then reduce
224			 * send buffer hiwater marks to maintain backpressure.
225			 * Wake up readers.
226			 */
227			if (control) {
228				if (sbappendcontrol(rcv, m, control))
229					control = 0;
230			} else
231				sbappend(rcv, m);
232			snd->sb_mbmax -=
233			    rcv->sb_mbcnt - unp->unp_conn->unp_mbcnt;
234			unp->unp_conn->unp_mbcnt = rcv->sb_mbcnt;
235			snd->sb_hiwat -= rcv->sb_cc - unp->unp_conn->unp_cc;
236			unp->unp_conn->unp_cc = rcv->sb_cc;
237			sorwakeup(so2);
238			m = 0;
239#undef snd
240#undef rcv
241			break;
242
243		default:
244			panic("uipc 4");
245		}
246		break;
247
248	case PRU_ABORT:
249		unp_drop(unp, ECONNABORTED);
250		break;
251
252	case PRU_SENSE:
253		((struct stat *) m)->st_blksize = so->so_snd.sb_hiwat;
254		if (so->so_type == SOCK_STREAM && unp->unp_conn != 0) {
255			so2 = unp->unp_conn->unp_socket;
256			((struct stat *) m)->st_blksize += so2->so_rcv.sb_cc;
257		}
258		((struct stat *) m)->st_dev = NODEV;
259		if (unp->unp_ino == 0)
260			unp->unp_ino = unp_ino++;
261		((struct stat *) m)->st_ino = unp->unp_ino;
262		return (0);
263
264	case PRU_RCVOOB:
265		return (EOPNOTSUPP);
266
267	case PRU_SENDOOB:
268		error = EOPNOTSUPP;
269		break;
270
271	case PRU_SOCKADDR:
272		if (unp->unp_addr) {
273			nam->m_len = unp->unp_addr->m_len;
274			bcopy(mtod(unp->unp_addr, caddr_t),
275			    mtod(nam, caddr_t), (unsigned)nam->m_len);
276		} else
277			nam->m_len = 0;
278		break;
279
280	case PRU_PEERADDR:
281		if (unp->unp_conn && unp->unp_conn->unp_addr) {
282			nam->m_len = unp->unp_conn->unp_addr->m_len;
283			bcopy(mtod(unp->unp_conn->unp_addr, caddr_t),
284			    mtod(nam, caddr_t), (unsigned)nam->m_len);
285		} else
286			nam->m_len = 0;
287		break;
288
289	case PRU_SLOWTIMO:
290		break;
291
292	default:
293		panic("piusrreq");
294	}
295release:
296	if (control)
297		m_freem(control);
298	if (m)
299		m_freem(m);
300	return (error);
301}
302
303/*
304 * Both send and receive buffers are allocated PIPSIZ bytes of buffering
305 * for stream sockets, although the total for sender and receiver is
306 * actually only PIPSIZ.
307 * Datagram sockets really use the sendspace as the maximum datagram size,
308 * and don't really want to reserve the sendspace.  Their recvspace should
309 * be large enough for at least one max-size datagram plus address.
310 */
311#define	PIPSIZ	4096
312u_long	unpst_sendspace = PIPSIZ;
313u_long	unpst_recvspace = PIPSIZ;
314u_long	unpdg_sendspace = 2*1024;	/* really max datagram size */
315u_long	unpdg_recvspace = 4*1024;
316
317int	unp_rights;			/* file descriptors in flight */
318
319int
320unp_attach(so)
321	struct socket *so;
322{
323	register struct mbuf *m;
324	register struct unpcb *unp;
325	int error;
326
327	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
328		switch (so->so_type) {
329
330		case SOCK_STREAM:
331			error = soreserve(so, unpst_sendspace, unpst_recvspace);
332			break;
333
334		case SOCK_DGRAM:
335			error = soreserve(so, unpdg_sendspace, unpdg_recvspace);
336			break;
337
338		default:
339			panic("unp_attach");
340		}
341		if (error)
342			return (error);
343	}
344	m = m_getclr(M_DONTWAIT, MT_PCB);
345	if (m == NULL)
346		return (ENOBUFS);
347	unp = mtod(m, struct unpcb *);
348	so->so_pcb = (caddr_t)unp;
349	unp->unp_socket = so;
350	return (0);
351}
352
353void
354unp_detach(unp)
355	register struct unpcb *unp;
356{
357
358	if (unp->unp_vnode) {
359		unp->unp_vnode->v_socket = 0;
360		vrele(unp->unp_vnode);
361		unp->unp_vnode = 0;
362	}
363	if (unp->unp_conn)
364		unp_disconnect(unp);
365	while (unp->unp_refs)
366		unp_drop(unp->unp_refs, ECONNRESET);
367	soisdisconnected(unp->unp_socket);
368	unp->unp_socket->so_pcb = 0;
369	m_freem(unp->unp_addr);
370	(void) m_free(dtom(unp));
371	if (unp_rights) {
372		/*
373		 * Normally the receive buffer is flushed later,
374		 * in sofree, but if our receive buffer holds references
375		 * to descriptors that are now garbage, we will dispose
376		 * of those descriptor references after the garbage collector
377		 * gets them (resulting in a "panic: closef: count < 0").
378		 */
379		sorflush(unp->unp_socket);
380		unp_gc();
381	}
382}
383
384int
385unp_bind(unp, nam, p)
386	struct unpcb *unp;
387	struct mbuf *nam;
388	struct proc *p;
389{
390	struct sockaddr_un *soun = mtod(nam, struct sockaddr_un *);
391	register struct vnode *vp;
392	struct vattr vattr;
393	int error;
394	struct nameidata nd;
395
396	NDINIT(&nd, CREATE, FOLLOW | LOCKPARENT, UIO_SYSSPACE,
397		soun->sun_path, p);
398	if (unp->unp_vnode != NULL)
399		return (EINVAL);
400	if (nam->m_len == MLEN) {
401		if (*(mtod(nam, caddr_t) + nam->m_len - 1) != 0)
402			return (EINVAL);
403	} else
404		*(mtod(nam, caddr_t) + nam->m_len) = 0;
405/* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */
406	error = namei(&nd);
407	if (error)
408		return (error);
409	vp = nd.ni_vp;
410	if (vp != NULL) {
411		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
412		if (nd.ni_dvp == vp)
413			vrele(nd.ni_dvp);
414		else
415			vput(nd.ni_dvp);
416		vrele(vp);
417		return (EADDRINUSE);
418	}
419	VATTR_NULL(&vattr);
420	vattr.va_type = VSOCK;
421	vattr.va_mode = ACCESSPERMS;
422	LEASE_CHECK(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
423	error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
424	if (error)
425		return (error);
426	vp = nd.ni_vp;
427	vp->v_socket = unp->unp_socket;
428	unp->unp_vnode = vp;
429	unp->unp_addr = m_copy(nam, 0, (int)M_COPYALL);
430	VOP_UNLOCK(vp);
431	return (0);
432}
433
434int
435unp_connect(so, nam, p)
436	struct socket *so;
437	struct mbuf *nam;
438	struct proc *p;
439{
440	register struct sockaddr_un *soun = mtod(nam, struct sockaddr_un *);
441	register struct vnode *vp;
442	register struct socket *so2, *so3;
443	struct unpcb *unp2, *unp3;
444	int error;
445	struct nameidata nd;
446
447	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, soun->sun_path, p);
448	if (nam->m_data + nam->m_len == &nam->m_dat[MLEN]) {	/* XXX */
449		if (*(mtod(nam, caddr_t) + nam->m_len - 1) != 0)
450			return (EMSGSIZE);
451	} else
452		*(mtod(nam, caddr_t) + nam->m_len) = 0;
453	error = namei(&nd);
454	if (error)
455		return (error);
456	vp = nd.ni_vp;
457	if (vp->v_type != VSOCK) {
458		error = ENOTSOCK;
459		goto bad;
460	}
461	error = VOP_ACCESS(vp, VWRITE, p->p_ucred, p);
462	if (error)
463		goto bad;
464	so2 = vp->v_socket;
465	if (so2 == 0) {
466		error = ECONNREFUSED;
467		goto bad;
468	}
469	if (so->so_type != so2->so_type) {
470		error = EPROTOTYPE;
471		goto bad;
472	}
473	if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
474		if ((so2->so_options & SO_ACCEPTCONN) == 0 ||
475		    (so3 = sonewconn(so2, 0)) == 0) {
476			error = ECONNREFUSED;
477			goto bad;
478		}
479		unp2 = sotounpcb(so2);
480		unp3 = sotounpcb(so3);
481		if (unp2->unp_addr)
482			unp3->unp_addr =
483				  m_copy(unp2->unp_addr, 0, (int)M_COPYALL);
484		so2 = so3;
485	}
486	error = unp_connect2(so, so2);
487bad:
488	vput(vp);
489	return (error);
490}
491
492int
493unp_connect2(so, so2)
494	register struct socket *so;
495	register struct socket *so2;
496{
497	register struct unpcb *unp = sotounpcb(so);
498	register struct unpcb *unp2;
499
500	if (so2->so_type != so->so_type)
501		return (EPROTOTYPE);
502	unp2 = sotounpcb(so2);
503	unp->unp_conn = unp2;
504	switch (so->so_type) {
505
506	case SOCK_DGRAM:
507		unp->unp_nextref = unp2->unp_refs;
508		unp2->unp_refs = unp;
509		soisconnected(so);
510		break;
511
512	case SOCK_STREAM:
513		unp2->unp_conn = unp;
514		soisconnected(so);
515		soisconnected(so2);
516		break;
517
518	default:
519		panic("unp_connect2");
520	}
521	return (0);
522}
523
524void
525unp_disconnect(unp)
526	struct unpcb *unp;
527{
528	register struct unpcb *unp2 = unp->unp_conn;
529
530	if (unp2 == 0)
531		return;
532	unp->unp_conn = 0;
533	switch (unp->unp_socket->so_type) {
534
535	case SOCK_DGRAM:
536		if (unp2->unp_refs == unp)
537			unp2->unp_refs = unp->unp_nextref;
538		else {
539			unp2 = unp2->unp_refs;
540			for (;;) {
541				if (unp2 == 0)
542					panic("unp_disconnect");
543				if (unp2->unp_nextref == unp)
544					break;
545				unp2 = unp2->unp_nextref;
546			}
547			unp2->unp_nextref = unp->unp_nextref;
548		}
549		unp->unp_nextref = 0;
550		unp->unp_socket->so_state &= ~SS_ISCONNECTED;
551		break;
552
553	case SOCK_STREAM:
554		soisdisconnected(unp->unp_socket);
555		unp2->unp_conn = 0;
556		soisdisconnected(unp2->unp_socket);
557		break;
558	}
559}
560
561#ifdef notdef
562void
563unp_abort(unp)
564	struct unpcb *unp;
565{
566
567	unp_detach(unp);
568}
569#endif
570
571void
572unp_shutdown(unp)
573	struct unpcb *unp;
574{
575	struct socket *so;
576
577	if (unp->unp_socket->so_type == SOCK_STREAM && unp->unp_conn &&
578	    (so = unp->unp_conn->unp_socket))
579		socantrcvmore(so);
580}
581
582void
583unp_drop(unp, errno)
584	struct unpcb *unp;
585	int errno;
586{
587	struct socket *so = unp->unp_socket;
588
589	so->so_error = errno;
590	unp_disconnect(unp);
591	if (so->so_head) {
592		so->so_pcb = (caddr_t) 0;
593		m_freem(unp->unp_addr);
594		(void) m_free(dtom(unp));
595		sofree(so);
596	}
597}
598
599#ifdef notdef
600void
601unp_drain()
602{
603
604}
605#endif
606
607int
608unp_externalize(rights)
609	struct mbuf *rights;
610{
611	struct proc *p = curproc;		/* XXX */
612	register int i;
613	register struct cmsghdr *cm = mtod(rights, struct cmsghdr *);
614	register struct file **rp = (struct file **)(cm + 1);
615	register struct file *fp;
616	int newfds = (cm->cmsg_len - sizeof(*cm)) / sizeof (int);
617	int f;
618
619	if (!fdavail(p, newfds)) {
620		for (i = 0; i < newfds; i++) {
621			fp = *rp;
622			unp_discard(fp);
623			*rp++ = 0;
624		}
625		return (EMSGSIZE);
626	}
627	for (i = 0; i < newfds; i++) {
628		if (fdalloc(p, 0, &f))
629			panic("unp_externalize");
630		fp = *rp;
631		p->p_fd->fd_ofiles[f] = fp;
632		fp->f_msgcount--;
633		unp_rights--;
634		*(int *)rp++ = f;
635	}
636	return (0);
637}
638
639int
640unp_internalize(control, p)
641	struct mbuf *control;
642	struct proc *p;
643{
644	struct filedesc *fdp = p->p_fd;
645	register struct cmsghdr *cm = mtod(control, struct cmsghdr *);
646	register struct file **rp;
647	register struct file *fp;
648	register int i, fd;
649	int oldfds;
650
651	if (cm->cmsg_type != SCM_RIGHTS || cm->cmsg_level != SOL_SOCKET ||
652	    cm->cmsg_len != control->m_len)
653		return (EINVAL);
654	oldfds = (cm->cmsg_len - sizeof (*cm)) / sizeof (int);
655	rp = (struct file **)(cm + 1);
656	for (i = 0; i < oldfds; i++) {
657		fd = *(int *)rp++;
658		if ((unsigned)fd >= fdp->fd_nfiles ||
659		    fdp->fd_ofiles[fd] == NULL)
660			return (EBADF);
661	}
662	rp = (struct file **)(cm + 1);
663	for (i = 0; i < oldfds; i++) {
664		fp = fdp->fd_ofiles[*(int *)rp];
665		*rp++ = fp;
666		fp->f_count++;
667		fp->f_msgcount++;
668		unp_rights++;
669	}
670	return (0);
671}
672
673int	unp_defer, unp_gcing;
674extern	struct domain unixdomain;
675
676void
677unp_gc()
678{
679	register struct file *fp, *nextfp;
680	register struct socket *so;
681	struct file **extra_ref, **fpp;
682	int nunref, i;
683
684	if (unp_gcing)
685		return;
686	unp_gcing = 1;
687	unp_defer = 0;
688	for (fp = filehead; fp; fp = fp->f_filef)
689		fp->f_flag &= ~(FMARK|FDEFER);
690	do {
691		for (fp = filehead; fp; fp = fp->f_filef) {
692			if (fp->f_count == 0)
693				continue;
694			if (fp->f_flag & FDEFER) {
695				fp->f_flag &= ~FDEFER;
696				unp_defer--;
697			} else {
698				if (fp->f_flag & FMARK)
699					continue;
700				if (fp->f_count == fp->f_msgcount)
701					continue;
702				fp->f_flag |= FMARK;
703			}
704			if (fp->f_type != DTYPE_SOCKET ||
705			    (so = (struct socket *)fp->f_data) == 0)
706				continue;
707			if (so->so_proto->pr_domain != &unixdomain ||
708			    (so->so_proto->pr_flags&PR_RIGHTS) == 0)
709				continue;
710#ifdef notdef
711			if (so->so_rcv.sb_flags & SB_LOCK) {
712				/*
713				 * This is problematical; it's not clear
714				 * we need to wait for the sockbuf to be
715				 * unlocked (on a uniprocessor, at least),
716				 * and it's also not clear what to do
717				 * if sbwait returns an error due to receipt
718				 * of a signal.  If sbwait does return
719				 * an error, we'll go into an infinite
720				 * loop.  Delete all of this for now.
721				 */
722				(void) sbwait(&so->so_rcv);
723				goto restart;
724			}
725#endif
726			unp_scan(so->so_rcv.sb_mb, unp_mark);
727		}
728	} while (unp_defer);
729	/*
730	 * We grab an extra reference to each of the file table entries
731	 * that are not otherwise accessible and then free the rights
732	 * that are stored in messages on them.
733	 *
734	 * The bug in the orginal code is a little tricky, so I'll describe
735	 * what's wrong with it here.
736	 *
737	 * It is incorrect to simply unp_discard each entry for f_msgcount
738	 * times -- consider the case of sockets A and B that contain
739	 * references to each other.  On a last close of some other socket,
740	 * we trigger a gc since the number of outstanding rights (unp_rights)
741	 * is non-zero.  If during the sweep phase the gc code un_discards,
742	 * we end up doing a (full) closef on the descriptor.  A closef on A
743	 * results in the following chain.  Closef calls soo_close, which
744	 * calls soclose.   Soclose calls first (through the switch
745	 * uipc_usrreq) unp_detach, which re-invokes unp_gc.  Unp_gc simply
746	 * returns because the previous instance had set unp_gcing, and
747	 * we return all the way back to soclose, which marks the socket
748	 * with SS_NOFDREF, and then calls sofree.  Sofree calls sorflush
749	 * to free up the rights that are queued in messages on the socket A,
750	 * i.e., the reference on B.  The sorflush calls via the dom_dispose
751	 * switch unp_dispose, which unp_scans with unp_discard.  This second
752	 * instance of unp_discard just calls closef on B.
753	 *
754	 * Well, a similar chain occurs on B, resulting in a sorflush on B,
755	 * which results in another closef on A.  Unfortunately, A is already
756	 * being closed, and the descriptor has already been marked with
757	 * SS_NOFDREF, and soclose panics at this point.
758	 *
759	 * Here, we first take an extra reference to each inaccessible
760	 * descriptor.  Then, we call sorflush ourself, since we know
761	 * it is a Unix domain socket anyhow.  After we destroy all the
762	 * rights carried in messages, we do a last closef to get rid
763	 * of our extra reference.  This is the last close, and the
764	 * unp_detach etc will shut down the socket.
765	 *
766	 * 91/09/19, bsy@cs.cmu.edu
767	 */
768	extra_ref = malloc(nfiles * sizeof(struct file *), M_FILE, M_WAITOK);
769	for (nunref = 0, fp = filehead, fpp = extra_ref; fp; fp = nextfp) {
770		nextfp = fp->f_filef;
771		if (fp->f_count == 0)
772			continue;
773		if (fp->f_count == fp->f_msgcount && !(fp->f_flag & FMARK)) {
774			*fpp++ = fp;
775			nunref++;
776			fp->f_count++;
777		}
778	}
779	for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp)
780		sorflush((struct socket *)(*fpp)->f_data);
781	for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp)
782		closef(*fpp,(struct proc*) NULL);
783	free((caddr_t)extra_ref, M_FILE);
784	unp_gcing = 0;
785}
786
787void
788unp_dispose(m)
789	struct mbuf *m;
790{
791	if (m)
792		unp_scan(m, unp_discard);
793}
794
795void
796unp_scan(m0, op)
797	register struct mbuf *m0;
798	void (*op)(struct file *);
799{
800	register struct mbuf *m;
801	register struct file **rp;
802	register struct cmsghdr *cm;
803	register int i;
804	int qfds;
805
806	while (m0) {
807		for (m = m0; m; m = m->m_next)
808			if (m->m_type == MT_CONTROL &&
809			    m->m_len >= sizeof(*cm)) {
810				cm = mtod(m, struct cmsghdr *);
811				if (cm->cmsg_level != SOL_SOCKET ||
812				    cm->cmsg_type != SCM_RIGHTS)
813					continue;
814				qfds = (cm->cmsg_len - sizeof *cm)
815						/ sizeof (struct file *);
816				rp = (struct file **)(cm + 1);
817				for (i = 0; i < qfds; i++)
818					(*op)(*rp++);
819				break;		/* XXX, but saves time */
820			}
821		m0 = m0->m_act;
822	}
823}
824
825void
826unp_mark(fp)
827	struct file *fp;
828{
829
830	if (fp->f_flag & FMARK)
831		return;
832	unp_defer++;
833	fp->f_flag |= (FMARK|FDEFER);
834}
835
836void
837unp_discard(fp)
838	struct file *fp;
839{
840
841	fp->f_msgcount--;
842	unp_rights--;
843	(void) closef(fp, (struct proc *)NULL);
844}
845