xref: /freebsd/sys/kern/uipc_usrreq.c (revision 8692c02553ffe249cc0f55a712223ff5e6dece7b)
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>
40df8bae1dSRodney W. Grimes #include <sys/domain.h>
413ac4d1efSBruce Evans #include <sys/fcntl.h>
42d826c479SBruce Evans #include <sys/malloc.h>		/* XXX must be before <sys/file.h> */
43639acc13SGarrett Wollman #include <sys/file.h>
44639acc13SGarrett Wollman #include <sys/filedesc.h>
45639acc13SGarrett Wollman #include <sys/mbuf.h>
46639acc13SGarrett Wollman #include <sys/namei.h>
47639acc13SGarrett Wollman #include <sys/proc.h>
48df8bae1dSRodney W. Grimes #include <sys/protosw.h>
49df8bae1dSRodney W. Grimes #include <sys/socket.h>
50df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
51df8bae1dSRodney W. Grimes #include <sys/stat.h>
52639acc13SGarrett Wollman #include <sys/sysctl.h>
53639acc13SGarrett Wollman #include <sys/un.h>
5498271db4SGarrett Wollman #include <sys/unpcb.h>
55639acc13SGarrett Wollman #include <sys/vnode.h>
56df8bae1dSRodney W. Grimes 
5798271db4SGarrett Wollman #include <vm/vm_zone.h>
5898271db4SGarrett Wollman 
59632a035fSEivind Eklund static	struct vm_zone *unp_zone;
6098271db4SGarrett Wollman static	unp_gen_t unp_gencnt;
6198271db4SGarrett Wollman static	u_int unp_count;
6298271db4SGarrett Wollman 
6398271db4SGarrett Wollman static	struct unp_head unp_shead, unp_dhead;
6498271db4SGarrett Wollman 
65df8bae1dSRodney W. Grimes /*
66df8bae1dSRodney W. Grimes  * Unix communications domain.
67df8bae1dSRodney W. Grimes  *
68df8bae1dSRodney W. Grimes  * TODO:
69df8bae1dSRodney W. Grimes  *	SEQPACKET, RDM
70df8bae1dSRodney W. Grimes  *	rethink name space problems
71df8bae1dSRodney W. Grimes  *	need a proper out-of-band
7298271db4SGarrett Wollman  *	lock pushdown
73df8bae1dSRodney W. Grimes  */
74f708ef1bSPoul-Henning Kamp static struct	sockaddr sun_noname = { sizeof(sun_noname), AF_LOCAL };
75f708ef1bSPoul-Henning Kamp static ino_t	unp_ino;		/* prototype for fake inode numbers */
76f708ef1bSPoul-Henning Kamp 
77f708ef1bSPoul-Henning Kamp static int     unp_attach __P((struct socket *));
78f708ef1bSPoul-Henning Kamp static void    unp_detach __P((struct unpcb *));
7957bf258eSGarrett Wollman static int     unp_bind __P((struct unpcb *,struct sockaddr *, struct proc *));
8057bf258eSGarrett Wollman static int     unp_connect __P((struct socket *,struct sockaddr *,
8157bf258eSGarrett Wollman 				struct proc *));
82f708ef1bSPoul-Henning Kamp static void    unp_disconnect __P((struct unpcb *));
83f708ef1bSPoul-Henning Kamp static void    unp_shutdown __P((struct unpcb *));
84f708ef1bSPoul-Henning Kamp static void    unp_drop __P((struct unpcb *, int));
85f708ef1bSPoul-Henning Kamp static void    unp_gc __P((void));
86f708ef1bSPoul-Henning Kamp static void    unp_scan __P((struct mbuf *, void (*)(struct file *)));
87f708ef1bSPoul-Henning Kamp static void    unp_mark __P((struct file *));
88f708ef1bSPoul-Henning Kamp static void    unp_discard __P((struct file *));
89f708ef1bSPoul-Henning Kamp static int     unp_internalize __P((struct mbuf *, struct proc *));
90f708ef1bSPoul-Henning Kamp 
91a29f300eSGarrett Wollman static int
92a29f300eSGarrett Wollman uipc_abort(struct socket *so)
93df8bae1dSRodney W. Grimes {
94df8bae1dSRodney W. Grimes 	struct unpcb *unp = sotounpcb(so);
95df8bae1dSRodney W. Grimes 
96a29f300eSGarrett Wollman 	if (unp == 0)
97a29f300eSGarrett Wollman 		return EINVAL;
98a29f300eSGarrett Wollman 	unp_drop(unp, ECONNABORTED);
99a29f300eSGarrett Wollman 	return 0;
100df8bae1dSRodney W. Grimes }
101df8bae1dSRodney W. Grimes 
102a29f300eSGarrett Wollman static int
10357bf258eSGarrett Wollman uipc_accept(struct socket *so, struct sockaddr **nam)
104a29f300eSGarrett Wollman {
105a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so);
106df8bae1dSRodney W. Grimes 
107a29f300eSGarrett Wollman 	if (unp == 0)
108a29f300eSGarrett Wollman 		return EINVAL;
109df8bae1dSRodney W. Grimes 
110df8bae1dSRodney W. Grimes 	/*
111df8bae1dSRodney W. Grimes 	 * Pass back name of connected socket,
112df8bae1dSRodney W. Grimes 	 * if it was bound and we are still connected
113df8bae1dSRodney W. Grimes 	 * (our peer may have closed already!).
114df8bae1dSRodney W. Grimes 	 */
115df8bae1dSRodney W. Grimes 	if (unp->unp_conn && unp->unp_conn->unp_addr) {
11657bf258eSGarrett Wollman 		*nam = dup_sockaddr((struct sockaddr *)unp->unp_conn->unp_addr,
11757bf258eSGarrett Wollman 				    1);
118df8bae1dSRodney W. Grimes 	} else {
11957bf258eSGarrett Wollman 		*nam = dup_sockaddr((struct sockaddr *)&sun_noname, 1);
120df8bae1dSRodney W. Grimes 	}
121a29f300eSGarrett Wollman 	return 0;
122a29f300eSGarrett Wollman }
123df8bae1dSRodney W. Grimes 
124a29f300eSGarrett Wollman static int
125a29f300eSGarrett Wollman uipc_attach(struct socket *so, int proto, struct proc *p)
126a29f300eSGarrett Wollman {
127a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so);
128df8bae1dSRodney W. Grimes 
129a29f300eSGarrett Wollman 	if (unp != 0)
130a29f300eSGarrett Wollman 		return EISCONN;
131a29f300eSGarrett Wollman 	return unp_attach(so);
132a29f300eSGarrett Wollman }
133a29f300eSGarrett Wollman 
134a29f300eSGarrett Wollman static int
13557bf258eSGarrett Wollman uipc_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
136a29f300eSGarrett Wollman {
137a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so);
138a29f300eSGarrett Wollman 
139a29f300eSGarrett Wollman 	if (unp == 0)
140a29f300eSGarrett Wollman 		return EINVAL;
141a29f300eSGarrett Wollman 
142a29f300eSGarrett Wollman 	return unp_bind(unp, nam, p);
143a29f300eSGarrett Wollman }
144a29f300eSGarrett Wollman 
145a29f300eSGarrett Wollman static int
14657bf258eSGarrett Wollman uipc_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
147a29f300eSGarrett Wollman {
148a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so);
149a29f300eSGarrett Wollman 
150a29f300eSGarrett Wollman 	if (unp == 0)
151a29f300eSGarrett Wollman 		return EINVAL;
152a29f300eSGarrett Wollman 	return unp_connect(so, nam, curproc);
153a29f300eSGarrett Wollman }
154a29f300eSGarrett Wollman 
155a29f300eSGarrett Wollman static int
156a29f300eSGarrett Wollman uipc_connect2(struct socket *so1, struct socket *so2)
157a29f300eSGarrett Wollman {
158a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so1);
159a29f300eSGarrett Wollman 
160a29f300eSGarrett Wollman 	if (unp == 0)
161a29f300eSGarrett Wollman 		return EINVAL;
162a29f300eSGarrett Wollman 
163a29f300eSGarrett Wollman 	return unp_connect2(so1, so2);
164a29f300eSGarrett Wollman }
165a29f300eSGarrett Wollman 
166a29f300eSGarrett Wollman /* control is EOPNOTSUPP */
167a29f300eSGarrett Wollman 
168a29f300eSGarrett Wollman static int
169a29f300eSGarrett Wollman uipc_detach(struct socket *so)
170a29f300eSGarrett Wollman {
171a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so);
172a29f300eSGarrett Wollman 
173a29f300eSGarrett Wollman 	if (unp == 0)
174a29f300eSGarrett Wollman 		return EINVAL;
175a29f300eSGarrett Wollman 
176a29f300eSGarrett Wollman 	unp_detach(unp);
177a29f300eSGarrett Wollman 	return 0;
178a29f300eSGarrett Wollman }
179a29f300eSGarrett Wollman 
180a29f300eSGarrett Wollman static int
181a29f300eSGarrett Wollman uipc_disconnect(struct socket *so)
182a29f300eSGarrett Wollman {
183a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so);
184a29f300eSGarrett Wollman 
185a29f300eSGarrett Wollman 	if (unp == 0)
186a29f300eSGarrett Wollman 		return EINVAL;
187a29f300eSGarrett Wollman 	unp_disconnect(unp);
188a29f300eSGarrett Wollman 	return 0;
189a29f300eSGarrett Wollman }
190a29f300eSGarrett Wollman 
191a29f300eSGarrett Wollman static int
192a29f300eSGarrett Wollman uipc_listen(struct socket *so, struct proc *p)
193a29f300eSGarrett Wollman {
194a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so);
195a29f300eSGarrett Wollman 
196a29f300eSGarrett Wollman 	if (unp == 0 || unp->unp_vnode == 0)
197a29f300eSGarrett Wollman 		return EINVAL;
198a29f300eSGarrett Wollman 	return 0;
199a29f300eSGarrett Wollman }
200a29f300eSGarrett Wollman 
201a29f300eSGarrett Wollman static int
20257bf258eSGarrett Wollman uipc_peeraddr(struct socket *so, struct sockaddr **nam)
203a29f300eSGarrett Wollman {
204a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so);
205a29f300eSGarrett Wollman 
206a29f300eSGarrett Wollman 	if (unp == 0)
207a29f300eSGarrett Wollman 		return EINVAL;
20857bf258eSGarrett Wollman 	if (unp->unp_conn && unp->unp_conn->unp_addr)
20957bf258eSGarrett Wollman 		*nam = dup_sockaddr((struct sockaddr *)unp->unp_conn->unp_addr,
21057bf258eSGarrett Wollman 				    1);
211a29f300eSGarrett Wollman 	return 0;
212a29f300eSGarrett Wollman }
213a29f300eSGarrett Wollman 
214a29f300eSGarrett Wollman static int
215a29f300eSGarrett Wollman uipc_rcvd(struct socket *so, int flags)
216a29f300eSGarrett Wollman {
217a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so);
218a29f300eSGarrett Wollman 	struct socket *so2;
219a29f300eSGarrett Wollman 
220a29f300eSGarrett Wollman 	if (unp == 0)
221a29f300eSGarrett Wollman 		return EINVAL;
222df8bae1dSRodney W. Grimes 	switch (so->so_type) {
223df8bae1dSRodney W. Grimes 	case SOCK_DGRAM:
224a29f300eSGarrett Wollman 		panic("uipc_rcvd DGRAM?");
225df8bae1dSRodney W. Grimes 		/*NOTREACHED*/
226df8bae1dSRodney W. Grimes 
227df8bae1dSRodney W. Grimes 	case SOCK_STREAM:
228df8bae1dSRodney W. Grimes 		if (unp->unp_conn == 0)
229df8bae1dSRodney W. Grimes 			break;
230df8bae1dSRodney W. Grimes 		so2 = unp->unp_conn->unp_socket;
231df8bae1dSRodney W. Grimes 		/*
232df8bae1dSRodney W. Grimes 		 * Adjust backpressure on sender
233df8bae1dSRodney W. Grimes 		 * and wakeup any waiting to write.
234df8bae1dSRodney W. Grimes 		 */
235ff8b0106SBrian Feldman 		so2->so_snd.sb_mbmax += unp->unp_mbcnt - so->so_rcv.sb_mbcnt;
236ff8b0106SBrian Feldman 		unp->unp_mbcnt = so->so_rcv.sb_mbcnt;
237ff8b0106SBrian Feldman 		so2->so_snd.sb_hiwat += unp->unp_cc - so->so_rcv.sb_cc;
238ecf72308SBrian Feldman 		(void)chgsbsize(so2->so_cred->cr_uid,
239ecf72308SBrian Feldman 		    (rlim_t)unp->unp_cc - so->so_rcv.sb_cc);
240ff8b0106SBrian Feldman 		unp->unp_cc = so->so_rcv.sb_cc;
241df8bae1dSRodney W. Grimes 		sowwakeup(so2);
242df8bae1dSRodney W. Grimes 		break;
243df8bae1dSRodney W. Grimes 
244df8bae1dSRodney W. Grimes 	default:
245a29f300eSGarrett Wollman 		panic("uipc_rcvd unknown socktype");
246df8bae1dSRodney W. Grimes 	}
247a29f300eSGarrett Wollman 	return 0;
248a29f300eSGarrett Wollman }
249df8bae1dSRodney W. Grimes 
250a29f300eSGarrett Wollman /* pru_rcvoob is EOPNOTSUPP */
251a29f300eSGarrett Wollman 
252a29f300eSGarrett Wollman static int
25357bf258eSGarrett Wollman uipc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
254a29f300eSGarrett Wollman 	  struct mbuf *control, struct proc *p)
255a29f300eSGarrett Wollman {
256a29f300eSGarrett Wollman 	int error = 0;
257a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so);
258a29f300eSGarrett Wollman 	struct socket *so2;
259a29f300eSGarrett Wollman 
260a29f300eSGarrett Wollman 	if (unp == 0) {
261a29f300eSGarrett Wollman 		error = EINVAL;
262a29f300eSGarrett Wollman 		goto release;
263a29f300eSGarrett Wollman 	}
264a29f300eSGarrett Wollman 	if (flags & PRUS_OOB) {
265a29f300eSGarrett Wollman 		error = EOPNOTSUPP;
266a29f300eSGarrett Wollman 		goto release;
267a29f300eSGarrett Wollman 	}
268a29f300eSGarrett Wollman 
269df8bae1dSRodney W. Grimes 	if (control && (error = unp_internalize(control, p)))
270a29f300eSGarrett Wollman 		goto release;
271df8bae1dSRodney W. Grimes 
272a29f300eSGarrett Wollman 	switch (so->so_type) {
273a29f300eSGarrett Wollman 	case SOCK_DGRAM:
274a29f300eSGarrett Wollman 	{
275df8bae1dSRodney W. Grimes 		struct sockaddr *from;
276df8bae1dSRodney W. Grimes 
277df8bae1dSRodney W. Grimes 		if (nam) {
278df8bae1dSRodney W. Grimes 			if (unp->unp_conn) {
279df8bae1dSRodney W. Grimes 				error = EISCONN;
280df8bae1dSRodney W. Grimes 				break;
281df8bae1dSRodney W. Grimes 			}
282df8bae1dSRodney W. Grimes 			error = unp_connect(so, nam, p);
283df8bae1dSRodney W. Grimes 			if (error)
284df8bae1dSRodney W. Grimes 				break;
285df8bae1dSRodney W. Grimes 		} else {
286df8bae1dSRodney W. Grimes 			if (unp->unp_conn == 0) {
287df8bae1dSRodney W. Grimes 				error = ENOTCONN;
288df8bae1dSRodney W. Grimes 				break;
289df8bae1dSRodney W. Grimes 			}
290df8bae1dSRodney W. Grimes 		}
291df8bae1dSRodney W. Grimes 		so2 = unp->unp_conn->unp_socket;
292df8bae1dSRodney W. Grimes 		if (unp->unp_addr)
29357bf258eSGarrett Wollman 			from = (struct sockaddr *)unp->unp_addr;
294df8bae1dSRodney W. Grimes 		else
295df8bae1dSRodney W. Grimes 			from = &sun_noname;
296df8bae1dSRodney W. Grimes 		if (sbappendaddr(&so2->so_rcv, from, m, control)) {
297df8bae1dSRodney W. Grimes 			sorwakeup(so2);
298df8bae1dSRodney W. Grimes 			m = 0;
299df8bae1dSRodney W. Grimes 			control = 0;
300df8bae1dSRodney W. Grimes 		} else
301df8bae1dSRodney W. Grimes 			error = ENOBUFS;
302df8bae1dSRodney W. Grimes 		if (nam)
303df8bae1dSRodney W. Grimes 			unp_disconnect(unp);
304df8bae1dSRodney W. Grimes 		break;
305df8bae1dSRodney W. Grimes 	}
306df8bae1dSRodney W. Grimes 
307df8bae1dSRodney W. Grimes 	case SOCK_STREAM:
3086b8fda4dSGarrett Wollman 		/* Connect if not connected yet. */
3096b8fda4dSGarrett Wollman 		/*
3106b8fda4dSGarrett Wollman 		 * Note: A better implementation would complain
311402cc72dSDavid Greenman 		 * if not equal to the peer's address.
3126b8fda4dSGarrett Wollman 		 */
313402cc72dSDavid Greenman 		if ((so->so_state & SS_ISCONNECTED) == 0) {
314402cc72dSDavid Greenman 			if (nam) {
315402cc72dSDavid Greenman 				error = unp_connect(so, nam, p);
316402cc72dSDavid Greenman 				if (error)
3176b8fda4dSGarrett Wollman 					break;	/* XXX */
318402cc72dSDavid Greenman 			} else {
319402cc72dSDavid Greenman 				error = ENOTCONN;
320402cc72dSDavid Greenman 				break;
321402cc72dSDavid Greenman 			}
322402cc72dSDavid Greenman 		}
323402cc72dSDavid Greenman 
324df8bae1dSRodney W. Grimes 		if (so->so_state & SS_CANTSENDMORE) {
325df8bae1dSRodney W. Grimes 			error = EPIPE;
326df8bae1dSRodney W. Grimes 			break;
327df8bae1dSRodney W. Grimes 		}
328df8bae1dSRodney W. Grimes 		if (unp->unp_conn == 0)
329a29f300eSGarrett Wollman 			panic("uipc_send connected but no connection?");
330df8bae1dSRodney W. Grimes 		so2 = unp->unp_conn->unp_socket;
331df8bae1dSRodney W. Grimes 		/*
332df8bae1dSRodney W. Grimes 		 * Send to paired receive port, and then reduce
333df8bae1dSRodney W. Grimes 		 * send buffer hiwater marks to maintain backpressure.
334df8bae1dSRodney W. Grimes 		 * Wake up readers.
335df8bae1dSRodney W. Grimes 		 */
336df8bae1dSRodney W. Grimes 		if (control) {
337ff8b0106SBrian Feldman 			if (sbappendcontrol(&so2->so_rcv, m, control))
338df8bae1dSRodney W. Grimes 				control = 0;
339df8bae1dSRodney W. Grimes 		} else
340ff8b0106SBrian Feldman 			sbappend(&so2->so_rcv, m);
341ff8b0106SBrian Feldman 		so->so_snd.sb_mbmax -=
342ff8b0106SBrian Feldman 			so2->so_rcv.sb_mbcnt - unp->unp_conn->unp_mbcnt;
343ff8b0106SBrian Feldman 		unp->unp_conn->unp_mbcnt = so2->so_rcv.sb_mbcnt;
344ff8b0106SBrian Feldman 		so->so_snd.sb_hiwat -=
345ff8b0106SBrian Feldman 		    so2->so_rcv.sb_cc - unp->unp_conn->unp_cc;
346ecf72308SBrian Feldman 		(void)chgsbsize(so->so_cred->cr_uid,
347ecf72308SBrian Feldman 		    (rlim_t)unp->unp_conn->unp_cc - so2->so_rcv.sb_cc);
348ff8b0106SBrian Feldman 		unp->unp_conn->unp_cc = so2->so_rcv.sb_cc;
349df8bae1dSRodney W. Grimes 		sorwakeup(so2);
350df8bae1dSRodney W. Grimes 		m = 0;
351df8bae1dSRodney W. Grimes 		break;
352df8bae1dSRodney W. Grimes 
353df8bae1dSRodney W. Grimes 	default:
354a29f300eSGarrett Wollman 		panic("uipc_send unknown socktype");
355df8bae1dSRodney W. Grimes 	}
356a29f300eSGarrett Wollman 
3576b8fda4dSGarrett Wollman 	/*
3586b8fda4dSGarrett Wollman 	 * SEND_EOF is equivalent to a SEND followed by
3596b8fda4dSGarrett Wollman 	 * a SHUTDOWN.
3606b8fda4dSGarrett Wollman 	 */
361a29f300eSGarrett Wollman 	if (flags & PRUS_EOF) {
3626b8fda4dSGarrett Wollman 		socantsendmore(so);
3636b8fda4dSGarrett Wollman 		unp_shutdown(unp);
3646b8fda4dSGarrett Wollman 	}
365df8bae1dSRodney W. Grimes 
366bd508d39SDon Lewis 	if (control && error != 0)
367bd508d39SDon Lewis 		unp_dispose(control);
368bd508d39SDon Lewis 
369a29f300eSGarrett Wollman release:
370a29f300eSGarrett Wollman 	if (control)
371a29f300eSGarrett Wollman 		m_freem(control);
372a29f300eSGarrett Wollman 	if (m)
373a29f300eSGarrett Wollman 		m_freem(m);
374a29f300eSGarrett Wollman 	return error;
375a29f300eSGarrett Wollman }
376df8bae1dSRodney W. Grimes 
377a29f300eSGarrett Wollman static int
378a29f300eSGarrett Wollman uipc_sense(struct socket *so, struct stat *sb)
379a29f300eSGarrett Wollman {
380a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so);
381a29f300eSGarrett Wollman 	struct socket *so2;
382a29f300eSGarrett Wollman 
383a29f300eSGarrett Wollman 	if (unp == 0)
384a29f300eSGarrett Wollman 		return EINVAL;
385a29f300eSGarrett Wollman 	sb->st_blksize = so->so_snd.sb_hiwat;
386df8bae1dSRodney W. Grimes 	if (so->so_type == SOCK_STREAM && unp->unp_conn != 0) {
387df8bae1dSRodney W. Grimes 		so2 = unp->unp_conn->unp_socket;
388a29f300eSGarrett Wollman 		sb->st_blksize += so2->so_rcv.sb_cc;
389df8bae1dSRodney W. Grimes 	}
390bfbb9ce6SPoul-Henning Kamp 	sb->st_dev = NOUDEV;
391df8bae1dSRodney W. Grimes 	if (unp->unp_ino == 0)
392df8bae1dSRodney W. Grimes 		unp->unp_ino = unp_ino++;
393a29f300eSGarrett Wollman 	sb->st_ino = unp->unp_ino;
394df8bae1dSRodney W. Grimes 	return (0);
395a29f300eSGarrett Wollman }
396df8bae1dSRodney W. Grimes 
397a29f300eSGarrett Wollman static int
398a29f300eSGarrett Wollman uipc_shutdown(struct socket *so)
399a29f300eSGarrett Wollman {
400a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so);
401df8bae1dSRodney W. Grimes 
402a29f300eSGarrett Wollman 	if (unp == 0)
403a29f300eSGarrett Wollman 		return EINVAL;
404a29f300eSGarrett Wollman 	socantsendmore(so);
405a29f300eSGarrett Wollman 	unp_shutdown(unp);
406a29f300eSGarrett Wollman 	return 0;
407a29f300eSGarrett Wollman }
408df8bae1dSRodney W. Grimes 
409a29f300eSGarrett Wollman static int
41057bf258eSGarrett Wollman uipc_sockaddr(struct socket *so, struct sockaddr **nam)
411a29f300eSGarrett Wollman {
412a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so);
413a29f300eSGarrett Wollman 
414a29f300eSGarrett Wollman 	if (unp == 0)
415a29f300eSGarrett Wollman 		return EINVAL;
41657bf258eSGarrett Wollman 	if (unp->unp_addr)
41757bf258eSGarrett Wollman 		*nam = dup_sockaddr((struct sockaddr *)unp->unp_addr, 1);
418a29f300eSGarrett Wollman 	return 0;
419df8bae1dSRodney W. Grimes }
420a29f300eSGarrett Wollman 
421a29f300eSGarrett Wollman struct pr_usrreqs uipc_usrreqs = {
422a29f300eSGarrett Wollman 	uipc_abort, uipc_accept, uipc_attach, uipc_bind, uipc_connect,
423a29f300eSGarrett Wollman 	uipc_connect2, pru_control_notsupp, uipc_detach, uipc_disconnect,
424a29f300eSGarrett Wollman 	uipc_listen, uipc_peeraddr, uipc_rcvd, pru_rcvoob_notsupp,
425a29f300eSGarrett Wollman 	uipc_send, uipc_sense, uipc_shutdown, uipc_sockaddr,
42651338ea8SPeter Wemm 	sosend, soreceive, sopoll
427a29f300eSGarrett Wollman };
428df8bae1dSRodney W. Grimes 
429df8bae1dSRodney W. Grimes /*
430df8bae1dSRodney W. Grimes  * Both send and receive buffers are allocated PIPSIZ bytes of buffering
431df8bae1dSRodney W. Grimes  * for stream sockets, although the total for sender and receiver is
432df8bae1dSRodney W. Grimes  * actually only PIPSIZ.
433df8bae1dSRodney W. Grimes  * Datagram sockets really use the sendspace as the maximum datagram size,
434df8bae1dSRodney W. Grimes  * and don't really want to reserve the sendspace.  Their recvspace should
435df8bae1dSRodney W. Grimes  * be large enough for at least one max-size datagram plus address.
436df8bae1dSRodney W. Grimes  */
4375dce41c5SJohn Dyson #ifndef PIPSIZ
4385dce41c5SJohn Dyson #define	PIPSIZ	8192
4395dce41c5SJohn Dyson #endif
440f708ef1bSPoul-Henning Kamp static u_long	unpst_sendspace = PIPSIZ;
441f708ef1bSPoul-Henning Kamp static u_long	unpst_recvspace = PIPSIZ;
442f708ef1bSPoul-Henning Kamp static u_long	unpdg_sendspace = 2*1024;	/* really max datagram size */
443f708ef1bSPoul-Henning Kamp static u_long	unpdg_recvspace = 4*1024;
444df8bae1dSRodney W. Grimes 
445f708ef1bSPoul-Henning Kamp static int	unp_rights;			/* file descriptors in flight */
446df8bae1dSRodney W. Grimes 
447ce02431fSDoug Rabson SYSCTL_DECL(_net_local_stream);
448639acc13SGarrett Wollman SYSCTL_INT(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW,
449639acc13SGarrett Wollman 	   &unpst_sendspace, 0, "");
450639acc13SGarrett Wollman SYSCTL_INT(_net_local_stream, OID_AUTO, recvspace, CTLFLAG_RW,
451639acc13SGarrett Wollman 	   &unpst_recvspace, 0, "");
452ce02431fSDoug Rabson SYSCTL_DECL(_net_local_dgram);
453639acc13SGarrett Wollman SYSCTL_INT(_net_local_dgram, OID_AUTO, maxdgram, CTLFLAG_RW,
454639acc13SGarrett Wollman 	   &unpdg_sendspace, 0, "");
455639acc13SGarrett Wollman SYSCTL_INT(_net_local_dgram, OID_AUTO, recvspace, CTLFLAG_RW,
456639acc13SGarrett Wollman 	   &unpdg_recvspace, 0, "");
457ce02431fSDoug Rabson SYSCTL_DECL(_net_local);
458639acc13SGarrett Wollman SYSCTL_INT(_net_local, OID_AUTO, inflight, CTLFLAG_RD, &unp_rights, 0, "");
459639acc13SGarrett Wollman 
460f708ef1bSPoul-Henning Kamp static int
461df8bae1dSRodney W. Grimes unp_attach(so)
462df8bae1dSRodney W. Grimes 	struct socket *so;
463df8bae1dSRodney W. Grimes {
464df8bae1dSRodney W. Grimes 	register struct unpcb *unp;
465df8bae1dSRodney W. Grimes 	int error;
466df8bae1dSRodney W. Grimes 
467df8bae1dSRodney W. Grimes 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
468df8bae1dSRodney W. Grimes 		switch (so->so_type) {
469df8bae1dSRodney W. Grimes 
470df8bae1dSRodney W. Grimes 		case SOCK_STREAM:
471df8bae1dSRodney W. Grimes 			error = soreserve(so, unpst_sendspace, unpst_recvspace);
472df8bae1dSRodney W. Grimes 			break;
473df8bae1dSRodney W. Grimes 
474df8bae1dSRodney W. Grimes 		case SOCK_DGRAM:
475df8bae1dSRodney W. Grimes 			error = soreserve(so, unpdg_sendspace, unpdg_recvspace);
476df8bae1dSRodney W. Grimes 			break;
477df8bae1dSRodney W. Grimes 
478df8bae1dSRodney W. Grimes 		default:
479df8bae1dSRodney W. Grimes 			panic("unp_attach");
480df8bae1dSRodney W. Grimes 		}
481df8bae1dSRodney W. Grimes 		if (error)
482df8bae1dSRodney W. Grimes 			return (error);
483df8bae1dSRodney W. Grimes 	}
48498271db4SGarrett Wollman 	unp = zalloc(unp_zone);
48557bf258eSGarrett Wollman 	if (unp == NULL)
486df8bae1dSRodney W. Grimes 		return (ENOBUFS);
48757bf258eSGarrett Wollman 	bzero(unp, sizeof *unp);
48898271db4SGarrett Wollman 	unp->unp_gencnt = ++unp_gencnt;
48998271db4SGarrett Wollman 	unp_count++;
49098271db4SGarrett Wollman 	LIST_INIT(&unp->unp_refs);
491df8bae1dSRodney W. Grimes 	unp->unp_socket = so;
49275c13541SPoul-Henning Kamp 	unp->unp_rvnode = curproc->p_fd->fd_rdir;
49398271db4SGarrett Wollman 	LIST_INSERT_HEAD(so->so_type == SOCK_DGRAM ? &unp_dhead
49498271db4SGarrett Wollman 			 : &unp_shead, unp, unp_link);
49598271db4SGarrett Wollman 	so->so_pcb = (caddr_t)unp;
496df8bae1dSRodney W. Grimes 	return (0);
497df8bae1dSRodney W. Grimes }
498df8bae1dSRodney W. Grimes 
499f708ef1bSPoul-Henning Kamp static void
500df8bae1dSRodney W. Grimes unp_detach(unp)
501df8bae1dSRodney W. Grimes 	register struct unpcb *unp;
502df8bae1dSRodney W. Grimes {
50398271db4SGarrett Wollman 	LIST_REMOVE(unp, unp_link);
50498271db4SGarrett Wollman 	unp->unp_gencnt = ++unp_gencnt;
50598271db4SGarrett Wollman 	--unp_count;
506df8bae1dSRodney W. Grimes 	if (unp->unp_vnode) {
507df8bae1dSRodney W. Grimes 		unp->unp_vnode->v_socket = 0;
508df8bae1dSRodney W. Grimes 		vrele(unp->unp_vnode);
509df8bae1dSRodney W. Grimes 		unp->unp_vnode = 0;
510df8bae1dSRodney W. Grimes 	}
511df8bae1dSRodney W. Grimes 	if (unp->unp_conn)
512df8bae1dSRodney W. Grimes 		unp_disconnect(unp);
5132e3c8fcbSPoul-Henning Kamp 	while (!LIST_EMPTY(&unp->unp_refs))
5142e3c8fcbSPoul-Henning Kamp 		unp_drop(LIST_FIRST(&unp->unp_refs), ECONNRESET);
515df8bae1dSRodney W. Grimes 	soisdisconnected(unp->unp_socket);
516df8bae1dSRodney W. Grimes 	unp->unp_socket->so_pcb = 0;
517df8bae1dSRodney W. Grimes 	if (unp_rights) {
518df8bae1dSRodney W. Grimes 		/*
519df8bae1dSRodney W. Grimes 		 * Normally the receive buffer is flushed later,
520df8bae1dSRodney W. Grimes 		 * in sofree, but if our receive buffer holds references
521df8bae1dSRodney W. Grimes 		 * to descriptors that are now garbage, we will dispose
522df8bae1dSRodney W. Grimes 		 * of those descriptor references after the garbage collector
523df8bae1dSRodney W. Grimes 		 * gets them (resulting in a "panic: closef: count < 0").
524df8bae1dSRodney W. Grimes 		 */
525df8bae1dSRodney W. Grimes 		sorflush(unp->unp_socket);
526df8bae1dSRodney W. Grimes 		unp_gc();
527df8bae1dSRodney W. Grimes 	}
52857bf258eSGarrett Wollman 	if (unp->unp_addr)
52957bf258eSGarrett Wollman 		FREE(unp->unp_addr, M_SONAME);
53098271db4SGarrett Wollman 	zfree(unp_zone, unp);
531df8bae1dSRodney W. Grimes }
532df8bae1dSRodney W. Grimes 
533f708ef1bSPoul-Henning Kamp static int
534df8bae1dSRodney W. Grimes unp_bind(unp, nam, p)
535df8bae1dSRodney W. Grimes 	struct unpcb *unp;
53657bf258eSGarrett Wollman 	struct sockaddr *nam;
537df8bae1dSRodney W. Grimes 	struct proc *p;
538df8bae1dSRodney W. Grimes {
53957bf258eSGarrett Wollman 	struct sockaddr_un *soun = (struct sockaddr_un *)nam;
540df8bae1dSRodney W. Grimes 	register struct vnode *vp;
541df8bae1dSRodney W. Grimes 	struct vattr vattr;
54257bf258eSGarrett Wollman 	int error, namelen;
543df8bae1dSRodney W. Grimes 	struct nameidata nd;
54457bf258eSGarrett Wollman 	char buf[SOCK_MAXADDRLEN];
545df8bae1dSRodney W. Grimes 
546df8bae1dSRodney W. Grimes 	if (unp->unp_vnode != NULL)
547df8bae1dSRodney W. Grimes 		return (EINVAL);
54857bf258eSGarrett Wollman #define offsetof(s, e) ((char *)&((s *)0)->e - (char *)((s *)0))
54957bf258eSGarrett Wollman 	namelen = soun->sun_len - offsetof(struct sockaddr_un, sun_path);
55057bf258eSGarrett Wollman 	if (namelen <= 0)
55157bf258eSGarrett Wollman 		return EINVAL;
55257bf258eSGarrett Wollman 	strncpy(buf, soun->sun_path, namelen);
55357bf258eSGarrett Wollman 	buf[namelen] = 0;	/* null-terminate the string */
554974784e8SGuido van Rooij 	NDINIT(&nd, CREATE, NOFOLLOW | LOCKPARENT, UIO_SYSSPACE,
55557bf258eSGarrett Wollman 	    buf, p);
556df8bae1dSRodney W. Grimes /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */
557797f2d22SPoul-Henning Kamp 	error = namei(&nd);
558797f2d22SPoul-Henning Kamp 	if (error)
559df8bae1dSRodney W. Grimes 		return (error);
560df8bae1dSRodney W. Grimes 	vp = nd.ni_vp;
561df8bae1dSRodney W. Grimes 	if (vp != NULL) {
562762e6b85SEivind Eklund 		NDFREE(&nd, NDF_ONLY_PNBUF);
563df8bae1dSRodney W. Grimes 		if (nd.ni_dvp == vp)
564df8bae1dSRodney W. Grimes 			vrele(nd.ni_dvp);
565df8bae1dSRodney W. Grimes 		else
566df8bae1dSRodney W. Grimes 			vput(nd.ni_dvp);
567df8bae1dSRodney W. Grimes 		vrele(vp);
568df8bae1dSRodney W. Grimes 		return (EADDRINUSE);
569df8bae1dSRodney W. Grimes 	}
570df8bae1dSRodney W. Grimes 	VATTR_NULL(&vattr);
571df8bae1dSRodney W. Grimes 	vattr.va_type = VSOCK;
572a29f300eSGarrett Wollman 	vattr.va_mode = (ACCESSPERMS & ~p->p_fd->fd_cmask);
573996c772fSJohn Dyson 	VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
5747be2d300SMike Smith 	error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
575762e6b85SEivind Eklund 	NDFREE(&nd, NDF_ONLY_PNBUF);
5767be2d300SMike Smith 	vput(nd.ni_dvp);
5777be2d300SMike Smith 	if (error)
578df8bae1dSRodney W. Grimes 		return (error);
579df8bae1dSRodney W. Grimes 	vp = nd.ni_vp;
580df8bae1dSRodney W. Grimes 	vp->v_socket = unp->unp_socket;
581df8bae1dSRodney W. Grimes 	unp->unp_vnode = vp;
58257bf258eSGarrett Wollman 	unp->unp_addr = (struct sockaddr_un *)dup_sockaddr(nam, 1);
583996c772fSJohn Dyson 	VOP_UNLOCK(vp, 0, p);
584df8bae1dSRodney W. Grimes 	return (0);
585df8bae1dSRodney W. Grimes }
586df8bae1dSRodney W. Grimes 
587f708ef1bSPoul-Henning Kamp static int
588df8bae1dSRodney W. Grimes unp_connect(so, nam, p)
589df8bae1dSRodney W. Grimes 	struct socket *so;
59057bf258eSGarrett Wollman 	struct sockaddr *nam;
591df8bae1dSRodney W. Grimes 	struct proc *p;
592df8bae1dSRodney W. Grimes {
59357bf258eSGarrett Wollman 	register struct sockaddr_un *soun = (struct sockaddr_un *)nam;
594df8bae1dSRodney W. Grimes 	register struct vnode *vp;
595df8bae1dSRodney W. Grimes 	register struct socket *so2, *so3;
596df8bae1dSRodney W. Grimes 	struct unpcb *unp2, *unp3;
59757bf258eSGarrett Wollman 	int error, len;
598df8bae1dSRodney W. Grimes 	struct nameidata nd;
59957bf258eSGarrett Wollman 	char buf[SOCK_MAXADDRLEN];
600df8bae1dSRodney W. Grimes 
60157bf258eSGarrett Wollman 	len = nam->sa_len - offsetof(struct sockaddr_un, sun_path);
60257bf258eSGarrett Wollman 	if (len <= 0)
60357bf258eSGarrett Wollman 		return EINVAL;
60457bf258eSGarrett Wollman 	strncpy(buf, soun->sun_path, len);
60557bf258eSGarrett Wollman 	buf[len] = 0;
60657bf258eSGarrett Wollman 
60757bf258eSGarrett Wollman 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, buf, p);
608797f2d22SPoul-Henning Kamp 	error = namei(&nd);
609797f2d22SPoul-Henning Kamp 	if (error)
610df8bae1dSRodney W. Grimes 		return (error);
611df8bae1dSRodney W. Grimes 	vp = nd.ni_vp;
612762e6b85SEivind Eklund 	NDFREE(&nd, NDF_ONLY_PNBUF);
613df8bae1dSRodney W. Grimes 	if (vp->v_type != VSOCK) {
614df8bae1dSRodney W. Grimes 		error = ENOTSOCK;
615df8bae1dSRodney W. Grimes 		goto bad;
616df8bae1dSRodney W. Grimes 	}
617797f2d22SPoul-Henning Kamp 	error = VOP_ACCESS(vp, VWRITE, p->p_ucred, p);
618797f2d22SPoul-Henning Kamp 	if (error)
619df8bae1dSRodney W. Grimes 		goto bad;
620df8bae1dSRodney W. Grimes 	so2 = vp->v_socket;
621df8bae1dSRodney W. Grimes 	if (so2 == 0) {
622df8bae1dSRodney W. Grimes 		error = ECONNREFUSED;
623df8bae1dSRodney W. Grimes 		goto bad;
624df8bae1dSRodney W. Grimes 	}
625df8bae1dSRodney W. Grimes 	if (so->so_type != so2->so_type) {
626df8bae1dSRodney W. Grimes 		error = EPROTOTYPE;
627df8bae1dSRodney W. Grimes 		goto bad;
628df8bae1dSRodney W. Grimes 	}
629df8bae1dSRodney W. Grimes 	if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
630df8bae1dSRodney W. Grimes 		if ((so2->so_options & SO_ACCEPTCONN) == 0 ||
6312f9a2132SBrian Feldman 		    (so3 = sonewconn3(so2, 0, p)) == 0) {
632df8bae1dSRodney W. Grimes 			error = ECONNREFUSED;
633df8bae1dSRodney W. Grimes 			goto bad;
634df8bae1dSRodney W. Grimes 		}
635df8bae1dSRodney W. Grimes 		unp2 = sotounpcb(so2);
636df8bae1dSRodney W. Grimes 		unp3 = sotounpcb(so3);
637df8bae1dSRodney W. Grimes 		if (unp2->unp_addr)
63857bf258eSGarrett Wollman 			unp3->unp_addr = (struct sockaddr_un *)
63957bf258eSGarrett Wollman 				dup_sockaddr((struct sockaddr *)
64057bf258eSGarrett Wollman 					     unp2->unp_addr, 1);
641df8bae1dSRodney W. Grimes 		so2 = so3;
642df8bae1dSRodney W. Grimes 	}
643df8bae1dSRodney W. Grimes 	error = unp_connect2(so, so2);
644df8bae1dSRodney W. Grimes bad:
645df8bae1dSRodney W. Grimes 	vput(vp);
646df8bae1dSRodney W. Grimes 	return (error);
647df8bae1dSRodney W. Grimes }
648df8bae1dSRodney W. Grimes 
64926f9a767SRodney W. Grimes int
650df8bae1dSRodney W. Grimes unp_connect2(so, so2)
651df8bae1dSRodney W. Grimes 	register struct socket *so;
652df8bae1dSRodney W. Grimes 	register struct socket *so2;
653df8bae1dSRodney W. Grimes {
654df8bae1dSRodney W. Grimes 	register struct unpcb *unp = sotounpcb(so);
655df8bae1dSRodney W. Grimes 	register struct unpcb *unp2;
656df8bae1dSRodney W. Grimes 
657df8bae1dSRodney W. Grimes 	if (so2->so_type != so->so_type)
658df8bae1dSRodney W. Grimes 		return (EPROTOTYPE);
659df8bae1dSRodney W. Grimes 	unp2 = sotounpcb(so2);
660df8bae1dSRodney W. Grimes 	unp->unp_conn = unp2;
661df8bae1dSRodney W. Grimes 	switch (so->so_type) {
662df8bae1dSRodney W. Grimes 
663df8bae1dSRodney W. Grimes 	case SOCK_DGRAM:
66498271db4SGarrett Wollman 		LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink);
665df8bae1dSRodney W. Grimes 		soisconnected(so);
666df8bae1dSRodney W. Grimes 		break;
667df8bae1dSRodney W. Grimes 
668df8bae1dSRodney W. Grimes 	case SOCK_STREAM:
669df8bae1dSRodney W. Grimes 		unp2->unp_conn = unp;
670df8bae1dSRodney W. Grimes 		soisconnected(so);
671df8bae1dSRodney W. Grimes 		soisconnected(so2);
672df8bae1dSRodney W. Grimes 		break;
673df8bae1dSRodney W. Grimes 
674df8bae1dSRodney W. Grimes 	default:
675df8bae1dSRodney W. Grimes 		panic("unp_connect2");
676df8bae1dSRodney W. Grimes 	}
677df8bae1dSRodney W. Grimes 	return (0);
678df8bae1dSRodney W. Grimes }
679df8bae1dSRodney W. Grimes 
680f708ef1bSPoul-Henning Kamp static void
681df8bae1dSRodney W. Grimes unp_disconnect(unp)
682df8bae1dSRodney W. Grimes 	struct unpcb *unp;
683df8bae1dSRodney W. Grimes {
684df8bae1dSRodney W. Grimes 	register struct unpcb *unp2 = unp->unp_conn;
685df8bae1dSRodney W. Grimes 
686df8bae1dSRodney W. Grimes 	if (unp2 == 0)
687df8bae1dSRodney W. Grimes 		return;
688df8bae1dSRodney W. Grimes 	unp->unp_conn = 0;
689df8bae1dSRodney W. Grimes 	switch (unp->unp_socket->so_type) {
690df8bae1dSRodney W. Grimes 
691df8bae1dSRodney W. Grimes 	case SOCK_DGRAM:
69298271db4SGarrett Wollman 		LIST_REMOVE(unp, unp_reflink);
693df8bae1dSRodney W. Grimes 		unp->unp_socket->so_state &= ~SS_ISCONNECTED;
694df8bae1dSRodney W. Grimes 		break;
695df8bae1dSRodney W. Grimes 
696df8bae1dSRodney W. Grimes 	case SOCK_STREAM:
697df8bae1dSRodney W. Grimes 		soisdisconnected(unp->unp_socket);
698df8bae1dSRodney W. Grimes 		unp2->unp_conn = 0;
699df8bae1dSRodney W. Grimes 		soisdisconnected(unp2->unp_socket);
700df8bae1dSRodney W. Grimes 		break;
701df8bae1dSRodney W. Grimes 	}
702df8bae1dSRodney W. Grimes }
703df8bae1dSRodney W. Grimes 
704df8bae1dSRodney W. Grimes #ifdef notdef
70526f9a767SRodney W. Grimes void
706df8bae1dSRodney W. Grimes unp_abort(unp)
707df8bae1dSRodney W. Grimes 	struct unpcb *unp;
708df8bae1dSRodney W. Grimes {
709df8bae1dSRodney W. Grimes 
710df8bae1dSRodney W. Grimes 	unp_detach(unp);
711df8bae1dSRodney W. Grimes }
712df8bae1dSRodney W. Grimes #endif
713df8bae1dSRodney W. Grimes 
71498271db4SGarrett Wollman static int
71575c13541SPoul-Henning Kamp prison_unpcb(struct proc *p, struct unpcb *unp)
71675c13541SPoul-Henning Kamp {
71775c13541SPoul-Henning Kamp 	if (!p->p_prison)
71875c13541SPoul-Henning Kamp 		return (0);
71975c13541SPoul-Henning Kamp 	if (p->p_fd->fd_rdir == unp->unp_rvnode)
72075c13541SPoul-Henning Kamp 		return (0);
72175c13541SPoul-Henning Kamp 	return (1);
72275c13541SPoul-Henning Kamp }
72375c13541SPoul-Henning Kamp 
72475c13541SPoul-Henning Kamp static int
72598271db4SGarrett Wollman unp_pcblist SYSCTL_HANDLER_ARGS
72698271db4SGarrett Wollman {
727f5ef029eSPoul-Henning Kamp 	int error, i, n;
72898271db4SGarrett Wollman 	struct unpcb *unp, **unp_list;
72998271db4SGarrett Wollman 	unp_gen_t gencnt;
73098271db4SGarrett Wollman 	struct xunpgen xug;
73198271db4SGarrett Wollman 	struct unp_head *head;
73298271db4SGarrett Wollman 
733a23d65bfSBruce Evans 	head = ((intptr_t)arg1 == SOCK_DGRAM ? &unp_dhead : &unp_shead);
73498271db4SGarrett Wollman 
73598271db4SGarrett Wollman 	/*
73698271db4SGarrett Wollman 	 * The process of preparing the PCB list is too time-consuming and
73798271db4SGarrett Wollman 	 * resource-intensive to repeat twice on every request.
73898271db4SGarrett Wollman 	 */
73998271db4SGarrett Wollman 	if (req->oldptr == 0) {
74098271db4SGarrett Wollman 		n = unp_count;
74198271db4SGarrett Wollman 		req->oldidx = 2 * (sizeof xug)
74298271db4SGarrett Wollman 			+ (n + n/8) * sizeof(struct xunpcb);
74398271db4SGarrett Wollman 		return 0;
74498271db4SGarrett Wollman 	}
74598271db4SGarrett Wollman 
74698271db4SGarrett Wollman 	if (req->newptr != 0)
74798271db4SGarrett Wollman 		return EPERM;
74898271db4SGarrett Wollman 
74998271db4SGarrett Wollman 	/*
75098271db4SGarrett Wollman 	 * OK, now we're committed to doing something.
75198271db4SGarrett Wollman 	 */
75298271db4SGarrett Wollman 	gencnt = unp_gencnt;
75398271db4SGarrett Wollman 	n = unp_count;
75498271db4SGarrett Wollman 
75598271db4SGarrett Wollman 	xug.xug_len = sizeof xug;
75698271db4SGarrett Wollman 	xug.xug_count = n;
75798271db4SGarrett Wollman 	xug.xug_gen = gencnt;
75898271db4SGarrett Wollman 	xug.xug_sogen = so_gencnt;
75998271db4SGarrett Wollman 	error = SYSCTL_OUT(req, &xug, sizeof xug);
76098271db4SGarrett Wollman 	if (error)
76198271db4SGarrett Wollman 		return error;
76298271db4SGarrett Wollman 
76398271db4SGarrett Wollman 	unp_list = malloc(n * sizeof *unp_list, M_TEMP, M_WAITOK);
76498271db4SGarrett Wollman 	if (unp_list == 0)
76598271db4SGarrett Wollman 		return ENOMEM;
76698271db4SGarrett Wollman 
7672e3c8fcbSPoul-Henning Kamp 	for (unp = LIST_FIRST(head), i = 0; unp && i < n;
7682e3c8fcbSPoul-Henning Kamp 	     unp = LIST_NEXT(unp, unp_link)) {
76975c13541SPoul-Henning Kamp 		if (unp->unp_gencnt <= gencnt && !prison_unpcb(req->p, unp))
77098271db4SGarrett Wollman 			unp_list[i++] = unp;
77198271db4SGarrett Wollman 	}
77298271db4SGarrett Wollman 	n = i;			/* in case we lost some during malloc */
77398271db4SGarrett Wollman 
77498271db4SGarrett Wollman 	error = 0;
77598271db4SGarrett Wollman 	for (i = 0; i < n; i++) {
77698271db4SGarrett Wollman 		unp = unp_list[i];
77798271db4SGarrett Wollman 		if (unp->unp_gencnt <= gencnt) {
77898271db4SGarrett Wollman 			struct xunpcb xu;
77998271db4SGarrett Wollman 			xu.xu_len = sizeof xu;
78098271db4SGarrett Wollman 			xu.xu_unpp = unp;
78198271db4SGarrett Wollman 			/*
78298271db4SGarrett Wollman 			 * XXX - need more locking here to protect against
78398271db4SGarrett Wollman 			 * connect/disconnect races for SMP.
78498271db4SGarrett Wollman 			 */
78598271db4SGarrett Wollman 			if (unp->unp_addr)
78698271db4SGarrett Wollman 				bcopy(unp->unp_addr, &xu.xu_addr,
78798271db4SGarrett Wollman 				      unp->unp_addr->sun_len);
78898271db4SGarrett Wollman 			if (unp->unp_conn && unp->unp_conn->unp_addr)
78998271db4SGarrett Wollman 				bcopy(unp->unp_conn->unp_addr,
79098271db4SGarrett Wollman 				      &xu.xu_caddr,
79198271db4SGarrett Wollman 				      unp->unp_conn->unp_addr->sun_len);
79298271db4SGarrett Wollman 			bcopy(unp, &xu.xu_unp, sizeof *unp);
79398271db4SGarrett Wollman 			sotoxsocket(unp->unp_socket, &xu.xu_socket);
79498271db4SGarrett Wollman 			error = SYSCTL_OUT(req, &xu, sizeof xu);
79598271db4SGarrett Wollman 		}
79698271db4SGarrett Wollman 	}
79798271db4SGarrett Wollman 	if (!error) {
79898271db4SGarrett Wollman 		/*
79998271db4SGarrett Wollman 		 * Give the user an updated idea of our state.
80098271db4SGarrett Wollman 		 * If the generation differs from what we told
80198271db4SGarrett Wollman 		 * her before, she knows that something happened
80298271db4SGarrett Wollman 		 * while we were processing this request, and it
80398271db4SGarrett Wollman 		 * might be necessary to retry.
80498271db4SGarrett Wollman 		 */
80598271db4SGarrett Wollman 		xug.xug_gen = unp_gencnt;
80698271db4SGarrett Wollman 		xug.xug_sogen = so_gencnt;
80798271db4SGarrett Wollman 		xug.xug_count = unp_count;
80898271db4SGarrett Wollman 		error = SYSCTL_OUT(req, &xug, sizeof xug);
80998271db4SGarrett Wollman 	}
81098271db4SGarrett Wollman 	free(unp_list, M_TEMP);
81198271db4SGarrett Wollman 	return error;
81298271db4SGarrett Wollman }
81398271db4SGarrett Wollman 
81498271db4SGarrett Wollman SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLFLAG_RD,
81598271db4SGarrett Wollman 	    (caddr_t)(long)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb",
81698271db4SGarrett Wollman 	    "List of active local datagram sockets");
81798271db4SGarrett Wollman SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, CTLFLAG_RD,
81898271db4SGarrett Wollman 	    (caddr_t)(long)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb",
81998271db4SGarrett Wollman 	    "List of active local stream sockets");
82098271db4SGarrett Wollman 
821f708ef1bSPoul-Henning Kamp static void
822df8bae1dSRodney W. Grimes unp_shutdown(unp)
823df8bae1dSRodney W. Grimes 	struct unpcb *unp;
824df8bae1dSRodney W. Grimes {
825df8bae1dSRodney W. Grimes 	struct socket *so;
826df8bae1dSRodney W. Grimes 
827df8bae1dSRodney W. Grimes 	if (unp->unp_socket->so_type == SOCK_STREAM && unp->unp_conn &&
828df8bae1dSRodney W. Grimes 	    (so = unp->unp_conn->unp_socket))
829df8bae1dSRodney W. Grimes 		socantrcvmore(so);
830df8bae1dSRodney W. Grimes }
831df8bae1dSRodney W. Grimes 
832f708ef1bSPoul-Henning Kamp static void
833df8bae1dSRodney W. Grimes unp_drop(unp, errno)
834df8bae1dSRodney W. Grimes 	struct unpcb *unp;
835df8bae1dSRodney W. Grimes 	int errno;
836df8bae1dSRodney W. Grimes {
837df8bae1dSRodney W. Grimes 	struct socket *so = unp->unp_socket;
838df8bae1dSRodney W. Grimes 
839df8bae1dSRodney W. Grimes 	so->so_error = errno;
840df8bae1dSRodney W. Grimes 	unp_disconnect(unp);
841df8bae1dSRodney W. Grimes 	if (so->so_head) {
84298271db4SGarrett Wollman 		LIST_REMOVE(unp, unp_link);
84398271db4SGarrett Wollman 		unp->unp_gencnt = ++unp_gencnt;
84498271db4SGarrett Wollman 		unp_count--;
845df8bae1dSRodney W. Grimes 		so->so_pcb = (caddr_t) 0;
84657bf258eSGarrett Wollman 		if (unp->unp_addr)
84757bf258eSGarrett Wollman 			FREE(unp->unp_addr, M_SONAME);
84898271db4SGarrett Wollman 		zfree(unp_zone, unp);
849df8bae1dSRodney W. Grimes 		sofree(so);
850df8bae1dSRodney W. Grimes 	}
851df8bae1dSRodney W. Grimes }
852df8bae1dSRodney W. Grimes 
853df8bae1dSRodney W. Grimes #ifdef notdef
85426f9a767SRodney W. Grimes void
855df8bae1dSRodney W. Grimes unp_drain()
856df8bae1dSRodney W. Grimes {
857df8bae1dSRodney W. Grimes 
858df8bae1dSRodney W. Grimes }
859df8bae1dSRodney W. Grimes #endif
860df8bae1dSRodney W. Grimes 
86126f9a767SRodney W. Grimes int
862df8bae1dSRodney W. Grimes unp_externalize(rights)
863df8bae1dSRodney W. Grimes 	struct mbuf *rights;
864df8bae1dSRodney W. Grimes {
865df8bae1dSRodney W. Grimes 	struct proc *p = curproc;		/* XXX */
866df8bae1dSRodney W. Grimes 	register int i;
867df8bae1dSRodney W. Grimes 	register struct cmsghdr *cm = mtod(rights, struct cmsghdr *);
8688692c025SYoshinobu Inoue 	register int *fdp;
8698692c025SYoshinobu Inoue 	register struct file **rp;
870df8bae1dSRodney W. Grimes 	register struct file *fp;
8718692c025SYoshinobu Inoue 	int newfds = (cm->cmsg_len - (CMSG_DATA(cm) - (u_char *)cm))
8728692c025SYoshinobu Inoue 		/ sizeof (struct file *);
873df8bae1dSRodney W. Grimes 	int f;
874df8bae1dSRodney W. Grimes 
875ed5b7817SJulian Elischer 	/*
876ed5b7817SJulian Elischer 	 * if the new FD's will not fit, then we free them all
877ed5b7817SJulian Elischer 	 */
878df8bae1dSRodney W. Grimes 	if (!fdavail(p, newfds)) {
8798692c025SYoshinobu Inoue 		rp = (struct file **)CMSG_DATA(cm);
880df8bae1dSRodney W. Grimes 		for (i = 0; i < newfds; i++) {
881df8bae1dSRodney W. Grimes 			fp = *rp;
8828692c025SYoshinobu Inoue 			/*
8838692c025SYoshinobu Inoue 			 * zero the pointer before calling unp_discard,
8848692c025SYoshinobu Inoue 			 * since it may end up in unp_gc()..
8858692c025SYoshinobu Inoue 			 */
886df8bae1dSRodney W. Grimes 			*rp++ = 0;
8878692c025SYoshinobu Inoue 			unp_discard(fp);
888df8bae1dSRodney W. Grimes 		}
889df8bae1dSRodney W. Grimes 		return (EMSGSIZE);
890df8bae1dSRodney W. Grimes 	}
891ed5b7817SJulian Elischer 	/*
892ed5b7817SJulian Elischer 	 * now change each pointer to an fd in the global table to
893ed5b7817SJulian Elischer 	 * an integer that is the index to the local fd table entry
894ed5b7817SJulian Elischer 	 * that we set up to point to the global one we are transferring.
8958692c025SYoshinobu Inoue 	 * If sizeof (struct file *) is bigger than or equal to sizeof int,
8968692c025SYoshinobu Inoue 	 * then do it in forward order. In that case, an integer will
8978692c025SYoshinobu Inoue 	 * always come in the same place or before its corresponding
8988692c025SYoshinobu Inoue 	 * struct file pointer.
8998692c025SYoshinobu Inoue 	 * If sizeof (struct file *) is smaller than sizeof int, then
9008692c025SYoshinobu Inoue 	 * do it in reverse order.
901ed5b7817SJulian Elischer 	 */
9028692c025SYoshinobu Inoue 	if (sizeof (struct file *) >= sizeof (int)) {
9038692c025SYoshinobu Inoue 		fdp = (int *)(cm + 1);
9048692c025SYoshinobu Inoue 		rp = (struct file **)CMSG_DATA(cm);
905df8bae1dSRodney W. Grimes 		for (i = 0; i < newfds; i++) {
906df8bae1dSRodney W. Grimes 			if (fdalloc(p, 0, &f))
907df8bae1dSRodney W. Grimes 				panic("unp_externalize");
9088692c025SYoshinobu Inoue 			fp = *rp++;
909df8bae1dSRodney W. Grimes 			p->p_fd->fd_ofiles[f] = fp;
910df8bae1dSRodney W. Grimes 			fp->f_msgcount--;
911df8bae1dSRodney W. Grimes 			unp_rights--;
9128692c025SYoshinobu Inoue 			*fdp++ = f;
913df8bae1dSRodney W. Grimes 		}
9148692c025SYoshinobu Inoue 	} else {
9158692c025SYoshinobu Inoue 		fdp = (int *)(cm + 1) + newfds - 1;
9168692c025SYoshinobu Inoue 		rp = (struct file **)CMSG_DATA(cm) + newfds - 1;
9178692c025SYoshinobu Inoue 		for (i = 0; i < newfds; i++) {
9188692c025SYoshinobu Inoue 			if (fdalloc(p, 0, &f))
9198692c025SYoshinobu Inoue 				panic("unp_externalize");
9208692c025SYoshinobu Inoue 			fp = *rp--;
9218692c025SYoshinobu Inoue 			p->p_fd->fd_ofiles[f] = fp;
9228692c025SYoshinobu Inoue 			fp->f_msgcount--;
9238692c025SYoshinobu Inoue 			unp_rights--;
9248692c025SYoshinobu Inoue 			*fdp-- = f;
9258692c025SYoshinobu Inoue 		}
9268692c025SYoshinobu Inoue 	}
9278692c025SYoshinobu Inoue 
9288692c025SYoshinobu Inoue 	/*
9298692c025SYoshinobu Inoue 	 * Adjust length, in case sizeof(struct file *) and sizeof(int)
9308692c025SYoshinobu Inoue 	 * differs.
9318692c025SYoshinobu Inoue 	 */
9328692c025SYoshinobu Inoue 	cm->cmsg_len = CMSG_LEN(newfds * sizeof(int));
9338692c025SYoshinobu Inoue 	rights->m_len = cm->cmsg_len;
934df8bae1dSRodney W. Grimes 	return (0);
935df8bae1dSRodney W. Grimes }
936df8bae1dSRodney W. Grimes 
93798271db4SGarrett Wollman void
93898271db4SGarrett Wollman unp_init(void)
93998271db4SGarrett Wollman {
94098271db4SGarrett Wollman 	unp_zone = zinit("unpcb", sizeof(struct unpcb), nmbclusters, 0, 0);
94198271db4SGarrett Wollman 	if (unp_zone == 0)
94298271db4SGarrett Wollman 		panic("unp_init");
94398271db4SGarrett Wollman 	LIST_INIT(&unp_dhead);
94498271db4SGarrett Wollman 	LIST_INIT(&unp_shead);
94598271db4SGarrett Wollman }
94698271db4SGarrett Wollman 
9470b788fa1SBill Paul #ifndef MIN
9480b788fa1SBill Paul #define	MIN(a,b) (((a)<(b))?(a):(b))
9490b788fa1SBill Paul #endif
9500b788fa1SBill Paul 
951f708ef1bSPoul-Henning Kamp static int
952df8bae1dSRodney W. Grimes unp_internalize(control, p)
953df8bae1dSRodney W. Grimes 	struct mbuf *control;
954df8bae1dSRodney W. Grimes 	struct proc *p;
955df8bae1dSRodney W. Grimes {
9568692c025SYoshinobu Inoue 	struct filedesc *fdescp = p->p_fd;
957df8bae1dSRodney W. Grimes 	register struct cmsghdr *cm = mtod(control, struct cmsghdr *);
958df8bae1dSRodney W. Grimes 	register struct file **rp;
959df8bae1dSRodney W. Grimes 	register struct file *fp;
9608692c025SYoshinobu Inoue 	register int i, fd, *fdp;
9610b788fa1SBill Paul 	register struct cmsgcred *cmcred;
962df8bae1dSRodney W. Grimes 	int oldfds;
9638692c025SYoshinobu Inoue 	u_int newlen;
964df8bae1dSRodney W. Grimes 
9650b788fa1SBill Paul 	if ((cm->cmsg_type != SCM_RIGHTS && cm->cmsg_type != SCM_CREDS) ||
9660b788fa1SBill Paul 	    cm->cmsg_level != SOL_SOCKET || cm->cmsg_len != control->m_len)
967df8bae1dSRodney W. Grimes 		return (EINVAL);
9680b788fa1SBill Paul 
9690b788fa1SBill Paul 	/*
9700b788fa1SBill Paul 	 * Fill in credential information.
9710b788fa1SBill Paul 	 */
9720b788fa1SBill Paul 	if (cm->cmsg_type == SCM_CREDS) {
9730b788fa1SBill Paul 		cmcred = (struct cmsgcred *)(cm + 1);
9740b788fa1SBill Paul 		cmcred->cmcred_pid = p->p_pid;
9750b788fa1SBill Paul 		cmcred->cmcred_uid = p->p_cred->p_ruid;
9760b788fa1SBill Paul 		cmcred->cmcred_gid = p->p_cred->p_rgid;
9770b788fa1SBill Paul 		cmcred->cmcred_euid = p->p_ucred->cr_uid;
9780b788fa1SBill Paul 		cmcred->cmcred_ngroups = MIN(p->p_ucred->cr_ngroups,
9790b788fa1SBill Paul 							CMGROUP_MAX);
9800b788fa1SBill Paul 		for (i = 0; i < cmcred->cmcred_ngroups; i++)
9810b788fa1SBill Paul 			cmcred->cmcred_groups[i] = p->p_ucred->cr_groups[i];
9820b788fa1SBill Paul 		return(0);
9830b788fa1SBill Paul 	}
9840b788fa1SBill Paul 
985df8bae1dSRodney W. Grimes 	oldfds = (cm->cmsg_len - sizeof (*cm)) / sizeof (int);
986ed5b7817SJulian Elischer 	/*
987ed5b7817SJulian Elischer 	 * check that all the FDs passed in refer to legal OPEN files
988ed5b7817SJulian Elischer 	 * If not, reject the entire operation.
989ed5b7817SJulian Elischer 	 */
9908692c025SYoshinobu Inoue 	fdp = (int *)(cm + 1);
991df8bae1dSRodney W. Grimes 	for (i = 0; i < oldfds; i++) {
9928692c025SYoshinobu Inoue 		fd = *fdp++;
9938692c025SYoshinobu Inoue 		if ((unsigned)fd >= fdescp->fd_nfiles ||
9948692c025SYoshinobu Inoue 		    fdescp->fd_ofiles[fd] == NULL)
995df8bae1dSRodney W. Grimes 			return (EBADF);
996df8bae1dSRodney W. Grimes 	}
997ed5b7817SJulian Elischer 	/*
998ed5b7817SJulian Elischer 	 * Now replace the integer FDs with pointers to
999ed5b7817SJulian Elischer 	 * the associated global file table entry..
10008692c025SYoshinobu Inoue 	 * Allocate a bigger buffer as necessary. But if an cluster is not
10018692c025SYoshinobu Inoue 	 * enough, return E2BIG.
1002ed5b7817SJulian Elischer 	 */
10038692c025SYoshinobu Inoue 	newlen = CMSG_LEN(oldfds * sizeof(struct file *));
10048692c025SYoshinobu Inoue 	if (newlen > MCLBYTES)
10058692c025SYoshinobu Inoue 		return (E2BIG);
10068692c025SYoshinobu Inoue 	if (newlen - control->m_len > M_TRAILINGSPACE(control)) {
10078692c025SYoshinobu Inoue 		if (control->m_flags & M_EXT)
10088692c025SYoshinobu Inoue 			return (E2BIG);
10098692c025SYoshinobu Inoue 		MCLGET(control, M_WAIT);
10108692c025SYoshinobu Inoue 		if ((control->m_flags & M_EXT) == 0)
10118692c025SYoshinobu Inoue 			return (ENOBUFS);
10128692c025SYoshinobu Inoue 
10138692c025SYoshinobu Inoue 		/* copy the data to the cluster */
10148692c025SYoshinobu Inoue 		memcpy(mtod(control, char *), cm, cm->cmsg_len);
10158692c025SYoshinobu Inoue 		cm = mtod(control, struct cmsghdr *);
10168692c025SYoshinobu Inoue 	}
10178692c025SYoshinobu Inoue 
10188692c025SYoshinobu Inoue 	/*
10198692c025SYoshinobu Inoue 	 * Adjust length, in case sizeof(struct file *) and sizeof(int)
10208692c025SYoshinobu Inoue 	 * differs.
10218692c025SYoshinobu Inoue 	 */
10228692c025SYoshinobu Inoue 	control->m_len = cm->cmsg_len = newlen;
10238692c025SYoshinobu Inoue 
10248692c025SYoshinobu Inoue 	/*
10258692c025SYoshinobu Inoue 	 * Transform the file descriptors into struct file pointers.
10268692c025SYoshinobu Inoue 	 * If sizeof (struct file *) is bigger than or equal to sizeof int,
10278692c025SYoshinobu Inoue 	 * then do it in reverse order so that the int won't get until
10288692c025SYoshinobu Inoue 	 * we're done.
10298692c025SYoshinobu Inoue 	 * If sizeof (struct file *) is smaller than sizeof int, then
10308692c025SYoshinobu Inoue 	 * do it in forward order.
10318692c025SYoshinobu Inoue 	 */
10328692c025SYoshinobu Inoue 	if (sizeof (struct file *) >= sizeof (int)) {
10338692c025SYoshinobu Inoue 		fdp = (int *)(cm + 1) + oldfds - 1;
10348692c025SYoshinobu Inoue 		rp = (struct file **)CMSG_DATA(cm) + oldfds - 1;
1035df8bae1dSRodney W. Grimes 		for (i = 0; i < oldfds; i++) {
10368692c025SYoshinobu Inoue 			fp = fdescp->fd_ofiles[*fdp--];
10378692c025SYoshinobu Inoue 			*rp-- = fp;
10388692c025SYoshinobu Inoue 			fp->f_count++;
10398692c025SYoshinobu Inoue 			fp->f_msgcount++;
10408692c025SYoshinobu Inoue 			unp_rights++;
10418692c025SYoshinobu Inoue 		}
10428692c025SYoshinobu Inoue 	} else {
10438692c025SYoshinobu Inoue 		fdp = (int *)(cm + 1);
10448692c025SYoshinobu Inoue 		rp = (struct file **)CMSG_DATA(cm);
10458692c025SYoshinobu Inoue 		for (i = 0; i < oldfds; i++) {
10468692c025SYoshinobu Inoue 			fp = fdescp->fd_ofiles[*fdp++];
1047df8bae1dSRodney W. Grimes 			*rp++ = fp;
1048df8bae1dSRodney W. Grimes 			fp->f_count++;
1049df8bae1dSRodney W. Grimes 			fp->f_msgcount++;
1050df8bae1dSRodney W. Grimes 			unp_rights++;
1051df8bae1dSRodney W. Grimes 		}
10528692c025SYoshinobu Inoue 	}
1053df8bae1dSRodney W. Grimes 	return (0);
1054df8bae1dSRodney W. Grimes }
1055df8bae1dSRodney W. Grimes 
1056f708ef1bSPoul-Henning Kamp static int	unp_defer, unp_gcing;
1057df8bae1dSRodney W. Grimes 
1058f708ef1bSPoul-Henning Kamp static void
1059df8bae1dSRodney W. Grimes unp_gc()
1060df8bae1dSRodney W. Grimes {
1061df8bae1dSRodney W. Grimes 	register struct file *fp, *nextfp;
1062df8bae1dSRodney W. Grimes 	register struct socket *so;
1063df8bae1dSRodney W. Grimes 	struct file **extra_ref, **fpp;
1064df8bae1dSRodney W. Grimes 	int nunref, i;
1065df8bae1dSRodney W. Grimes 
1066df8bae1dSRodney W. Grimes 	if (unp_gcing)
1067df8bae1dSRodney W. Grimes 		return;
1068df8bae1dSRodney W. Grimes 	unp_gcing = 1;
1069df8bae1dSRodney W. Grimes 	unp_defer = 0;
1070ed5b7817SJulian Elischer 	/*
1071ed5b7817SJulian Elischer 	 * before going through all this, set all FDs to
1072ed5b7817SJulian Elischer 	 * be NOT defered and NOT externally accessible
1073ed5b7817SJulian Elischer 	 */
10742e3c8fcbSPoul-Henning Kamp 	LIST_FOREACH(fp, &filehead, f_list)
1075df8bae1dSRodney W. Grimes 		fp->f_flag &= ~(FMARK|FDEFER);
1076df8bae1dSRodney W. Grimes 	do {
10772e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(fp, &filehead, f_list) {
1078ed5b7817SJulian Elischer 			/*
1079ed5b7817SJulian Elischer 			 * If the file is not open, skip it
1080ed5b7817SJulian Elischer 			 */
1081df8bae1dSRodney W. Grimes 			if (fp->f_count == 0)
1082df8bae1dSRodney W. Grimes 				continue;
1083ed5b7817SJulian Elischer 			/*
1084ed5b7817SJulian Elischer 			 * If we already marked it as 'defer'  in a
1085ed5b7817SJulian Elischer 			 * previous pass, then try process it this time
1086ed5b7817SJulian Elischer 			 * and un-mark it
1087ed5b7817SJulian Elischer 			 */
1088df8bae1dSRodney W. Grimes 			if (fp->f_flag & FDEFER) {
1089df8bae1dSRodney W. Grimes 				fp->f_flag &= ~FDEFER;
1090df8bae1dSRodney W. Grimes 				unp_defer--;
1091df8bae1dSRodney W. Grimes 			} else {
1092ed5b7817SJulian Elischer 				/*
1093ed5b7817SJulian Elischer 				 * if it's not defered, then check if it's
1094ed5b7817SJulian Elischer 				 * already marked.. if so skip it
1095ed5b7817SJulian Elischer 				 */
1096df8bae1dSRodney W. Grimes 				if (fp->f_flag & FMARK)
1097df8bae1dSRodney W. Grimes 					continue;
1098ed5b7817SJulian Elischer 				/*
1099ed5b7817SJulian Elischer 				 * If all references are from messages
1100ed5b7817SJulian Elischer 				 * in transit, then skip it. it's not
1101ed5b7817SJulian Elischer 				 * externally accessible.
1102ed5b7817SJulian Elischer 				 */
1103df8bae1dSRodney W. Grimes 				if (fp->f_count == fp->f_msgcount)
1104df8bae1dSRodney W. Grimes 					continue;
1105ed5b7817SJulian Elischer 				/*
1106ed5b7817SJulian Elischer 				 * If it got this far then it must be
1107ed5b7817SJulian Elischer 				 * externally accessible.
1108ed5b7817SJulian Elischer 				 */
1109df8bae1dSRodney W. Grimes 				fp->f_flag |= FMARK;
1110df8bae1dSRodney W. Grimes 			}
1111ed5b7817SJulian Elischer 			/*
1112ed5b7817SJulian Elischer 			 * either it was defered, or it is externally
1113ed5b7817SJulian Elischer 			 * accessible and not already marked so.
1114ed5b7817SJulian Elischer 			 * Now check if it is possibly one of OUR sockets.
1115ed5b7817SJulian Elischer 			 */
1116df8bae1dSRodney W. Grimes 			if (fp->f_type != DTYPE_SOCKET ||
1117df8bae1dSRodney W. Grimes 			    (so = (struct socket *)fp->f_data) == 0)
1118df8bae1dSRodney W. Grimes 				continue;
1119748e0b0aSGarrett Wollman 			if (so->so_proto->pr_domain != &localdomain ||
1120df8bae1dSRodney W. Grimes 			    (so->so_proto->pr_flags&PR_RIGHTS) == 0)
1121df8bae1dSRodney W. Grimes 				continue;
1122df8bae1dSRodney W. Grimes #ifdef notdef
1123df8bae1dSRodney W. Grimes 			if (so->so_rcv.sb_flags & SB_LOCK) {
1124df8bae1dSRodney W. Grimes 				/*
1125df8bae1dSRodney W. Grimes 				 * This is problematical; it's not clear
1126df8bae1dSRodney W. Grimes 				 * we need to wait for the sockbuf to be
1127df8bae1dSRodney W. Grimes 				 * unlocked (on a uniprocessor, at least),
1128df8bae1dSRodney W. Grimes 				 * and it's also not clear what to do
1129df8bae1dSRodney W. Grimes 				 * if sbwait returns an error due to receipt
1130df8bae1dSRodney W. Grimes 				 * of a signal.  If sbwait does return
1131df8bae1dSRodney W. Grimes 				 * an error, we'll go into an infinite
1132df8bae1dSRodney W. Grimes 				 * loop.  Delete all of this for now.
1133df8bae1dSRodney W. Grimes 				 */
1134df8bae1dSRodney W. Grimes 				(void) sbwait(&so->so_rcv);
1135df8bae1dSRodney W. Grimes 				goto restart;
1136df8bae1dSRodney W. Grimes 			}
1137df8bae1dSRodney W. Grimes #endif
1138ed5b7817SJulian Elischer 			/*
1139ed5b7817SJulian Elischer 			 * So, Ok, it's one of our sockets and it IS externally
1140ed5b7817SJulian Elischer 			 * accessible (or was defered). Now we look
1141dc733423SDag-Erling Smørgrav 			 * to see if we hold any file descriptors in its
1142ed5b7817SJulian Elischer 			 * message buffers. Follow those links and mark them
1143ed5b7817SJulian Elischer 			 * as accessible too.
1144ed5b7817SJulian Elischer 			 */
1145df8bae1dSRodney W. Grimes 			unp_scan(so->so_rcv.sb_mb, unp_mark);
1146df8bae1dSRodney W. Grimes 		}
1147df8bae1dSRodney W. Grimes 	} while (unp_defer);
1148df8bae1dSRodney W. Grimes 	/*
1149df8bae1dSRodney W. Grimes 	 * We grab an extra reference to each of the file table entries
1150df8bae1dSRodney W. Grimes 	 * that are not otherwise accessible and then free the rights
1151df8bae1dSRodney W. Grimes 	 * that are stored in messages on them.
1152df8bae1dSRodney W. Grimes 	 *
1153df8bae1dSRodney W. Grimes 	 * The bug in the orginal code is a little tricky, so I'll describe
1154df8bae1dSRodney W. Grimes 	 * what's wrong with it here.
1155df8bae1dSRodney W. Grimes 	 *
1156df8bae1dSRodney W. Grimes 	 * It is incorrect to simply unp_discard each entry for f_msgcount
1157df8bae1dSRodney W. Grimes 	 * times -- consider the case of sockets A and B that contain
1158df8bae1dSRodney W. Grimes 	 * references to each other.  On a last close of some other socket,
1159df8bae1dSRodney W. Grimes 	 * we trigger a gc since the number of outstanding rights (unp_rights)
1160df8bae1dSRodney W. Grimes 	 * is non-zero.  If during the sweep phase the gc code un_discards,
1161df8bae1dSRodney W. Grimes 	 * we end up doing a (full) closef on the descriptor.  A closef on A
1162df8bae1dSRodney W. Grimes 	 * results in the following chain.  Closef calls soo_close, which
1163df8bae1dSRodney W. Grimes 	 * calls soclose.   Soclose calls first (through the switch
1164df8bae1dSRodney W. Grimes 	 * uipc_usrreq) unp_detach, which re-invokes unp_gc.  Unp_gc simply
1165df8bae1dSRodney W. Grimes 	 * returns because the previous instance had set unp_gcing, and
1166df8bae1dSRodney W. Grimes 	 * we return all the way back to soclose, which marks the socket
1167df8bae1dSRodney W. Grimes 	 * with SS_NOFDREF, and then calls sofree.  Sofree calls sorflush
1168df8bae1dSRodney W. Grimes 	 * to free up the rights that are queued in messages on the socket A,
1169df8bae1dSRodney W. Grimes 	 * i.e., the reference on B.  The sorflush calls via the dom_dispose
1170df8bae1dSRodney W. Grimes 	 * switch unp_dispose, which unp_scans with unp_discard.  This second
1171df8bae1dSRodney W. Grimes 	 * instance of unp_discard just calls closef on B.
1172df8bae1dSRodney W. Grimes 	 *
1173df8bae1dSRodney W. Grimes 	 * Well, a similar chain occurs on B, resulting in a sorflush on B,
1174df8bae1dSRodney W. Grimes 	 * which results in another closef on A.  Unfortunately, A is already
1175df8bae1dSRodney W. Grimes 	 * being closed, and the descriptor has already been marked with
1176df8bae1dSRodney W. Grimes 	 * SS_NOFDREF, and soclose panics at this point.
1177df8bae1dSRodney W. Grimes 	 *
1178df8bae1dSRodney W. Grimes 	 * Here, we first take an extra reference to each inaccessible
1179df8bae1dSRodney W. Grimes 	 * descriptor.  Then, we call sorflush ourself, since we know
1180df8bae1dSRodney W. Grimes 	 * it is a Unix domain socket anyhow.  After we destroy all the
1181df8bae1dSRodney W. Grimes 	 * rights carried in messages, we do a last closef to get rid
1182df8bae1dSRodney W. Grimes 	 * of our extra reference.  This is the last close, and the
1183df8bae1dSRodney W. Grimes 	 * unp_detach etc will shut down the socket.
1184df8bae1dSRodney W. Grimes 	 *
1185df8bae1dSRodney W. Grimes 	 * 91/09/19, bsy@cs.cmu.edu
1186df8bae1dSRodney W. Grimes 	 */
1187df8bae1dSRodney W. Grimes 	extra_ref = malloc(nfiles * sizeof(struct file *), M_FILE, M_WAITOK);
11882e3c8fcbSPoul-Henning Kamp 	for (nunref = 0, fp = LIST_FIRST(&filehead), fpp = extra_ref; fp != 0;
1189bc6f0e79SJeffrey Hsu 	    fp = nextfp) {
11902e3c8fcbSPoul-Henning Kamp 		nextfp = LIST_NEXT(fp, f_list);
1191ed5b7817SJulian Elischer 		/*
1192ed5b7817SJulian Elischer 		 * If it's not open, skip it
1193ed5b7817SJulian Elischer 		 */
1194df8bae1dSRodney W. Grimes 		if (fp->f_count == 0)
1195df8bae1dSRodney W. Grimes 			continue;
1196ed5b7817SJulian Elischer 		/*
1197ed5b7817SJulian Elischer 		 * If all refs are from msgs, and it's not marked accessible
1198ed5b7817SJulian Elischer 		 * then it must be referenced from some unreachable cycle
1199ed5b7817SJulian Elischer 		 * of (shut-down) FDs, so include it in our
1200ed5b7817SJulian Elischer 		 * list of FDs to remove
1201ed5b7817SJulian Elischer 		 */
1202df8bae1dSRodney W. Grimes 		if (fp->f_count == fp->f_msgcount && !(fp->f_flag & FMARK)) {
1203df8bae1dSRodney W. Grimes 			*fpp++ = fp;
1204df8bae1dSRodney W. Grimes 			nunref++;
1205df8bae1dSRodney W. Grimes 			fp->f_count++;
1206df8bae1dSRodney W. Grimes 		}
1207df8bae1dSRodney W. Grimes 	}
1208ed5b7817SJulian Elischer 	/*
1209ed5b7817SJulian Elischer 	 * for each FD on our hit list, do the following two things
1210ed5b7817SJulian Elischer 	 */
12111c7c3c6aSMatthew Dillon 	for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) {
12121c7c3c6aSMatthew Dillon 		struct file *tfp = *fpp;
12131c7c3c6aSMatthew Dillon 		if (tfp->f_type == DTYPE_SOCKET && tfp->f_data != NULL)
12141c7c3c6aSMatthew Dillon 			sorflush((struct socket *)(tfp->f_data));
12151c7c3c6aSMatthew Dillon 	}
1216df8bae1dSRodney W. Grimes 	for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp)
121792cbac68SPoul-Henning Kamp 		closef(*fpp, (struct proc *) NULL);
1218df8bae1dSRodney W. Grimes 	free((caddr_t)extra_ref, M_FILE);
1219df8bae1dSRodney W. Grimes 	unp_gcing = 0;
1220df8bae1dSRodney W. Grimes }
1221df8bae1dSRodney W. Grimes 
122226f9a767SRodney W. Grimes void
1223df8bae1dSRodney W. Grimes unp_dispose(m)
1224df8bae1dSRodney W. Grimes 	struct mbuf *m;
1225df8bae1dSRodney W. Grimes {
1226996c772fSJohn Dyson 
1227df8bae1dSRodney W. Grimes 	if (m)
1228df8bae1dSRodney W. Grimes 		unp_scan(m, unp_discard);
1229df8bae1dSRodney W. Grimes }
1230df8bae1dSRodney W. Grimes 
1231f708ef1bSPoul-Henning Kamp static void
1232df8bae1dSRodney W. Grimes unp_scan(m0, op)
1233df8bae1dSRodney W. Grimes 	register struct mbuf *m0;
1234996c772fSJohn Dyson 	void (*op) __P((struct file *));
1235df8bae1dSRodney W. Grimes {
1236df8bae1dSRodney W. Grimes 	register struct mbuf *m;
1237df8bae1dSRodney W. Grimes 	register struct file **rp;
1238df8bae1dSRodney W. Grimes 	register struct cmsghdr *cm;
1239df8bae1dSRodney W. Grimes 	register int i;
1240df8bae1dSRodney W. Grimes 	int qfds;
1241df8bae1dSRodney W. Grimes 
1242df8bae1dSRodney W. Grimes 	while (m0) {
1243df8bae1dSRodney W. Grimes 		for (m = m0; m; m = m->m_next)
1244df8bae1dSRodney W. Grimes 			if (m->m_type == MT_CONTROL &&
1245df8bae1dSRodney W. Grimes 			    m->m_len >= sizeof(*cm)) {
1246df8bae1dSRodney W. Grimes 				cm = mtod(m, struct cmsghdr *);
1247df8bae1dSRodney W. Grimes 				if (cm->cmsg_level != SOL_SOCKET ||
1248df8bae1dSRodney W. Grimes 				    cm->cmsg_type != SCM_RIGHTS)
1249df8bae1dSRodney W. Grimes 					continue;
12508692c025SYoshinobu Inoue 				qfds = (cm->cmsg_len -
12518692c025SYoshinobu Inoue 					(CMSG_DATA(cm) - (u_char *)cm))
1252df8bae1dSRodney W. Grimes 						/ sizeof (struct file *);
12538692c025SYoshinobu Inoue 				rp = (struct file **)CMSG_DATA(cm);
1254df8bae1dSRodney W. Grimes 				for (i = 0; i < qfds; i++)
1255df8bae1dSRodney W. Grimes 					(*op)(*rp++);
1256df8bae1dSRodney W. Grimes 				break;		/* XXX, but saves time */
1257df8bae1dSRodney W. Grimes 			}
1258df8bae1dSRodney W. Grimes 		m0 = m0->m_act;
1259df8bae1dSRodney W. Grimes 	}
1260df8bae1dSRodney W. Grimes }
1261df8bae1dSRodney W. Grimes 
1262f708ef1bSPoul-Henning Kamp static void
1263df8bae1dSRodney W. Grimes unp_mark(fp)
1264df8bae1dSRodney W. Grimes 	struct file *fp;
1265df8bae1dSRodney W. Grimes {
1266df8bae1dSRodney W. Grimes 
1267df8bae1dSRodney W. Grimes 	if (fp->f_flag & FMARK)
1268df8bae1dSRodney W. Grimes 		return;
1269df8bae1dSRodney W. Grimes 	unp_defer++;
1270df8bae1dSRodney W. Grimes 	fp->f_flag |= (FMARK|FDEFER);
1271df8bae1dSRodney W. Grimes }
1272df8bae1dSRodney W. Grimes 
1273f708ef1bSPoul-Henning Kamp static void
1274df8bae1dSRodney W. Grimes unp_discard(fp)
1275df8bae1dSRodney W. Grimes 	struct file *fp;
1276df8bae1dSRodney W. Grimes {
1277df8bae1dSRodney W. Grimes 
1278df8bae1dSRodney W. Grimes 	fp->f_msgcount--;
1279df8bae1dSRodney W. Grimes 	unp_rights--;
1280df8bae1dSRodney W. Grimes 	(void) closef(fp, (struct proc *)NULL);
1281df8bae1dSRodney W. Grimes }
1282