uipc_usrreq.c revision 101126
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: head/sys/kern/uipc_usrreq.c 101126 2002-08-01 01:18:42Z rwatson $
35 */
36
37#include "opt_mac.h"
38
39#include <sys/param.h>
40#include <sys/domain.h>
41#include <sys/fcntl.h>
42#include <sys/malloc.h>		/* XXX must be before <sys/file.h> */
43#include <sys/file.h>
44#include <sys/filedesc.h>
45#include <sys/jail.h>
46#include <sys/kernel.h>
47#include <sys/lock.h>
48#include <sys/mac.h>
49#include <sys/mbuf.h>
50#include <sys/mutex.h>
51#include <sys/namei.h>
52#include <sys/proc.h>
53#include <sys/protosw.h>
54#include <sys/resourcevar.h>
55#include <sys/socket.h>
56#include <sys/socketvar.h>
57#include <sys/signalvar.h>
58#include <sys/stat.h>
59#include <sys/sx.h>
60#include <sys/sysctl.h>
61#include <sys/systm.h>
62#include <sys/un.h>
63#include <sys/unpcb.h>
64#include <sys/vnode.h>
65
66#include <vm/uma.h>
67
68static uma_zone_t unp_zone;
69static	unp_gen_t unp_gencnt;
70static	u_int unp_count;
71
72static	struct unp_head unp_shead, unp_dhead;
73
74/*
75 * Unix communications domain.
76 *
77 * TODO:
78 *	SEQPACKET, RDM
79 *	rethink name space problems
80 *	need a proper out-of-band
81 *	lock pushdown
82 */
83static struct	sockaddr sun_noname = { sizeof(sun_noname), AF_LOCAL };
84static ino_t	unp_ino;		/* prototype for fake inode numbers */
85
86static int     unp_attach(struct socket *);
87static void    unp_detach(struct unpcb *);
88static int     unp_bind(struct unpcb *,struct sockaddr *, struct thread *);
89static int     unp_connect(struct socket *,struct sockaddr *, struct thread *);
90static void    unp_disconnect(struct unpcb *);
91static void    unp_shutdown(struct unpcb *);
92static void    unp_drop(struct unpcb *, int);
93static void    unp_gc(void);
94static void    unp_scan(struct mbuf *, void (*)(struct file *));
95static void    unp_mark(struct file *);
96static void    unp_discard(struct file *);
97static void    unp_freerights(struct file **, int);
98static int     unp_internalize(struct mbuf **, struct thread *);
99static int     unp_listen(struct unpcb *, struct thread *);
100
101static int
102uipc_abort(struct socket *so)
103{
104	struct unpcb *unp = sotounpcb(so);
105
106	if (unp == 0)
107		return EINVAL;
108	unp_drop(unp, ECONNABORTED);
109	unp_detach(unp);
110	sotryfree(so);
111	return 0;
112}
113
114static int
115uipc_accept(struct socket *so, struct sockaddr **nam)
116{
117	struct unpcb *unp = sotounpcb(so);
118
119	if (unp == 0)
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				    1);
130	} else {
131		*nam = dup_sockaddr((struct sockaddr *)&sun_noname, 1);
132	}
133	return 0;
134}
135
136static int
137uipc_attach(struct socket *so, int proto, struct thread *td)
138{
139	struct unpcb *unp = sotounpcb(so);
140
141	if (unp != 0)
142		return EISCONN;
143	return unp_attach(so);
144}
145
146static int
147uipc_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
148{
149	struct unpcb *unp = sotounpcb(so);
150
151	if (unp == 0)
152		return EINVAL;
153
154	return unp_bind(unp, nam, td);
155}
156
157static int
158uipc_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
159{
160	struct unpcb *unp = sotounpcb(so);
161
162	if (unp == 0)
163		return EINVAL;
164	return unp_connect(so, nam, curthread);
165}
166
167static int
168uipc_connect2(struct socket *so1, struct socket *so2)
169{
170	struct unpcb *unp = sotounpcb(so1);
171
172	if (unp == 0)
173		return EINVAL;
174
175	return unp_connect2(so1, so2);
176}
177
178/* control is EOPNOTSUPP */
179
180static int
181uipc_detach(struct socket *so)
182{
183	struct unpcb *unp = sotounpcb(so);
184
185	if (unp == 0)
186		return EINVAL;
187
188	unp_detach(unp);
189	return 0;
190}
191
192static int
193uipc_disconnect(struct socket *so)
194{
195	struct unpcb *unp = sotounpcb(so);
196
197	if (unp == 0)
198		return EINVAL;
199	unp_disconnect(unp);
200	return 0;
201}
202
203static int
204uipc_listen(struct socket *so, struct thread *td)
205{
206	struct unpcb *unp = sotounpcb(so);
207
208	if (unp == 0 || unp->unp_vnode == 0)
209		return EINVAL;
210	return unp_listen(unp, td);
211}
212
213static int
214uipc_peeraddr(struct socket *so, struct sockaddr **nam)
215{
216	struct unpcb *unp = sotounpcb(so);
217
218	if (unp == 0)
219		return EINVAL;
220	if (unp->unp_conn && unp->unp_conn->unp_addr)
221		*nam = dup_sockaddr((struct sockaddr *)unp->unp_conn->unp_addr,
222				    1);
223	return 0;
224}
225
226static int
227uipc_rcvd(struct socket *so, int flags)
228{
229	struct unpcb *unp = sotounpcb(so);
230	struct socket *so2;
231	u_long newhiwat;
232
233	if (unp == 0)
234		return EINVAL;
235	switch (so->so_type) {
236	case SOCK_DGRAM:
237		panic("uipc_rcvd DGRAM?");
238		/*NOTREACHED*/
239
240	case SOCK_STREAM:
241		if (unp->unp_conn == 0)
242			break;
243		so2 = unp->unp_conn->unp_socket;
244		/*
245		 * Adjust backpressure on sender
246		 * and wakeup any waiting to write.
247		 */
248		so2->so_snd.sb_mbmax += unp->unp_mbcnt - so->so_rcv.sb_mbcnt;
249		unp->unp_mbcnt = so->so_rcv.sb_mbcnt;
250		newhiwat = so2->so_snd.sb_hiwat + unp->unp_cc -
251		    so->so_rcv.sb_cc;
252		(void)chgsbsize(so2->so_cred->cr_uidinfo, &so2->so_snd.sb_hiwat,
253		    newhiwat, RLIM_INFINITY);
254		unp->unp_cc = so->so_rcv.sb_cc;
255		sowwakeup(so2);
256		break;
257
258	default:
259		panic("uipc_rcvd unknown socktype");
260	}
261	return 0;
262}
263
264/* pru_rcvoob is EOPNOTSUPP */
265
266static int
267uipc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
268	  struct mbuf *control, struct thread *td)
269{
270	int error = 0;
271	struct unpcb *unp = sotounpcb(so);
272	struct socket *so2;
273	u_long newhiwat;
274
275	if (unp == 0) {
276		error = EINVAL;
277		goto release;
278	}
279	if (flags & PRUS_OOB) {
280		error = EOPNOTSUPP;
281		goto release;
282	}
283
284	if (control && (error = unp_internalize(&control, td)))
285		goto release;
286
287	switch (so->so_type) {
288	case SOCK_DGRAM:
289	{
290		struct sockaddr *from;
291
292		if (nam) {
293			if (unp->unp_conn) {
294				error = EISCONN;
295				break;
296			}
297			error = unp_connect(so, nam, td);
298			if (error)
299				break;
300		} else {
301			if (unp->unp_conn == 0) {
302				error = ENOTCONN;
303				break;
304			}
305		}
306		so2 = unp->unp_conn->unp_socket;
307		if (unp->unp_addr)
308			from = (struct sockaddr *)unp->unp_addr;
309		else
310			from = &sun_noname;
311		if (sbappendaddr(&so2->so_rcv, from, m, control)) {
312			sorwakeup(so2);
313			m = 0;
314			control = 0;
315		} else
316			error = ENOBUFS;
317		if (nam)
318			unp_disconnect(unp);
319		break;
320	}
321
322	case SOCK_STREAM:
323		/* Connect if not connected yet. */
324		/*
325		 * Note: A better implementation would complain
326		 * if not equal to the peer's address.
327		 */
328		if ((so->so_state & SS_ISCONNECTED) == 0) {
329			if (nam) {
330				error = unp_connect(so, nam, td);
331				if (error)
332					break;	/* XXX */
333			} else {
334				error = ENOTCONN;
335				break;
336			}
337		}
338
339		if (so->so_state & SS_CANTSENDMORE) {
340			error = EPIPE;
341			break;
342		}
343		if (unp->unp_conn == 0)
344			panic("uipc_send connected but no connection?");
345		so2 = unp->unp_conn->unp_socket;
346		/*
347		 * Send to paired receive port, and then reduce
348		 * send buffer hiwater marks to maintain backpressure.
349		 * Wake up readers.
350		 */
351		if (control) {
352			if (sbappendcontrol(&so2->so_rcv, m, control))
353				control = 0;
354		} else
355			sbappend(&so2->so_rcv, m);
356		so->so_snd.sb_mbmax -=
357			so2->so_rcv.sb_mbcnt - unp->unp_conn->unp_mbcnt;
358		unp->unp_conn->unp_mbcnt = so2->so_rcv.sb_mbcnt;
359		newhiwat = so->so_snd.sb_hiwat -
360		    (so2->so_rcv.sb_cc - unp->unp_conn->unp_cc);
361		(void)chgsbsize(so->so_cred->cr_uidinfo, &so->so_snd.sb_hiwat,
362		    newhiwat, RLIM_INFINITY);
363		unp->unp_conn->unp_cc = so2->so_rcv.sb_cc;
364		sorwakeup(so2);
365		m = 0;
366		break;
367
368	default:
369		panic("uipc_send unknown socktype");
370	}
371
372	/*
373	 * SEND_EOF is equivalent to a SEND followed by
374	 * a SHUTDOWN.
375	 */
376	if (flags & PRUS_EOF) {
377		socantsendmore(so);
378		unp_shutdown(unp);
379	}
380
381	if (control && error != 0)
382		unp_dispose(control);
383
384release:
385	if (control)
386		m_freem(control);
387	if (m)
388		m_freem(m);
389	return error;
390}
391
392static int
393uipc_sense(struct socket *so, struct stat *sb)
394{
395	struct unpcb *unp = sotounpcb(so);
396	struct socket *so2;
397
398	if (unp == 0)
399		return EINVAL;
400	sb->st_blksize = so->so_snd.sb_hiwat;
401	if (so->so_type == SOCK_STREAM && unp->unp_conn != 0) {
402		so2 = unp->unp_conn->unp_socket;
403		sb->st_blksize += so2->so_rcv.sb_cc;
404	}
405	sb->st_dev = NOUDEV;
406	if (unp->unp_ino == 0)
407		unp->unp_ino = unp_ino++;
408	sb->st_ino = unp->unp_ino;
409	return (0);
410}
411
412static int
413uipc_shutdown(struct socket *so)
414{
415	struct unpcb *unp = sotounpcb(so);
416
417	if (unp == 0)
418		return EINVAL;
419	socantsendmore(so);
420	unp_shutdown(unp);
421	return 0;
422}
423
424static int
425uipc_sockaddr(struct socket *so, struct sockaddr **nam)
426{
427	struct unpcb *unp = sotounpcb(so);
428
429	if (unp == 0)
430		return EINVAL;
431	if (unp->unp_addr)
432		*nam = dup_sockaddr((struct sockaddr *)unp->unp_addr, 1);
433	else
434		*nam = dup_sockaddr((struct sockaddr *)&sun_noname, 1);
435	return 0;
436}
437
438struct pr_usrreqs uipc_usrreqs = {
439	uipc_abort, uipc_accept, uipc_attach, uipc_bind, uipc_connect,
440	uipc_connect2, pru_control_notsupp, uipc_detach, uipc_disconnect,
441	uipc_listen, uipc_peeraddr, uipc_rcvd, pru_rcvoob_notsupp,
442	uipc_send, uipc_sense, uipc_shutdown, uipc_sockaddr,
443	sosend, soreceive, sopoll
444};
445
446int
447uipc_ctloutput(so, sopt)
448	struct socket *so;
449	struct sockopt *sopt;
450{
451	struct unpcb *unp = sotounpcb(so);
452	int error;
453
454	switch (sopt->sopt_dir) {
455	case SOPT_GET:
456		switch (sopt->sopt_name) {
457		case LOCAL_PEERCRED:
458			if (unp->unp_flags & UNP_HAVEPC)
459				error = sooptcopyout(sopt, &unp->unp_peercred,
460				    sizeof(unp->unp_peercred));
461			else {
462				if (so->so_type == SOCK_STREAM)
463					error = ENOTCONN;
464				else
465					error = EINVAL;
466			}
467			break;
468		default:
469			error = EOPNOTSUPP;
470			break;
471		}
472		break;
473	case SOPT_SET:
474	default:
475		error = EOPNOTSUPP;
476		break;
477	}
478	return (error);
479}
480
481/*
482 * Both send and receive buffers are allocated PIPSIZ bytes of buffering
483 * for stream sockets, although the total for sender and receiver is
484 * actually only PIPSIZ.
485 * Datagram sockets really use the sendspace as the maximum datagram size,
486 * and don't really want to reserve the sendspace.  Their recvspace should
487 * be large enough for at least one max-size datagram plus address.
488 */
489#ifndef PIPSIZ
490#define	PIPSIZ	8192
491#endif
492static u_long	unpst_sendspace = PIPSIZ;
493static u_long	unpst_recvspace = PIPSIZ;
494static u_long	unpdg_sendspace = 2*1024;	/* really max datagram size */
495static u_long	unpdg_recvspace = 4*1024;
496
497static int	unp_rights;			/* file descriptors in flight */
498
499SYSCTL_DECL(_net_local_stream);
500SYSCTL_INT(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW,
501	   &unpst_sendspace, 0, "");
502SYSCTL_INT(_net_local_stream, OID_AUTO, recvspace, CTLFLAG_RW,
503	   &unpst_recvspace, 0, "");
504SYSCTL_DECL(_net_local_dgram);
505SYSCTL_INT(_net_local_dgram, OID_AUTO, maxdgram, CTLFLAG_RW,
506	   &unpdg_sendspace, 0, "");
507SYSCTL_INT(_net_local_dgram, OID_AUTO, recvspace, CTLFLAG_RW,
508	   &unpdg_recvspace, 0, "");
509SYSCTL_DECL(_net_local);
510SYSCTL_INT(_net_local, OID_AUTO, inflight, CTLFLAG_RD, &unp_rights, 0, "");
511
512static int
513unp_attach(so)
514	struct socket *so;
515{
516	register struct unpcb *unp;
517	int error;
518
519	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
520		switch (so->so_type) {
521
522		case SOCK_STREAM:
523			error = soreserve(so, unpst_sendspace, unpst_recvspace);
524			break;
525
526		case SOCK_DGRAM:
527			error = soreserve(so, unpdg_sendspace, unpdg_recvspace);
528			break;
529
530		default:
531			panic("unp_attach");
532		}
533		if (error)
534			return (error);
535	}
536	unp = uma_zalloc(unp_zone, M_WAITOK);
537	if (unp == NULL)
538		return (ENOBUFS);
539	bzero(unp, sizeof *unp);
540	unp->unp_gencnt = ++unp_gencnt;
541	unp_count++;
542	LIST_INIT(&unp->unp_refs);
543	unp->unp_socket = so;
544	FILEDESC_LOCK(curproc->p_fd);
545	unp->unp_rvnode = curthread->td_proc->p_fd->fd_rdir;
546	FILEDESC_UNLOCK(curproc->p_fd);
547	LIST_INSERT_HEAD(so->so_type == SOCK_DGRAM ? &unp_dhead
548			 : &unp_shead, unp, unp_link);
549	so->so_pcb = unp;
550	return (0);
551}
552
553static void
554unp_detach(unp)
555	register struct unpcb *unp;
556{
557	LIST_REMOVE(unp, unp_link);
558	unp->unp_gencnt = ++unp_gencnt;
559	--unp_count;
560	if (unp->unp_vnode) {
561		unp->unp_vnode->v_socket = 0;
562		vrele(unp->unp_vnode);
563		unp->unp_vnode = 0;
564	}
565	if (unp->unp_conn)
566		unp_disconnect(unp);
567	while (!LIST_EMPTY(&unp->unp_refs))
568		unp_drop(LIST_FIRST(&unp->unp_refs), ECONNRESET);
569	soisdisconnected(unp->unp_socket);
570	unp->unp_socket->so_pcb = 0;
571	if (unp_rights) {
572		/*
573		 * Normally the receive buffer is flushed later,
574		 * in sofree, but if our receive buffer holds references
575		 * to descriptors that are now garbage, we will dispose
576		 * of those descriptor references after the garbage collector
577		 * gets them (resulting in a "panic: closef: count < 0").
578		 */
579		sorflush(unp->unp_socket);
580		unp_gc();
581	}
582	if (unp->unp_addr)
583		FREE(unp->unp_addr, M_SONAME);
584	uma_zfree(unp_zone, unp);
585}
586
587static int
588unp_bind(unp, nam, td)
589	struct unpcb *unp;
590	struct sockaddr *nam;
591	struct thread *td;
592{
593	struct sockaddr_un *soun = (struct sockaddr_un *)nam;
594	struct vnode *vp;
595	struct mount *mp;
596	struct vattr vattr;
597	int error, namelen;
598	struct nameidata nd;
599	char *buf;
600
601	if (unp->unp_vnode != NULL)
602		return (EINVAL);
603	namelen = soun->sun_len - offsetof(struct sockaddr_un, sun_path);
604	if (namelen <= 0)
605		return EINVAL;
606	buf = malloc(SOCK_MAXADDRLEN, M_TEMP, M_WAITOK);
607	strncpy(buf, soun->sun_path, namelen);
608	buf[namelen] = 0;	/* null-terminate the string */
609restart:
610	NDINIT(&nd, CREATE, NOFOLLOW | LOCKPARENT | SAVENAME, UIO_SYSSPACE,
611	    buf, td);
612/* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */
613	error = namei(&nd);
614	if (error) {
615		free(buf, M_TEMP);
616		return (error);
617	}
618	vp = nd.ni_vp;
619	if (vp != NULL || vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
620		NDFREE(&nd, NDF_ONLY_PNBUF);
621		if (nd.ni_dvp == vp)
622			vrele(nd.ni_dvp);
623		else
624			vput(nd.ni_dvp);
625		if (vp != NULL) {
626			vrele(vp);
627			free(buf, M_TEMP);
628			return (EADDRINUSE);
629		}
630		error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH);
631		if (error) {
632			free(buf, M_TEMP);
633			return (error);
634		}
635		goto restart;
636	}
637	VATTR_NULL(&vattr);
638	vattr.va_type = VSOCK;
639	FILEDESC_LOCK(td->td_proc->p_fd);
640	vattr.va_mode = (ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask);
641	FILEDESC_UNLOCK(td->td_proc->p_fd);
642#ifdef MAC
643	error = mac_check_vnode_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
644	    &vattr);
645#endif /* MAC */
646	if (error == 0) {
647		VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
648		error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
649	}
650	NDFREE(&nd, NDF_ONLY_PNBUF);
651	vput(nd.ni_dvp);
652	if (error) {
653		free(buf, M_TEMP);
654		return (error);
655	}
656	vp = nd.ni_vp;
657	vp->v_socket = unp->unp_socket;
658	unp->unp_vnode = vp;
659	unp->unp_addr = (struct sockaddr_un *)dup_sockaddr(nam, 1);
660	VOP_UNLOCK(vp, 0, td);
661	vn_finished_write(mp);
662	free(buf, M_TEMP);
663	return (0);
664}
665
666static int
667unp_connect(so, nam, td)
668	struct socket *so;
669	struct sockaddr *nam;
670	struct thread *td;
671{
672	register struct sockaddr_un *soun = (struct sockaddr_un *)nam;
673	register struct vnode *vp;
674	register struct socket *so2, *so3;
675	struct unpcb *unp, *unp2, *unp3;
676	int error, len;
677	struct nameidata nd;
678	char buf[SOCK_MAXADDRLEN];
679
680	len = nam->sa_len - offsetof(struct sockaddr_un, sun_path);
681	if (len <= 0)
682		return EINVAL;
683	strncpy(buf, soun->sun_path, len);
684	buf[len] = 0;
685
686	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, buf, td);
687	error = namei(&nd);
688	if (error)
689		return (error);
690	vp = nd.ni_vp;
691	NDFREE(&nd, NDF_ONLY_PNBUF);
692	if (vp->v_type != VSOCK) {
693		error = ENOTSOCK;
694		goto bad;
695	}
696	error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td);
697	if (error)
698		goto bad;
699	so2 = vp->v_socket;
700	if (so2 == 0) {
701		error = ECONNREFUSED;
702		goto bad;
703	}
704	if (so->so_type != so2->so_type) {
705		error = EPROTOTYPE;
706		goto bad;
707	}
708	if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
709		if ((so2->so_options & SO_ACCEPTCONN) == 0 ||
710		    (so3 = sonewconn(so2, 0)) == 0) {
711			error = ECONNREFUSED;
712			goto bad;
713		}
714		unp = sotounpcb(so);
715		unp2 = sotounpcb(so2);
716		unp3 = sotounpcb(so3);
717		if (unp2->unp_addr)
718			unp3->unp_addr = (struct sockaddr_un *)
719				dup_sockaddr((struct sockaddr *)
720					     unp2->unp_addr, 1);
721
722		/*
723		 * unp_peercred management:
724		 *
725		 * The connecter's (client's) credentials are copied
726		 * from its process structure at the time of connect()
727		 * (which is now).
728		 */
729		cru2x(td->td_ucred, &unp3->unp_peercred);
730		unp3->unp_flags |= UNP_HAVEPC;
731		/*
732		 * The receiver's (server's) credentials are copied
733		 * from the unp_peercred member of socket on which the
734		 * former called listen(); unp_listen() cached that
735		 * process's credentials at that time so we can use
736		 * them now.
737		 */
738		KASSERT(unp2->unp_flags & UNP_HAVEPCCACHED,
739		    ("unp_connect: listener without cached peercred"));
740		memcpy(&unp->unp_peercred, &unp2->unp_peercred,
741		    sizeof(unp->unp_peercred));
742		unp->unp_flags |= UNP_HAVEPC;
743#ifdef MAC
744		mac_set_socket_peer_from_socket(so, so3);
745		mac_set_socket_peer_from_socket(so3, so);
746#endif
747
748		so2 = so3;
749	}
750	error = unp_connect2(so, so2);
751bad:
752	vput(vp);
753	return (error);
754}
755
756int
757unp_connect2(so, so2)
758	register struct socket *so;
759	register struct socket *so2;
760{
761	register struct unpcb *unp = sotounpcb(so);
762	register struct unpcb *unp2;
763
764	if (so2->so_type != so->so_type)
765		return (EPROTOTYPE);
766	unp2 = sotounpcb(so2);
767	unp->unp_conn = unp2;
768	switch (so->so_type) {
769
770	case SOCK_DGRAM:
771		LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink);
772		soisconnected(so);
773		break;
774
775	case SOCK_STREAM:
776		unp2->unp_conn = unp;
777		soisconnected(so);
778		soisconnected(so2);
779		break;
780
781	default:
782		panic("unp_connect2");
783	}
784	return (0);
785}
786
787static void
788unp_disconnect(unp)
789	struct unpcb *unp;
790{
791	register struct unpcb *unp2 = unp->unp_conn;
792
793	if (unp2 == 0)
794		return;
795	unp->unp_conn = 0;
796	switch (unp->unp_socket->so_type) {
797
798	case SOCK_DGRAM:
799		LIST_REMOVE(unp, unp_reflink);
800		unp->unp_socket->so_state &= ~SS_ISCONNECTED;
801		break;
802
803	case SOCK_STREAM:
804		soisdisconnected(unp->unp_socket);
805		unp2->unp_conn = 0;
806		soisdisconnected(unp2->unp_socket);
807		break;
808	}
809}
810
811#ifdef notdef
812void
813unp_abort(unp)
814	struct unpcb *unp;
815{
816
817	unp_detach(unp);
818}
819#endif
820
821static int
822unp_pcblist(SYSCTL_HANDLER_ARGS)
823{
824	int error, i, n;
825	struct unpcb *unp, **unp_list;
826	unp_gen_t gencnt;
827	struct xunpgen *xug;
828	struct unp_head *head;
829	struct xunpcb *xu;
830
831	head = ((intptr_t)arg1 == SOCK_DGRAM ? &unp_dhead : &unp_shead);
832
833	/*
834	 * The process of preparing the PCB list is too time-consuming and
835	 * resource-intensive to repeat twice on every request.
836	 */
837	if (req->oldptr == 0) {
838		n = unp_count;
839		req->oldidx = 2 * (sizeof *xug)
840			+ (n + n/8) * sizeof(struct xunpcb);
841		return 0;
842	}
843
844	if (req->newptr != 0)
845		return EPERM;
846
847	/*
848	 * OK, now we're committed to doing something.
849	 */
850	xug = malloc(sizeof(*xug), M_TEMP, M_WAITOK);
851	gencnt = unp_gencnt;
852	n = unp_count;
853
854	xug->xug_len = sizeof *xug;
855	xug->xug_count = n;
856	xug->xug_gen = gencnt;
857	xug->xug_sogen = so_gencnt;
858	error = SYSCTL_OUT(req, xug, sizeof *xug);
859	if (error) {
860		free(xug, M_TEMP);
861		return error;
862	}
863
864	unp_list = malloc(n * sizeof *unp_list, M_TEMP, M_WAITOK);
865
866	for (unp = LIST_FIRST(head), i = 0; unp && i < n;
867	     unp = LIST_NEXT(unp, unp_link)) {
868		if (unp->unp_gencnt <= gencnt) {
869			if (cr_cansee(req->td->td_ucred,
870			    unp->unp_socket->so_cred))
871				continue;
872			unp_list[i++] = unp;
873		}
874	}
875	n = i;			/* in case we lost some during malloc */
876
877	error = 0;
878	xu = malloc(sizeof(*xu), M_TEMP, M_WAITOK);
879	for (i = 0; i < n; i++) {
880		unp = unp_list[i];
881		if (unp->unp_gencnt <= gencnt) {
882			xu->xu_len = sizeof *xu;
883			xu->xu_unpp = unp;
884			/*
885			 * XXX - need more locking here to protect against
886			 * connect/disconnect races for SMP.
887			 */
888			if (unp->unp_addr)
889				bcopy(unp->unp_addr, &xu->xu_addr,
890				      unp->unp_addr->sun_len);
891			if (unp->unp_conn && unp->unp_conn->unp_addr)
892				bcopy(unp->unp_conn->unp_addr,
893				      &xu->xu_caddr,
894				      unp->unp_conn->unp_addr->sun_len);
895			bcopy(unp, &xu->xu_unp, sizeof *unp);
896			sotoxsocket(unp->unp_socket, &xu->xu_socket);
897			error = SYSCTL_OUT(req, xu, sizeof *xu);
898		}
899	}
900	free(xu, M_TEMP);
901	if (!error) {
902		/*
903		 * Give the user an updated idea of our state.
904		 * If the generation differs from what we told
905		 * her before, she knows that something happened
906		 * while we were processing this request, and it
907		 * might be necessary to retry.
908		 */
909		xug->xug_gen = unp_gencnt;
910		xug->xug_sogen = so_gencnt;
911		xug->xug_count = unp_count;
912		error = SYSCTL_OUT(req, xug, sizeof *xug);
913	}
914	free(unp_list, M_TEMP);
915	free(xug, M_TEMP);
916	return error;
917}
918
919SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLFLAG_RD,
920	    (caddr_t)(long)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb",
921	    "List of active local datagram sockets");
922SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, CTLFLAG_RD,
923	    (caddr_t)(long)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb",
924	    "List of active local stream sockets");
925
926static void
927unp_shutdown(unp)
928	struct unpcb *unp;
929{
930	struct socket *so;
931
932	if (unp->unp_socket->so_type == SOCK_STREAM && unp->unp_conn &&
933	    (so = unp->unp_conn->unp_socket))
934		socantrcvmore(so);
935}
936
937static void
938unp_drop(unp, errno)
939	struct unpcb *unp;
940	int errno;
941{
942	struct socket *so = unp->unp_socket;
943
944	so->so_error = errno;
945	unp_disconnect(unp);
946}
947
948#ifdef notdef
949void
950unp_drain()
951{
952
953}
954#endif
955
956static void
957unp_freerights(rp, fdcount)
958	struct file **rp;
959	int fdcount;
960{
961	int i;
962	struct file *fp;
963
964	for (i = 0; i < fdcount; i++) {
965		fp = *rp;
966		/*
967		 * zero the pointer before calling
968		 * unp_discard since it may end up
969		 * in unp_gc()..
970		 */
971		*rp++ = 0;
972		unp_discard(fp);
973	}
974}
975
976int
977unp_externalize(control, controlp)
978	struct mbuf *control, **controlp;
979{
980	struct thread *td = curthread;		/* XXX */
981	struct cmsghdr *cm = mtod(control, struct cmsghdr *);
982	int i;
983	int *fdp;
984	struct file **rp;
985	struct file *fp;
986	void *data;
987	socklen_t clen = control->m_len, datalen;
988	int error, newfds;
989	int f;
990	u_int newlen;
991
992	error = 0;
993	if (controlp != NULL) /* controlp == NULL => free control messages */
994		*controlp = NULL;
995
996	while (cm != NULL) {
997		if (sizeof(*cm) > clen || cm->cmsg_len > clen) {
998			error = EINVAL;
999			break;
1000		}
1001
1002		data = CMSG_DATA(cm);
1003		datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
1004
1005		if (cm->cmsg_level == SOL_SOCKET
1006		    && cm->cmsg_type == SCM_RIGHTS) {
1007			newfds = datalen / sizeof(struct file *);
1008			rp = data;
1009
1010			/* If we're not outputting the discriptors free them. */
1011			if (error || controlp == NULL) {
1012				unp_freerights(rp, newfds);
1013				goto next;
1014			}
1015			FILEDESC_LOCK(td->td_proc->p_fd);
1016			/* if the new FD's will not fit free them.  */
1017			if (!fdavail(td, newfds)) {
1018				FILEDESC_UNLOCK(td->td_proc->p_fd);
1019				error = EMSGSIZE;
1020				unp_freerights(rp, newfds);
1021				goto next;
1022			}
1023			/*
1024			 * now change each pointer to an fd in the global
1025			 * table to an integer that is the index to the
1026			 * local fd table entry that we set up to point
1027			 * to the global one we are transferring.
1028			 */
1029			newlen = newfds * sizeof(int);
1030			*controlp = sbcreatecontrol(NULL, newlen,
1031			    SCM_RIGHTS, SOL_SOCKET);
1032			if (*controlp == NULL) {
1033				FILEDESC_UNLOCK(td->td_proc->p_fd);
1034				error = E2BIG;
1035				unp_freerights(rp, newfds);
1036				goto next;
1037			}
1038
1039			fdp = (int *)
1040			    CMSG_DATA(mtod(*controlp, struct cmsghdr *));
1041			for (i = 0; i < newfds; i++) {
1042				if (fdalloc(td, 0, &f))
1043					panic("unp_externalize fdalloc failed");
1044				fp = *rp++;
1045				td->td_proc->p_fd->fd_ofiles[f] = fp;
1046				FILE_LOCK(fp);
1047				fp->f_msgcount--;
1048				FILE_UNLOCK(fp);
1049				unp_rights--;
1050				*fdp++ = f;
1051			}
1052			FILEDESC_UNLOCK(td->td_proc->p_fd);
1053		} else { /* We can just copy anything else across */
1054			if (error || controlp == NULL)
1055				goto next;
1056			*controlp = sbcreatecontrol(NULL, datalen,
1057			    cm->cmsg_type, cm->cmsg_level);
1058			if (*controlp == NULL) {
1059				error = ENOBUFS;
1060				goto next;
1061			}
1062			bcopy(data,
1063			    CMSG_DATA(mtod(*controlp, struct cmsghdr *)),
1064			    datalen);
1065		}
1066
1067		controlp = &(*controlp)->m_next;
1068
1069next:
1070		if (CMSG_SPACE(datalen) < clen) {
1071			clen -= CMSG_SPACE(datalen);
1072			cm = (struct cmsghdr *)
1073			    ((caddr_t)cm + CMSG_SPACE(datalen));
1074		} else {
1075			clen = 0;
1076			cm = NULL;
1077		}
1078	}
1079
1080	m_freem(control);
1081
1082	return (error);
1083}
1084
1085void
1086unp_init(void)
1087{
1088	unp_zone = uma_zcreate("unpcb", sizeof(struct unpcb), NULL, NULL,
1089	    NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
1090	uma_zone_set_max(unp_zone, nmbclusters);
1091	if (unp_zone == 0)
1092		panic("unp_init");
1093	LIST_INIT(&unp_dhead);
1094	LIST_INIT(&unp_shead);
1095}
1096
1097#ifndef MIN
1098#define	MIN(a,b) (((a)<(b))?(a):(b))
1099#endif
1100
1101static int
1102unp_internalize(controlp, td)
1103	struct mbuf **controlp;
1104	struct thread *td;
1105{
1106	struct mbuf *control = *controlp;
1107	struct proc *p = td->td_proc;
1108	struct filedesc *fdescp = p->p_fd;
1109	struct cmsghdr *cm = mtod(control, struct cmsghdr *);
1110	struct cmsgcred *cmcred;
1111	struct file **rp;
1112	struct file *fp;
1113	struct timeval *tv;
1114	int i, fd, *fdp;
1115	void *data;
1116	socklen_t clen = control->m_len, datalen;
1117	int error, oldfds;
1118	u_int newlen;
1119
1120	error = 0;
1121	*controlp = NULL;
1122
1123	while (cm != NULL) {
1124		if (sizeof(*cm) > clen || cm->cmsg_level != SOL_SOCKET
1125		    || cm->cmsg_len > clen) {
1126			error = EINVAL;
1127			goto out;
1128		}
1129
1130		data = CMSG_DATA(cm);
1131		datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
1132
1133		switch (cm->cmsg_type) {
1134		/*
1135		 * Fill in credential information.
1136		 */
1137		case SCM_CREDS:
1138			*controlp = sbcreatecontrol(NULL, sizeof(*cmcred),
1139			    SCM_CREDS, SOL_SOCKET);
1140			if (*controlp == NULL) {
1141				error = ENOBUFS;
1142				goto out;
1143			}
1144
1145			cmcred = (struct cmsgcred *)
1146			    CMSG_DATA(mtod(*controlp, struct cmsghdr *));
1147			cmcred->cmcred_pid = p->p_pid;
1148			cmcred->cmcred_uid = td->td_ucred->cr_ruid;
1149			cmcred->cmcred_gid = td->td_ucred->cr_rgid;
1150			cmcred->cmcred_euid = td->td_ucred->cr_uid;
1151			cmcred->cmcred_ngroups = MIN(td->td_ucred->cr_ngroups,
1152							CMGROUP_MAX);
1153			for (i = 0; i < cmcred->cmcred_ngroups; i++)
1154				cmcred->cmcred_groups[i] =
1155				    td->td_ucred->cr_groups[i];
1156			break;
1157
1158		case SCM_RIGHTS:
1159			oldfds = datalen / sizeof (int);
1160			/*
1161			 * check that all the FDs passed in refer to legal files
1162			 * If not, reject the entire operation.
1163			 */
1164			fdp = data;
1165			FILEDESC_LOCK(fdescp);
1166			for (i = 0; i < oldfds; i++) {
1167				fd = *fdp++;
1168				if ((unsigned)fd >= fdescp->fd_nfiles ||
1169				    fdescp->fd_ofiles[fd] == NULL) {
1170					FILEDESC_UNLOCK(fdescp);
1171					error = EBADF;
1172					goto out;
1173				}
1174			}
1175			/*
1176			 * Now replace the integer FDs with pointers to
1177			 * the associated global file table entry..
1178			 */
1179			newlen = oldfds * sizeof(struct file *);
1180			*controlp = sbcreatecontrol(NULL, newlen,
1181			    SCM_RIGHTS, SOL_SOCKET);
1182			if (*controlp == NULL) {
1183				FILEDESC_UNLOCK(fdescp);
1184				error = E2BIG;
1185				goto out;
1186			}
1187
1188			fdp = data;
1189			rp = (struct file **)
1190			    CMSG_DATA(mtod(*controlp, struct cmsghdr *));
1191			for (i = 0; i < oldfds; i++) {
1192				fp = fdescp->fd_ofiles[*fdp++];
1193				*rp++ = fp;
1194				FILE_LOCK(fp);
1195				fp->f_count++;
1196				fp->f_msgcount++;
1197				FILE_UNLOCK(fp);
1198				unp_rights++;
1199			}
1200			FILEDESC_UNLOCK(fdescp);
1201			break;
1202
1203		case SCM_TIMESTAMP:
1204			*controlp = sbcreatecontrol(NULL, sizeof(*tv),
1205			    SCM_TIMESTAMP, SOL_SOCKET);
1206			if (*controlp == NULL) {
1207				error = ENOBUFS;
1208				goto out;
1209			}
1210			tv = (struct timeval *)
1211			    CMSG_DATA(mtod(*controlp, struct cmsghdr *));
1212			microtime(tv);
1213			break;
1214
1215		default:
1216			error = EINVAL;
1217			goto out;
1218		}
1219
1220		controlp = &(*controlp)->m_next;
1221
1222		if (CMSG_SPACE(datalen) < clen) {
1223			clen -= CMSG_SPACE(datalen);
1224			cm = (struct cmsghdr *)
1225			    ((caddr_t)cm + CMSG_SPACE(datalen));
1226		} else {
1227			clen = 0;
1228			cm = NULL;
1229		}
1230	}
1231
1232out:
1233	m_freem(control);
1234
1235	return (error);
1236}
1237
1238static int	unp_defer, unp_gcing;
1239
1240static void
1241unp_gc()
1242{
1243	register struct file *fp, *nextfp;
1244	register struct socket *so;
1245	struct file **extra_ref, **fpp;
1246	int nunref, i;
1247
1248	if (unp_gcing)
1249		return;
1250	unp_gcing = 1;
1251	unp_defer = 0;
1252	/*
1253	 * before going through all this, set all FDs to
1254	 * be NOT defered and NOT externally accessible
1255	 */
1256	sx_slock(&filelist_lock);
1257	LIST_FOREACH(fp, &filehead, f_list)
1258		fp->f_gcflag &= ~(FMARK|FDEFER);
1259	do {
1260		LIST_FOREACH(fp, &filehead, f_list) {
1261			FILE_LOCK(fp);
1262			/*
1263			 * If the file is not open, skip it
1264			 */
1265			if (fp->f_count == 0) {
1266				FILE_UNLOCK(fp);
1267				continue;
1268			}
1269			/*
1270			 * If we already marked it as 'defer'  in a
1271			 * previous pass, then try process it this time
1272			 * and un-mark it
1273			 */
1274			if (fp->f_gcflag & FDEFER) {
1275				fp->f_gcflag &= ~FDEFER;
1276				unp_defer--;
1277			} else {
1278				/*
1279				 * if it's not defered, then check if it's
1280				 * already marked.. if so skip it
1281				 */
1282				if (fp->f_gcflag & FMARK) {
1283					FILE_UNLOCK(fp);
1284					continue;
1285				}
1286				/*
1287				 * If all references are from messages
1288				 * in transit, then skip it. it's not
1289				 * externally accessible.
1290				 */
1291				if (fp->f_count == fp->f_msgcount) {
1292					FILE_UNLOCK(fp);
1293					continue;
1294				}
1295				/*
1296				 * If it got this far then it must be
1297				 * externally accessible.
1298				 */
1299				fp->f_gcflag |= FMARK;
1300			}
1301			/*
1302			 * either it was defered, or it is externally
1303			 * accessible and not already marked so.
1304			 * Now check if it is possibly one of OUR sockets.
1305			 */
1306			if (fp->f_type != DTYPE_SOCKET ||
1307			    (so = (struct socket *)fp->f_data) == 0) {
1308				FILE_UNLOCK(fp);
1309				continue;
1310			}
1311			FILE_UNLOCK(fp);
1312			if (so->so_proto->pr_domain != &localdomain ||
1313			    (so->so_proto->pr_flags&PR_RIGHTS) == 0)
1314				continue;
1315#ifdef notdef
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				(void) 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			unp_scan(so->so_rcv.sb_mb, unp_mark);
1339		}
1340	} while (unp_defer);
1341	sx_sunlock(&filelist_lock);
1342	/*
1343	 * We grab an extra reference to each of the file table entries
1344	 * that are not otherwise accessible and then free the rights
1345	 * that are stored in messages on them.
1346	 *
1347	 * The bug in the orginal code is a little tricky, so I'll describe
1348	 * what's wrong with it here.
1349	 *
1350	 * It is incorrect to simply unp_discard each entry for f_msgcount
1351	 * times -- consider the case of sockets A and B that contain
1352	 * references to each other.  On a last close of some other socket,
1353	 * we trigger a gc since the number of outstanding rights (unp_rights)
1354	 * is non-zero.  If during the sweep phase the gc code un_discards,
1355	 * we end up doing a (full) closef on the descriptor.  A closef on A
1356	 * results in the following chain.  Closef calls soo_close, which
1357	 * calls soclose.   Soclose calls first (through the switch
1358	 * uipc_usrreq) unp_detach, which re-invokes unp_gc.  Unp_gc simply
1359	 * returns because the previous instance had set unp_gcing, and
1360	 * we return all the way back to soclose, which marks the socket
1361	 * with SS_NOFDREF, and then calls sofree.  Sofree calls sorflush
1362	 * to free up the rights that are queued in messages on the socket A,
1363	 * i.e., the reference on B.  The sorflush calls via the dom_dispose
1364	 * switch unp_dispose, which unp_scans with unp_discard.  This second
1365	 * instance of unp_discard just calls closef on B.
1366	 *
1367	 * Well, a similar chain occurs on B, resulting in a sorflush on B,
1368	 * which results in another closef on A.  Unfortunately, A is already
1369	 * being closed, and the descriptor has already been marked with
1370	 * SS_NOFDREF, and soclose panics at this point.
1371	 *
1372	 * Here, we first take an extra reference to each inaccessible
1373	 * descriptor.  Then, we call sorflush ourself, since we know
1374	 * it is a Unix domain socket anyhow.  After we destroy all the
1375	 * rights carried in messages, we do a last closef to get rid
1376	 * of our extra reference.  This is the last close, and the
1377	 * unp_detach etc will shut down the socket.
1378	 *
1379	 * 91/09/19, bsy@cs.cmu.edu
1380	 */
1381	extra_ref = malloc(nfiles * sizeof(struct file *), M_TEMP, M_WAITOK);
1382	sx_slock(&filelist_lock);
1383	for (nunref = 0, fp = LIST_FIRST(&filehead), fpp = extra_ref; fp != 0;
1384	    fp = nextfp) {
1385		nextfp = LIST_NEXT(fp, f_list);
1386		FILE_LOCK(fp);
1387		/*
1388		 * If it's not open, skip it
1389		 */
1390		if (fp->f_count == 0) {
1391			FILE_UNLOCK(fp);
1392			continue;
1393		}
1394		/*
1395		 * If all refs are from msgs, and it's not marked accessible
1396		 * then it must be referenced from some unreachable cycle
1397		 * of (shut-down) FDs, so include it in our
1398		 * list of FDs to remove
1399		 */
1400		if (fp->f_count == fp->f_msgcount && !(fp->f_gcflag & FMARK)) {
1401			*fpp++ = fp;
1402			nunref++;
1403			fp->f_count++;
1404		}
1405		FILE_UNLOCK(fp);
1406	}
1407	sx_sunlock(&filelist_lock);
1408	/*
1409	 * for each FD on our hit list, do the following two things
1410	 */
1411	for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) {
1412		struct file *tfp = *fpp;
1413		FILE_LOCK(tfp);
1414		if (tfp->f_type == DTYPE_SOCKET && tfp->f_data != NULL) {
1415			FILE_UNLOCK(tfp);
1416			sorflush((struct socket *)(tfp->f_data));
1417		} else
1418			FILE_UNLOCK(tfp);
1419	}
1420	for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp)
1421		closef(*fpp, (struct thread *) NULL);
1422	free(extra_ref, M_TEMP);
1423	unp_gcing = 0;
1424}
1425
1426void
1427unp_dispose(m)
1428	struct mbuf *m;
1429{
1430
1431	if (m)
1432		unp_scan(m, unp_discard);
1433}
1434
1435static int
1436unp_listen(unp, td)
1437	struct unpcb *unp;
1438	struct thread *td;
1439{
1440
1441	cru2x(td->td_ucred, &unp->unp_peercred);
1442	unp->unp_flags |= UNP_HAVEPCCACHED;
1443	return (0);
1444}
1445
1446static void
1447unp_scan(m0, op)
1448	register struct mbuf *m0;
1449	void (*op)(struct file *);
1450{
1451	struct mbuf *m;
1452	struct file **rp;
1453	struct cmsghdr *cm;
1454	void *data;
1455	int i;
1456	socklen_t clen, datalen;
1457	int qfds;
1458
1459	while (m0) {
1460		for (m = m0; m; m = m->m_next) {
1461			if (m->m_type != MT_CONTROL)
1462				continue;
1463
1464			cm = mtod(m, struct cmsghdr *);
1465			clen = m->m_len;
1466
1467			while (cm != NULL) {
1468				if (sizeof(*cm) > clen || cm->cmsg_len > clen)
1469					break;
1470
1471				data = CMSG_DATA(cm);
1472				datalen = (caddr_t)cm + cm->cmsg_len
1473				    - (caddr_t)data;
1474
1475				if (cm->cmsg_level == SOL_SOCKET &&
1476				    cm->cmsg_type == SCM_RIGHTS) {
1477					qfds = datalen / sizeof (struct file *);
1478					rp = data;
1479					for (i = 0; i < qfds; i++)
1480						(*op)(*rp++);
1481				}
1482
1483				if (CMSG_SPACE(datalen) < clen) {
1484					clen -= CMSG_SPACE(datalen);
1485					cm = (struct cmsghdr *)
1486					    ((caddr_t)cm + CMSG_SPACE(datalen));
1487				} else {
1488					clen = 0;
1489					cm = NULL;
1490				}
1491			}
1492		}
1493		m0 = m0->m_act;
1494	}
1495}
1496
1497static void
1498unp_mark(fp)
1499	struct file *fp;
1500{
1501	if (fp->f_gcflag & FMARK)
1502		return;
1503	unp_defer++;
1504	fp->f_gcflag |= (FMARK|FDEFER);
1505}
1506
1507static void
1508unp_discard(fp)
1509	struct file *fp;
1510{
1511	FILE_LOCK(fp);
1512	fp->f_msgcount--;
1513	unp_rights--;
1514	FILE_UNLOCK(fp);
1515	(void) closef(fp, (struct thread *)NULL);
1516}
1517