xref: /freebsd/sys/kern/uipc_usrreq.c (revision 0b788fa1dae7ae26d13e1e77c1476b4697bb2d2d)
1df8bae1dSRodney W. Grimes /*
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1989, 1991, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
14df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
15df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
16df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
17df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
18df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
19df8bae1dSRodney W. Grimes  *    without specific prior written permission.
20df8bae1dSRodney W. Grimes  *
21df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
32df8bae1dSRodney W. Grimes  *
33748e0b0aSGarrett Wollman  *	From: @(#)uipc_usrreq.c	8.3 (Berkeley) 1/4/94
340b788fa1SBill Paul  *	$Id: uipc_usrreq.c,v 1.20 1997/02/24 20:30:58 wollman Exp $
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
37df8bae1dSRodney W. Grimes #include <sys/param.h>
382ee45d7dSDavid Greenman #include <sys/queue.h>
39df8bae1dSRodney W. Grimes #include <sys/systm.h>
40639acc13SGarrett Wollman #include <sys/kernel.h>
41df8bae1dSRodney W. Grimes #include <sys/domain.h>
42639acc13SGarrett Wollman #include <sys/file.h>
43639acc13SGarrett Wollman #include <sys/filedesc.h>
44639acc13SGarrett Wollman #include <sys/mbuf.h>
45639acc13SGarrett Wollman #include <sys/namei.h>
46639acc13SGarrett Wollman #include <sys/proc.h>
47df8bae1dSRodney W. Grimes #include <sys/protosw.h>
48df8bae1dSRodney W. Grimes #include <sys/socket.h>
49df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
50df8bae1dSRodney W. Grimes #include <sys/stat.h>
51639acc13SGarrett Wollman #include <sys/sysctl.h>
52639acc13SGarrett Wollman #include <sys/un.h>
53639acc13SGarrett Wollman #include <sys/unpcb.h>
54639acc13SGarrett Wollman #include <sys/vnode.h>
55df8bae1dSRodney W. Grimes 
56df8bae1dSRodney W. Grimes /*
57df8bae1dSRodney W. Grimes  * Unix communications domain.
58df8bae1dSRodney W. Grimes  *
59df8bae1dSRodney W. Grimes  * TODO:
60df8bae1dSRodney W. Grimes  *	SEQPACKET, RDM
61df8bae1dSRodney W. Grimes  *	rethink name space problems
62df8bae1dSRodney W. Grimes  *	need a proper out-of-band
63df8bae1dSRodney W. Grimes  */
64f708ef1bSPoul-Henning Kamp static struct	sockaddr sun_noname = { sizeof(sun_noname), AF_LOCAL };
65f708ef1bSPoul-Henning Kamp static ino_t	unp_ino;		/* prototype for fake inode numbers */
66f708ef1bSPoul-Henning Kamp 
67f708ef1bSPoul-Henning Kamp static int     unp_attach __P((struct socket *));
68f708ef1bSPoul-Henning Kamp static void    unp_detach __P((struct unpcb *));
69f708ef1bSPoul-Henning Kamp static int     unp_bind __P((struct unpcb *,struct mbuf *, struct proc *));
70f708ef1bSPoul-Henning Kamp static int     unp_connect __P((struct socket *,struct mbuf *, struct proc *));
71f708ef1bSPoul-Henning Kamp static void    unp_disconnect __P((struct unpcb *));
72f708ef1bSPoul-Henning Kamp static void    unp_shutdown __P((struct unpcb *));
73f708ef1bSPoul-Henning Kamp static void    unp_drop __P((struct unpcb *, int));
74f708ef1bSPoul-Henning Kamp static void    unp_gc __P((void));
75f708ef1bSPoul-Henning Kamp static void    unp_scan __P((struct mbuf *, void (*)(struct file *)));
76f708ef1bSPoul-Henning Kamp static void    unp_mark __P((struct file *));
77f708ef1bSPoul-Henning Kamp static void    unp_discard __P((struct file *));
78f708ef1bSPoul-Henning Kamp static int     unp_internalize __P((struct mbuf *, struct proc *));
79f708ef1bSPoul-Henning Kamp 
80df8bae1dSRodney W. Grimes 
81df8bae1dSRodney W. Grimes /*ARGSUSED*/
8226f9a767SRodney W. Grimes int
83df8bae1dSRodney W. Grimes uipc_usrreq(so, req, m, nam, control)
84df8bae1dSRodney W. Grimes 	struct socket *so;
85df8bae1dSRodney W. Grimes 	int req;
86df8bae1dSRodney W. Grimes 	struct mbuf *m, *nam, *control;
87df8bae1dSRodney W. Grimes {
88df8bae1dSRodney W. Grimes 	struct unpcb *unp = sotounpcb(so);
89df8bae1dSRodney W. Grimes 	register struct socket *so2;
90df8bae1dSRodney W. Grimes 	register int error = 0;
91df8bae1dSRodney W. Grimes 	struct proc *p = curproc;	/* XXX */
92df8bae1dSRodney W. Grimes 
93df8bae1dSRodney W. Grimes 	if (req == PRU_CONTROL)
94df8bae1dSRodney W. Grimes 		return (EOPNOTSUPP);
95df8bae1dSRodney W. Grimes 	if (req != PRU_SEND && control && control->m_len) {
96df8bae1dSRodney W. Grimes 		error = EOPNOTSUPP;
97df8bae1dSRodney W. Grimes 		goto release;
98df8bae1dSRodney W. Grimes 	}
99df8bae1dSRodney W. Grimes 	if (unp == 0 && req != PRU_ATTACH) {
100df8bae1dSRodney W. Grimes 		error = EINVAL;
101df8bae1dSRodney W. Grimes 		goto release;
102df8bae1dSRodney W. Grimes 	}
103df8bae1dSRodney W. Grimes 	switch (req) {
104df8bae1dSRodney W. Grimes 
105df8bae1dSRodney W. Grimes 	case PRU_ATTACH:
106df8bae1dSRodney W. Grimes 		if (unp) {
107df8bae1dSRodney W. Grimes 			error = EISCONN;
108df8bae1dSRodney W. Grimes 			break;
109df8bae1dSRodney W. Grimes 		}
110df8bae1dSRodney W. Grimes 		error = unp_attach(so);
111df8bae1dSRodney W. Grimes 		break;
112df8bae1dSRodney W. Grimes 
113df8bae1dSRodney W. Grimes 	case PRU_DETACH:
114df8bae1dSRodney W. Grimes 		unp_detach(unp);
115df8bae1dSRodney W. Grimes 		break;
116df8bae1dSRodney W. Grimes 
117df8bae1dSRodney W. Grimes 	case PRU_BIND:
118df8bae1dSRodney W. Grimes 		error = unp_bind(unp, nam, p);
119df8bae1dSRodney W. Grimes 		break;
120df8bae1dSRodney W. Grimes 
121df8bae1dSRodney W. Grimes 	case PRU_LISTEN:
122df8bae1dSRodney W. Grimes 		if (unp->unp_vnode == 0)
123df8bae1dSRodney W. Grimes 			error = EINVAL;
124df8bae1dSRodney W. Grimes 		break;
125df8bae1dSRodney W. Grimes 
126df8bae1dSRodney W. Grimes 	case PRU_CONNECT:
127df8bae1dSRodney W. Grimes 		error = unp_connect(so, nam, p);
128df8bae1dSRodney W. Grimes 		break;
129df8bae1dSRodney W. Grimes 
130df8bae1dSRodney W. Grimes 	case PRU_CONNECT2:
131df8bae1dSRodney W. Grimes 		error = unp_connect2(so, (struct socket *)nam);
132df8bae1dSRodney W. Grimes 		break;
133df8bae1dSRodney W. Grimes 
134df8bae1dSRodney W. Grimes 	case PRU_DISCONNECT:
135df8bae1dSRodney W. Grimes 		unp_disconnect(unp);
136df8bae1dSRodney W. Grimes 		break;
137df8bae1dSRodney W. Grimes 
138df8bae1dSRodney W. Grimes 	case PRU_ACCEPT:
139df8bae1dSRodney W. Grimes 		/*
140df8bae1dSRodney W. Grimes 		 * Pass back name of connected socket,
141df8bae1dSRodney W. Grimes 		 * if it was bound and we are still connected
142df8bae1dSRodney W. Grimes 		 * (our peer may have closed already!).
143df8bae1dSRodney W. Grimes 		 */
144df8bae1dSRodney W. Grimes 		if (unp->unp_conn && unp->unp_conn->unp_addr) {
145df8bae1dSRodney W. Grimes 			nam->m_len = unp->unp_conn->unp_addr->m_len;
146df8bae1dSRodney W. Grimes 			bcopy(mtod(unp->unp_conn->unp_addr, caddr_t),
147df8bae1dSRodney W. Grimes 			    mtod(nam, caddr_t), (unsigned)nam->m_len);
148df8bae1dSRodney W. Grimes 		} else {
149df8bae1dSRodney W. Grimes 			nam->m_len = sizeof(sun_noname);
150df8bae1dSRodney W. Grimes 			*(mtod(nam, struct sockaddr *)) = sun_noname;
151df8bae1dSRodney W. Grimes 		}
152df8bae1dSRodney W. Grimes 		break;
153df8bae1dSRodney W. Grimes 
154df8bae1dSRodney W. Grimes 	case PRU_SHUTDOWN:
155df8bae1dSRodney W. Grimes 		socantsendmore(so);
156df8bae1dSRodney W. Grimes 		unp_shutdown(unp);
157df8bae1dSRodney W. Grimes 		break;
158df8bae1dSRodney W. Grimes 
159df8bae1dSRodney W. Grimes 	case PRU_RCVD:
160df8bae1dSRodney W. Grimes 		switch (so->so_type) {
161df8bae1dSRodney W. Grimes 
162df8bae1dSRodney W. Grimes 		case SOCK_DGRAM:
163df8bae1dSRodney W. Grimes 			panic("uipc 1");
164df8bae1dSRodney W. Grimes 			/*NOTREACHED*/
165df8bae1dSRodney W. Grimes 
166df8bae1dSRodney W. Grimes 		case SOCK_STREAM:
167df8bae1dSRodney W. Grimes #define	rcv (&so->so_rcv)
168df8bae1dSRodney W. Grimes #define snd (&so2->so_snd)
169df8bae1dSRodney W. Grimes 			if (unp->unp_conn == 0)
170df8bae1dSRodney W. Grimes 				break;
171df8bae1dSRodney W. Grimes 			so2 = unp->unp_conn->unp_socket;
172df8bae1dSRodney W. Grimes 			/*
173df8bae1dSRodney W. Grimes 			 * Adjust backpressure on sender
174df8bae1dSRodney W. Grimes 			 * and wakeup any waiting to write.
175df8bae1dSRodney W. Grimes 			 */
176df8bae1dSRodney W. Grimes 			snd->sb_mbmax += unp->unp_mbcnt - rcv->sb_mbcnt;
177df8bae1dSRodney W. Grimes 			unp->unp_mbcnt = rcv->sb_mbcnt;
178df8bae1dSRodney W. Grimes 			snd->sb_hiwat += unp->unp_cc - rcv->sb_cc;
179df8bae1dSRodney W. Grimes 			unp->unp_cc = rcv->sb_cc;
180df8bae1dSRodney W. Grimes 			sowwakeup(so2);
181df8bae1dSRodney W. Grimes #undef snd
182df8bae1dSRodney W. Grimes #undef rcv
183df8bae1dSRodney W. Grimes 			break;
184df8bae1dSRodney W. Grimes 
185df8bae1dSRodney W. Grimes 		default:
186df8bae1dSRodney W. Grimes 			panic("uipc 2");
187df8bae1dSRodney W. Grimes 		}
188df8bae1dSRodney W. Grimes 		break;
189df8bae1dSRodney W. Grimes 
190df8bae1dSRodney W. Grimes 	case PRU_SEND:
1916b8fda4dSGarrett Wollman 	case PRU_SEND_EOF:
192df8bae1dSRodney W. Grimes 		if (control && (error = unp_internalize(control, p)))
193df8bae1dSRodney W. Grimes 			break;
194df8bae1dSRodney W. Grimes 		switch (so->so_type) {
195df8bae1dSRodney W. Grimes 
196df8bae1dSRodney W. Grimes 		case SOCK_DGRAM: {
197df8bae1dSRodney W. Grimes 			struct sockaddr *from;
198df8bae1dSRodney W. Grimes 
199df8bae1dSRodney W. Grimes 			if (nam) {
200df8bae1dSRodney W. Grimes 				if (unp->unp_conn) {
201df8bae1dSRodney W. Grimes 					error = EISCONN;
202df8bae1dSRodney W. Grimes 					break;
203df8bae1dSRodney W. Grimes 				}
204df8bae1dSRodney W. Grimes 				error = unp_connect(so, nam, p);
205df8bae1dSRodney W. Grimes 				if (error)
206df8bae1dSRodney W. Grimes 					break;
207df8bae1dSRodney W. Grimes 			} else {
208df8bae1dSRodney W. Grimes 				if (unp->unp_conn == 0) {
209df8bae1dSRodney W. Grimes 					error = ENOTCONN;
210df8bae1dSRodney W. Grimes 					break;
211df8bae1dSRodney W. Grimes 				}
212df8bae1dSRodney W. Grimes 			}
213df8bae1dSRodney W. Grimes 			so2 = unp->unp_conn->unp_socket;
214df8bae1dSRodney W. Grimes 			if (unp->unp_addr)
215df8bae1dSRodney W. Grimes 				from = mtod(unp->unp_addr, struct sockaddr *);
216df8bae1dSRodney W. Grimes 			else
217df8bae1dSRodney W. Grimes 				from = &sun_noname;
218df8bae1dSRodney W. Grimes 			if (sbappendaddr(&so2->so_rcv, from, m, control)) {
219df8bae1dSRodney W. Grimes 				sorwakeup(so2);
220df8bae1dSRodney W. Grimes 				m = 0;
221df8bae1dSRodney W. Grimes 				control = 0;
222df8bae1dSRodney W. Grimes 			} else
223df8bae1dSRodney W. Grimes 				error = ENOBUFS;
224df8bae1dSRodney W. Grimes 			if (nam)
225df8bae1dSRodney W. Grimes 				unp_disconnect(unp);
226df8bae1dSRodney W. Grimes 			break;
227df8bae1dSRodney W. Grimes 		}
228df8bae1dSRodney W. Grimes 
229df8bae1dSRodney W. Grimes 		case SOCK_STREAM:
230df8bae1dSRodney W. Grimes #define	rcv (&so2->so_rcv)
231df8bae1dSRodney W. Grimes #define	snd (&so->so_snd)
2326b8fda4dSGarrett Wollman 			/* Connect if not connected yet. */
2336b8fda4dSGarrett Wollman 			/*
2346b8fda4dSGarrett Wollman 			 * Note: A better implementation would complain
235402cc72dSDavid Greenman 			 * if not equal to the peer's address.
2366b8fda4dSGarrett Wollman 			 */
237402cc72dSDavid Greenman 			if ((so->so_state & SS_ISCONNECTED) == 0) {
238402cc72dSDavid Greenman 				if (nam) {
239402cc72dSDavid Greenman 		    			error = unp_connect(so, nam, p);
240402cc72dSDavid Greenman 					if (error)
2416b8fda4dSGarrett Wollman 						break;	/* XXX */
242402cc72dSDavid Greenman 				} else {
243402cc72dSDavid Greenman 					error = ENOTCONN;
244402cc72dSDavid Greenman 					break;
245402cc72dSDavid Greenman 				}
246402cc72dSDavid Greenman 			}
247402cc72dSDavid Greenman 
248df8bae1dSRodney W. Grimes 			if (so->so_state & SS_CANTSENDMORE) {
249df8bae1dSRodney W. Grimes 				error = EPIPE;
250df8bae1dSRodney W. Grimes 				break;
251df8bae1dSRodney W. Grimes 			}
252df8bae1dSRodney W. Grimes 			if (unp->unp_conn == 0)
253df8bae1dSRodney W. Grimes 				panic("uipc 3");
254df8bae1dSRodney W. Grimes 			so2 = unp->unp_conn->unp_socket;
255df8bae1dSRodney W. Grimes 			/*
256df8bae1dSRodney W. Grimes 			 * Send to paired receive port, and then reduce
257df8bae1dSRodney W. Grimes 			 * send buffer hiwater marks to maintain backpressure.
258df8bae1dSRodney W. Grimes 			 * Wake up readers.
259df8bae1dSRodney W. Grimes 			 */
260df8bae1dSRodney W. Grimes 			if (control) {
261df8bae1dSRodney W. Grimes 				if (sbappendcontrol(rcv, m, control))
262df8bae1dSRodney W. Grimes 					control = 0;
263df8bae1dSRodney W. Grimes 			} else
264df8bae1dSRodney W. Grimes 				sbappend(rcv, m);
265df8bae1dSRodney W. Grimes 			snd->sb_mbmax -=
266df8bae1dSRodney W. Grimes 			    rcv->sb_mbcnt - unp->unp_conn->unp_mbcnt;
267df8bae1dSRodney W. Grimes 			unp->unp_conn->unp_mbcnt = rcv->sb_mbcnt;
268df8bae1dSRodney W. Grimes 			snd->sb_hiwat -= rcv->sb_cc - unp->unp_conn->unp_cc;
269df8bae1dSRodney W. Grimes 			unp->unp_conn->unp_cc = rcv->sb_cc;
270df8bae1dSRodney W. Grimes 			sorwakeup(so2);
271df8bae1dSRodney W. Grimes 			m = 0;
272df8bae1dSRodney W. Grimes #undef snd
273df8bae1dSRodney W. Grimes #undef rcv
274df8bae1dSRodney W. Grimes 			break;
275df8bae1dSRodney W. Grimes 
276df8bae1dSRodney W. Grimes 		default:
277df8bae1dSRodney W. Grimes 			panic("uipc 4");
278df8bae1dSRodney W. Grimes 		}
2796b8fda4dSGarrett Wollman 		/*
2806b8fda4dSGarrett Wollman 		 * SEND_EOF is equivalent to a SEND followed by
2816b8fda4dSGarrett Wollman 		 * a SHUTDOWN.
2826b8fda4dSGarrett Wollman 		 */
2836b8fda4dSGarrett Wollman 		if (req == PRU_SEND_EOF) {
2846b8fda4dSGarrett Wollman 			socantsendmore(so);
2856b8fda4dSGarrett Wollman 			unp_shutdown(unp);
2866b8fda4dSGarrett Wollman 		}
287df8bae1dSRodney W. Grimes 		break;
288df8bae1dSRodney W. Grimes 
289df8bae1dSRodney W. Grimes 	case PRU_ABORT:
290df8bae1dSRodney W. Grimes 		unp_drop(unp, ECONNABORTED);
291df8bae1dSRodney W. Grimes 		break;
292df8bae1dSRodney W. Grimes 
293df8bae1dSRodney W. Grimes 	case PRU_SENSE:
294df8bae1dSRodney W. Grimes 		((struct stat *) m)->st_blksize = so->so_snd.sb_hiwat;
295df8bae1dSRodney W. Grimes 		if (so->so_type == SOCK_STREAM && unp->unp_conn != 0) {
296df8bae1dSRodney W. Grimes 			so2 = unp->unp_conn->unp_socket;
297df8bae1dSRodney W. Grimes 			((struct stat *) m)->st_blksize += so2->so_rcv.sb_cc;
298df8bae1dSRodney W. Grimes 		}
299df8bae1dSRodney W. Grimes 		((struct stat *) m)->st_dev = NODEV;
300df8bae1dSRodney W. Grimes 		if (unp->unp_ino == 0)
301df8bae1dSRodney W. Grimes 			unp->unp_ino = unp_ino++;
302df8bae1dSRodney W. Grimes 		((struct stat *) m)->st_ino = unp->unp_ino;
303df8bae1dSRodney W. Grimes 		return (0);
304df8bae1dSRodney W. Grimes 
305df8bae1dSRodney W. Grimes 	case PRU_RCVOOB:
306df8bae1dSRodney W. Grimes 		return (EOPNOTSUPP);
307df8bae1dSRodney W. Grimes 
308df8bae1dSRodney W. Grimes 	case PRU_SENDOOB:
309df8bae1dSRodney W. Grimes 		error = EOPNOTSUPP;
310df8bae1dSRodney W. Grimes 		break;
311df8bae1dSRodney W. Grimes 
312df8bae1dSRodney W. Grimes 	case PRU_SOCKADDR:
313df8bae1dSRodney W. Grimes 		if (unp->unp_addr) {
314df8bae1dSRodney W. Grimes 			nam->m_len = unp->unp_addr->m_len;
315df8bae1dSRodney W. Grimes 			bcopy(mtod(unp->unp_addr, caddr_t),
316df8bae1dSRodney W. Grimes 			    mtod(nam, caddr_t), (unsigned)nam->m_len);
317df8bae1dSRodney W. Grimes 		} else
318df8bae1dSRodney W. Grimes 			nam->m_len = 0;
319df8bae1dSRodney W. Grimes 		break;
320df8bae1dSRodney W. Grimes 
321df8bae1dSRodney W. Grimes 	case PRU_PEERADDR:
322df8bae1dSRodney W. Grimes 		if (unp->unp_conn && unp->unp_conn->unp_addr) {
323df8bae1dSRodney W. Grimes 			nam->m_len = unp->unp_conn->unp_addr->m_len;
324df8bae1dSRodney W. Grimes 			bcopy(mtod(unp->unp_conn->unp_addr, caddr_t),
325df8bae1dSRodney W. Grimes 			    mtod(nam, caddr_t), (unsigned)nam->m_len);
326df8bae1dSRodney W. Grimes 		} else
327df8bae1dSRodney W. Grimes 			nam->m_len = 0;
328df8bae1dSRodney W. Grimes 		break;
329df8bae1dSRodney W. Grimes 
330df8bae1dSRodney W. Grimes 	case PRU_SLOWTIMO:
331df8bae1dSRodney W. Grimes 		break;
332df8bae1dSRodney W. Grimes 
333df8bae1dSRodney W. Grimes 	default:
334df8bae1dSRodney W. Grimes 		panic("piusrreq");
335df8bae1dSRodney W. Grimes 	}
336df8bae1dSRodney W. Grimes release:
337df8bae1dSRodney W. Grimes 	if (control)
338df8bae1dSRodney W. Grimes 		m_freem(control);
339df8bae1dSRodney W. Grimes 	if (m)
340df8bae1dSRodney W. Grimes 		m_freem(m);
341df8bae1dSRodney W. Grimes 	return (error);
342df8bae1dSRodney W. Grimes }
343df8bae1dSRodney W. Grimes 
344df8bae1dSRodney W. Grimes /*
345df8bae1dSRodney W. Grimes  * Both send and receive buffers are allocated PIPSIZ bytes of buffering
346df8bae1dSRodney W. Grimes  * for stream sockets, although the total for sender and receiver is
347df8bae1dSRodney W. Grimes  * actually only PIPSIZ.
348df8bae1dSRodney W. Grimes  * Datagram sockets really use the sendspace as the maximum datagram size,
349df8bae1dSRodney W. Grimes  * and don't really want to reserve the sendspace.  Their recvspace should
350df8bae1dSRodney W. Grimes  * be large enough for at least one max-size datagram plus address.
351df8bae1dSRodney W. Grimes  */
3525dce41c5SJohn Dyson #ifndef PIPSIZ
3535dce41c5SJohn Dyson #define	PIPSIZ	8192
3545dce41c5SJohn Dyson #endif
355f708ef1bSPoul-Henning Kamp static u_long	unpst_sendspace = PIPSIZ;
356f708ef1bSPoul-Henning Kamp static u_long	unpst_recvspace = PIPSIZ;
357f708ef1bSPoul-Henning Kamp static u_long	unpdg_sendspace = 2*1024;	/* really max datagram size */
358f708ef1bSPoul-Henning Kamp static u_long	unpdg_recvspace = 4*1024;
359df8bae1dSRodney W. Grimes 
360f708ef1bSPoul-Henning Kamp static int	unp_rights;			/* file descriptors in flight */
361df8bae1dSRodney W. Grimes 
362639acc13SGarrett Wollman SYSCTL_INT(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW,
363639acc13SGarrett Wollman 	   &unpst_sendspace, 0, "");
364639acc13SGarrett Wollman SYSCTL_INT(_net_local_stream, OID_AUTO, recvspace, CTLFLAG_RW,
365639acc13SGarrett Wollman 	   &unpst_recvspace, 0, "");
366639acc13SGarrett Wollman SYSCTL_INT(_net_local_dgram, OID_AUTO, maxdgram, CTLFLAG_RW,
367639acc13SGarrett Wollman 	   &unpdg_sendspace, 0, "");
368639acc13SGarrett Wollman SYSCTL_INT(_net_local_dgram, OID_AUTO, recvspace, CTLFLAG_RW,
369639acc13SGarrett Wollman 	   &unpdg_recvspace, 0, "");
370639acc13SGarrett Wollman SYSCTL_INT(_net_local, OID_AUTO, inflight, CTLFLAG_RD, &unp_rights, 0, "");
371639acc13SGarrett Wollman 
372f708ef1bSPoul-Henning Kamp static int
373df8bae1dSRodney W. Grimes unp_attach(so)
374df8bae1dSRodney W. Grimes 	struct socket *so;
375df8bae1dSRodney W. Grimes {
376df8bae1dSRodney W. Grimes 	register struct mbuf *m;
377df8bae1dSRodney W. Grimes 	register struct unpcb *unp;
378df8bae1dSRodney W. Grimes 	int error;
379df8bae1dSRodney W. Grimes 
380df8bae1dSRodney W. Grimes 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
381df8bae1dSRodney W. Grimes 		switch (so->so_type) {
382df8bae1dSRodney W. Grimes 
383df8bae1dSRodney W. Grimes 		case SOCK_STREAM:
384df8bae1dSRodney W. Grimes 			error = soreserve(so, unpst_sendspace, unpst_recvspace);
385df8bae1dSRodney W. Grimes 			break;
386df8bae1dSRodney W. Grimes 
387df8bae1dSRodney W. Grimes 		case SOCK_DGRAM:
388df8bae1dSRodney W. Grimes 			error = soreserve(so, unpdg_sendspace, unpdg_recvspace);
389df8bae1dSRodney W. Grimes 			break;
390df8bae1dSRodney W. Grimes 
391df8bae1dSRodney W. Grimes 		default:
392df8bae1dSRodney W. Grimes 			panic("unp_attach");
393df8bae1dSRodney W. Grimes 		}
394df8bae1dSRodney W. Grimes 		if (error)
395df8bae1dSRodney W. Grimes 			return (error);
396df8bae1dSRodney W. Grimes 	}
397df8bae1dSRodney W. Grimes 	m = m_getclr(M_DONTWAIT, MT_PCB);
398df8bae1dSRodney W. Grimes 	if (m == NULL)
399df8bae1dSRodney W. Grimes 		return (ENOBUFS);
400df8bae1dSRodney W. Grimes 	unp = mtod(m, struct unpcb *);
401df8bae1dSRodney W. Grimes 	so->so_pcb = (caddr_t)unp;
402df8bae1dSRodney W. Grimes 	unp->unp_socket = so;
403df8bae1dSRodney W. Grimes 	return (0);
404df8bae1dSRodney W. Grimes }
405df8bae1dSRodney W. Grimes 
406f708ef1bSPoul-Henning Kamp static void
407df8bae1dSRodney W. Grimes unp_detach(unp)
408df8bae1dSRodney W. Grimes 	register struct unpcb *unp;
409df8bae1dSRodney W. Grimes {
410df8bae1dSRodney W. Grimes 
411df8bae1dSRodney W. Grimes 	if (unp->unp_vnode) {
412df8bae1dSRodney W. Grimes 		unp->unp_vnode->v_socket = 0;
413df8bae1dSRodney W. Grimes 		vrele(unp->unp_vnode);
414df8bae1dSRodney W. Grimes 		unp->unp_vnode = 0;
415df8bae1dSRodney W. Grimes 	}
416df8bae1dSRodney W. Grimes 	if (unp->unp_conn)
417df8bae1dSRodney W. Grimes 		unp_disconnect(unp);
418df8bae1dSRodney W. Grimes 	while (unp->unp_refs)
419df8bae1dSRodney W. Grimes 		unp_drop(unp->unp_refs, ECONNRESET);
420df8bae1dSRodney W. Grimes 	soisdisconnected(unp->unp_socket);
421df8bae1dSRodney W. Grimes 	unp->unp_socket->so_pcb = 0;
422df8bae1dSRodney W. Grimes 	if (unp_rights) {
423df8bae1dSRodney W. Grimes 		/*
424df8bae1dSRodney W. Grimes 		 * Normally the receive buffer is flushed later,
425df8bae1dSRodney W. Grimes 		 * in sofree, but if our receive buffer holds references
426df8bae1dSRodney W. Grimes 		 * to descriptors that are now garbage, we will dispose
427df8bae1dSRodney W. Grimes 		 * of those descriptor references after the garbage collector
428df8bae1dSRodney W. Grimes 		 * gets them (resulting in a "panic: closef: count < 0").
429df8bae1dSRodney W. Grimes 		 */
430df8bae1dSRodney W. Grimes 		sorflush(unp->unp_socket);
431df8bae1dSRodney W. Grimes 		unp_gc();
432df8bae1dSRodney W. Grimes 	}
433b4c19655SDavid Greenman 	m_freem(unp->unp_addr);
434b4c19655SDavid Greenman 	(void) m_free(dtom(unp));
435df8bae1dSRodney W. Grimes }
436df8bae1dSRodney W. Grimes 
437f708ef1bSPoul-Henning Kamp static int
438df8bae1dSRodney W. Grimes unp_bind(unp, nam, p)
439df8bae1dSRodney W. Grimes 	struct unpcb *unp;
440df8bae1dSRodney W. Grimes 	struct mbuf *nam;
441df8bae1dSRodney W. Grimes 	struct proc *p;
442df8bae1dSRodney W. Grimes {
443df8bae1dSRodney W. Grimes 	struct sockaddr_un *soun = mtod(nam, struct sockaddr_un *);
444df8bae1dSRodney W. Grimes 	register struct vnode *vp;
445df8bae1dSRodney W. Grimes 	struct vattr vattr;
446df8bae1dSRodney W. Grimes 	int error;
447df8bae1dSRodney W. Grimes 	struct nameidata nd;
448df8bae1dSRodney W. Grimes 
449df8bae1dSRodney W. Grimes 	NDINIT(&nd, CREATE, FOLLOW | LOCKPARENT, UIO_SYSSPACE,
450df8bae1dSRodney W. Grimes 	    soun->sun_path, p);
451df8bae1dSRodney W. Grimes 	if (unp->unp_vnode != NULL)
452df8bae1dSRodney W. Grimes 		return (EINVAL);
453df8bae1dSRodney W. Grimes 	if (nam->m_len == MLEN) {
454df8bae1dSRodney W. Grimes 		if (*(mtod(nam, caddr_t) + nam->m_len - 1) != 0)
455df8bae1dSRodney W. Grimes 			return (EINVAL);
456df8bae1dSRodney W. Grimes 	} else
457df8bae1dSRodney W. Grimes 		*(mtod(nam, caddr_t) + nam->m_len) = 0;
458df8bae1dSRodney W. Grimes /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */
459797f2d22SPoul-Henning Kamp 	error = namei(&nd);
460797f2d22SPoul-Henning Kamp 	if (error)
461df8bae1dSRodney W. Grimes 		return (error);
462df8bae1dSRodney W. Grimes 	vp = nd.ni_vp;
463df8bae1dSRodney W. Grimes 	if (vp != NULL) {
464df8bae1dSRodney W. Grimes 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
465df8bae1dSRodney W. Grimes 		if (nd.ni_dvp == vp)
466df8bae1dSRodney W. Grimes 			vrele(nd.ni_dvp);
467df8bae1dSRodney W. Grimes 		else
468df8bae1dSRodney W. Grimes 			vput(nd.ni_dvp);
469df8bae1dSRodney W. Grimes 		vrele(vp);
470df8bae1dSRodney W. Grimes 		return (EADDRINUSE);
471df8bae1dSRodney W. Grimes 	}
472df8bae1dSRodney W. Grimes 	VATTR_NULL(&vattr);
473df8bae1dSRodney W. Grimes 	vattr.va_type = VSOCK;
474df8bae1dSRodney W. Grimes 	vattr.va_mode = ACCESSPERMS;
475996c772fSJohn Dyson 	VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
476996c772fSJohn Dyson 	if (error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr))
477df8bae1dSRodney W. Grimes 		return (error);
478df8bae1dSRodney W. Grimes 	vp = nd.ni_vp;
479df8bae1dSRodney W. Grimes 	vp->v_socket = unp->unp_socket;
480df8bae1dSRodney W. Grimes 	unp->unp_vnode = vp;
481df8bae1dSRodney W. Grimes 	unp->unp_addr = m_copy(nam, 0, (int)M_COPYALL);
482996c772fSJohn Dyson 	VOP_UNLOCK(vp, 0, p);
483df8bae1dSRodney W. Grimes 	return (0);
484df8bae1dSRodney W. Grimes }
485df8bae1dSRodney W. Grimes 
486f708ef1bSPoul-Henning Kamp static int
487df8bae1dSRodney W. Grimes unp_connect(so, nam, p)
488df8bae1dSRodney W. Grimes 	struct socket *so;
489df8bae1dSRodney W. Grimes 	struct mbuf *nam;
490df8bae1dSRodney W. Grimes 	struct proc *p;
491df8bae1dSRodney W. Grimes {
492df8bae1dSRodney W. Grimes 	register struct sockaddr_un *soun = mtod(nam, struct sockaddr_un *);
493df8bae1dSRodney W. Grimes 	register struct vnode *vp;
494df8bae1dSRodney W. Grimes 	register struct socket *so2, *so3;
495df8bae1dSRodney W. Grimes 	struct unpcb *unp2, *unp3;
496df8bae1dSRodney W. Grimes 	int error;
497df8bae1dSRodney W. Grimes 	struct nameidata nd;
498df8bae1dSRodney W. Grimes 
499df8bae1dSRodney W. Grimes 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, soun->sun_path, p);
500df8bae1dSRodney W. Grimes 	if (nam->m_data + nam->m_len == &nam->m_dat[MLEN]) {	/* XXX */
501df8bae1dSRodney W. Grimes 		if (*(mtod(nam, caddr_t) + nam->m_len - 1) != 0)
502df8bae1dSRodney W. Grimes 			return (EMSGSIZE);
503df8bae1dSRodney W. Grimes 	} else
504df8bae1dSRodney W. Grimes 		*(mtod(nam, caddr_t) + nam->m_len) = 0;
505797f2d22SPoul-Henning Kamp 	error = namei(&nd);
506797f2d22SPoul-Henning Kamp 	if (error)
507df8bae1dSRodney W. Grimes 		return (error);
508df8bae1dSRodney W. Grimes 	vp = nd.ni_vp;
509df8bae1dSRodney W. Grimes 	if (vp->v_type != VSOCK) {
510df8bae1dSRodney W. Grimes 		error = ENOTSOCK;
511df8bae1dSRodney W. Grimes 		goto bad;
512df8bae1dSRodney W. Grimes 	}
513797f2d22SPoul-Henning Kamp 	error = VOP_ACCESS(vp, VWRITE, p->p_ucred, p);
514797f2d22SPoul-Henning Kamp 	if (error)
515df8bae1dSRodney W. Grimes 		goto bad;
516df8bae1dSRodney W. Grimes 	so2 = vp->v_socket;
517df8bae1dSRodney W. Grimes 	if (so2 == 0) {
518df8bae1dSRodney W. Grimes 		error = ECONNREFUSED;
519df8bae1dSRodney W. Grimes 		goto bad;
520df8bae1dSRodney W. Grimes 	}
521df8bae1dSRodney W. Grimes 	if (so->so_type != so2->so_type) {
522df8bae1dSRodney W. Grimes 		error = EPROTOTYPE;
523df8bae1dSRodney W. Grimes 		goto bad;
524df8bae1dSRodney W. Grimes 	}
525df8bae1dSRodney W. Grimes 	if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
526df8bae1dSRodney W. Grimes 		if ((so2->so_options & SO_ACCEPTCONN) == 0 ||
527df8bae1dSRodney W. Grimes 		    (so3 = sonewconn(so2, 0)) == 0) {
528df8bae1dSRodney W. Grimes 			error = ECONNREFUSED;
529df8bae1dSRodney W. Grimes 			goto bad;
530df8bae1dSRodney W. Grimes 		}
531df8bae1dSRodney W. Grimes 		unp2 = sotounpcb(so2);
532df8bae1dSRodney W. Grimes 		unp3 = sotounpcb(so3);
533df8bae1dSRodney W. Grimes 		if (unp2->unp_addr)
534df8bae1dSRodney W. Grimes 			unp3->unp_addr =
535df8bae1dSRodney W. Grimes 				  m_copy(unp2->unp_addr, 0, (int)M_COPYALL);
536df8bae1dSRodney W. Grimes 		so2 = so3;
537df8bae1dSRodney W. Grimes 	}
538df8bae1dSRodney W. Grimes 	error = unp_connect2(so, so2);
539df8bae1dSRodney W. Grimes bad:
540df8bae1dSRodney W. Grimes 	vput(vp);
541df8bae1dSRodney W. Grimes 	return (error);
542df8bae1dSRodney W. Grimes }
543df8bae1dSRodney W. Grimes 
54426f9a767SRodney W. Grimes int
545df8bae1dSRodney W. Grimes unp_connect2(so, so2)
546df8bae1dSRodney W. Grimes 	register struct socket *so;
547df8bae1dSRodney W. Grimes 	register struct socket *so2;
548df8bae1dSRodney W. Grimes {
549df8bae1dSRodney W. Grimes 	register struct unpcb *unp = sotounpcb(so);
550df8bae1dSRodney W. Grimes 	register struct unpcb *unp2;
551df8bae1dSRodney W. Grimes 
552df8bae1dSRodney W. Grimes 	if (so2->so_type != so->so_type)
553df8bae1dSRodney W. Grimes 		return (EPROTOTYPE);
554df8bae1dSRodney W. Grimes 	unp2 = sotounpcb(so2);
555df8bae1dSRodney W. Grimes 	unp->unp_conn = unp2;
556df8bae1dSRodney W. Grimes 	switch (so->so_type) {
557df8bae1dSRodney W. Grimes 
558df8bae1dSRodney W. Grimes 	case SOCK_DGRAM:
559df8bae1dSRodney W. Grimes 		unp->unp_nextref = unp2->unp_refs;
560df8bae1dSRodney W. Grimes 		unp2->unp_refs = unp;
561df8bae1dSRodney W. Grimes 		soisconnected(so);
562df8bae1dSRodney W. Grimes 		break;
563df8bae1dSRodney W. Grimes 
564df8bae1dSRodney W. Grimes 	case SOCK_STREAM:
565df8bae1dSRodney W. Grimes 		unp2->unp_conn = unp;
566df8bae1dSRodney W. Grimes 		soisconnected(so);
567df8bae1dSRodney W. Grimes 		soisconnected(so2);
568df8bae1dSRodney W. Grimes 		break;
569df8bae1dSRodney W. Grimes 
570df8bae1dSRodney W. Grimes 	default:
571df8bae1dSRodney W. Grimes 		panic("unp_connect2");
572df8bae1dSRodney W. Grimes 	}
573df8bae1dSRodney W. Grimes 	return (0);
574df8bae1dSRodney W. Grimes }
575df8bae1dSRodney W. Grimes 
576f708ef1bSPoul-Henning Kamp static void
577df8bae1dSRodney W. Grimes unp_disconnect(unp)
578df8bae1dSRodney W. Grimes 	struct unpcb *unp;
579df8bae1dSRodney W. Grimes {
580df8bae1dSRodney W. Grimes 	register struct unpcb *unp2 = unp->unp_conn;
581df8bae1dSRodney W. Grimes 
582df8bae1dSRodney W. Grimes 	if (unp2 == 0)
583df8bae1dSRodney W. Grimes 		return;
584df8bae1dSRodney W. Grimes 	unp->unp_conn = 0;
585df8bae1dSRodney W. Grimes 	switch (unp->unp_socket->so_type) {
586df8bae1dSRodney W. Grimes 
587df8bae1dSRodney W. Grimes 	case SOCK_DGRAM:
588df8bae1dSRodney W. Grimes 		if (unp2->unp_refs == unp)
589df8bae1dSRodney W. Grimes 			unp2->unp_refs = unp->unp_nextref;
590df8bae1dSRodney W. Grimes 		else {
591df8bae1dSRodney W. Grimes 			unp2 = unp2->unp_refs;
592df8bae1dSRodney W. Grimes 			for (;;) {
593df8bae1dSRodney W. Grimes 				if (unp2 == 0)
594df8bae1dSRodney W. Grimes 					panic("unp_disconnect");
595df8bae1dSRodney W. Grimes 				if (unp2->unp_nextref == unp)
596df8bae1dSRodney W. Grimes 					break;
597df8bae1dSRodney W. Grimes 				unp2 = unp2->unp_nextref;
598df8bae1dSRodney W. Grimes 			}
599df8bae1dSRodney W. Grimes 			unp2->unp_nextref = unp->unp_nextref;
600df8bae1dSRodney W. Grimes 		}
601df8bae1dSRodney W. Grimes 		unp->unp_nextref = 0;
602df8bae1dSRodney W. Grimes 		unp->unp_socket->so_state &= ~SS_ISCONNECTED;
603df8bae1dSRodney W. Grimes 		break;
604df8bae1dSRodney W. Grimes 
605df8bae1dSRodney W. Grimes 	case SOCK_STREAM:
606df8bae1dSRodney W. Grimes 		soisdisconnected(unp->unp_socket);
607df8bae1dSRodney W. Grimes 		unp2->unp_conn = 0;
608df8bae1dSRodney W. Grimes 		soisdisconnected(unp2->unp_socket);
609df8bae1dSRodney W. Grimes 		break;
610df8bae1dSRodney W. Grimes 	}
611df8bae1dSRodney W. Grimes }
612df8bae1dSRodney W. Grimes 
613df8bae1dSRodney W. Grimes #ifdef notdef
61426f9a767SRodney W. Grimes void
615df8bae1dSRodney W. Grimes unp_abort(unp)
616df8bae1dSRodney W. Grimes 	struct unpcb *unp;
617df8bae1dSRodney W. Grimes {
618df8bae1dSRodney W. Grimes 
619df8bae1dSRodney W. Grimes 	unp_detach(unp);
620df8bae1dSRodney W. Grimes }
621df8bae1dSRodney W. Grimes #endif
622df8bae1dSRodney W. Grimes 
623f708ef1bSPoul-Henning Kamp static void
624df8bae1dSRodney W. Grimes unp_shutdown(unp)
625df8bae1dSRodney W. Grimes 	struct unpcb *unp;
626df8bae1dSRodney W. Grimes {
627df8bae1dSRodney W. Grimes 	struct socket *so;
628df8bae1dSRodney W. Grimes 
629df8bae1dSRodney W. Grimes 	if (unp->unp_socket->so_type == SOCK_STREAM && unp->unp_conn &&
630df8bae1dSRodney W. Grimes 	    (so = unp->unp_conn->unp_socket))
631df8bae1dSRodney W. Grimes 		socantrcvmore(so);
632df8bae1dSRodney W. Grimes }
633df8bae1dSRodney W. Grimes 
634f708ef1bSPoul-Henning Kamp static void
635df8bae1dSRodney W. Grimes unp_drop(unp, errno)
636df8bae1dSRodney W. Grimes 	struct unpcb *unp;
637df8bae1dSRodney W. Grimes 	int errno;
638df8bae1dSRodney W. Grimes {
639df8bae1dSRodney W. Grimes 	struct socket *so = unp->unp_socket;
640df8bae1dSRodney W. Grimes 
641df8bae1dSRodney W. Grimes 	so->so_error = errno;
642df8bae1dSRodney W. Grimes 	unp_disconnect(unp);
643df8bae1dSRodney W. Grimes 	if (so->so_head) {
644df8bae1dSRodney W. Grimes 		so->so_pcb = (caddr_t) 0;
645df8bae1dSRodney W. Grimes 		m_freem(unp->unp_addr);
646df8bae1dSRodney W. Grimes 		(void) m_free(dtom(unp));
647df8bae1dSRodney W. Grimes 		sofree(so);
648df8bae1dSRodney W. Grimes 	}
649df8bae1dSRodney W. Grimes }
650df8bae1dSRodney W. Grimes 
651df8bae1dSRodney W. Grimes #ifdef notdef
65226f9a767SRodney W. Grimes void
653df8bae1dSRodney W. Grimes unp_drain()
654df8bae1dSRodney W. Grimes {
655df8bae1dSRodney W. Grimes 
656df8bae1dSRodney W. Grimes }
657df8bae1dSRodney W. Grimes #endif
658df8bae1dSRodney W. Grimes 
65926f9a767SRodney W. Grimes int
660df8bae1dSRodney W. Grimes unp_externalize(rights)
661df8bae1dSRodney W. Grimes 	struct mbuf *rights;
662df8bae1dSRodney W. Grimes {
663df8bae1dSRodney W. Grimes 	struct proc *p = curproc;		/* XXX */
664df8bae1dSRodney W. Grimes 	register int i;
665df8bae1dSRodney W. Grimes 	register struct cmsghdr *cm = mtod(rights, struct cmsghdr *);
666df8bae1dSRodney W. Grimes 	register struct file **rp = (struct file **)(cm + 1);
667df8bae1dSRodney W. Grimes 	register struct file *fp;
668df8bae1dSRodney W. Grimes 	int newfds = (cm->cmsg_len - sizeof(*cm)) / sizeof (int);
669df8bae1dSRodney W. Grimes 	int f;
670df8bae1dSRodney W. Grimes 
671ed5b7817SJulian Elischer 	/*
672ed5b7817SJulian Elischer 	 * if the new FD's will not fit, then we free them all
673ed5b7817SJulian Elischer 	 */
674df8bae1dSRodney W. Grimes 	if (!fdavail(p, newfds)) {
675df8bae1dSRodney W. Grimes 		for (i = 0; i < newfds; i++) {
676df8bae1dSRodney W. Grimes 			fp = *rp;
677df8bae1dSRodney W. Grimes 			unp_discard(fp);
678df8bae1dSRodney W. Grimes 			*rp++ = 0;
679df8bae1dSRodney W. Grimes 		}
680df8bae1dSRodney W. Grimes 		return (EMSGSIZE);
681df8bae1dSRodney W. Grimes 	}
682ed5b7817SJulian Elischer 	/*
683ed5b7817SJulian Elischer 	 * now change each pointer to an fd in the global table to
684ed5b7817SJulian Elischer 	 * an integer that is the index to the local fd table entry
685ed5b7817SJulian Elischer 	 * that we set up to point to the global one we are transferring.
686ed5b7817SJulian Elischer 	 * XXX this assumes a pointer and int are the same size...!
687ed5b7817SJulian Elischer 	 */
688df8bae1dSRodney W. Grimes 	for (i = 0; i < newfds; i++) {
689df8bae1dSRodney W. Grimes 		if (fdalloc(p, 0, &f))
690df8bae1dSRodney W. Grimes 			panic("unp_externalize");
691df8bae1dSRodney W. Grimes 		fp = *rp;
692df8bae1dSRodney W. Grimes 		p->p_fd->fd_ofiles[f] = fp;
693df8bae1dSRodney W. Grimes 		fp->f_msgcount--;
694df8bae1dSRodney W. Grimes 		unp_rights--;
695df8bae1dSRodney W. Grimes 		*(int *)rp++ = f;
696df8bae1dSRodney W. Grimes 	}
697df8bae1dSRodney W. Grimes 	return (0);
698df8bae1dSRodney W. Grimes }
699df8bae1dSRodney W. Grimes 
7000b788fa1SBill Paul #ifndef MIN
7010b788fa1SBill Paul #define	MIN(a,b) (((a)<(b))?(a):(b))
7020b788fa1SBill Paul #endif
7030b788fa1SBill Paul 
704f708ef1bSPoul-Henning Kamp static int
705df8bae1dSRodney W. Grimes unp_internalize(control, p)
706df8bae1dSRodney W. Grimes 	struct mbuf *control;
707df8bae1dSRodney W. Grimes 	struct proc *p;
708df8bae1dSRodney W. Grimes {
709df8bae1dSRodney W. Grimes 	struct filedesc *fdp = p->p_fd;
710df8bae1dSRodney W. Grimes 	register struct cmsghdr *cm = mtod(control, struct cmsghdr *);
711df8bae1dSRodney W. Grimes 	register struct file **rp;
712df8bae1dSRodney W. Grimes 	register struct file *fp;
713df8bae1dSRodney W. Grimes 	register int i, fd;
7140b788fa1SBill Paul 	register struct cmsgcred *cmcred;
715df8bae1dSRodney W. Grimes 	int oldfds;
716df8bae1dSRodney W. Grimes 
7170b788fa1SBill Paul 	if ((cm->cmsg_type != SCM_RIGHTS && cm->cmsg_type != SCM_CREDS) ||
7180b788fa1SBill Paul 	    cm->cmsg_level != SOL_SOCKET || cm->cmsg_len != control->m_len)
719df8bae1dSRodney W. Grimes 		return (EINVAL);
7200b788fa1SBill Paul 
7210b788fa1SBill Paul 	/*
7220b788fa1SBill Paul 	 * Fill in credential information.
7230b788fa1SBill Paul 	 */
7240b788fa1SBill Paul 	if (cm->cmsg_type == SCM_CREDS) {
7250b788fa1SBill Paul 		cmcred = (struct cmsgcred *)(cm + 1);
7260b788fa1SBill Paul 		cmcred->cmcred_pid = p->p_pid;
7270b788fa1SBill Paul 		cmcred->cmcred_uid = p->p_cred->p_ruid;
7280b788fa1SBill Paul 		cmcred->cmcred_gid = p->p_cred->p_rgid;
7290b788fa1SBill Paul 		cmcred->cmcred_euid = p->p_ucred->cr_uid;
7300b788fa1SBill Paul 		cmcred->cmcred_ngroups = MIN(p->p_ucred->cr_ngroups,
7310b788fa1SBill Paul 							CMGROUP_MAX);
7320b788fa1SBill Paul 		for (i = 0; i < cmcred->cmcred_ngroups; i++)
7330b788fa1SBill Paul 			cmcred->cmcred_groups[i] = p->p_ucred->cr_groups[i];
7340b788fa1SBill Paul 		return(0);
7350b788fa1SBill Paul 	}
7360b788fa1SBill Paul 
737df8bae1dSRodney W. Grimes 	oldfds = (cm->cmsg_len - sizeof (*cm)) / sizeof (int);
738ed5b7817SJulian Elischer 	/*
739ed5b7817SJulian Elischer 	 * check that all the FDs passed in refer to legal OPEN files
740ed5b7817SJulian Elischer 	 * If not, reject the entire operation.
741ed5b7817SJulian Elischer 	 */
742df8bae1dSRodney W. Grimes 	rp = (struct file **)(cm + 1);
743df8bae1dSRodney W. Grimes 	for (i = 0; i < oldfds; i++) {
744df8bae1dSRodney W. Grimes 		fd = *(int *)rp++;
745df8bae1dSRodney W. Grimes 		if ((unsigned)fd >= fdp->fd_nfiles ||
746df8bae1dSRodney W. Grimes 		    fdp->fd_ofiles[fd] == NULL)
747df8bae1dSRodney W. Grimes 			return (EBADF);
748df8bae1dSRodney W. Grimes 	}
749ed5b7817SJulian Elischer 	/*
750ed5b7817SJulian Elischer 	 * Now replace the integer FDs with pointers to
751ed5b7817SJulian Elischer 	 * the associated global file table entry..
752ed5b7817SJulian Elischer 	 * XXX this assumes a pointer and an int are the same size!
753ed5b7817SJulian Elischer 	 */
754df8bae1dSRodney W. Grimes 	rp = (struct file **)(cm + 1);
755df8bae1dSRodney W. Grimes 	for (i = 0; i < oldfds; i++) {
756df8bae1dSRodney W. Grimes 		fp = fdp->fd_ofiles[*(int *)rp];
757df8bae1dSRodney W. Grimes 		*rp++ = fp;
758df8bae1dSRodney W. Grimes 		fp->f_count++;
759df8bae1dSRodney W. Grimes 		fp->f_msgcount++;
760df8bae1dSRodney W. Grimes 		unp_rights++;
761df8bae1dSRodney W. Grimes 	}
762df8bae1dSRodney W. Grimes 	return (0);
763df8bae1dSRodney W. Grimes }
764df8bae1dSRodney W. Grimes 
765f708ef1bSPoul-Henning Kamp static int	unp_defer, unp_gcing;
766df8bae1dSRodney W. Grimes 
767f708ef1bSPoul-Henning Kamp static void
768df8bae1dSRodney W. Grimes unp_gc()
769df8bae1dSRodney W. Grimes {
770df8bae1dSRodney W. Grimes 	register struct file *fp, *nextfp;
771df8bae1dSRodney W. Grimes 	register struct socket *so;
772df8bae1dSRodney W. Grimes 	struct file **extra_ref, **fpp;
773df8bae1dSRodney W. Grimes 	int nunref, i;
774df8bae1dSRodney W. Grimes 
775df8bae1dSRodney W. Grimes 	if (unp_gcing)
776df8bae1dSRodney W. Grimes 		return;
777df8bae1dSRodney W. Grimes 	unp_gcing = 1;
778df8bae1dSRodney W. Grimes 	unp_defer = 0;
779ed5b7817SJulian Elischer 	/*
780ed5b7817SJulian Elischer 	 * before going through all this, set all FDs to
781ed5b7817SJulian Elischer 	 * be NOT defered and NOT externally accessible
782ed5b7817SJulian Elischer 	 */
783bc6f0e79SJeffrey Hsu 	for (fp = filehead.lh_first; fp != 0; fp = fp->f_list.le_next)
784df8bae1dSRodney W. Grimes 		fp->f_flag &= ~(FMARK|FDEFER);
785df8bae1dSRodney W. Grimes 	do {
786bc6f0e79SJeffrey Hsu 		for (fp = filehead.lh_first; fp != 0; fp = fp->f_list.le_next) {
787ed5b7817SJulian Elischer 			/*
788ed5b7817SJulian Elischer 			 * If the file is not open, skip it
789ed5b7817SJulian Elischer 			 */
790df8bae1dSRodney W. Grimes 			if (fp->f_count == 0)
791df8bae1dSRodney W. Grimes 				continue;
792ed5b7817SJulian Elischer 			/*
793ed5b7817SJulian Elischer 			 * If we already marked it as 'defer'  in a
794ed5b7817SJulian Elischer 			 * previous pass, then try process it this time
795ed5b7817SJulian Elischer 			 * and un-mark it
796ed5b7817SJulian Elischer 			 */
797df8bae1dSRodney W. Grimes 			if (fp->f_flag & FDEFER) {
798df8bae1dSRodney W. Grimes 				fp->f_flag &= ~FDEFER;
799df8bae1dSRodney W. Grimes 				unp_defer--;
800df8bae1dSRodney W. Grimes 			} else {
801ed5b7817SJulian Elischer 				/*
802ed5b7817SJulian Elischer 				 * if it's not defered, then check if it's
803ed5b7817SJulian Elischer 				 * already marked.. if so skip it
804ed5b7817SJulian Elischer 				 */
805df8bae1dSRodney W. Grimes 				if (fp->f_flag & FMARK)
806df8bae1dSRodney W. Grimes 					continue;
807ed5b7817SJulian Elischer 				/*
808ed5b7817SJulian Elischer 				 * If all references are from messages
809ed5b7817SJulian Elischer 				 * in transit, then skip it. it's not
810ed5b7817SJulian Elischer 				 * externally accessible.
811ed5b7817SJulian Elischer 				 */
812df8bae1dSRodney W. Grimes 				if (fp->f_count == fp->f_msgcount)
813df8bae1dSRodney W. Grimes 					continue;
814ed5b7817SJulian Elischer 				/*
815ed5b7817SJulian Elischer 				 * If it got this far then it must be
816ed5b7817SJulian Elischer 				 * externally accessible.
817ed5b7817SJulian Elischer 				 */
818df8bae1dSRodney W. Grimes 				fp->f_flag |= FMARK;
819df8bae1dSRodney W. Grimes 			}
820ed5b7817SJulian Elischer 			/*
821ed5b7817SJulian Elischer 			 * either it was defered, or it is externally
822ed5b7817SJulian Elischer 			 * accessible and not already marked so.
823ed5b7817SJulian Elischer 			 * Now check if it is possibly one of OUR sockets.
824ed5b7817SJulian Elischer 			 */
825df8bae1dSRodney W. Grimes 			if (fp->f_type != DTYPE_SOCKET ||
826df8bae1dSRodney W. Grimes 			    (so = (struct socket *)fp->f_data) == 0)
827df8bae1dSRodney W. Grimes 				continue;
828748e0b0aSGarrett Wollman 			if (so->so_proto->pr_domain != &localdomain ||
829df8bae1dSRodney W. Grimes 			    (so->so_proto->pr_flags&PR_RIGHTS) == 0)
830df8bae1dSRodney W. Grimes 				continue;
831df8bae1dSRodney W. Grimes #ifdef notdef
832df8bae1dSRodney W. Grimes 			if (so->so_rcv.sb_flags & SB_LOCK) {
833df8bae1dSRodney W. Grimes 				/*
834df8bae1dSRodney W. Grimes 				 * This is problematical; it's not clear
835df8bae1dSRodney W. Grimes 				 * we need to wait for the sockbuf to be
836df8bae1dSRodney W. Grimes 				 * unlocked (on a uniprocessor, at least),
837df8bae1dSRodney W. Grimes 				 * and it's also not clear what to do
838df8bae1dSRodney W. Grimes 				 * if sbwait returns an error due to receipt
839df8bae1dSRodney W. Grimes 				 * of a signal.  If sbwait does return
840df8bae1dSRodney W. Grimes 				 * an error, we'll go into an infinite
841df8bae1dSRodney W. Grimes 				 * loop.  Delete all of this for now.
842df8bae1dSRodney W. Grimes 				 */
843df8bae1dSRodney W. Grimes 				(void) sbwait(&so->so_rcv);
844df8bae1dSRodney W. Grimes 				goto restart;
845df8bae1dSRodney W. Grimes 			}
846df8bae1dSRodney W. Grimes #endif
847ed5b7817SJulian Elischer 			/*
848ed5b7817SJulian Elischer 			 * So, Ok, it's one of our sockets and it IS externally
849ed5b7817SJulian Elischer 			 * accessible (or was defered). Now we look
850ed5b7817SJulian Elischer 			 * to see if we hold any file descriptors in it's
851ed5b7817SJulian Elischer 			 * message buffers. Follow those links and mark them
852ed5b7817SJulian Elischer 			 * as accessible too.
853ed5b7817SJulian Elischer 			 */
854df8bae1dSRodney W. Grimes 			unp_scan(so->so_rcv.sb_mb, unp_mark);
855df8bae1dSRodney W. Grimes 		}
856df8bae1dSRodney W. Grimes 	} while (unp_defer);
857df8bae1dSRodney W. Grimes 	/*
858df8bae1dSRodney W. Grimes 	 * We grab an extra reference to each of the file table entries
859df8bae1dSRodney W. Grimes 	 * that are not otherwise accessible and then free the rights
860df8bae1dSRodney W. Grimes 	 * that are stored in messages on them.
861df8bae1dSRodney W. Grimes 	 *
862df8bae1dSRodney W. Grimes 	 * The bug in the orginal code is a little tricky, so I'll describe
863df8bae1dSRodney W. Grimes 	 * what's wrong with it here.
864df8bae1dSRodney W. Grimes 	 *
865df8bae1dSRodney W. Grimes 	 * It is incorrect to simply unp_discard each entry for f_msgcount
866df8bae1dSRodney W. Grimes 	 * times -- consider the case of sockets A and B that contain
867df8bae1dSRodney W. Grimes 	 * references to each other.  On a last close of some other socket,
868df8bae1dSRodney W. Grimes 	 * we trigger a gc since the number of outstanding rights (unp_rights)
869df8bae1dSRodney W. Grimes 	 * is non-zero.  If during the sweep phase the gc code un_discards,
870df8bae1dSRodney W. Grimes 	 * we end up doing a (full) closef on the descriptor.  A closef on A
871df8bae1dSRodney W. Grimes 	 * results in the following chain.  Closef calls soo_close, which
872df8bae1dSRodney W. Grimes 	 * calls soclose.   Soclose calls first (through the switch
873df8bae1dSRodney W. Grimes 	 * uipc_usrreq) unp_detach, which re-invokes unp_gc.  Unp_gc simply
874df8bae1dSRodney W. Grimes 	 * returns because the previous instance had set unp_gcing, and
875df8bae1dSRodney W. Grimes 	 * we return all the way back to soclose, which marks the socket
876df8bae1dSRodney W. Grimes 	 * with SS_NOFDREF, and then calls sofree.  Sofree calls sorflush
877df8bae1dSRodney W. Grimes 	 * to free up the rights that are queued in messages on the socket A,
878df8bae1dSRodney W. Grimes 	 * i.e., the reference on B.  The sorflush calls via the dom_dispose
879df8bae1dSRodney W. Grimes 	 * switch unp_dispose, which unp_scans with unp_discard.  This second
880df8bae1dSRodney W. Grimes 	 * instance of unp_discard just calls closef on B.
881df8bae1dSRodney W. Grimes 	 *
882df8bae1dSRodney W. Grimes 	 * Well, a similar chain occurs on B, resulting in a sorflush on B,
883df8bae1dSRodney W. Grimes 	 * which results in another closef on A.  Unfortunately, A is already
884df8bae1dSRodney W. Grimes 	 * being closed, and the descriptor has already been marked with
885df8bae1dSRodney W. Grimes 	 * SS_NOFDREF, and soclose panics at this point.
886df8bae1dSRodney W. Grimes 	 *
887df8bae1dSRodney W. Grimes 	 * Here, we first take an extra reference to each inaccessible
888df8bae1dSRodney W. Grimes 	 * descriptor.  Then, we call sorflush ourself, since we know
889df8bae1dSRodney W. Grimes 	 * it is a Unix domain socket anyhow.  After we destroy all the
890df8bae1dSRodney W. Grimes 	 * rights carried in messages, we do a last closef to get rid
891df8bae1dSRodney W. Grimes 	 * of our extra reference.  This is the last close, and the
892df8bae1dSRodney W. Grimes 	 * unp_detach etc will shut down the socket.
893df8bae1dSRodney W. Grimes 	 *
894df8bae1dSRodney W. Grimes 	 * 91/09/19, bsy@cs.cmu.edu
895df8bae1dSRodney W. Grimes 	 */
896df8bae1dSRodney W. Grimes 	extra_ref = malloc(nfiles * sizeof(struct file *), M_FILE, M_WAITOK);
897bc6f0e79SJeffrey Hsu 	for (nunref = 0, fp = filehead.lh_first, fpp = extra_ref; fp != 0;
898bc6f0e79SJeffrey Hsu 	    fp = nextfp) {
899bc6f0e79SJeffrey Hsu 		nextfp = fp->f_list.le_next;
900ed5b7817SJulian Elischer 		/*
901ed5b7817SJulian Elischer 		 * If it's not open, skip it
902ed5b7817SJulian Elischer 		 */
903df8bae1dSRodney W. Grimes 		if (fp->f_count == 0)
904df8bae1dSRodney W. Grimes 			continue;
905ed5b7817SJulian Elischer 		/*
906ed5b7817SJulian Elischer 		 * If all refs are from msgs, and it's not marked accessible
907ed5b7817SJulian Elischer 		 * then it must be referenced from some unreachable cycle
908ed5b7817SJulian Elischer 		 * of (shut-down) FDs, so include it in our
909ed5b7817SJulian Elischer 		 * list of FDs to remove
910ed5b7817SJulian Elischer 		 */
911df8bae1dSRodney W. Grimes 		if (fp->f_count == fp->f_msgcount && !(fp->f_flag & FMARK)) {
912df8bae1dSRodney W. Grimes 			*fpp++ = fp;
913df8bae1dSRodney W. Grimes 			nunref++;
914df8bae1dSRodney W. Grimes 			fp->f_count++;
915df8bae1dSRodney W. Grimes 		}
916df8bae1dSRodney W. Grimes 	}
917ed5b7817SJulian Elischer 	/*
918ed5b7817SJulian Elischer 	 * for each FD on our hit list, do the following two things
919ed5b7817SJulian Elischer 	 */
920df8bae1dSRodney W. Grimes 	for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp)
921df8bae1dSRodney W. Grimes 		sorflush((struct socket *)(*fpp)->f_data);
922df8bae1dSRodney W. Grimes 	for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp)
92392cbac68SPoul-Henning Kamp 		closef(*fpp, (struct proc *) NULL);
924df8bae1dSRodney W. Grimes 	free((caddr_t)extra_ref, M_FILE);
925df8bae1dSRodney W. Grimes 	unp_gcing = 0;
926df8bae1dSRodney W. Grimes }
927df8bae1dSRodney W. Grimes 
92826f9a767SRodney W. Grimes void
929df8bae1dSRodney W. Grimes unp_dispose(m)
930df8bae1dSRodney W. Grimes 	struct mbuf *m;
931df8bae1dSRodney W. Grimes {
932996c772fSJohn Dyson 
933df8bae1dSRodney W. Grimes 	if (m)
934df8bae1dSRodney W. Grimes 		unp_scan(m, unp_discard);
935df8bae1dSRodney W. Grimes }
936df8bae1dSRodney W. Grimes 
937f708ef1bSPoul-Henning Kamp static void
938df8bae1dSRodney W. Grimes unp_scan(m0, op)
939df8bae1dSRodney W. Grimes 	register struct mbuf *m0;
940996c772fSJohn Dyson 	void (*op) __P((struct file *));
941df8bae1dSRodney W. Grimes {
942df8bae1dSRodney W. Grimes 	register struct mbuf *m;
943df8bae1dSRodney W. Grimes 	register struct file **rp;
944df8bae1dSRodney W. Grimes 	register struct cmsghdr *cm;
945df8bae1dSRodney W. Grimes 	register int i;
946df8bae1dSRodney W. Grimes 	int qfds;
947df8bae1dSRodney W. Grimes 
948df8bae1dSRodney W. Grimes 	while (m0) {
949df8bae1dSRodney W. Grimes 		for (m = m0; m; m = m->m_next)
950df8bae1dSRodney W. Grimes 			if (m->m_type == MT_CONTROL &&
951df8bae1dSRodney W. Grimes 			    m->m_len >= sizeof(*cm)) {
952df8bae1dSRodney W. Grimes 				cm = mtod(m, struct cmsghdr *);
953df8bae1dSRodney W. Grimes 				if (cm->cmsg_level != SOL_SOCKET ||
954df8bae1dSRodney W. Grimes 				    cm->cmsg_type != SCM_RIGHTS)
955df8bae1dSRodney W. Grimes 					continue;
956df8bae1dSRodney W. Grimes 				qfds = (cm->cmsg_len - sizeof *cm)
957df8bae1dSRodney W. Grimes 						/ sizeof (struct file *);
958df8bae1dSRodney W. Grimes 				rp = (struct file **)(cm + 1);
959df8bae1dSRodney W. Grimes 				for (i = 0; i < qfds; i++)
960df8bae1dSRodney W. Grimes 					(*op)(*rp++);
961df8bae1dSRodney W. Grimes 				break;		/* XXX, but saves time */
962df8bae1dSRodney W. Grimes 			}
963df8bae1dSRodney W. Grimes 		m0 = m0->m_act;
964df8bae1dSRodney W. Grimes 	}
965df8bae1dSRodney W. Grimes }
966df8bae1dSRodney W. Grimes 
967f708ef1bSPoul-Henning Kamp static void
968df8bae1dSRodney W. Grimes unp_mark(fp)
969df8bae1dSRodney W. Grimes 	struct file *fp;
970df8bae1dSRodney W. Grimes {
971df8bae1dSRodney W. Grimes 
972df8bae1dSRodney W. Grimes 	if (fp->f_flag & FMARK)
973df8bae1dSRodney W. Grimes 		return;
974df8bae1dSRodney W. Grimes 	unp_defer++;
975df8bae1dSRodney W. Grimes 	fp->f_flag |= (FMARK|FDEFER);
976df8bae1dSRodney W. Grimes }
977df8bae1dSRodney W. Grimes 
978f708ef1bSPoul-Henning Kamp static void
979df8bae1dSRodney W. Grimes unp_discard(fp)
980df8bae1dSRodney W. Grimes 	struct file *fp;
981df8bae1dSRodney W. Grimes {
982df8bae1dSRodney W. Grimes 
983df8bae1dSRodney W. Grimes 	fp->f_msgcount--;
984df8bae1dSRodney W. Grimes 	unp_rights--;
985df8bae1dSRodney W. Grimes 	(void) closef(fp, (struct proc *)NULL);
986df8bae1dSRodney W. Grimes }
987