xref: /freebsd/sys/kern/uipc_usrreq.c (revision a854ed98931b2e365eddd24cd2a1bb53a3f1828f)
1df8bae1dSRodney W. Grimes /*
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1989, 1991, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
14df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
15df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
16df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
17df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
18df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
19df8bae1dSRodney W. Grimes  *    without specific prior written permission.
20df8bae1dSRodney W. Grimes  *
21df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
32df8bae1dSRodney W. Grimes  *
33748e0b0aSGarrett Wollman  *	From: @(#)uipc_usrreq.c	8.3 (Berkeley) 1/4/94
34c3aac50fSPeter Wemm  * $FreeBSD$
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
37df8bae1dSRodney W. Grimes #include <sys/param.h>
38df8bae1dSRodney W. Grimes #include <sys/systm.h>
39639acc13SGarrett Wollman #include <sys/kernel.h>
403ac4d1efSBruce Evans #include <sys/fcntl.h>
41fb919e4dSMark Murray #include <sys/domain.h>
42fb919e4dSMark Murray #include <sys/filedesc.h>
43fb919e4dSMark Murray #include <sys/lock.h>
44d826c479SBruce Evans #include <sys/malloc.h>		/* XXX must be before <sys/file.h> */
45639acc13SGarrett Wollman #include <sys/file.h>
46fb919e4dSMark Murray #include <sys/mutex.h>
47639acc13SGarrett Wollman #include <sys/mbuf.h>
48639acc13SGarrett Wollman #include <sys/namei.h>
49639acc13SGarrett Wollman #include <sys/proc.h>
50df8bae1dSRodney W. Grimes #include <sys/protosw.h>
51df8bae1dSRodney W. Grimes #include <sys/socket.h>
52df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
53c6362551SAlfred Perlstein #include <sys/resourcevar.h>
54df8bae1dSRodney W. Grimes #include <sys/stat.h>
55639acc13SGarrett Wollman #include <sys/sysctl.h>
56639acc13SGarrett Wollman #include <sys/un.h>
5798271db4SGarrett Wollman #include <sys/unpcb.h>
58639acc13SGarrett Wollman #include <sys/vnode.h>
5991421ba2SRobert Watson #include <sys/jail.h>
60426da3bcSAlfred Perlstein #include <sys/sx.h>
61df8bae1dSRodney W. Grimes 
6298271db4SGarrett Wollman #include <vm/vm_zone.h>
6398271db4SGarrett Wollman 
64632a035fSEivind Eklund static	struct vm_zone *unp_zone;
6598271db4SGarrett Wollman static	unp_gen_t unp_gencnt;
6698271db4SGarrett Wollman static	u_int unp_count;
6798271db4SGarrett Wollman 
6898271db4SGarrett Wollman static	struct unp_head unp_shead, unp_dhead;
6998271db4SGarrett Wollman 
70df8bae1dSRodney W. Grimes /*
71df8bae1dSRodney W. Grimes  * Unix communications domain.
72df8bae1dSRodney W. Grimes  *
73df8bae1dSRodney W. Grimes  * TODO:
74df8bae1dSRodney W. Grimes  *	SEQPACKET, RDM
75df8bae1dSRodney W. Grimes  *	rethink name space problems
76df8bae1dSRodney W. Grimes  *	need a proper out-of-band
7798271db4SGarrett Wollman  *	lock pushdown
78df8bae1dSRodney W. Grimes  */
79f708ef1bSPoul-Henning Kamp static struct	sockaddr sun_noname = { sizeof(sun_noname), AF_LOCAL };
80f708ef1bSPoul-Henning Kamp static ino_t	unp_ino;		/* prototype for fake inode numbers */
81f708ef1bSPoul-Henning Kamp 
82f708ef1bSPoul-Henning Kamp static int     unp_attach __P((struct socket *));
83f708ef1bSPoul-Henning Kamp static void    unp_detach __P((struct unpcb *));
84b40ce416SJulian Elischer static int     unp_bind __P((struct unpcb *,struct sockaddr *, struct thread *));
8557bf258eSGarrett Wollman static int     unp_connect __P((struct socket *,struct sockaddr *,
86b40ce416SJulian Elischer 				struct thread *));
87f708ef1bSPoul-Henning Kamp static void    unp_disconnect __P((struct unpcb *));
88f708ef1bSPoul-Henning Kamp static void    unp_shutdown __P((struct unpcb *));
89f708ef1bSPoul-Henning Kamp static void    unp_drop __P((struct unpcb *, int));
90f708ef1bSPoul-Henning Kamp static void    unp_gc __P((void));
91f708ef1bSPoul-Henning Kamp static void    unp_scan __P((struct mbuf *, void (*)(struct file *)));
92f708ef1bSPoul-Henning Kamp static void    unp_mark __P((struct file *));
93f708ef1bSPoul-Henning Kamp static void    unp_discard __P((struct file *));
942bc21ed9SDavid Malone static void    unp_freerights __P((struct file **, int));
952bc21ed9SDavid Malone static int     unp_internalize __P((struct mbuf **, struct thread *));
960c1bb4fbSDima Dorfman static int     unp_listen __P((struct unpcb *, struct proc *));
97f708ef1bSPoul-Henning Kamp 
98a29f300eSGarrett Wollman static int
99a29f300eSGarrett Wollman uipc_abort(struct socket *so)
100df8bae1dSRodney W. Grimes {
101df8bae1dSRodney W. Grimes 	struct unpcb *unp = sotounpcb(so);
102df8bae1dSRodney W. Grimes 
103a29f300eSGarrett Wollman 	if (unp == 0)
104a29f300eSGarrett Wollman 		return EINVAL;
105a29f300eSGarrett Wollman 	unp_drop(unp, ECONNABORTED);
106ddb7d629SIan Dowse 	unp_detach(unp);
107ddb7d629SIan Dowse 	sotryfree(so);
108a29f300eSGarrett Wollman 	return 0;
109df8bae1dSRodney W. Grimes }
110df8bae1dSRodney W. Grimes 
111a29f300eSGarrett Wollman static int
11257bf258eSGarrett Wollman uipc_accept(struct socket *so, struct sockaddr **nam)
113a29f300eSGarrett Wollman {
114a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so);
115df8bae1dSRodney W. Grimes 
116a29f300eSGarrett Wollman 	if (unp == 0)
117a29f300eSGarrett Wollman 		return EINVAL;
118df8bae1dSRodney W. Grimes 
119df8bae1dSRodney W. Grimes 	/*
120df8bae1dSRodney W. Grimes 	 * Pass back name of connected socket,
121df8bae1dSRodney W. Grimes 	 * if it was bound and we are still connected
122df8bae1dSRodney W. Grimes 	 * (our peer may have closed already!).
123df8bae1dSRodney W. Grimes 	 */
124df8bae1dSRodney W. Grimes 	if (unp->unp_conn && unp->unp_conn->unp_addr) {
12557bf258eSGarrett Wollman 		*nam = dup_sockaddr((struct sockaddr *)unp->unp_conn->unp_addr,
12657bf258eSGarrett Wollman 				    1);
127df8bae1dSRodney W. Grimes 	} else {
12857bf258eSGarrett Wollman 		*nam = dup_sockaddr((struct sockaddr *)&sun_noname, 1);
129df8bae1dSRodney W. Grimes 	}
130a29f300eSGarrett Wollman 	return 0;
131a29f300eSGarrett Wollman }
132df8bae1dSRodney W. Grimes 
133a29f300eSGarrett Wollman static int
134b40ce416SJulian Elischer uipc_attach(struct socket *so, int proto, struct thread *td)
135a29f300eSGarrett Wollman {
136a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so);
137df8bae1dSRodney W. Grimes 
138a29f300eSGarrett Wollman 	if (unp != 0)
139a29f300eSGarrett Wollman 		return EISCONN;
140a29f300eSGarrett Wollman 	return unp_attach(so);
141a29f300eSGarrett Wollman }
142a29f300eSGarrett Wollman 
143a29f300eSGarrett Wollman static int
144b40ce416SJulian Elischer uipc_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
145a29f300eSGarrett Wollman {
146a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so);
147a29f300eSGarrett Wollman 
148a29f300eSGarrett Wollman 	if (unp == 0)
149a29f300eSGarrett Wollman 		return EINVAL;
150a29f300eSGarrett Wollman 
151b40ce416SJulian Elischer 	return unp_bind(unp, nam, td);
152a29f300eSGarrett Wollman }
153a29f300eSGarrett Wollman 
154a29f300eSGarrett Wollman static int
155b40ce416SJulian Elischer uipc_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
156a29f300eSGarrett Wollman {
157a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so);
158a29f300eSGarrett Wollman 
159a29f300eSGarrett Wollman 	if (unp == 0)
160a29f300eSGarrett Wollman 		return EINVAL;
161b40ce416SJulian Elischer 	return unp_connect(so, nam, curthread);
162a29f300eSGarrett Wollman }
163a29f300eSGarrett Wollman 
164a29f300eSGarrett Wollman static int
165a29f300eSGarrett Wollman uipc_connect2(struct socket *so1, struct socket *so2)
166a29f300eSGarrett Wollman {
167a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so1);
168a29f300eSGarrett Wollman 
169a29f300eSGarrett Wollman 	if (unp == 0)
170a29f300eSGarrett Wollman 		return EINVAL;
171a29f300eSGarrett Wollman 
172a29f300eSGarrett Wollman 	return unp_connect2(so1, so2);
173a29f300eSGarrett Wollman }
174a29f300eSGarrett Wollman 
175a29f300eSGarrett Wollman /* control is EOPNOTSUPP */
176a29f300eSGarrett Wollman 
177a29f300eSGarrett Wollman static int
178a29f300eSGarrett Wollman uipc_detach(struct socket *so)
179a29f300eSGarrett Wollman {
180a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so);
181a29f300eSGarrett Wollman 
182a29f300eSGarrett Wollman 	if (unp == 0)
183a29f300eSGarrett Wollman 		return EINVAL;
184a29f300eSGarrett Wollman 
185a29f300eSGarrett Wollman 	unp_detach(unp);
186a29f300eSGarrett Wollman 	return 0;
187a29f300eSGarrett Wollman }
188a29f300eSGarrett Wollman 
189a29f300eSGarrett Wollman static int
190a29f300eSGarrett Wollman uipc_disconnect(struct socket *so)
191a29f300eSGarrett Wollman {
192a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so);
193a29f300eSGarrett Wollman 
194a29f300eSGarrett Wollman 	if (unp == 0)
195a29f300eSGarrett Wollman 		return EINVAL;
196a29f300eSGarrett Wollman 	unp_disconnect(unp);
197a29f300eSGarrett Wollman 	return 0;
198a29f300eSGarrett Wollman }
199a29f300eSGarrett Wollman 
200a29f300eSGarrett Wollman static int
201b40ce416SJulian Elischer uipc_listen(struct socket *so, struct thread *td)
202a29f300eSGarrett Wollman {
203a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so);
204a29f300eSGarrett Wollman 
205a29f300eSGarrett Wollman 	if (unp == 0 || unp->unp_vnode == 0)
206a29f300eSGarrett Wollman 		return EINVAL;
207b40ce416SJulian Elischer 	return unp_listen(unp, td->td_proc);
208a29f300eSGarrett Wollman }
209a29f300eSGarrett Wollman 
210a29f300eSGarrett Wollman static int
21157bf258eSGarrett Wollman uipc_peeraddr(struct socket *so, struct sockaddr **nam)
212a29f300eSGarrett Wollman {
213a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so);
214a29f300eSGarrett Wollman 
215a29f300eSGarrett Wollman 	if (unp == 0)
216a29f300eSGarrett Wollman 		return EINVAL;
21757bf258eSGarrett Wollman 	if (unp->unp_conn && unp->unp_conn->unp_addr)
21857bf258eSGarrett Wollman 		*nam = dup_sockaddr((struct sockaddr *)unp->unp_conn->unp_addr,
21957bf258eSGarrett Wollman 				    1);
220a29f300eSGarrett Wollman 	return 0;
221a29f300eSGarrett Wollman }
222a29f300eSGarrett Wollman 
223a29f300eSGarrett Wollman static int
224a29f300eSGarrett Wollman uipc_rcvd(struct socket *so, int flags)
225a29f300eSGarrett Wollman {
226a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so);
227a29f300eSGarrett Wollman 	struct socket *so2;
2286aef685fSBrian Feldman 	u_long newhiwat;
229a29f300eSGarrett Wollman 
230a29f300eSGarrett Wollman 	if (unp == 0)
231a29f300eSGarrett Wollman 		return EINVAL;
232df8bae1dSRodney W. Grimes 	switch (so->so_type) {
233df8bae1dSRodney W. Grimes 	case SOCK_DGRAM:
234a29f300eSGarrett Wollman 		panic("uipc_rcvd DGRAM?");
235df8bae1dSRodney W. Grimes 		/*NOTREACHED*/
236df8bae1dSRodney W. Grimes 
237df8bae1dSRodney W. Grimes 	case SOCK_STREAM:
238df8bae1dSRodney W. Grimes 		if (unp->unp_conn == 0)
239df8bae1dSRodney W. Grimes 			break;
240df8bae1dSRodney W. Grimes 		so2 = unp->unp_conn->unp_socket;
241df8bae1dSRodney W. Grimes 		/*
242df8bae1dSRodney W. Grimes 		 * Adjust backpressure on sender
243df8bae1dSRodney W. Grimes 		 * and wakeup any waiting to write.
244df8bae1dSRodney W. Grimes 		 */
245ff8b0106SBrian Feldman 		so2->so_snd.sb_mbmax += unp->unp_mbcnt - so->so_rcv.sb_mbcnt;
246ff8b0106SBrian Feldman 		unp->unp_mbcnt = so->so_rcv.sb_mbcnt;
2476aef685fSBrian Feldman 		newhiwat = so2->so_snd.sb_hiwat + unp->unp_cc -
2486aef685fSBrian Feldman 		    so->so_rcv.sb_cc;
249f535380cSDon Lewis 		(void)chgsbsize(so2->so_cred->cr_uidinfo, &so2->so_snd.sb_hiwat,
2506aef685fSBrian Feldman 		    newhiwat, RLIM_INFINITY);
251ff8b0106SBrian Feldman 		unp->unp_cc = so->so_rcv.sb_cc;
252df8bae1dSRodney W. Grimes 		sowwakeup(so2);
253df8bae1dSRodney W. Grimes 		break;
254df8bae1dSRodney W. Grimes 
255df8bae1dSRodney W. Grimes 	default:
256a29f300eSGarrett Wollman 		panic("uipc_rcvd unknown socktype");
257df8bae1dSRodney W. Grimes 	}
258a29f300eSGarrett Wollman 	return 0;
259a29f300eSGarrett Wollman }
260df8bae1dSRodney W. Grimes 
261a29f300eSGarrett Wollman /* pru_rcvoob is EOPNOTSUPP */
262a29f300eSGarrett Wollman 
263a29f300eSGarrett Wollman static int
26457bf258eSGarrett Wollman uipc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
265b40ce416SJulian Elischer 	  struct mbuf *control, struct thread *td)
266a29f300eSGarrett Wollman {
267a29f300eSGarrett Wollman 	int error = 0;
268a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so);
269a29f300eSGarrett Wollman 	struct socket *so2;
2706aef685fSBrian Feldman 	u_long newhiwat;
271a29f300eSGarrett Wollman 
272a29f300eSGarrett Wollman 	if (unp == 0) {
273a29f300eSGarrett Wollman 		error = EINVAL;
274a29f300eSGarrett Wollman 		goto release;
275a29f300eSGarrett Wollman 	}
276a29f300eSGarrett Wollman 	if (flags & PRUS_OOB) {
277a29f300eSGarrett Wollman 		error = EOPNOTSUPP;
278a29f300eSGarrett Wollman 		goto release;
279a29f300eSGarrett Wollman 	}
280a29f300eSGarrett Wollman 
2812bc21ed9SDavid Malone 	if (control && (error = unp_internalize(&control, td)))
282a29f300eSGarrett Wollman 		goto release;
283df8bae1dSRodney W. Grimes 
284a29f300eSGarrett Wollman 	switch (so->so_type) {
285a29f300eSGarrett Wollman 	case SOCK_DGRAM:
286a29f300eSGarrett Wollman 	{
287df8bae1dSRodney W. Grimes 		struct sockaddr *from;
288df8bae1dSRodney W. Grimes 
289df8bae1dSRodney W. Grimes 		if (nam) {
290df8bae1dSRodney W. Grimes 			if (unp->unp_conn) {
291df8bae1dSRodney W. Grimes 				error = EISCONN;
292df8bae1dSRodney W. Grimes 				break;
293df8bae1dSRodney W. Grimes 			}
294b40ce416SJulian Elischer 			error = unp_connect(so, nam, td);
295df8bae1dSRodney W. Grimes 			if (error)
296df8bae1dSRodney W. Grimes 				break;
297df8bae1dSRodney W. Grimes 		} else {
298df8bae1dSRodney W. Grimes 			if (unp->unp_conn == 0) {
299df8bae1dSRodney W. Grimes 				error = ENOTCONN;
300df8bae1dSRodney W. Grimes 				break;
301df8bae1dSRodney W. Grimes 			}
302df8bae1dSRodney W. Grimes 		}
303df8bae1dSRodney W. Grimes 		so2 = unp->unp_conn->unp_socket;
304df8bae1dSRodney W. Grimes 		if (unp->unp_addr)
30557bf258eSGarrett Wollman 			from = (struct sockaddr *)unp->unp_addr;
306df8bae1dSRodney W. Grimes 		else
307df8bae1dSRodney W. Grimes 			from = &sun_noname;
308df8bae1dSRodney W. Grimes 		if (sbappendaddr(&so2->so_rcv, from, m, control)) {
309df8bae1dSRodney W. Grimes 			sorwakeup(so2);
310df8bae1dSRodney W. Grimes 			m = 0;
311df8bae1dSRodney W. Grimes 			control = 0;
312df8bae1dSRodney W. Grimes 		} else
313df8bae1dSRodney W. Grimes 			error = ENOBUFS;
314df8bae1dSRodney W. Grimes 		if (nam)
315df8bae1dSRodney W. Grimes 			unp_disconnect(unp);
316df8bae1dSRodney W. Grimes 		break;
317df8bae1dSRodney W. Grimes 	}
318df8bae1dSRodney W. Grimes 
319df8bae1dSRodney W. Grimes 	case SOCK_STREAM:
3206b8fda4dSGarrett Wollman 		/* Connect if not connected yet. */
3216b8fda4dSGarrett Wollman 		/*
3226b8fda4dSGarrett Wollman 		 * Note: A better implementation would complain
323402cc72dSDavid Greenman 		 * if not equal to the peer's address.
3246b8fda4dSGarrett Wollman 		 */
325402cc72dSDavid Greenman 		if ((so->so_state & SS_ISCONNECTED) == 0) {
326402cc72dSDavid Greenman 			if (nam) {
327b40ce416SJulian Elischer 				error = unp_connect(so, nam, td);
328402cc72dSDavid Greenman 				if (error)
3296b8fda4dSGarrett Wollman 					break;	/* XXX */
330402cc72dSDavid Greenman 			} else {
331402cc72dSDavid Greenman 				error = ENOTCONN;
332402cc72dSDavid Greenman 				break;
333402cc72dSDavid Greenman 			}
334402cc72dSDavid Greenman 		}
335402cc72dSDavid Greenman 
336df8bae1dSRodney W. Grimes 		if (so->so_state & SS_CANTSENDMORE) {
337df8bae1dSRodney W. Grimes 			error = EPIPE;
338df8bae1dSRodney W. Grimes 			break;
339df8bae1dSRodney W. Grimes 		}
340df8bae1dSRodney W. Grimes 		if (unp->unp_conn == 0)
341a29f300eSGarrett Wollman 			panic("uipc_send connected but no connection?");
342df8bae1dSRodney W. Grimes 		so2 = unp->unp_conn->unp_socket;
343df8bae1dSRodney W. Grimes 		/*
344df8bae1dSRodney W. Grimes 		 * Send to paired receive port, and then reduce
345df8bae1dSRodney W. Grimes 		 * send buffer hiwater marks to maintain backpressure.
346df8bae1dSRodney W. Grimes 		 * Wake up readers.
347df8bae1dSRodney W. Grimes 		 */
348df8bae1dSRodney W. Grimes 		if (control) {
349ff8b0106SBrian Feldman 			if (sbappendcontrol(&so2->so_rcv, m, control))
350df8bae1dSRodney W. Grimes 				control = 0;
351df8bae1dSRodney W. Grimes 		} else
352ff8b0106SBrian Feldman 			sbappend(&so2->so_rcv, m);
353ff8b0106SBrian Feldman 		so->so_snd.sb_mbmax -=
354ff8b0106SBrian Feldman 			so2->so_rcv.sb_mbcnt - unp->unp_conn->unp_mbcnt;
355ff8b0106SBrian Feldman 		unp->unp_conn->unp_mbcnt = so2->so_rcv.sb_mbcnt;
3566aef685fSBrian Feldman 		newhiwat = so->so_snd.sb_hiwat -
3576aef685fSBrian Feldman 		    (so2->so_rcv.sb_cc - unp->unp_conn->unp_cc);
358f535380cSDon Lewis 		(void)chgsbsize(so->so_cred->cr_uidinfo, &so->so_snd.sb_hiwat,
3596aef685fSBrian Feldman 		    newhiwat, RLIM_INFINITY);
360ff8b0106SBrian Feldman 		unp->unp_conn->unp_cc = so2->so_rcv.sb_cc;
361df8bae1dSRodney W. Grimes 		sorwakeup(so2);
362df8bae1dSRodney W. Grimes 		m = 0;
363df8bae1dSRodney W. Grimes 		break;
364df8bae1dSRodney W. Grimes 
365df8bae1dSRodney W. Grimes 	default:
366a29f300eSGarrett Wollman 		panic("uipc_send unknown socktype");
367df8bae1dSRodney W. Grimes 	}
368a29f300eSGarrett Wollman 
3696b8fda4dSGarrett Wollman 	/*
3706b8fda4dSGarrett Wollman 	 * SEND_EOF is equivalent to a SEND followed by
3716b8fda4dSGarrett Wollman 	 * a SHUTDOWN.
3726b8fda4dSGarrett Wollman 	 */
373a29f300eSGarrett Wollman 	if (flags & PRUS_EOF) {
3746b8fda4dSGarrett Wollman 		socantsendmore(so);
3756b8fda4dSGarrett Wollman 		unp_shutdown(unp);
3766b8fda4dSGarrett Wollman 	}
377df8bae1dSRodney W. Grimes 
378bd508d39SDon Lewis 	if (control && error != 0)
379bd508d39SDon Lewis 		unp_dispose(control);
380bd508d39SDon Lewis 
381a29f300eSGarrett Wollman release:
382a29f300eSGarrett Wollman 	if (control)
383a29f300eSGarrett Wollman 		m_freem(control);
384a29f300eSGarrett Wollman 	if (m)
385a29f300eSGarrett Wollman 		m_freem(m);
386a29f300eSGarrett Wollman 	return error;
387a29f300eSGarrett Wollman }
388df8bae1dSRodney W. Grimes 
389a29f300eSGarrett Wollman static int
390a29f300eSGarrett Wollman uipc_sense(struct socket *so, struct stat *sb)
391a29f300eSGarrett Wollman {
392a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so);
393a29f300eSGarrett Wollman 	struct socket *so2;
394a29f300eSGarrett Wollman 
395a29f300eSGarrett Wollman 	if (unp == 0)
396a29f300eSGarrett Wollman 		return EINVAL;
397a29f300eSGarrett Wollman 	sb->st_blksize = so->so_snd.sb_hiwat;
398df8bae1dSRodney W. Grimes 	if (so->so_type == SOCK_STREAM && unp->unp_conn != 0) {
399df8bae1dSRodney W. Grimes 		so2 = unp->unp_conn->unp_socket;
400a29f300eSGarrett Wollman 		sb->st_blksize += so2->so_rcv.sb_cc;
401df8bae1dSRodney W. Grimes 	}
402bfbb9ce6SPoul-Henning Kamp 	sb->st_dev = NOUDEV;
403df8bae1dSRodney W. Grimes 	if (unp->unp_ino == 0)
404df8bae1dSRodney W. Grimes 		unp->unp_ino = unp_ino++;
405a29f300eSGarrett Wollman 	sb->st_ino = unp->unp_ino;
406df8bae1dSRodney W. Grimes 	return (0);
407a29f300eSGarrett Wollman }
408df8bae1dSRodney W. Grimes 
409a29f300eSGarrett Wollman static int
410a29f300eSGarrett Wollman uipc_shutdown(struct socket *so)
411a29f300eSGarrett Wollman {
412a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so);
413df8bae1dSRodney W. Grimes 
414a29f300eSGarrett Wollman 	if (unp == 0)
415a29f300eSGarrett Wollman 		return EINVAL;
416a29f300eSGarrett Wollman 	socantsendmore(so);
417a29f300eSGarrett Wollman 	unp_shutdown(unp);
418a29f300eSGarrett Wollman 	return 0;
419a29f300eSGarrett Wollman }
420df8bae1dSRodney W. Grimes 
421a29f300eSGarrett Wollman static int
42257bf258eSGarrett Wollman uipc_sockaddr(struct socket *so, struct sockaddr **nam)
423a29f300eSGarrett Wollman {
424a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so);
425a29f300eSGarrett Wollman 
426a29f300eSGarrett Wollman 	if (unp == 0)
427a29f300eSGarrett Wollman 		return EINVAL;
42857bf258eSGarrett Wollman 	if (unp->unp_addr)
42957bf258eSGarrett Wollman 		*nam = dup_sockaddr((struct sockaddr *)unp->unp_addr, 1);
43083f3198bSThomas Moestl 	else
43183f3198bSThomas Moestl 		*nam = dup_sockaddr((struct sockaddr *)&sun_noname, 1);
432a29f300eSGarrett Wollman 	return 0;
433df8bae1dSRodney W. Grimes }
434a29f300eSGarrett Wollman 
435a29f300eSGarrett Wollman struct pr_usrreqs uipc_usrreqs = {
436a29f300eSGarrett Wollman 	uipc_abort, uipc_accept, uipc_attach, uipc_bind, uipc_connect,
437a29f300eSGarrett Wollman 	uipc_connect2, pru_control_notsupp, uipc_detach, uipc_disconnect,
438a29f300eSGarrett Wollman 	uipc_listen, uipc_peeraddr, uipc_rcvd, pru_rcvoob_notsupp,
439a29f300eSGarrett Wollman 	uipc_send, uipc_sense, uipc_shutdown, uipc_sockaddr,
44051338ea8SPeter Wemm 	sosend, soreceive, sopoll
441a29f300eSGarrett Wollman };
442df8bae1dSRodney W. Grimes 
4430c1bb4fbSDima Dorfman int
4440c1bb4fbSDima Dorfman uipc_ctloutput(so, sopt)
4450c1bb4fbSDima Dorfman 	struct socket *so;
4460c1bb4fbSDima Dorfman 	struct sockopt *sopt;
4470c1bb4fbSDima Dorfman {
4480c1bb4fbSDima Dorfman 	struct unpcb *unp = sotounpcb(so);
4490c1bb4fbSDima Dorfman 	int error;
4500c1bb4fbSDima Dorfman 
4510c1bb4fbSDima Dorfman 	switch (sopt->sopt_dir) {
4520c1bb4fbSDima Dorfman 	case SOPT_GET:
4530c1bb4fbSDima Dorfman 		switch (sopt->sopt_name) {
4540c1bb4fbSDima Dorfman 		case LOCAL_PEERCRED:
4550c1bb4fbSDima Dorfman 			if (unp->unp_flags & UNP_HAVEPC)
4560c1bb4fbSDima Dorfman 				error = sooptcopyout(sopt, &unp->unp_peercred,
4570c1bb4fbSDima Dorfman 				    sizeof(unp->unp_peercred));
4580c1bb4fbSDima Dorfman 			else {
4590c1bb4fbSDima Dorfman 				if (so->so_type == SOCK_STREAM)
4600c1bb4fbSDima Dorfman 					error = ENOTCONN;
4610c1bb4fbSDima Dorfman 				else
4620c1bb4fbSDima Dorfman 					error = EINVAL;
4630c1bb4fbSDima Dorfman 			}
4640c1bb4fbSDima Dorfman 			break;
4650c1bb4fbSDima Dorfman 		default:
4660c1bb4fbSDima Dorfman 			error = EOPNOTSUPP;
4670c1bb4fbSDima Dorfman 			break;
4680c1bb4fbSDima Dorfman 		}
4690c1bb4fbSDima Dorfman 		break;
4700c1bb4fbSDima Dorfman 	case SOPT_SET:
4710c1bb4fbSDima Dorfman 	default:
4720c1bb4fbSDima Dorfman 		error = EOPNOTSUPP;
4730c1bb4fbSDima Dorfman 		break;
4740c1bb4fbSDima Dorfman 	}
4750c1bb4fbSDima Dorfman 	return (error);
4760c1bb4fbSDima Dorfman }
4770c1bb4fbSDima Dorfman 
478df8bae1dSRodney W. Grimes /*
479df8bae1dSRodney W. Grimes  * Both send and receive buffers are allocated PIPSIZ bytes of buffering
480df8bae1dSRodney W. Grimes  * for stream sockets, although the total for sender and receiver is
481df8bae1dSRodney W. Grimes  * actually only PIPSIZ.
482df8bae1dSRodney W. Grimes  * Datagram sockets really use the sendspace as the maximum datagram size,
483df8bae1dSRodney W. Grimes  * and don't really want to reserve the sendspace.  Their recvspace should
484df8bae1dSRodney W. Grimes  * be large enough for at least one max-size datagram plus address.
485df8bae1dSRodney W. Grimes  */
4865dce41c5SJohn Dyson #ifndef PIPSIZ
4875dce41c5SJohn Dyson #define	PIPSIZ	8192
4885dce41c5SJohn Dyson #endif
489f708ef1bSPoul-Henning Kamp static u_long	unpst_sendspace = PIPSIZ;
490f708ef1bSPoul-Henning Kamp static u_long	unpst_recvspace = PIPSIZ;
491f708ef1bSPoul-Henning Kamp static u_long	unpdg_sendspace = 2*1024;	/* really max datagram size */
492f708ef1bSPoul-Henning Kamp static u_long	unpdg_recvspace = 4*1024;
493df8bae1dSRodney W. Grimes 
494f708ef1bSPoul-Henning Kamp static int	unp_rights;			/* file descriptors in flight */
495df8bae1dSRodney W. Grimes 
496ce02431fSDoug Rabson SYSCTL_DECL(_net_local_stream);
497639acc13SGarrett Wollman SYSCTL_INT(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW,
498639acc13SGarrett Wollman 	   &unpst_sendspace, 0, "");
499639acc13SGarrett Wollman SYSCTL_INT(_net_local_stream, OID_AUTO, recvspace, CTLFLAG_RW,
500639acc13SGarrett Wollman 	   &unpst_recvspace, 0, "");
501ce02431fSDoug Rabson SYSCTL_DECL(_net_local_dgram);
502639acc13SGarrett Wollman SYSCTL_INT(_net_local_dgram, OID_AUTO, maxdgram, CTLFLAG_RW,
503639acc13SGarrett Wollman 	   &unpdg_sendspace, 0, "");
504639acc13SGarrett Wollman SYSCTL_INT(_net_local_dgram, OID_AUTO, recvspace, CTLFLAG_RW,
505639acc13SGarrett Wollman 	   &unpdg_recvspace, 0, "");
506ce02431fSDoug Rabson SYSCTL_DECL(_net_local);
507639acc13SGarrett Wollman SYSCTL_INT(_net_local, OID_AUTO, inflight, CTLFLAG_RD, &unp_rights, 0, "");
508639acc13SGarrett Wollman 
509f708ef1bSPoul-Henning Kamp static int
510df8bae1dSRodney W. Grimes unp_attach(so)
511df8bae1dSRodney W. Grimes 	struct socket *so;
512df8bae1dSRodney W. Grimes {
513df8bae1dSRodney W. Grimes 	register struct unpcb *unp;
514df8bae1dSRodney W. Grimes 	int error;
515df8bae1dSRodney W. Grimes 
516df8bae1dSRodney W. Grimes 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
517df8bae1dSRodney W. Grimes 		switch (so->so_type) {
518df8bae1dSRodney W. Grimes 
519df8bae1dSRodney W. Grimes 		case SOCK_STREAM:
520df8bae1dSRodney W. Grimes 			error = soreserve(so, unpst_sendspace, unpst_recvspace);
521df8bae1dSRodney W. Grimes 			break;
522df8bae1dSRodney W. Grimes 
523df8bae1dSRodney W. Grimes 		case SOCK_DGRAM:
524df8bae1dSRodney W. Grimes 			error = soreserve(so, unpdg_sendspace, unpdg_recvspace);
525df8bae1dSRodney W. Grimes 			break;
526df8bae1dSRodney W. Grimes 
527df8bae1dSRodney W. Grimes 		default:
528df8bae1dSRodney W. Grimes 			panic("unp_attach");
529df8bae1dSRodney W. Grimes 		}
530df8bae1dSRodney W. Grimes 		if (error)
531df8bae1dSRodney W. Grimes 			return (error);
532df8bae1dSRodney W. Grimes 	}
53398271db4SGarrett Wollman 	unp = zalloc(unp_zone);
53457bf258eSGarrett Wollman 	if (unp == NULL)
535df8bae1dSRodney W. Grimes 		return (ENOBUFS);
53657bf258eSGarrett Wollman 	bzero(unp, sizeof *unp);
53798271db4SGarrett Wollman 	unp->unp_gencnt = ++unp_gencnt;
53898271db4SGarrett Wollman 	unp_count++;
53998271db4SGarrett Wollman 	LIST_INIT(&unp->unp_refs);
540df8bae1dSRodney W. Grimes 	unp->unp_socket = so;
541426da3bcSAlfred Perlstein 	FILEDESC_LOCK(curproc->p_fd);
542b40ce416SJulian Elischer 	unp->unp_rvnode = curthread->td_proc->p_fd->fd_rdir;
543426da3bcSAlfred Perlstein 	FILEDESC_UNLOCK(curproc->p_fd);
54498271db4SGarrett Wollman 	LIST_INSERT_HEAD(so->so_type == SOCK_DGRAM ? &unp_dhead
54598271db4SGarrett Wollman 			 : &unp_shead, unp, unp_link);
54698271db4SGarrett Wollman 	so->so_pcb = (caddr_t)unp;
547df8bae1dSRodney W. Grimes 	return (0);
548df8bae1dSRodney W. Grimes }
549df8bae1dSRodney W. Grimes 
550f708ef1bSPoul-Henning Kamp static void
551df8bae1dSRodney W. Grimes unp_detach(unp)
552df8bae1dSRodney W. Grimes 	register struct unpcb *unp;
553df8bae1dSRodney W. Grimes {
55498271db4SGarrett Wollman 	LIST_REMOVE(unp, unp_link);
55598271db4SGarrett Wollman 	unp->unp_gencnt = ++unp_gencnt;
55698271db4SGarrett Wollman 	--unp_count;
557df8bae1dSRodney W. Grimes 	if (unp->unp_vnode) {
558df8bae1dSRodney W. Grimes 		unp->unp_vnode->v_socket = 0;
559df8bae1dSRodney W. Grimes 		vrele(unp->unp_vnode);
560df8bae1dSRodney W. Grimes 		unp->unp_vnode = 0;
561df8bae1dSRodney W. Grimes 	}
562df8bae1dSRodney W. Grimes 	if (unp->unp_conn)
563df8bae1dSRodney W. Grimes 		unp_disconnect(unp);
5642e3c8fcbSPoul-Henning Kamp 	while (!LIST_EMPTY(&unp->unp_refs))
5652e3c8fcbSPoul-Henning Kamp 		unp_drop(LIST_FIRST(&unp->unp_refs), ECONNRESET);
566df8bae1dSRodney W. Grimes 	soisdisconnected(unp->unp_socket);
567df8bae1dSRodney W. Grimes 	unp->unp_socket->so_pcb = 0;
568df8bae1dSRodney W. Grimes 	if (unp_rights) {
569df8bae1dSRodney W. Grimes 		/*
570df8bae1dSRodney W. Grimes 		 * Normally the receive buffer is flushed later,
571df8bae1dSRodney W. Grimes 		 * in sofree, but if our receive buffer holds references
572df8bae1dSRodney W. Grimes 		 * to descriptors that are now garbage, we will dispose
573df8bae1dSRodney W. Grimes 		 * of those descriptor references after the garbage collector
574df8bae1dSRodney W. Grimes 		 * gets them (resulting in a "panic: closef: count < 0").
575df8bae1dSRodney W. Grimes 		 */
576df8bae1dSRodney W. Grimes 		sorflush(unp->unp_socket);
577df8bae1dSRodney W. Grimes 		unp_gc();
578df8bae1dSRodney W. Grimes 	}
57957bf258eSGarrett Wollman 	if (unp->unp_addr)
58057bf258eSGarrett Wollman 		FREE(unp->unp_addr, M_SONAME);
58198271db4SGarrett Wollman 	zfree(unp_zone, unp);
582df8bae1dSRodney W. Grimes }
583df8bae1dSRodney W. Grimes 
584f708ef1bSPoul-Henning Kamp static int
585b40ce416SJulian Elischer unp_bind(unp, nam, td)
586df8bae1dSRodney W. Grimes 	struct unpcb *unp;
58757bf258eSGarrett Wollman 	struct sockaddr *nam;
588b40ce416SJulian Elischer 	struct thread *td;
589df8bae1dSRodney W. Grimes {
59057bf258eSGarrett Wollman 	struct sockaddr_un *soun = (struct sockaddr_un *)nam;
591f2a2857bSKirk McKusick 	struct vnode *vp;
592f2a2857bSKirk McKusick 	struct mount *mp;
593df8bae1dSRodney W. Grimes 	struct vattr vattr;
59457bf258eSGarrett Wollman 	int error, namelen;
595df8bae1dSRodney W. Grimes 	struct nameidata nd;
5968f364875SJulian Elischer 	char *buf;
597df8bae1dSRodney W. Grimes 
598df8bae1dSRodney W. Grimes 	if (unp->unp_vnode != NULL)
599df8bae1dSRodney W. Grimes 		return (EINVAL);
60057bf258eSGarrett Wollman 	namelen = soun->sun_len - offsetof(struct sockaddr_un, sun_path);
60157bf258eSGarrett Wollman 	if (namelen <= 0)
60257bf258eSGarrett Wollman 		return EINVAL;
6038f364875SJulian Elischer 	buf = malloc(SOCK_MAXADDRLEN, M_TEMP, M_WAITOK);
60457bf258eSGarrett Wollman 	strncpy(buf, soun->sun_path, namelen);
60557bf258eSGarrett Wollman 	buf[namelen] = 0;	/* null-terminate the string */
606f2a2857bSKirk McKusick restart:
607974784e8SGuido van Rooij 	NDINIT(&nd, CREATE, NOFOLLOW | LOCKPARENT, UIO_SYSSPACE,
608b40ce416SJulian Elischer 	    buf, td);
609df8bae1dSRodney W. Grimes /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */
610797f2d22SPoul-Henning Kamp 	error = namei(&nd);
6118f364875SJulian Elischer 	if (error) {
6128f364875SJulian Elischer 		free(buf, M_TEMP);
613ad4ff090SJulian Elischer 		return (error);
6148f364875SJulian Elischer 	}
615df8bae1dSRodney W. Grimes 	vp = nd.ni_vp;
616f2a2857bSKirk McKusick 	if (vp != NULL || vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
617762e6b85SEivind Eklund 		NDFREE(&nd, NDF_ONLY_PNBUF);
618df8bae1dSRodney W. Grimes 		if (nd.ni_dvp == vp)
619df8bae1dSRodney W. Grimes 			vrele(nd.ni_dvp);
620df8bae1dSRodney W. Grimes 		else
621df8bae1dSRodney W. Grimes 			vput(nd.ni_dvp);
622f2a2857bSKirk McKusick 		if (vp != NULL) {
623df8bae1dSRodney W. Grimes 			vrele(vp);
6248f364875SJulian Elischer 			free(buf, M_TEMP);
625df8bae1dSRodney W. Grimes 			return (EADDRINUSE);
626df8bae1dSRodney W. Grimes 		}
6278f364875SJulian Elischer 		error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH);
6288f364875SJulian Elischer 		if (error) {
6298f364875SJulian Elischer 			free(buf, M_TEMP);
630f2a2857bSKirk McKusick 			return (error);
6318f364875SJulian Elischer 		}
632f2a2857bSKirk McKusick 		goto restart;
633f2a2857bSKirk McKusick 	}
634df8bae1dSRodney W. Grimes 	VATTR_NULL(&vattr);
635df8bae1dSRodney W. Grimes 	vattr.va_type = VSOCK;
636426da3bcSAlfred Perlstein 	FILEDESC_LOCK(td->td_proc->p_fd);
637b40ce416SJulian Elischer 	vattr.va_mode = (ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask);
638426da3bcSAlfred Perlstein 	FILEDESC_UNLOCK(td->td_proc->p_fd);
639a854ed98SJohn Baldwin 	VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
6407be2d300SMike Smith 	error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
641762e6b85SEivind Eklund 	NDFREE(&nd, NDF_ONLY_PNBUF);
6427be2d300SMike Smith 	vput(nd.ni_dvp);
6438f364875SJulian Elischer 	if (error) {
6448f364875SJulian Elischer 		free(buf, M_TEMP);
645df8bae1dSRodney W. Grimes 		return (error);
6468f364875SJulian Elischer 	}
647df8bae1dSRodney W. Grimes 	vp = nd.ni_vp;
648df8bae1dSRodney W. Grimes 	vp->v_socket = unp->unp_socket;
649df8bae1dSRodney W. Grimes 	unp->unp_vnode = vp;
65057bf258eSGarrett Wollman 	unp->unp_addr = (struct sockaddr_un *)dup_sockaddr(nam, 1);
651b40ce416SJulian Elischer 	VOP_UNLOCK(vp, 0, td);
652f2a2857bSKirk McKusick 	vn_finished_write(mp);
6538f364875SJulian Elischer 	free(buf, M_TEMP);
654df8bae1dSRodney W. Grimes 	return (0);
655df8bae1dSRodney W. Grimes }
656df8bae1dSRodney W. Grimes 
657f708ef1bSPoul-Henning Kamp static int
658b40ce416SJulian Elischer unp_connect(so, nam, td)
659df8bae1dSRodney W. Grimes 	struct socket *so;
66057bf258eSGarrett Wollman 	struct sockaddr *nam;
661b40ce416SJulian Elischer 	struct thread *td;
662df8bae1dSRodney W. Grimes {
66357bf258eSGarrett Wollman 	register struct sockaddr_un *soun = (struct sockaddr_un *)nam;
664df8bae1dSRodney W. Grimes 	register struct vnode *vp;
665df8bae1dSRodney W. Grimes 	register struct socket *so2, *so3;
6660c1bb4fbSDima Dorfman 	struct unpcb *unp, *unp2, *unp3;
66757bf258eSGarrett Wollman 	int error, len;
668df8bae1dSRodney W. Grimes 	struct nameidata nd;
66957bf258eSGarrett Wollman 	char buf[SOCK_MAXADDRLEN];
670df8bae1dSRodney W. Grimes 
67157bf258eSGarrett Wollman 	len = nam->sa_len - offsetof(struct sockaddr_un, sun_path);
67257bf258eSGarrett Wollman 	if (len <= 0)
67357bf258eSGarrett Wollman 		return EINVAL;
67457bf258eSGarrett Wollman 	strncpy(buf, soun->sun_path, len);
67557bf258eSGarrett Wollman 	buf[len] = 0;
67657bf258eSGarrett Wollman 
677b40ce416SJulian Elischer 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, buf, td);
678797f2d22SPoul-Henning Kamp 	error = namei(&nd);
679797f2d22SPoul-Henning Kamp 	if (error)
680df8bae1dSRodney W. Grimes 		return (error);
681df8bae1dSRodney W. Grimes 	vp = nd.ni_vp;
682762e6b85SEivind Eklund 	NDFREE(&nd, NDF_ONLY_PNBUF);
683df8bae1dSRodney W. Grimes 	if (vp->v_type != VSOCK) {
684df8bae1dSRodney W. Grimes 		error = ENOTSOCK;
685df8bae1dSRodney W. Grimes 		goto bad;
686df8bae1dSRodney W. Grimes 	}
687a854ed98SJohn Baldwin 	error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td);
688797f2d22SPoul-Henning Kamp 	if (error)
689df8bae1dSRodney W. Grimes 		goto bad;
690df8bae1dSRodney W. Grimes 	so2 = vp->v_socket;
691df8bae1dSRodney W. Grimes 	if (so2 == 0) {
692df8bae1dSRodney W. Grimes 		error = ECONNREFUSED;
693df8bae1dSRodney W. Grimes 		goto bad;
694df8bae1dSRodney W. Grimes 	}
695df8bae1dSRodney W. Grimes 	if (so->so_type != so2->so_type) {
696df8bae1dSRodney W. Grimes 		error = EPROTOTYPE;
697df8bae1dSRodney W. Grimes 		goto bad;
698df8bae1dSRodney W. Grimes 	}
699df8bae1dSRodney W. Grimes 	if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
700df8bae1dSRodney W. Grimes 		if ((so2->so_options & SO_ACCEPTCONN) == 0 ||
701f8cf411eSRobert Watson 		    (so3 = sonewconn(so2, 0)) == 0) {
702df8bae1dSRodney W. Grimes 			error = ECONNREFUSED;
703df8bae1dSRodney W. Grimes 			goto bad;
704df8bae1dSRodney W. Grimes 		}
7050c1bb4fbSDima Dorfman 		unp = sotounpcb(so);
706df8bae1dSRodney W. Grimes 		unp2 = sotounpcb(so2);
707df8bae1dSRodney W. Grimes 		unp3 = sotounpcb(so3);
708df8bae1dSRodney W. Grimes 		if (unp2->unp_addr)
70957bf258eSGarrett Wollman 			unp3->unp_addr = (struct sockaddr_un *)
71057bf258eSGarrett Wollman 				dup_sockaddr((struct sockaddr *)
71157bf258eSGarrett Wollman 					     unp2->unp_addr, 1);
7120c1bb4fbSDima Dorfman 
7130c1bb4fbSDima Dorfman 		/*
7140c1bb4fbSDima Dorfman 		 * unp_peercred management:
7150c1bb4fbSDima Dorfman 		 *
7160c1bb4fbSDima Dorfman 		 * The connecter's (client's) credentials are copied
7170c1bb4fbSDima Dorfman 		 * from its process structure at the time of connect()
7180c1bb4fbSDima Dorfman 		 * (which is now).
7190c1bb4fbSDima Dorfman 		 */
720a854ed98SJohn Baldwin 		cru2x(td->td_ucred, &unp3->unp_peercred);
7210c1bb4fbSDima Dorfman 		unp3->unp_flags |= UNP_HAVEPC;
7220c1bb4fbSDima Dorfman 		/*
7230c1bb4fbSDima Dorfman 		 * The receiver's (server's) credentials are copied
7240c1bb4fbSDima Dorfman 		 * from the unp_peercred member of socket on which the
7250c1bb4fbSDima Dorfman 		 * former called listen(); unp_listen() cached that
7260c1bb4fbSDima Dorfman 		 * process's credentials at that time so we can use
7270c1bb4fbSDima Dorfman 		 * them now.
7280c1bb4fbSDima Dorfman 		 */
7290c1bb4fbSDima Dorfman 		KASSERT(unp2->unp_flags & UNP_HAVEPCCACHED,
7300c1bb4fbSDima Dorfman 		    ("unp_connect: listener without cached peercred"));
7310c1bb4fbSDima Dorfman 		memcpy(&unp->unp_peercred, &unp2->unp_peercred,
7320c1bb4fbSDima Dorfman 		    sizeof(unp->unp_peercred));
7330c1bb4fbSDima Dorfman 		unp->unp_flags |= UNP_HAVEPC;
7340c1bb4fbSDima Dorfman 
735df8bae1dSRodney W. Grimes 		so2 = so3;
736df8bae1dSRodney W. Grimes 	}
737df8bae1dSRodney W. Grimes 	error = unp_connect2(so, so2);
738df8bae1dSRodney W. Grimes bad:
739df8bae1dSRodney W. Grimes 	vput(vp);
740df8bae1dSRodney W. Grimes 	return (error);
741df8bae1dSRodney W. Grimes }
742df8bae1dSRodney W. Grimes 
74326f9a767SRodney W. Grimes int
744df8bae1dSRodney W. Grimes unp_connect2(so, so2)
745df8bae1dSRodney W. Grimes 	register struct socket *so;
746df8bae1dSRodney W. Grimes 	register struct socket *so2;
747df8bae1dSRodney W. Grimes {
748df8bae1dSRodney W. Grimes 	register struct unpcb *unp = sotounpcb(so);
749df8bae1dSRodney W. Grimes 	register struct unpcb *unp2;
750df8bae1dSRodney W. Grimes 
751df8bae1dSRodney W. Grimes 	if (so2->so_type != so->so_type)
752df8bae1dSRodney W. Grimes 		return (EPROTOTYPE);
753df8bae1dSRodney W. Grimes 	unp2 = sotounpcb(so2);
754df8bae1dSRodney W. Grimes 	unp->unp_conn = unp2;
755df8bae1dSRodney W. Grimes 	switch (so->so_type) {
756df8bae1dSRodney W. Grimes 
757df8bae1dSRodney W. Grimes 	case SOCK_DGRAM:
75898271db4SGarrett Wollman 		LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink);
759df8bae1dSRodney W. Grimes 		soisconnected(so);
760df8bae1dSRodney W. Grimes 		break;
761df8bae1dSRodney W. Grimes 
762df8bae1dSRodney W. Grimes 	case SOCK_STREAM:
763df8bae1dSRodney W. Grimes 		unp2->unp_conn = unp;
764df8bae1dSRodney W. Grimes 		soisconnected(so);
765df8bae1dSRodney W. Grimes 		soisconnected(so2);
766df8bae1dSRodney W. Grimes 		break;
767df8bae1dSRodney W. Grimes 
768df8bae1dSRodney W. Grimes 	default:
769df8bae1dSRodney W. Grimes 		panic("unp_connect2");
770df8bae1dSRodney W. Grimes 	}
771df8bae1dSRodney W. Grimes 	return (0);
772df8bae1dSRodney W. Grimes }
773df8bae1dSRodney W. Grimes 
774f708ef1bSPoul-Henning Kamp static void
775df8bae1dSRodney W. Grimes unp_disconnect(unp)
776df8bae1dSRodney W. Grimes 	struct unpcb *unp;
777df8bae1dSRodney W. Grimes {
778df8bae1dSRodney W. Grimes 	register struct unpcb *unp2 = unp->unp_conn;
779df8bae1dSRodney W. Grimes 
780df8bae1dSRodney W. Grimes 	if (unp2 == 0)
781df8bae1dSRodney W. Grimes 		return;
782df8bae1dSRodney W. Grimes 	unp->unp_conn = 0;
783df8bae1dSRodney W. Grimes 	switch (unp->unp_socket->so_type) {
784df8bae1dSRodney W. Grimes 
785df8bae1dSRodney W. Grimes 	case SOCK_DGRAM:
78698271db4SGarrett Wollman 		LIST_REMOVE(unp, unp_reflink);
787df8bae1dSRodney W. Grimes 		unp->unp_socket->so_state &= ~SS_ISCONNECTED;
788df8bae1dSRodney W. Grimes 		break;
789df8bae1dSRodney W. Grimes 
790df8bae1dSRodney W. Grimes 	case SOCK_STREAM:
791df8bae1dSRodney W. Grimes 		soisdisconnected(unp->unp_socket);
792df8bae1dSRodney W. Grimes 		unp2->unp_conn = 0;
793df8bae1dSRodney W. Grimes 		soisdisconnected(unp2->unp_socket);
794df8bae1dSRodney W. Grimes 		break;
795df8bae1dSRodney W. Grimes 	}
796df8bae1dSRodney W. Grimes }
797df8bae1dSRodney W. Grimes 
798df8bae1dSRodney W. Grimes #ifdef notdef
79926f9a767SRodney W. Grimes void
800df8bae1dSRodney W. Grimes unp_abort(unp)
801df8bae1dSRodney W. Grimes 	struct unpcb *unp;
802df8bae1dSRodney W. Grimes {
803df8bae1dSRodney W. Grimes 
804df8bae1dSRodney W. Grimes 	unp_detach(unp);
805df8bae1dSRodney W. Grimes }
806df8bae1dSRodney W. Grimes #endif
807df8bae1dSRodney W. Grimes 
80898271db4SGarrett Wollman static int
80982d9ae4eSPoul-Henning Kamp unp_pcblist(SYSCTL_HANDLER_ARGS)
81098271db4SGarrett Wollman {
811f5ef029eSPoul-Henning Kamp 	int error, i, n;
81298271db4SGarrett Wollman 	struct unpcb *unp, **unp_list;
81398271db4SGarrett Wollman 	unp_gen_t gencnt;
8148f364875SJulian Elischer 	struct xunpgen *xug;
81598271db4SGarrett Wollman 	struct unp_head *head;
8168f364875SJulian Elischer 	struct xunpcb *xu;
81798271db4SGarrett Wollman 
818a23d65bfSBruce Evans 	head = ((intptr_t)arg1 == SOCK_DGRAM ? &unp_dhead : &unp_shead);
81998271db4SGarrett Wollman 
82098271db4SGarrett Wollman 	/*
82198271db4SGarrett Wollman 	 * The process of preparing the PCB list is too time-consuming and
82298271db4SGarrett Wollman 	 * resource-intensive to repeat twice on every request.
82398271db4SGarrett Wollman 	 */
82498271db4SGarrett Wollman 	if (req->oldptr == 0) {
82598271db4SGarrett Wollman 		n = unp_count;
8268f364875SJulian Elischer 		req->oldidx = 2 * (sizeof *xug)
82798271db4SGarrett Wollman 			+ (n + n/8) * sizeof(struct xunpcb);
82898271db4SGarrett Wollman 		return 0;
82998271db4SGarrett Wollman 	}
83098271db4SGarrett Wollman 
83198271db4SGarrett Wollman 	if (req->newptr != 0)
83298271db4SGarrett Wollman 		return EPERM;
83398271db4SGarrett Wollman 
83498271db4SGarrett Wollman 	/*
83598271db4SGarrett Wollman 	 * OK, now we're committed to doing something.
83698271db4SGarrett Wollman 	 */
8378f364875SJulian Elischer 	xug = malloc(sizeof(*xug), M_TEMP, M_WAITOK);
83898271db4SGarrett Wollman 	gencnt = unp_gencnt;
83998271db4SGarrett Wollman 	n = unp_count;
84098271db4SGarrett Wollman 
8418f364875SJulian Elischer 	xug->xug_len = sizeof *xug;
8428f364875SJulian Elischer 	xug->xug_count = n;
8438f364875SJulian Elischer 	xug->xug_gen = gencnt;
8448f364875SJulian Elischer 	xug->xug_sogen = so_gencnt;
8458f364875SJulian Elischer 	error = SYSCTL_OUT(req, xug, sizeof *xug);
8468f364875SJulian Elischer 	if (error) {
8478f364875SJulian Elischer 		free(xug, M_TEMP);
84898271db4SGarrett Wollman 		return error;
8498f364875SJulian Elischer 	}
85098271db4SGarrett Wollman 
85198271db4SGarrett Wollman 	unp_list = malloc(n * sizeof *unp_list, M_TEMP, M_WAITOK);
85298271db4SGarrett Wollman 
8532e3c8fcbSPoul-Henning Kamp 	for (unp = LIST_FIRST(head), i = 0; unp && i < n;
8542e3c8fcbSPoul-Henning Kamp 	     unp = LIST_NEXT(unp, unp_link)) {
8558a7d8cc6SRobert Watson 		if (unp->unp_gencnt <= gencnt) {
856a854ed98SJohn Baldwin 			if (cr_cansee(req->td->td_ucred,
8578a7d8cc6SRobert Watson 			    unp->unp_socket->so_cred))
8584787fd37SPaul Saab 				continue;
85998271db4SGarrett Wollman 			unp_list[i++] = unp;
86098271db4SGarrett Wollman 		}
8614787fd37SPaul Saab 	}
86298271db4SGarrett Wollman 	n = i;			/* in case we lost some during malloc */
86398271db4SGarrett Wollman 
86498271db4SGarrett Wollman 	error = 0;
8658f364875SJulian Elischer 	xu = malloc(sizeof(*xu), M_TEMP, M_WAITOK);
86698271db4SGarrett Wollman 	for (i = 0; i < n; i++) {
86798271db4SGarrett Wollman 		unp = unp_list[i];
86898271db4SGarrett Wollman 		if (unp->unp_gencnt <= gencnt) {
8698f364875SJulian Elischer 			xu->xu_len = sizeof *xu;
8708f364875SJulian Elischer 			xu->xu_unpp = unp;
87198271db4SGarrett Wollman 			/*
87298271db4SGarrett Wollman 			 * XXX - need more locking here to protect against
87398271db4SGarrett Wollman 			 * connect/disconnect races for SMP.
87498271db4SGarrett Wollman 			 */
87598271db4SGarrett Wollman 			if (unp->unp_addr)
8768f364875SJulian Elischer 				bcopy(unp->unp_addr, &xu->xu_addr,
87798271db4SGarrett Wollman 				      unp->unp_addr->sun_len);
87898271db4SGarrett Wollman 			if (unp->unp_conn && unp->unp_conn->unp_addr)
87998271db4SGarrett Wollman 				bcopy(unp->unp_conn->unp_addr,
8808f364875SJulian Elischer 				      &xu->xu_caddr,
88198271db4SGarrett Wollman 				      unp->unp_conn->unp_addr->sun_len);
8828f364875SJulian Elischer 			bcopy(unp, &xu->xu_unp, sizeof *unp);
8838f364875SJulian Elischer 			sotoxsocket(unp->unp_socket, &xu->xu_socket);
8848f364875SJulian Elischer 			error = SYSCTL_OUT(req, xu, sizeof *xu);
88598271db4SGarrett Wollman 		}
88698271db4SGarrett Wollman 	}
8878f364875SJulian Elischer 	free(xu, M_TEMP);
88898271db4SGarrett Wollman 	if (!error) {
88998271db4SGarrett Wollman 		/*
89098271db4SGarrett Wollman 		 * Give the user an updated idea of our state.
89198271db4SGarrett Wollman 		 * If the generation differs from what we told
89298271db4SGarrett Wollman 		 * her before, she knows that something happened
89398271db4SGarrett Wollman 		 * while we were processing this request, and it
89498271db4SGarrett Wollman 		 * might be necessary to retry.
89598271db4SGarrett Wollman 		 */
8968f364875SJulian Elischer 		xug->xug_gen = unp_gencnt;
8978f364875SJulian Elischer 		xug->xug_sogen = so_gencnt;
8988f364875SJulian Elischer 		xug->xug_count = unp_count;
8998f364875SJulian Elischer 		error = SYSCTL_OUT(req, xug, sizeof *xug);
90098271db4SGarrett Wollman 	}
90198271db4SGarrett Wollman 	free(unp_list, M_TEMP);
9028f364875SJulian Elischer 	free(xug, M_TEMP);
90398271db4SGarrett Wollman 	return error;
90498271db4SGarrett Wollman }
90598271db4SGarrett Wollman 
90698271db4SGarrett Wollman SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLFLAG_RD,
90798271db4SGarrett Wollman 	    (caddr_t)(long)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb",
90898271db4SGarrett Wollman 	    "List of active local datagram sockets");
90998271db4SGarrett Wollman SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, CTLFLAG_RD,
91098271db4SGarrett Wollman 	    (caddr_t)(long)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb",
91198271db4SGarrett Wollman 	    "List of active local stream sockets");
91298271db4SGarrett Wollman 
913f708ef1bSPoul-Henning Kamp static void
914df8bae1dSRodney W. Grimes unp_shutdown(unp)
915df8bae1dSRodney W. Grimes 	struct unpcb *unp;
916df8bae1dSRodney W. Grimes {
917df8bae1dSRodney W. Grimes 	struct socket *so;
918df8bae1dSRodney W. Grimes 
919df8bae1dSRodney W. Grimes 	if (unp->unp_socket->so_type == SOCK_STREAM && unp->unp_conn &&
920df8bae1dSRodney W. Grimes 	    (so = unp->unp_conn->unp_socket))
921df8bae1dSRodney W. Grimes 		socantrcvmore(so);
922df8bae1dSRodney W. Grimes }
923df8bae1dSRodney W. Grimes 
924f708ef1bSPoul-Henning Kamp static void
925df8bae1dSRodney W. Grimes unp_drop(unp, errno)
926df8bae1dSRodney W. Grimes 	struct unpcb *unp;
927df8bae1dSRodney W. Grimes 	int errno;
928df8bae1dSRodney W. Grimes {
929df8bae1dSRodney W. Grimes 	struct socket *so = unp->unp_socket;
930df8bae1dSRodney W. Grimes 
931df8bae1dSRodney W. Grimes 	so->so_error = errno;
932df8bae1dSRodney W. Grimes 	unp_disconnect(unp);
933df8bae1dSRodney W. Grimes }
934df8bae1dSRodney W. Grimes 
935df8bae1dSRodney W. Grimes #ifdef notdef
93626f9a767SRodney W. Grimes void
937df8bae1dSRodney W. Grimes unp_drain()
938df8bae1dSRodney W. Grimes {
939df8bae1dSRodney W. Grimes 
940df8bae1dSRodney W. Grimes }
941df8bae1dSRodney W. Grimes #endif
942df8bae1dSRodney W. Grimes 
9432bc21ed9SDavid Malone static void
9442bc21ed9SDavid Malone unp_freerights(rp, fdcount)
9452bc21ed9SDavid Malone 	struct file **rp;
9462bc21ed9SDavid Malone 	int fdcount;
947df8bae1dSRodney W. Grimes {
9482bc21ed9SDavid Malone 	int i;
9492bc21ed9SDavid Malone 	struct file *fp;
950df8bae1dSRodney W. Grimes 
9512bc21ed9SDavid Malone 	for (i = 0; i < fdcount; i++) {
952df8bae1dSRodney W. Grimes 		fp = *rp;
9538692c025SYoshinobu Inoue 		/*
9542bc21ed9SDavid Malone 		 * zero the pointer before calling
9552bc21ed9SDavid Malone 		 * unp_discard since it may end up
9562bc21ed9SDavid Malone 		 * in unp_gc()..
9578692c025SYoshinobu Inoue 		 */
958df8bae1dSRodney W. Grimes 		*rp++ = 0;
9598692c025SYoshinobu Inoue 		unp_discard(fp);
960df8bae1dSRodney W. Grimes 	}
9612bc21ed9SDavid Malone }
9622bc21ed9SDavid Malone 
9632bc21ed9SDavid Malone int
9642bc21ed9SDavid Malone unp_externalize(control, controlp)
9652bc21ed9SDavid Malone 	struct mbuf *control, **controlp;
9662bc21ed9SDavid Malone {
9672bc21ed9SDavid Malone 	struct thread *td = curthread;		/* XXX */
9682bc21ed9SDavid Malone 	struct cmsghdr *cm = mtod(control, struct cmsghdr *);
9692bc21ed9SDavid Malone 	int i;
9702bc21ed9SDavid Malone 	int *fdp;
9712bc21ed9SDavid Malone 	struct file **rp;
9722bc21ed9SDavid Malone 	struct file *fp;
9732bc21ed9SDavid Malone 	void *data;
9742bc21ed9SDavid Malone 	socklen_t clen = control->m_len, datalen;
9752bc21ed9SDavid Malone 	int error, newfds;
9762bc21ed9SDavid Malone 	int f;
9772bc21ed9SDavid Malone 	u_int newlen;
9782bc21ed9SDavid Malone 
9792bc21ed9SDavid Malone 	error = 0;
9802bc21ed9SDavid Malone 	if (controlp != NULL) /* controlp == NULL => free control messages */
9812bc21ed9SDavid Malone 		*controlp = NULL;
9822bc21ed9SDavid Malone 
9832bc21ed9SDavid Malone 	while (cm != NULL) {
9842bc21ed9SDavid Malone 		if (sizeof(*cm) > clen || cm->cmsg_len > clen) {
9852bc21ed9SDavid Malone 			error = EINVAL;
9862bc21ed9SDavid Malone 			break;
9872bc21ed9SDavid Malone 		}
9882bc21ed9SDavid Malone 
9892bc21ed9SDavid Malone 		data = CMSG_DATA(cm);
9902bc21ed9SDavid Malone 		datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
9912bc21ed9SDavid Malone 
9922bc21ed9SDavid Malone 		if (cm->cmsg_level == SOL_SOCKET
9932bc21ed9SDavid Malone 		    && cm->cmsg_type == SCM_RIGHTS) {
9942bc21ed9SDavid Malone 			newfds = datalen / sizeof(struct file *);
9952bc21ed9SDavid Malone 			rp = data;
9962bc21ed9SDavid Malone 
9972bc21ed9SDavid Malone 			/* If we're not outputting the discriptors free them. */
9982bc21ed9SDavid Malone 			if (error || controlp == NULL) {
9992bc21ed9SDavid Malone 				unp_freerights(rp, newfds);
10002bc21ed9SDavid Malone 				goto next;
10012bc21ed9SDavid Malone 			}
1002426da3bcSAlfred Perlstein 			FILEDESC_LOCK(td->td_proc->p_fd);
10032bc21ed9SDavid Malone 			/* if the new FD's will not fit free them.  */
10042bc21ed9SDavid Malone 			if (!fdavail(td, newfds)) {
1005426da3bcSAlfred Perlstein 				FILEDESC_UNLOCK(td->td_proc->p_fd);
10062bc21ed9SDavid Malone 				error = EMSGSIZE;
10072bc21ed9SDavid Malone 				unp_freerights(rp, newfds);
10082bc21ed9SDavid Malone 				goto next;
1009df8bae1dSRodney W. Grimes 			}
1010ed5b7817SJulian Elischer 			/*
10112bc21ed9SDavid Malone 			 * now change each pointer to an fd in the global
10122bc21ed9SDavid Malone 			 * table to an integer that is the index to the
10132bc21ed9SDavid Malone 			 * local fd table entry that we set up to point
10142bc21ed9SDavid Malone 			 * to the global one we are transferring.
1015ed5b7817SJulian Elischer 			 */
10162bc21ed9SDavid Malone 			newlen = newfds * sizeof(int);
10172bc21ed9SDavid Malone 			*controlp = sbcreatecontrol(NULL, newlen,
10182bc21ed9SDavid Malone 			    SCM_RIGHTS, SOL_SOCKET);
10192bc21ed9SDavid Malone 			if (*controlp == NULL) {
1020426da3bcSAlfred Perlstein 				FILEDESC_UNLOCK(td->td_proc->p_fd);
10212bc21ed9SDavid Malone 				error = E2BIG;
10222bc21ed9SDavid Malone 				unp_freerights(rp, newfds);
10232bc21ed9SDavid Malone 				goto next;
10242bc21ed9SDavid Malone 			}
10252bc21ed9SDavid Malone 
10262bc21ed9SDavid Malone 			fdp = (int *)
10272bc21ed9SDavid Malone 			    CMSG_DATA(mtod(*controlp, struct cmsghdr *));
1028df8bae1dSRodney W. Grimes 			for (i = 0; i < newfds; i++) {
1029b40ce416SJulian Elischer 				if (fdalloc(td, 0, &f))
10302bc21ed9SDavid Malone 					panic("unp_externalize fdalloc failed");
10318692c025SYoshinobu Inoue 				fp = *rp++;
1032b40ce416SJulian Elischer 				td->td_proc->p_fd->fd_ofiles[f] = fp;
1033426da3bcSAlfred Perlstein 				FILE_LOCK(fp);
1034df8bae1dSRodney W. Grimes 				fp->f_msgcount--;
1035426da3bcSAlfred Perlstein 				FILE_UNLOCK(fp);
1036df8bae1dSRodney W. Grimes 				unp_rights--;
10378692c025SYoshinobu Inoue 				*fdp++ = f;
1038df8bae1dSRodney W. Grimes 			}
1039426da3bcSAlfred Perlstein 			FILEDESC_UNLOCK(td->td_proc->p_fd);
10402bc21ed9SDavid Malone 		} else { /* We can just copy anything else across */
10412bc21ed9SDavid Malone 			if (error || controlp == NULL)
10422bc21ed9SDavid Malone 				goto next;
10432bc21ed9SDavid Malone 			*controlp = sbcreatecontrol(NULL, datalen,
10442bc21ed9SDavid Malone 			    cm->cmsg_type, cm->cmsg_level);
10452bc21ed9SDavid Malone 			if (*controlp == NULL) {
10462bc21ed9SDavid Malone 				error = ENOBUFS;
10472bc21ed9SDavid Malone 				goto next;
10482bc21ed9SDavid Malone 			}
10492bc21ed9SDavid Malone 			bcopy(data,
10502bc21ed9SDavid Malone 			    CMSG_DATA(mtod(*controlp, struct cmsghdr *)),
10512bc21ed9SDavid Malone 			    datalen);
10522bc21ed9SDavid Malone 		}
10532bc21ed9SDavid Malone 
10542bc21ed9SDavid Malone 		controlp = &(*controlp)->m_next;
10552bc21ed9SDavid Malone 
10562bc21ed9SDavid Malone next:
10572bc21ed9SDavid Malone 		if (CMSG_SPACE(datalen) < clen) {
10582bc21ed9SDavid Malone 			clen -= CMSG_SPACE(datalen);
10592bc21ed9SDavid Malone 			cm = (struct cmsghdr *)
10602bc21ed9SDavid Malone 			    ((caddr_t)cm + CMSG_SPACE(datalen));
10618692c025SYoshinobu Inoue 		} else {
10622bc21ed9SDavid Malone 			clen = 0;
10632bc21ed9SDavid Malone 			cm = NULL;
10648692c025SYoshinobu Inoue 		}
10658692c025SYoshinobu Inoue 	}
10668692c025SYoshinobu Inoue 
10672bc21ed9SDavid Malone 	m_freem(control);
10682bc21ed9SDavid Malone 
10692bc21ed9SDavid Malone 	return (error);
1070df8bae1dSRodney W. Grimes }
1071df8bae1dSRodney W. Grimes 
107298271db4SGarrett Wollman void
107398271db4SGarrett Wollman unp_init(void)
107498271db4SGarrett Wollman {
107598271db4SGarrett Wollman 	unp_zone = zinit("unpcb", sizeof(struct unpcb), nmbclusters, 0, 0);
107698271db4SGarrett Wollman 	if (unp_zone == 0)
107798271db4SGarrett Wollman 		panic("unp_init");
107898271db4SGarrett Wollman 	LIST_INIT(&unp_dhead);
107998271db4SGarrett Wollman 	LIST_INIT(&unp_shead);
108098271db4SGarrett Wollman }
108198271db4SGarrett Wollman 
10820b788fa1SBill Paul #ifndef MIN
10830b788fa1SBill Paul #define	MIN(a,b) (((a)<(b))?(a):(b))
10840b788fa1SBill Paul #endif
10850b788fa1SBill Paul 
1086f708ef1bSPoul-Henning Kamp static int
10872bc21ed9SDavid Malone unp_internalize(controlp, td)
10882bc21ed9SDavid Malone 	struct mbuf **controlp;
1089b40ce416SJulian Elischer 	struct thread *td;
1090df8bae1dSRodney W. Grimes {
10912bc21ed9SDavid Malone 	struct mbuf *control = *controlp;
1092b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
10938692c025SYoshinobu Inoue 	struct filedesc *fdescp = p->p_fd;
10942bc21ed9SDavid Malone 	struct cmsghdr *cm = mtod(control, struct cmsghdr *);
10952bc21ed9SDavid Malone 	struct cmsgcred *cmcred;
10962bc21ed9SDavid Malone 	struct file **rp;
10972bc21ed9SDavid Malone 	struct file *fp;
10982bc21ed9SDavid Malone 	struct timeval *tv;
10992bc21ed9SDavid Malone 	int i, fd, *fdp;
11002bc21ed9SDavid Malone 	void *data;
11012bc21ed9SDavid Malone 	socklen_t clen = control->m_len, datalen;
11022bc21ed9SDavid Malone 	int error, oldfds;
11038692c025SYoshinobu Inoue 	u_int newlen;
1104df8bae1dSRodney W. Grimes 
11052bc21ed9SDavid Malone 	error = 0;
11062bc21ed9SDavid Malone 	*controlp = NULL;
11070b788fa1SBill Paul 
11082bc21ed9SDavid Malone 	while (cm != NULL) {
11092bc21ed9SDavid Malone 		if (sizeof(*cm) > clen || cm->cmsg_level != SOL_SOCKET
11102bc21ed9SDavid Malone 		    || cm->cmsg_len > clen) {
11112bc21ed9SDavid Malone 			error = EINVAL;
11122bc21ed9SDavid Malone 			goto out;
11132bc21ed9SDavid Malone 		}
11142bc21ed9SDavid Malone 
11152bc21ed9SDavid Malone 		data = CMSG_DATA(cm);
11162bc21ed9SDavid Malone 		datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
11172bc21ed9SDavid Malone 
11182bc21ed9SDavid Malone 		switch (cm->cmsg_type) {
11190b788fa1SBill Paul 		/*
11200b788fa1SBill Paul 		 * Fill in credential information.
11210b788fa1SBill Paul 		 */
11222bc21ed9SDavid Malone 		case SCM_CREDS:
11232bc21ed9SDavid Malone 			*controlp = sbcreatecontrol(NULL, sizeof(*cmcred),
11242bc21ed9SDavid Malone 			    SCM_CREDS, SOL_SOCKET);
11252bc21ed9SDavid Malone 			if (*controlp == NULL) {
11262bc21ed9SDavid Malone 				error = ENOBUFS;
11272bc21ed9SDavid Malone 				goto out;
11282bc21ed9SDavid Malone 			}
11292bc21ed9SDavid Malone 
11302bc21ed9SDavid Malone 			cmcred = (struct cmsgcred *)
11312bc21ed9SDavid Malone 			    CMSG_DATA(mtod(*controlp, struct cmsghdr *));
11320b788fa1SBill Paul 			cmcred->cmcred_pid = p->p_pid;
1133a854ed98SJohn Baldwin 			cmcred->cmcred_uid = td->td_ucred->cr_ruid;
1134a854ed98SJohn Baldwin 			cmcred->cmcred_gid = td->td_ucred->cr_rgid;
1135a854ed98SJohn Baldwin 			cmcred->cmcred_euid = td->td_ucred->cr_uid;
1136a854ed98SJohn Baldwin 			cmcred->cmcred_ngroups = MIN(td->td_ucred->cr_ngroups,
11370b788fa1SBill Paul 							CMGROUP_MAX);
11380b788fa1SBill Paul 			for (i = 0; i < cmcred->cmcred_ngroups; i++)
11392bc21ed9SDavid Malone 				cmcred->cmcred_groups[i] =
1140a854ed98SJohn Baldwin 				    td->td_ucred->cr_groups[i];
11412bc21ed9SDavid Malone 			break;
11420b788fa1SBill Paul 
11432bc21ed9SDavid Malone 		case SCM_RIGHTS:
11442bc21ed9SDavid Malone 			oldfds = datalen / sizeof (int);
1145ed5b7817SJulian Elischer 			/*
11462bc21ed9SDavid Malone 			 * check that all the FDs passed in refer to legal files
1147ed5b7817SJulian Elischer 			 * If not, reject the entire operation.
1148ed5b7817SJulian Elischer 			 */
11492bc21ed9SDavid Malone 			fdp = data;
1150426da3bcSAlfred Perlstein 			FILEDESC_LOCK(fdescp);
1151df8bae1dSRodney W. Grimes 			for (i = 0; i < oldfds; i++) {
11528692c025SYoshinobu Inoue 				fd = *fdp++;
11538692c025SYoshinobu Inoue 				if ((unsigned)fd >= fdescp->fd_nfiles ||
11542bc21ed9SDavid Malone 				    fdescp->fd_ofiles[fd] == NULL) {
1155426da3bcSAlfred Perlstein 					FILEDESC_UNLOCK(fdescp);
11562bc21ed9SDavid Malone 					error = EBADF;
11572bc21ed9SDavid Malone 					goto out;
11582bc21ed9SDavid Malone 				}
1159df8bae1dSRodney W. Grimes 			}
1160ed5b7817SJulian Elischer 			/*
1161ed5b7817SJulian Elischer 			 * Now replace the integer FDs with pointers to
1162ed5b7817SJulian Elischer 			 * the associated global file table entry..
1163ed5b7817SJulian Elischer 			 */
11642bc21ed9SDavid Malone 			newlen = oldfds * sizeof(struct file *);
11652bc21ed9SDavid Malone 			*controlp = sbcreatecontrol(NULL, newlen,
11662bc21ed9SDavid Malone 			    SCM_RIGHTS, SOL_SOCKET);
11672bc21ed9SDavid Malone 			if (*controlp == NULL) {
1168426da3bcSAlfred Perlstein 				FILEDESC_UNLOCK(fdescp);
11692bc21ed9SDavid Malone 				error = E2BIG;
11702bc21ed9SDavid Malone 				goto out;
11718692c025SYoshinobu Inoue 			}
11728692c025SYoshinobu Inoue 
11732bc21ed9SDavid Malone 			fdp = data;
11742bc21ed9SDavid Malone 			rp = (struct file **)
11752bc21ed9SDavid Malone 			    CMSG_DATA(mtod(*controlp, struct cmsghdr *));
11768692c025SYoshinobu Inoue 			for (i = 0; i < oldfds; i++) {
11778692c025SYoshinobu Inoue 				fp = fdescp->fd_ofiles[*fdp++];
1178df8bae1dSRodney W. Grimes 				*rp++ = fp;
1179426da3bcSAlfred Perlstein 				FILE_LOCK(fp);
1180df8bae1dSRodney W. Grimes 				fp->f_count++;
1181df8bae1dSRodney W. Grimes 				fp->f_msgcount++;
1182426da3bcSAlfred Perlstein 				FILE_UNLOCK(fp);
1183df8bae1dSRodney W. Grimes 				unp_rights++;
1184df8bae1dSRodney W. Grimes 			}
1185426da3bcSAlfred Perlstein 			FILEDESC_UNLOCK(fdescp);
11862bc21ed9SDavid Malone 			break;
11872bc21ed9SDavid Malone 
11882bc21ed9SDavid Malone 		case SCM_TIMESTAMP:
11892bc21ed9SDavid Malone 			*controlp = sbcreatecontrol(NULL, sizeof(*tv),
11902bc21ed9SDavid Malone 			    SCM_TIMESTAMP, SOL_SOCKET);
11912bc21ed9SDavid Malone 			if (*controlp == NULL) {
11922bc21ed9SDavid Malone 				error = ENOBUFS;
11932bc21ed9SDavid Malone 				goto out;
11948692c025SYoshinobu Inoue 			}
11952bc21ed9SDavid Malone 			tv = (struct timeval *)
11962bc21ed9SDavid Malone 			    CMSG_DATA(mtod(*controlp, struct cmsghdr *));
11972bc21ed9SDavid Malone 			microtime(tv);
11982bc21ed9SDavid Malone 			break;
11992bc21ed9SDavid Malone 
12002bc21ed9SDavid Malone 		default:
12012bc21ed9SDavid Malone 			error = EINVAL;
12022bc21ed9SDavid Malone 			goto out;
12032bc21ed9SDavid Malone 		}
12042bc21ed9SDavid Malone 
12052bc21ed9SDavid Malone 		controlp = &(*controlp)->m_next;
12062bc21ed9SDavid Malone 
12072bc21ed9SDavid Malone 		if (CMSG_SPACE(datalen) < clen) {
12082bc21ed9SDavid Malone 			clen -= CMSG_SPACE(datalen);
12092bc21ed9SDavid Malone 			cm = (struct cmsghdr *)
12102bc21ed9SDavid Malone 			    ((caddr_t)cm + CMSG_SPACE(datalen));
12112bc21ed9SDavid Malone 		} else {
12122bc21ed9SDavid Malone 			clen = 0;
12132bc21ed9SDavid Malone 			cm = NULL;
12142bc21ed9SDavid Malone 		}
12152bc21ed9SDavid Malone 	}
12162bc21ed9SDavid Malone 
12172bc21ed9SDavid Malone out:
12182bc21ed9SDavid Malone 	m_freem(control);
12192bc21ed9SDavid Malone 
12202bc21ed9SDavid Malone 	return (error);
1221df8bae1dSRodney W. Grimes }
1222df8bae1dSRodney W. Grimes 
1223f708ef1bSPoul-Henning Kamp static int	unp_defer, unp_gcing;
1224df8bae1dSRodney W. Grimes 
1225f708ef1bSPoul-Henning Kamp static void
1226df8bae1dSRodney W. Grimes unp_gc()
1227df8bae1dSRodney W. Grimes {
1228df8bae1dSRodney W. Grimes 	register struct file *fp, *nextfp;
1229df8bae1dSRodney W. Grimes 	register struct socket *so;
1230df8bae1dSRodney W. Grimes 	struct file **extra_ref, **fpp;
1231df8bae1dSRodney W. Grimes 	int nunref, i;
1232df8bae1dSRodney W. Grimes 
1233df8bae1dSRodney W. Grimes 	if (unp_gcing)
1234df8bae1dSRodney W. Grimes 		return;
1235df8bae1dSRodney W. Grimes 	unp_gcing = 1;
1236df8bae1dSRodney W. Grimes 	unp_defer = 0;
1237ed5b7817SJulian Elischer 	/*
1238ed5b7817SJulian Elischer 	 * before going through all this, set all FDs to
1239ed5b7817SJulian Elischer 	 * be NOT defered and NOT externally accessible
1240ed5b7817SJulian Elischer 	 */
1241426da3bcSAlfred Perlstein 	sx_slock(&filelist_lock);
12422e3c8fcbSPoul-Henning Kamp 	LIST_FOREACH(fp, &filehead, f_list)
1243426da3bcSAlfred Perlstein 		fp->f_gcflag &= ~(FMARK|FDEFER);
1244df8bae1dSRodney W. Grimes 	do {
12452e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(fp, &filehead, f_list) {
1246426da3bcSAlfred Perlstein 			FILE_LOCK(fp);
1247ed5b7817SJulian Elischer 			/*
1248ed5b7817SJulian Elischer 			 * If the file is not open, skip it
1249ed5b7817SJulian Elischer 			 */
1250426da3bcSAlfred Perlstein 			if (fp->f_count == 0) {
1251426da3bcSAlfred Perlstein 				FILE_UNLOCK(fp);
1252df8bae1dSRodney W. Grimes 				continue;
1253426da3bcSAlfred Perlstein 			}
1254ed5b7817SJulian Elischer 			/*
1255ed5b7817SJulian Elischer 			 * If we already marked it as 'defer'  in a
1256ed5b7817SJulian Elischer 			 * previous pass, then try process it this time
1257ed5b7817SJulian Elischer 			 * and un-mark it
1258ed5b7817SJulian Elischer 			 */
1259426da3bcSAlfred Perlstein 			if (fp->f_gcflag & FDEFER) {
1260426da3bcSAlfred Perlstein 				fp->f_gcflag &= ~FDEFER;
1261df8bae1dSRodney W. Grimes 				unp_defer--;
1262df8bae1dSRodney W. Grimes 			} else {
1263ed5b7817SJulian Elischer 				/*
1264ed5b7817SJulian Elischer 				 * if it's not defered, then check if it's
1265ed5b7817SJulian Elischer 				 * already marked.. if so skip it
1266ed5b7817SJulian Elischer 				 */
1267426da3bcSAlfred Perlstein 				if (fp->f_gcflag & FMARK) {
1268426da3bcSAlfred Perlstein 					FILE_UNLOCK(fp);
1269df8bae1dSRodney W. Grimes 					continue;
1270426da3bcSAlfred Perlstein 				}
1271ed5b7817SJulian Elischer 				/*
1272ed5b7817SJulian Elischer 				 * If all references are from messages
1273ed5b7817SJulian Elischer 				 * in transit, then skip it. it's not
1274ed5b7817SJulian Elischer 				 * externally accessible.
1275ed5b7817SJulian Elischer 				 */
1276426da3bcSAlfred Perlstein 				if (fp->f_count == fp->f_msgcount) {
1277426da3bcSAlfred Perlstein 					FILE_UNLOCK(fp);
1278df8bae1dSRodney W. Grimes 					continue;
1279426da3bcSAlfred Perlstein 				}
1280ed5b7817SJulian Elischer 				/*
1281ed5b7817SJulian Elischer 				 * If it got this far then it must be
1282ed5b7817SJulian Elischer 				 * externally accessible.
1283ed5b7817SJulian Elischer 				 */
1284426da3bcSAlfred Perlstein 				fp->f_gcflag |= FMARK;
1285df8bae1dSRodney W. Grimes 			}
1286ed5b7817SJulian Elischer 			/*
1287ed5b7817SJulian Elischer 			 * either it was defered, or it is externally
1288ed5b7817SJulian Elischer 			 * accessible and not already marked so.
1289ed5b7817SJulian Elischer 			 * Now check if it is possibly one of OUR sockets.
1290ed5b7817SJulian Elischer 			 */
1291df8bae1dSRodney W. Grimes 			if (fp->f_type != DTYPE_SOCKET ||
1292426da3bcSAlfred Perlstein 			    (so = (struct socket *)fp->f_data) == 0) {
1293426da3bcSAlfred Perlstein 				FILE_UNLOCK(fp);
1294df8bae1dSRodney W. Grimes 				continue;
1295426da3bcSAlfred Perlstein 			}
1296426da3bcSAlfred Perlstein 			FILE_UNLOCK(fp);
1297748e0b0aSGarrett Wollman 			if (so->so_proto->pr_domain != &localdomain ||
1298df8bae1dSRodney W. Grimes 			    (so->so_proto->pr_flags&PR_RIGHTS) == 0)
1299df8bae1dSRodney W. Grimes 				continue;
1300df8bae1dSRodney W. Grimes #ifdef notdef
1301df8bae1dSRodney W. Grimes 			if (so->so_rcv.sb_flags & SB_LOCK) {
1302df8bae1dSRodney W. Grimes 				/*
1303df8bae1dSRodney W. Grimes 				 * This is problematical; it's not clear
1304df8bae1dSRodney W. Grimes 				 * we need to wait for the sockbuf to be
1305df8bae1dSRodney W. Grimes 				 * unlocked (on a uniprocessor, at least),
1306df8bae1dSRodney W. Grimes 				 * and it's also not clear what to do
1307df8bae1dSRodney W. Grimes 				 * if sbwait returns an error due to receipt
1308df8bae1dSRodney W. Grimes 				 * of a signal.  If sbwait does return
1309df8bae1dSRodney W. Grimes 				 * an error, we'll go into an infinite
1310df8bae1dSRodney W. Grimes 				 * loop.  Delete all of this for now.
1311df8bae1dSRodney W. Grimes 				 */
1312df8bae1dSRodney W. Grimes 				(void) sbwait(&so->so_rcv);
1313df8bae1dSRodney W. Grimes 				goto restart;
1314df8bae1dSRodney W. Grimes 			}
1315df8bae1dSRodney W. Grimes #endif
1316ed5b7817SJulian Elischer 			/*
1317ed5b7817SJulian Elischer 			 * So, Ok, it's one of our sockets and it IS externally
1318ed5b7817SJulian Elischer 			 * accessible (or was defered). Now we look
1319dc733423SDag-Erling Smørgrav 			 * to see if we hold any file descriptors in its
1320ed5b7817SJulian Elischer 			 * message buffers. Follow those links and mark them
1321ed5b7817SJulian Elischer 			 * as accessible too.
1322ed5b7817SJulian Elischer 			 */
1323df8bae1dSRodney W. Grimes 			unp_scan(so->so_rcv.sb_mb, unp_mark);
1324df8bae1dSRodney W. Grimes 		}
1325df8bae1dSRodney W. Grimes 	} while (unp_defer);
1326426da3bcSAlfred Perlstein 	sx_sunlock(&filelist_lock);
1327df8bae1dSRodney W. Grimes 	/*
1328df8bae1dSRodney W. Grimes 	 * We grab an extra reference to each of the file table entries
1329df8bae1dSRodney W. Grimes 	 * that are not otherwise accessible and then free the rights
1330df8bae1dSRodney W. Grimes 	 * that are stored in messages on them.
1331df8bae1dSRodney W. Grimes 	 *
1332df8bae1dSRodney W. Grimes 	 * The bug in the orginal code is a little tricky, so I'll describe
1333df8bae1dSRodney W. Grimes 	 * what's wrong with it here.
1334df8bae1dSRodney W. Grimes 	 *
1335df8bae1dSRodney W. Grimes 	 * It is incorrect to simply unp_discard each entry for f_msgcount
1336df8bae1dSRodney W. Grimes 	 * times -- consider the case of sockets A and B that contain
1337df8bae1dSRodney W. Grimes 	 * references to each other.  On a last close of some other socket,
1338df8bae1dSRodney W. Grimes 	 * we trigger a gc since the number of outstanding rights (unp_rights)
1339df8bae1dSRodney W. Grimes 	 * is non-zero.  If during the sweep phase the gc code un_discards,
1340df8bae1dSRodney W. Grimes 	 * we end up doing a (full) closef on the descriptor.  A closef on A
1341df8bae1dSRodney W. Grimes 	 * results in the following chain.  Closef calls soo_close, which
1342df8bae1dSRodney W. Grimes 	 * calls soclose.   Soclose calls first (through the switch
1343df8bae1dSRodney W. Grimes 	 * uipc_usrreq) unp_detach, which re-invokes unp_gc.  Unp_gc simply
1344df8bae1dSRodney W. Grimes 	 * returns because the previous instance had set unp_gcing, and
1345df8bae1dSRodney W. Grimes 	 * we return all the way back to soclose, which marks the socket
1346df8bae1dSRodney W. Grimes 	 * with SS_NOFDREF, and then calls sofree.  Sofree calls sorflush
1347df8bae1dSRodney W. Grimes 	 * to free up the rights that are queued in messages on the socket A,
1348df8bae1dSRodney W. Grimes 	 * i.e., the reference on B.  The sorflush calls via the dom_dispose
1349df8bae1dSRodney W. Grimes 	 * switch unp_dispose, which unp_scans with unp_discard.  This second
1350df8bae1dSRodney W. Grimes 	 * instance of unp_discard just calls closef on B.
1351df8bae1dSRodney W. Grimes 	 *
1352df8bae1dSRodney W. Grimes 	 * Well, a similar chain occurs on B, resulting in a sorflush on B,
1353df8bae1dSRodney W. Grimes 	 * which results in another closef on A.  Unfortunately, A is already
1354df8bae1dSRodney W. Grimes 	 * being closed, and the descriptor has already been marked with
1355df8bae1dSRodney W. Grimes 	 * SS_NOFDREF, and soclose panics at this point.
1356df8bae1dSRodney W. Grimes 	 *
1357df8bae1dSRodney W. Grimes 	 * Here, we first take an extra reference to each inaccessible
1358df8bae1dSRodney W. Grimes 	 * descriptor.  Then, we call sorflush ourself, since we know
1359df8bae1dSRodney W. Grimes 	 * it is a Unix domain socket anyhow.  After we destroy all the
1360df8bae1dSRodney W. Grimes 	 * rights carried in messages, we do a last closef to get rid
1361df8bae1dSRodney W. Grimes 	 * of our extra reference.  This is the last close, and the
1362df8bae1dSRodney W. Grimes 	 * unp_detach etc will shut down the socket.
1363df8bae1dSRodney W. Grimes 	 *
1364df8bae1dSRodney W. Grimes 	 * 91/09/19, bsy@cs.cmu.edu
1365df8bae1dSRodney W. Grimes 	 */
1366df8bae1dSRodney W. Grimes 	extra_ref = malloc(nfiles * sizeof(struct file *), M_FILE, M_WAITOK);
1367426da3bcSAlfred Perlstein 	sx_slock(&filelist_lock);
13682e3c8fcbSPoul-Henning Kamp 	for (nunref = 0, fp = LIST_FIRST(&filehead), fpp = extra_ref; fp != 0;
1369bc6f0e79SJeffrey Hsu 	    fp = nextfp) {
13702e3c8fcbSPoul-Henning Kamp 		nextfp = LIST_NEXT(fp, f_list);
1371426da3bcSAlfred Perlstein 		FILE_LOCK(fp);
1372ed5b7817SJulian Elischer 		/*
1373ed5b7817SJulian Elischer 		 * If it's not open, skip it
1374ed5b7817SJulian Elischer 		 */
1375426da3bcSAlfred Perlstein 		if (fp->f_count == 0) {
1376426da3bcSAlfred Perlstein 			FILE_UNLOCK(fp);
1377df8bae1dSRodney W. Grimes 			continue;
1378426da3bcSAlfred Perlstein 		}
1379ed5b7817SJulian Elischer 		/*
1380ed5b7817SJulian Elischer 		 * If all refs are from msgs, and it's not marked accessible
1381ed5b7817SJulian Elischer 		 * then it must be referenced from some unreachable cycle
1382ed5b7817SJulian Elischer 		 * of (shut-down) FDs, so include it in our
1383ed5b7817SJulian Elischer 		 * list of FDs to remove
1384ed5b7817SJulian Elischer 		 */
1385426da3bcSAlfred Perlstein 		if (fp->f_count == fp->f_msgcount && !(fp->f_gcflag & FMARK)) {
1386df8bae1dSRodney W. Grimes 			*fpp++ = fp;
1387df8bae1dSRodney W. Grimes 			nunref++;
1388df8bae1dSRodney W. Grimes 			fp->f_count++;
1389df8bae1dSRodney W. Grimes 		}
1390426da3bcSAlfred Perlstein 		FILE_UNLOCK(fp);
1391df8bae1dSRodney W. Grimes 	}
1392426da3bcSAlfred Perlstein 	sx_sunlock(&filelist_lock);
1393ed5b7817SJulian Elischer 	/*
1394ed5b7817SJulian Elischer 	 * for each FD on our hit list, do the following two things
1395ed5b7817SJulian Elischer 	 */
13961c7c3c6aSMatthew Dillon 	for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) {
13971c7c3c6aSMatthew Dillon 		struct file *tfp = *fpp;
1398426da3bcSAlfred Perlstein 		FILE_LOCK(tfp);
1399426da3bcSAlfred Perlstein 		if (tfp->f_type == DTYPE_SOCKET && tfp->f_data != NULL) {
1400426da3bcSAlfred Perlstein 			FILE_UNLOCK(tfp);
14011c7c3c6aSMatthew Dillon 			sorflush((struct socket *)(tfp->f_data));
1402426da3bcSAlfred Perlstein 		} else
1403426da3bcSAlfred Perlstein 			FILE_UNLOCK(tfp);
14041c7c3c6aSMatthew Dillon 	}
1405df8bae1dSRodney W. Grimes 	for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp)
1406b40ce416SJulian Elischer 		closef(*fpp, (struct thread *) NULL);
1407df8bae1dSRodney W. Grimes 	free((caddr_t)extra_ref, M_FILE);
1408df8bae1dSRodney W. Grimes 	unp_gcing = 0;
1409df8bae1dSRodney W. Grimes }
1410df8bae1dSRodney W. Grimes 
141126f9a767SRodney W. Grimes void
1412df8bae1dSRodney W. Grimes unp_dispose(m)
1413df8bae1dSRodney W. Grimes 	struct mbuf *m;
1414df8bae1dSRodney W. Grimes {
1415996c772fSJohn Dyson 
1416df8bae1dSRodney W. Grimes 	if (m)
1417df8bae1dSRodney W. Grimes 		unp_scan(m, unp_discard);
1418df8bae1dSRodney W. Grimes }
1419df8bae1dSRodney W. Grimes 
14200c1bb4fbSDima Dorfman static int
14210c1bb4fbSDima Dorfman unp_listen(unp, p)
14220c1bb4fbSDima Dorfman 	struct unpcb *unp;
14230c1bb4fbSDima Dorfman 	struct proc *p;
14240c1bb4fbSDima Dorfman {
14250c1bb4fbSDima Dorfman 
142676183f34SDima Dorfman 	cru2x(p->p_ucred, &unp->unp_peercred);
14270c1bb4fbSDima Dorfman 	unp->unp_flags |= UNP_HAVEPCCACHED;
14280c1bb4fbSDima Dorfman 	return (0);
14290c1bb4fbSDima Dorfman }
14300c1bb4fbSDima Dorfman 
1431f708ef1bSPoul-Henning Kamp static void
1432df8bae1dSRodney W. Grimes unp_scan(m0, op)
1433df8bae1dSRodney W. Grimes 	register struct mbuf *m0;
1434996c772fSJohn Dyson 	void (*op) __P((struct file *));
1435df8bae1dSRodney W. Grimes {
14362bc21ed9SDavid Malone 	struct mbuf *m;
14372bc21ed9SDavid Malone 	struct file **rp;
14382bc21ed9SDavid Malone 	struct cmsghdr *cm;
14392bc21ed9SDavid Malone 	void *data;
14402bc21ed9SDavid Malone 	int i;
14412bc21ed9SDavid Malone 	socklen_t clen, datalen;
1442df8bae1dSRodney W. Grimes 	int qfds;
1443df8bae1dSRodney W. Grimes 
1444df8bae1dSRodney W. Grimes 	while (m0) {
14452bc21ed9SDavid Malone 		for (m = m0; m; m = m->m_next) {
144612396bdcSDavid Malone 			if (m->m_type != MT_CONTROL)
1447df8bae1dSRodney W. Grimes 				continue;
14482bc21ed9SDavid Malone 
14492bc21ed9SDavid Malone 			cm = mtod(m, struct cmsghdr *);
14502bc21ed9SDavid Malone 			clen = m->m_len;
14512bc21ed9SDavid Malone 
14522bc21ed9SDavid Malone 			while (cm != NULL) {
14532bc21ed9SDavid Malone 				if (sizeof(*cm) > clen || cm->cmsg_len > clen)
14542bc21ed9SDavid Malone 					break;
14552bc21ed9SDavid Malone 
14562bc21ed9SDavid Malone 				data = CMSG_DATA(cm);
14572bc21ed9SDavid Malone 				datalen = (caddr_t)cm + cm->cmsg_len
14582bc21ed9SDavid Malone 				    - (caddr_t)data;
14592bc21ed9SDavid Malone 
14602bc21ed9SDavid Malone 				if (cm->cmsg_level == SOL_SOCKET &&
14612bc21ed9SDavid Malone 				    cm->cmsg_type == SCM_RIGHTS) {
14622bc21ed9SDavid Malone 					qfds = datalen / sizeof (struct file *);
14632bc21ed9SDavid Malone 					rp = data;
1464df8bae1dSRodney W. Grimes 					for (i = 0; i < qfds; i++)
1465df8bae1dSRodney W. Grimes 						(*op)(*rp++);
14662bc21ed9SDavid Malone 				}
14672bc21ed9SDavid Malone 
14682bc21ed9SDavid Malone 				if (CMSG_SPACE(datalen) < clen) {
14692bc21ed9SDavid Malone 					clen -= CMSG_SPACE(datalen);
14702bc21ed9SDavid Malone 					cm = (struct cmsghdr *)
14712bc21ed9SDavid Malone 					    ((caddr_t)cm + CMSG_SPACE(datalen));
14722bc21ed9SDavid Malone 				} else {
14732bc21ed9SDavid Malone 					clen = 0;
14742bc21ed9SDavid Malone 					cm = NULL;
14752bc21ed9SDavid Malone 				}
14762bc21ed9SDavid Malone 			}
1477df8bae1dSRodney W. Grimes 		}
1478df8bae1dSRodney W. Grimes 		m0 = m0->m_act;
1479df8bae1dSRodney W. Grimes 	}
1480df8bae1dSRodney W. Grimes }
1481df8bae1dSRodney W. Grimes 
1482f708ef1bSPoul-Henning Kamp static void
1483df8bae1dSRodney W. Grimes unp_mark(fp)
1484df8bae1dSRodney W. Grimes 	struct file *fp;
1485df8bae1dSRodney W. Grimes {
1486426da3bcSAlfred Perlstein 	if (fp->f_gcflag & FMARK)
1487df8bae1dSRodney W. Grimes 		return;
1488df8bae1dSRodney W. Grimes 	unp_defer++;
1489426da3bcSAlfred Perlstein 	fp->f_gcflag |= (FMARK|FDEFER);
1490df8bae1dSRodney W. Grimes }
1491df8bae1dSRodney W. Grimes 
1492f708ef1bSPoul-Henning Kamp static void
1493df8bae1dSRodney W. Grimes unp_discard(fp)
1494df8bae1dSRodney W. Grimes 	struct file *fp;
1495df8bae1dSRodney W. Grimes {
1496426da3bcSAlfred Perlstein 	FILE_LOCK(fp);
1497df8bae1dSRodney W. Grimes 	fp->f_msgcount--;
1498df8bae1dSRodney W. Grimes 	unp_rights--;
1499426da3bcSAlfred Perlstein 	FILE_UNLOCK(fp);
1500b40ce416SJulian Elischer 	(void) closef(fp, (struct thread *)NULL);
1501df8bae1dSRodney W. Grimes }
1502