xref: /freebsd/sys/kern/uipc_usrreq.c (revision 335654d73e2c46b04b41961ab1d52bb8445f8c02)
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 
37335654d7SRobert Watson #include "opt_mac.h"
38335654d7SRobert Watson 
39df8bae1dSRodney W. Grimes #include <sys/param.h>
40fb919e4dSMark Murray #include <sys/domain.h>
41960ed29cSSeigo Tanimura #include <sys/fcntl.h>
42d826c479SBruce Evans #include <sys/malloc.h>		/* XXX must be before <sys/file.h> */
43639acc13SGarrett Wollman #include <sys/file.h>
44960ed29cSSeigo Tanimura #include <sys/filedesc.h>
45960ed29cSSeigo Tanimura #include <sys/jail.h>
46960ed29cSSeigo Tanimura #include <sys/kernel.h>
47960ed29cSSeigo Tanimura #include <sys/lock.h>
48639acc13SGarrett Wollman #include <sys/mbuf.h>
49960ed29cSSeigo Tanimura #include <sys/mutex.h>
50639acc13SGarrett Wollman #include <sys/namei.h>
51639acc13SGarrett Wollman #include <sys/proc.h>
52df8bae1dSRodney W. Grimes #include <sys/protosw.h>
53960ed29cSSeigo Tanimura #include <sys/resourcevar.h>
54df8bae1dSRodney W. Grimes #include <sys/socket.h>
55df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
56960ed29cSSeigo Tanimura #include <sys/signalvar.h>
57df8bae1dSRodney W. Grimes #include <sys/stat.h>
58960ed29cSSeigo Tanimura #include <sys/sx.h>
59639acc13SGarrett Wollman #include <sys/sysctl.h>
60960ed29cSSeigo Tanimura #include <sys/systm.h>
61639acc13SGarrett Wollman #include <sys/un.h>
6298271db4SGarrett Wollman #include <sys/unpcb.h>
63639acc13SGarrett Wollman #include <sys/vnode.h>
64df8bae1dSRodney W. Grimes 
659e9d298aSJeff Roberson #include <vm/uma.h>
6698271db4SGarrett Wollman 
679e9d298aSJeff Roberson static uma_zone_t unp_zone;
6898271db4SGarrett Wollman static	unp_gen_t unp_gencnt;
6998271db4SGarrett Wollman static	u_int unp_count;
7098271db4SGarrett Wollman 
7198271db4SGarrett Wollman static	struct unp_head unp_shead, unp_dhead;
7298271db4SGarrett Wollman 
73df8bae1dSRodney W. Grimes /*
74df8bae1dSRodney W. Grimes  * Unix communications domain.
75df8bae1dSRodney W. Grimes  *
76df8bae1dSRodney W. Grimes  * TODO:
77df8bae1dSRodney W. Grimes  *	SEQPACKET, RDM
78df8bae1dSRodney W. Grimes  *	rethink name space problems
79df8bae1dSRodney W. Grimes  *	need a proper out-of-band
8098271db4SGarrett Wollman  *	lock pushdown
81df8bae1dSRodney W. Grimes  */
82f708ef1bSPoul-Henning Kamp static struct	sockaddr sun_noname = { sizeof(sun_noname), AF_LOCAL };
83f708ef1bSPoul-Henning Kamp static ino_t	unp_ino;		/* prototype for fake inode numbers */
84f708ef1bSPoul-Henning Kamp 
854d77a549SAlfred Perlstein static int     unp_attach(struct socket *);
864d77a549SAlfred Perlstein static void    unp_detach(struct unpcb *);
874d77a549SAlfred Perlstein static int     unp_bind(struct unpcb *,struct sockaddr *, struct thread *);
8870f52b48SBruce Evans static int     unp_connect(struct socket *,struct sockaddr *, struct thread *);
894d77a549SAlfred Perlstein static void    unp_disconnect(struct unpcb *);
904d77a549SAlfred Perlstein static void    unp_shutdown(struct unpcb *);
914d77a549SAlfred Perlstein static void    unp_drop(struct unpcb *, int);
924d77a549SAlfred Perlstein static void    unp_gc(void);
934d77a549SAlfred Perlstein static void    unp_scan(struct mbuf *, void (*)(struct file *));
944d77a549SAlfred Perlstein static void    unp_mark(struct file *);
954d77a549SAlfred Perlstein static void    unp_discard(struct file *);
964d77a549SAlfred Perlstein static void    unp_freerights(struct file **, int);
974d77a549SAlfred Perlstein static int     unp_internalize(struct mbuf **, struct thread *);
984d77a549SAlfred Perlstein static int     unp_listen(struct unpcb *, struct thread *);
99f708ef1bSPoul-Henning Kamp 
100a29f300eSGarrett Wollman static int
101a29f300eSGarrett Wollman uipc_abort(struct socket *so)
102df8bae1dSRodney W. Grimes {
103df8bae1dSRodney W. Grimes 	struct unpcb *unp = sotounpcb(so);
104df8bae1dSRodney W. Grimes 
105a29f300eSGarrett Wollman 	if (unp == 0)
106a29f300eSGarrett Wollman 		return EINVAL;
107a29f300eSGarrett Wollman 	unp_drop(unp, ECONNABORTED);
108ddb7d629SIan Dowse 	unp_detach(unp);
109ddb7d629SIan Dowse 	sotryfree(so);
110a29f300eSGarrett Wollman 	return 0;
111df8bae1dSRodney W. Grimes }
112df8bae1dSRodney W. Grimes 
113a29f300eSGarrett Wollman static int
11457bf258eSGarrett Wollman uipc_accept(struct socket *so, struct sockaddr **nam)
115a29f300eSGarrett Wollman {
116a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so);
117df8bae1dSRodney W. Grimes 
118a29f300eSGarrett Wollman 	if (unp == 0)
119a29f300eSGarrett Wollman 		return EINVAL;
120df8bae1dSRodney W. Grimes 
121df8bae1dSRodney W. Grimes 	/*
122df8bae1dSRodney W. Grimes 	 * Pass back name of connected socket,
123df8bae1dSRodney W. Grimes 	 * if it was bound and we are still connected
124df8bae1dSRodney W. Grimes 	 * (our peer may have closed already!).
125df8bae1dSRodney W. Grimes 	 */
126df8bae1dSRodney W. Grimes 	if (unp->unp_conn && unp->unp_conn->unp_addr) {
12757bf258eSGarrett Wollman 		*nam = dup_sockaddr((struct sockaddr *)unp->unp_conn->unp_addr,
12857bf258eSGarrett Wollman 				    1);
129df8bae1dSRodney W. Grimes 	} else {
13057bf258eSGarrett Wollman 		*nam = dup_sockaddr((struct sockaddr *)&sun_noname, 1);
131df8bae1dSRodney W. Grimes 	}
132a29f300eSGarrett Wollman 	return 0;
133a29f300eSGarrett Wollman }
134df8bae1dSRodney W. Grimes 
135a29f300eSGarrett Wollman static int
136b40ce416SJulian Elischer uipc_attach(struct socket *so, int proto, struct thread *td)
137a29f300eSGarrett Wollman {
138a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so);
139df8bae1dSRodney W. Grimes 
140a29f300eSGarrett Wollman 	if (unp != 0)
141a29f300eSGarrett Wollman 		return EISCONN;
142a29f300eSGarrett Wollman 	return unp_attach(so);
143a29f300eSGarrett Wollman }
144a29f300eSGarrett Wollman 
145a29f300eSGarrett Wollman static int
146b40ce416SJulian Elischer uipc_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
147a29f300eSGarrett Wollman {
148a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so);
149a29f300eSGarrett Wollman 
150a29f300eSGarrett Wollman 	if (unp == 0)
151a29f300eSGarrett Wollman 		return EINVAL;
152a29f300eSGarrett Wollman 
153b40ce416SJulian Elischer 	return unp_bind(unp, nam, td);
154a29f300eSGarrett Wollman }
155a29f300eSGarrett Wollman 
156a29f300eSGarrett Wollman static int
157b40ce416SJulian Elischer uipc_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
158a29f300eSGarrett Wollman {
159a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so);
160a29f300eSGarrett Wollman 
161a29f300eSGarrett Wollman 	if (unp == 0)
162a29f300eSGarrett Wollman 		return EINVAL;
163b40ce416SJulian Elischer 	return unp_connect(so, nam, curthread);
164a29f300eSGarrett Wollman }
165a29f300eSGarrett Wollman 
166a29f300eSGarrett Wollman static int
167a29f300eSGarrett Wollman uipc_connect2(struct socket *so1, struct socket *so2)
168a29f300eSGarrett Wollman {
169a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so1);
170a29f300eSGarrett Wollman 
171a29f300eSGarrett Wollman 	if (unp == 0)
172a29f300eSGarrett Wollman 		return EINVAL;
173a29f300eSGarrett Wollman 
174a29f300eSGarrett Wollman 	return unp_connect2(so1, so2);
175a29f300eSGarrett Wollman }
176a29f300eSGarrett Wollman 
177a29f300eSGarrett Wollman /* control is EOPNOTSUPP */
178a29f300eSGarrett Wollman 
179a29f300eSGarrett Wollman static int
180a29f300eSGarrett Wollman uipc_detach(struct socket *so)
181a29f300eSGarrett Wollman {
182a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so);
183a29f300eSGarrett Wollman 
184a29f300eSGarrett Wollman 	if (unp == 0)
185a29f300eSGarrett Wollman 		return EINVAL;
186a29f300eSGarrett Wollman 
187a29f300eSGarrett Wollman 	unp_detach(unp);
188a29f300eSGarrett Wollman 	return 0;
189a29f300eSGarrett Wollman }
190a29f300eSGarrett Wollman 
191a29f300eSGarrett Wollman static int
192a29f300eSGarrett Wollman uipc_disconnect(struct socket *so)
193a29f300eSGarrett Wollman {
194a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so);
195a29f300eSGarrett Wollman 
196a29f300eSGarrett Wollman 	if (unp == 0)
197a29f300eSGarrett Wollman 		return EINVAL;
198a29f300eSGarrett Wollman 	unp_disconnect(unp);
199a29f300eSGarrett Wollman 	return 0;
200a29f300eSGarrett Wollman }
201a29f300eSGarrett Wollman 
202a29f300eSGarrett Wollman static int
203b40ce416SJulian Elischer uipc_listen(struct socket *so, struct thread *td)
204a29f300eSGarrett Wollman {
205a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so);
206a29f300eSGarrett Wollman 
207a29f300eSGarrett Wollman 	if (unp == 0 || unp->unp_vnode == 0)
208a29f300eSGarrett Wollman 		return EINVAL;
2096f105b34SJohn Baldwin 	return unp_listen(unp, td);
210a29f300eSGarrett Wollman }
211a29f300eSGarrett Wollman 
212a29f300eSGarrett Wollman static int
21357bf258eSGarrett Wollman uipc_peeraddr(struct socket *so, struct sockaddr **nam)
214a29f300eSGarrett Wollman {
215a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so);
216a29f300eSGarrett Wollman 
217a29f300eSGarrett Wollman 	if (unp == 0)
218a29f300eSGarrett Wollman 		return EINVAL;
21957bf258eSGarrett Wollman 	if (unp->unp_conn && unp->unp_conn->unp_addr)
22057bf258eSGarrett Wollman 		*nam = dup_sockaddr((struct sockaddr *)unp->unp_conn->unp_addr,
22157bf258eSGarrett Wollman 				    1);
222a29f300eSGarrett Wollman 	return 0;
223a29f300eSGarrett Wollman }
224a29f300eSGarrett Wollman 
225a29f300eSGarrett Wollman static int
226a29f300eSGarrett Wollman uipc_rcvd(struct socket *so, int flags)
227a29f300eSGarrett Wollman {
228a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so);
229a29f300eSGarrett Wollman 	struct socket *so2;
2306aef685fSBrian Feldman 	u_long newhiwat;
231a29f300eSGarrett Wollman 
232a29f300eSGarrett Wollman 	if (unp == 0)
233a29f300eSGarrett Wollman 		return EINVAL;
234df8bae1dSRodney W. Grimes 	switch (so->so_type) {
235df8bae1dSRodney W. Grimes 	case SOCK_DGRAM:
236a29f300eSGarrett Wollman 		panic("uipc_rcvd DGRAM?");
237df8bae1dSRodney W. Grimes 		/*NOTREACHED*/
238df8bae1dSRodney W. Grimes 
239df8bae1dSRodney W. Grimes 	case SOCK_STREAM:
240df8bae1dSRodney W. Grimes 		if (unp->unp_conn == 0)
241df8bae1dSRodney W. Grimes 			break;
242df8bae1dSRodney W. Grimes 		so2 = unp->unp_conn->unp_socket;
243df8bae1dSRodney W. Grimes 		/*
244df8bae1dSRodney W. Grimes 		 * Adjust backpressure on sender
245df8bae1dSRodney W. Grimes 		 * and wakeup any waiting to write.
246df8bae1dSRodney W. Grimes 		 */
247ff8b0106SBrian Feldman 		so2->so_snd.sb_mbmax += unp->unp_mbcnt - so->so_rcv.sb_mbcnt;
248ff8b0106SBrian Feldman 		unp->unp_mbcnt = so->so_rcv.sb_mbcnt;
2496aef685fSBrian Feldman 		newhiwat = so2->so_snd.sb_hiwat + unp->unp_cc -
2506aef685fSBrian Feldman 		    so->so_rcv.sb_cc;
251f535380cSDon Lewis 		(void)chgsbsize(so2->so_cred->cr_uidinfo, &so2->so_snd.sb_hiwat,
2526aef685fSBrian Feldman 		    newhiwat, RLIM_INFINITY);
253ff8b0106SBrian Feldman 		unp->unp_cc = so->so_rcv.sb_cc;
254df8bae1dSRodney W. Grimes 		sowwakeup(so2);
255df8bae1dSRodney W. Grimes 		break;
256df8bae1dSRodney W. Grimes 
257df8bae1dSRodney W. Grimes 	default:
258a29f300eSGarrett Wollman 		panic("uipc_rcvd unknown socktype");
259df8bae1dSRodney W. Grimes 	}
260a29f300eSGarrett Wollman 	return 0;
261a29f300eSGarrett Wollman }
262df8bae1dSRodney W. Grimes 
263a29f300eSGarrett Wollman /* pru_rcvoob is EOPNOTSUPP */
264a29f300eSGarrett Wollman 
265a29f300eSGarrett Wollman static int
26657bf258eSGarrett Wollman uipc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
267b40ce416SJulian Elischer 	  struct mbuf *control, struct thread *td)
268a29f300eSGarrett Wollman {
269a29f300eSGarrett Wollman 	int error = 0;
270a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so);
271a29f300eSGarrett Wollman 	struct socket *so2;
2726aef685fSBrian Feldman 	u_long newhiwat;
273a29f300eSGarrett Wollman 
274a29f300eSGarrett Wollman 	if (unp == 0) {
275a29f300eSGarrett Wollman 		error = EINVAL;
276a29f300eSGarrett Wollman 		goto release;
277a29f300eSGarrett Wollman 	}
278a29f300eSGarrett Wollman 	if (flags & PRUS_OOB) {
279a29f300eSGarrett Wollman 		error = EOPNOTSUPP;
280a29f300eSGarrett Wollman 		goto release;
281a29f300eSGarrett Wollman 	}
282a29f300eSGarrett Wollman 
2832bc21ed9SDavid Malone 	if (control && (error = unp_internalize(&control, td)))
284a29f300eSGarrett Wollman 		goto release;
285df8bae1dSRodney W. Grimes 
286a29f300eSGarrett Wollman 	switch (so->so_type) {
287a29f300eSGarrett Wollman 	case SOCK_DGRAM:
288a29f300eSGarrett Wollman 	{
289df8bae1dSRodney W. Grimes 		struct sockaddr *from;
290df8bae1dSRodney W. Grimes 
291df8bae1dSRodney W. Grimes 		if (nam) {
292df8bae1dSRodney W. Grimes 			if (unp->unp_conn) {
293df8bae1dSRodney W. Grimes 				error = EISCONN;
294df8bae1dSRodney W. Grimes 				break;
295df8bae1dSRodney W. Grimes 			}
296b40ce416SJulian Elischer 			error = unp_connect(so, nam, td);
297df8bae1dSRodney W. Grimes 			if (error)
298df8bae1dSRodney W. Grimes 				break;
299df8bae1dSRodney W. Grimes 		} else {
300df8bae1dSRodney W. Grimes 			if (unp->unp_conn == 0) {
301df8bae1dSRodney W. Grimes 				error = ENOTCONN;
302df8bae1dSRodney W. Grimes 				break;
303df8bae1dSRodney W. Grimes 			}
304df8bae1dSRodney W. Grimes 		}
305df8bae1dSRodney W. Grimes 		so2 = unp->unp_conn->unp_socket;
306df8bae1dSRodney W. Grimes 		if (unp->unp_addr)
30757bf258eSGarrett Wollman 			from = (struct sockaddr *)unp->unp_addr;
308df8bae1dSRodney W. Grimes 		else
309df8bae1dSRodney W. Grimes 			from = &sun_noname;
310df8bae1dSRodney W. Grimes 		if (sbappendaddr(&so2->so_rcv, from, m, control)) {
311df8bae1dSRodney W. Grimes 			sorwakeup(so2);
312df8bae1dSRodney W. Grimes 			m = 0;
313df8bae1dSRodney W. Grimes 			control = 0;
314df8bae1dSRodney W. Grimes 		} else
315df8bae1dSRodney W. Grimes 			error = ENOBUFS;
316df8bae1dSRodney W. Grimes 		if (nam)
317df8bae1dSRodney W. Grimes 			unp_disconnect(unp);
318df8bae1dSRodney W. Grimes 		break;
319df8bae1dSRodney W. Grimes 	}
320df8bae1dSRodney W. Grimes 
321df8bae1dSRodney W. Grimes 	case SOCK_STREAM:
3226b8fda4dSGarrett Wollman 		/* Connect if not connected yet. */
3236b8fda4dSGarrett Wollman 		/*
3246b8fda4dSGarrett Wollman 		 * Note: A better implementation would complain
325402cc72dSDavid Greenman 		 * if not equal to the peer's address.
3266b8fda4dSGarrett Wollman 		 */
327402cc72dSDavid Greenman 		if ((so->so_state & SS_ISCONNECTED) == 0) {
328402cc72dSDavid Greenman 			if (nam) {
329b40ce416SJulian Elischer 				error = unp_connect(so, nam, td);
330402cc72dSDavid Greenman 				if (error)
3316b8fda4dSGarrett Wollman 					break;	/* XXX */
332402cc72dSDavid Greenman 			} else {
333402cc72dSDavid Greenman 				error = ENOTCONN;
334402cc72dSDavid Greenman 				break;
335402cc72dSDavid Greenman 			}
336402cc72dSDavid Greenman 		}
337402cc72dSDavid Greenman 
338df8bae1dSRodney W. Grimes 		if (so->so_state & SS_CANTSENDMORE) {
339df8bae1dSRodney W. Grimes 			error = EPIPE;
340df8bae1dSRodney W. Grimes 			break;
341df8bae1dSRodney W. Grimes 		}
342df8bae1dSRodney W. Grimes 		if (unp->unp_conn == 0)
343a29f300eSGarrett Wollman 			panic("uipc_send connected but no connection?");
344df8bae1dSRodney W. Grimes 		so2 = unp->unp_conn->unp_socket;
345df8bae1dSRodney W. Grimes 		/*
346df8bae1dSRodney W. Grimes 		 * Send to paired receive port, and then reduce
347df8bae1dSRodney W. Grimes 		 * send buffer hiwater marks to maintain backpressure.
348df8bae1dSRodney W. Grimes 		 * Wake up readers.
349df8bae1dSRodney W. Grimes 		 */
350df8bae1dSRodney W. Grimes 		if (control) {
351ff8b0106SBrian Feldman 			if (sbappendcontrol(&so2->so_rcv, m, control))
352df8bae1dSRodney W. Grimes 				control = 0;
353df8bae1dSRodney W. Grimes 		} else
354ff8b0106SBrian Feldman 			sbappend(&so2->so_rcv, m);
355ff8b0106SBrian Feldman 		so->so_snd.sb_mbmax -=
356ff8b0106SBrian Feldman 			so2->so_rcv.sb_mbcnt - unp->unp_conn->unp_mbcnt;
357ff8b0106SBrian Feldman 		unp->unp_conn->unp_mbcnt = so2->so_rcv.sb_mbcnt;
3586aef685fSBrian Feldman 		newhiwat = so->so_snd.sb_hiwat -
3596aef685fSBrian Feldman 		    (so2->so_rcv.sb_cc - unp->unp_conn->unp_cc);
360f535380cSDon Lewis 		(void)chgsbsize(so->so_cred->cr_uidinfo, &so->so_snd.sb_hiwat,
3616aef685fSBrian Feldman 		    newhiwat, RLIM_INFINITY);
362ff8b0106SBrian Feldman 		unp->unp_conn->unp_cc = so2->so_rcv.sb_cc;
363df8bae1dSRodney W. Grimes 		sorwakeup(so2);
364df8bae1dSRodney W. Grimes 		m = 0;
365df8bae1dSRodney W. Grimes 		break;
366df8bae1dSRodney W. Grimes 
367df8bae1dSRodney W. Grimes 	default:
368a29f300eSGarrett Wollman 		panic("uipc_send unknown socktype");
369df8bae1dSRodney W. Grimes 	}
370a29f300eSGarrett Wollman 
3716b8fda4dSGarrett Wollman 	/*
3726b8fda4dSGarrett Wollman 	 * SEND_EOF is equivalent to a SEND followed by
3736b8fda4dSGarrett Wollman 	 * a SHUTDOWN.
3746b8fda4dSGarrett Wollman 	 */
375a29f300eSGarrett Wollman 	if (flags & PRUS_EOF) {
3766b8fda4dSGarrett Wollman 		socantsendmore(so);
3776b8fda4dSGarrett Wollman 		unp_shutdown(unp);
3786b8fda4dSGarrett Wollman 	}
379df8bae1dSRodney W. Grimes 
380bd508d39SDon Lewis 	if (control && error != 0)
381bd508d39SDon Lewis 		unp_dispose(control);
382bd508d39SDon Lewis 
383a29f300eSGarrett Wollman release:
384a29f300eSGarrett Wollman 	if (control)
385a29f300eSGarrett Wollman 		m_freem(control);
386a29f300eSGarrett Wollman 	if (m)
387a29f300eSGarrett Wollman 		m_freem(m);
388a29f300eSGarrett Wollman 	return error;
389a29f300eSGarrett Wollman }
390df8bae1dSRodney W. Grimes 
391a29f300eSGarrett Wollman static int
392a29f300eSGarrett Wollman uipc_sense(struct socket *so, struct stat *sb)
393a29f300eSGarrett Wollman {
394a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so);
395a29f300eSGarrett Wollman 	struct socket *so2;
396a29f300eSGarrett Wollman 
397a29f300eSGarrett Wollman 	if (unp == 0)
398a29f300eSGarrett Wollman 		return EINVAL;
399a29f300eSGarrett Wollman 	sb->st_blksize = so->so_snd.sb_hiwat;
400df8bae1dSRodney W. Grimes 	if (so->so_type == SOCK_STREAM && unp->unp_conn != 0) {
401df8bae1dSRodney W. Grimes 		so2 = unp->unp_conn->unp_socket;
402a29f300eSGarrett Wollman 		sb->st_blksize += so2->so_rcv.sb_cc;
403df8bae1dSRodney W. Grimes 	}
404bfbb9ce6SPoul-Henning Kamp 	sb->st_dev = NOUDEV;
405df8bae1dSRodney W. Grimes 	if (unp->unp_ino == 0)
406df8bae1dSRodney W. Grimes 		unp->unp_ino = unp_ino++;
407a29f300eSGarrett Wollman 	sb->st_ino = unp->unp_ino;
408df8bae1dSRodney W. Grimes 	return (0);
409a29f300eSGarrett Wollman }
410df8bae1dSRodney W. Grimes 
411a29f300eSGarrett Wollman static int
412a29f300eSGarrett Wollman uipc_shutdown(struct socket *so)
413a29f300eSGarrett Wollman {
414a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so);
415df8bae1dSRodney W. Grimes 
416a29f300eSGarrett Wollman 	if (unp == 0)
417a29f300eSGarrett Wollman 		return EINVAL;
418a29f300eSGarrett Wollman 	socantsendmore(so);
419a29f300eSGarrett Wollman 	unp_shutdown(unp);
420a29f300eSGarrett Wollman 	return 0;
421a29f300eSGarrett Wollman }
422df8bae1dSRodney W. Grimes 
423a29f300eSGarrett Wollman static int
42457bf258eSGarrett Wollman uipc_sockaddr(struct socket *so, struct sockaddr **nam)
425a29f300eSGarrett Wollman {
426a29f300eSGarrett Wollman 	struct unpcb *unp = sotounpcb(so);
427a29f300eSGarrett Wollman 
428a29f300eSGarrett Wollman 	if (unp == 0)
429a29f300eSGarrett Wollman 		return EINVAL;
43057bf258eSGarrett Wollman 	if (unp->unp_addr)
43157bf258eSGarrett Wollman 		*nam = dup_sockaddr((struct sockaddr *)unp->unp_addr, 1);
43283f3198bSThomas Moestl 	else
43383f3198bSThomas Moestl 		*nam = dup_sockaddr((struct sockaddr *)&sun_noname, 1);
434a29f300eSGarrett Wollman 	return 0;
435df8bae1dSRodney W. Grimes }
436a29f300eSGarrett Wollman 
437a29f300eSGarrett Wollman struct pr_usrreqs uipc_usrreqs = {
438a29f300eSGarrett Wollman 	uipc_abort, uipc_accept, uipc_attach, uipc_bind, uipc_connect,
439a29f300eSGarrett Wollman 	uipc_connect2, pru_control_notsupp, uipc_detach, uipc_disconnect,
440a29f300eSGarrett Wollman 	uipc_listen, uipc_peeraddr, uipc_rcvd, pru_rcvoob_notsupp,
441a29f300eSGarrett Wollman 	uipc_send, uipc_sense, uipc_shutdown, uipc_sockaddr,
44251338ea8SPeter Wemm 	sosend, soreceive, sopoll
443a29f300eSGarrett Wollman };
444df8bae1dSRodney W. Grimes 
4450c1bb4fbSDima Dorfman int
4460c1bb4fbSDima Dorfman uipc_ctloutput(so, sopt)
4470c1bb4fbSDima Dorfman 	struct socket *so;
4480c1bb4fbSDima Dorfman 	struct sockopt *sopt;
4490c1bb4fbSDima Dorfman {
4500c1bb4fbSDima Dorfman 	struct unpcb *unp = sotounpcb(so);
4510c1bb4fbSDima Dorfman 	int error;
4520c1bb4fbSDima Dorfman 
4530c1bb4fbSDima Dorfman 	switch (sopt->sopt_dir) {
4540c1bb4fbSDima Dorfman 	case SOPT_GET:
4550c1bb4fbSDima Dorfman 		switch (sopt->sopt_name) {
4560c1bb4fbSDima Dorfman 		case LOCAL_PEERCRED:
4570c1bb4fbSDima Dorfman 			if (unp->unp_flags & UNP_HAVEPC)
4580c1bb4fbSDima Dorfman 				error = sooptcopyout(sopt, &unp->unp_peercred,
4590c1bb4fbSDima Dorfman 				    sizeof(unp->unp_peercred));
4600c1bb4fbSDima Dorfman 			else {
4610c1bb4fbSDima Dorfman 				if (so->so_type == SOCK_STREAM)
4620c1bb4fbSDima Dorfman 					error = ENOTCONN;
4630c1bb4fbSDima Dorfman 				else
4640c1bb4fbSDima Dorfman 					error = EINVAL;
4650c1bb4fbSDima Dorfman 			}
4660c1bb4fbSDima Dorfman 			break;
4670c1bb4fbSDima Dorfman 		default:
4680c1bb4fbSDima Dorfman 			error = EOPNOTSUPP;
4690c1bb4fbSDima Dorfman 			break;
4700c1bb4fbSDima Dorfman 		}
4710c1bb4fbSDima Dorfman 		break;
4720c1bb4fbSDima Dorfman 	case SOPT_SET:
4730c1bb4fbSDima Dorfman 	default:
4740c1bb4fbSDima Dorfman 		error = EOPNOTSUPP;
4750c1bb4fbSDima Dorfman 		break;
4760c1bb4fbSDima Dorfman 	}
4770c1bb4fbSDima Dorfman 	return (error);
4780c1bb4fbSDima Dorfman }
4790c1bb4fbSDima Dorfman 
480df8bae1dSRodney W. Grimes /*
481df8bae1dSRodney W. Grimes  * Both send and receive buffers are allocated PIPSIZ bytes of buffering
482df8bae1dSRodney W. Grimes  * for stream sockets, although the total for sender and receiver is
483df8bae1dSRodney W. Grimes  * actually only PIPSIZ.
484df8bae1dSRodney W. Grimes  * Datagram sockets really use the sendspace as the maximum datagram size,
485df8bae1dSRodney W. Grimes  * and don't really want to reserve the sendspace.  Their recvspace should
486df8bae1dSRodney W. Grimes  * be large enough for at least one max-size datagram plus address.
487df8bae1dSRodney W. Grimes  */
4885dce41c5SJohn Dyson #ifndef PIPSIZ
4895dce41c5SJohn Dyson #define	PIPSIZ	8192
4905dce41c5SJohn Dyson #endif
491f708ef1bSPoul-Henning Kamp static u_long	unpst_sendspace = PIPSIZ;
492f708ef1bSPoul-Henning Kamp static u_long	unpst_recvspace = PIPSIZ;
493f708ef1bSPoul-Henning Kamp static u_long	unpdg_sendspace = 2*1024;	/* really max datagram size */
494f708ef1bSPoul-Henning Kamp static u_long	unpdg_recvspace = 4*1024;
495df8bae1dSRodney W. Grimes 
496f708ef1bSPoul-Henning Kamp static int	unp_rights;			/* file descriptors in flight */
497df8bae1dSRodney W. Grimes 
498ce02431fSDoug Rabson SYSCTL_DECL(_net_local_stream);
499639acc13SGarrett Wollman SYSCTL_INT(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW,
500639acc13SGarrett Wollman 	   &unpst_sendspace, 0, "");
501639acc13SGarrett Wollman SYSCTL_INT(_net_local_stream, OID_AUTO, recvspace, CTLFLAG_RW,
502639acc13SGarrett Wollman 	   &unpst_recvspace, 0, "");
503ce02431fSDoug Rabson SYSCTL_DECL(_net_local_dgram);
504639acc13SGarrett Wollman SYSCTL_INT(_net_local_dgram, OID_AUTO, maxdgram, CTLFLAG_RW,
505639acc13SGarrett Wollman 	   &unpdg_sendspace, 0, "");
506639acc13SGarrett Wollman SYSCTL_INT(_net_local_dgram, OID_AUTO, recvspace, CTLFLAG_RW,
507639acc13SGarrett Wollman 	   &unpdg_recvspace, 0, "");
508ce02431fSDoug Rabson SYSCTL_DECL(_net_local);
509639acc13SGarrett Wollman SYSCTL_INT(_net_local, OID_AUTO, inflight, CTLFLAG_RD, &unp_rights, 0, "");
510639acc13SGarrett Wollman 
511f708ef1bSPoul-Henning Kamp static int
512df8bae1dSRodney W. Grimes unp_attach(so)
513df8bae1dSRodney W. Grimes 	struct socket *so;
514df8bae1dSRodney W. Grimes {
515df8bae1dSRodney W. Grimes 	register struct unpcb *unp;
516df8bae1dSRodney W. Grimes 	int error;
517df8bae1dSRodney W. Grimes 
518df8bae1dSRodney W. Grimes 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
519df8bae1dSRodney W. Grimes 		switch (so->so_type) {
520df8bae1dSRodney W. Grimes 
521df8bae1dSRodney W. Grimes 		case SOCK_STREAM:
522df8bae1dSRodney W. Grimes 			error = soreserve(so, unpst_sendspace, unpst_recvspace);
523df8bae1dSRodney W. Grimes 			break;
524df8bae1dSRodney W. Grimes 
525df8bae1dSRodney W. Grimes 		case SOCK_DGRAM:
526df8bae1dSRodney W. Grimes 			error = soreserve(so, unpdg_sendspace, unpdg_recvspace);
527df8bae1dSRodney W. Grimes 			break;
528df8bae1dSRodney W. Grimes 
529df8bae1dSRodney W. Grimes 		default:
530df8bae1dSRodney W. Grimes 			panic("unp_attach");
531df8bae1dSRodney W. Grimes 		}
532df8bae1dSRodney W. Grimes 		if (error)
533df8bae1dSRodney W. Grimes 			return (error);
534df8bae1dSRodney W. Grimes 	}
5359e9d298aSJeff Roberson 	unp = uma_zalloc(unp_zone, M_WAITOK);
53657bf258eSGarrett Wollman 	if (unp == NULL)
537df8bae1dSRodney W. Grimes 		return (ENOBUFS);
53857bf258eSGarrett Wollman 	bzero(unp, sizeof *unp);
53998271db4SGarrett Wollman 	unp->unp_gencnt = ++unp_gencnt;
54098271db4SGarrett Wollman 	unp_count++;
54198271db4SGarrett Wollman 	LIST_INIT(&unp->unp_refs);
542df8bae1dSRodney W. Grimes 	unp->unp_socket = so;
543426da3bcSAlfred Perlstein 	FILEDESC_LOCK(curproc->p_fd);
544b40ce416SJulian Elischer 	unp->unp_rvnode = curthread->td_proc->p_fd->fd_rdir;
545426da3bcSAlfred Perlstein 	FILEDESC_UNLOCK(curproc->p_fd);
54698271db4SGarrett Wollman 	LIST_INSERT_HEAD(so->so_type == SOCK_DGRAM ? &unp_dhead
54798271db4SGarrett Wollman 			 : &unp_shead, unp, unp_link);
548210a5a71SAlfred Perlstein 	so->so_pcb = unp;
549df8bae1dSRodney W. Grimes 	return (0);
550df8bae1dSRodney W. Grimes }
551df8bae1dSRodney W. Grimes 
552f708ef1bSPoul-Henning Kamp static void
553df8bae1dSRodney W. Grimes unp_detach(unp)
554df8bae1dSRodney W. Grimes 	register struct unpcb *unp;
555df8bae1dSRodney W. Grimes {
55698271db4SGarrett Wollman 	LIST_REMOVE(unp, unp_link);
55798271db4SGarrett Wollman 	unp->unp_gencnt = ++unp_gencnt;
55898271db4SGarrett Wollman 	--unp_count;
559df8bae1dSRodney W. Grimes 	if (unp->unp_vnode) {
560df8bae1dSRodney W. Grimes 		unp->unp_vnode->v_socket = 0;
561df8bae1dSRodney W. Grimes 		vrele(unp->unp_vnode);
562df8bae1dSRodney W. Grimes 		unp->unp_vnode = 0;
563df8bae1dSRodney W. Grimes 	}
564df8bae1dSRodney W. Grimes 	if (unp->unp_conn)
565df8bae1dSRodney W. Grimes 		unp_disconnect(unp);
5662e3c8fcbSPoul-Henning Kamp 	while (!LIST_EMPTY(&unp->unp_refs))
5672e3c8fcbSPoul-Henning Kamp 		unp_drop(LIST_FIRST(&unp->unp_refs), ECONNRESET);
568df8bae1dSRodney W. Grimes 	soisdisconnected(unp->unp_socket);
569df8bae1dSRodney W. Grimes 	unp->unp_socket->so_pcb = 0;
570df8bae1dSRodney W. Grimes 	if (unp_rights) {
571df8bae1dSRodney W. Grimes 		/*
572df8bae1dSRodney W. Grimes 		 * Normally the receive buffer is flushed later,
573df8bae1dSRodney W. Grimes 		 * in sofree, but if our receive buffer holds references
574df8bae1dSRodney W. Grimes 		 * to descriptors that are now garbage, we will dispose
575df8bae1dSRodney W. Grimes 		 * of those descriptor references after the garbage collector
576df8bae1dSRodney W. Grimes 		 * gets them (resulting in a "panic: closef: count < 0").
577df8bae1dSRodney W. Grimes 		 */
578df8bae1dSRodney W. Grimes 		sorflush(unp->unp_socket);
579df8bae1dSRodney W. Grimes 		unp_gc();
580df8bae1dSRodney W. Grimes 	}
58157bf258eSGarrett Wollman 	if (unp->unp_addr)
58257bf258eSGarrett Wollman 		FREE(unp->unp_addr, M_SONAME);
5839e9d298aSJeff Roberson 	uma_zfree(unp_zone, unp);
584df8bae1dSRodney W. Grimes }
585df8bae1dSRodney W. Grimes 
586f708ef1bSPoul-Henning Kamp static int
587b40ce416SJulian Elischer unp_bind(unp, nam, td)
588df8bae1dSRodney W. Grimes 	struct unpcb *unp;
58957bf258eSGarrett Wollman 	struct sockaddr *nam;
590b40ce416SJulian Elischer 	struct thread *td;
591df8bae1dSRodney W. Grimes {
59257bf258eSGarrett Wollman 	struct sockaddr_un *soun = (struct sockaddr_un *)nam;
593f2a2857bSKirk McKusick 	struct vnode *vp;
594f2a2857bSKirk McKusick 	struct mount *mp;
595df8bae1dSRodney W. Grimes 	struct vattr vattr;
59657bf258eSGarrett Wollman 	int error, namelen;
597df8bae1dSRodney W. Grimes 	struct nameidata nd;
5988f364875SJulian Elischer 	char *buf;
599df8bae1dSRodney W. Grimes 
600df8bae1dSRodney W. Grimes 	if (unp->unp_vnode != NULL)
601df8bae1dSRodney W. Grimes 		return (EINVAL);
60257bf258eSGarrett Wollman 	namelen = soun->sun_len - offsetof(struct sockaddr_un, sun_path);
60357bf258eSGarrett Wollman 	if (namelen <= 0)
60457bf258eSGarrett Wollman 		return EINVAL;
6058f364875SJulian Elischer 	buf = malloc(SOCK_MAXADDRLEN, M_TEMP, M_WAITOK);
60657bf258eSGarrett Wollman 	strncpy(buf, soun->sun_path, namelen);
60757bf258eSGarrett Wollman 	buf[namelen] = 0;	/* null-terminate the string */
608f2a2857bSKirk McKusick restart:
609974784e8SGuido van Rooij 	NDINIT(&nd, CREATE, NOFOLLOW | LOCKPARENT, UIO_SYSSPACE,
610b40ce416SJulian Elischer 	    buf, td);
611df8bae1dSRodney W. Grimes /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */
612797f2d22SPoul-Henning Kamp 	error = namei(&nd);
6138f364875SJulian Elischer 	if (error) {
6148f364875SJulian Elischer 		free(buf, M_TEMP);
615ad4ff090SJulian Elischer 		return (error);
6168f364875SJulian Elischer 	}
617df8bae1dSRodney W. Grimes 	vp = nd.ni_vp;
618f2a2857bSKirk McKusick 	if (vp != NULL || vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
619762e6b85SEivind Eklund 		NDFREE(&nd, NDF_ONLY_PNBUF);
620df8bae1dSRodney W. Grimes 		if (nd.ni_dvp == vp)
621df8bae1dSRodney W. Grimes 			vrele(nd.ni_dvp);
622df8bae1dSRodney W. Grimes 		else
623df8bae1dSRodney W. Grimes 			vput(nd.ni_dvp);
624f2a2857bSKirk McKusick 		if (vp != NULL) {
625df8bae1dSRodney W. Grimes 			vrele(vp);
6268f364875SJulian Elischer 			free(buf, M_TEMP);
627df8bae1dSRodney W. Grimes 			return (EADDRINUSE);
628df8bae1dSRodney W. Grimes 		}
6298f364875SJulian Elischer 		error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH);
6308f364875SJulian Elischer 		if (error) {
6318f364875SJulian Elischer 			free(buf, M_TEMP);
632f2a2857bSKirk McKusick 			return (error);
6338f364875SJulian Elischer 		}
634f2a2857bSKirk McKusick 		goto restart;
635f2a2857bSKirk McKusick 	}
636df8bae1dSRodney W. Grimes 	VATTR_NULL(&vattr);
637df8bae1dSRodney W. Grimes 	vattr.va_type = VSOCK;
638426da3bcSAlfred Perlstein 	FILEDESC_LOCK(td->td_proc->p_fd);
639b40ce416SJulian Elischer 	vattr.va_mode = (ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask);
640426da3bcSAlfred Perlstein 	FILEDESC_UNLOCK(td->td_proc->p_fd);
641a854ed98SJohn Baldwin 	VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
6427be2d300SMike Smith 	error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
643762e6b85SEivind Eklund 	NDFREE(&nd, NDF_ONLY_PNBUF);
6447be2d300SMike Smith 	vput(nd.ni_dvp);
6458f364875SJulian Elischer 	if (error) {
6468f364875SJulian Elischer 		free(buf, M_TEMP);
647df8bae1dSRodney W. Grimes 		return (error);
6488f364875SJulian Elischer 	}
649df8bae1dSRodney W. Grimes 	vp = nd.ni_vp;
650df8bae1dSRodney W. Grimes 	vp->v_socket = unp->unp_socket;
651df8bae1dSRodney W. Grimes 	unp->unp_vnode = vp;
65257bf258eSGarrett Wollman 	unp->unp_addr = (struct sockaddr_un *)dup_sockaddr(nam, 1);
653b40ce416SJulian Elischer 	VOP_UNLOCK(vp, 0, td);
654f2a2857bSKirk McKusick 	vn_finished_write(mp);
6558f364875SJulian Elischer 	free(buf, M_TEMP);
656df8bae1dSRodney W. Grimes 	return (0);
657df8bae1dSRodney W. Grimes }
658df8bae1dSRodney W. Grimes 
659f708ef1bSPoul-Henning Kamp static int
660b40ce416SJulian Elischer unp_connect(so, nam, td)
661df8bae1dSRodney W. Grimes 	struct socket *so;
66257bf258eSGarrett Wollman 	struct sockaddr *nam;
663b40ce416SJulian Elischer 	struct thread *td;
664df8bae1dSRodney W. Grimes {
66557bf258eSGarrett Wollman 	register struct sockaddr_un *soun = (struct sockaddr_un *)nam;
666df8bae1dSRodney W. Grimes 	register struct vnode *vp;
667df8bae1dSRodney W. Grimes 	register struct socket *so2, *so3;
6680c1bb4fbSDima Dorfman 	struct unpcb *unp, *unp2, *unp3;
66957bf258eSGarrett Wollman 	int error, len;
670df8bae1dSRodney W. Grimes 	struct nameidata nd;
67157bf258eSGarrett Wollman 	char buf[SOCK_MAXADDRLEN];
672df8bae1dSRodney W. Grimes 
67357bf258eSGarrett Wollman 	len = nam->sa_len - offsetof(struct sockaddr_un, sun_path);
67457bf258eSGarrett Wollman 	if (len <= 0)
67557bf258eSGarrett Wollman 		return EINVAL;
67657bf258eSGarrett Wollman 	strncpy(buf, soun->sun_path, len);
67757bf258eSGarrett Wollman 	buf[len] = 0;
67857bf258eSGarrett Wollman 
679b40ce416SJulian Elischer 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, buf, td);
680797f2d22SPoul-Henning Kamp 	error = namei(&nd);
681797f2d22SPoul-Henning Kamp 	if (error)
682df8bae1dSRodney W. Grimes 		return (error);
683df8bae1dSRodney W. Grimes 	vp = nd.ni_vp;
684762e6b85SEivind Eklund 	NDFREE(&nd, NDF_ONLY_PNBUF);
685df8bae1dSRodney W. Grimes 	if (vp->v_type != VSOCK) {
686df8bae1dSRodney W. Grimes 		error = ENOTSOCK;
687df8bae1dSRodney W. Grimes 		goto bad;
688df8bae1dSRodney W. Grimes 	}
689a854ed98SJohn Baldwin 	error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td);
690797f2d22SPoul-Henning Kamp 	if (error)
691df8bae1dSRodney W. Grimes 		goto bad;
692df8bae1dSRodney W. Grimes 	so2 = vp->v_socket;
693df8bae1dSRodney W. Grimes 	if (so2 == 0) {
694df8bae1dSRodney W. Grimes 		error = ECONNREFUSED;
695df8bae1dSRodney W. Grimes 		goto bad;
696df8bae1dSRodney W. Grimes 	}
697df8bae1dSRodney W. Grimes 	if (so->so_type != so2->so_type) {
698df8bae1dSRodney W. Grimes 		error = EPROTOTYPE;
699df8bae1dSRodney W. Grimes 		goto bad;
700df8bae1dSRodney W. Grimes 	}
701df8bae1dSRodney W. Grimes 	if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
7024cc20ab1SSeigo Tanimura 		if ((so2->so_options & SO_ACCEPTCONN) == 0 ||
7034cc20ab1SSeigo Tanimura 		    (so3 = sonewconn(so2, 0)) == 0) {
704df8bae1dSRodney W. Grimes 			error = ECONNREFUSED;
705df8bae1dSRodney W. Grimes 			goto bad;
706df8bae1dSRodney W. Grimes 		}
7070c1bb4fbSDima Dorfman 		unp = sotounpcb(so);
708df8bae1dSRodney W. Grimes 		unp2 = sotounpcb(so2);
709df8bae1dSRodney W. Grimes 		unp3 = sotounpcb(so3);
710df8bae1dSRodney W. Grimes 		if (unp2->unp_addr)
71157bf258eSGarrett Wollman 			unp3->unp_addr = (struct sockaddr_un *)
71257bf258eSGarrett Wollman 				dup_sockaddr((struct sockaddr *)
71357bf258eSGarrett Wollman 					     unp2->unp_addr, 1);
7140c1bb4fbSDima Dorfman 
7150c1bb4fbSDima Dorfman 		/*
7160c1bb4fbSDima Dorfman 		 * unp_peercred management:
7170c1bb4fbSDima Dorfman 		 *
7180c1bb4fbSDima Dorfman 		 * The connecter's (client's) credentials are copied
7190c1bb4fbSDima Dorfman 		 * from its process structure at the time of connect()
7200c1bb4fbSDima Dorfman 		 * (which is now).
7210c1bb4fbSDima Dorfman 		 */
722a854ed98SJohn Baldwin 		cru2x(td->td_ucred, &unp3->unp_peercred);
7230c1bb4fbSDima Dorfman 		unp3->unp_flags |= UNP_HAVEPC;
7240c1bb4fbSDima Dorfman 		/*
7250c1bb4fbSDima Dorfman 		 * The receiver's (server's) credentials are copied
7260c1bb4fbSDima Dorfman 		 * from the unp_peercred member of socket on which the
7270c1bb4fbSDima Dorfman 		 * former called listen(); unp_listen() cached that
7280c1bb4fbSDima Dorfman 		 * process's credentials at that time so we can use
7290c1bb4fbSDima Dorfman 		 * them now.
7300c1bb4fbSDima Dorfman 		 */
7310c1bb4fbSDima Dorfman 		KASSERT(unp2->unp_flags & UNP_HAVEPCCACHED,
7320c1bb4fbSDima Dorfman 		    ("unp_connect: listener without cached peercred"));
7330c1bb4fbSDima Dorfman 		memcpy(&unp->unp_peercred, &unp2->unp_peercred,
7340c1bb4fbSDima Dorfman 		    sizeof(unp->unp_peercred));
7350c1bb4fbSDima Dorfman 		unp->unp_flags |= UNP_HAVEPC;
736335654d7SRobert Watson #ifdef MAC
737335654d7SRobert Watson 		mac_set_socket_peer_from_socket(so, so3);
738335654d7SRobert Watson 		mac_set_socket_peer_from_socket(so3, so);
739335654d7SRobert Watson #endif
7400c1bb4fbSDima Dorfman 
741df8bae1dSRodney W. Grimes 		so2 = so3;
742df8bae1dSRodney W. Grimes 	}
743df8bae1dSRodney W. Grimes 	error = unp_connect2(so, so2);
744df8bae1dSRodney W. Grimes bad:
745df8bae1dSRodney W. Grimes 	vput(vp);
746df8bae1dSRodney W. Grimes 	return (error);
747df8bae1dSRodney W. Grimes }
748df8bae1dSRodney W. Grimes 
74926f9a767SRodney W. Grimes int
750df8bae1dSRodney W. Grimes unp_connect2(so, so2)
751df8bae1dSRodney W. Grimes 	register struct socket *so;
752df8bae1dSRodney W. Grimes 	register struct socket *so2;
753df8bae1dSRodney W. Grimes {
754df8bae1dSRodney W. Grimes 	register struct unpcb *unp = sotounpcb(so);
755df8bae1dSRodney W. Grimes 	register struct unpcb *unp2;
756df8bae1dSRodney W. Grimes 
757df8bae1dSRodney W. Grimes 	if (so2->so_type != so->so_type)
758df8bae1dSRodney W. Grimes 		return (EPROTOTYPE);
759df8bae1dSRodney W. Grimes 	unp2 = sotounpcb(so2);
760df8bae1dSRodney W. Grimes 	unp->unp_conn = unp2;
761df8bae1dSRodney W. Grimes 	switch (so->so_type) {
762df8bae1dSRodney W. Grimes 
763df8bae1dSRodney W. Grimes 	case SOCK_DGRAM:
76498271db4SGarrett Wollman 		LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink);
765df8bae1dSRodney W. Grimes 		soisconnected(so);
766df8bae1dSRodney W. Grimes 		break;
767df8bae1dSRodney W. Grimes 
768df8bae1dSRodney W. Grimes 	case SOCK_STREAM:
769df8bae1dSRodney W. Grimes 		unp2->unp_conn = unp;
770df8bae1dSRodney W. Grimes 		soisconnected(so);
771df8bae1dSRodney W. Grimes 		soisconnected(so2);
772df8bae1dSRodney W. Grimes 		break;
773df8bae1dSRodney W. Grimes 
774df8bae1dSRodney W. Grimes 	default:
775df8bae1dSRodney W. Grimes 		panic("unp_connect2");
776df8bae1dSRodney W. Grimes 	}
777df8bae1dSRodney W. Grimes 	return (0);
778df8bae1dSRodney W. Grimes }
779df8bae1dSRodney W. Grimes 
780f708ef1bSPoul-Henning Kamp static void
781df8bae1dSRodney W. Grimes unp_disconnect(unp)
782df8bae1dSRodney W. Grimes 	struct unpcb *unp;
783df8bae1dSRodney W. Grimes {
784df8bae1dSRodney W. Grimes 	register struct unpcb *unp2 = unp->unp_conn;
785df8bae1dSRodney W. Grimes 
786df8bae1dSRodney W. Grimes 	if (unp2 == 0)
787df8bae1dSRodney W. Grimes 		return;
788df8bae1dSRodney W. Grimes 	unp->unp_conn = 0;
789df8bae1dSRodney W. Grimes 	switch (unp->unp_socket->so_type) {
790df8bae1dSRodney W. Grimes 
791df8bae1dSRodney W. Grimes 	case SOCK_DGRAM:
79298271db4SGarrett Wollman 		LIST_REMOVE(unp, unp_reflink);
793df8bae1dSRodney W. Grimes 		unp->unp_socket->so_state &= ~SS_ISCONNECTED;
794df8bae1dSRodney W. Grimes 		break;
795df8bae1dSRodney W. Grimes 
796df8bae1dSRodney W. Grimes 	case SOCK_STREAM:
797df8bae1dSRodney W. Grimes 		soisdisconnected(unp->unp_socket);
798df8bae1dSRodney W. Grimes 		unp2->unp_conn = 0;
799df8bae1dSRodney W. Grimes 		soisdisconnected(unp2->unp_socket);
800df8bae1dSRodney W. Grimes 		break;
801df8bae1dSRodney W. Grimes 	}
802df8bae1dSRodney W. Grimes }
803df8bae1dSRodney W. Grimes 
804df8bae1dSRodney W. Grimes #ifdef notdef
80526f9a767SRodney W. Grimes void
806df8bae1dSRodney W. Grimes unp_abort(unp)
807df8bae1dSRodney W. Grimes 	struct unpcb *unp;
808df8bae1dSRodney W. Grimes {
809df8bae1dSRodney W. Grimes 
810df8bae1dSRodney W. Grimes 	unp_detach(unp);
811df8bae1dSRodney W. Grimes }
812df8bae1dSRodney W. Grimes #endif
813df8bae1dSRodney W. Grimes 
81498271db4SGarrett Wollman static int
81582d9ae4eSPoul-Henning Kamp unp_pcblist(SYSCTL_HANDLER_ARGS)
81698271db4SGarrett Wollman {
817f5ef029eSPoul-Henning Kamp 	int error, i, n;
81898271db4SGarrett Wollman 	struct unpcb *unp, **unp_list;
81998271db4SGarrett Wollman 	unp_gen_t gencnt;
8208f364875SJulian Elischer 	struct xunpgen *xug;
82198271db4SGarrett Wollman 	struct unp_head *head;
8228f364875SJulian Elischer 	struct xunpcb *xu;
82398271db4SGarrett Wollman 
824a23d65bfSBruce Evans 	head = ((intptr_t)arg1 == SOCK_DGRAM ? &unp_dhead : &unp_shead);
82598271db4SGarrett Wollman 
82698271db4SGarrett Wollman 	/*
82798271db4SGarrett Wollman 	 * The process of preparing the PCB list is too time-consuming and
82898271db4SGarrett Wollman 	 * resource-intensive to repeat twice on every request.
82998271db4SGarrett Wollman 	 */
83098271db4SGarrett Wollman 	if (req->oldptr == 0) {
83198271db4SGarrett Wollman 		n = unp_count;
8328f364875SJulian Elischer 		req->oldidx = 2 * (sizeof *xug)
83398271db4SGarrett Wollman 			+ (n + n/8) * sizeof(struct xunpcb);
83498271db4SGarrett Wollman 		return 0;
83598271db4SGarrett Wollman 	}
83698271db4SGarrett Wollman 
83798271db4SGarrett Wollman 	if (req->newptr != 0)
83898271db4SGarrett Wollman 		return EPERM;
83998271db4SGarrett Wollman 
84098271db4SGarrett Wollman 	/*
84198271db4SGarrett Wollman 	 * OK, now we're committed to doing something.
84298271db4SGarrett Wollman 	 */
8438f364875SJulian Elischer 	xug = malloc(sizeof(*xug), M_TEMP, M_WAITOK);
84498271db4SGarrett Wollman 	gencnt = unp_gencnt;
84598271db4SGarrett Wollman 	n = unp_count;
84698271db4SGarrett Wollman 
8478f364875SJulian Elischer 	xug->xug_len = sizeof *xug;
8488f364875SJulian Elischer 	xug->xug_count = n;
8498f364875SJulian Elischer 	xug->xug_gen = gencnt;
8508f364875SJulian Elischer 	xug->xug_sogen = so_gencnt;
8518f364875SJulian Elischer 	error = SYSCTL_OUT(req, xug, sizeof *xug);
8528f364875SJulian Elischer 	if (error) {
8538f364875SJulian Elischer 		free(xug, M_TEMP);
85498271db4SGarrett Wollman 		return error;
8558f364875SJulian Elischer 	}
85698271db4SGarrett Wollman 
85798271db4SGarrett Wollman 	unp_list = malloc(n * sizeof *unp_list, M_TEMP, M_WAITOK);
85898271db4SGarrett Wollman 
8592e3c8fcbSPoul-Henning Kamp 	for (unp = LIST_FIRST(head), i = 0; unp && i < n;
8602e3c8fcbSPoul-Henning Kamp 	     unp = LIST_NEXT(unp, unp_link)) {
8618a7d8cc6SRobert Watson 		if (unp->unp_gencnt <= gencnt) {
862a854ed98SJohn Baldwin 			if (cr_cansee(req->td->td_ucred,
8638a7d8cc6SRobert Watson 			    unp->unp_socket->so_cred))
8644787fd37SPaul Saab 				continue;
86598271db4SGarrett Wollman 			unp_list[i++] = unp;
86698271db4SGarrett Wollman 		}
8674787fd37SPaul Saab 	}
86898271db4SGarrett Wollman 	n = i;			/* in case we lost some during malloc */
86998271db4SGarrett Wollman 
87098271db4SGarrett Wollman 	error = 0;
8718f364875SJulian Elischer 	xu = malloc(sizeof(*xu), M_TEMP, M_WAITOK);
87298271db4SGarrett Wollman 	for (i = 0; i < n; i++) {
87398271db4SGarrett Wollman 		unp = unp_list[i];
87498271db4SGarrett Wollman 		if (unp->unp_gencnt <= gencnt) {
8758f364875SJulian Elischer 			xu->xu_len = sizeof *xu;
8768f364875SJulian Elischer 			xu->xu_unpp = unp;
87798271db4SGarrett Wollman 			/*
87898271db4SGarrett Wollman 			 * XXX - need more locking here to protect against
87998271db4SGarrett Wollman 			 * connect/disconnect races for SMP.
88098271db4SGarrett Wollman 			 */
88198271db4SGarrett Wollman 			if (unp->unp_addr)
8828f364875SJulian Elischer 				bcopy(unp->unp_addr, &xu->xu_addr,
88398271db4SGarrett Wollman 				      unp->unp_addr->sun_len);
88498271db4SGarrett Wollman 			if (unp->unp_conn && unp->unp_conn->unp_addr)
88598271db4SGarrett Wollman 				bcopy(unp->unp_conn->unp_addr,
8868f364875SJulian Elischer 				      &xu->xu_caddr,
88798271db4SGarrett Wollman 				      unp->unp_conn->unp_addr->sun_len);
8888f364875SJulian Elischer 			bcopy(unp, &xu->xu_unp, sizeof *unp);
8898f364875SJulian Elischer 			sotoxsocket(unp->unp_socket, &xu->xu_socket);
8908f364875SJulian Elischer 			error = SYSCTL_OUT(req, xu, sizeof *xu);
89198271db4SGarrett Wollman 		}
89298271db4SGarrett Wollman 	}
8938f364875SJulian Elischer 	free(xu, M_TEMP);
89498271db4SGarrett Wollman 	if (!error) {
89598271db4SGarrett Wollman 		/*
89698271db4SGarrett Wollman 		 * Give the user an updated idea of our state.
89798271db4SGarrett Wollman 		 * If the generation differs from what we told
89898271db4SGarrett Wollman 		 * her before, she knows that something happened
89998271db4SGarrett Wollman 		 * while we were processing this request, and it
90098271db4SGarrett Wollman 		 * might be necessary to retry.
90198271db4SGarrett Wollman 		 */
9028f364875SJulian Elischer 		xug->xug_gen = unp_gencnt;
9038f364875SJulian Elischer 		xug->xug_sogen = so_gencnt;
9048f364875SJulian Elischer 		xug->xug_count = unp_count;
9058f364875SJulian Elischer 		error = SYSCTL_OUT(req, xug, sizeof *xug);
90698271db4SGarrett Wollman 	}
90798271db4SGarrett Wollman 	free(unp_list, M_TEMP);
9088f364875SJulian Elischer 	free(xug, M_TEMP);
90998271db4SGarrett Wollman 	return error;
91098271db4SGarrett Wollman }
91198271db4SGarrett Wollman 
91298271db4SGarrett Wollman SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLFLAG_RD,
91398271db4SGarrett Wollman 	    (caddr_t)(long)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb",
91498271db4SGarrett Wollman 	    "List of active local datagram sockets");
91598271db4SGarrett Wollman SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, CTLFLAG_RD,
91698271db4SGarrett Wollman 	    (caddr_t)(long)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb",
91798271db4SGarrett Wollman 	    "List of active local stream sockets");
91898271db4SGarrett Wollman 
919f708ef1bSPoul-Henning Kamp static void
920df8bae1dSRodney W. Grimes unp_shutdown(unp)
921df8bae1dSRodney W. Grimes 	struct unpcb *unp;
922df8bae1dSRodney W. Grimes {
923df8bae1dSRodney W. Grimes 	struct socket *so;
924df8bae1dSRodney W. Grimes 
925df8bae1dSRodney W. Grimes 	if (unp->unp_socket->so_type == SOCK_STREAM && unp->unp_conn &&
926df8bae1dSRodney W. Grimes 	    (so = unp->unp_conn->unp_socket))
927df8bae1dSRodney W. Grimes 		socantrcvmore(so);
928df8bae1dSRodney W. Grimes }
929df8bae1dSRodney W. Grimes 
930f708ef1bSPoul-Henning Kamp static void
931df8bae1dSRodney W. Grimes unp_drop(unp, errno)
932df8bae1dSRodney W. Grimes 	struct unpcb *unp;
933df8bae1dSRodney W. Grimes 	int errno;
934df8bae1dSRodney W. Grimes {
935df8bae1dSRodney W. Grimes 	struct socket *so = unp->unp_socket;
936df8bae1dSRodney W. Grimes 
937df8bae1dSRodney W. Grimes 	so->so_error = errno;
938df8bae1dSRodney W. Grimes 	unp_disconnect(unp);
939df8bae1dSRodney W. Grimes }
940df8bae1dSRodney W. Grimes 
941df8bae1dSRodney W. Grimes #ifdef notdef
94226f9a767SRodney W. Grimes void
943df8bae1dSRodney W. Grimes unp_drain()
944df8bae1dSRodney W. Grimes {
945df8bae1dSRodney W. Grimes 
946df8bae1dSRodney W. Grimes }
947df8bae1dSRodney W. Grimes #endif
948df8bae1dSRodney W. Grimes 
9492bc21ed9SDavid Malone static void
9502bc21ed9SDavid Malone unp_freerights(rp, fdcount)
9512bc21ed9SDavid Malone 	struct file **rp;
9522bc21ed9SDavid Malone 	int fdcount;
953df8bae1dSRodney W. Grimes {
9542bc21ed9SDavid Malone 	int i;
9552bc21ed9SDavid Malone 	struct file *fp;
956df8bae1dSRodney W. Grimes 
9572bc21ed9SDavid Malone 	for (i = 0; i < fdcount; i++) {
958df8bae1dSRodney W. Grimes 		fp = *rp;
9598692c025SYoshinobu Inoue 		/*
9602bc21ed9SDavid Malone 		 * zero the pointer before calling
9612bc21ed9SDavid Malone 		 * unp_discard since it may end up
9622bc21ed9SDavid Malone 		 * in unp_gc()..
9638692c025SYoshinobu Inoue 		 */
964df8bae1dSRodney W. Grimes 		*rp++ = 0;
9658692c025SYoshinobu Inoue 		unp_discard(fp);
966df8bae1dSRodney W. Grimes 	}
9672bc21ed9SDavid Malone }
9682bc21ed9SDavid Malone 
9692bc21ed9SDavid Malone int
9702bc21ed9SDavid Malone unp_externalize(control, controlp)
9712bc21ed9SDavid Malone 	struct mbuf *control, **controlp;
9722bc21ed9SDavid Malone {
9732bc21ed9SDavid Malone 	struct thread *td = curthread;		/* XXX */
9742bc21ed9SDavid Malone 	struct cmsghdr *cm = mtod(control, struct cmsghdr *);
9752bc21ed9SDavid Malone 	int i;
9762bc21ed9SDavid Malone 	int *fdp;
9772bc21ed9SDavid Malone 	struct file **rp;
9782bc21ed9SDavid Malone 	struct file *fp;
9792bc21ed9SDavid Malone 	void *data;
9802bc21ed9SDavid Malone 	socklen_t clen = control->m_len, datalen;
9812bc21ed9SDavid Malone 	int error, newfds;
9822bc21ed9SDavid Malone 	int f;
9832bc21ed9SDavid Malone 	u_int newlen;
9842bc21ed9SDavid Malone 
9852bc21ed9SDavid Malone 	error = 0;
9862bc21ed9SDavid Malone 	if (controlp != NULL) /* controlp == NULL => free control messages */
9872bc21ed9SDavid Malone 		*controlp = NULL;
9882bc21ed9SDavid Malone 
9892bc21ed9SDavid Malone 	while (cm != NULL) {
9902bc21ed9SDavid Malone 		if (sizeof(*cm) > clen || cm->cmsg_len > clen) {
9912bc21ed9SDavid Malone 			error = EINVAL;
9922bc21ed9SDavid Malone 			break;
9932bc21ed9SDavid Malone 		}
9942bc21ed9SDavid Malone 
9952bc21ed9SDavid Malone 		data = CMSG_DATA(cm);
9962bc21ed9SDavid Malone 		datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
9972bc21ed9SDavid Malone 
9982bc21ed9SDavid Malone 		if (cm->cmsg_level == SOL_SOCKET
9992bc21ed9SDavid Malone 		    && cm->cmsg_type == SCM_RIGHTS) {
10002bc21ed9SDavid Malone 			newfds = datalen / sizeof(struct file *);
10012bc21ed9SDavid Malone 			rp = data;
10022bc21ed9SDavid Malone 
10032bc21ed9SDavid Malone 			/* If we're not outputting the discriptors free them. */
10042bc21ed9SDavid Malone 			if (error || controlp == NULL) {
10052bc21ed9SDavid Malone 				unp_freerights(rp, newfds);
10062bc21ed9SDavid Malone 				goto next;
10072bc21ed9SDavid Malone 			}
1008426da3bcSAlfred Perlstein 			FILEDESC_LOCK(td->td_proc->p_fd);
10092bc21ed9SDavid Malone 			/* if the new FD's will not fit free them.  */
10102bc21ed9SDavid Malone 			if (!fdavail(td, newfds)) {
1011426da3bcSAlfred Perlstein 				FILEDESC_UNLOCK(td->td_proc->p_fd);
10122bc21ed9SDavid Malone 				error = EMSGSIZE;
10132bc21ed9SDavid Malone 				unp_freerights(rp, newfds);
10142bc21ed9SDavid Malone 				goto next;
1015df8bae1dSRodney W. Grimes 			}
1016ed5b7817SJulian Elischer 			/*
10172bc21ed9SDavid Malone 			 * now change each pointer to an fd in the global
10182bc21ed9SDavid Malone 			 * table to an integer that is the index to the
10192bc21ed9SDavid Malone 			 * local fd table entry that we set up to point
10202bc21ed9SDavid Malone 			 * to the global one we are transferring.
1021ed5b7817SJulian Elischer 			 */
10222bc21ed9SDavid Malone 			newlen = newfds * sizeof(int);
10232bc21ed9SDavid Malone 			*controlp = sbcreatecontrol(NULL, newlen,
10242bc21ed9SDavid Malone 			    SCM_RIGHTS, SOL_SOCKET);
10252bc21ed9SDavid Malone 			if (*controlp == NULL) {
1026426da3bcSAlfred Perlstein 				FILEDESC_UNLOCK(td->td_proc->p_fd);
10272bc21ed9SDavid Malone 				error = E2BIG;
10282bc21ed9SDavid Malone 				unp_freerights(rp, newfds);
10292bc21ed9SDavid Malone 				goto next;
10302bc21ed9SDavid Malone 			}
10312bc21ed9SDavid Malone 
10322bc21ed9SDavid Malone 			fdp = (int *)
10332bc21ed9SDavid Malone 			    CMSG_DATA(mtod(*controlp, struct cmsghdr *));
1034df8bae1dSRodney W. Grimes 			for (i = 0; i < newfds; i++) {
1035b40ce416SJulian Elischer 				if (fdalloc(td, 0, &f))
10362bc21ed9SDavid Malone 					panic("unp_externalize fdalloc failed");
10378692c025SYoshinobu Inoue 				fp = *rp++;
1038b40ce416SJulian Elischer 				td->td_proc->p_fd->fd_ofiles[f] = fp;
1039426da3bcSAlfred Perlstein 				FILE_LOCK(fp);
1040df8bae1dSRodney W. Grimes 				fp->f_msgcount--;
1041426da3bcSAlfred Perlstein 				FILE_UNLOCK(fp);
1042df8bae1dSRodney W. Grimes 				unp_rights--;
10438692c025SYoshinobu Inoue 				*fdp++ = f;
1044df8bae1dSRodney W. Grimes 			}
1045426da3bcSAlfred Perlstein 			FILEDESC_UNLOCK(td->td_proc->p_fd);
10462bc21ed9SDavid Malone 		} else { /* We can just copy anything else across */
10472bc21ed9SDavid Malone 			if (error || controlp == NULL)
10482bc21ed9SDavid Malone 				goto next;
10492bc21ed9SDavid Malone 			*controlp = sbcreatecontrol(NULL, datalen,
10502bc21ed9SDavid Malone 			    cm->cmsg_type, cm->cmsg_level);
10512bc21ed9SDavid Malone 			if (*controlp == NULL) {
10522bc21ed9SDavid Malone 				error = ENOBUFS;
10532bc21ed9SDavid Malone 				goto next;
10542bc21ed9SDavid Malone 			}
10552bc21ed9SDavid Malone 			bcopy(data,
10562bc21ed9SDavid Malone 			    CMSG_DATA(mtod(*controlp, struct cmsghdr *)),
10572bc21ed9SDavid Malone 			    datalen);
10582bc21ed9SDavid Malone 		}
10592bc21ed9SDavid Malone 
10602bc21ed9SDavid Malone 		controlp = &(*controlp)->m_next;
10612bc21ed9SDavid Malone 
10622bc21ed9SDavid Malone next:
10632bc21ed9SDavid Malone 		if (CMSG_SPACE(datalen) < clen) {
10642bc21ed9SDavid Malone 			clen -= CMSG_SPACE(datalen);
10652bc21ed9SDavid Malone 			cm = (struct cmsghdr *)
10662bc21ed9SDavid Malone 			    ((caddr_t)cm + CMSG_SPACE(datalen));
10678692c025SYoshinobu Inoue 		} else {
10682bc21ed9SDavid Malone 			clen = 0;
10692bc21ed9SDavid Malone 			cm = NULL;
10708692c025SYoshinobu Inoue 		}
10718692c025SYoshinobu Inoue 	}
10728692c025SYoshinobu Inoue 
10732bc21ed9SDavid Malone 	m_freem(control);
10742bc21ed9SDavid Malone 
10752bc21ed9SDavid Malone 	return (error);
1076df8bae1dSRodney W. Grimes }
1077df8bae1dSRodney W. Grimes 
107898271db4SGarrett Wollman void
107998271db4SGarrett Wollman unp_init(void)
108098271db4SGarrett Wollman {
10819e9d298aSJeff Roberson 	unp_zone = uma_zcreate("unpcb", sizeof(struct unpcb), NULL, NULL,
10829e9d298aSJeff Roberson 	    NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
1083586c8b6bSJeff Roberson 	uma_zone_set_max(unp_zone, nmbclusters);
108498271db4SGarrett Wollman 	if (unp_zone == 0)
108598271db4SGarrett Wollman 		panic("unp_init");
108698271db4SGarrett Wollman 	LIST_INIT(&unp_dhead);
108798271db4SGarrett Wollman 	LIST_INIT(&unp_shead);
108898271db4SGarrett Wollman }
108998271db4SGarrett Wollman 
10900b788fa1SBill Paul #ifndef MIN
10910b788fa1SBill Paul #define	MIN(a,b) (((a)<(b))?(a):(b))
10920b788fa1SBill Paul #endif
10930b788fa1SBill Paul 
1094f708ef1bSPoul-Henning Kamp static int
10952bc21ed9SDavid Malone unp_internalize(controlp, td)
10962bc21ed9SDavid Malone 	struct mbuf **controlp;
1097b40ce416SJulian Elischer 	struct thread *td;
1098df8bae1dSRodney W. Grimes {
10992bc21ed9SDavid Malone 	struct mbuf *control = *controlp;
1100b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
11018692c025SYoshinobu Inoue 	struct filedesc *fdescp = p->p_fd;
11022bc21ed9SDavid Malone 	struct cmsghdr *cm = mtod(control, struct cmsghdr *);
11032bc21ed9SDavid Malone 	struct cmsgcred *cmcred;
11042bc21ed9SDavid Malone 	struct file **rp;
11052bc21ed9SDavid Malone 	struct file *fp;
11062bc21ed9SDavid Malone 	struct timeval *tv;
11072bc21ed9SDavid Malone 	int i, fd, *fdp;
11082bc21ed9SDavid Malone 	void *data;
11092bc21ed9SDavid Malone 	socklen_t clen = control->m_len, datalen;
11102bc21ed9SDavid Malone 	int error, oldfds;
11118692c025SYoshinobu Inoue 	u_int newlen;
1112df8bae1dSRodney W. Grimes 
11132bc21ed9SDavid Malone 	error = 0;
11142bc21ed9SDavid Malone 	*controlp = NULL;
11150b788fa1SBill Paul 
11162bc21ed9SDavid Malone 	while (cm != NULL) {
11172bc21ed9SDavid Malone 		if (sizeof(*cm) > clen || cm->cmsg_level != SOL_SOCKET
11182bc21ed9SDavid Malone 		    || cm->cmsg_len > clen) {
11192bc21ed9SDavid Malone 			error = EINVAL;
11202bc21ed9SDavid Malone 			goto out;
11212bc21ed9SDavid Malone 		}
11222bc21ed9SDavid Malone 
11232bc21ed9SDavid Malone 		data = CMSG_DATA(cm);
11242bc21ed9SDavid Malone 		datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
11252bc21ed9SDavid Malone 
11262bc21ed9SDavid Malone 		switch (cm->cmsg_type) {
11270b788fa1SBill Paul 		/*
11280b788fa1SBill Paul 		 * Fill in credential information.
11290b788fa1SBill Paul 		 */
11302bc21ed9SDavid Malone 		case SCM_CREDS:
11312bc21ed9SDavid Malone 			*controlp = sbcreatecontrol(NULL, sizeof(*cmcred),
11322bc21ed9SDavid Malone 			    SCM_CREDS, SOL_SOCKET);
11332bc21ed9SDavid Malone 			if (*controlp == NULL) {
11342bc21ed9SDavid Malone 				error = ENOBUFS;
11352bc21ed9SDavid Malone 				goto out;
11362bc21ed9SDavid Malone 			}
11372bc21ed9SDavid Malone 
11382bc21ed9SDavid Malone 			cmcred = (struct cmsgcred *)
11392bc21ed9SDavid Malone 			    CMSG_DATA(mtod(*controlp, struct cmsghdr *));
11400b788fa1SBill Paul 			cmcred->cmcred_pid = p->p_pid;
1141a854ed98SJohn Baldwin 			cmcred->cmcred_uid = td->td_ucred->cr_ruid;
1142a854ed98SJohn Baldwin 			cmcred->cmcred_gid = td->td_ucred->cr_rgid;
1143a854ed98SJohn Baldwin 			cmcred->cmcred_euid = td->td_ucred->cr_uid;
1144a854ed98SJohn Baldwin 			cmcred->cmcred_ngroups = MIN(td->td_ucred->cr_ngroups,
11450b788fa1SBill Paul 							CMGROUP_MAX);
11460b788fa1SBill Paul 			for (i = 0; i < cmcred->cmcred_ngroups; i++)
11472bc21ed9SDavid Malone 				cmcred->cmcred_groups[i] =
1148a854ed98SJohn Baldwin 				    td->td_ucred->cr_groups[i];
11492bc21ed9SDavid Malone 			break;
11500b788fa1SBill Paul 
11512bc21ed9SDavid Malone 		case SCM_RIGHTS:
11522bc21ed9SDavid Malone 			oldfds = datalen / sizeof (int);
1153ed5b7817SJulian Elischer 			/*
11542bc21ed9SDavid Malone 			 * check that all the FDs passed in refer to legal files
1155ed5b7817SJulian Elischer 			 * If not, reject the entire operation.
1156ed5b7817SJulian Elischer 			 */
11572bc21ed9SDavid Malone 			fdp = data;
1158426da3bcSAlfred Perlstein 			FILEDESC_LOCK(fdescp);
1159df8bae1dSRodney W. Grimes 			for (i = 0; i < oldfds; i++) {
11608692c025SYoshinobu Inoue 				fd = *fdp++;
11618692c025SYoshinobu Inoue 				if ((unsigned)fd >= fdescp->fd_nfiles ||
11622bc21ed9SDavid Malone 				    fdescp->fd_ofiles[fd] == NULL) {
1163426da3bcSAlfred Perlstein 					FILEDESC_UNLOCK(fdescp);
11642bc21ed9SDavid Malone 					error = EBADF;
11652bc21ed9SDavid Malone 					goto out;
11662bc21ed9SDavid Malone 				}
1167df8bae1dSRodney W. Grimes 			}
1168ed5b7817SJulian Elischer 			/*
1169ed5b7817SJulian Elischer 			 * Now replace the integer FDs with pointers to
1170ed5b7817SJulian Elischer 			 * the associated global file table entry..
1171ed5b7817SJulian Elischer 			 */
11722bc21ed9SDavid Malone 			newlen = oldfds * sizeof(struct file *);
11732bc21ed9SDavid Malone 			*controlp = sbcreatecontrol(NULL, newlen,
11742bc21ed9SDavid Malone 			    SCM_RIGHTS, SOL_SOCKET);
11752bc21ed9SDavid Malone 			if (*controlp == NULL) {
1176426da3bcSAlfred Perlstein 				FILEDESC_UNLOCK(fdescp);
11772bc21ed9SDavid Malone 				error = E2BIG;
11782bc21ed9SDavid Malone 				goto out;
11798692c025SYoshinobu Inoue 			}
11808692c025SYoshinobu Inoue 
11812bc21ed9SDavid Malone 			fdp = data;
11822bc21ed9SDavid Malone 			rp = (struct file **)
11832bc21ed9SDavid Malone 			    CMSG_DATA(mtod(*controlp, struct cmsghdr *));
11848692c025SYoshinobu Inoue 			for (i = 0; i < oldfds; i++) {
11858692c025SYoshinobu Inoue 				fp = fdescp->fd_ofiles[*fdp++];
1186df8bae1dSRodney W. Grimes 				*rp++ = fp;
1187426da3bcSAlfred Perlstein 				FILE_LOCK(fp);
1188df8bae1dSRodney W. Grimes 				fp->f_count++;
1189df8bae1dSRodney W. Grimes 				fp->f_msgcount++;
1190426da3bcSAlfred Perlstein 				FILE_UNLOCK(fp);
1191df8bae1dSRodney W. Grimes 				unp_rights++;
1192df8bae1dSRodney W. Grimes 			}
1193426da3bcSAlfred Perlstein 			FILEDESC_UNLOCK(fdescp);
11942bc21ed9SDavid Malone 			break;
11952bc21ed9SDavid Malone 
11962bc21ed9SDavid Malone 		case SCM_TIMESTAMP:
11972bc21ed9SDavid Malone 			*controlp = sbcreatecontrol(NULL, sizeof(*tv),
11982bc21ed9SDavid Malone 			    SCM_TIMESTAMP, SOL_SOCKET);
11992bc21ed9SDavid Malone 			if (*controlp == NULL) {
12002bc21ed9SDavid Malone 				error = ENOBUFS;
12012bc21ed9SDavid Malone 				goto out;
12028692c025SYoshinobu Inoue 			}
12032bc21ed9SDavid Malone 			tv = (struct timeval *)
12042bc21ed9SDavid Malone 			    CMSG_DATA(mtod(*controlp, struct cmsghdr *));
12052bc21ed9SDavid Malone 			microtime(tv);
12062bc21ed9SDavid Malone 			break;
12072bc21ed9SDavid Malone 
12082bc21ed9SDavid Malone 		default:
12092bc21ed9SDavid Malone 			error = EINVAL;
12102bc21ed9SDavid Malone 			goto out;
12112bc21ed9SDavid Malone 		}
12122bc21ed9SDavid Malone 
12132bc21ed9SDavid Malone 		controlp = &(*controlp)->m_next;
12142bc21ed9SDavid Malone 
12152bc21ed9SDavid Malone 		if (CMSG_SPACE(datalen) < clen) {
12162bc21ed9SDavid Malone 			clen -= CMSG_SPACE(datalen);
12172bc21ed9SDavid Malone 			cm = (struct cmsghdr *)
12182bc21ed9SDavid Malone 			    ((caddr_t)cm + CMSG_SPACE(datalen));
12192bc21ed9SDavid Malone 		} else {
12202bc21ed9SDavid Malone 			clen = 0;
12212bc21ed9SDavid Malone 			cm = NULL;
12222bc21ed9SDavid Malone 		}
12232bc21ed9SDavid Malone 	}
12242bc21ed9SDavid Malone 
12252bc21ed9SDavid Malone out:
12262bc21ed9SDavid Malone 	m_freem(control);
12272bc21ed9SDavid Malone 
12282bc21ed9SDavid Malone 	return (error);
1229df8bae1dSRodney W. Grimes }
1230df8bae1dSRodney W. Grimes 
1231f708ef1bSPoul-Henning Kamp static int	unp_defer, unp_gcing;
1232df8bae1dSRodney W. Grimes 
1233f708ef1bSPoul-Henning Kamp static void
1234df8bae1dSRodney W. Grimes unp_gc()
1235df8bae1dSRodney W. Grimes {
1236df8bae1dSRodney W. Grimes 	register struct file *fp, *nextfp;
1237df8bae1dSRodney W. Grimes 	register struct socket *so;
1238df8bae1dSRodney W. Grimes 	struct file **extra_ref, **fpp;
1239df8bae1dSRodney W. Grimes 	int nunref, i;
1240df8bae1dSRodney W. Grimes 
1241df8bae1dSRodney W. Grimes 	if (unp_gcing)
1242df8bae1dSRodney W. Grimes 		return;
1243df8bae1dSRodney W. Grimes 	unp_gcing = 1;
1244df8bae1dSRodney W. Grimes 	unp_defer = 0;
1245ed5b7817SJulian Elischer 	/*
1246ed5b7817SJulian Elischer 	 * before going through all this, set all FDs to
1247ed5b7817SJulian Elischer 	 * be NOT defered and NOT externally accessible
1248ed5b7817SJulian Elischer 	 */
1249426da3bcSAlfred Perlstein 	sx_slock(&filelist_lock);
12502e3c8fcbSPoul-Henning Kamp 	LIST_FOREACH(fp, &filehead, f_list)
1251426da3bcSAlfred Perlstein 		fp->f_gcflag &= ~(FMARK|FDEFER);
1252df8bae1dSRodney W. Grimes 	do {
12532e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(fp, &filehead, f_list) {
1254426da3bcSAlfred Perlstein 			FILE_LOCK(fp);
1255ed5b7817SJulian Elischer 			/*
1256ed5b7817SJulian Elischer 			 * If the file is not open, skip it
1257ed5b7817SJulian Elischer 			 */
1258426da3bcSAlfred Perlstein 			if (fp->f_count == 0) {
1259426da3bcSAlfred Perlstein 				FILE_UNLOCK(fp);
1260df8bae1dSRodney W. Grimes 				continue;
1261426da3bcSAlfred Perlstein 			}
1262ed5b7817SJulian Elischer 			/*
1263ed5b7817SJulian Elischer 			 * If we already marked it as 'defer'  in a
1264ed5b7817SJulian Elischer 			 * previous pass, then try process it this time
1265ed5b7817SJulian Elischer 			 * and un-mark it
1266ed5b7817SJulian Elischer 			 */
1267426da3bcSAlfred Perlstein 			if (fp->f_gcflag & FDEFER) {
1268426da3bcSAlfred Perlstein 				fp->f_gcflag &= ~FDEFER;
1269df8bae1dSRodney W. Grimes 				unp_defer--;
1270df8bae1dSRodney W. Grimes 			} else {
1271ed5b7817SJulian Elischer 				/*
1272ed5b7817SJulian Elischer 				 * if it's not defered, then check if it's
1273ed5b7817SJulian Elischer 				 * already marked.. if so skip it
1274ed5b7817SJulian Elischer 				 */
1275426da3bcSAlfred Perlstein 				if (fp->f_gcflag & FMARK) {
1276426da3bcSAlfred Perlstein 					FILE_UNLOCK(fp);
1277df8bae1dSRodney W. Grimes 					continue;
1278426da3bcSAlfred Perlstein 				}
1279ed5b7817SJulian Elischer 				/*
1280ed5b7817SJulian Elischer 				 * If all references are from messages
1281ed5b7817SJulian Elischer 				 * in transit, then skip it. it's not
1282ed5b7817SJulian Elischer 				 * externally accessible.
1283ed5b7817SJulian Elischer 				 */
1284426da3bcSAlfred Perlstein 				if (fp->f_count == fp->f_msgcount) {
1285426da3bcSAlfred Perlstein 					FILE_UNLOCK(fp);
1286df8bae1dSRodney W. Grimes 					continue;
1287426da3bcSAlfred Perlstein 				}
1288ed5b7817SJulian Elischer 				/*
1289ed5b7817SJulian Elischer 				 * If it got this far then it must be
1290ed5b7817SJulian Elischer 				 * externally accessible.
1291ed5b7817SJulian Elischer 				 */
1292426da3bcSAlfred Perlstein 				fp->f_gcflag |= FMARK;
1293df8bae1dSRodney W. Grimes 			}
1294ed5b7817SJulian Elischer 			/*
1295ed5b7817SJulian Elischer 			 * either it was defered, or it is externally
1296ed5b7817SJulian Elischer 			 * accessible and not already marked so.
1297ed5b7817SJulian Elischer 			 * Now check if it is possibly one of OUR sockets.
1298ed5b7817SJulian Elischer 			 */
1299df8bae1dSRodney W. Grimes 			if (fp->f_type != DTYPE_SOCKET ||
1300426da3bcSAlfred Perlstein 			    (so = (struct socket *)fp->f_data) == 0) {
1301426da3bcSAlfred Perlstein 				FILE_UNLOCK(fp);
1302df8bae1dSRodney W. Grimes 				continue;
1303426da3bcSAlfred Perlstein 			}
1304426da3bcSAlfred Perlstein 			FILE_UNLOCK(fp);
1305748e0b0aSGarrett Wollman 			if (so->so_proto->pr_domain != &localdomain ||
1306df8bae1dSRodney W. Grimes 			    (so->so_proto->pr_flags&PR_RIGHTS) == 0)
1307df8bae1dSRodney W. Grimes 				continue;
1308df8bae1dSRodney W. Grimes #ifdef notdef
1309df8bae1dSRodney W. Grimes 			if (so->so_rcv.sb_flags & SB_LOCK) {
1310df8bae1dSRodney W. Grimes 				/*
1311df8bae1dSRodney W. Grimes 				 * This is problematical; it's not clear
1312df8bae1dSRodney W. Grimes 				 * we need to wait for the sockbuf to be
1313df8bae1dSRodney W. Grimes 				 * unlocked (on a uniprocessor, at least),
1314df8bae1dSRodney W. Grimes 				 * and it's also not clear what to do
1315df8bae1dSRodney W. Grimes 				 * if sbwait returns an error due to receipt
1316df8bae1dSRodney W. Grimes 				 * of a signal.  If sbwait does return
1317df8bae1dSRodney W. Grimes 				 * an error, we'll go into an infinite
1318df8bae1dSRodney W. Grimes 				 * loop.  Delete all of this for now.
1319df8bae1dSRodney W. Grimes 				 */
1320df8bae1dSRodney W. Grimes 				(void) sbwait(&so->so_rcv);
1321df8bae1dSRodney W. Grimes 				goto restart;
1322df8bae1dSRodney W. Grimes 			}
1323df8bae1dSRodney W. Grimes #endif
1324ed5b7817SJulian Elischer 			/*
1325ed5b7817SJulian Elischer 			 * So, Ok, it's one of our sockets and it IS externally
1326ed5b7817SJulian Elischer 			 * accessible (or was defered). Now we look
1327dc733423SDag-Erling Smørgrav 			 * to see if we hold any file descriptors in its
1328ed5b7817SJulian Elischer 			 * message buffers. Follow those links and mark them
1329ed5b7817SJulian Elischer 			 * as accessible too.
1330ed5b7817SJulian Elischer 			 */
1331df8bae1dSRodney W. Grimes 			unp_scan(so->so_rcv.sb_mb, unp_mark);
1332df8bae1dSRodney W. Grimes 		}
1333df8bae1dSRodney W. Grimes 	} while (unp_defer);
1334426da3bcSAlfred Perlstein 	sx_sunlock(&filelist_lock);
1335df8bae1dSRodney W. Grimes 	/*
1336df8bae1dSRodney W. Grimes 	 * We grab an extra reference to each of the file table entries
1337df8bae1dSRodney W. Grimes 	 * that are not otherwise accessible and then free the rights
1338df8bae1dSRodney W. Grimes 	 * that are stored in messages on them.
1339df8bae1dSRodney W. Grimes 	 *
1340df8bae1dSRodney W. Grimes 	 * The bug in the orginal code is a little tricky, so I'll describe
1341df8bae1dSRodney W. Grimes 	 * what's wrong with it here.
1342df8bae1dSRodney W. Grimes 	 *
1343df8bae1dSRodney W. Grimes 	 * It is incorrect to simply unp_discard each entry for f_msgcount
1344df8bae1dSRodney W. Grimes 	 * times -- consider the case of sockets A and B that contain
1345df8bae1dSRodney W. Grimes 	 * references to each other.  On a last close of some other socket,
1346df8bae1dSRodney W. Grimes 	 * we trigger a gc since the number of outstanding rights (unp_rights)
1347df8bae1dSRodney W. Grimes 	 * is non-zero.  If during the sweep phase the gc code un_discards,
1348df8bae1dSRodney W. Grimes 	 * we end up doing a (full) closef on the descriptor.  A closef on A
1349df8bae1dSRodney W. Grimes 	 * results in the following chain.  Closef calls soo_close, which
1350df8bae1dSRodney W. Grimes 	 * calls soclose.   Soclose calls first (through the switch
1351df8bae1dSRodney W. Grimes 	 * uipc_usrreq) unp_detach, which re-invokes unp_gc.  Unp_gc simply
1352df8bae1dSRodney W. Grimes 	 * returns because the previous instance had set unp_gcing, and
1353df8bae1dSRodney W. Grimes 	 * we return all the way back to soclose, which marks the socket
1354df8bae1dSRodney W. Grimes 	 * with SS_NOFDREF, and then calls sofree.  Sofree calls sorflush
1355df8bae1dSRodney W. Grimes 	 * to free up the rights that are queued in messages on the socket A,
1356df8bae1dSRodney W. Grimes 	 * i.e., the reference on B.  The sorflush calls via the dom_dispose
1357df8bae1dSRodney W. Grimes 	 * switch unp_dispose, which unp_scans with unp_discard.  This second
1358df8bae1dSRodney W. Grimes 	 * instance of unp_discard just calls closef on B.
1359df8bae1dSRodney W. Grimes 	 *
1360df8bae1dSRodney W. Grimes 	 * Well, a similar chain occurs on B, resulting in a sorflush on B,
1361df8bae1dSRodney W. Grimes 	 * which results in another closef on A.  Unfortunately, A is already
1362df8bae1dSRodney W. Grimes 	 * being closed, and the descriptor has already been marked with
1363df8bae1dSRodney W. Grimes 	 * SS_NOFDREF, and soclose panics at this point.
1364df8bae1dSRodney W. Grimes 	 *
1365df8bae1dSRodney W. Grimes 	 * Here, we first take an extra reference to each inaccessible
1366df8bae1dSRodney W. Grimes 	 * descriptor.  Then, we call sorflush ourself, since we know
1367df8bae1dSRodney W. Grimes 	 * it is a Unix domain socket anyhow.  After we destroy all the
1368df8bae1dSRodney W. Grimes 	 * rights carried in messages, we do a last closef to get rid
1369df8bae1dSRodney W. Grimes 	 * of our extra reference.  This is the last close, and the
1370df8bae1dSRodney W. Grimes 	 * unp_detach etc will shut down the socket.
1371df8bae1dSRodney W. Grimes 	 *
1372df8bae1dSRodney W. Grimes 	 * 91/09/19, bsy@cs.cmu.edu
1373df8bae1dSRodney W. Grimes 	 */
13748355f576SJeff Roberson 	extra_ref = malloc(nfiles * sizeof(struct file *), M_TEMP, M_WAITOK);
1375426da3bcSAlfred Perlstein 	sx_slock(&filelist_lock);
13762e3c8fcbSPoul-Henning Kamp 	for (nunref = 0, fp = LIST_FIRST(&filehead), fpp = extra_ref; fp != 0;
1377bc6f0e79SJeffrey Hsu 	    fp = nextfp) {
13782e3c8fcbSPoul-Henning Kamp 		nextfp = LIST_NEXT(fp, f_list);
1379426da3bcSAlfred Perlstein 		FILE_LOCK(fp);
1380ed5b7817SJulian Elischer 		/*
1381ed5b7817SJulian Elischer 		 * If it's not open, skip it
1382ed5b7817SJulian Elischer 		 */
1383426da3bcSAlfred Perlstein 		if (fp->f_count == 0) {
1384426da3bcSAlfred Perlstein 			FILE_UNLOCK(fp);
1385df8bae1dSRodney W. Grimes 			continue;
1386426da3bcSAlfred Perlstein 		}
1387ed5b7817SJulian Elischer 		/*
1388ed5b7817SJulian Elischer 		 * If all refs are from msgs, and it's not marked accessible
1389ed5b7817SJulian Elischer 		 * then it must be referenced from some unreachable cycle
1390ed5b7817SJulian Elischer 		 * of (shut-down) FDs, so include it in our
1391ed5b7817SJulian Elischer 		 * list of FDs to remove
1392ed5b7817SJulian Elischer 		 */
1393426da3bcSAlfred Perlstein 		if (fp->f_count == fp->f_msgcount && !(fp->f_gcflag & FMARK)) {
1394df8bae1dSRodney W. Grimes 			*fpp++ = fp;
1395df8bae1dSRodney W. Grimes 			nunref++;
1396df8bae1dSRodney W. Grimes 			fp->f_count++;
1397df8bae1dSRodney W. Grimes 		}
1398426da3bcSAlfred Perlstein 		FILE_UNLOCK(fp);
1399df8bae1dSRodney W. Grimes 	}
1400426da3bcSAlfred Perlstein 	sx_sunlock(&filelist_lock);
1401ed5b7817SJulian Elischer 	/*
1402ed5b7817SJulian Elischer 	 * for each FD on our hit list, do the following two things
1403ed5b7817SJulian Elischer 	 */
14041c7c3c6aSMatthew Dillon 	for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) {
14051c7c3c6aSMatthew Dillon 		struct file *tfp = *fpp;
1406426da3bcSAlfred Perlstein 		FILE_LOCK(tfp);
1407426da3bcSAlfred Perlstein 		if (tfp->f_type == DTYPE_SOCKET && tfp->f_data != NULL) {
1408426da3bcSAlfred Perlstein 			FILE_UNLOCK(tfp);
14091c7c3c6aSMatthew Dillon 			sorflush((struct socket *)(tfp->f_data));
1410426da3bcSAlfred Perlstein 		} else
1411426da3bcSAlfred Perlstein 			FILE_UNLOCK(tfp);
14121c7c3c6aSMatthew Dillon 	}
1413df8bae1dSRodney W. Grimes 	for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp)
1414b40ce416SJulian Elischer 		closef(*fpp, (struct thread *) NULL);
1415210a5a71SAlfred Perlstein 	free(extra_ref, M_TEMP);
1416df8bae1dSRodney W. Grimes 	unp_gcing = 0;
1417df8bae1dSRodney W. Grimes }
1418df8bae1dSRodney W. Grimes 
141926f9a767SRodney W. Grimes void
1420df8bae1dSRodney W. Grimes unp_dispose(m)
1421df8bae1dSRodney W. Grimes 	struct mbuf *m;
1422df8bae1dSRodney W. Grimes {
1423996c772fSJohn Dyson 
1424df8bae1dSRodney W. Grimes 	if (m)
1425df8bae1dSRodney W. Grimes 		unp_scan(m, unp_discard);
1426df8bae1dSRodney W. Grimes }
1427df8bae1dSRodney W. Grimes 
14280c1bb4fbSDima Dorfman static int
14296f105b34SJohn Baldwin unp_listen(unp, td)
14300c1bb4fbSDima Dorfman 	struct unpcb *unp;
14316f105b34SJohn Baldwin 	struct thread *td;
14320c1bb4fbSDima Dorfman {
14330c1bb4fbSDima Dorfman 
14346f105b34SJohn Baldwin 	cru2x(td->td_ucred, &unp->unp_peercred);
14350c1bb4fbSDima Dorfman 	unp->unp_flags |= UNP_HAVEPCCACHED;
14360c1bb4fbSDima Dorfman 	return (0);
14370c1bb4fbSDima Dorfman }
14380c1bb4fbSDima Dorfman 
1439f708ef1bSPoul-Henning Kamp static void
1440df8bae1dSRodney W. Grimes unp_scan(m0, op)
1441df8bae1dSRodney W. Grimes 	register struct mbuf *m0;
14424d77a549SAlfred Perlstein 	void (*op)(struct file *);
1443df8bae1dSRodney W. Grimes {
14442bc21ed9SDavid Malone 	struct mbuf *m;
14452bc21ed9SDavid Malone 	struct file **rp;
14462bc21ed9SDavid Malone 	struct cmsghdr *cm;
14472bc21ed9SDavid Malone 	void *data;
14482bc21ed9SDavid Malone 	int i;
14492bc21ed9SDavid Malone 	socklen_t clen, datalen;
1450df8bae1dSRodney W. Grimes 	int qfds;
1451df8bae1dSRodney W. Grimes 
1452df8bae1dSRodney W. Grimes 	while (m0) {
14532bc21ed9SDavid Malone 		for (m = m0; m; m = m->m_next) {
145412396bdcSDavid Malone 			if (m->m_type != MT_CONTROL)
1455df8bae1dSRodney W. Grimes 				continue;
14562bc21ed9SDavid Malone 
14572bc21ed9SDavid Malone 			cm = mtod(m, struct cmsghdr *);
14582bc21ed9SDavid Malone 			clen = m->m_len;
14592bc21ed9SDavid Malone 
14602bc21ed9SDavid Malone 			while (cm != NULL) {
14612bc21ed9SDavid Malone 				if (sizeof(*cm) > clen || cm->cmsg_len > clen)
14622bc21ed9SDavid Malone 					break;
14632bc21ed9SDavid Malone 
14642bc21ed9SDavid Malone 				data = CMSG_DATA(cm);
14652bc21ed9SDavid Malone 				datalen = (caddr_t)cm + cm->cmsg_len
14662bc21ed9SDavid Malone 				    - (caddr_t)data;
14672bc21ed9SDavid Malone 
14682bc21ed9SDavid Malone 				if (cm->cmsg_level == SOL_SOCKET &&
14692bc21ed9SDavid Malone 				    cm->cmsg_type == SCM_RIGHTS) {
14702bc21ed9SDavid Malone 					qfds = datalen / sizeof (struct file *);
14712bc21ed9SDavid Malone 					rp = data;
1472df8bae1dSRodney W. Grimes 					for (i = 0; i < qfds; i++)
1473df8bae1dSRodney W. Grimes 						(*op)(*rp++);
14742bc21ed9SDavid Malone 				}
14752bc21ed9SDavid Malone 
14762bc21ed9SDavid Malone 				if (CMSG_SPACE(datalen) < clen) {
14772bc21ed9SDavid Malone 					clen -= CMSG_SPACE(datalen);
14782bc21ed9SDavid Malone 					cm = (struct cmsghdr *)
14792bc21ed9SDavid Malone 					    ((caddr_t)cm + CMSG_SPACE(datalen));
14802bc21ed9SDavid Malone 				} else {
14812bc21ed9SDavid Malone 					clen = 0;
14822bc21ed9SDavid Malone 					cm = NULL;
14832bc21ed9SDavid Malone 				}
14842bc21ed9SDavid Malone 			}
1485df8bae1dSRodney W. Grimes 		}
1486df8bae1dSRodney W. Grimes 		m0 = m0->m_act;
1487df8bae1dSRodney W. Grimes 	}
1488df8bae1dSRodney W. Grimes }
1489df8bae1dSRodney W. Grimes 
1490f708ef1bSPoul-Henning Kamp static void
1491df8bae1dSRodney W. Grimes unp_mark(fp)
1492df8bae1dSRodney W. Grimes 	struct file *fp;
1493df8bae1dSRodney W. Grimes {
1494426da3bcSAlfred Perlstein 	if (fp->f_gcflag & FMARK)
1495df8bae1dSRodney W. Grimes 		return;
1496df8bae1dSRodney W. Grimes 	unp_defer++;
1497426da3bcSAlfred Perlstein 	fp->f_gcflag |= (FMARK|FDEFER);
1498df8bae1dSRodney W. Grimes }
1499df8bae1dSRodney W. Grimes 
1500f708ef1bSPoul-Henning Kamp static void
1501df8bae1dSRodney W. Grimes unp_discard(fp)
1502df8bae1dSRodney W. Grimes 	struct file *fp;
1503df8bae1dSRodney W. Grimes {
1504426da3bcSAlfred Perlstein 	FILE_LOCK(fp);
1505df8bae1dSRodney W. Grimes 	fp->f_msgcount--;
1506df8bae1dSRodney W. Grimes 	unp_rights--;
1507426da3bcSAlfred Perlstein 	FILE_UNLOCK(fp);
1508b40ce416SJulian Elischer 	(void) closef(fp, (struct thread *)NULL);
1509df8bae1dSRodney W. Grimes }
1510