xref: /freebsd/sys/kern/uipc_usrreq.c (revision 2bc21ed9856748bd3510da944f11597975ef8d81)
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>
60df8bae1dSRodney W. Grimes 
6198271db4SGarrett Wollman #include <vm/vm_zone.h>
6298271db4SGarrett Wollman 
63632a035fSEivind Eklund static	struct vm_zone *unp_zone;
6498271db4SGarrett Wollman static	unp_gen_t unp_gencnt;
6598271db4SGarrett Wollman static	u_int unp_count;
6698271db4SGarrett Wollman 
6798271db4SGarrett Wollman static	struct unp_head unp_shead, unp_dhead;
6898271db4SGarrett Wollman 
69df8bae1dSRodney W. Grimes /*
70df8bae1dSRodney W. Grimes  * Unix communications domain.
71df8bae1dSRodney W. Grimes  *
72df8bae1dSRodney W. Grimes  * TODO:
73df8bae1dSRodney W. Grimes  *	SEQPACKET, RDM
74df8bae1dSRodney W. Grimes  *	rethink name space problems
75df8bae1dSRodney W. Grimes  *	need a proper out-of-band
7698271db4SGarrett Wollman  *	lock pushdown
77df8bae1dSRodney W. Grimes  */
78f708ef1bSPoul-Henning Kamp static struct	sockaddr sun_noname = { sizeof(sun_noname), AF_LOCAL };
79f708ef1bSPoul-Henning Kamp static ino_t	unp_ino;		/* prototype for fake inode numbers */
80f708ef1bSPoul-Henning Kamp 
81f708ef1bSPoul-Henning Kamp static int     unp_attach __P((struct socket *));
82f708ef1bSPoul-Henning Kamp static void    unp_detach __P((struct unpcb *));
83b40ce416SJulian Elischer static int     unp_bind __P((struct unpcb *,struct sockaddr *, struct thread *));
8457bf258eSGarrett Wollman static int     unp_connect __P((struct socket *,struct sockaddr *,
85b40ce416SJulian Elischer 				struct thread *));
86f708ef1bSPoul-Henning Kamp static void    unp_disconnect __P((struct unpcb *));
87f708ef1bSPoul-Henning Kamp static void    unp_shutdown __P((struct unpcb *));
88f708ef1bSPoul-Henning Kamp static void    unp_drop __P((struct unpcb *, int));
89f708ef1bSPoul-Henning Kamp static void    unp_gc __P((void));
90f708ef1bSPoul-Henning Kamp static void    unp_scan __P((struct mbuf *, void (*)(struct file *)));
91f708ef1bSPoul-Henning Kamp static void    unp_mark __P((struct file *));
92f708ef1bSPoul-Henning Kamp static void    unp_discard __P((struct file *));
932bc21ed9SDavid Malone static void    unp_freerights __P((struct file **, int));
942bc21ed9SDavid Malone static int     unp_internalize __P((struct mbuf **, struct thread *));
950c1bb4fbSDima Dorfman static int     unp_listen __P((struct unpcb *, struct proc *));
96f708ef1bSPoul-Henning Kamp 
97a29f300eSGarrett Wollman static int
98a29f300eSGarrett Wollman uipc_abort(struct socket *so)
99df8bae1dSRodney W. Grimes {
100df8bae1dSRodney W. Grimes 	struct unpcb *unp = sotounpcb(so);
101df8bae1dSRodney W. Grimes 
102a29f300eSGarrett Wollman 	if (unp == 0)
103a29f300eSGarrett Wollman 		return EINVAL;
104a29f300eSGarrett Wollman 	unp_drop(unp, ECONNABORTED);
105a29f300eSGarrett Wollman 	return 0;
106df8bae1dSRodney W. Grimes }
107df8bae1dSRodney W. Grimes 
108a29f300eSGarrett Wollman static int
10957bf258eSGarrett Wollman uipc_accept(struct socket *so, struct sockaddr **nam)
110a29f300eSGarrett Wollman {
111a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so);
112df8bae1dSRodney W. Grimes 
113a29f300eSGarrett Wollman 	if (unp == 0)
114a29f300eSGarrett Wollman 		return EINVAL;
115df8bae1dSRodney W. Grimes 
116df8bae1dSRodney W. Grimes 	/*
117df8bae1dSRodney W. Grimes 	 * Pass back name of connected socket,
118df8bae1dSRodney W. Grimes 	 * if it was bound and we are still connected
119df8bae1dSRodney W. Grimes 	 * (our peer may have closed already!).
120df8bae1dSRodney W. Grimes 	 */
121df8bae1dSRodney W. Grimes 	if (unp->unp_conn && unp->unp_conn->unp_addr) {
12257bf258eSGarrett Wollman 		*nam = dup_sockaddr((struct sockaddr *)unp->unp_conn->unp_addr,
12357bf258eSGarrett Wollman 				    1);
124df8bae1dSRodney W. Grimes 	} else {
12557bf258eSGarrett Wollman 		*nam = dup_sockaddr((struct sockaddr *)&sun_noname, 1);
126df8bae1dSRodney W. Grimes 	}
127a29f300eSGarrett Wollman 	return 0;
128a29f300eSGarrett Wollman }
129df8bae1dSRodney W. Grimes 
130a29f300eSGarrett Wollman static int
131b40ce416SJulian Elischer uipc_attach(struct socket *so, int proto, struct thread *td)
132a29f300eSGarrett Wollman {
133a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so);
134df8bae1dSRodney W. Grimes 
135a29f300eSGarrett Wollman 	if (unp != 0)
136a29f300eSGarrett Wollman 		return EISCONN;
137a29f300eSGarrett Wollman 	return unp_attach(so);
138a29f300eSGarrett Wollman }
139a29f300eSGarrett Wollman 
140a29f300eSGarrett Wollman static int
141b40ce416SJulian Elischer uipc_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
142a29f300eSGarrett Wollman {
143a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so);
144a29f300eSGarrett Wollman 
145a29f300eSGarrett Wollman 	if (unp == 0)
146a29f300eSGarrett Wollman 		return EINVAL;
147a29f300eSGarrett Wollman 
148b40ce416SJulian Elischer 	return unp_bind(unp, nam, td);
149a29f300eSGarrett Wollman }
150a29f300eSGarrett Wollman 
151a29f300eSGarrett Wollman static int
152b40ce416SJulian Elischer uipc_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
153a29f300eSGarrett Wollman {
154a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so);
155a29f300eSGarrett Wollman 
156a29f300eSGarrett Wollman 	if (unp == 0)
157a29f300eSGarrett Wollman 		return EINVAL;
158b40ce416SJulian Elischer 	return unp_connect(so, nam, curthread);
159a29f300eSGarrett Wollman }
160a29f300eSGarrett Wollman 
161a29f300eSGarrett Wollman static int
162a29f300eSGarrett Wollman uipc_connect2(struct socket *so1, struct socket *so2)
163a29f300eSGarrett Wollman {
164a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so1);
165a29f300eSGarrett Wollman 
166a29f300eSGarrett Wollman 	if (unp == 0)
167a29f300eSGarrett Wollman 		return EINVAL;
168a29f300eSGarrett Wollman 
169a29f300eSGarrett Wollman 	return unp_connect2(so1, so2);
170a29f300eSGarrett Wollman }
171a29f300eSGarrett Wollman 
172a29f300eSGarrett Wollman /* control is EOPNOTSUPP */
173a29f300eSGarrett Wollman 
174a29f300eSGarrett Wollman static int
175a29f300eSGarrett Wollman uipc_detach(struct socket *so)
176a29f300eSGarrett Wollman {
177a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so);
178a29f300eSGarrett Wollman 
179a29f300eSGarrett Wollman 	if (unp == 0)
180a29f300eSGarrett Wollman 		return EINVAL;
181a29f300eSGarrett Wollman 
182a29f300eSGarrett Wollman 	unp_detach(unp);
183a29f300eSGarrett Wollman 	return 0;
184a29f300eSGarrett Wollman }
185a29f300eSGarrett Wollman 
186a29f300eSGarrett Wollman static int
187a29f300eSGarrett Wollman uipc_disconnect(struct socket *so)
188a29f300eSGarrett Wollman {
189a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so);
190a29f300eSGarrett Wollman 
191a29f300eSGarrett Wollman 	if (unp == 0)
192a29f300eSGarrett Wollman 		return EINVAL;
193a29f300eSGarrett Wollman 	unp_disconnect(unp);
194a29f300eSGarrett Wollman 	return 0;
195a29f300eSGarrett Wollman }
196a29f300eSGarrett Wollman 
197a29f300eSGarrett Wollman static int
198b40ce416SJulian Elischer uipc_listen(struct socket *so, struct thread *td)
199a29f300eSGarrett Wollman {
200a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so);
201a29f300eSGarrett Wollman 
202a29f300eSGarrett Wollman 	if (unp == 0 || unp->unp_vnode == 0)
203a29f300eSGarrett Wollman 		return EINVAL;
204b40ce416SJulian Elischer 	return unp_listen(unp, td->td_proc);
205a29f300eSGarrett Wollman }
206a29f300eSGarrett Wollman 
207a29f300eSGarrett Wollman static int
20857bf258eSGarrett Wollman uipc_peeraddr(struct socket *so, struct sockaddr **nam)
209a29f300eSGarrett Wollman {
210a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so);
211a29f300eSGarrett Wollman 
212a29f300eSGarrett Wollman 	if (unp == 0)
213a29f300eSGarrett Wollman 		return EINVAL;
21457bf258eSGarrett Wollman 	if (unp->unp_conn && unp->unp_conn->unp_addr)
21557bf258eSGarrett Wollman 		*nam = dup_sockaddr((struct sockaddr *)unp->unp_conn->unp_addr,
21657bf258eSGarrett Wollman 				    1);
217a29f300eSGarrett Wollman 	return 0;
218a29f300eSGarrett Wollman }
219a29f300eSGarrett Wollman 
220a29f300eSGarrett Wollman static int
221a29f300eSGarrett Wollman uipc_rcvd(struct socket *so, int flags)
222a29f300eSGarrett Wollman {
223a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so);
224a29f300eSGarrett Wollman 	struct socket *so2;
2256aef685fSBrian Feldman 	u_long newhiwat;
226a29f300eSGarrett Wollman 
227a29f300eSGarrett Wollman 	if (unp == 0)
228a29f300eSGarrett Wollman 		return EINVAL;
229df8bae1dSRodney W. Grimes 	switch (so->so_type) {
230df8bae1dSRodney W. Grimes 	case SOCK_DGRAM:
231a29f300eSGarrett Wollman 		panic("uipc_rcvd DGRAM?");
232df8bae1dSRodney W. Grimes 		/*NOTREACHED*/
233df8bae1dSRodney W. Grimes 
234df8bae1dSRodney W. Grimes 	case SOCK_STREAM:
235df8bae1dSRodney W. Grimes 		if (unp->unp_conn == 0)
236df8bae1dSRodney W. Grimes 			break;
237df8bae1dSRodney W. Grimes 		so2 = unp->unp_conn->unp_socket;
238df8bae1dSRodney W. Grimes 		/*
239df8bae1dSRodney W. Grimes 		 * Adjust backpressure on sender
240df8bae1dSRodney W. Grimes 		 * and wakeup any waiting to write.
241df8bae1dSRodney W. Grimes 		 */
242ff8b0106SBrian Feldman 		so2->so_snd.sb_mbmax += unp->unp_mbcnt - so->so_rcv.sb_mbcnt;
243ff8b0106SBrian Feldman 		unp->unp_mbcnt = so->so_rcv.sb_mbcnt;
2446aef685fSBrian Feldman 		newhiwat = so2->so_snd.sb_hiwat + unp->unp_cc -
2456aef685fSBrian Feldman 		    so->so_rcv.sb_cc;
246f535380cSDon Lewis 		(void)chgsbsize(so2->so_cred->cr_uidinfo, &so2->so_snd.sb_hiwat,
2476aef685fSBrian Feldman 		    newhiwat, RLIM_INFINITY);
248ff8b0106SBrian Feldman 		unp->unp_cc = so->so_rcv.sb_cc;
249df8bae1dSRodney W. Grimes 		sowwakeup(so2);
250df8bae1dSRodney W. Grimes 		break;
251df8bae1dSRodney W. Grimes 
252df8bae1dSRodney W. Grimes 	default:
253a29f300eSGarrett Wollman 		panic("uipc_rcvd unknown socktype");
254df8bae1dSRodney W. Grimes 	}
255a29f300eSGarrett Wollman 	return 0;
256a29f300eSGarrett Wollman }
257df8bae1dSRodney W. Grimes 
258a29f300eSGarrett Wollman /* pru_rcvoob is EOPNOTSUPP */
259a29f300eSGarrett Wollman 
260a29f300eSGarrett Wollman static int
26157bf258eSGarrett Wollman uipc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
262b40ce416SJulian Elischer 	  struct mbuf *control, struct thread *td)
263a29f300eSGarrett Wollman {
264a29f300eSGarrett Wollman 	int error = 0;
265a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so);
266a29f300eSGarrett Wollman 	struct socket *so2;
2676aef685fSBrian Feldman 	u_long newhiwat;
268a29f300eSGarrett Wollman 
269a29f300eSGarrett Wollman 	if (unp == 0) {
270a29f300eSGarrett Wollman 		error = EINVAL;
271a29f300eSGarrett Wollman 		goto release;
272a29f300eSGarrett Wollman 	}
273a29f300eSGarrett Wollman 	if (flags & PRUS_OOB) {
274a29f300eSGarrett Wollman 		error = EOPNOTSUPP;
275a29f300eSGarrett Wollman 		goto release;
276a29f300eSGarrett Wollman 	}
277a29f300eSGarrett Wollman 
2782bc21ed9SDavid Malone 	if (control && (error = unp_internalize(&control, td)))
279a29f300eSGarrett Wollman 		goto release;
280df8bae1dSRodney W. Grimes 
281a29f300eSGarrett Wollman 	switch (so->so_type) {
282a29f300eSGarrett Wollman 	case SOCK_DGRAM:
283a29f300eSGarrett Wollman 	{
284df8bae1dSRodney W. Grimes 		struct sockaddr *from;
285df8bae1dSRodney W. Grimes 
286df8bae1dSRodney W. Grimes 		if (nam) {
287df8bae1dSRodney W. Grimes 			if (unp->unp_conn) {
288df8bae1dSRodney W. Grimes 				error = EISCONN;
289df8bae1dSRodney W. Grimes 				break;
290df8bae1dSRodney W. Grimes 			}
291b40ce416SJulian Elischer 			error = unp_connect(so, nam, td);
292df8bae1dSRodney W. Grimes 			if (error)
293df8bae1dSRodney W. Grimes 				break;
294df8bae1dSRodney W. Grimes 		} else {
295df8bae1dSRodney W. Grimes 			if (unp->unp_conn == 0) {
296df8bae1dSRodney W. Grimes 				error = ENOTCONN;
297df8bae1dSRodney W. Grimes 				break;
298df8bae1dSRodney W. Grimes 			}
299df8bae1dSRodney W. Grimes 		}
300df8bae1dSRodney W. Grimes 		so2 = unp->unp_conn->unp_socket;
301df8bae1dSRodney W. Grimes 		if (unp->unp_addr)
30257bf258eSGarrett Wollman 			from = (struct sockaddr *)unp->unp_addr;
303df8bae1dSRodney W. Grimes 		else
304df8bae1dSRodney W. Grimes 			from = &sun_noname;
305df8bae1dSRodney W. Grimes 		if (sbappendaddr(&so2->so_rcv, from, m, control)) {
306df8bae1dSRodney W. Grimes 			sorwakeup(so2);
307df8bae1dSRodney W. Grimes 			m = 0;
308df8bae1dSRodney W. Grimes 			control = 0;
309df8bae1dSRodney W. Grimes 		} else
310df8bae1dSRodney W. Grimes 			error = ENOBUFS;
311df8bae1dSRodney W. Grimes 		if (nam)
312df8bae1dSRodney W. Grimes 			unp_disconnect(unp);
313df8bae1dSRodney W. Grimes 		break;
314df8bae1dSRodney W. Grimes 	}
315df8bae1dSRodney W. Grimes 
316df8bae1dSRodney W. Grimes 	case SOCK_STREAM:
3176b8fda4dSGarrett Wollman 		/* Connect if not connected yet. */
3186b8fda4dSGarrett Wollman 		/*
3196b8fda4dSGarrett Wollman 		 * Note: A better implementation would complain
320402cc72dSDavid Greenman 		 * if not equal to the peer's address.
3216b8fda4dSGarrett Wollman 		 */
322402cc72dSDavid Greenman 		if ((so->so_state & SS_ISCONNECTED) == 0) {
323402cc72dSDavid Greenman 			if (nam) {
324b40ce416SJulian Elischer 				error = unp_connect(so, nam, td);
325402cc72dSDavid Greenman 				if (error)
3266b8fda4dSGarrett Wollman 					break;	/* XXX */
327402cc72dSDavid Greenman 			} else {
328402cc72dSDavid Greenman 				error = ENOTCONN;
329402cc72dSDavid Greenman 				break;
330402cc72dSDavid Greenman 			}
331402cc72dSDavid Greenman 		}
332402cc72dSDavid Greenman 
333df8bae1dSRodney W. Grimes 		if (so->so_state & SS_CANTSENDMORE) {
334df8bae1dSRodney W. Grimes 			error = EPIPE;
335df8bae1dSRodney W. Grimes 			break;
336df8bae1dSRodney W. Grimes 		}
337df8bae1dSRodney W. Grimes 		if (unp->unp_conn == 0)
338a29f300eSGarrett Wollman 			panic("uipc_send connected but no connection?");
339df8bae1dSRodney W. Grimes 		so2 = unp->unp_conn->unp_socket;
340df8bae1dSRodney W. Grimes 		/*
341df8bae1dSRodney W. Grimes 		 * Send to paired receive port, and then reduce
342df8bae1dSRodney W. Grimes 		 * send buffer hiwater marks to maintain backpressure.
343df8bae1dSRodney W. Grimes 		 * Wake up readers.
344df8bae1dSRodney W. Grimes 		 */
345df8bae1dSRodney W. Grimes 		if (control) {
346ff8b0106SBrian Feldman 			if (sbappendcontrol(&so2->so_rcv, m, control))
347df8bae1dSRodney W. Grimes 				control = 0;
348df8bae1dSRodney W. Grimes 		} else
349ff8b0106SBrian Feldman 			sbappend(&so2->so_rcv, m);
350ff8b0106SBrian Feldman 		so->so_snd.sb_mbmax -=
351ff8b0106SBrian Feldman 			so2->so_rcv.sb_mbcnt - unp->unp_conn->unp_mbcnt;
352ff8b0106SBrian Feldman 		unp->unp_conn->unp_mbcnt = so2->so_rcv.sb_mbcnt;
3536aef685fSBrian Feldman 		newhiwat = so->so_snd.sb_hiwat -
3546aef685fSBrian Feldman 		    (so2->so_rcv.sb_cc - unp->unp_conn->unp_cc);
355f535380cSDon Lewis 		(void)chgsbsize(so->so_cred->cr_uidinfo, &so->so_snd.sb_hiwat,
3566aef685fSBrian Feldman 		    newhiwat, RLIM_INFINITY);
357ff8b0106SBrian Feldman 		unp->unp_conn->unp_cc = so2->so_rcv.sb_cc;
358df8bae1dSRodney W. Grimes 		sorwakeup(so2);
359df8bae1dSRodney W. Grimes 		m = 0;
360df8bae1dSRodney W. Grimes 		break;
361df8bae1dSRodney W. Grimes 
362df8bae1dSRodney W. Grimes 	default:
363a29f300eSGarrett Wollman 		panic("uipc_send unknown socktype");
364df8bae1dSRodney W. Grimes 	}
365a29f300eSGarrett Wollman 
3666b8fda4dSGarrett Wollman 	/*
3676b8fda4dSGarrett Wollman 	 * SEND_EOF is equivalent to a SEND followed by
3686b8fda4dSGarrett Wollman 	 * a SHUTDOWN.
3696b8fda4dSGarrett Wollman 	 */
370a29f300eSGarrett Wollman 	if (flags & PRUS_EOF) {
3716b8fda4dSGarrett Wollman 		socantsendmore(so);
3726b8fda4dSGarrett Wollman 		unp_shutdown(unp);
3736b8fda4dSGarrett Wollman 	}
374df8bae1dSRodney W. Grimes 
375bd508d39SDon Lewis 	if (control && error != 0)
376bd508d39SDon Lewis 		unp_dispose(control);
377bd508d39SDon Lewis 
378a29f300eSGarrett Wollman release:
379a29f300eSGarrett Wollman 	if (control)
380a29f300eSGarrett Wollman 		m_freem(control);
381a29f300eSGarrett Wollman 	if (m)
382a29f300eSGarrett Wollman 		m_freem(m);
383a29f300eSGarrett Wollman 	return error;
384a29f300eSGarrett Wollman }
385df8bae1dSRodney W. Grimes 
386a29f300eSGarrett Wollman static int
387a29f300eSGarrett Wollman uipc_sense(struct socket *so, struct stat *sb)
388a29f300eSGarrett Wollman {
389a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so);
390a29f300eSGarrett Wollman 	struct socket *so2;
391a29f300eSGarrett Wollman 
392a29f300eSGarrett Wollman 	if (unp == 0)
393a29f300eSGarrett Wollman 		return EINVAL;
394a29f300eSGarrett Wollman 	sb->st_blksize = so->so_snd.sb_hiwat;
395df8bae1dSRodney W. Grimes 	if (so->so_type == SOCK_STREAM && unp->unp_conn != 0) {
396df8bae1dSRodney W. Grimes 		so2 = unp->unp_conn->unp_socket;
397a29f300eSGarrett Wollman 		sb->st_blksize += so2->so_rcv.sb_cc;
398df8bae1dSRodney W. Grimes 	}
399bfbb9ce6SPoul-Henning Kamp 	sb->st_dev = NOUDEV;
400df8bae1dSRodney W. Grimes 	if (unp->unp_ino == 0)
401df8bae1dSRodney W. Grimes 		unp->unp_ino = unp_ino++;
402a29f300eSGarrett Wollman 	sb->st_ino = unp->unp_ino;
403df8bae1dSRodney W. Grimes 	return (0);
404a29f300eSGarrett Wollman }
405df8bae1dSRodney W. Grimes 
406a29f300eSGarrett Wollman static int
407a29f300eSGarrett Wollman uipc_shutdown(struct socket *so)
408a29f300eSGarrett Wollman {
409a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so);
410df8bae1dSRodney W. Grimes 
411a29f300eSGarrett Wollman 	if (unp == 0)
412a29f300eSGarrett Wollman 		return EINVAL;
413a29f300eSGarrett Wollman 	socantsendmore(so);
414a29f300eSGarrett Wollman 	unp_shutdown(unp);
415a29f300eSGarrett Wollman 	return 0;
416a29f300eSGarrett Wollman }
417df8bae1dSRodney W. Grimes 
418a29f300eSGarrett Wollman static int
41957bf258eSGarrett Wollman uipc_sockaddr(struct socket *so, struct sockaddr **nam)
420a29f300eSGarrett Wollman {
421a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so);
422a29f300eSGarrett Wollman 
423a29f300eSGarrett Wollman 	if (unp == 0)
424a29f300eSGarrett Wollman 		return EINVAL;
42557bf258eSGarrett Wollman 	if (unp->unp_addr)
42657bf258eSGarrett Wollman 		*nam = dup_sockaddr((struct sockaddr *)unp->unp_addr, 1);
42783f3198bSThomas Moestl 	else
42883f3198bSThomas Moestl 		*nam = dup_sockaddr((struct sockaddr *)&sun_noname, 1);
429a29f300eSGarrett Wollman 	return 0;
430df8bae1dSRodney W. Grimes }
431a29f300eSGarrett Wollman 
432a29f300eSGarrett Wollman struct pr_usrreqs uipc_usrreqs = {
433a29f300eSGarrett Wollman 	uipc_abort, uipc_accept, uipc_attach, uipc_bind, uipc_connect,
434a29f300eSGarrett Wollman 	uipc_connect2, pru_control_notsupp, uipc_detach, uipc_disconnect,
435a29f300eSGarrett Wollman 	uipc_listen, uipc_peeraddr, uipc_rcvd, pru_rcvoob_notsupp,
436a29f300eSGarrett Wollman 	uipc_send, uipc_sense, uipc_shutdown, uipc_sockaddr,
43751338ea8SPeter Wemm 	sosend, soreceive, sopoll
438a29f300eSGarrett Wollman };
439df8bae1dSRodney W. Grimes 
4400c1bb4fbSDima Dorfman int
4410c1bb4fbSDima Dorfman uipc_ctloutput(so, sopt)
4420c1bb4fbSDima Dorfman 	struct socket *so;
4430c1bb4fbSDima Dorfman 	struct sockopt *sopt;
4440c1bb4fbSDima Dorfman {
4450c1bb4fbSDima Dorfman 	struct unpcb *unp = sotounpcb(so);
4460c1bb4fbSDima Dorfman 	int error;
4470c1bb4fbSDima Dorfman 
4480c1bb4fbSDima Dorfman 	switch (sopt->sopt_dir) {
4490c1bb4fbSDima Dorfman 	case SOPT_GET:
4500c1bb4fbSDima Dorfman 		switch (sopt->sopt_name) {
4510c1bb4fbSDima Dorfman 		case LOCAL_PEERCRED:
4520c1bb4fbSDima Dorfman 			if (unp->unp_flags & UNP_HAVEPC)
4530c1bb4fbSDima Dorfman 				error = sooptcopyout(sopt, &unp->unp_peercred,
4540c1bb4fbSDima Dorfman 				    sizeof(unp->unp_peercred));
4550c1bb4fbSDima Dorfman 			else {
4560c1bb4fbSDima Dorfman 				if (so->so_type == SOCK_STREAM)
4570c1bb4fbSDima Dorfman 					error = ENOTCONN;
4580c1bb4fbSDima Dorfman 				else
4590c1bb4fbSDima Dorfman 					error = EINVAL;
4600c1bb4fbSDima Dorfman 			}
4610c1bb4fbSDima Dorfman 			break;
4620c1bb4fbSDima Dorfman 		default:
4630c1bb4fbSDima Dorfman 			error = EOPNOTSUPP;
4640c1bb4fbSDima Dorfman 			break;
4650c1bb4fbSDima Dorfman 		}
4660c1bb4fbSDima Dorfman 		break;
4670c1bb4fbSDima Dorfman 	case SOPT_SET:
4680c1bb4fbSDima Dorfman 	default:
4690c1bb4fbSDima Dorfman 		error = EOPNOTSUPP;
4700c1bb4fbSDima Dorfman 		break;
4710c1bb4fbSDima Dorfman 	}
4720c1bb4fbSDima Dorfman 	return (error);
4730c1bb4fbSDima Dorfman }
4740c1bb4fbSDima Dorfman 
475df8bae1dSRodney W. Grimes /*
476df8bae1dSRodney W. Grimes  * Both send and receive buffers are allocated PIPSIZ bytes of buffering
477df8bae1dSRodney W. Grimes  * for stream sockets, although the total for sender and receiver is
478df8bae1dSRodney W. Grimes  * actually only PIPSIZ.
479df8bae1dSRodney W. Grimes  * Datagram sockets really use the sendspace as the maximum datagram size,
480df8bae1dSRodney W. Grimes  * and don't really want to reserve the sendspace.  Their recvspace should
481df8bae1dSRodney W. Grimes  * be large enough for at least one max-size datagram plus address.
482df8bae1dSRodney W. Grimes  */
4835dce41c5SJohn Dyson #ifndef PIPSIZ
4845dce41c5SJohn Dyson #define	PIPSIZ	8192
4855dce41c5SJohn Dyson #endif
486f708ef1bSPoul-Henning Kamp static u_long	unpst_sendspace = PIPSIZ;
487f708ef1bSPoul-Henning Kamp static u_long	unpst_recvspace = PIPSIZ;
488f708ef1bSPoul-Henning Kamp static u_long	unpdg_sendspace = 2*1024;	/* really max datagram size */
489f708ef1bSPoul-Henning Kamp static u_long	unpdg_recvspace = 4*1024;
490df8bae1dSRodney W. Grimes 
491f708ef1bSPoul-Henning Kamp static int	unp_rights;			/* file descriptors in flight */
492df8bae1dSRodney W. Grimes 
493ce02431fSDoug Rabson SYSCTL_DECL(_net_local_stream);
494639acc13SGarrett Wollman SYSCTL_INT(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW,
495639acc13SGarrett Wollman 	   &unpst_sendspace, 0, "");
496639acc13SGarrett Wollman SYSCTL_INT(_net_local_stream, OID_AUTO, recvspace, CTLFLAG_RW,
497639acc13SGarrett Wollman 	   &unpst_recvspace, 0, "");
498ce02431fSDoug Rabson SYSCTL_DECL(_net_local_dgram);
499639acc13SGarrett Wollman SYSCTL_INT(_net_local_dgram, OID_AUTO, maxdgram, CTLFLAG_RW,
500639acc13SGarrett Wollman 	   &unpdg_sendspace, 0, "");
501639acc13SGarrett Wollman SYSCTL_INT(_net_local_dgram, OID_AUTO, recvspace, CTLFLAG_RW,
502639acc13SGarrett Wollman 	   &unpdg_recvspace, 0, "");
503ce02431fSDoug Rabson SYSCTL_DECL(_net_local);
504639acc13SGarrett Wollman SYSCTL_INT(_net_local, OID_AUTO, inflight, CTLFLAG_RD, &unp_rights, 0, "");
505639acc13SGarrett Wollman 
506f708ef1bSPoul-Henning Kamp static int
507df8bae1dSRodney W. Grimes unp_attach(so)
508df8bae1dSRodney W. Grimes 	struct socket *so;
509df8bae1dSRodney W. Grimes {
510df8bae1dSRodney W. Grimes 	register struct unpcb *unp;
511df8bae1dSRodney W. Grimes 	int error;
512df8bae1dSRodney W. Grimes 
513df8bae1dSRodney W. Grimes 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
514df8bae1dSRodney W. Grimes 		switch (so->so_type) {
515df8bae1dSRodney W. Grimes 
516df8bae1dSRodney W. Grimes 		case SOCK_STREAM:
517df8bae1dSRodney W. Grimes 			error = soreserve(so, unpst_sendspace, unpst_recvspace);
518df8bae1dSRodney W. Grimes 			break;
519df8bae1dSRodney W. Grimes 
520df8bae1dSRodney W. Grimes 		case SOCK_DGRAM:
521df8bae1dSRodney W. Grimes 			error = soreserve(so, unpdg_sendspace, unpdg_recvspace);
522df8bae1dSRodney W. Grimes 			break;
523df8bae1dSRodney W. Grimes 
524df8bae1dSRodney W. Grimes 		default:
525df8bae1dSRodney W. Grimes 			panic("unp_attach");
526df8bae1dSRodney W. Grimes 		}
527df8bae1dSRodney W. Grimes 		if (error)
528df8bae1dSRodney W. Grimes 			return (error);
529df8bae1dSRodney W. Grimes 	}
53098271db4SGarrett Wollman 	unp = zalloc(unp_zone);
53157bf258eSGarrett Wollman 	if (unp == NULL)
532df8bae1dSRodney W. Grimes 		return (ENOBUFS);
53357bf258eSGarrett Wollman 	bzero(unp, sizeof *unp);
53498271db4SGarrett Wollman 	unp->unp_gencnt = ++unp_gencnt;
53598271db4SGarrett Wollman 	unp_count++;
53698271db4SGarrett Wollman 	LIST_INIT(&unp->unp_refs);
537df8bae1dSRodney W. Grimes 	unp->unp_socket = so;
538b40ce416SJulian Elischer 	unp->unp_rvnode = curthread->td_proc->p_fd->fd_rdir;
53998271db4SGarrett Wollman 	LIST_INSERT_HEAD(so->so_type == SOCK_DGRAM ? &unp_dhead
54098271db4SGarrett Wollman 			 : &unp_shead, unp, unp_link);
54198271db4SGarrett Wollman 	so->so_pcb = (caddr_t)unp;
542df8bae1dSRodney W. Grimes 	return (0);
543df8bae1dSRodney W. Grimes }
544df8bae1dSRodney W. Grimes 
545f708ef1bSPoul-Henning Kamp static void
546df8bae1dSRodney W. Grimes unp_detach(unp)
547df8bae1dSRodney W. Grimes 	register struct unpcb *unp;
548df8bae1dSRodney W. Grimes {
54998271db4SGarrett Wollman 	LIST_REMOVE(unp, unp_link);
55098271db4SGarrett Wollman 	unp->unp_gencnt = ++unp_gencnt;
55198271db4SGarrett Wollman 	--unp_count;
552df8bae1dSRodney W. Grimes 	if (unp->unp_vnode) {
553df8bae1dSRodney W. Grimes 		unp->unp_vnode->v_socket = 0;
554df8bae1dSRodney W. Grimes 		vrele(unp->unp_vnode);
555df8bae1dSRodney W. Grimes 		unp->unp_vnode = 0;
556df8bae1dSRodney W. Grimes 	}
557df8bae1dSRodney W. Grimes 	if (unp->unp_conn)
558df8bae1dSRodney W. Grimes 		unp_disconnect(unp);
5592e3c8fcbSPoul-Henning Kamp 	while (!LIST_EMPTY(&unp->unp_refs))
5602e3c8fcbSPoul-Henning Kamp 		unp_drop(LIST_FIRST(&unp->unp_refs), ECONNRESET);
561df8bae1dSRodney W. Grimes 	soisdisconnected(unp->unp_socket);
562df8bae1dSRodney W. Grimes 	unp->unp_socket->so_pcb = 0;
563df8bae1dSRodney W. Grimes 	if (unp_rights) {
564df8bae1dSRodney W. Grimes 		/*
565df8bae1dSRodney W. Grimes 		 * Normally the receive buffer is flushed later,
566df8bae1dSRodney W. Grimes 		 * in sofree, but if our receive buffer holds references
567df8bae1dSRodney W. Grimes 		 * to descriptors that are now garbage, we will dispose
568df8bae1dSRodney W. Grimes 		 * of those descriptor references after the garbage collector
569df8bae1dSRodney W. Grimes 		 * gets them (resulting in a "panic: closef: count < 0").
570df8bae1dSRodney W. Grimes 		 */
571df8bae1dSRodney W. Grimes 		sorflush(unp->unp_socket);
572df8bae1dSRodney W. Grimes 		unp_gc();
573df8bae1dSRodney W. Grimes 	}
57457bf258eSGarrett Wollman 	if (unp->unp_addr)
57557bf258eSGarrett Wollman 		FREE(unp->unp_addr, M_SONAME);
57698271db4SGarrett Wollman 	zfree(unp_zone, unp);
577df8bae1dSRodney W. Grimes }
578df8bae1dSRodney W. Grimes 
579f708ef1bSPoul-Henning Kamp static int
580b40ce416SJulian Elischer unp_bind(unp, nam, td)
581df8bae1dSRodney W. Grimes 	struct unpcb *unp;
58257bf258eSGarrett Wollman 	struct sockaddr *nam;
583b40ce416SJulian Elischer 	struct thread *td;
584df8bae1dSRodney W. Grimes {
58557bf258eSGarrett Wollman 	struct sockaddr_un *soun = (struct sockaddr_un *)nam;
586f2a2857bSKirk McKusick 	struct vnode *vp;
587f2a2857bSKirk McKusick 	struct mount *mp;
588df8bae1dSRodney W. Grimes 	struct vattr vattr;
58957bf258eSGarrett Wollman 	int error, namelen;
590df8bae1dSRodney W. Grimes 	struct nameidata nd;
5918f364875SJulian Elischer 	char *buf;
592df8bae1dSRodney W. Grimes 
593df8bae1dSRodney W. Grimes 	if (unp->unp_vnode != NULL)
594df8bae1dSRodney W. Grimes 		return (EINVAL);
59557bf258eSGarrett Wollman 	namelen = soun->sun_len - offsetof(struct sockaddr_un, sun_path);
59657bf258eSGarrett Wollman 	if (namelen <= 0)
59757bf258eSGarrett Wollman 		return EINVAL;
5988f364875SJulian Elischer 	buf = malloc(SOCK_MAXADDRLEN, M_TEMP, M_WAITOK);
59957bf258eSGarrett Wollman 	strncpy(buf, soun->sun_path, namelen);
60057bf258eSGarrett Wollman 	buf[namelen] = 0;	/* null-terminate the string */
601f2a2857bSKirk McKusick restart:
602974784e8SGuido van Rooij 	NDINIT(&nd, CREATE, NOFOLLOW | LOCKPARENT, UIO_SYSSPACE,
603b40ce416SJulian Elischer 	    buf, td);
604df8bae1dSRodney W. Grimes /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */
605797f2d22SPoul-Henning Kamp 	error = namei(&nd);
6068f364875SJulian Elischer 	if (error) {
6078f364875SJulian Elischer 		free(buf, M_TEMP);
608ad4ff090SJulian Elischer 		return (error);
6098f364875SJulian Elischer 	}
610df8bae1dSRodney W. Grimes 	vp = nd.ni_vp;
611f2a2857bSKirk McKusick 	if (vp != NULL || vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
612762e6b85SEivind Eklund 		NDFREE(&nd, NDF_ONLY_PNBUF);
613df8bae1dSRodney W. Grimes 		if (nd.ni_dvp == vp)
614df8bae1dSRodney W. Grimes 			vrele(nd.ni_dvp);
615df8bae1dSRodney W. Grimes 		else
616df8bae1dSRodney W. Grimes 			vput(nd.ni_dvp);
617f2a2857bSKirk McKusick 		if (vp != NULL) {
618df8bae1dSRodney W. Grimes 			vrele(vp);
6198f364875SJulian Elischer 			free(buf, M_TEMP);
620df8bae1dSRodney W. Grimes 			return (EADDRINUSE);
621df8bae1dSRodney W. Grimes 		}
6228f364875SJulian Elischer 		error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH);
6238f364875SJulian Elischer 		if (error) {
6248f364875SJulian Elischer 			free(buf, M_TEMP);
625f2a2857bSKirk McKusick 			return (error);
6268f364875SJulian Elischer 		}
627f2a2857bSKirk McKusick 		goto restart;
628f2a2857bSKirk McKusick 	}
629df8bae1dSRodney W. Grimes 	VATTR_NULL(&vattr);
630df8bae1dSRodney W. Grimes 	vattr.va_type = VSOCK;
631b40ce416SJulian Elischer 	vattr.va_mode = (ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask);
632b40ce416SJulian Elischer 	VOP_LEASE(nd.ni_dvp, td, td->td_proc->p_ucred, LEASE_WRITE);
6337be2d300SMike Smith 	error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
634762e6b85SEivind Eklund 	NDFREE(&nd, NDF_ONLY_PNBUF);
6357be2d300SMike Smith 	vput(nd.ni_dvp);
6368f364875SJulian Elischer 	if (error) {
6378f364875SJulian Elischer 		free(buf, M_TEMP);
638df8bae1dSRodney W. Grimes 		return (error);
6398f364875SJulian Elischer 	}
640df8bae1dSRodney W. Grimes 	vp = nd.ni_vp;
641df8bae1dSRodney W. Grimes 	vp->v_socket = unp->unp_socket;
642df8bae1dSRodney W. Grimes 	unp->unp_vnode = vp;
64357bf258eSGarrett Wollman 	unp->unp_addr = (struct sockaddr_un *)dup_sockaddr(nam, 1);
644b40ce416SJulian Elischer 	VOP_UNLOCK(vp, 0, td);
645f2a2857bSKirk McKusick 	vn_finished_write(mp);
6468f364875SJulian Elischer 	free(buf, M_TEMP);
647df8bae1dSRodney W. Grimes 	return (0);
648df8bae1dSRodney W. Grimes }
649df8bae1dSRodney W. Grimes 
650f708ef1bSPoul-Henning Kamp static int
651b40ce416SJulian Elischer unp_connect(so, nam, td)
652df8bae1dSRodney W. Grimes 	struct socket *so;
65357bf258eSGarrett Wollman 	struct sockaddr *nam;
654b40ce416SJulian Elischer 	struct thread *td;
655df8bae1dSRodney W. Grimes {
65657bf258eSGarrett Wollman 	register struct sockaddr_un *soun = (struct sockaddr_un *)nam;
657df8bae1dSRodney W. Grimes 	register struct vnode *vp;
658df8bae1dSRodney W. Grimes 	register struct socket *so2, *so3;
6590c1bb4fbSDima Dorfman 	struct unpcb *unp, *unp2, *unp3;
66057bf258eSGarrett Wollman 	int error, len;
661df8bae1dSRodney W. Grimes 	struct nameidata nd;
66257bf258eSGarrett Wollman 	char buf[SOCK_MAXADDRLEN];
663df8bae1dSRodney W. Grimes 
66457bf258eSGarrett Wollman 	len = nam->sa_len - offsetof(struct sockaddr_un, sun_path);
66557bf258eSGarrett Wollman 	if (len <= 0)
66657bf258eSGarrett Wollman 		return EINVAL;
66757bf258eSGarrett Wollman 	strncpy(buf, soun->sun_path, len);
66857bf258eSGarrett Wollman 	buf[len] = 0;
66957bf258eSGarrett Wollman 
670b40ce416SJulian Elischer 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, buf, td);
671797f2d22SPoul-Henning Kamp 	error = namei(&nd);
672797f2d22SPoul-Henning Kamp 	if (error)
673df8bae1dSRodney W. Grimes 		return (error);
674df8bae1dSRodney W. Grimes 	vp = nd.ni_vp;
675762e6b85SEivind Eklund 	NDFREE(&nd, NDF_ONLY_PNBUF);
676df8bae1dSRodney W. Grimes 	if (vp->v_type != VSOCK) {
677df8bae1dSRodney W. Grimes 		error = ENOTSOCK;
678df8bae1dSRodney W. Grimes 		goto bad;
679df8bae1dSRodney W. Grimes 	}
680b40ce416SJulian Elischer 	error = VOP_ACCESS(vp, VWRITE, td->td_proc->p_ucred, td);
681797f2d22SPoul-Henning Kamp 	if (error)
682df8bae1dSRodney W. Grimes 		goto bad;
683df8bae1dSRodney W. Grimes 	so2 = vp->v_socket;
684df8bae1dSRodney W. Grimes 	if (so2 == 0) {
685df8bae1dSRodney W. Grimes 		error = ECONNREFUSED;
686df8bae1dSRodney W. Grimes 		goto bad;
687df8bae1dSRodney W. Grimes 	}
688df8bae1dSRodney W. Grimes 	if (so->so_type != so2->so_type) {
689df8bae1dSRodney W. Grimes 		error = EPROTOTYPE;
690df8bae1dSRodney W. Grimes 		goto bad;
691df8bae1dSRodney W. Grimes 	}
692df8bae1dSRodney W. Grimes 	if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
693df8bae1dSRodney W. Grimes 		if ((so2->so_options & SO_ACCEPTCONN) == 0 ||
694b40ce416SJulian Elischer 		    (so3 = sonewconn3(so2, 0, td)) == 0) {
695df8bae1dSRodney W. Grimes 			error = ECONNREFUSED;
696df8bae1dSRodney W. Grimes 			goto bad;
697df8bae1dSRodney W. Grimes 		}
6980c1bb4fbSDima Dorfman 		unp = sotounpcb(so);
699df8bae1dSRodney W. Grimes 		unp2 = sotounpcb(so2);
700df8bae1dSRodney W. Grimes 		unp3 = sotounpcb(so3);
701df8bae1dSRodney W. Grimes 		if (unp2->unp_addr)
70257bf258eSGarrett Wollman 			unp3->unp_addr = (struct sockaddr_un *)
70357bf258eSGarrett Wollman 				dup_sockaddr((struct sockaddr *)
70457bf258eSGarrett Wollman 					     unp2->unp_addr, 1);
7050c1bb4fbSDima Dorfman 
7060c1bb4fbSDima Dorfman 		/*
7070c1bb4fbSDima Dorfman 		 * unp_peercred management:
7080c1bb4fbSDima Dorfman 		 *
7090c1bb4fbSDima Dorfman 		 * The connecter's (client's) credentials are copied
7100c1bb4fbSDima Dorfman 		 * from its process structure at the time of connect()
7110c1bb4fbSDima Dorfman 		 * (which is now).
7120c1bb4fbSDima Dorfman 		 */
7130c1bb4fbSDima Dorfman 		memset(&unp3->unp_peercred, '\0', sizeof(unp3->unp_peercred));
714b40ce416SJulian Elischer 		unp3->unp_peercred.cr_uid = td->td_proc->p_ucred->cr_uid;
715b40ce416SJulian Elischer 		unp3->unp_peercred.cr_ngroups = td->td_proc->p_ucred->cr_ngroups;
716b40ce416SJulian Elischer 		memcpy(unp3->unp_peercred.cr_groups, td->td_proc->p_ucred->cr_groups,
7170c1bb4fbSDima Dorfman 		    sizeof(unp3->unp_peercred.cr_groups));
7180c1bb4fbSDima Dorfman 		unp3->unp_flags |= UNP_HAVEPC;
7190c1bb4fbSDima Dorfman 		/*
7200c1bb4fbSDima Dorfman 		 * The receiver's (server's) credentials are copied
7210c1bb4fbSDima Dorfman 		 * from the unp_peercred member of socket on which the
7220c1bb4fbSDima Dorfman 		 * former called listen(); unp_listen() cached that
7230c1bb4fbSDima Dorfman 		 * process's credentials at that time so we can use
7240c1bb4fbSDima Dorfman 		 * them now.
7250c1bb4fbSDima Dorfman 		 */
7260c1bb4fbSDima Dorfman 		KASSERT(unp2->unp_flags & UNP_HAVEPCCACHED,
7270c1bb4fbSDima Dorfman 		    ("unp_connect: listener without cached peercred"));
7280c1bb4fbSDima Dorfman 		memcpy(&unp->unp_peercred, &unp2->unp_peercred,
7290c1bb4fbSDima Dorfman 		    sizeof(unp->unp_peercred));
7300c1bb4fbSDima Dorfman 		unp->unp_flags |= UNP_HAVEPC;
7310c1bb4fbSDima Dorfman 
732df8bae1dSRodney W. Grimes 		so2 = so3;
733df8bae1dSRodney W. Grimes 	}
734df8bae1dSRodney W. Grimes 	error = unp_connect2(so, so2);
735df8bae1dSRodney W. Grimes bad:
736df8bae1dSRodney W. Grimes 	vput(vp);
737df8bae1dSRodney W. Grimes 	return (error);
738df8bae1dSRodney W. Grimes }
739df8bae1dSRodney W. Grimes 
74026f9a767SRodney W. Grimes int
741df8bae1dSRodney W. Grimes unp_connect2(so, so2)
742df8bae1dSRodney W. Grimes 	register struct socket *so;
743df8bae1dSRodney W. Grimes 	register struct socket *so2;
744df8bae1dSRodney W. Grimes {
745df8bae1dSRodney W. Grimes 	register struct unpcb *unp = sotounpcb(so);
746df8bae1dSRodney W. Grimes 	register struct unpcb *unp2;
747df8bae1dSRodney W. Grimes 
748df8bae1dSRodney W. Grimes 	if (so2->so_type != so->so_type)
749df8bae1dSRodney W. Grimes 		return (EPROTOTYPE);
750df8bae1dSRodney W. Grimes 	unp2 = sotounpcb(so2);
751df8bae1dSRodney W. Grimes 	unp->unp_conn = unp2;
752df8bae1dSRodney W. Grimes 	switch (so->so_type) {
753df8bae1dSRodney W. Grimes 
754df8bae1dSRodney W. Grimes 	case SOCK_DGRAM:
75598271db4SGarrett Wollman 		LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink);
756df8bae1dSRodney W. Grimes 		soisconnected(so);
757df8bae1dSRodney W. Grimes 		break;
758df8bae1dSRodney W. Grimes 
759df8bae1dSRodney W. Grimes 	case SOCK_STREAM:
760df8bae1dSRodney W. Grimes 		unp2->unp_conn = unp;
761df8bae1dSRodney W. Grimes 		soisconnected(so);
762df8bae1dSRodney W. Grimes 		soisconnected(so2);
763df8bae1dSRodney W. Grimes 		break;
764df8bae1dSRodney W. Grimes 
765df8bae1dSRodney W. Grimes 	default:
766df8bae1dSRodney W. Grimes 		panic("unp_connect2");
767df8bae1dSRodney W. Grimes 	}
768df8bae1dSRodney W. Grimes 	return (0);
769df8bae1dSRodney W. Grimes }
770df8bae1dSRodney W. Grimes 
771f708ef1bSPoul-Henning Kamp static void
772df8bae1dSRodney W. Grimes unp_disconnect(unp)
773df8bae1dSRodney W. Grimes 	struct unpcb *unp;
774df8bae1dSRodney W. Grimes {
775df8bae1dSRodney W. Grimes 	register struct unpcb *unp2 = unp->unp_conn;
776df8bae1dSRodney W. Grimes 
777df8bae1dSRodney W. Grimes 	if (unp2 == 0)
778df8bae1dSRodney W. Grimes 		return;
779df8bae1dSRodney W. Grimes 	unp->unp_conn = 0;
780df8bae1dSRodney W. Grimes 	switch (unp->unp_socket->so_type) {
781df8bae1dSRodney W. Grimes 
782df8bae1dSRodney W. Grimes 	case SOCK_DGRAM:
78398271db4SGarrett Wollman 		LIST_REMOVE(unp, unp_reflink);
784df8bae1dSRodney W. Grimes 		unp->unp_socket->so_state &= ~SS_ISCONNECTED;
785df8bae1dSRodney W. Grimes 		break;
786df8bae1dSRodney W. Grimes 
787df8bae1dSRodney W. Grimes 	case SOCK_STREAM:
788df8bae1dSRodney W. Grimes 		soisdisconnected(unp->unp_socket);
789df8bae1dSRodney W. Grimes 		unp2->unp_conn = 0;
790df8bae1dSRodney W. Grimes 		soisdisconnected(unp2->unp_socket);
791df8bae1dSRodney W. Grimes 		break;
792df8bae1dSRodney W. Grimes 	}
793df8bae1dSRodney W. Grimes }
794df8bae1dSRodney W. Grimes 
795df8bae1dSRodney W. Grimes #ifdef notdef
79626f9a767SRodney W. Grimes void
797df8bae1dSRodney W. Grimes unp_abort(unp)
798df8bae1dSRodney W. Grimes 	struct unpcb *unp;
799df8bae1dSRodney W. Grimes {
800df8bae1dSRodney W. Grimes 
801df8bae1dSRodney W. Grimes 	unp_detach(unp);
802df8bae1dSRodney W. Grimes }
803df8bae1dSRodney W. Grimes #endif
804df8bae1dSRodney W. Grimes 
80598271db4SGarrett Wollman static int
80675c13541SPoul-Henning Kamp prison_unpcb(struct proc *p, struct unpcb *unp)
80775c13541SPoul-Henning Kamp {
80891421ba2SRobert Watson 	if (!jailed(p->p_ucred))
80975c13541SPoul-Henning Kamp 		return (0);
81075c13541SPoul-Henning Kamp 	if (p->p_fd->fd_rdir == unp->unp_rvnode)
81175c13541SPoul-Henning Kamp 		return (0);
81275c13541SPoul-Henning Kamp 	return (1);
81375c13541SPoul-Henning Kamp }
81475c13541SPoul-Henning Kamp 
81575c13541SPoul-Henning Kamp static int
81682d9ae4eSPoul-Henning Kamp unp_pcblist(SYSCTL_HANDLER_ARGS)
81798271db4SGarrett Wollman {
818f5ef029eSPoul-Henning Kamp 	int error, i, n;
81998271db4SGarrett Wollman 	struct unpcb *unp, **unp_list;
82098271db4SGarrett Wollman 	unp_gen_t gencnt;
8218f364875SJulian Elischer 	struct xunpgen *xug;
82298271db4SGarrett Wollman 	struct unp_head *head;
8238f364875SJulian Elischer 	struct xunpcb *xu;
82498271db4SGarrett Wollman 
825a23d65bfSBruce Evans 	head = ((intptr_t)arg1 == SOCK_DGRAM ? &unp_dhead : &unp_shead);
82698271db4SGarrett Wollman 
82798271db4SGarrett Wollman 	/*
82898271db4SGarrett Wollman 	 * The process of preparing the PCB list is too time-consuming and
82998271db4SGarrett Wollman 	 * resource-intensive to repeat twice on every request.
83098271db4SGarrett Wollman 	 */
83198271db4SGarrett Wollman 	if (req->oldptr == 0) {
83298271db4SGarrett Wollman 		n = unp_count;
8338f364875SJulian Elischer 		req->oldidx = 2 * (sizeof *xug)
83498271db4SGarrett Wollman 			+ (n + n/8) * sizeof(struct xunpcb);
83598271db4SGarrett Wollman 		return 0;
83698271db4SGarrett Wollman 	}
83798271db4SGarrett Wollman 
83898271db4SGarrett Wollman 	if (req->newptr != 0)
83998271db4SGarrett Wollman 		return EPERM;
84098271db4SGarrett Wollman 
84198271db4SGarrett Wollman 	/*
84298271db4SGarrett Wollman 	 * OK, now we're committed to doing something.
84398271db4SGarrett Wollman 	 */
8448f364875SJulian Elischer 	xug = malloc(sizeof(*xug), M_TEMP, M_WAITOK);
84598271db4SGarrett Wollman 	gencnt = unp_gencnt;
84698271db4SGarrett Wollman 	n = unp_count;
84798271db4SGarrett Wollman 
8488f364875SJulian Elischer 	xug->xug_len = sizeof *xug;
8498f364875SJulian Elischer 	xug->xug_count = n;
8508f364875SJulian Elischer 	xug->xug_gen = gencnt;
8518f364875SJulian Elischer 	xug->xug_sogen = so_gencnt;
8528f364875SJulian Elischer 	error = SYSCTL_OUT(req, xug, sizeof *xug);
8538f364875SJulian Elischer 	if (error) {
8548f364875SJulian Elischer 		free(xug, M_TEMP);
85598271db4SGarrett Wollman 		return error;
8568f364875SJulian Elischer 	}
85798271db4SGarrett Wollman 
85898271db4SGarrett Wollman 	unp_list = malloc(n * sizeof *unp_list, M_TEMP, M_WAITOK);
85998271db4SGarrett Wollman 
8602e3c8fcbSPoul-Henning Kamp 	for (unp = LIST_FIRST(head), i = 0; unp && i < n;
8612e3c8fcbSPoul-Henning Kamp 	     unp = LIST_NEXT(unp, unp_link)) {
86275c13541SPoul-Henning Kamp 		if (unp->unp_gencnt <= gencnt && !prison_unpcb(req->p, unp))
86398271db4SGarrett Wollman 			unp_list[i++] = unp;
86498271db4SGarrett Wollman 	}
86598271db4SGarrett Wollman 	n = i;			/* in case we lost some during malloc */
86698271db4SGarrett Wollman 
86798271db4SGarrett Wollman 	error = 0;
8688f364875SJulian Elischer 	xu = malloc(sizeof(*xu), M_TEMP, M_WAITOK);
86998271db4SGarrett Wollman 	for (i = 0; i < n; i++) {
87098271db4SGarrett Wollman 		unp = unp_list[i];
87198271db4SGarrett Wollman 		if (unp->unp_gencnt <= gencnt) {
8728f364875SJulian Elischer 			xu->xu_len = sizeof *xu;
8738f364875SJulian Elischer 			xu->xu_unpp = unp;
87498271db4SGarrett Wollman 			/*
87598271db4SGarrett Wollman 			 * XXX - need more locking here to protect against
87698271db4SGarrett Wollman 			 * connect/disconnect races for SMP.
87798271db4SGarrett Wollman 			 */
87898271db4SGarrett Wollman 			if (unp->unp_addr)
8798f364875SJulian Elischer 				bcopy(unp->unp_addr, &xu->xu_addr,
88098271db4SGarrett Wollman 				      unp->unp_addr->sun_len);
88198271db4SGarrett Wollman 			if (unp->unp_conn && unp->unp_conn->unp_addr)
88298271db4SGarrett Wollman 				bcopy(unp->unp_conn->unp_addr,
8838f364875SJulian Elischer 				      &xu->xu_caddr,
88498271db4SGarrett Wollman 				      unp->unp_conn->unp_addr->sun_len);
8858f364875SJulian Elischer 			bcopy(unp, &xu->xu_unp, sizeof *unp);
8868f364875SJulian Elischer 			sotoxsocket(unp->unp_socket, &xu->xu_socket);
8878f364875SJulian Elischer 			error = SYSCTL_OUT(req, xu, sizeof *xu);
88898271db4SGarrett Wollman 		}
88998271db4SGarrett Wollman 	}
8908f364875SJulian Elischer 	free(xu, M_TEMP);
89198271db4SGarrett Wollman 	if (!error) {
89298271db4SGarrett Wollman 		/*
89398271db4SGarrett Wollman 		 * Give the user an updated idea of our state.
89498271db4SGarrett Wollman 		 * If the generation differs from what we told
89598271db4SGarrett Wollman 		 * her before, she knows that something happened
89698271db4SGarrett Wollman 		 * while we were processing this request, and it
89798271db4SGarrett Wollman 		 * might be necessary to retry.
89898271db4SGarrett Wollman 		 */
8998f364875SJulian Elischer 		xug->xug_gen = unp_gencnt;
9008f364875SJulian Elischer 		xug->xug_sogen = so_gencnt;
9018f364875SJulian Elischer 		xug->xug_count = unp_count;
9028f364875SJulian Elischer 		error = SYSCTL_OUT(req, xug, sizeof *xug);
90398271db4SGarrett Wollman 	}
90498271db4SGarrett Wollman 	free(unp_list, M_TEMP);
9058f364875SJulian Elischer 	free(xug, M_TEMP);
90698271db4SGarrett Wollman 	return error;
90798271db4SGarrett Wollman }
90898271db4SGarrett Wollman 
90998271db4SGarrett Wollman SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLFLAG_RD,
91098271db4SGarrett Wollman 	    (caddr_t)(long)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb",
91198271db4SGarrett Wollman 	    "List of active local datagram sockets");
91298271db4SGarrett Wollman SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, CTLFLAG_RD,
91398271db4SGarrett Wollman 	    (caddr_t)(long)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb",
91498271db4SGarrett Wollman 	    "List of active local stream sockets");
91598271db4SGarrett Wollman 
916f708ef1bSPoul-Henning Kamp static void
917df8bae1dSRodney W. Grimes unp_shutdown(unp)
918df8bae1dSRodney W. Grimes 	struct unpcb *unp;
919df8bae1dSRodney W. Grimes {
920df8bae1dSRodney W. Grimes 	struct socket *so;
921df8bae1dSRodney W. Grimes 
922df8bae1dSRodney W. Grimes 	if (unp->unp_socket->so_type == SOCK_STREAM && unp->unp_conn &&
923df8bae1dSRodney W. Grimes 	    (so = unp->unp_conn->unp_socket))
924df8bae1dSRodney W. Grimes 		socantrcvmore(so);
925df8bae1dSRodney W. Grimes }
926df8bae1dSRodney W. Grimes 
927f708ef1bSPoul-Henning Kamp static void
928df8bae1dSRodney W. Grimes unp_drop(unp, errno)
929df8bae1dSRodney W. Grimes 	struct unpcb *unp;
930df8bae1dSRodney W. Grimes 	int errno;
931df8bae1dSRodney W. Grimes {
932df8bae1dSRodney W. Grimes 	struct socket *so = unp->unp_socket;
933df8bae1dSRodney W. Grimes 
934df8bae1dSRodney W. Grimes 	so->so_error = errno;
935df8bae1dSRodney W. Grimes 	unp_disconnect(unp);
936df8bae1dSRodney W. Grimes 	if (so->so_head) {
93798271db4SGarrett Wollman 		LIST_REMOVE(unp, unp_link);
93898271db4SGarrett Wollman 		unp->unp_gencnt = ++unp_gencnt;
93998271db4SGarrett Wollman 		unp_count--;
940df8bae1dSRodney W. Grimes 		so->so_pcb = (caddr_t) 0;
94157bf258eSGarrett Wollman 		if (unp->unp_addr)
94257bf258eSGarrett Wollman 			FREE(unp->unp_addr, M_SONAME);
94398271db4SGarrett Wollman 		zfree(unp_zone, unp);
944df8bae1dSRodney W. Grimes 		sofree(so);
945df8bae1dSRodney W. Grimes 	}
946df8bae1dSRodney W. Grimes }
947df8bae1dSRodney W. Grimes 
948df8bae1dSRodney W. Grimes #ifdef notdef
94926f9a767SRodney W. Grimes void
950df8bae1dSRodney W. Grimes unp_drain()
951df8bae1dSRodney W. Grimes {
952df8bae1dSRodney W. Grimes 
953df8bae1dSRodney W. Grimes }
954df8bae1dSRodney W. Grimes #endif
955df8bae1dSRodney W. Grimes 
9562bc21ed9SDavid Malone static void
9572bc21ed9SDavid Malone unp_freerights(rp, fdcount)
9582bc21ed9SDavid Malone 	struct file **rp;
9592bc21ed9SDavid Malone 	int fdcount;
960df8bae1dSRodney W. Grimes {
9612bc21ed9SDavid Malone 	int i;
9622bc21ed9SDavid Malone 	struct file *fp;
963df8bae1dSRodney W. Grimes 
9642bc21ed9SDavid Malone 	for (i = 0; i < fdcount; i++) {
965df8bae1dSRodney W. Grimes 		fp = *rp;
9668692c025SYoshinobu Inoue 		/*
9672bc21ed9SDavid Malone 		 * zero the pointer before calling
9682bc21ed9SDavid Malone 		 * unp_discard since it may end up
9692bc21ed9SDavid Malone 		 * in unp_gc()..
9708692c025SYoshinobu Inoue 		 */
971df8bae1dSRodney W. Grimes 		*rp++ = 0;
9728692c025SYoshinobu Inoue 		unp_discard(fp);
973df8bae1dSRodney W. Grimes 	}
9742bc21ed9SDavid Malone }
9752bc21ed9SDavid Malone 
9762bc21ed9SDavid Malone int
9772bc21ed9SDavid Malone unp_externalize(control, controlp)
9782bc21ed9SDavid Malone 	struct mbuf *control, **controlp;
9792bc21ed9SDavid Malone {
9802bc21ed9SDavid Malone 	struct thread *td = curthread;		/* XXX */
9812bc21ed9SDavid Malone 	struct cmsghdr *cm = mtod(control, struct cmsghdr *);
9822bc21ed9SDavid Malone 	int i;
9832bc21ed9SDavid Malone 	int *fdp;
9842bc21ed9SDavid Malone 	struct file **rp;
9852bc21ed9SDavid Malone 	struct file *fp;
9862bc21ed9SDavid Malone 	void *data;
9872bc21ed9SDavid Malone 	socklen_t clen = control->m_len, datalen;
9882bc21ed9SDavid Malone 	int error, newfds;
9892bc21ed9SDavid Malone 	int f;
9902bc21ed9SDavid Malone 	u_int newlen;
9912bc21ed9SDavid Malone 
9922bc21ed9SDavid Malone 	error = 0;
9932bc21ed9SDavid Malone 	if (controlp != NULL) /* controlp == NULL => free control messages */
9942bc21ed9SDavid Malone 		*controlp = NULL;
9952bc21ed9SDavid Malone 
9962bc21ed9SDavid Malone 	while (cm != NULL) {
9972bc21ed9SDavid Malone 		if (sizeof(*cm) > clen || cm->cmsg_len > clen) {
9982bc21ed9SDavid Malone 			error = EINVAL;
9992bc21ed9SDavid Malone 			break;
10002bc21ed9SDavid Malone 		}
10012bc21ed9SDavid Malone 
10022bc21ed9SDavid Malone 		data = CMSG_DATA(cm);
10032bc21ed9SDavid Malone 		datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
10042bc21ed9SDavid Malone 
10052bc21ed9SDavid Malone 		if (cm->cmsg_level == SOL_SOCKET
10062bc21ed9SDavid Malone 		    && cm->cmsg_type == SCM_RIGHTS) {
10072bc21ed9SDavid Malone 			newfds = datalen / sizeof(struct file *);
10082bc21ed9SDavid Malone 			rp = data;
10092bc21ed9SDavid Malone 
10102bc21ed9SDavid Malone 			/* If we're not outputting the discriptors free them. */
10112bc21ed9SDavid Malone 			if (error || controlp == NULL) {
10122bc21ed9SDavid Malone 				unp_freerights(rp, newfds);
10132bc21ed9SDavid Malone 				goto next;
10142bc21ed9SDavid Malone 			}
10152bc21ed9SDavid Malone 			/* if the new FD's will not fit free them.  */
10162bc21ed9SDavid Malone 			if (!fdavail(td, newfds)) {
10172bc21ed9SDavid Malone 				error = EMSGSIZE;
10182bc21ed9SDavid Malone 				unp_freerights(rp, newfds);
10192bc21ed9SDavid Malone 				goto next;
1020df8bae1dSRodney W. Grimes 			}
1021ed5b7817SJulian Elischer 			/*
10222bc21ed9SDavid Malone 			 * now change each pointer to an fd in the global
10232bc21ed9SDavid Malone 			 * table to an integer that is the index to the
10242bc21ed9SDavid Malone 			 * local fd table entry that we set up to point
10252bc21ed9SDavid Malone 			 * to the global one we are transferring.
1026ed5b7817SJulian Elischer 			 */
10272bc21ed9SDavid Malone 			newlen = newfds * sizeof(int);
10282bc21ed9SDavid Malone 			*controlp = sbcreatecontrol(NULL, newlen,
10292bc21ed9SDavid Malone 			    SCM_RIGHTS, SOL_SOCKET);
10302bc21ed9SDavid Malone 			if (*controlp == NULL) {
10312bc21ed9SDavid Malone 				error = E2BIG;
10322bc21ed9SDavid Malone 				unp_freerights(rp, newfds);
10332bc21ed9SDavid Malone 				goto next;
10342bc21ed9SDavid Malone 			}
10352bc21ed9SDavid Malone 
10362bc21ed9SDavid Malone 			fdp = (int *)
10372bc21ed9SDavid Malone 			    CMSG_DATA(mtod(*controlp, struct cmsghdr *));
1038df8bae1dSRodney W. Grimes 			for (i = 0; i < newfds; i++) {
1039b40ce416SJulian Elischer 				if (fdalloc(td, 0, &f))
10402bc21ed9SDavid Malone 					panic("unp_externalize fdalloc failed");
10418692c025SYoshinobu Inoue 				fp = *rp++;
1042b40ce416SJulian Elischer 				td->td_proc->p_fd->fd_ofiles[f] = fp;
1043df8bae1dSRodney W. Grimes 				fp->f_msgcount--;
1044df8bae1dSRodney W. Grimes 				unp_rights--;
10458692c025SYoshinobu Inoue 				*fdp++ = f;
1046df8bae1dSRodney W. Grimes 			}
10472bc21ed9SDavid Malone 		} else { /* We can just copy anything else across */
10482bc21ed9SDavid Malone 			if (error || controlp == NULL)
10492bc21ed9SDavid Malone 				goto next;
10502bc21ed9SDavid Malone 			*controlp = sbcreatecontrol(NULL, datalen,
10512bc21ed9SDavid Malone 			    cm->cmsg_type, cm->cmsg_level);
10522bc21ed9SDavid Malone 			if (*controlp == NULL) {
10532bc21ed9SDavid Malone 				error = ENOBUFS;
10542bc21ed9SDavid Malone 				goto next;
10552bc21ed9SDavid Malone 			}
10562bc21ed9SDavid Malone 			bcopy(data,
10572bc21ed9SDavid Malone 			    CMSG_DATA(mtod(*controlp, struct cmsghdr *)),
10582bc21ed9SDavid Malone 			    datalen);
10592bc21ed9SDavid Malone 		}
10602bc21ed9SDavid Malone 
10612bc21ed9SDavid Malone 		controlp = &(*controlp)->m_next;
10622bc21ed9SDavid Malone 
10632bc21ed9SDavid Malone next:
10642bc21ed9SDavid Malone 		if (CMSG_SPACE(datalen) < clen) {
10652bc21ed9SDavid Malone 			clen -= CMSG_SPACE(datalen);
10662bc21ed9SDavid Malone 			cm = (struct cmsghdr *)
10672bc21ed9SDavid Malone 			    ((caddr_t)cm + CMSG_SPACE(datalen));
10688692c025SYoshinobu Inoue 		} else {
10692bc21ed9SDavid Malone 			clen = 0;
10702bc21ed9SDavid Malone 			cm = NULL;
10718692c025SYoshinobu Inoue 		}
10728692c025SYoshinobu Inoue 	}
10738692c025SYoshinobu Inoue 
10742bc21ed9SDavid Malone 	m_freem(control);
10752bc21ed9SDavid Malone 
10762bc21ed9SDavid Malone 	return (error);
1077df8bae1dSRodney W. Grimes }
1078df8bae1dSRodney W. Grimes 
107998271db4SGarrett Wollman void
108098271db4SGarrett Wollman unp_init(void)
108198271db4SGarrett Wollman {
108298271db4SGarrett Wollman 	unp_zone = zinit("unpcb", sizeof(struct unpcb), nmbclusters, 0, 0);
108398271db4SGarrett Wollman 	if (unp_zone == 0)
108498271db4SGarrett Wollman 		panic("unp_init");
108598271db4SGarrett Wollman 	LIST_INIT(&unp_dhead);
108698271db4SGarrett Wollman 	LIST_INIT(&unp_shead);
108798271db4SGarrett Wollman }
108898271db4SGarrett Wollman 
10890b788fa1SBill Paul #ifndef MIN
10900b788fa1SBill Paul #define	MIN(a,b) (((a)<(b))?(a):(b))
10910b788fa1SBill Paul #endif
10920b788fa1SBill Paul 
1093f708ef1bSPoul-Henning Kamp static int
10942bc21ed9SDavid Malone unp_internalize(controlp, td)
10952bc21ed9SDavid Malone 	struct mbuf **controlp;
1096b40ce416SJulian Elischer 	struct thread *td;
1097df8bae1dSRodney W. Grimes {
10982bc21ed9SDavid Malone 	struct mbuf *control = *controlp;
1099b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
11008692c025SYoshinobu Inoue 	struct filedesc *fdescp = p->p_fd;
11012bc21ed9SDavid Malone 	struct cmsghdr *cm = mtod(control, struct cmsghdr *);
11022bc21ed9SDavid Malone 	struct cmsgcred *cmcred;
11032bc21ed9SDavid Malone 	struct file **rp;
11042bc21ed9SDavid Malone 	struct file *fp;
11052bc21ed9SDavid Malone 	struct timeval *tv;
11062bc21ed9SDavid Malone 	int i, fd, *fdp;
11072bc21ed9SDavid Malone 	void *data;
11082bc21ed9SDavid Malone 	socklen_t clen = control->m_len, datalen;
11092bc21ed9SDavid Malone 	int error, oldfds;
11108692c025SYoshinobu Inoue 	u_int newlen;
1111df8bae1dSRodney W. Grimes 
11122bc21ed9SDavid Malone 	error = 0;
11132bc21ed9SDavid Malone 	*controlp = NULL;
11140b788fa1SBill Paul 
11152bc21ed9SDavid Malone 	while (cm != NULL) {
11162bc21ed9SDavid Malone 		if (sizeof(*cm) > clen || cm->cmsg_level != SOL_SOCKET
11172bc21ed9SDavid Malone 		    || cm->cmsg_len > clen) {
11182bc21ed9SDavid Malone 			error = EINVAL;
11192bc21ed9SDavid Malone 			goto out;
11202bc21ed9SDavid Malone 		}
11212bc21ed9SDavid Malone 
11222bc21ed9SDavid Malone 		data = CMSG_DATA(cm);
11232bc21ed9SDavid Malone 		datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
11242bc21ed9SDavid Malone 
11252bc21ed9SDavid Malone 		switch (cm->cmsg_type) {
11260b788fa1SBill Paul 		/*
11270b788fa1SBill Paul 		 * Fill in credential information.
11280b788fa1SBill Paul 		 */
11292bc21ed9SDavid Malone 		case SCM_CREDS:
11302bc21ed9SDavid Malone 			*controlp = sbcreatecontrol(NULL, sizeof(*cmcred),
11312bc21ed9SDavid Malone 			    SCM_CREDS, SOL_SOCKET);
11322bc21ed9SDavid Malone 			if (*controlp == NULL) {
11332bc21ed9SDavid Malone 				error = ENOBUFS;
11342bc21ed9SDavid Malone 				goto out;
11352bc21ed9SDavid Malone 			}
11362bc21ed9SDavid Malone 
11372bc21ed9SDavid Malone 			cmcred = (struct cmsgcred *)
11382bc21ed9SDavid Malone 			    CMSG_DATA(mtod(*controlp, struct cmsghdr *));
11390b788fa1SBill Paul 			cmcred->cmcred_pid = p->p_pid;
1140b1fc0ec1SRobert Watson 			cmcred->cmcred_uid = p->p_ucred->cr_ruid;
1141b1fc0ec1SRobert Watson 			cmcred->cmcred_gid = p->p_ucred->cr_rgid;
11420b788fa1SBill Paul 			cmcred->cmcred_euid = p->p_ucred->cr_uid;
11430b788fa1SBill Paul 			cmcred->cmcred_ngroups = MIN(p->p_ucred->cr_ngroups,
11440b788fa1SBill Paul 							CMGROUP_MAX);
11450b788fa1SBill Paul 			for (i = 0; i < cmcred->cmcred_ngroups; i++)
11462bc21ed9SDavid Malone 				cmcred->cmcred_groups[i] =
11472bc21ed9SDavid Malone 				    p->p_ucred->cr_groups[i];
11482bc21ed9SDavid Malone 			break;
11490b788fa1SBill Paul 
11502bc21ed9SDavid Malone 		case SCM_RIGHTS:
11512bc21ed9SDavid Malone 			oldfds = datalen / sizeof (int);
1152ed5b7817SJulian Elischer 			/*
11532bc21ed9SDavid Malone 			 * check that all the FDs passed in refer to legal files
1154ed5b7817SJulian Elischer 			 * If not, reject the entire operation.
1155ed5b7817SJulian Elischer 			 */
11562bc21ed9SDavid Malone 			fdp = data;
1157df8bae1dSRodney W. Grimes 			for (i = 0; i < oldfds; i++) {
11588692c025SYoshinobu Inoue 				fd = *fdp++;
11598692c025SYoshinobu Inoue 				if ((unsigned)fd >= fdescp->fd_nfiles ||
11602bc21ed9SDavid Malone 				    fdescp->fd_ofiles[fd] == NULL) {
11612bc21ed9SDavid Malone 					error = EBADF;
11622bc21ed9SDavid Malone 					goto out;
11632bc21ed9SDavid Malone 				}
1164df8bae1dSRodney W. Grimes 			}
1165ed5b7817SJulian Elischer 			/*
1166ed5b7817SJulian Elischer 			 * Now replace the integer FDs with pointers to
1167ed5b7817SJulian Elischer 			 * the associated global file table entry..
1168ed5b7817SJulian Elischer 			 */
11692bc21ed9SDavid Malone 			newlen = oldfds * sizeof(struct file *);
11702bc21ed9SDavid Malone 			*controlp = sbcreatecontrol(NULL, newlen,
11712bc21ed9SDavid Malone 			    SCM_RIGHTS, SOL_SOCKET);
11722bc21ed9SDavid Malone 			if (*controlp == NULL) {
11732bc21ed9SDavid Malone 				error = E2BIG;
11742bc21ed9SDavid Malone 				goto out;
11758692c025SYoshinobu Inoue 			}
11768692c025SYoshinobu Inoue 
11772bc21ed9SDavid Malone 			fdp = data;
11782bc21ed9SDavid Malone 			rp = (struct file **)
11792bc21ed9SDavid Malone 			    CMSG_DATA(mtod(*controlp, struct cmsghdr *));
11808692c025SYoshinobu Inoue 			for (i = 0; i < oldfds; i++) {
11818692c025SYoshinobu Inoue 				fp = fdescp->fd_ofiles[*fdp++];
1182df8bae1dSRodney W. Grimes 				*rp++ = fp;
1183df8bae1dSRodney W. Grimes 				fp->f_count++;
1184df8bae1dSRodney W. Grimes 				fp->f_msgcount++;
1185df8bae1dSRodney W. Grimes 				unp_rights++;
1186df8bae1dSRodney W. Grimes 			}
11872bc21ed9SDavid Malone 			break;
11882bc21ed9SDavid Malone 
11892bc21ed9SDavid Malone 		case SCM_TIMESTAMP:
11902bc21ed9SDavid Malone 			*controlp = sbcreatecontrol(NULL, sizeof(*tv),
11912bc21ed9SDavid Malone 			    SCM_TIMESTAMP, SOL_SOCKET);
11922bc21ed9SDavid Malone 			if (*controlp == NULL) {
11932bc21ed9SDavid Malone 				error = ENOBUFS;
11942bc21ed9SDavid Malone 				goto out;
11958692c025SYoshinobu Inoue 			}
11962bc21ed9SDavid Malone 			tv = (struct timeval *)
11972bc21ed9SDavid Malone 			    CMSG_DATA(mtod(*controlp, struct cmsghdr *));
11982bc21ed9SDavid Malone 			microtime(tv);
11992bc21ed9SDavid Malone 			break;
12002bc21ed9SDavid Malone 
12012bc21ed9SDavid Malone 		default:
12022bc21ed9SDavid Malone 			error = EINVAL;
12032bc21ed9SDavid Malone 			goto out;
12042bc21ed9SDavid Malone 		}
12052bc21ed9SDavid Malone 
12062bc21ed9SDavid Malone 		controlp = &(*controlp)->m_next;
12072bc21ed9SDavid Malone 
12082bc21ed9SDavid Malone 		if (CMSG_SPACE(datalen) < clen) {
12092bc21ed9SDavid Malone 			clen -= CMSG_SPACE(datalen);
12102bc21ed9SDavid Malone 			cm = (struct cmsghdr *)
12112bc21ed9SDavid Malone 			    ((caddr_t)cm + CMSG_SPACE(datalen));
12122bc21ed9SDavid Malone 		} else {
12132bc21ed9SDavid Malone 			clen = 0;
12142bc21ed9SDavid Malone 			cm = NULL;
12152bc21ed9SDavid Malone 		}
12162bc21ed9SDavid Malone 	}
12172bc21ed9SDavid Malone 
12182bc21ed9SDavid Malone out:
12192bc21ed9SDavid Malone 	m_freem(control);
12202bc21ed9SDavid Malone 
12212bc21ed9SDavid Malone 	return (error);
1222df8bae1dSRodney W. Grimes }
1223df8bae1dSRodney W. Grimes 
1224f708ef1bSPoul-Henning Kamp static int	unp_defer, unp_gcing;
1225df8bae1dSRodney W. Grimes 
1226f708ef1bSPoul-Henning Kamp static void
1227df8bae1dSRodney W. Grimes unp_gc()
1228df8bae1dSRodney W. Grimes {
1229df8bae1dSRodney W. Grimes 	register struct file *fp, *nextfp;
1230df8bae1dSRodney W. Grimes 	register struct socket *so;
1231df8bae1dSRodney W. Grimes 	struct file **extra_ref, **fpp;
1232df8bae1dSRodney W. Grimes 	int nunref, i;
1233df8bae1dSRodney W. Grimes 
1234df8bae1dSRodney W. Grimes 	if (unp_gcing)
1235df8bae1dSRodney W. Grimes 		return;
1236df8bae1dSRodney W. Grimes 	unp_gcing = 1;
1237df8bae1dSRodney W. Grimes 	unp_defer = 0;
1238ed5b7817SJulian Elischer 	/*
1239ed5b7817SJulian Elischer 	 * before going through all this, set all FDs to
1240ed5b7817SJulian Elischer 	 * be NOT defered and NOT externally accessible
1241ed5b7817SJulian Elischer 	 */
12422e3c8fcbSPoul-Henning Kamp 	LIST_FOREACH(fp, &filehead, f_list)
1243df8bae1dSRodney W. Grimes 		fp->f_flag &= ~(FMARK|FDEFER);
1244df8bae1dSRodney W. Grimes 	do {
12452e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(fp, &filehead, f_list) {
1246ed5b7817SJulian Elischer 			/*
1247ed5b7817SJulian Elischer 			 * If the file is not open, skip it
1248ed5b7817SJulian Elischer 			 */
1249df8bae1dSRodney W. Grimes 			if (fp->f_count == 0)
1250df8bae1dSRodney W. Grimes 				continue;
1251ed5b7817SJulian Elischer 			/*
1252ed5b7817SJulian Elischer 			 * If we already marked it as 'defer'  in a
1253ed5b7817SJulian Elischer 			 * previous pass, then try process it this time
1254ed5b7817SJulian Elischer 			 * and un-mark it
1255ed5b7817SJulian Elischer 			 */
1256df8bae1dSRodney W. Grimes 			if (fp->f_flag & FDEFER) {
1257df8bae1dSRodney W. Grimes 				fp->f_flag &= ~FDEFER;
1258df8bae1dSRodney W. Grimes 				unp_defer--;
1259df8bae1dSRodney W. Grimes 			} else {
1260ed5b7817SJulian Elischer 				/*
1261ed5b7817SJulian Elischer 				 * if it's not defered, then check if it's
1262ed5b7817SJulian Elischer 				 * already marked.. if so skip it
1263ed5b7817SJulian Elischer 				 */
1264df8bae1dSRodney W. Grimes 				if (fp->f_flag & FMARK)
1265df8bae1dSRodney W. Grimes 					continue;
1266ed5b7817SJulian Elischer 				/*
1267ed5b7817SJulian Elischer 				 * If all references are from messages
1268ed5b7817SJulian Elischer 				 * in transit, then skip it. it's not
1269ed5b7817SJulian Elischer 				 * externally accessible.
1270ed5b7817SJulian Elischer 				 */
1271df8bae1dSRodney W. Grimes 				if (fp->f_count == fp->f_msgcount)
1272df8bae1dSRodney W. Grimes 					continue;
1273ed5b7817SJulian Elischer 				/*
1274ed5b7817SJulian Elischer 				 * If it got this far then it must be
1275ed5b7817SJulian Elischer 				 * externally accessible.
1276ed5b7817SJulian Elischer 				 */
1277df8bae1dSRodney W. Grimes 				fp->f_flag |= FMARK;
1278df8bae1dSRodney W. Grimes 			}
1279ed5b7817SJulian Elischer 			/*
1280ed5b7817SJulian Elischer 			 * either it was defered, or it is externally
1281ed5b7817SJulian Elischer 			 * accessible and not already marked so.
1282ed5b7817SJulian Elischer 			 * Now check if it is possibly one of OUR sockets.
1283ed5b7817SJulian Elischer 			 */
1284df8bae1dSRodney W. Grimes 			if (fp->f_type != DTYPE_SOCKET ||
1285df8bae1dSRodney W. Grimes 			    (so = (struct socket *)fp->f_data) == 0)
1286df8bae1dSRodney W. Grimes 				continue;
1287748e0b0aSGarrett Wollman 			if (so->so_proto->pr_domain != &localdomain ||
1288df8bae1dSRodney W. Grimes 			    (so->so_proto->pr_flags&PR_RIGHTS) == 0)
1289df8bae1dSRodney W. Grimes 				continue;
1290df8bae1dSRodney W. Grimes #ifdef notdef
1291df8bae1dSRodney W. Grimes 			if (so->so_rcv.sb_flags & SB_LOCK) {
1292df8bae1dSRodney W. Grimes 				/*
1293df8bae1dSRodney W. Grimes 				 * This is problematical; it's not clear
1294df8bae1dSRodney W. Grimes 				 * we need to wait for the sockbuf to be
1295df8bae1dSRodney W. Grimes 				 * unlocked (on a uniprocessor, at least),
1296df8bae1dSRodney W. Grimes 				 * and it's also not clear what to do
1297df8bae1dSRodney W. Grimes 				 * if sbwait returns an error due to receipt
1298df8bae1dSRodney W. Grimes 				 * of a signal.  If sbwait does return
1299df8bae1dSRodney W. Grimes 				 * an error, we'll go into an infinite
1300df8bae1dSRodney W. Grimes 				 * loop.  Delete all of this for now.
1301df8bae1dSRodney W. Grimes 				 */
1302df8bae1dSRodney W. Grimes 				(void) sbwait(&so->so_rcv);
1303df8bae1dSRodney W. Grimes 				goto restart;
1304df8bae1dSRodney W. Grimes 			}
1305df8bae1dSRodney W. Grimes #endif
1306ed5b7817SJulian Elischer 			/*
1307ed5b7817SJulian Elischer 			 * So, Ok, it's one of our sockets and it IS externally
1308ed5b7817SJulian Elischer 			 * accessible (or was defered). Now we look
1309dc733423SDag-Erling Smørgrav 			 * to see if we hold any file descriptors in its
1310ed5b7817SJulian Elischer 			 * message buffers. Follow those links and mark them
1311ed5b7817SJulian Elischer 			 * as accessible too.
1312ed5b7817SJulian Elischer 			 */
1313df8bae1dSRodney W. Grimes 			unp_scan(so->so_rcv.sb_mb, unp_mark);
1314df8bae1dSRodney W. Grimes 		}
1315df8bae1dSRodney W. Grimes 	} while (unp_defer);
1316df8bae1dSRodney W. Grimes 	/*
1317df8bae1dSRodney W. Grimes 	 * We grab an extra reference to each of the file table entries
1318df8bae1dSRodney W. Grimes 	 * that are not otherwise accessible and then free the rights
1319df8bae1dSRodney W. Grimes 	 * that are stored in messages on them.
1320df8bae1dSRodney W. Grimes 	 *
1321df8bae1dSRodney W. Grimes 	 * The bug in the orginal code is a little tricky, so I'll describe
1322df8bae1dSRodney W. Grimes 	 * what's wrong with it here.
1323df8bae1dSRodney W. Grimes 	 *
1324df8bae1dSRodney W. Grimes 	 * It is incorrect to simply unp_discard each entry for f_msgcount
1325df8bae1dSRodney W. Grimes 	 * times -- consider the case of sockets A and B that contain
1326df8bae1dSRodney W. Grimes 	 * references to each other.  On a last close of some other socket,
1327df8bae1dSRodney W. Grimes 	 * we trigger a gc since the number of outstanding rights (unp_rights)
1328df8bae1dSRodney W. Grimes 	 * is non-zero.  If during the sweep phase the gc code un_discards,
1329df8bae1dSRodney W. Grimes 	 * we end up doing a (full) closef on the descriptor.  A closef on A
1330df8bae1dSRodney W. Grimes 	 * results in the following chain.  Closef calls soo_close, which
1331df8bae1dSRodney W. Grimes 	 * calls soclose.   Soclose calls first (through the switch
1332df8bae1dSRodney W. Grimes 	 * uipc_usrreq) unp_detach, which re-invokes unp_gc.  Unp_gc simply
1333df8bae1dSRodney W. Grimes 	 * returns because the previous instance had set unp_gcing, and
1334df8bae1dSRodney W. Grimes 	 * we return all the way back to soclose, which marks the socket
1335df8bae1dSRodney W. Grimes 	 * with SS_NOFDREF, and then calls sofree.  Sofree calls sorflush
1336df8bae1dSRodney W. Grimes 	 * to free up the rights that are queued in messages on the socket A,
1337df8bae1dSRodney W. Grimes 	 * i.e., the reference on B.  The sorflush calls via the dom_dispose
1338df8bae1dSRodney W. Grimes 	 * switch unp_dispose, which unp_scans with unp_discard.  This second
1339df8bae1dSRodney W. Grimes 	 * instance of unp_discard just calls closef on B.
1340df8bae1dSRodney W. Grimes 	 *
1341df8bae1dSRodney W. Grimes 	 * Well, a similar chain occurs on B, resulting in a sorflush on B,
1342df8bae1dSRodney W. Grimes 	 * which results in another closef on A.  Unfortunately, A is already
1343df8bae1dSRodney W. Grimes 	 * being closed, and the descriptor has already been marked with
1344df8bae1dSRodney W. Grimes 	 * SS_NOFDREF, and soclose panics at this point.
1345df8bae1dSRodney W. Grimes 	 *
1346df8bae1dSRodney W. Grimes 	 * Here, we first take an extra reference to each inaccessible
1347df8bae1dSRodney W. Grimes 	 * descriptor.  Then, we call sorflush ourself, since we know
1348df8bae1dSRodney W. Grimes 	 * it is a Unix domain socket anyhow.  After we destroy all the
1349df8bae1dSRodney W. Grimes 	 * rights carried in messages, we do a last closef to get rid
1350df8bae1dSRodney W. Grimes 	 * of our extra reference.  This is the last close, and the
1351df8bae1dSRodney W. Grimes 	 * unp_detach etc will shut down the socket.
1352df8bae1dSRodney W. Grimes 	 *
1353df8bae1dSRodney W. Grimes 	 * 91/09/19, bsy@cs.cmu.edu
1354df8bae1dSRodney W. Grimes 	 */
1355df8bae1dSRodney W. Grimes 	extra_ref = malloc(nfiles * sizeof(struct file *), M_FILE, M_WAITOK);
13562e3c8fcbSPoul-Henning Kamp 	for (nunref = 0, fp = LIST_FIRST(&filehead), fpp = extra_ref; fp != 0;
1357bc6f0e79SJeffrey Hsu 	    fp = nextfp) {
13582e3c8fcbSPoul-Henning Kamp 		nextfp = LIST_NEXT(fp, f_list);
1359ed5b7817SJulian Elischer 		/*
1360ed5b7817SJulian Elischer 		 * If it's not open, skip it
1361ed5b7817SJulian Elischer 		 */
1362df8bae1dSRodney W. Grimes 		if (fp->f_count == 0)
1363df8bae1dSRodney W. Grimes 			continue;
1364ed5b7817SJulian Elischer 		/*
1365ed5b7817SJulian Elischer 		 * If all refs are from msgs, and it's not marked accessible
1366ed5b7817SJulian Elischer 		 * then it must be referenced from some unreachable cycle
1367ed5b7817SJulian Elischer 		 * of (shut-down) FDs, so include it in our
1368ed5b7817SJulian Elischer 		 * list of FDs to remove
1369ed5b7817SJulian Elischer 		 */
1370df8bae1dSRodney W. Grimes 		if (fp->f_count == fp->f_msgcount && !(fp->f_flag & FMARK)) {
1371df8bae1dSRodney W. Grimes 			*fpp++ = fp;
1372df8bae1dSRodney W. Grimes 			nunref++;
1373df8bae1dSRodney W. Grimes 			fp->f_count++;
1374df8bae1dSRodney W. Grimes 		}
1375df8bae1dSRodney W. Grimes 	}
1376ed5b7817SJulian Elischer 	/*
1377ed5b7817SJulian Elischer 	 * for each FD on our hit list, do the following two things
1378ed5b7817SJulian Elischer 	 */
13791c7c3c6aSMatthew Dillon 	for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) {
13801c7c3c6aSMatthew Dillon 		struct file *tfp = *fpp;
13811c7c3c6aSMatthew Dillon 		if (tfp->f_type == DTYPE_SOCKET && tfp->f_data != NULL)
13821c7c3c6aSMatthew Dillon 			sorflush((struct socket *)(tfp->f_data));
13831c7c3c6aSMatthew Dillon 	}
1384df8bae1dSRodney W. Grimes 	for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp)
1385b40ce416SJulian Elischer 		closef(*fpp, (struct thread *) NULL);
1386df8bae1dSRodney W. Grimes 	free((caddr_t)extra_ref, M_FILE);
1387df8bae1dSRodney W. Grimes 	unp_gcing = 0;
1388df8bae1dSRodney W. Grimes }
1389df8bae1dSRodney W. Grimes 
139026f9a767SRodney W. Grimes void
1391df8bae1dSRodney W. Grimes unp_dispose(m)
1392df8bae1dSRodney W. Grimes 	struct mbuf *m;
1393df8bae1dSRodney W. Grimes {
1394996c772fSJohn Dyson 
1395df8bae1dSRodney W. Grimes 	if (m)
1396df8bae1dSRodney W. Grimes 		unp_scan(m, unp_discard);
1397df8bae1dSRodney W. Grimes }
1398df8bae1dSRodney W. Grimes 
13990c1bb4fbSDima Dorfman static int
14000c1bb4fbSDima Dorfman unp_listen(unp, p)
14010c1bb4fbSDima Dorfman 	struct unpcb *unp;
14020c1bb4fbSDima Dorfman 	struct proc *p;
14030c1bb4fbSDima Dorfman {
14040c1bb4fbSDima Dorfman 
14050c1bb4fbSDima Dorfman 	bzero(&unp->unp_peercred, sizeof(unp->unp_peercred));
14060c1bb4fbSDima Dorfman 	unp->unp_peercred.cr_uid = p->p_ucred->cr_uid;
14070c1bb4fbSDima Dorfman 	unp->unp_peercred.cr_ngroups = p->p_ucred->cr_ngroups;
14080c1bb4fbSDima Dorfman 	bcopy(p->p_ucred->cr_groups, unp->unp_peercred.cr_groups,
14090c1bb4fbSDima Dorfman 	    sizeof(unp->unp_peercred.cr_groups));
14100c1bb4fbSDima Dorfman 	unp->unp_flags |= UNP_HAVEPCCACHED;
14110c1bb4fbSDima Dorfman 	return (0);
14120c1bb4fbSDima Dorfman }
14130c1bb4fbSDima Dorfman 
1414f708ef1bSPoul-Henning Kamp static void
1415df8bae1dSRodney W. Grimes unp_scan(m0, op)
1416df8bae1dSRodney W. Grimes 	register struct mbuf *m0;
1417996c772fSJohn Dyson 	void (*op) __P((struct file *));
1418df8bae1dSRodney W. Grimes {
14192bc21ed9SDavid Malone 	struct mbuf *m;
14202bc21ed9SDavid Malone 	struct file **rp;
14212bc21ed9SDavid Malone 	struct cmsghdr *cm;
14222bc21ed9SDavid Malone 	void *data;
14232bc21ed9SDavid Malone 	int i;
14242bc21ed9SDavid Malone 	socklen_t clen, datalen;
1425df8bae1dSRodney W. Grimes 	int qfds;
1426df8bae1dSRodney W. Grimes 
1427df8bae1dSRodney W. Grimes 	while (m0) {
14282bc21ed9SDavid Malone 		for (m = m0; m; m = m->m_next) {
14292bc21ed9SDavid Malone 			if (m->m_type == MT_CONTROL)
1430df8bae1dSRodney W. Grimes 				continue;
14312bc21ed9SDavid Malone 
14322bc21ed9SDavid Malone 			cm = mtod(m, struct cmsghdr *);
14332bc21ed9SDavid Malone 			clen = m->m_len;
14342bc21ed9SDavid Malone 
14352bc21ed9SDavid Malone 			while (cm != NULL) {
14362bc21ed9SDavid Malone 				if (sizeof(*cm) > clen || cm->cmsg_len > clen)
14372bc21ed9SDavid Malone 					break;
14382bc21ed9SDavid Malone 
14392bc21ed9SDavid Malone 				data = CMSG_DATA(cm);
14402bc21ed9SDavid Malone 				datalen = (caddr_t)cm + cm->cmsg_len
14412bc21ed9SDavid Malone 				    - (caddr_t)data;
14422bc21ed9SDavid Malone 
14432bc21ed9SDavid Malone 				if (cm->cmsg_level == SOL_SOCKET &&
14442bc21ed9SDavid Malone 				    cm->cmsg_type == SCM_RIGHTS) {
14452bc21ed9SDavid Malone 					qfds = datalen / sizeof (struct file *);
14462bc21ed9SDavid Malone 					rp = data;
1447df8bae1dSRodney W. Grimes 					for (i = 0; i < qfds; i++)
1448df8bae1dSRodney W. Grimes 						(*op)(*rp++);
14492bc21ed9SDavid Malone 				}
14502bc21ed9SDavid Malone 
14512bc21ed9SDavid Malone 				if (CMSG_SPACE(datalen) < clen) {
14522bc21ed9SDavid Malone 					clen -= CMSG_SPACE(datalen);
14532bc21ed9SDavid Malone 					cm = (struct cmsghdr *)
14542bc21ed9SDavid Malone 					    ((caddr_t)cm + CMSG_SPACE(datalen));
14552bc21ed9SDavid Malone 				} else {
14562bc21ed9SDavid Malone 					clen = 0;
14572bc21ed9SDavid Malone 					cm = NULL;
14582bc21ed9SDavid Malone 				}
14592bc21ed9SDavid Malone 			}
1460df8bae1dSRodney W. Grimes 		}
1461df8bae1dSRodney W. Grimes 		m0 = m0->m_act;
1462df8bae1dSRodney W. Grimes 	}
1463df8bae1dSRodney W. Grimes }
1464df8bae1dSRodney W. Grimes 
1465f708ef1bSPoul-Henning Kamp static void
1466df8bae1dSRodney W. Grimes unp_mark(fp)
1467df8bae1dSRodney W. Grimes 	struct file *fp;
1468df8bae1dSRodney W. Grimes {
1469df8bae1dSRodney W. Grimes 
1470df8bae1dSRodney W. Grimes 	if (fp->f_flag & FMARK)
1471df8bae1dSRodney W. Grimes 		return;
1472df8bae1dSRodney W. Grimes 	unp_defer++;
1473df8bae1dSRodney W. Grimes 	fp->f_flag |= (FMARK|FDEFER);
1474df8bae1dSRodney W. Grimes }
1475df8bae1dSRodney W. Grimes 
1476f708ef1bSPoul-Henning Kamp static void
1477df8bae1dSRodney W. Grimes unp_discard(fp)
1478df8bae1dSRodney W. Grimes 	struct file *fp;
1479df8bae1dSRodney W. Grimes {
1480df8bae1dSRodney W. Grimes 
1481df8bae1dSRodney W. Grimes 	fp->f_msgcount--;
1482df8bae1dSRodney W. Grimes 	unp_rights--;
1483b40ce416SJulian Elischer 	(void) closef(fp, (struct thread *)NULL);
1484df8bae1dSRodney W. Grimes }
1485