xref: /freebsd/sys/kern/uipc_usrreq.c (revision a152f8a36128ce99cc252941396d7db06ec7084e)
19454b2d8SWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1989, 1991, 1993
3e1ac28e2SRobert Watson  *	The Regents of the University of California.
44d4b555eSRobert Watson  * Copyright 2004-2006 Robert N. M. Watson
5e1ac28e2SRobert Watson  * All rights reserved.
6df8bae1dSRodney W. Grimes  *
7df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
8df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
9df8bae1dSRodney W. Grimes  * are met:
10df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
12df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
13df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
14df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
16df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
17df8bae1dSRodney W. Grimes  *    without specific prior written permission.
18df8bae1dSRodney W. Grimes  *
19df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
30df8bae1dSRodney W. Grimes  *
31748e0b0aSGarrett Wollman  *	From: @(#)uipc_usrreq.c	8.3 (Berkeley) 1/4/94
32df8bae1dSRodney W. Grimes  */
33df8bae1dSRodney W. Grimes 
34677b542eSDavid E. O'Brien #include <sys/cdefs.h>
35677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
36677b542eSDavid E. O'Brien 
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> */
434f590175SPaul Saab #include <sys/eventhandler.h>
44639acc13SGarrett Wollman #include <sys/file.h>
45960ed29cSSeigo Tanimura #include <sys/filedesc.h>
46960ed29cSSeigo Tanimura #include <sys/jail.h>
47960ed29cSSeigo Tanimura #include <sys/kernel.h>
48960ed29cSSeigo Tanimura #include <sys/lock.h>
496ea48a90SRobert Watson #include <sys/mac.h>
50639acc13SGarrett Wollman #include <sys/mbuf.h>
51033eb86eSJeff Roberson #include <sys/mount.h>
52960ed29cSSeigo Tanimura #include <sys/mutex.h>
53639acc13SGarrett Wollman #include <sys/namei.h>
54639acc13SGarrett Wollman #include <sys/proc.h>
55df8bae1dSRodney W. Grimes #include <sys/protosw.h>
56960ed29cSSeigo Tanimura #include <sys/resourcevar.h>
57df8bae1dSRodney W. Grimes #include <sys/socket.h>
58df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
59960ed29cSSeigo Tanimura #include <sys/signalvar.h>
60df8bae1dSRodney W. Grimes #include <sys/stat.h>
61960ed29cSSeigo Tanimura #include <sys/sx.h>
62639acc13SGarrett Wollman #include <sys/sysctl.h>
63960ed29cSSeigo Tanimura #include <sys/systm.h>
64a0ec558aSRobert Watson #include <sys/taskqueue.h>
65639acc13SGarrett Wollman #include <sys/un.h>
6698271db4SGarrett Wollman #include <sys/unpcb.h>
67639acc13SGarrett Wollman #include <sys/vnode.h>
68df8bae1dSRodney W. Grimes 
699e9d298aSJeff Roberson #include <vm/uma.h>
7098271db4SGarrett Wollman 
719e9d298aSJeff Roberson static uma_zone_t unp_zone;
7298271db4SGarrett Wollman static	unp_gen_t unp_gencnt;
7398271db4SGarrett Wollman static	u_int unp_count;
7498271db4SGarrett Wollman 
7598271db4SGarrett Wollman static	struct unp_head unp_shead, unp_dhead;
7698271db4SGarrett Wollman 
77df8bae1dSRodney W. Grimes /*
78df8bae1dSRodney W. Grimes  * Unix communications domain.
79df8bae1dSRodney W. Grimes  *
80df8bae1dSRodney W. Grimes  * TODO:
81df8bae1dSRodney W. Grimes  *	SEQPACKET, RDM
82df8bae1dSRodney W. Grimes  *	rethink name space problems
83df8bae1dSRodney W. Grimes  *	need a proper out-of-band
8498271db4SGarrett Wollman  *	lock pushdown
85df8bae1dSRodney W. Grimes  */
86e7dd9a10SRobert Watson static const struct	sockaddr sun_noname = { sizeof(sun_noname), AF_LOCAL };
87f708ef1bSPoul-Henning Kamp static ino_t	unp_ino;		/* prototype for fake inode numbers */
886a2989fdSMatthew N. Dodd struct mbuf *unp_addsockcred(struct thread *, struct mbuf *);
89f708ef1bSPoul-Henning Kamp 
90ce5f32deSRobert Watson /*
91ce5f32deSRobert Watson  * Currently, UNIX domain sockets are protected by a single subsystem lock,
92ce5f32deSRobert Watson  * which covers global data structures and variables, the contents of each
93ce5f32deSRobert Watson  * per-socket unpcb structure, and the so_pcb field in sockets attached to
94ce5f32deSRobert Watson  * the UNIX domain.  This provides for a moderate degree of paralellism, as
95ce5f32deSRobert Watson  * receive operations on UNIX domain sockets do not need to acquire the
96ce5f32deSRobert Watson  * subsystem lock.  Finer grained locking to permit send() without acquiring
97ce5f32deSRobert Watson  * a global lock would be a logical next step.
98ce5f32deSRobert Watson  *
99ce5f32deSRobert Watson  * The UNIX domain socket lock preceds all socket layer locks, including the
100ce5f32deSRobert Watson  * socket lock and socket buffer lock, permitting UNIX domain socket code to
101ce5f32deSRobert Watson  * call into socket support routines without releasing its locks.
102ce5f32deSRobert Watson  *
103ce5f32deSRobert Watson  * Some caution is required in areas where the UNIX domain socket code enters
104ce5f32deSRobert Watson  * VFS in order to create or find rendezvous points.  This results in
105ce5f32deSRobert Watson  * dropping of the UNIX domain socket subsystem lock, acquisition of the
106ce5f32deSRobert Watson  * Giant lock, and potential sleeping.  This increases the chances of races,
107ce5f32deSRobert Watson  * and exposes weaknesses in the socket->protocol API by offering poor
108ce5f32deSRobert Watson  * failure modes.
109ce5f32deSRobert Watson  */
1100d9ce3a1SRobert Watson static struct mtx unp_mtx;
1110d9ce3a1SRobert Watson #define	UNP_LOCK_INIT() \
1120d9ce3a1SRobert Watson 	mtx_init(&unp_mtx, "unp", NULL, MTX_DEF)
1130d9ce3a1SRobert Watson #define	UNP_LOCK()		mtx_lock(&unp_mtx)
1140d9ce3a1SRobert Watson #define	UNP_UNLOCK()		mtx_unlock(&unp_mtx)
1150d9ce3a1SRobert Watson #define	UNP_LOCK_ASSERT()	mtx_assert(&unp_mtx, MA_OWNED)
1164c5bc1caSRobert Watson #define	UNP_UNLOCK_ASSERT()	mtx_assert(&unp_mtx, MA_NOTOWNED)
1170d9ce3a1SRobert Watson 
118a0ec558aSRobert Watson /*
119a0ec558aSRobert Watson  * Garbage collection of cyclic file descriptor/socket references occurs
120a0ec558aSRobert Watson  * asynchronously in a taskqueue context in order to avoid recursion and
121a0ec558aSRobert Watson  * reentrance in the UNIX domain socket, file descriptor, and socket layer
122a0ec558aSRobert Watson  * code.  See unp_gc() for a full description.
123a0ec558aSRobert Watson  */
124a0ec558aSRobert Watson static struct task	unp_gc_task;
125a0ec558aSRobert Watson 
1264d77a549SAlfred Perlstein static int     unp_attach(struct socket *);
1274d77a549SAlfred Perlstein static void    unp_detach(struct unpcb *);
1284d77a549SAlfred Perlstein static int     unp_bind(struct unpcb *,struct sockaddr *, struct thread *);
12970f52b48SBruce Evans static int     unp_connect(struct socket *,struct sockaddr *, struct thread *);
1306a2989fdSMatthew N. Dodd static int     unp_connect2(struct socket *so, struct socket *so2, int);
1314d77a549SAlfred Perlstein static void    unp_disconnect(struct unpcb *);
1324d77a549SAlfred Perlstein static void    unp_shutdown(struct unpcb *);
1334d77a549SAlfred Perlstein static void    unp_drop(struct unpcb *, int);
134a0ec558aSRobert Watson static void    unp_gc(__unused void *, int);
1354d77a549SAlfred Perlstein static void    unp_scan(struct mbuf *, void (*)(struct file *));
1364d77a549SAlfred Perlstein static void    unp_mark(struct file *);
1374d77a549SAlfred Perlstein static void    unp_discard(struct file *);
1384d77a549SAlfred Perlstein static void    unp_freerights(struct file **, int);
1394d77a549SAlfred Perlstein static int     unp_internalize(struct mbuf **, struct thread *);
140d374e81eSRobert Watson static int     unp_listen(struct socket *, struct unpcb *, int,
141d374e81eSRobert Watson 		   struct thread *);
142f708ef1bSPoul-Henning Kamp 
143ac45e92fSRobert Watson static void
144a29f300eSGarrett Wollman uipc_abort(struct socket *so)
145df8bae1dSRodney W. Grimes {
14640f2ac28SRobert Watson 	struct unpcb *unp;
147df8bae1dSRodney W. Grimes 
14840f2ac28SRobert Watson 	unp = sotounpcb(so);
1494d4b555eSRobert Watson 	KASSERT(unp != NULL, ("uipc_abort: unp == NULL"));
1504d4b555eSRobert Watson 	UNP_LOCK();
151a29f300eSGarrett Wollman 	unp_drop(unp, ECONNABORTED);
152a152f8a3SRobert Watson 	UNP_UNLOCK();
153df8bae1dSRodney W. Grimes }
154df8bae1dSRodney W. Grimes 
155a29f300eSGarrett Wollman static int
15657bf258eSGarrett Wollman uipc_accept(struct socket *so, struct sockaddr **nam)
157a29f300eSGarrett Wollman {
15840f2ac28SRobert Watson 	struct unpcb *unp;
1590d9ce3a1SRobert Watson 	const struct sockaddr *sa;
160df8bae1dSRodney W. Grimes 
161df8bae1dSRodney W. Grimes 	/*
162df8bae1dSRodney W. Grimes 	 * Pass back name of connected socket,
163df8bae1dSRodney W. Grimes 	 * if it was bound and we are still connected
164df8bae1dSRodney W. Grimes 	 * (our peer may have closed already!).
165df8bae1dSRodney W. Grimes 	 */
1664d4b555eSRobert Watson 	unp = sotounpcb(so);
1674d4b555eSRobert Watson 	KASSERT(unp != NULL, ("uipc_accept: unp == NULL"));
1680d9ce3a1SRobert Watson 	*nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK);
1690d9ce3a1SRobert Watson 	UNP_LOCK();
1700d9ce3a1SRobert Watson 	if (unp->unp_conn != NULL && unp->unp_conn->unp_addr != NULL)
1710d9ce3a1SRobert Watson 		sa = (struct sockaddr *) unp->unp_conn->unp_addr;
1720d9ce3a1SRobert Watson 	else
1730d9ce3a1SRobert Watson 		sa = &sun_noname;
1740d9ce3a1SRobert Watson 	bcopy(sa, *nam, sa->sa_len);
1750d9ce3a1SRobert Watson 	UNP_UNLOCK();
176e5aeaa0cSDag-Erling Smørgrav 	return (0);
177a29f300eSGarrett Wollman }
178df8bae1dSRodney W. Grimes 
179a29f300eSGarrett Wollman static int
180b40ce416SJulian Elischer uipc_attach(struct socket *so, int proto, struct thread *td)
181a29f300eSGarrett Wollman {
182df8bae1dSRodney W. Grimes 
183e5aeaa0cSDag-Erling Smørgrav 	return (unp_attach(so));
184a29f300eSGarrett Wollman }
185a29f300eSGarrett Wollman 
186a29f300eSGarrett Wollman static int
187b40ce416SJulian Elischer uipc_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
188a29f300eSGarrett Wollman {
18940f2ac28SRobert Watson 	struct unpcb *unp;
19040f2ac28SRobert Watson 	int error;
191a29f300eSGarrett Wollman 
19240f2ac28SRobert Watson 	unp = sotounpcb(so);
1934d4b555eSRobert Watson 	KASSERT(unp != NULL, ("uipc_bind: unp == NULL"));
1944d4b555eSRobert Watson 	UNP_LOCK();
19540f2ac28SRobert Watson 	error = unp_bind(unp, nam, td);
19640f2ac28SRobert Watson 	UNP_UNLOCK();
19740f2ac28SRobert Watson 	return (error);
198a29f300eSGarrett Wollman }
199a29f300eSGarrett Wollman 
200a29f300eSGarrett Wollman static int
201b40ce416SJulian Elischer uipc_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
202a29f300eSGarrett Wollman {
2030d9ce3a1SRobert Watson 	int error;
204a29f300eSGarrett Wollman 
205fd179ee9SRobert Watson 	KASSERT(td == curthread, ("uipc_connect: td != curthread"));
2064d4b555eSRobert Watson 	UNP_LOCK();
207fd179ee9SRobert Watson 	error = unp_connect(so, nam, td);
2080d9ce3a1SRobert Watson 	UNP_UNLOCK();
2090d9ce3a1SRobert Watson 	return (error);
210a29f300eSGarrett Wollman }
211a29f300eSGarrett Wollman 
212a152f8a3SRobert Watson /*
213a152f8a3SRobert Watson  * XXXRW: Should also unbind?
214a152f8a3SRobert Watson  */
215a152f8a3SRobert Watson static void
216a152f8a3SRobert Watson uipc_close(struct socket *so)
217a152f8a3SRobert Watson {
218a152f8a3SRobert Watson 	struct unpcb *unp;
219a152f8a3SRobert Watson 
220a152f8a3SRobert Watson 	unp = sotounpcb(so);
221a152f8a3SRobert Watson 	KASSERT(unp != NULL, ("uipc_close: unp == NULL"));
222a152f8a3SRobert Watson 	UNP_LOCK();
223a152f8a3SRobert Watson 	unp_disconnect(unp);
224a152f8a3SRobert Watson 	UNP_UNLOCK();
225a152f8a3SRobert Watson }
226a152f8a3SRobert Watson 
227db48c0d2SRobert Watson int
228a29f300eSGarrett Wollman uipc_connect2(struct socket *so1, struct socket *so2)
229a29f300eSGarrett Wollman {
23040f2ac28SRobert Watson 	struct unpcb *unp;
2310d9ce3a1SRobert Watson 	int error;
232a29f300eSGarrett Wollman 
23340f2ac28SRobert Watson 	unp = sotounpcb(so1);
2344d4b555eSRobert Watson 	KASSERT(unp != NULL, ("uipc_connect2: unp == NULL"));
2354d4b555eSRobert Watson 	UNP_LOCK();
2366a2989fdSMatthew N. Dodd 	error = unp_connect2(so1, so2, PRU_CONNECT2);
2370d9ce3a1SRobert Watson 	UNP_UNLOCK();
2380d9ce3a1SRobert Watson 	return (error);
239a29f300eSGarrett Wollman }
240a29f300eSGarrett Wollman 
241a29f300eSGarrett Wollman /* control is EOPNOTSUPP */
242a29f300eSGarrett Wollman 
243bc725eafSRobert Watson static void
244a29f300eSGarrett Wollman uipc_detach(struct socket *so)
245a29f300eSGarrett Wollman {
24640f2ac28SRobert Watson 	struct unpcb *unp;
247a29f300eSGarrett Wollman 
24840f2ac28SRobert Watson 	unp = sotounpcb(so);
2494d4b555eSRobert Watson 	KASSERT(unp != NULL, ("uipc_detach: unp == NULL"));
2504d4b555eSRobert Watson 	UNP_LOCK();
2514c5bc1caSRobert Watson 	unp_detach(unp);
2524c5bc1caSRobert Watson 	UNP_UNLOCK_ASSERT();
253a29f300eSGarrett Wollman }
254a29f300eSGarrett Wollman 
255a29f300eSGarrett Wollman static int
256a29f300eSGarrett Wollman uipc_disconnect(struct socket *so)
257a29f300eSGarrett Wollman {
25840f2ac28SRobert Watson 	struct unpcb *unp;
259a29f300eSGarrett Wollman 
26040f2ac28SRobert Watson 	unp = sotounpcb(so);
2614d4b555eSRobert Watson 	KASSERT(unp != NULL, ("uipc_disconnect: unp == NULL"));
2624d4b555eSRobert Watson 	UNP_LOCK();
263a29f300eSGarrett Wollman 	unp_disconnect(unp);
2640d9ce3a1SRobert Watson 	UNP_UNLOCK();
265e5aeaa0cSDag-Erling Smørgrav 	return (0);
266a29f300eSGarrett Wollman }
267a29f300eSGarrett Wollman 
268a29f300eSGarrett Wollman static int
269d374e81eSRobert Watson uipc_listen(struct socket *so, int backlog, struct thread *td)
270a29f300eSGarrett Wollman {
27140f2ac28SRobert Watson 	struct unpcb *unp;
2720d9ce3a1SRobert Watson 	int error;
273a29f300eSGarrett Wollman 
27440f2ac28SRobert Watson 	unp = sotounpcb(so);
2754d4b555eSRobert Watson 	KASSERT(unp != NULL, ("uipc_listen: unp == NULL"));
2764d4b555eSRobert Watson 	UNP_LOCK();
2774d4b555eSRobert Watson 	if (unp->unp_vnode == NULL) {
27840f2ac28SRobert Watson 		UNP_UNLOCK();
27940f2ac28SRobert Watson 		return (EINVAL);
28040f2ac28SRobert Watson 	}
281d374e81eSRobert Watson 	error = unp_listen(so, unp, backlog, td);
2820d9ce3a1SRobert Watson 	UNP_UNLOCK();
2830d9ce3a1SRobert Watson 	return (error);
284a29f300eSGarrett Wollman }
285a29f300eSGarrett Wollman 
286a29f300eSGarrett Wollman static int
28757bf258eSGarrett Wollman uipc_peeraddr(struct socket *so, struct sockaddr **nam)
288a29f300eSGarrett Wollman {
28940f2ac28SRobert Watson 	struct unpcb *unp;
2900d9ce3a1SRobert Watson 	const struct sockaddr *sa;
291a29f300eSGarrett Wollman 
2924d4b555eSRobert Watson 	unp = sotounpcb(so);
2934d4b555eSRobert Watson 	KASSERT(unp != NULL, ("uipc_peeraddr: unp == NULL"));
2940d9ce3a1SRobert Watson 	*nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK);
2950d9ce3a1SRobert Watson 	UNP_LOCK();
296fc3fcacfSRobert Watson 	if (unp->unp_conn != NULL && unp->unp_conn->unp_addr!= NULL)
2970d9ce3a1SRobert Watson 		sa = (struct sockaddr *) unp->unp_conn->unp_addr;
298bdc5f6a3SHajimu UMEMOTO 	else {
299bdc5f6a3SHajimu UMEMOTO 		/*
300bdc5f6a3SHajimu UMEMOTO 		 * XXX: It seems that this test always fails even when
301bdc5f6a3SHajimu UMEMOTO 		 * connection is established.  So, this else clause is
302bdc5f6a3SHajimu UMEMOTO 		 * added as workaround to return PF_LOCAL sockaddr.
303bdc5f6a3SHajimu UMEMOTO 		 */
3040d9ce3a1SRobert Watson 		sa = &sun_noname;
305bdc5f6a3SHajimu UMEMOTO 	}
3060d9ce3a1SRobert Watson 	bcopy(sa, *nam, sa->sa_len);
3070d9ce3a1SRobert Watson 	UNP_UNLOCK();
308e5aeaa0cSDag-Erling Smørgrav 	return (0);
309a29f300eSGarrett Wollman }
310a29f300eSGarrett Wollman 
311a29f300eSGarrett Wollman static int
312a29f300eSGarrett Wollman uipc_rcvd(struct socket *so, int flags)
313a29f300eSGarrett Wollman {
31440f2ac28SRobert Watson 	struct unpcb *unp;
315a29f300eSGarrett Wollman 	struct socket *so2;
316337cc6b6SRobert Watson 	u_int mbcnt, sbcc;
3176aef685fSBrian Feldman 	u_long newhiwat;
318a29f300eSGarrett Wollman 
31940f2ac28SRobert Watson 	unp = sotounpcb(so);
3204d4b555eSRobert Watson 	KASSERT(unp != NULL, ("uipc_rcvd: unp == NULL"));
321df8bae1dSRodney W. Grimes 	switch (so->so_type) {
322df8bae1dSRodney W. Grimes 	case SOCK_DGRAM:
323a29f300eSGarrett Wollman 		panic("uipc_rcvd DGRAM?");
324df8bae1dSRodney W. Grimes 		/*NOTREACHED*/
325df8bae1dSRodney W. Grimes 
326df8bae1dSRodney W. Grimes 	case SOCK_STREAM:
327df8bae1dSRodney W. Grimes 		/*
328df8bae1dSRodney W. Grimes 		 * Adjust backpressure on sender
329df8bae1dSRodney W. Grimes 		 * and wakeup any waiting to write.
330df8bae1dSRodney W. Grimes 		 */
331337cc6b6SRobert Watson 		SOCKBUF_LOCK(&so->so_rcv);
332337cc6b6SRobert Watson 		mbcnt = so->so_rcv.sb_mbcnt;
333337cc6b6SRobert Watson 		sbcc = so->so_rcv.sb_cc;
334337cc6b6SRobert Watson 		SOCKBUF_UNLOCK(&so->so_rcv);
335337cc6b6SRobert Watson 		UNP_LOCK();
336337cc6b6SRobert Watson 		if (unp->unp_conn == NULL) {
337337cc6b6SRobert Watson 			UNP_UNLOCK();
338337cc6b6SRobert Watson 			break;
339337cc6b6SRobert Watson 		}
340337cc6b6SRobert Watson 		so2 = unp->unp_conn->unp_socket;
341337cc6b6SRobert Watson 		SOCKBUF_LOCK(&so2->so_snd);
342337cc6b6SRobert Watson 		so2->so_snd.sb_mbmax += unp->unp_mbcnt - mbcnt;
343337cc6b6SRobert Watson 		newhiwat = so2->so_snd.sb_hiwat + unp->unp_cc - sbcc;
344f535380cSDon Lewis 		(void)chgsbsize(so2->so_cred->cr_uidinfo, &so2->so_snd.sb_hiwat,
3456aef685fSBrian Feldman 		    newhiwat, RLIM_INFINITY);
3461e4d7da7SRobert Watson 		sowwakeup_locked(so2);
347337cc6b6SRobert Watson 		unp->unp_mbcnt = mbcnt;
348337cc6b6SRobert Watson 		unp->unp_cc = sbcc;
349337cc6b6SRobert Watson 		UNP_UNLOCK();
350df8bae1dSRodney W. Grimes 		break;
351df8bae1dSRodney W. Grimes 
352df8bae1dSRodney W. Grimes 	default:
353a29f300eSGarrett Wollman 		panic("uipc_rcvd unknown socktype");
354df8bae1dSRodney W. Grimes 	}
355e5aeaa0cSDag-Erling Smørgrav 	return (0);
356a29f300eSGarrett Wollman }
357df8bae1dSRodney W. Grimes 
358a29f300eSGarrett Wollman /* pru_rcvoob is EOPNOTSUPP */
359a29f300eSGarrett Wollman 
360a29f300eSGarrett Wollman static int
36157bf258eSGarrett Wollman uipc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
362b40ce416SJulian Elischer     struct mbuf *control, struct thread *td)
363a29f300eSGarrett Wollman {
364a29f300eSGarrett Wollman 	int error = 0;
36540f2ac28SRobert Watson 	struct unpcb *unp;
366a29f300eSGarrett Wollman 	struct socket *so2;
367337cc6b6SRobert Watson 	u_int mbcnt, sbcc;
3686aef685fSBrian Feldman 	u_long newhiwat;
369a29f300eSGarrett Wollman 
37040f2ac28SRobert Watson 	unp = sotounpcb(so);
3714d4b555eSRobert Watson 	KASSERT(unp != NULL, ("uipc_send: unp == NULL"));
372a29f300eSGarrett Wollman 	if (flags & PRUS_OOB) {
373a29f300eSGarrett Wollman 		error = EOPNOTSUPP;
374a29f300eSGarrett Wollman 		goto release;
375a29f300eSGarrett Wollman 	}
376a29f300eSGarrett Wollman 
377fc3fcacfSRobert Watson 	if (control != NULL && (error = unp_internalize(&control, td)))
378a29f300eSGarrett Wollman 		goto release;
379df8bae1dSRodney W. Grimes 
3800d9ce3a1SRobert Watson 	UNP_LOCK();
381a29f300eSGarrett Wollman 	switch (so->so_type) {
382a29f300eSGarrett Wollman 	case SOCK_DGRAM:
383a29f300eSGarrett Wollman 	{
384e7dd9a10SRobert Watson 		const struct sockaddr *from;
385df8bae1dSRodney W. Grimes 
386fc3fcacfSRobert Watson 		if (nam != NULL) {
387fc3fcacfSRobert Watson 			if (unp->unp_conn != NULL) {
388df8bae1dSRodney W. Grimes 				error = EISCONN;
389df8bae1dSRodney W. Grimes 				break;
390df8bae1dSRodney W. Grimes 			}
391b40ce416SJulian Elischer 			error = unp_connect(so, nam, td);
392df8bae1dSRodney W. Grimes 			if (error)
393df8bae1dSRodney W. Grimes 				break;
394df8bae1dSRodney W. Grimes 		} else {
395fc3fcacfSRobert Watson 			if (unp->unp_conn == NULL) {
396df8bae1dSRodney W. Grimes 				error = ENOTCONN;
397df8bae1dSRodney W. Grimes 				break;
398df8bae1dSRodney W. Grimes 			}
399df8bae1dSRodney W. Grimes 		}
400df8bae1dSRodney W. Grimes 		so2 = unp->unp_conn->unp_socket;
401fc3fcacfSRobert Watson 		if (unp->unp_addr != NULL)
40257bf258eSGarrett Wollman 			from = (struct sockaddr *)unp->unp_addr;
403df8bae1dSRodney W. Grimes 		else
404df8bae1dSRodney W. Grimes 			from = &sun_noname;
4056a2989fdSMatthew N. Dodd 		if (unp->unp_conn->unp_flags & UNP_WANTCRED)
4066a2989fdSMatthew N. Dodd 			control = unp_addsockcred(td, control);
407a34b7046SRobert Watson 		SOCKBUF_LOCK(&so2->so_rcv);
408a34b7046SRobert Watson 		if (sbappendaddr_locked(&so2->so_rcv, from, m, control)) {
4091e4d7da7SRobert Watson 			sorwakeup_locked(so2);
410fc3fcacfSRobert Watson 			m = NULL;
411fc3fcacfSRobert Watson 			control = NULL;
412e5aeaa0cSDag-Erling Smørgrav 		} else {
413a34b7046SRobert Watson 			SOCKBUF_UNLOCK(&so2->so_rcv);
414df8bae1dSRodney W. Grimes 			error = ENOBUFS;
415e5aeaa0cSDag-Erling Smørgrav 		}
416fc3fcacfSRobert Watson 		if (nam != NULL)
417df8bae1dSRodney W. Grimes 			unp_disconnect(unp);
418df8bae1dSRodney W. Grimes 		break;
419df8bae1dSRodney W. Grimes 	}
420df8bae1dSRodney W. Grimes 
421df8bae1dSRodney W. Grimes 	case SOCK_STREAM:
4226b8fda4dSGarrett Wollman 		/* Connect if not connected yet. */
4236b8fda4dSGarrett Wollman 		/*
4246b8fda4dSGarrett Wollman 		 * Note: A better implementation would complain
425402cc72dSDavid Greenman 		 * if not equal to the peer's address.
4266b8fda4dSGarrett Wollman 		 */
427402cc72dSDavid Greenman 		if ((so->so_state & SS_ISCONNECTED) == 0) {
428fc3fcacfSRobert Watson 			if (nam != NULL) {
429b40ce416SJulian Elischer 				error = unp_connect(so, nam, td);
430402cc72dSDavid Greenman 				if (error)
4316b8fda4dSGarrett Wollman 					break;	/* XXX */
432402cc72dSDavid Greenman 			} else {
433402cc72dSDavid Greenman 				error = ENOTCONN;
434402cc72dSDavid Greenman 				break;
435402cc72dSDavid Greenman 			}
436402cc72dSDavid Greenman 		}
437402cc72dSDavid Greenman 
438337cc6b6SRobert Watson 		/* Lockless read. */
439c0b99ffaSRobert Watson 		if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
440df8bae1dSRodney W. Grimes 			error = EPIPE;
441df8bae1dSRodney W. Grimes 			break;
442df8bae1dSRodney W. Grimes 		}
443fc3fcacfSRobert Watson 		if (unp->unp_conn == NULL)
444a29f300eSGarrett Wollman 			panic("uipc_send connected but no connection?");
445df8bae1dSRodney W. Grimes 		so2 = unp->unp_conn->unp_socket;
446a34b7046SRobert Watson 		SOCKBUF_LOCK(&so2->so_rcv);
4476a2989fdSMatthew N. Dodd 		if (unp->unp_conn->unp_flags & UNP_WANTCRED) {
4486a2989fdSMatthew N. Dodd 			/*
4496a2989fdSMatthew N. Dodd 			 * Credentials are passed only once on
4506a2989fdSMatthew N. Dodd 			 * SOCK_STREAM.
4516a2989fdSMatthew N. Dodd 			 */
4526a2989fdSMatthew N. Dodd 			unp->unp_conn->unp_flags &= ~UNP_WANTCRED;
4536a2989fdSMatthew N. Dodd 			control = unp_addsockcred(td, control);
4546a2989fdSMatthew N. Dodd 		}
455df8bae1dSRodney W. Grimes 		/*
456df8bae1dSRodney W. Grimes 		 * Send to paired receive port, and then reduce
457df8bae1dSRodney W. Grimes 		 * send buffer hiwater marks to maintain backpressure.
458df8bae1dSRodney W. Grimes 		 * Wake up readers.
459df8bae1dSRodney W. Grimes 		 */
460fc3fcacfSRobert Watson 		if (control != NULL) {
461a34b7046SRobert Watson 			if (sbappendcontrol_locked(&so2->so_rcv, m, control))
462fc3fcacfSRobert Watson 				control = NULL;
463e5aeaa0cSDag-Erling Smørgrav 		} else {
464a34b7046SRobert Watson 			sbappend_locked(&so2->so_rcv, m);
465e5aeaa0cSDag-Erling Smørgrav 		}
466337cc6b6SRobert Watson 		mbcnt = so2->so_rcv.sb_mbcnt - unp->unp_conn->unp_mbcnt;
467ff8b0106SBrian Feldman 		unp->unp_conn->unp_mbcnt = so2->so_rcv.sb_mbcnt;
468337cc6b6SRobert Watson 		sbcc = so2->so_rcv.sb_cc;
469337cc6b6SRobert Watson 		sorwakeup_locked(so2);
470337cc6b6SRobert Watson 
471337cc6b6SRobert Watson 		SOCKBUF_LOCK(&so->so_snd);
4726aef685fSBrian Feldman 		newhiwat = so->so_snd.sb_hiwat -
473337cc6b6SRobert Watson 		    (sbcc - unp->unp_conn->unp_cc);
474f535380cSDon Lewis 		(void)chgsbsize(so->so_cred->cr_uidinfo, &so->so_snd.sb_hiwat,
4756aef685fSBrian Feldman 		    newhiwat, RLIM_INFINITY);
476337cc6b6SRobert Watson 		so->so_snd.sb_mbmax -= mbcnt;
4777abe2ac2SAlan Cox 		SOCKBUF_UNLOCK(&so->so_snd);
478337cc6b6SRobert Watson 
479337cc6b6SRobert Watson 		unp->unp_conn->unp_cc = sbcc;
480fc3fcacfSRobert Watson 		m = NULL;
481df8bae1dSRodney W. Grimes 		break;
482df8bae1dSRodney W. Grimes 
483df8bae1dSRodney W. Grimes 	default:
484a29f300eSGarrett Wollman 		panic("uipc_send unknown socktype");
485df8bae1dSRodney W. Grimes 	}
486a29f300eSGarrett Wollman 
4876b8fda4dSGarrett Wollman 	/*
4886b8fda4dSGarrett Wollman 	 * SEND_EOF is equivalent to a SEND followed by
4896b8fda4dSGarrett Wollman 	 * a SHUTDOWN.
4906b8fda4dSGarrett Wollman 	 */
491a29f300eSGarrett Wollman 	if (flags & PRUS_EOF) {
4926b8fda4dSGarrett Wollman 		socantsendmore(so);
4936b8fda4dSGarrett Wollman 		unp_shutdown(unp);
4946b8fda4dSGarrett Wollman 	}
4950d9ce3a1SRobert Watson 	UNP_UNLOCK();
496df8bae1dSRodney W. Grimes 
497fc3fcacfSRobert Watson 	if (control != NULL && error != 0)
498bd508d39SDon Lewis 		unp_dispose(control);
499bd508d39SDon Lewis 
500a29f300eSGarrett Wollman release:
501fc3fcacfSRobert Watson 	if (control != NULL)
502a29f300eSGarrett Wollman 		m_freem(control);
503fc3fcacfSRobert Watson 	if (m != NULL)
504a29f300eSGarrett Wollman 		m_freem(m);
505e5aeaa0cSDag-Erling Smørgrav 	return (error);
506a29f300eSGarrett Wollman }
507df8bae1dSRodney W. Grimes 
508a29f300eSGarrett Wollman static int
509a29f300eSGarrett Wollman uipc_sense(struct socket *so, struct stat *sb)
510a29f300eSGarrett Wollman {
51140f2ac28SRobert Watson 	struct unpcb *unp;
512a29f300eSGarrett Wollman 	struct socket *so2;
513a29f300eSGarrett Wollman 
51440f2ac28SRobert Watson 	unp = sotounpcb(so);
5154d4b555eSRobert Watson 	KASSERT(unp != NULL, ("uipc_sense: unp == NULL"));
5164d4b555eSRobert Watson 	UNP_LOCK();
517a29f300eSGarrett Wollman 	sb->st_blksize = so->so_snd.sb_hiwat;
518fc3fcacfSRobert Watson 	if (so->so_type == SOCK_STREAM && unp->unp_conn != NULL) {
519df8bae1dSRodney W. Grimes 		so2 = unp->unp_conn->unp_socket;
520a29f300eSGarrett Wollman 		sb->st_blksize += so2->so_rcv.sb_cc;
521df8bae1dSRodney W. Grimes 	}
522f3732fd1SPoul-Henning Kamp 	sb->st_dev = NODEV;
523df8bae1dSRodney W. Grimes 	if (unp->unp_ino == 0)
5246f782c46SJeffrey Hsu 		unp->unp_ino = (++unp_ino == 0) ? ++unp_ino : unp_ino;
525a29f300eSGarrett Wollman 	sb->st_ino = unp->unp_ino;
5260d9ce3a1SRobert Watson 	UNP_UNLOCK();
527df8bae1dSRodney W. Grimes 	return (0);
528a29f300eSGarrett Wollman }
529df8bae1dSRodney W. Grimes 
530a29f300eSGarrett Wollman static int
531a29f300eSGarrett Wollman uipc_shutdown(struct socket *so)
532a29f300eSGarrett Wollman {
53340f2ac28SRobert Watson 	struct unpcb *unp;
534df8bae1dSRodney W. Grimes 
53540f2ac28SRobert Watson 	unp = sotounpcb(so);
5364d4b555eSRobert Watson 	KASSERT(unp != NULL, ("uipc_shutdown: unp == NULL"));
5374d4b555eSRobert Watson 	UNP_LOCK();
538a29f300eSGarrett Wollman 	socantsendmore(so);
539a29f300eSGarrett Wollman 	unp_shutdown(unp);
5400d9ce3a1SRobert Watson 	UNP_UNLOCK();
541e5aeaa0cSDag-Erling Smørgrav 	return (0);
542a29f300eSGarrett Wollman }
543df8bae1dSRodney W. Grimes 
544a29f300eSGarrett Wollman static int
54557bf258eSGarrett Wollman uipc_sockaddr(struct socket *so, struct sockaddr **nam)
546a29f300eSGarrett Wollman {
54740f2ac28SRobert Watson 	struct unpcb *unp;
5480d9ce3a1SRobert Watson 	const struct sockaddr *sa;
549a29f300eSGarrett Wollman 
5504d4b555eSRobert Watson 	unp = sotounpcb(so);
5514d4b555eSRobert Watson 	KASSERT(unp != NULL, ("uipc_sockaddr: unp == NULL"));
5520d9ce3a1SRobert Watson 	*nam = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK);
5530d9ce3a1SRobert Watson 	UNP_LOCK();
554fc3fcacfSRobert Watson 	if (unp->unp_addr != NULL)
5550d9ce3a1SRobert Watson 		sa = (struct sockaddr *) unp->unp_addr;
55683f3198bSThomas Moestl 	else
5570d9ce3a1SRobert Watson 		sa = &sun_noname;
5580d9ce3a1SRobert Watson 	bcopy(sa, *nam, sa->sa_len);
5590d9ce3a1SRobert Watson 	UNP_UNLOCK();
560e5aeaa0cSDag-Erling Smørgrav 	return (0);
561df8bae1dSRodney W. Grimes }
562a29f300eSGarrett Wollman 
563a29f300eSGarrett Wollman struct pr_usrreqs uipc_usrreqs = {
564756d52a1SPoul-Henning Kamp 	.pru_abort = 		uipc_abort,
565756d52a1SPoul-Henning Kamp 	.pru_accept =		uipc_accept,
566756d52a1SPoul-Henning Kamp 	.pru_attach =		uipc_attach,
567756d52a1SPoul-Henning Kamp 	.pru_bind =		uipc_bind,
568756d52a1SPoul-Henning Kamp 	.pru_connect =		uipc_connect,
569756d52a1SPoul-Henning Kamp 	.pru_connect2 =		uipc_connect2,
570756d52a1SPoul-Henning Kamp 	.pru_detach =		uipc_detach,
571756d52a1SPoul-Henning Kamp 	.pru_disconnect =	uipc_disconnect,
572756d52a1SPoul-Henning Kamp 	.pru_listen =		uipc_listen,
573756d52a1SPoul-Henning Kamp 	.pru_peeraddr =		uipc_peeraddr,
574756d52a1SPoul-Henning Kamp 	.pru_rcvd =		uipc_rcvd,
575756d52a1SPoul-Henning Kamp 	.pru_send =		uipc_send,
576756d52a1SPoul-Henning Kamp 	.pru_sense =		uipc_sense,
577756d52a1SPoul-Henning Kamp 	.pru_shutdown =		uipc_shutdown,
578756d52a1SPoul-Henning Kamp 	.pru_sockaddr =		uipc_sockaddr,
579756d52a1SPoul-Henning Kamp 	.pru_sosend =		sosend,
580756d52a1SPoul-Henning Kamp 	.pru_soreceive =	soreceive,
581756d52a1SPoul-Henning Kamp 	.pru_sopoll =		sopoll,
582a152f8a3SRobert Watson 	.pru_close =		uipc_close,
583a29f300eSGarrett Wollman };
584df8bae1dSRodney W. Grimes 
5850c1bb4fbSDima Dorfman int
586892af6b9SRobert Watson uipc_ctloutput(struct socket *so, struct sockopt *sopt)
5870c1bb4fbSDima Dorfman {
58840f2ac28SRobert Watson 	struct unpcb *unp;
5890d9ce3a1SRobert Watson 	struct xucred xu;
5906a2989fdSMatthew N. Dodd 	int error, optval;
5916a2989fdSMatthew N. Dodd 
59296a041b5SMatthew N. Dodd 	if (sopt->sopt_level != 0)
59396a041b5SMatthew N. Dodd 		return (EINVAL);
59496a041b5SMatthew N. Dodd 
5956a2989fdSMatthew N. Dodd 	unp = sotounpcb(so);
5964d4b555eSRobert Watson 	KASSERT(unp != NULL, ("uipc_ctloutput: unp == NULL"));
5974d4b555eSRobert Watson 	UNP_LOCK();
5986a2989fdSMatthew N. Dodd 	error = 0;
5990c1bb4fbSDima Dorfman 	switch (sopt->sopt_dir) {
6000c1bb4fbSDima Dorfman 	case SOPT_GET:
6010c1bb4fbSDima Dorfman 		switch (sopt->sopt_name) {
6020c1bb4fbSDima Dorfman 		case LOCAL_PEERCRED:
6030c1bb4fbSDima Dorfman 			if (unp->unp_flags & UNP_HAVEPC)
6040d9ce3a1SRobert Watson 				xu = unp->unp_peercred;
6050c1bb4fbSDima Dorfman 			else {
6060c1bb4fbSDima Dorfman 				if (so->so_type == SOCK_STREAM)
6070c1bb4fbSDima Dorfman 					error = ENOTCONN;
6080c1bb4fbSDima Dorfman 				else
6090c1bb4fbSDima Dorfman 					error = EINVAL;
6100c1bb4fbSDima Dorfman 			}
6110d9ce3a1SRobert Watson 			if (error == 0)
6120d9ce3a1SRobert Watson 				error = sooptcopyout(sopt, &xu, sizeof(xu));
6130c1bb4fbSDima Dorfman 			break;
6146a2989fdSMatthew N. Dodd 		case LOCAL_CREDS:
6156a2989fdSMatthew N. Dodd 			optval = unp->unp_flags & UNP_WANTCRED ? 1 : 0;
6166a2989fdSMatthew N. Dodd 			error = sooptcopyout(sopt, &optval, sizeof(optval));
6176a2989fdSMatthew N. Dodd 			break;
6186a2989fdSMatthew N. Dodd 		case LOCAL_CONNWAIT:
6196a2989fdSMatthew N. Dodd 			optval = unp->unp_flags & UNP_CONNWAIT ? 1 : 0;
6206a2989fdSMatthew N. Dodd 			error = sooptcopyout(sopt, &optval, sizeof(optval));
6216a2989fdSMatthew N. Dodd 			break;
6220c1bb4fbSDima Dorfman 		default:
6230c1bb4fbSDima Dorfman 			error = EOPNOTSUPP;
6240c1bb4fbSDima Dorfman 			break;
6250c1bb4fbSDima Dorfman 		}
6260c1bb4fbSDima Dorfman 		break;
6270c1bb4fbSDima Dorfman 	case SOPT_SET:
6286a2989fdSMatthew N. Dodd 		switch (sopt->sopt_name) {
6296a2989fdSMatthew N. Dodd 		case LOCAL_CREDS:
6306a2989fdSMatthew N. Dodd 		case LOCAL_CONNWAIT:
6316a2989fdSMatthew N. Dodd 			error = sooptcopyin(sopt, &optval, sizeof(optval),
6326a2989fdSMatthew N. Dodd 					    sizeof(optval));
6336a2989fdSMatthew N. Dodd 			if (error)
6346a2989fdSMatthew N. Dodd 				break;
6356a2989fdSMatthew N. Dodd 
6366a2989fdSMatthew N. Dodd #define	OPTSET(bit) \
6376a2989fdSMatthew N. Dodd 	if (optval) \
6386a2989fdSMatthew N. Dodd 		unp->unp_flags |= bit; \
6396a2989fdSMatthew N. Dodd 	else \
6406a2989fdSMatthew N. Dodd 		unp->unp_flags &= ~bit;
6416a2989fdSMatthew N. Dodd 
6426a2989fdSMatthew N. Dodd 			switch (sopt->sopt_name) {
6436a2989fdSMatthew N. Dodd 			case LOCAL_CREDS:
6446a2989fdSMatthew N. Dodd 				OPTSET(UNP_WANTCRED);
6456a2989fdSMatthew N. Dodd 				break;
6466a2989fdSMatthew N. Dodd 			case LOCAL_CONNWAIT:
6476a2989fdSMatthew N. Dodd 				OPTSET(UNP_CONNWAIT);
6486a2989fdSMatthew N. Dodd 				break;
6496a2989fdSMatthew N. Dodd 			default:
6506a2989fdSMatthew N. Dodd 				break;
6516a2989fdSMatthew N. Dodd 			}
6526a2989fdSMatthew N. Dodd 			break;
6536a2989fdSMatthew N. Dodd #undef	OPTSET
6546a2989fdSMatthew N. Dodd 		default:
6556a2989fdSMatthew N. Dodd 			error = ENOPROTOOPT;
6566a2989fdSMatthew N. Dodd 			break;
6576a2989fdSMatthew N. Dodd 		}
658abb886faSMatthew N. Dodd 		break;
6590c1bb4fbSDima Dorfman 	default:
6600c1bb4fbSDima Dorfman 		error = EOPNOTSUPP;
6610c1bb4fbSDima Dorfman 		break;
6620c1bb4fbSDima Dorfman 	}
6636a2989fdSMatthew N. Dodd 	UNP_UNLOCK();
6640c1bb4fbSDima Dorfman 	return (error);
6650c1bb4fbSDima Dorfman }
6660c1bb4fbSDima Dorfman 
667df8bae1dSRodney W. Grimes /*
668df8bae1dSRodney W. Grimes  * Both send and receive buffers are allocated PIPSIZ bytes of buffering
669df8bae1dSRodney W. Grimes  * for stream sockets, although the total for sender and receiver is
670df8bae1dSRodney W. Grimes  * actually only PIPSIZ.
671df8bae1dSRodney W. Grimes  * Datagram sockets really use the sendspace as the maximum datagram size,
672df8bae1dSRodney W. Grimes  * and don't really want to reserve the sendspace.  Their recvspace should
673df8bae1dSRodney W. Grimes  * be large enough for at least one max-size datagram plus address.
674df8bae1dSRodney W. Grimes  */
6755dce41c5SJohn Dyson #ifndef PIPSIZ
6765dce41c5SJohn Dyson #define	PIPSIZ	8192
6775dce41c5SJohn Dyson #endif
678f708ef1bSPoul-Henning Kamp static u_long	unpst_sendspace = PIPSIZ;
679f708ef1bSPoul-Henning Kamp static u_long	unpst_recvspace = PIPSIZ;
680f708ef1bSPoul-Henning Kamp static u_long	unpdg_sendspace = 2*1024;	/* really max datagram size */
681f708ef1bSPoul-Henning Kamp static u_long	unpdg_recvspace = 4*1024;
682df8bae1dSRodney W. Grimes 
683f708ef1bSPoul-Henning Kamp static int	unp_rights;			/* file descriptors in flight */
684df8bae1dSRodney W. Grimes 
685ce02431fSDoug Rabson SYSCTL_DECL(_net_local_stream);
686e59898ffSMaxime Henrion SYSCTL_ULONG(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW,
687639acc13SGarrett Wollman 	   &unpst_sendspace, 0, "");
688e59898ffSMaxime Henrion SYSCTL_ULONG(_net_local_stream, OID_AUTO, recvspace, CTLFLAG_RW,
689639acc13SGarrett Wollman 	   &unpst_recvspace, 0, "");
690ce02431fSDoug Rabson SYSCTL_DECL(_net_local_dgram);
691e59898ffSMaxime Henrion SYSCTL_ULONG(_net_local_dgram, OID_AUTO, maxdgram, CTLFLAG_RW,
692639acc13SGarrett Wollman 	   &unpdg_sendspace, 0, "");
693e59898ffSMaxime Henrion SYSCTL_ULONG(_net_local_dgram, OID_AUTO, recvspace, CTLFLAG_RW,
694639acc13SGarrett Wollman 	   &unpdg_recvspace, 0, "");
695ce02431fSDoug Rabson SYSCTL_DECL(_net_local);
696639acc13SGarrett Wollman SYSCTL_INT(_net_local, OID_AUTO, inflight, CTLFLAG_RD, &unp_rights, 0, "");
697639acc13SGarrett Wollman 
698f708ef1bSPoul-Henning Kamp static int
699892af6b9SRobert Watson unp_attach(struct socket *so)
700df8bae1dSRodney W. Grimes {
701892af6b9SRobert Watson 	struct unpcb *unp;
702df8bae1dSRodney W. Grimes 	int error;
703df8bae1dSRodney W. Grimes 
7044d4b555eSRobert Watson 	KASSERT(so->so_pcb == NULL, ("unp_attach: so_pcb != NULL"));
705df8bae1dSRodney W. Grimes 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
706df8bae1dSRodney W. Grimes 		switch (so->so_type) {
707df8bae1dSRodney W. Grimes 
708df8bae1dSRodney W. Grimes 		case SOCK_STREAM:
709df8bae1dSRodney W. Grimes 			error = soreserve(so, unpst_sendspace, unpst_recvspace);
710df8bae1dSRodney W. Grimes 			break;
711df8bae1dSRodney W. Grimes 
712df8bae1dSRodney W. Grimes 		case SOCK_DGRAM:
713df8bae1dSRodney W. Grimes 			error = soreserve(so, unpdg_sendspace, unpdg_recvspace);
714df8bae1dSRodney W. Grimes 			break;
715df8bae1dSRodney W. Grimes 
716df8bae1dSRodney W. Grimes 		default:
717df8bae1dSRodney W. Grimes 			panic("unp_attach");
718df8bae1dSRodney W. Grimes 		}
719df8bae1dSRodney W. Grimes 		if (error)
720df8bae1dSRodney W. Grimes 			return (error);
721df8bae1dSRodney W. Grimes 	}
722d664e4faSRobert Watson 	unp = uma_zalloc(unp_zone, M_WAITOK | M_ZERO);
72357bf258eSGarrett Wollman 	if (unp == NULL)
724df8bae1dSRodney W. Grimes 		return (ENOBUFS);
72598271db4SGarrett Wollman 	LIST_INIT(&unp->unp_refs);
726df8bae1dSRodney W. Grimes 	unp->unp_socket = so;
7277301cf23SRobert Watson 	so->so_pcb = unp;
7280d9ce3a1SRobert Watson 
7290d9ce3a1SRobert Watson 	UNP_LOCK();
7300d9ce3a1SRobert Watson 	unp->unp_gencnt = ++unp_gencnt;
7310d9ce3a1SRobert Watson 	unp_count++;
73298271db4SGarrett Wollman 	LIST_INSERT_HEAD(so->so_type == SOCK_DGRAM ? &unp_dhead
73398271db4SGarrett Wollman 			 : &unp_shead, unp, unp_link);
7340d9ce3a1SRobert Watson 	UNP_UNLOCK();
7350d9ce3a1SRobert Watson 
736df8bae1dSRodney W. Grimes 	return (0);
737df8bae1dSRodney W. Grimes }
738df8bae1dSRodney W. Grimes 
739f708ef1bSPoul-Henning Kamp static void
740892af6b9SRobert Watson unp_detach(struct unpcb *unp)
741df8bae1dSRodney W. Grimes {
7420d9ce3a1SRobert Watson 	struct vnode *vp;
743a0ec558aSRobert Watson 	int local_unp_rights;
7440d9ce3a1SRobert Watson 
7450d9ce3a1SRobert Watson 	UNP_LOCK_ASSERT();
7460d9ce3a1SRobert Watson 
74798271db4SGarrett Wollman 	LIST_REMOVE(unp, unp_link);
74898271db4SGarrett Wollman 	unp->unp_gencnt = ++unp_gencnt;
74998271db4SGarrett Wollman 	--unp_count;
7500d9ce3a1SRobert Watson 	if ((vp = unp->unp_vnode) != NULL) {
7510d9ce3a1SRobert Watson 		/*
7520d9ce3a1SRobert Watson 		 * XXXRW: should v_socket be frobbed only while holding
7530d9ce3a1SRobert Watson 		 * Giant?
7540d9ce3a1SRobert Watson 		 */
755fc3fcacfSRobert Watson 		unp->unp_vnode->v_socket = NULL;
756fc3fcacfSRobert Watson 		unp->unp_vnode = NULL;
757df8bae1dSRodney W. Grimes 	}
758fc3fcacfSRobert Watson 	if (unp->unp_conn != NULL)
759df8bae1dSRodney W. Grimes 		unp_disconnect(unp);
7600d9ce3a1SRobert Watson 	while (!LIST_EMPTY(&unp->unp_refs)) {
7610d9ce3a1SRobert Watson 		struct unpcb *ref = LIST_FIRST(&unp->unp_refs);
7620d9ce3a1SRobert Watson 		unp_drop(ref, ECONNRESET);
7630d9ce3a1SRobert Watson 	}
764df8bae1dSRodney W. Grimes 	soisdisconnected(unp->unp_socket);
765fc3fcacfSRobert Watson 	unp->unp_socket->so_pcb = NULL;
766a0ec558aSRobert Watson 	local_unp_rights = unp_rights;
767a5993a97SRobert Watson 	UNP_UNLOCK();
768fc3fcacfSRobert Watson 	if (unp->unp_addr != NULL)
76957bf258eSGarrett Wollman 		FREE(unp->unp_addr, M_SONAME);
7709e9d298aSJeff Roberson 	uma_zfree(unp_zone, unp);
7710d9ce3a1SRobert Watson 	if (vp) {
772033eb86eSJeff Roberson 		int vfslocked;
773033eb86eSJeff Roberson 
774033eb86eSJeff Roberson 		vfslocked = VFS_LOCK_GIANT(vp->v_mount);
7750d9ce3a1SRobert Watson 		vrele(vp);
776033eb86eSJeff Roberson 		VFS_UNLOCK_GIANT(vfslocked);
7770d9ce3a1SRobert Watson 	}
778a0ec558aSRobert Watson 	if (local_unp_rights)
779a0ec558aSRobert Watson 		taskqueue_enqueue(taskqueue_thread, &unp_gc_task);
780df8bae1dSRodney W. Grimes }
781df8bae1dSRodney W. Grimes 
782f708ef1bSPoul-Henning Kamp static int
783892af6b9SRobert Watson unp_bind(struct unpcb *unp, struct sockaddr *nam, struct thread *td)
784df8bae1dSRodney W. Grimes {
78557bf258eSGarrett Wollman 	struct sockaddr_un *soun = (struct sockaddr_un *)nam;
786f2a2857bSKirk McKusick 	struct vnode *vp;
787f2a2857bSKirk McKusick 	struct mount *mp;
788df8bae1dSRodney W. Grimes 	struct vattr vattr;
78957bf258eSGarrett Wollman 	int error, namelen;
790df8bae1dSRodney W. Grimes 	struct nameidata nd;
7918f364875SJulian Elischer 	char *buf;
792df8bae1dSRodney W. Grimes 
79340f2ac28SRobert Watson 	UNP_LOCK_ASSERT();
79440f2ac28SRobert Watson 
7950d9ce3a1SRobert Watson 	/*
7960d9ce3a1SRobert Watson 	 * XXXRW: This test-and-set of unp_vnode is non-atomic; the
7970d9ce3a1SRobert Watson 	 * unlocked read here is fine, but the value of unp_vnode needs
7980d9ce3a1SRobert Watson 	 * to be tested again after we do all the lookups to see if the
7990d9ce3a1SRobert Watson 	 * pcb is still unbound?
8000d9ce3a1SRobert Watson 	 */
801df8bae1dSRodney W. Grimes 	if (unp->unp_vnode != NULL)
802df8bae1dSRodney W. Grimes 		return (EINVAL);
80355c85568SRobert Drehmel 
80457bf258eSGarrett Wollman 	namelen = soun->sun_len - offsetof(struct sockaddr_un, sun_path);
80557bf258eSGarrett Wollman 	if (namelen <= 0)
806e5aeaa0cSDag-Erling Smørgrav 		return (EINVAL);
80755c85568SRobert Drehmel 
80840f2ac28SRobert Watson 	UNP_UNLOCK();
80940f2ac28SRobert Watson 
810a163d034SWarner Losh 	buf = malloc(namelen + 1, M_TEMP, M_WAITOK);
81155c85568SRobert Drehmel 	strlcpy(buf, soun->sun_path, namelen + 1);
81255c85568SRobert Drehmel 
8130d9ce3a1SRobert Watson 	mtx_lock(&Giant);
814f2a2857bSKirk McKusick restart:
8150d9ce3a1SRobert Watson 	mtx_assert(&Giant, MA_OWNED);
816b65f6f6bSRobert Watson 	NDINIT(&nd, CREATE, NOFOLLOW | LOCKPARENT | SAVENAME, UIO_SYSSPACE,
817b40ce416SJulian Elischer 	    buf, td);
818df8bae1dSRodney W. Grimes /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */
819797f2d22SPoul-Henning Kamp 	error = namei(&nd);
8200d9ce3a1SRobert Watson 	if (error)
8210d9ce3a1SRobert Watson 		goto done;
822df8bae1dSRodney W. Grimes 	vp = nd.ni_vp;
823f2a2857bSKirk McKusick 	if (vp != NULL || vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
824762e6b85SEivind Eklund 		NDFREE(&nd, NDF_ONLY_PNBUF);
825df8bae1dSRodney W. Grimes 		if (nd.ni_dvp == vp)
826df8bae1dSRodney W. Grimes 			vrele(nd.ni_dvp);
827df8bae1dSRodney W. Grimes 		else
828df8bae1dSRodney W. Grimes 			vput(nd.ni_dvp);
829f2a2857bSKirk McKusick 		if (vp != NULL) {
830df8bae1dSRodney W. Grimes 			vrele(vp);
8310d9ce3a1SRobert Watson 			error = EADDRINUSE;
8320d9ce3a1SRobert Watson 			goto done;
833df8bae1dSRodney W. Grimes 		}
8348f364875SJulian Elischer 		error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH);
8350d9ce3a1SRobert Watson 		if (error)
8360d9ce3a1SRobert Watson 			goto done;
837f2a2857bSKirk McKusick 		goto restart;
838f2a2857bSKirk McKusick 	}
839df8bae1dSRodney W. Grimes 	VATTR_NULL(&vattr);
840df8bae1dSRodney W. Grimes 	vattr.va_type = VSOCK;
841b40ce416SJulian Elischer 	vattr.va_mode = (ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask);
8426ea48a90SRobert Watson #ifdef MAC
8436ea48a90SRobert Watson 	error = mac_check_vnode_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
8446ea48a90SRobert Watson 	    &vattr);
8456151efaaSRobert Watson #endif
8466ea48a90SRobert Watson 	if (error == 0) {
847a854ed98SJohn Baldwin 		VOP_LEASE(nd.ni_dvp, td, td->td_ucred, LEASE_WRITE);
8487be2d300SMike Smith 		error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
8496ea48a90SRobert Watson 	}
850762e6b85SEivind Eklund 	NDFREE(&nd, NDF_ONLY_PNBUF);
8517be2d300SMike Smith 	vput(nd.ni_dvp);
852c364c823SRobert Watson 	if (error) {
853c364c823SRobert Watson 		vn_finished_write(mp);
8540d9ce3a1SRobert Watson 		goto done;
855c364c823SRobert Watson 	}
856df8bae1dSRodney W. Grimes 	vp = nd.ni_vp;
8570d9ce3a1SRobert Watson 	ASSERT_VOP_LOCKED(vp, "unp_bind");
8580d9ce3a1SRobert Watson 	soun = (struct sockaddr_un *)sodupsockaddr(nam, M_WAITOK);
8590d9ce3a1SRobert Watson 	UNP_LOCK();
860df8bae1dSRodney W. Grimes 	vp->v_socket = unp->unp_socket;
861df8bae1dSRodney W. Grimes 	unp->unp_vnode = vp;
8620d9ce3a1SRobert Watson 	unp->unp_addr = soun;
8630d9ce3a1SRobert Watson 	UNP_UNLOCK();
864b40ce416SJulian Elischer 	VOP_UNLOCK(vp, 0, td);
865f2a2857bSKirk McKusick 	vn_finished_write(mp);
8660d9ce3a1SRobert Watson done:
8670d9ce3a1SRobert Watson 	mtx_unlock(&Giant);
8688f364875SJulian Elischer 	free(buf, M_TEMP);
86940f2ac28SRobert Watson 	UNP_LOCK();
8700d9ce3a1SRobert Watson 	return (error);
871df8bae1dSRodney W. Grimes }
872df8bae1dSRodney W. Grimes 
873f708ef1bSPoul-Henning Kamp static int
874892af6b9SRobert Watson unp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
875df8bae1dSRodney W. Grimes {
876892af6b9SRobert Watson 	struct sockaddr_un *soun = (struct sockaddr_un *)nam;
877892af6b9SRobert Watson 	struct vnode *vp;
878892af6b9SRobert Watson 	struct socket *so2, *so3;
879b295bdcdSRobert Watson 	struct unpcb *unp, *unp2, *unp3;
88057bf258eSGarrett Wollman 	int error, len;
881df8bae1dSRodney W. Grimes 	struct nameidata nd;
88257bf258eSGarrett Wollman 	char buf[SOCK_MAXADDRLEN];
8830d9ce3a1SRobert Watson 	struct sockaddr *sa;
8840d9ce3a1SRobert Watson 
8850d9ce3a1SRobert Watson 	UNP_LOCK_ASSERT();
886df8bae1dSRodney W. Grimes 
8874d4b555eSRobert Watson 	unp = sotounpcb(so);
8884d4b555eSRobert Watson 	KASSERT(unp != NULL, ("unp_connect: unp == NULL"));
88957bf258eSGarrett Wollman 	len = nam->sa_len - offsetof(struct sockaddr_un, sun_path);
89057bf258eSGarrett Wollman 	if (len <= 0)
891e5aeaa0cSDag-Erling Smørgrav 		return (EINVAL);
89255c85568SRobert Drehmel 	strlcpy(buf, soun->sun_path, len + 1);
8930d9ce3a1SRobert Watson 	UNP_UNLOCK();
8940d9ce3a1SRobert Watson 	sa = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK);
8950d9ce3a1SRobert Watson 	mtx_lock(&Giant);
896b40ce416SJulian Elischer 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, buf, td);
897797f2d22SPoul-Henning Kamp 	error = namei(&nd);
898797f2d22SPoul-Henning Kamp 	if (error)
8990d9ce3a1SRobert Watson 		vp = NULL;
9000d9ce3a1SRobert Watson 	else
901df8bae1dSRodney W. Grimes 		vp = nd.ni_vp;
9020d9ce3a1SRobert Watson 	ASSERT_VOP_LOCKED(vp, "unp_connect");
903762e6b85SEivind Eklund 	NDFREE(&nd, NDF_ONLY_PNBUF);
9040d9ce3a1SRobert Watson 	if (error)
9050d9ce3a1SRobert Watson 		goto bad;
9060d9ce3a1SRobert Watson 
907df8bae1dSRodney W. Grimes 	if (vp->v_type != VSOCK) {
908df8bae1dSRodney W. Grimes 		error = ENOTSOCK;
909df8bae1dSRodney W. Grimes 		goto bad;
910df8bae1dSRodney W. Grimes 	}
911a854ed98SJohn Baldwin 	error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td);
912797f2d22SPoul-Henning Kamp 	if (error)
913df8bae1dSRodney W. Grimes 		goto bad;
9142260c03dSRobert Watson 	mtx_unlock(&Giant);
9152260c03dSRobert Watson 	UNP_LOCK();
916b295bdcdSRobert Watson 	unp = sotounpcb(so);
9174d4b555eSRobert Watson 	KASSERT(unp != NULL, ("unp_connect: unp == NULL"));
918df8bae1dSRodney W. Grimes 	so2 = vp->v_socket;
919fc3fcacfSRobert Watson 	if (so2 == NULL) {
920df8bae1dSRodney W. Grimes 		error = ECONNREFUSED;
9212260c03dSRobert Watson 		goto bad2;
922df8bae1dSRodney W. Grimes 	}
923df8bae1dSRodney W. Grimes 	if (so->so_type != so2->so_type) {
924df8bae1dSRodney W. Grimes 		error = EPROTOTYPE;
9252260c03dSRobert Watson 		goto bad2;
926df8bae1dSRodney W. Grimes 	}
927df8bae1dSRodney W. Grimes 	if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
9280d9ce3a1SRobert Watson 		if (so2->so_options & SO_ACCEPTCONN) {
9290d9ce3a1SRobert Watson 			/*
9300d9ce3a1SRobert Watson 			 * NB: drop locks here so unp_attach is entered
9310d9ce3a1SRobert Watson 			 *     w/o locks; this avoids a recursive lock
9320d9ce3a1SRobert Watson 			 *     of the head and holding sleep locks across
9330d9ce3a1SRobert Watson 			 *     a (potentially) blocking malloc.
9340d9ce3a1SRobert Watson 			 */
9350d9ce3a1SRobert Watson 			UNP_UNLOCK();
9360d9ce3a1SRobert Watson 			so3 = sonewconn(so2, 0);
9370d9ce3a1SRobert Watson 			UNP_LOCK();
9380d9ce3a1SRobert Watson 		} else
9390d9ce3a1SRobert Watson 			so3 = NULL;
9400d9ce3a1SRobert Watson 		if (so3 == NULL) {
941df8bae1dSRodney W. Grimes 			error = ECONNREFUSED;
9420d9ce3a1SRobert Watson 			goto bad2;
943df8bae1dSRodney W. Grimes 		}
9440c1bb4fbSDima Dorfman 		unp = sotounpcb(so);
945df8bae1dSRodney W. Grimes 		unp2 = sotounpcb(so2);
946df8bae1dSRodney W. Grimes 		unp3 = sotounpcb(so3);
9470d9ce3a1SRobert Watson 		if (unp2->unp_addr != NULL) {
9480d9ce3a1SRobert Watson 			bcopy(unp2->unp_addr, sa, unp2->unp_addr->sun_len);
9490d9ce3a1SRobert Watson 			unp3->unp_addr = (struct sockaddr_un *) sa;
9500d9ce3a1SRobert Watson 			sa = NULL;
9510d9ce3a1SRobert Watson 		}
9520c1bb4fbSDima Dorfman 		/*
9530c1bb4fbSDima Dorfman 		 * unp_peercred management:
9540c1bb4fbSDima Dorfman 		 *
9550c1bb4fbSDima Dorfman 		 * The connecter's (client's) credentials are copied
9560c1bb4fbSDima Dorfman 		 * from its process structure at the time of connect()
9570c1bb4fbSDima Dorfman 		 * (which is now).
9580c1bb4fbSDima Dorfman 		 */
959a854ed98SJohn Baldwin 		cru2x(td->td_ucred, &unp3->unp_peercred);
9600c1bb4fbSDima Dorfman 		unp3->unp_flags |= UNP_HAVEPC;
9610c1bb4fbSDima Dorfman 		/*
9620c1bb4fbSDima Dorfman 		 * The receiver's (server's) credentials are copied
9630c1bb4fbSDima Dorfman 		 * from the unp_peercred member of socket on which the
9640c1bb4fbSDima Dorfman 		 * former called listen(); unp_listen() cached that
9650c1bb4fbSDima Dorfman 		 * process's credentials at that time so we can use
9660c1bb4fbSDima Dorfman 		 * them now.
9670c1bb4fbSDima Dorfman 		 */
9680c1bb4fbSDima Dorfman 		KASSERT(unp2->unp_flags & UNP_HAVEPCCACHED,
9690c1bb4fbSDima Dorfman 		    ("unp_connect: listener without cached peercred"));
9700c1bb4fbSDima Dorfman 		memcpy(&unp->unp_peercred, &unp2->unp_peercred,
9710c1bb4fbSDima Dorfman 		    sizeof(unp->unp_peercred));
9720c1bb4fbSDima Dorfman 		unp->unp_flags |= UNP_HAVEPC;
973481f8fe8SMaxim Konovalov 		if (unp2->unp_flags & UNP_WANTCRED)
974481f8fe8SMaxim Konovalov 			unp3->unp_flags |= UNP_WANTCRED;
975335654d7SRobert Watson #ifdef MAC
976310e7cebSRobert Watson 		SOCK_LOCK(so);
977335654d7SRobert Watson 		mac_set_socket_peer_from_socket(so, so3);
978335654d7SRobert Watson 		mac_set_socket_peer_from_socket(so3, so);
979310e7cebSRobert Watson 		SOCK_UNLOCK(so);
980335654d7SRobert Watson #endif
9810c1bb4fbSDima Dorfman 
982df8bae1dSRodney W. Grimes 		so2 = so3;
983df8bae1dSRodney W. Grimes 	}
9846a2989fdSMatthew N. Dodd 	error = unp_connect2(so, so2, PRU_CONNECT);
9850d9ce3a1SRobert Watson bad2:
9860d9ce3a1SRobert Watson 	UNP_UNLOCK();
9870d9ce3a1SRobert Watson 	mtx_lock(&Giant);
988df8bae1dSRodney W. Grimes bad:
9890d9ce3a1SRobert Watson 	mtx_assert(&Giant, MA_OWNED);
9900d9ce3a1SRobert Watson 	if (vp != NULL)
991df8bae1dSRodney W. Grimes 		vput(vp);
9920d9ce3a1SRobert Watson 	mtx_unlock(&Giant);
9930d9ce3a1SRobert Watson 	free(sa, M_SONAME);
9940d9ce3a1SRobert Watson 	UNP_LOCK();
995df8bae1dSRodney W. Grimes 	return (error);
996df8bae1dSRodney W. Grimes }
997df8bae1dSRodney W. Grimes 
998db48c0d2SRobert Watson static int
9996a2989fdSMatthew N. Dodd unp_connect2(struct socket *so, struct socket *so2, int req)
1000df8bae1dSRodney W. Grimes {
1001892af6b9SRobert Watson 	struct unpcb *unp = sotounpcb(so);
1002892af6b9SRobert Watson 	struct unpcb *unp2;
1003df8bae1dSRodney W. Grimes 
10040d9ce3a1SRobert Watson 	UNP_LOCK_ASSERT();
10050d9ce3a1SRobert Watson 
1006df8bae1dSRodney W. Grimes 	if (so2->so_type != so->so_type)
1007df8bae1dSRodney W. Grimes 		return (EPROTOTYPE);
1008df8bae1dSRodney W. Grimes 	unp2 = sotounpcb(so2);
10094d4b555eSRobert Watson 	KASSERT(unp2 != NULL, ("unp_connect2: unp2 == NULL"));
1010df8bae1dSRodney W. Grimes 	unp->unp_conn = unp2;
1011df8bae1dSRodney W. Grimes 	switch (so->so_type) {
1012df8bae1dSRodney W. Grimes 
1013df8bae1dSRodney W. Grimes 	case SOCK_DGRAM:
101498271db4SGarrett Wollman 		LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink);
1015df8bae1dSRodney W. Grimes 		soisconnected(so);
1016df8bae1dSRodney W. Grimes 		break;
1017df8bae1dSRodney W. Grimes 
1018df8bae1dSRodney W. Grimes 	case SOCK_STREAM:
1019df8bae1dSRodney W. Grimes 		unp2->unp_conn = unp;
10206a2989fdSMatthew N. Dodd 		if (req == PRU_CONNECT &&
10216a2989fdSMatthew N. Dodd 		    ((unp->unp_flags | unp2->unp_flags) & UNP_CONNWAIT))
10226a2989fdSMatthew N. Dodd 			soisconnecting(so);
10236a2989fdSMatthew N. Dodd 		else
1024df8bae1dSRodney W. Grimes 			soisconnected(so);
1025df8bae1dSRodney W. Grimes 		soisconnected(so2);
1026df8bae1dSRodney W. Grimes 		break;
1027df8bae1dSRodney W. Grimes 
1028df8bae1dSRodney W. Grimes 	default:
1029df8bae1dSRodney W. Grimes 		panic("unp_connect2");
1030df8bae1dSRodney W. Grimes 	}
1031df8bae1dSRodney W. Grimes 	return (0);
1032df8bae1dSRodney W. Grimes }
1033df8bae1dSRodney W. Grimes 
1034f708ef1bSPoul-Henning Kamp static void
1035892af6b9SRobert Watson unp_disconnect(struct unpcb *unp)
1036df8bae1dSRodney W. Grimes {
1037892af6b9SRobert Watson 	struct unpcb *unp2 = unp->unp_conn;
10381b2e3b4bSRobert Watson 	struct socket *so;
1039df8bae1dSRodney W. Grimes 
10400d9ce3a1SRobert Watson 	UNP_LOCK_ASSERT();
10410d9ce3a1SRobert Watson 
1042fc3fcacfSRobert Watson 	if (unp2 == NULL)
1043df8bae1dSRodney W. Grimes 		return;
1044fc3fcacfSRobert Watson 	unp->unp_conn = NULL;
1045df8bae1dSRodney W. Grimes 	switch (unp->unp_socket->so_type) {
1046df8bae1dSRodney W. Grimes 	case SOCK_DGRAM:
104798271db4SGarrett Wollman 		LIST_REMOVE(unp, unp_reflink);
10481b2e3b4bSRobert Watson 		so = unp->unp_socket;
10491b2e3b4bSRobert Watson 		SOCK_LOCK(so);
10501b2e3b4bSRobert Watson 		so->so_state &= ~SS_ISCONNECTED;
10511b2e3b4bSRobert Watson 		SOCK_UNLOCK(so);
1052df8bae1dSRodney W. Grimes 		break;
1053df8bae1dSRodney W. Grimes 
1054df8bae1dSRodney W. Grimes 	case SOCK_STREAM:
1055df8bae1dSRodney W. Grimes 		soisdisconnected(unp->unp_socket);
1056fc3fcacfSRobert Watson 		unp2->unp_conn = NULL;
1057df8bae1dSRodney W. Grimes 		soisdisconnected(unp2->unp_socket);
1058df8bae1dSRodney W. Grimes 		break;
1059df8bae1dSRodney W. Grimes 	}
1060df8bae1dSRodney W. Grimes }
1061df8bae1dSRodney W. Grimes 
10620d9ce3a1SRobert Watson /*
10630d9ce3a1SRobert Watson  * unp_pcblist() assumes that UNIX domain socket memory is never reclaimed
10640d9ce3a1SRobert Watson  * by the zone (UMA_ZONE_NOFREE), and as such potentially stale pointers
10650d9ce3a1SRobert Watson  * are safe to reference.  It first scans the list of struct unpcb's to
10660d9ce3a1SRobert Watson  * generate a pointer list, then it rescans its list one entry at a time to
10670d9ce3a1SRobert Watson  * externalize and copyout.  It checks the generation number to see if a
10680d9ce3a1SRobert Watson  * struct unpcb has been reused, and will skip it if so.
10690d9ce3a1SRobert Watson  */
107098271db4SGarrett Wollman static int
107182d9ae4eSPoul-Henning Kamp unp_pcblist(SYSCTL_HANDLER_ARGS)
107298271db4SGarrett Wollman {
1073f5ef029eSPoul-Henning Kamp 	int error, i, n;
107498271db4SGarrett Wollman 	struct unpcb *unp, **unp_list;
107598271db4SGarrett Wollman 	unp_gen_t gencnt;
10768f364875SJulian Elischer 	struct xunpgen *xug;
107798271db4SGarrett Wollman 	struct unp_head *head;
10788f364875SJulian Elischer 	struct xunpcb *xu;
107998271db4SGarrett Wollman 
1080a23d65bfSBruce Evans 	head = ((intptr_t)arg1 == SOCK_DGRAM ? &unp_dhead : &unp_shead);
108198271db4SGarrett Wollman 
108298271db4SGarrett Wollman 	/*
108398271db4SGarrett Wollman 	 * The process of preparing the PCB list is too time-consuming and
108498271db4SGarrett Wollman 	 * resource-intensive to repeat twice on every request.
108598271db4SGarrett Wollman 	 */
1086fc3fcacfSRobert Watson 	if (req->oldptr == NULL) {
108798271db4SGarrett Wollman 		n = unp_count;
10888f364875SJulian Elischer 		req->oldidx = 2 * (sizeof *xug)
108998271db4SGarrett Wollman 			+ (n + n/8) * sizeof(struct xunpcb);
1090e5aeaa0cSDag-Erling Smørgrav 		return (0);
109198271db4SGarrett Wollman 	}
109298271db4SGarrett Wollman 
1093fc3fcacfSRobert Watson 	if (req->newptr != NULL)
1094e5aeaa0cSDag-Erling Smørgrav 		return (EPERM);
109598271db4SGarrett Wollman 
109698271db4SGarrett Wollman 	/*
109798271db4SGarrett Wollman 	 * OK, now we're committed to doing something.
109898271db4SGarrett Wollman 	 */
1099a163d034SWarner Losh 	xug = malloc(sizeof(*xug), M_TEMP, M_WAITOK);
11000d9ce3a1SRobert Watson 	UNP_LOCK();
110198271db4SGarrett Wollman 	gencnt = unp_gencnt;
110298271db4SGarrett Wollman 	n = unp_count;
11030d9ce3a1SRobert Watson 	UNP_UNLOCK();
110498271db4SGarrett Wollman 
11058f364875SJulian Elischer 	xug->xug_len = sizeof *xug;
11068f364875SJulian Elischer 	xug->xug_count = n;
11078f364875SJulian Elischer 	xug->xug_gen = gencnt;
11088f364875SJulian Elischer 	xug->xug_sogen = so_gencnt;
11098f364875SJulian Elischer 	error = SYSCTL_OUT(req, xug, sizeof *xug);
11108f364875SJulian Elischer 	if (error) {
11118f364875SJulian Elischer 		free(xug, M_TEMP);
1112e5aeaa0cSDag-Erling Smørgrav 		return (error);
11138f364875SJulian Elischer 	}
111498271db4SGarrett Wollman 
1115a163d034SWarner Losh 	unp_list = malloc(n * sizeof *unp_list, M_TEMP, M_WAITOK);
111698271db4SGarrett Wollman 
11170d9ce3a1SRobert Watson 	UNP_LOCK();
11182e3c8fcbSPoul-Henning Kamp 	for (unp = LIST_FIRST(head), i = 0; unp && i < n;
11192e3c8fcbSPoul-Henning Kamp 	     unp = LIST_NEXT(unp, unp_link)) {
11208a7d8cc6SRobert Watson 		if (unp->unp_gencnt <= gencnt) {
1121a854ed98SJohn Baldwin 			if (cr_cansee(req->td->td_ucred,
11228a7d8cc6SRobert Watson 			    unp->unp_socket->so_cred))
11234787fd37SPaul Saab 				continue;
112498271db4SGarrett Wollman 			unp_list[i++] = unp;
112598271db4SGarrett Wollman 		}
11264787fd37SPaul Saab 	}
11270d9ce3a1SRobert Watson 	UNP_UNLOCK();
112898271db4SGarrett Wollman 	n = i;			/* in case we lost some during malloc */
112998271db4SGarrett Wollman 
113098271db4SGarrett Wollman 	error = 0;
1131fe2eee82SColin Percival 	xu = malloc(sizeof(*xu), M_TEMP, M_WAITOK | M_ZERO);
113298271db4SGarrett Wollman 	for (i = 0; i < n; i++) {
113398271db4SGarrett Wollman 		unp = unp_list[i];
113498271db4SGarrett Wollman 		if (unp->unp_gencnt <= gencnt) {
11358f364875SJulian Elischer 			xu->xu_len = sizeof *xu;
11368f364875SJulian Elischer 			xu->xu_unpp = unp;
113798271db4SGarrett Wollman 			/*
113898271db4SGarrett Wollman 			 * XXX - need more locking here to protect against
113998271db4SGarrett Wollman 			 * connect/disconnect races for SMP.
114098271db4SGarrett Wollman 			 */
1141fc3fcacfSRobert Watson 			if (unp->unp_addr != NULL)
11428f364875SJulian Elischer 				bcopy(unp->unp_addr, &xu->xu_addr,
114398271db4SGarrett Wollman 				      unp->unp_addr->sun_len);
1144fc3fcacfSRobert Watson 			if (unp->unp_conn != NULL &&
1145fc3fcacfSRobert Watson 			    unp->unp_conn->unp_addr != NULL)
114698271db4SGarrett Wollman 				bcopy(unp->unp_conn->unp_addr,
11478f364875SJulian Elischer 				      &xu->xu_caddr,
114898271db4SGarrett Wollman 				      unp->unp_conn->unp_addr->sun_len);
11498f364875SJulian Elischer 			bcopy(unp, &xu->xu_unp, sizeof *unp);
11508f364875SJulian Elischer 			sotoxsocket(unp->unp_socket, &xu->xu_socket);
11518f364875SJulian Elischer 			error = SYSCTL_OUT(req, xu, sizeof *xu);
115298271db4SGarrett Wollman 		}
115398271db4SGarrett Wollman 	}
11548f364875SJulian Elischer 	free(xu, M_TEMP);
115598271db4SGarrett Wollman 	if (!error) {
115698271db4SGarrett Wollman 		/*
115798271db4SGarrett Wollman 		 * Give the user an updated idea of our state.
115898271db4SGarrett Wollman 		 * If the generation differs from what we told
115998271db4SGarrett Wollman 		 * her before, she knows that something happened
116098271db4SGarrett Wollman 		 * while we were processing this request, and it
116198271db4SGarrett Wollman 		 * might be necessary to retry.
116298271db4SGarrett Wollman 		 */
11638f364875SJulian Elischer 		xug->xug_gen = unp_gencnt;
11648f364875SJulian Elischer 		xug->xug_sogen = so_gencnt;
11658f364875SJulian Elischer 		xug->xug_count = unp_count;
11668f364875SJulian Elischer 		error = SYSCTL_OUT(req, xug, sizeof *xug);
116798271db4SGarrett Wollman 	}
116898271db4SGarrett Wollman 	free(unp_list, M_TEMP);
11698f364875SJulian Elischer 	free(xug, M_TEMP);
1170e5aeaa0cSDag-Erling Smørgrav 	return (error);
117198271db4SGarrett Wollman }
117298271db4SGarrett Wollman 
117398271db4SGarrett Wollman SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLFLAG_RD,
117498271db4SGarrett Wollman 	    (caddr_t)(long)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb",
117598271db4SGarrett Wollman 	    "List of active local datagram sockets");
117698271db4SGarrett Wollman SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, CTLFLAG_RD,
117798271db4SGarrett Wollman 	    (caddr_t)(long)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb",
117898271db4SGarrett Wollman 	    "List of active local stream sockets");
117998271db4SGarrett Wollman 
1180f708ef1bSPoul-Henning Kamp static void
1181892af6b9SRobert Watson unp_shutdown(struct unpcb *unp)
1182df8bae1dSRodney W. Grimes {
1183df8bae1dSRodney W. Grimes 	struct socket *so;
1184df8bae1dSRodney W. Grimes 
11850d9ce3a1SRobert Watson 	UNP_LOCK_ASSERT();
11860d9ce3a1SRobert Watson 
1187df8bae1dSRodney W. Grimes 	if (unp->unp_socket->so_type == SOCK_STREAM && unp->unp_conn &&
1188df8bae1dSRodney W. Grimes 	    (so = unp->unp_conn->unp_socket))
1189df8bae1dSRodney W. Grimes 		socantrcvmore(so);
1190df8bae1dSRodney W. Grimes }
1191df8bae1dSRodney W. Grimes 
1192f708ef1bSPoul-Henning Kamp static void
1193892af6b9SRobert Watson unp_drop(struct unpcb *unp, int errno)
1194df8bae1dSRodney W. Grimes {
1195df8bae1dSRodney W. Grimes 	struct socket *so = unp->unp_socket;
1196df8bae1dSRodney W. Grimes 
11970d9ce3a1SRobert Watson 	UNP_LOCK_ASSERT();
11980d9ce3a1SRobert Watson 
1199df8bae1dSRodney W. Grimes 	so->so_error = errno;
1200df8bae1dSRodney W. Grimes 	unp_disconnect(unp);
1201df8bae1dSRodney W. Grimes }
1202df8bae1dSRodney W. Grimes 
12032bc21ed9SDavid Malone static void
1204892af6b9SRobert Watson unp_freerights(struct file **rp, int fdcount)
1205df8bae1dSRodney W. Grimes {
12062bc21ed9SDavid Malone 	int i;
12072bc21ed9SDavid Malone 	struct file *fp;
1208df8bae1dSRodney W. Grimes 
12092bc21ed9SDavid Malone 	for (i = 0; i < fdcount; i++) {
1210df8bae1dSRodney W. Grimes 		fp = *rp;
12118692c025SYoshinobu Inoue 		/*
12122bc21ed9SDavid Malone 		 * zero the pointer before calling
12132bc21ed9SDavid Malone 		 * unp_discard since it may end up
12142bc21ed9SDavid Malone 		 * in unp_gc()..
1215d7dca903SRobert Watson 		 *
1216d7dca903SRobert Watson 		 * XXXRW: This is less true than it used to be.
12178692c025SYoshinobu Inoue 		 */
1218df8bae1dSRodney W. Grimes 		*rp++ = 0;
12198692c025SYoshinobu Inoue 		unp_discard(fp);
1220df8bae1dSRodney W. Grimes 	}
12212bc21ed9SDavid Malone }
12222bc21ed9SDavid Malone 
12232bc21ed9SDavid Malone int
1224892af6b9SRobert Watson unp_externalize(struct mbuf *control, struct mbuf **controlp)
12252bc21ed9SDavid Malone {
12262bc21ed9SDavid Malone 	struct thread *td = curthread;		/* XXX */
12272bc21ed9SDavid Malone 	struct cmsghdr *cm = mtod(control, struct cmsghdr *);
12282bc21ed9SDavid Malone 	int i;
12292bc21ed9SDavid Malone 	int *fdp;
12302bc21ed9SDavid Malone 	struct file **rp;
12312bc21ed9SDavid Malone 	struct file *fp;
12322bc21ed9SDavid Malone 	void *data;
12332bc21ed9SDavid Malone 	socklen_t clen = control->m_len, datalen;
12342bc21ed9SDavid Malone 	int error, newfds;
12352bc21ed9SDavid Malone 	int f;
12362bc21ed9SDavid Malone 	u_int newlen;
12372bc21ed9SDavid Malone 
12384c5bc1caSRobert Watson 	UNP_UNLOCK_ASSERT();
12394c5bc1caSRobert Watson 
12402bc21ed9SDavid Malone 	error = 0;
12412bc21ed9SDavid Malone 	if (controlp != NULL) /* controlp == NULL => free control messages */
12422bc21ed9SDavid Malone 		*controlp = NULL;
12432bc21ed9SDavid Malone 
12442bc21ed9SDavid Malone 	while (cm != NULL) {
12452bc21ed9SDavid Malone 		if (sizeof(*cm) > clen || cm->cmsg_len > clen) {
12462bc21ed9SDavid Malone 			error = EINVAL;
12472bc21ed9SDavid Malone 			break;
12482bc21ed9SDavid Malone 		}
12492bc21ed9SDavid Malone 
12502bc21ed9SDavid Malone 		data = CMSG_DATA(cm);
12512bc21ed9SDavid Malone 		datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
12522bc21ed9SDavid Malone 
12532bc21ed9SDavid Malone 		if (cm->cmsg_level == SOL_SOCKET
12542bc21ed9SDavid Malone 		    && cm->cmsg_type == SCM_RIGHTS) {
12552bc21ed9SDavid Malone 			newfds = datalen / sizeof(struct file *);
12562bc21ed9SDavid Malone 			rp = data;
12572bc21ed9SDavid Malone 
1258e2f9a08bSOlivier Houchard 			/* If we're not outputting the descriptors free them. */
12592bc21ed9SDavid Malone 			if (error || controlp == NULL) {
12602bc21ed9SDavid Malone 				unp_freerights(rp, newfds);
12612bc21ed9SDavid Malone 				goto next;
12622bc21ed9SDavid Malone 			}
1263426da3bcSAlfred Perlstein 			FILEDESC_LOCK(td->td_proc->p_fd);
12642bc21ed9SDavid Malone 			/* if the new FD's will not fit free them.  */
12652bc21ed9SDavid Malone 			if (!fdavail(td, newfds)) {
1266426da3bcSAlfred Perlstein 				FILEDESC_UNLOCK(td->td_proc->p_fd);
12672bc21ed9SDavid Malone 				error = EMSGSIZE;
12682bc21ed9SDavid Malone 				unp_freerights(rp, newfds);
12692bc21ed9SDavid Malone 				goto next;
1270df8bae1dSRodney W. Grimes 			}
1271ed5b7817SJulian Elischer 			/*
12722bc21ed9SDavid Malone 			 * now change each pointer to an fd in the global
12732bc21ed9SDavid Malone 			 * table to an integer that is the index to the
12742bc21ed9SDavid Malone 			 * local fd table entry that we set up to point
12752bc21ed9SDavid Malone 			 * to the global one we are transferring.
1276ed5b7817SJulian Elischer 			 */
12772bc21ed9SDavid Malone 			newlen = newfds * sizeof(int);
12782bc21ed9SDavid Malone 			*controlp = sbcreatecontrol(NULL, newlen,
12792bc21ed9SDavid Malone 			    SCM_RIGHTS, SOL_SOCKET);
12802bc21ed9SDavid Malone 			if (*controlp == NULL) {
1281426da3bcSAlfred Perlstein 				FILEDESC_UNLOCK(td->td_proc->p_fd);
12822bc21ed9SDavid Malone 				error = E2BIG;
12832bc21ed9SDavid Malone 				unp_freerights(rp, newfds);
12842bc21ed9SDavid Malone 				goto next;
12852bc21ed9SDavid Malone 			}
12862bc21ed9SDavid Malone 
12872bc21ed9SDavid Malone 			fdp = (int *)
12882bc21ed9SDavid Malone 			    CMSG_DATA(mtod(*controlp, struct cmsghdr *));
1289df8bae1dSRodney W. Grimes 			for (i = 0; i < newfds; i++) {
1290a6d4491cSDag-Erling Smørgrav 				if (fdalloc(td, 0, &f))
12912bc21ed9SDavid Malone 					panic("unp_externalize fdalloc failed");
12928692c025SYoshinobu Inoue 				fp = *rp++;
1293b40ce416SJulian Elischer 				td->td_proc->p_fd->fd_ofiles[f] = fp;
1294426da3bcSAlfred Perlstein 				FILE_LOCK(fp);
1295df8bae1dSRodney W. Grimes 				fp->f_msgcount--;
1296426da3bcSAlfred Perlstein 				FILE_UNLOCK(fp);
1297df8bae1dSRodney W. Grimes 				unp_rights--;
12988692c025SYoshinobu Inoue 				*fdp++ = f;
1299df8bae1dSRodney W. Grimes 			}
1300426da3bcSAlfred Perlstein 			FILEDESC_UNLOCK(td->td_proc->p_fd);
13012bc21ed9SDavid Malone 		} else { /* We can just copy anything else across */
13022bc21ed9SDavid Malone 			if (error || controlp == NULL)
13032bc21ed9SDavid Malone 				goto next;
13042bc21ed9SDavid Malone 			*controlp = sbcreatecontrol(NULL, datalen,
13052bc21ed9SDavid Malone 			    cm->cmsg_type, cm->cmsg_level);
13062bc21ed9SDavid Malone 			if (*controlp == NULL) {
13072bc21ed9SDavid Malone 				error = ENOBUFS;
13082bc21ed9SDavid Malone 				goto next;
13092bc21ed9SDavid Malone 			}
13102bc21ed9SDavid Malone 			bcopy(data,
13112bc21ed9SDavid Malone 			    CMSG_DATA(mtod(*controlp, struct cmsghdr *)),
13122bc21ed9SDavid Malone 			    datalen);
13132bc21ed9SDavid Malone 		}
13142bc21ed9SDavid Malone 
13152bc21ed9SDavid Malone 		controlp = &(*controlp)->m_next;
13162bc21ed9SDavid Malone 
13172bc21ed9SDavid Malone next:
13182bc21ed9SDavid Malone 		if (CMSG_SPACE(datalen) < clen) {
13192bc21ed9SDavid Malone 			clen -= CMSG_SPACE(datalen);
13202bc21ed9SDavid Malone 			cm = (struct cmsghdr *)
13212bc21ed9SDavid Malone 			    ((caddr_t)cm + CMSG_SPACE(datalen));
13228692c025SYoshinobu Inoue 		} else {
13232bc21ed9SDavid Malone 			clen = 0;
13242bc21ed9SDavid Malone 			cm = NULL;
13258692c025SYoshinobu Inoue 		}
13268692c025SYoshinobu Inoue 	}
13278692c025SYoshinobu Inoue 
13282bc21ed9SDavid Malone 	m_freem(control);
13292bc21ed9SDavid Malone 
13302bc21ed9SDavid Malone 	return (error);
1331df8bae1dSRodney W. Grimes }
1332df8bae1dSRodney W. Grimes 
13334f590175SPaul Saab static void
13344f590175SPaul Saab unp_zone_change(void *tag)
13354f590175SPaul Saab {
13364f590175SPaul Saab 
13374f590175SPaul Saab 	uma_zone_set_max(unp_zone, maxsockets);
13384f590175SPaul Saab }
13394f590175SPaul Saab 
134098271db4SGarrett Wollman void
134198271db4SGarrett Wollman unp_init(void)
134298271db4SGarrett Wollman {
13439e9d298aSJeff Roberson 	unp_zone = uma_zcreate("unpcb", sizeof(struct unpcb), NULL, NULL,
13449e9d298aSJeff Roberson 	    NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
1345fc3fcacfSRobert Watson 	if (unp_zone == NULL)
134698271db4SGarrett Wollman 		panic("unp_init");
13474f590175SPaul Saab 	uma_zone_set_max(unp_zone, maxsockets);
13484f590175SPaul Saab 	EVENTHANDLER_REGISTER(maxsockets_change, unp_zone_change,
13494f590175SPaul Saab 	    NULL, EVENTHANDLER_PRI_ANY);
135098271db4SGarrett Wollman 	LIST_INIT(&unp_dhead);
135198271db4SGarrett Wollman 	LIST_INIT(&unp_shead);
1352a0ec558aSRobert Watson 	TASK_INIT(&unp_gc_task, 0, unp_gc, NULL);
13530d9ce3a1SRobert Watson 	UNP_LOCK_INIT();
135498271db4SGarrett Wollman }
135598271db4SGarrett Wollman 
1356f708ef1bSPoul-Henning Kamp static int
1357892af6b9SRobert Watson unp_internalize(struct mbuf **controlp, struct thread *td)
1358df8bae1dSRodney W. Grimes {
13592bc21ed9SDavid Malone 	struct mbuf *control = *controlp;
1360b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
13618692c025SYoshinobu Inoue 	struct filedesc *fdescp = p->p_fd;
13622bc21ed9SDavid Malone 	struct cmsghdr *cm = mtod(control, struct cmsghdr *);
13632bc21ed9SDavid Malone 	struct cmsgcred *cmcred;
13642bc21ed9SDavid Malone 	struct file **rp;
13652bc21ed9SDavid Malone 	struct file *fp;
13662bc21ed9SDavid Malone 	struct timeval *tv;
13672bc21ed9SDavid Malone 	int i, fd, *fdp;
13682bc21ed9SDavid Malone 	void *data;
13692bc21ed9SDavid Malone 	socklen_t clen = control->m_len, datalen;
13702bc21ed9SDavid Malone 	int error, oldfds;
13718692c025SYoshinobu Inoue 	u_int newlen;
1372df8bae1dSRodney W. Grimes 
13734c5bc1caSRobert Watson 	UNP_UNLOCK_ASSERT();
13744c5bc1caSRobert Watson 
13752bc21ed9SDavid Malone 	error = 0;
13762bc21ed9SDavid Malone 	*controlp = NULL;
13770b788fa1SBill Paul 
13782bc21ed9SDavid Malone 	while (cm != NULL) {
13792bc21ed9SDavid Malone 		if (sizeof(*cm) > clen || cm->cmsg_level != SOL_SOCKET
13802bc21ed9SDavid Malone 		    || cm->cmsg_len > clen) {
13812bc21ed9SDavid Malone 			error = EINVAL;
13822bc21ed9SDavid Malone 			goto out;
13832bc21ed9SDavid Malone 		}
13842bc21ed9SDavid Malone 
13852bc21ed9SDavid Malone 		data = CMSG_DATA(cm);
13862bc21ed9SDavid Malone 		datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
13872bc21ed9SDavid Malone 
13882bc21ed9SDavid Malone 		switch (cm->cmsg_type) {
13890b788fa1SBill Paul 		/*
13900b788fa1SBill Paul 		 * Fill in credential information.
13910b788fa1SBill Paul 		 */
13922bc21ed9SDavid Malone 		case SCM_CREDS:
13932bc21ed9SDavid Malone 			*controlp = sbcreatecontrol(NULL, sizeof(*cmcred),
13942bc21ed9SDavid Malone 			    SCM_CREDS, SOL_SOCKET);
13952bc21ed9SDavid Malone 			if (*controlp == NULL) {
13962bc21ed9SDavid Malone 				error = ENOBUFS;
13972bc21ed9SDavid Malone 				goto out;
13982bc21ed9SDavid Malone 			}
13992bc21ed9SDavid Malone 
14002bc21ed9SDavid Malone 			cmcred = (struct cmsgcred *)
14012bc21ed9SDavid Malone 			    CMSG_DATA(mtod(*controlp, struct cmsghdr *));
14020b788fa1SBill Paul 			cmcred->cmcred_pid = p->p_pid;
1403a854ed98SJohn Baldwin 			cmcred->cmcred_uid = td->td_ucred->cr_ruid;
1404a854ed98SJohn Baldwin 			cmcred->cmcred_gid = td->td_ucred->cr_rgid;
1405a854ed98SJohn Baldwin 			cmcred->cmcred_euid = td->td_ucred->cr_uid;
1406a854ed98SJohn Baldwin 			cmcred->cmcred_ngroups = MIN(td->td_ucred->cr_ngroups,
14070b788fa1SBill Paul 							CMGROUP_MAX);
14080b788fa1SBill Paul 			for (i = 0; i < cmcred->cmcred_ngroups; i++)
14092bc21ed9SDavid Malone 				cmcred->cmcred_groups[i] =
1410a854ed98SJohn Baldwin 				    td->td_ucred->cr_groups[i];
14112bc21ed9SDavid Malone 			break;
14120b788fa1SBill Paul 
14132bc21ed9SDavid Malone 		case SCM_RIGHTS:
14142bc21ed9SDavid Malone 			oldfds = datalen / sizeof (int);
1415ed5b7817SJulian Elischer 			/*
14162bc21ed9SDavid Malone 			 * check that all the FDs passed in refer to legal files
1417ed5b7817SJulian Elischer 			 * If not, reject the entire operation.
1418ed5b7817SJulian Elischer 			 */
14192bc21ed9SDavid Malone 			fdp = data;
1420426da3bcSAlfred Perlstein 			FILEDESC_LOCK(fdescp);
1421df8bae1dSRodney W. Grimes 			for (i = 0; i < oldfds; i++) {
14228692c025SYoshinobu Inoue 				fd = *fdp++;
14238692c025SYoshinobu Inoue 				if ((unsigned)fd >= fdescp->fd_nfiles ||
14242bc21ed9SDavid Malone 				    fdescp->fd_ofiles[fd] == NULL) {
1425426da3bcSAlfred Perlstein 					FILEDESC_UNLOCK(fdescp);
14262bc21ed9SDavid Malone 					error = EBADF;
14272bc21ed9SDavid Malone 					goto out;
14282bc21ed9SDavid Malone 				}
1429e7d6662fSAlfred Perlstein 				fp = fdescp->fd_ofiles[fd];
1430e7d6662fSAlfred Perlstein 				if (!(fp->f_ops->fo_flags & DFLAG_PASSABLE)) {
1431e7d6662fSAlfred Perlstein 					FILEDESC_UNLOCK(fdescp);
1432e7d6662fSAlfred Perlstein 					error = EOPNOTSUPP;
1433e7d6662fSAlfred Perlstein 					goto out;
1434e7d6662fSAlfred Perlstein 				}
1435e7d6662fSAlfred Perlstein 
1436df8bae1dSRodney W. Grimes 			}
1437ed5b7817SJulian Elischer 			/*
1438ed5b7817SJulian Elischer 			 * Now replace the integer FDs with pointers to
1439ed5b7817SJulian Elischer 			 * the associated global file table entry..
1440ed5b7817SJulian Elischer 			 */
14412bc21ed9SDavid Malone 			newlen = oldfds * sizeof(struct file *);
14422bc21ed9SDavid Malone 			*controlp = sbcreatecontrol(NULL, newlen,
14432bc21ed9SDavid Malone 			    SCM_RIGHTS, SOL_SOCKET);
14442bc21ed9SDavid Malone 			if (*controlp == NULL) {
1445426da3bcSAlfred Perlstein 				FILEDESC_UNLOCK(fdescp);
14462bc21ed9SDavid Malone 				error = E2BIG;
14472bc21ed9SDavid Malone 				goto out;
14488692c025SYoshinobu Inoue 			}
14498692c025SYoshinobu Inoue 
14502bc21ed9SDavid Malone 			fdp = data;
14512bc21ed9SDavid Malone 			rp = (struct file **)
14522bc21ed9SDavid Malone 			    CMSG_DATA(mtod(*controlp, struct cmsghdr *));
14538692c025SYoshinobu Inoue 			for (i = 0; i < oldfds; i++) {
14548692c025SYoshinobu Inoue 				fp = fdescp->fd_ofiles[*fdp++];
1455df8bae1dSRodney W. Grimes 				*rp++ = fp;
1456426da3bcSAlfred Perlstein 				FILE_LOCK(fp);
1457df8bae1dSRodney W. Grimes 				fp->f_count++;
1458df8bae1dSRodney W. Grimes 				fp->f_msgcount++;
1459426da3bcSAlfred Perlstein 				FILE_UNLOCK(fp);
1460df8bae1dSRodney W. Grimes 				unp_rights++;
1461df8bae1dSRodney W. Grimes 			}
1462426da3bcSAlfred Perlstein 			FILEDESC_UNLOCK(fdescp);
14632bc21ed9SDavid Malone 			break;
14642bc21ed9SDavid Malone 
14652bc21ed9SDavid Malone 		case SCM_TIMESTAMP:
14662bc21ed9SDavid Malone 			*controlp = sbcreatecontrol(NULL, sizeof(*tv),
14672bc21ed9SDavid Malone 			    SCM_TIMESTAMP, SOL_SOCKET);
14682bc21ed9SDavid Malone 			if (*controlp == NULL) {
14692bc21ed9SDavid Malone 				error = ENOBUFS;
14702bc21ed9SDavid Malone 				goto out;
14718692c025SYoshinobu Inoue 			}
14722bc21ed9SDavid Malone 			tv = (struct timeval *)
14732bc21ed9SDavid Malone 			    CMSG_DATA(mtod(*controlp, struct cmsghdr *));
14742bc21ed9SDavid Malone 			microtime(tv);
14752bc21ed9SDavid Malone 			break;
14762bc21ed9SDavid Malone 
14772bc21ed9SDavid Malone 		default:
14782bc21ed9SDavid Malone 			error = EINVAL;
14792bc21ed9SDavid Malone 			goto out;
14802bc21ed9SDavid Malone 		}
14812bc21ed9SDavid Malone 
14822bc21ed9SDavid Malone 		controlp = &(*controlp)->m_next;
14832bc21ed9SDavid Malone 
14842bc21ed9SDavid Malone 		if (CMSG_SPACE(datalen) < clen) {
14852bc21ed9SDavid Malone 			clen -= CMSG_SPACE(datalen);
14862bc21ed9SDavid Malone 			cm = (struct cmsghdr *)
14872bc21ed9SDavid Malone 			    ((caddr_t)cm + CMSG_SPACE(datalen));
14882bc21ed9SDavid Malone 		} else {
14892bc21ed9SDavid Malone 			clen = 0;
14902bc21ed9SDavid Malone 			cm = NULL;
14912bc21ed9SDavid Malone 		}
14922bc21ed9SDavid Malone 	}
14932bc21ed9SDavid Malone 
14942bc21ed9SDavid Malone out:
14952bc21ed9SDavid Malone 	m_freem(control);
14962bc21ed9SDavid Malone 
14972bc21ed9SDavid Malone 	return (error);
1498df8bae1dSRodney W. Grimes }
1499df8bae1dSRodney W. Grimes 
15006a2989fdSMatthew N. Dodd struct mbuf *
15016a2989fdSMatthew N. Dodd unp_addsockcred(struct thread *td, struct mbuf *control)
15026a2989fdSMatthew N. Dodd {
150370df31f4SMaxim Konovalov 	struct mbuf *m, *n, *n_prev;
15046a2989fdSMatthew N. Dodd 	struct sockcred *sc;
150570df31f4SMaxim Konovalov 	const struct cmsghdr *cm;
15066a2989fdSMatthew N. Dodd 	int ngroups;
15076a2989fdSMatthew N. Dodd 	int i;
15086a2989fdSMatthew N. Dodd 
15096a2989fdSMatthew N. Dodd 	ngroups = MIN(td->td_ucred->cr_ngroups, CMGROUP_MAX);
15106a2989fdSMatthew N. Dodd 
15116a2989fdSMatthew N. Dodd 	m = sbcreatecontrol(NULL, SOCKCREDSIZE(ngroups), SCM_CREDS, SOL_SOCKET);
15126a2989fdSMatthew N. Dodd 	if (m == NULL)
15136a2989fdSMatthew N. Dodd 		return (control);
15146a2989fdSMatthew N. Dodd 
15156a2989fdSMatthew N. Dodd 	sc = (struct sockcred *) CMSG_DATA(mtod(m, struct cmsghdr *));
15166a2989fdSMatthew N. Dodd 	sc->sc_uid = td->td_ucred->cr_ruid;
15176a2989fdSMatthew N. Dodd 	sc->sc_euid = td->td_ucred->cr_uid;
15186a2989fdSMatthew N. Dodd 	sc->sc_gid = td->td_ucred->cr_rgid;
15196a2989fdSMatthew N. Dodd 	sc->sc_egid = td->td_ucred->cr_gid;
15206a2989fdSMatthew N. Dodd 	sc->sc_ngroups = ngroups;
15216a2989fdSMatthew N. Dodd 	for (i = 0; i < sc->sc_ngroups; i++)
15226a2989fdSMatthew N. Dodd 		sc->sc_groups[i] = td->td_ucred->cr_groups[i];
15236a2989fdSMatthew N. Dodd 
15246a2989fdSMatthew N. Dodd 	/*
152570df31f4SMaxim Konovalov 	 * Unlink SCM_CREDS control messages (struct cmsgcred), since
152670df31f4SMaxim Konovalov 	 * just created SCM_CREDS control message (struct sockcred) has
152770df31f4SMaxim Konovalov 	 * another format.
15286a2989fdSMatthew N. Dodd 	 */
152970df31f4SMaxim Konovalov 	if (control != NULL)
153070df31f4SMaxim Konovalov 		for (n = control, n_prev = NULL; n != NULL;) {
153170df31f4SMaxim Konovalov 			cm = mtod(n, struct cmsghdr *);
153270df31f4SMaxim Konovalov     			if (cm->cmsg_level == SOL_SOCKET &&
153370df31f4SMaxim Konovalov 			    cm->cmsg_type == SCM_CREDS) {
153470df31f4SMaxim Konovalov     				if (n_prev == NULL)
153570df31f4SMaxim Konovalov 					control = n->m_next;
153670df31f4SMaxim Konovalov 				else
153770df31f4SMaxim Konovalov 					n_prev->m_next = n->m_next;
153870df31f4SMaxim Konovalov 				n = m_free(n);
153970df31f4SMaxim Konovalov 			} else {
154070df31f4SMaxim Konovalov 				n_prev = n;
154170df31f4SMaxim Konovalov 				n = n->m_next;
154270df31f4SMaxim Konovalov 			}
154370df31f4SMaxim Konovalov 		}
15446a2989fdSMatthew N. Dodd 
154570df31f4SMaxim Konovalov 	/* Prepend it to the head. */
154670df31f4SMaxim Konovalov 	m->m_next = control;
154770df31f4SMaxim Konovalov 
154870df31f4SMaxim Konovalov 	return (m);
15496a2989fdSMatthew N. Dodd }
15506a2989fdSMatthew N. Dodd 
1551161a0c7cSRobert Watson /*
1552a0ec558aSRobert Watson  * unp_defer indicates whether additional work has been defered for a future
1553a0ec558aSRobert Watson  * pass through unp_gc().  It is thread local and does not require explicit
1554a0ec558aSRobert Watson  * synchronization.
1555161a0c7cSRobert Watson  */
1556a0ec558aSRobert Watson static int	unp_defer;
1557a0ec558aSRobert Watson 
1558a0ec558aSRobert Watson static int unp_taskcount;
1559a0ec558aSRobert Watson SYSCTL_INT(_net_local, OID_AUTO, taskcount, CTLFLAG_RD, &unp_taskcount, 0, "");
1560a0ec558aSRobert Watson 
1561a0ec558aSRobert Watson static int unp_recycled;
1562a0ec558aSRobert Watson SYSCTL_INT(_net_local, OID_AUTO, recycled, CTLFLAG_RD, &unp_recycled, 0, "");
1563df8bae1dSRodney W. Grimes 
1564f708ef1bSPoul-Henning Kamp static void
1565a0ec558aSRobert Watson unp_gc(__unused void *arg, int pending)
1566df8bae1dSRodney W. Grimes {
1567892af6b9SRobert Watson 	struct file *fp, *nextfp;
1568892af6b9SRobert Watson 	struct socket *so;
1569df8bae1dSRodney W. Grimes 	struct file **extra_ref, **fpp;
1570df8bae1dSRodney W. Grimes 	int nunref, i;
157195f004dcSAlfred Perlstein 	int nfiles_snap;
157295f004dcSAlfred Perlstein 	int nfiles_slack = 20;
1573df8bae1dSRodney W. Grimes 
1574a0ec558aSRobert Watson 	unp_taskcount++;
1575df8bae1dSRodney W. Grimes 	unp_defer = 0;
1576ed5b7817SJulian Elischer 	/*
1577ed5b7817SJulian Elischer 	 * before going through all this, set all FDs to
1578ed5b7817SJulian Elischer 	 * be NOT defered and NOT externally accessible
1579ed5b7817SJulian Elischer 	 */
1580426da3bcSAlfred Perlstein 	sx_slock(&filelist_lock);
15812e3c8fcbSPoul-Henning Kamp 	LIST_FOREACH(fp, &filehead, f_list)
1582426da3bcSAlfred Perlstein 		fp->f_gcflag &= ~(FMARK|FDEFER);
1583df8bae1dSRodney W. Grimes 	do {
15845bb84bc8SRobert Watson 		KASSERT(unp_defer >= 0, ("unp_gc: unp_defer %d", unp_defer));
15852e3c8fcbSPoul-Henning Kamp 		LIST_FOREACH(fp, &filehead, f_list) {
1586426da3bcSAlfred Perlstein 			FILE_LOCK(fp);
1587ed5b7817SJulian Elischer 			/*
1588a0ec558aSRobert Watson 			 * If the file is not open, skip it -- could be a
1589a0ec558aSRobert Watson 			 * file in the process of being opened, or in the
1590a0ec558aSRobert Watson 			 * process of being closed.  If the file is
1591a0ec558aSRobert Watson 			 * "closing", it may have been marked for deferred
1592a0ec558aSRobert Watson 			 * consideration.  Clear the flag now if so.
1593ed5b7817SJulian Elischer 			 */
1594426da3bcSAlfred Perlstein 			if (fp->f_count == 0) {
1595a0ec558aSRobert Watson 				if (fp->f_gcflag & FDEFER)
1596a0ec558aSRobert Watson 					unp_defer--;
1597a0ec558aSRobert Watson 				fp->f_gcflag &= ~(FMARK|FDEFER);
1598426da3bcSAlfred Perlstein 				FILE_UNLOCK(fp);
1599df8bae1dSRodney W. Grimes 				continue;
1600426da3bcSAlfred Perlstein 			}
1601ed5b7817SJulian Elischer 			/*
1602ed5b7817SJulian Elischer 			 * If we already marked it as 'defer'  in a
1603ed5b7817SJulian Elischer 			 * previous pass, then try process it this time
1604ed5b7817SJulian Elischer 			 * and un-mark it
1605ed5b7817SJulian Elischer 			 */
1606426da3bcSAlfred Perlstein 			if (fp->f_gcflag & FDEFER) {
1607426da3bcSAlfred Perlstein 				fp->f_gcflag &= ~FDEFER;
1608df8bae1dSRodney W. Grimes 				unp_defer--;
1609df8bae1dSRodney W. Grimes 			} else {
1610ed5b7817SJulian Elischer 				/*
1611ed5b7817SJulian Elischer 				 * if it's not defered, then check if it's
1612ed5b7817SJulian Elischer 				 * already marked.. if so skip it
1613ed5b7817SJulian Elischer 				 */
1614426da3bcSAlfred Perlstein 				if (fp->f_gcflag & FMARK) {
1615426da3bcSAlfred Perlstein 					FILE_UNLOCK(fp);
1616df8bae1dSRodney W. Grimes 					continue;
1617426da3bcSAlfred Perlstein 				}
1618ed5b7817SJulian Elischer 				/*
1619ed5b7817SJulian Elischer 				 * If all references are from messages
1620ed5b7817SJulian Elischer 				 * in transit, then skip it. it's not
1621ed5b7817SJulian Elischer 				 * externally accessible.
1622ed5b7817SJulian Elischer 				 */
1623426da3bcSAlfred Perlstein 				if (fp->f_count == fp->f_msgcount) {
1624426da3bcSAlfred Perlstein 					FILE_UNLOCK(fp);
1625df8bae1dSRodney W. Grimes 					continue;
1626426da3bcSAlfred Perlstein 				}
1627ed5b7817SJulian Elischer 				/*
1628ed5b7817SJulian Elischer 				 * If it got this far then it must be
1629ed5b7817SJulian Elischer 				 * externally accessible.
1630ed5b7817SJulian Elischer 				 */
1631426da3bcSAlfred Perlstein 				fp->f_gcflag |= FMARK;
1632df8bae1dSRodney W. Grimes 			}
1633ed5b7817SJulian Elischer 			/*
1634ed5b7817SJulian Elischer 			 * either it was defered, or it is externally
1635ed5b7817SJulian Elischer 			 * accessible and not already marked so.
1636ed5b7817SJulian Elischer 			 * Now check if it is possibly one of OUR sockets.
1637ed5b7817SJulian Elischer 			 */
1638df8bae1dSRodney W. Grimes 			if (fp->f_type != DTYPE_SOCKET ||
163948e3128bSMatthew Dillon 			    (so = fp->f_data) == NULL) {
1640426da3bcSAlfred Perlstein 				FILE_UNLOCK(fp);
1641df8bae1dSRodney W. Grimes 				continue;
1642426da3bcSAlfred Perlstein 			}
1643426da3bcSAlfred Perlstein 			FILE_UNLOCK(fp);
1644748e0b0aSGarrett Wollman 			if (so->so_proto->pr_domain != &localdomain ||
1645df8bae1dSRodney W. Grimes 			    (so->so_proto->pr_flags&PR_RIGHTS) == 0)
1646df8bae1dSRodney W. Grimes 				continue;
1647ed5b7817SJulian Elischer 			/*
1648ed5b7817SJulian Elischer 			 * So, Ok, it's one of our sockets and it IS externally
1649ed5b7817SJulian Elischer 			 * accessible (or was defered). Now we look
1650dc733423SDag-Erling Smørgrav 			 * to see if we hold any file descriptors in its
1651ed5b7817SJulian Elischer 			 * message buffers. Follow those links and mark them
1652ed5b7817SJulian Elischer 			 * as accessible too.
1653ed5b7817SJulian Elischer 			 */
16547717cf07SRobert Watson 			SOCKBUF_LOCK(&so->so_rcv);
1655df8bae1dSRodney W. Grimes 			unp_scan(so->so_rcv.sb_mb, unp_mark);
16567717cf07SRobert Watson 			SOCKBUF_UNLOCK(&so->so_rcv);
1657df8bae1dSRodney W. Grimes 		}
1658df8bae1dSRodney W. Grimes 	} while (unp_defer);
1659426da3bcSAlfred Perlstein 	sx_sunlock(&filelist_lock);
1660df8bae1dSRodney W. Grimes 	/*
1661a0ec558aSRobert Watson 	 * XXXRW: The following comments need updating for a post-SMPng and
1662a0ec558aSRobert Watson 	 * deferred unp_gc() world, but are still generally accurate.
1663a0ec558aSRobert Watson 	 *
1664df8bae1dSRodney W. Grimes 	 * We grab an extra reference to each of the file table entries
1665df8bae1dSRodney W. Grimes 	 * that are not otherwise accessible and then free the rights
1666df8bae1dSRodney W. Grimes 	 * that are stored in messages on them.
1667df8bae1dSRodney W. Grimes 	 *
1668df8bae1dSRodney W. Grimes 	 * The bug in the orginal code is a little tricky, so I'll describe
1669df8bae1dSRodney W. Grimes 	 * what's wrong with it here.
1670df8bae1dSRodney W. Grimes 	 *
1671df8bae1dSRodney W. Grimes 	 * It is incorrect to simply unp_discard each entry for f_msgcount
1672df8bae1dSRodney W. Grimes 	 * times -- consider the case of sockets A and B that contain
1673df8bae1dSRodney W. Grimes 	 * references to each other.  On a last close of some other socket,
1674df8bae1dSRodney W. Grimes 	 * we trigger a gc since the number of outstanding rights (unp_rights)
1675a0ec558aSRobert Watson 	 * is non-zero.  If during the sweep phase the gc code unp_discards,
1676df8bae1dSRodney W. Grimes 	 * we end up doing a (full) closef on the descriptor.  A closef on A
1677df8bae1dSRodney W. Grimes 	 * results in the following chain.  Closef calls soo_close, which
1678df8bae1dSRodney W. Grimes 	 * calls soclose.   Soclose calls first (through the switch
1679df8bae1dSRodney W. Grimes 	 * uipc_usrreq) unp_detach, which re-invokes unp_gc.  Unp_gc simply
1680df8bae1dSRodney W. Grimes 	 * returns because the previous instance had set unp_gcing, and
1681df8bae1dSRodney W. Grimes 	 * we return all the way back to soclose, which marks the socket
1682df8bae1dSRodney W. Grimes 	 * with SS_NOFDREF, and then calls sofree.  Sofree calls sorflush
1683df8bae1dSRodney W. Grimes 	 * to free up the rights that are queued in messages on the socket A,
1684df8bae1dSRodney W. Grimes 	 * i.e., the reference on B.  The sorflush calls via the dom_dispose
1685df8bae1dSRodney W. Grimes 	 * switch unp_dispose, which unp_scans with unp_discard.  This second
1686df8bae1dSRodney W. Grimes 	 * instance of unp_discard just calls closef on B.
1687df8bae1dSRodney W. Grimes 	 *
1688df8bae1dSRodney W. Grimes 	 * Well, a similar chain occurs on B, resulting in a sorflush on B,
1689df8bae1dSRodney W. Grimes 	 * which results in another closef on A.  Unfortunately, A is already
1690df8bae1dSRodney W. Grimes 	 * being closed, and the descriptor has already been marked with
1691df8bae1dSRodney W. Grimes 	 * SS_NOFDREF, and soclose panics at this point.
1692df8bae1dSRodney W. Grimes 	 *
1693df8bae1dSRodney W. Grimes 	 * Here, we first take an extra reference to each inaccessible
1694df8bae1dSRodney W. Grimes 	 * descriptor.  Then, we call sorflush ourself, since we know
1695df8bae1dSRodney W. Grimes 	 * it is a Unix domain socket anyhow.  After we destroy all the
1696df8bae1dSRodney W. Grimes 	 * rights carried in messages, we do a last closef to get rid
1697df8bae1dSRodney W. Grimes 	 * of our extra reference.  This is the last close, and the
1698df8bae1dSRodney W. Grimes 	 * unp_detach etc will shut down the socket.
1699df8bae1dSRodney W. Grimes 	 *
1700df8bae1dSRodney W. Grimes 	 * 91/09/19, bsy@cs.cmu.edu
1701df8bae1dSRodney W. Grimes 	 */
170295f004dcSAlfred Perlstein again:
1703e4643c73SPoul-Henning Kamp 	nfiles_snap = openfiles + nfiles_slack;	/* some slack */
170495f004dcSAlfred Perlstein 	extra_ref = malloc(nfiles_snap * sizeof(struct file *), M_TEMP,
170595f004dcSAlfred Perlstein 	    M_WAITOK);
1706426da3bcSAlfred Perlstein 	sx_slock(&filelist_lock);
1707e4643c73SPoul-Henning Kamp 	if (nfiles_snap < openfiles) {
170895f004dcSAlfred Perlstein 		sx_sunlock(&filelist_lock);
170995f004dcSAlfred Perlstein 		free(extra_ref, M_TEMP);
171095f004dcSAlfred Perlstein 		nfiles_slack += 20;
171195f004dcSAlfred Perlstein 		goto again;
171295f004dcSAlfred Perlstein 	}
1713fc3fcacfSRobert Watson 	for (nunref = 0, fp = LIST_FIRST(&filehead), fpp = extra_ref;
1714fc3fcacfSRobert Watson 	    fp != NULL; fp = nextfp) {
17152e3c8fcbSPoul-Henning Kamp 		nextfp = LIST_NEXT(fp, f_list);
1716426da3bcSAlfred Perlstein 		FILE_LOCK(fp);
1717ed5b7817SJulian Elischer 		/*
1718ed5b7817SJulian Elischer 		 * If it's not open, skip it
1719ed5b7817SJulian Elischer 		 */
1720426da3bcSAlfred Perlstein 		if (fp->f_count == 0) {
1721426da3bcSAlfred Perlstein 			FILE_UNLOCK(fp);
1722df8bae1dSRodney W. Grimes 			continue;
1723426da3bcSAlfred Perlstein 		}
1724ed5b7817SJulian Elischer 		/*
1725ed5b7817SJulian Elischer 		 * If all refs are from msgs, and it's not marked accessible
1726ed5b7817SJulian Elischer 		 * then it must be referenced from some unreachable cycle
1727ed5b7817SJulian Elischer 		 * of (shut-down) FDs, so include it in our
1728ed5b7817SJulian Elischer 		 * list of FDs to remove
1729ed5b7817SJulian Elischer 		 */
1730426da3bcSAlfred Perlstein 		if (fp->f_count == fp->f_msgcount && !(fp->f_gcflag & FMARK)) {
1731df8bae1dSRodney W. Grimes 			*fpp++ = fp;
1732df8bae1dSRodney W. Grimes 			nunref++;
1733df8bae1dSRodney W. Grimes 			fp->f_count++;
1734df8bae1dSRodney W. Grimes 		}
1735426da3bcSAlfred Perlstein 		FILE_UNLOCK(fp);
1736df8bae1dSRodney W. Grimes 	}
1737426da3bcSAlfred Perlstein 	sx_sunlock(&filelist_lock);
1738ed5b7817SJulian Elischer 	/*
1739ed5b7817SJulian Elischer 	 * for each FD on our hit list, do the following two things
1740ed5b7817SJulian Elischer 	 */
17411c7c3c6aSMatthew Dillon 	for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) {
17421c7c3c6aSMatthew Dillon 		struct file *tfp = *fpp;
1743426da3bcSAlfred Perlstein 		FILE_LOCK(tfp);
1744cd72f218SMatthew Dillon 		if (tfp->f_type == DTYPE_SOCKET &&
174548e3128bSMatthew Dillon 		    tfp->f_data != NULL) {
1746426da3bcSAlfred Perlstein 			FILE_UNLOCK(tfp);
174748e3128bSMatthew Dillon 			sorflush(tfp->f_data);
1748e5aeaa0cSDag-Erling Smørgrav 		} else {
1749426da3bcSAlfred Perlstein 			FILE_UNLOCK(tfp);
17501c7c3c6aSMatthew Dillon 		}
1751e5aeaa0cSDag-Erling Smørgrav 	}
1752a0ec558aSRobert Watson 	for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) {
1753b40ce416SJulian Elischer 		closef(*fpp, (struct thread *) NULL);
1754a0ec558aSRobert Watson 		unp_recycled++;
1755a0ec558aSRobert Watson 	}
1756210a5a71SAlfred Perlstein 	free(extra_ref, M_TEMP);
1757df8bae1dSRodney W. Grimes }
1758df8bae1dSRodney W. Grimes 
175926f9a767SRodney W. Grimes void
1760892af6b9SRobert Watson unp_dispose(struct mbuf *m)
1761df8bae1dSRodney W. Grimes {
1762996c772fSJohn Dyson 
1763df8bae1dSRodney W. Grimes 	if (m)
1764df8bae1dSRodney W. Grimes 		unp_scan(m, unp_discard);
1765df8bae1dSRodney W. Grimes }
1766df8bae1dSRodney W. Grimes 
17670c1bb4fbSDima Dorfman static int
1768d374e81eSRobert Watson unp_listen(struct socket *so, struct unpcb *unp, int backlog,
1769d374e81eSRobert Watson     struct thread *td)
17700c1bb4fbSDima Dorfman {
17710daccb9cSRobert Watson 	int error;
17720daccb9cSRobert Watson 
17730d9ce3a1SRobert Watson 	UNP_LOCK_ASSERT();
17740c1bb4fbSDima Dorfman 
17750daccb9cSRobert Watson 	SOCK_LOCK(so);
17760daccb9cSRobert Watson 	error = solisten_proto_check(so);
17770daccb9cSRobert Watson 	if (error == 0) {
17786f105b34SJohn Baldwin 		cru2x(td->td_ucred, &unp->unp_peercred);
17790c1bb4fbSDima Dorfman 		unp->unp_flags |= UNP_HAVEPCCACHED;
1780d374e81eSRobert Watson 		solisten_proto(so, backlog);
17810daccb9cSRobert Watson 	}
17820daccb9cSRobert Watson 	SOCK_UNLOCK(so);
17830daccb9cSRobert Watson 	return (error);
17840c1bb4fbSDima Dorfman }
17850c1bb4fbSDima Dorfman 
1786f708ef1bSPoul-Henning Kamp static void
1787892af6b9SRobert Watson unp_scan(struct mbuf *m0, void (*op)(struct file *))
1788df8bae1dSRodney W. Grimes {
17892bc21ed9SDavid Malone 	struct mbuf *m;
17902bc21ed9SDavid Malone 	struct file **rp;
17912bc21ed9SDavid Malone 	struct cmsghdr *cm;
17922bc21ed9SDavid Malone 	void *data;
17932bc21ed9SDavid Malone 	int i;
17942bc21ed9SDavid Malone 	socklen_t clen, datalen;
1795df8bae1dSRodney W. Grimes 	int qfds;
1796df8bae1dSRodney W. Grimes 
1797fc3fcacfSRobert Watson 	while (m0 != NULL) {
17982bc21ed9SDavid Malone 		for (m = m0; m; m = m->m_next) {
179912396bdcSDavid Malone 			if (m->m_type != MT_CONTROL)
1800df8bae1dSRodney W. Grimes 				continue;
18012bc21ed9SDavid Malone 
18022bc21ed9SDavid Malone 			cm = mtod(m, struct cmsghdr *);
18032bc21ed9SDavid Malone 			clen = m->m_len;
18042bc21ed9SDavid Malone 
18052bc21ed9SDavid Malone 			while (cm != NULL) {
18062bc21ed9SDavid Malone 				if (sizeof(*cm) > clen || cm->cmsg_len > clen)
18072bc21ed9SDavid Malone 					break;
18082bc21ed9SDavid Malone 
18092bc21ed9SDavid Malone 				data = CMSG_DATA(cm);
18102bc21ed9SDavid Malone 				datalen = (caddr_t)cm + cm->cmsg_len
18112bc21ed9SDavid Malone 				    - (caddr_t)data;
18122bc21ed9SDavid Malone 
18132bc21ed9SDavid Malone 				if (cm->cmsg_level == SOL_SOCKET &&
18142bc21ed9SDavid Malone 				    cm->cmsg_type == SCM_RIGHTS) {
18152bc21ed9SDavid Malone 					qfds = datalen / sizeof (struct file *);
18162bc21ed9SDavid Malone 					rp = data;
1817df8bae1dSRodney W. Grimes 					for (i = 0; i < qfds; i++)
1818df8bae1dSRodney W. Grimes 						(*op)(*rp++);
18192bc21ed9SDavid Malone 				}
18202bc21ed9SDavid Malone 
18212bc21ed9SDavid Malone 				if (CMSG_SPACE(datalen) < clen) {
18222bc21ed9SDavid Malone 					clen -= CMSG_SPACE(datalen);
18232bc21ed9SDavid Malone 					cm = (struct cmsghdr *)
18242bc21ed9SDavid Malone 					    ((caddr_t)cm + CMSG_SPACE(datalen));
18252bc21ed9SDavid Malone 				} else {
18262bc21ed9SDavid Malone 					clen = 0;
18272bc21ed9SDavid Malone 					cm = NULL;
18282bc21ed9SDavid Malone 				}
18292bc21ed9SDavid Malone 			}
1830df8bae1dSRodney W. Grimes 		}
1831df8bae1dSRodney W. Grimes 		m0 = m0->m_act;
1832df8bae1dSRodney W. Grimes 	}
1833df8bae1dSRodney W. Grimes }
1834df8bae1dSRodney W. Grimes 
1835f708ef1bSPoul-Henning Kamp static void
1836892af6b9SRobert Watson unp_mark(struct file *fp)
1837df8bae1dSRodney W. Grimes {
1838426da3bcSAlfred Perlstein 	if (fp->f_gcflag & FMARK)
1839df8bae1dSRodney W. Grimes 		return;
1840df8bae1dSRodney W. Grimes 	unp_defer++;
1841426da3bcSAlfred Perlstein 	fp->f_gcflag |= (FMARK|FDEFER);
1842df8bae1dSRodney W. Grimes }
1843df8bae1dSRodney W. Grimes 
1844f708ef1bSPoul-Henning Kamp static void
1845892af6b9SRobert Watson unp_discard(struct file *fp)
1846df8bae1dSRodney W. Grimes {
1847a0ec558aSRobert Watson 	UNP_LOCK();
1848426da3bcSAlfred Perlstein 	FILE_LOCK(fp);
1849df8bae1dSRodney W. Grimes 	fp->f_msgcount--;
1850df8bae1dSRodney W. Grimes 	unp_rights--;
1851426da3bcSAlfred Perlstein 	FILE_UNLOCK(fp);
1852a0ec558aSRobert Watson 	UNP_UNLOCK();
1853b40ce416SJulian Elischer 	(void) closef(fp, (struct thread *)NULL);
1854df8bae1dSRodney W. Grimes }
1855