uipc_usrreq.c revision 1.26 1 1.26 thorpej /* $NetBSD: uipc_usrreq.c,v 1.26 1997/06/24 19:12:55 thorpej Exp $ */
2 1.10 cgd
3 1.1 cgd /*
4 1.24 cgd * Copyright (c) 1997 Christopher G. Demetriou. All rights reserved.
5 1.8 mycroft * Copyright (c) 1982, 1986, 1989, 1991, 1993
6 1.8 mycroft * The Regents of the University of California. All rights reserved.
7 1.1 cgd *
8 1.1 cgd * Redistribution and use in source and binary forms, with or without
9 1.1 cgd * modification, are permitted provided that the following conditions
10 1.1 cgd * are met:
11 1.1 cgd * 1. Redistributions of source code must retain the above copyright
12 1.1 cgd * notice, this list of conditions and the following disclaimer.
13 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer in the
15 1.1 cgd * documentation and/or other materials provided with the distribution.
16 1.1 cgd * 3. All advertising materials mentioning features or use of this software
17 1.1 cgd * must display the following acknowledgement:
18 1.1 cgd * This product includes software developed by the University of
19 1.1 cgd * California, Berkeley and its contributors.
20 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
21 1.1 cgd * may be used to endorse or promote products derived from this software
22 1.1 cgd * without specific prior written permission.
23 1.1 cgd *
24 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.1 cgd * SUCH DAMAGE.
35 1.1 cgd *
36 1.10 cgd * @(#)uipc_usrreq.c 8.3 (Berkeley) 1/4/94
37 1.1 cgd */
38 1.1 cgd
39 1.7 mycroft #include <sys/param.h>
40 1.8 mycroft #include <sys/systm.h>
41 1.7 mycroft #include <sys/proc.h>
42 1.7 mycroft #include <sys/filedesc.h>
43 1.7 mycroft #include <sys/domain.h>
44 1.7 mycroft #include <sys/protosw.h>
45 1.7 mycroft #include <sys/socket.h>
46 1.7 mycroft #include <sys/socketvar.h>
47 1.7 mycroft #include <sys/unpcb.h>
48 1.7 mycroft #include <sys/un.h>
49 1.7 mycroft #include <sys/namei.h>
50 1.7 mycroft #include <sys/vnode.h>
51 1.7 mycroft #include <sys/file.h>
52 1.7 mycroft #include <sys/stat.h>
53 1.7 mycroft #include <sys/mbuf.h>
54 1.1 cgd
55 1.1 cgd /*
56 1.1 cgd * Unix communications domain.
57 1.1 cgd *
58 1.1 cgd * TODO:
59 1.1 cgd * SEQPACKET, RDM
60 1.1 cgd * rethink name space problems
61 1.1 cgd * need a proper out-of-band
62 1.1 cgd */
63 1.20 mycroft struct sockaddr_un sun_noname = { sizeof(sun_noname), AF_UNIX };
64 1.1 cgd ino_t unp_ino; /* prototype for fake inode numbers */
65 1.1 cgd
66 1.20 mycroft int
67 1.20 mycroft unp_output(m, control, unp)
68 1.20 mycroft struct mbuf *m, *control;
69 1.20 mycroft struct unpcb *unp;
70 1.20 mycroft {
71 1.20 mycroft struct socket *so2;
72 1.20 mycroft struct sockaddr_un *sun;
73 1.20 mycroft
74 1.20 mycroft so2 = unp->unp_conn->unp_socket;
75 1.20 mycroft if (unp->unp_addr)
76 1.20 mycroft sun = unp->unp_addr;
77 1.20 mycroft else
78 1.20 mycroft sun = &sun_noname;
79 1.20 mycroft if (sbappendaddr(&so2->so_rcv, (struct sockaddr *)sun, m,
80 1.20 mycroft control) == 0) {
81 1.20 mycroft m_freem(control);
82 1.20 mycroft m_freem(m);
83 1.20 mycroft return (EINVAL);
84 1.20 mycroft } else {
85 1.20 mycroft sorwakeup(so2);
86 1.20 mycroft return (0);
87 1.20 mycroft }
88 1.20 mycroft }
89 1.20 mycroft
90 1.20 mycroft void
91 1.20 mycroft unp_setsockaddr(unp, nam)
92 1.20 mycroft register struct unpcb *unp;
93 1.20 mycroft struct mbuf *nam;
94 1.20 mycroft {
95 1.20 mycroft struct sockaddr_un *sun;
96 1.20 mycroft
97 1.20 mycroft if (unp->unp_addr)
98 1.20 mycroft sun = unp->unp_addr;
99 1.20 mycroft else
100 1.20 mycroft sun = &sun_noname;
101 1.20 mycroft nam->m_len = sun->sun_len;
102 1.20 mycroft bcopy(sun, mtod(nam, caddr_t), (size_t)nam->m_len);
103 1.20 mycroft }
104 1.20 mycroft
105 1.20 mycroft void
106 1.20 mycroft unp_setpeeraddr(unp, nam)
107 1.20 mycroft register struct unpcb *unp;
108 1.20 mycroft struct mbuf *nam;
109 1.20 mycroft {
110 1.20 mycroft struct sockaddr_un *sun;
111 1.20 mycroft
112 1.20 mycroft if (unp->unp_conn && unp->unp_conn->unp_addr)
113 1.20 mycroft sun = unp->unp_conn->unp_addr;
114 1.20 mycroft else
115 1.20 mycroft sun = &sun_noname;
116 1.20 mycroft nam->m_len = sun->sun_len;
117 1.20 mycroft bcopy(sun, mtod(nam, caddr_t), (size_t)nam->m_len);
118 1.20 mycroft }
119 1.20 mycroft
120 1.1 cgd /*ARGSUSED*/
121 1.5 andrew int
122 1.19 mycroft uipc_usrreq(so, req, m, nam, control, p)
123 1.1 cgd struct socket *so;
124 1.1 cgd int req;
125 1.1 cgd struct mbuf *m, *nam, *control;
126 1.19 mycroft struct proc *p;
127 1.1 cgd {
128 1.1 cgd struct unpcb *unp = sotounpcb(so);
129 1.1 cgd register struct socket *so2;
130 1.1 cgd register int error = 0;
131 1.1 cgd
132 1.1 cgd if (req == PRU_CONTROL)
133 1.1 cgd return (EOPNOTSUPP);
134 1.20 mycroft
135 1.22 mycroft #ifdef DIAGNOSTIC
136 1.22 mycroft if (req != PRU_SEND && req != PRU_SENDOOB && control)
137 1.22 mycroft panic("uipc_usrreq: unexpected control mbuf");
138 1.22 mycroft #endif
139 1.1 cgd if (unp == 0 && req != PRU_ATTACH) {
140 1.1 cgd error = EINVAL;
141 1.1 cgd goto release;
142 1.1 cgd }
143 1.20 mycroft
144 1.1 cgd switch (req) {
145 1.1 cgd
146 1.1 cgd case PRU_ATTACH:
147 1.20 mycroft if (unp != 0) {
148 1.1 cgd error = EISCONN;
149 1.1 cgd break;
150 1.1 cgd }
151 1.1 cgd error = unp_attach(so);
152 1.1 cgd break;
153 1.1 cgd
154 1.1 cgd case PRU_DETACH:
155 1.1 cgd unp_detach(unp);
156 1.1 cgd break;
157 1.1 cgd
158 1.1 cgd case PRU_BIND:
159 1.1 cgd error = unp_bind(unp, nam, p);
160 1.1 cgd break;
161 1.1 cgd
162 1.1 cgd case PRU_LISTEN:
163 1.1 cgd if (unp->unp_vnode == 0)
164 1.1 cgd error = EINVAL;
165 1.1 cgd break;
166 1.1 cgd
167 1.1 cgd case PRU_CONNECT:
168 1.1 cgd error = unp_connect(so, nam, p);
169 1.1 cgd break;
170 1.1 cgd
171 1.1 cgd case PRU_CONNECT2:
172 1.1 cgd error = unp_connect2(so, (struct socket *)nam);
173 1.1 cgd break;
174 1.1 cgd
175 1.1 cgd case PRU_DISCONNECT:
176 1.1 cgd unp_disconnect(unp);
177 1.1 cgd break;
178 1.1 cgd
179 1.1 cgd case PRU_ACCEPT:
180 1.20 mycroft unp_setpeeraddr(unp, nam);
181 1.1 cgd break;
182 1.1 cgd
183 1.1 cgd case PRU_SHUTDOWN:
184 1.1 cgd socantsendmore(so);
185 1.1 cgd unp_shutdown(unp);
186 1.1 cgd break;
187 1.1 cgd
188 1.1 cgd case PRU_RCVD:
189 1.1 cgd switch (so->so_type) {
190 1.1 cgd
191 1.1 cgd case SOCK_DGRAM:
192 1.1 cgd panic("uipc 1");
193 1.1 cgd /*NOTREACHED*/
194 1.1 cgd
195 1.1 cgd case SOCK_STREAM:
196 1.1 cgd #define rcv (&so->so_rcv)
197 1.1 cgd #define snd (&so2->so_snd)
198 1.1 cgd if (unp->unp_conn == 0)
199 1.1 cgd break;
200 1.1 cgd so2 = unp->unp_conn->unp_socket;
201 1.1 cgd /*
202 1.1 cgd * Adjust backpressure on sender
203 1.1 cgd * and wakeup any waiting to write.
204 1.1 cgd */
205 1.1 cgd snd->sb_mbmax += unp->unp_mbcnt - rcv->sb_mbcnt;
206 1.1 cgd unp->unp_mbcnt = rcv->sb_mbcnt;
207 1.1 cgd snd->sb_hiwat += unp->unp_cc - rcv->sb_cc;
208 1.1 cgd unp->unp_cc = rcv->sb_cc;
209 1.1 cgd sowwakeup(so2);
210 1.1 cgd #undef snd
211 1.1 cgd #undef rcv
212 1.1 cgd break;
213 1.1 cgd
214 1.1 cgd default:
215 1.1 cgd panic("uipc 2");
216 1.1 cgd }
217 1.1 cgd break;
218 1.1 cgd
219 1.1 cgd case PRU_SEND:
220 1.1 cgd if (control && (error = unp_internalize(control, p)))
221 1.1 cgd break;
222 1.1 cgd switch (so->so_type) {
223 1.1 cgd
224 1.1 cgd case SOCK_DGRAM: {
225 1.1 cgd if (nam) {
226 1.20 mycroft if ((so->so_state & SS_ISCONNECTED) != 0) {
227 1.1 cgd error = EISCONN;
228 1.21 mycroft goto die;
229 1.1 cgd }
230 1.1 cgd error = unp_connect(so, nam, p);
231 1.20 mycroft if (error) {
232 1.23 mycroft die:
233 1.21 mycroft m_freem(control);
234 1.20 mycroft m_freem(m);
235 1.1 cgd break;
236 1.20 mycroft }
237 1.1 cgd } else {
238 1.20 mycroft if ((so->so_state & SS_ISCONNECTED) == 0) {
239 1.1 cgd error = ENOTCONN;
240 1.21 mycroft goto die;
241 1.1 cgd }
242 1.1 cgd }
243 1.20 mycroft error = unp_output(m, control, unp);
244 1.1 cgd if (nam)
245 1.1 cgd unp_disconnect(unp);
246 1.1 cgd break;
247 1.1 cgd }
248 1.1 cgd
249 1.1 cgd case SOCK_STREAM:
250 1.1 cgd #define rcv (&so2->so_rcv)
251 1.1 cgd #define snd (&so->so_snd)
252 1.1 cgd if (unp->unp_conn == 0)
253 1.1 cgd panic("uipc 3");
254 1.1 cgd so2 = unp->unp_conn->unp_socket;
255 1.1 cgd /*
256 1.1 cgd * Send to paired receive port, and then reduce
257 1.1 cgd * send buffer hiwater marks to maintain backpressure.
258 1.1 cgd * Wake up readers.
259 1.1 cgd */
260 1.1 cgd if (control) {
261 1.21 mycroft if (sbappendcontrol(rcv, m, control) == 0)
262 1.21 mycroft m_freem(control);
263 1.1 cgd } else
264 1.1 cgd sbappend(rcv, m);
265 1.1 cgd snd->sb_mbmax -=
266 1.1 cgd rcv->sb_mbcnt - unp->unp_conn->unp_mbcnt;
267 1.1 cgd unp->unp_conn->unp_mbcnt = rcv->sb_mbcnt;
268 1.1 cgd snd->sb_hiwat -= rcv->sb_cc - unp->unp_conn->unp_cc;
269 1.1 cgd unp->unp_conn->unp_cc = rcv->sb_cc;
270 1.1 cgd sorwakeup(so2);
271 1.1 cgd #undef snd
272 1.1 cgd #undef rcv
273 1.1 cgd break;
274 1.1 cgd
275 1.1 cgd default:
276 1.1 cgd panic("uipc 4");
277 1.1 cgd }
278 1.1 cgd break;
279 1.1 cgd
280 1.1 cgd case PRU_ABORT:
281 1.1 cgd unp_drop(unp, ECONNABORTED);
282 1.1 cgd break;
283 1.1 cgd
284 1.1 cgd case PRU_SENSE:
285 1.1 cgd ((struct stat *) m)->st_blksize = so->so_snd.sb_hiwat;
286 1.1 cgd if (so->so_type == SOCK_STREAM && unp->unp_conn != 0) {
287 1.1 cgd so2 = unp->unp_conn->unp_socket;
288 1.1 cgd ((struct stat *) m)->st_blksize += so2->so_rcv.sb_cc;
289 1.1 cgd }
290 1.1 cgd ((struct stat *) m)->st_dev = NODEV;
291 1.1 cgd if (unp->unp_ino == 0)
292 1.1 cgd unp->unp_ino = unp_ino++;
293 1.25 kleink ((struct stat *) m)->st_atimespec =
294 1.25 kleink ((struct stat *) m)->st_mtimespec =
295 1.25 kleink ((struct stat *) m)->st_ctimespec = unp->unp_ctime;
296 1.1 cgd ((struct stat *) m)->st_ino = unp->unp_ino;
297 1.1 cgd return (0);
298 1.1 cgd
299 1.1 cgd case PRU_RCVOOB:
300 1.20 mycroft error = EOPNOTSUPP;
301 1.20 mycroft break;
302 1.1 cgd
303 1.1 cgd case PRU_SENDOOB:
304 1.22 mycroft m_freem(control);
305 1.20 mycroft m_freem(m);
306 1.1 cgd error = EOPNOTSUPP;
307 1.1 cgd break;
308 1.1 cgd
309 1.1 cgd case PRU_SOCKADDR:
310 1.20 mycroft unp_setsockaddr(unp, nam);
311 1.1 cgd break;
312 1.1 cgd
313 1.1 cgd case PRU_PEERADDR:
314 1.20 mycroft unp_setpeeraddr(unp, nam);
315 1.1 cgd break;
316 1.1 cgd
317 1.1 cgd default:
318 1.1 cgd panic("piusrreq");
319 1.1 cgd }
320 1.20 mycroft
321 1.1 cgd release:
322 1.1 cgd return (error);
323 1.1 cgd }
324 1.1 cgd
325 1.1 cgd /*
326 1.1 cgd * Both send and receive buffers are allocated PIPSIZ bytes of buffering
327 1.1 cgd * for stream sockets, although the total for sender and receiver is
328 1.1 cgd * actually only PIPSIZ.
329 1.1 cgd * Datagram sockets really use the sendspace as the maximum datagram size,
330 1.1 cgd * and don't really want to reserve the sendspace. Their recvspace should
331 1.1 cgd * be large enough for at least one max-size datagram plus address.
332 1.1 cgd */
333 1.1 cgd #define PIPSIZ 4096
334 1.1 cgd u_long unpst_sendspace = PIPSIZ;
335 1.1 cgd u_long unpst_recvspace = PIPSIZ;
336 1.1 cgd u_long unpdg_sendspace = 2*1024; /* really max datagram size */
337 1.1 cgd u_long unpdg_recvspace = 4*1024;
338 1.1 cgd
339 1.1 cgd int unp_rights; /* file descriptors in flight */
340 1.1 cgd
341 1.5 andrew int
342 1.1 cgd unp_attach(so)
343 1.1 cgd struct socket *so;
344 1.1 cgd {
345 1.1 cgd register struct unpcb *unp;
346 1.25 kleink struct timeval tv;
347 1.1 cgd int error;
348 1.1 cgd
349 1.1 cgd if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
350 1.1 cgd switch (so->so_type) {
351 1.1 cgd
352 1.1 cgd case SOCK_STREAM:
353 1.1 cgd error = soreserve(so, unpst_sendspace, unpst_recvspace);
354 1.1 cgd break;
355 1.1 cgd
356 1.1 cgd case SOCK_DGRAM:
357 1.1 cgd error = soreserve(so, unpdg_sendspace, unpdg_recvspace);
358 1.1 cgd break;
359 1.8 mycroft
360 1.8 mycroft default:
361 1.8 mycroft panic("unp_attach");
362 1.1 cgd }
363 1.1 cgd if (error)
364 1.1 cgd return (error);
365 1.1 cgd }
366 1.14 mycroft unp = malloc(sizeof(*unp), M_PCB, M_NOWAIT);
367 1.14 mycroft if (unp == NULL)
368 1.1 cgd return (ENOBUFS);
369 1.14 mycroft bzero((caddr_t)unp, sizeof(*unp));
370 1.14 mycroft unp->unp_socket = so;
371 1.15 mycroft so->so_pcb = unp;
372 1.25 kleink microtime(&tv);
373 1.25 kleink TIMEVAL_TO_TIMESPEC(&tv, &unp->unp_ctime);
374 1.1 cgd return (0);
375 1.1 cgd }
376 1.1 cgd
377 1.17 pk void
378 1.1 cgd unp_detach(unp)
379 1.1 cgd register struct unpcb *unp;
380 1.1 cgd {
381 1.1 cgd
382 1.1 cgd if (unp->unp_vnode) {
383 1.1 cgd unp->unp_vnode->v_socket = 0;
384 1.1 cgd vrele(unp->unp_vnode);
385 1.1 cgd unp->unp_vnode = 0;
386 1.1 cgd }
387 1.1 cgd if (unp->unp_conn)
388 1.1 cgd unp_disconnect(unp);
389 1.1 cgd while (unp->unp_refs)
390 1.1 cgd unp_drop(unp->unp_refs, ECONNRESET);
391 1.1 cgd soisdisconnected(unp->unp_socket);
392 1.1 cgd unp->unp_socket->so_pcb = 0;
393 1.20 mycroft if (unp->unp_addr)
394 1.26 thorpej free(unp->unp_addr, M_SONAME);
395 1.8 mycroft if (unp_rights) {
396 1.8 mycroft /*
397 1.8 mycroft * Normally the receive buffer is flushed later,
398 1.8 mycroft * in sofree, but if our receive buffer holds references
399 1.8 mycroft * to descriptors that are now garbage, we will dispose
400 1.8 mycroft * of those descriptor references after the garbage collector
401 1.8 mycroft * gets them (resulting in a "panic: closef: count < 0").
402 1.8 mycroft */
403 1.8 mycroft sorflush(unp->unp_socket);
404 1.14 mycroft free(unp, M_PCB);
405 1.1 cgd unp_gc();
406 1.14 mycroft } else
407 1.14 mycroft free(unp, M_PCB);
408 1.1 cgd }
409 1.1 cgd
410 1.5 andrew int
411 1.1 cgd unp_bind(unp, nam, p)
412 1.1 cgd struct unpcb *unp;
413 1.1 cgd struct mbuf *nam;
414 1.1 cgd struct proc *p;
415 1.1 cgd {
416 1.20 mycroft struct sockaddr_un *sun = mtod(nam, struct sockaddr_un *);
417 1.1 cgd register struct vnode *vp;
418 1.1 cgd struct vattr vattr;
419 1.1 cgd int error;
420 1.1 cgd struct nameidata nd;
421 1.1 cgd
422 1.26 thorpej if (nam->m_len > sizeof(struct sockaddr_un))
423 1.26 thorpej return (EINVAL);
424 1.20 mycroft if (unp->unp_vnode != 0)
425 1.20 mycroft return (EINVAL);
426 1.9 mycroft NDINIT(&nd, CREATE, FOLLOW | LOCKPARENT, UIO_SYSSPACE,
427 1.20 mycroft sun->sun_path, p);
428 1.20 mycroft if (nam->m_data + nam->m_len == &nam->m_dat[MLEN]) { /* XXX */
429 1.1 cgd if (*(mtod(nam, caddr_t) + nam->m_len - 1) != 0)
430 1.1 cgd return (EINVAL);
431 1.1 cgd } else
432 1.1 cgd *(mtod(nam, caddr_t) + nam->m_len) = 0;
433 1.1 cgd /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */
434 1.16 christos if ((error = namei(&nd)) != 0)
435 1.1 cgd return (error);
436 1.9 mycroft vp = nd.ni_vp;
437 1.1 cgd if (vp != NULL) {
438 1.9 mycroft VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
439 1.9 mycroft if (nd.ni_dvp == vp)
440 1.9 mycroft vrele(nd.ni_dvp);
441 1.1 cgd else
442 1.9 mycroft vput(nd.ni_dvp);
443 1.1 cgd vrele(vp);
444 1.1 cgd return (EADDRINUSE);
445 1.1 cgd }
446 1.1 cgd VATTR_NULL(&vattr);
447 1.1 cgd vattr.va_type = VSOCK;
448 1.9 mycroft vattr.va_mode = ACCESSPERMS;
449 1.12 mycroft VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
450 1.16 christos error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
451 1.16 christos if (error)
452 1.1 cgd return (error);
453 1.9 mycroft vp = nd.ni_vp;
454 1.1 cgd vp->v_socket = unp->unp_socket;
455 1.1 cgd unp->unp_vnode = vp;
456 1.26 thorpej unp->unp_addrlen = nam->m_len;
457 1.26 thorpej unp->unp_addr = malloc(unp->unp_addrlen, M_SONAME, M_WAITOK);
458 1.26 thorpej m_copydata(nam, 0, unp->unp_addrlen, (caddr_t)unp->unp_addr);
459 1.1 cgd VOP_UNLOCK(vp);
460 1.1 cgd return (0);
461 1.1 cgd }
462 1.1 cgd
463 1.5 andrew int
464 1.1 cgd unp_connect(so, nam, p)
465 1.1 cgd struct socket *so;
466 1.1 cgd struct mbuf *nam;
467 1.1 cgd struct proc *p;
468 1.1 cgd {
469 1.20 mycroft register struct sockaddr_un *sun = mtod(nam, struct sockaddr_un *);
470 1.1 cgd register struct vnode *vp;
471 1.1 cgd register struct socket *so2, *so3;
472 1.1 cgd struct unpcb *unp2, *unp3;
473 1.1 cgd int error;
474 1.1 cgd struct nameidata nd;
475 1.1 cgd
476 1.20 mycroft NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, sun->sun_path, p);
477 1.1 cgd if (nam->m_data + nam->m_len == &nam->m_dat[MLEN]) { /* XXX */
478 1.1 cgd if (*(mtod(nam, caddr_t) + nam->m_len - 1) != 0)
479 1.20 mycroft return (EINVAL);
480 1.1 cgd } else
481 1.1 cgd *(mtod(nam, caddr_t) + nam->m_len) = 0;
482 1.16 christos if ((error = namei(&nd)) != 0)
483 1.1 cgd return (error);
484 1.9 mycroft vp = nd.ni_vp;
485 1.1 cgd if (vp->v_type != VSOCK) {
486 1.1 cgd error = ENOTSOCK;
487 1.1 cgd goto bad;
488 1.1 cgd }
489 1.16 christos if ((error = VOP_ACCESS(vp, VWRITE, p->p_ucred, p)) != 0)
490 1.1 cgd goto bad;
491 1.1 cgd so2 = vp->v_socket;
492 1.1 cgd if (so2 == 0) {
493 1.1 cgd error = ECONNREFUSED;
494 1.1 cgd goto bad;
495 1.1 cgd }
496 1.1 cgd if (so->so_type != so2->so_type) {
497 1.1 cgd error = EPROTOTYPE;
498 1.1 cgd goto bad;
499 1.1 cgd }
500 1.1 cgd if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
501 1.1 cgd if ((so2->so_options & SO_ACCEPTCONN) == 0 ||
502 1.1 cgd (so3 = sonewconn(so2, 0)) == 0) {
503 1.1 cgd error = ECONNREFUSED;
504 1.1 cgd goto bad;
505 1.1 cgd }
506 1.1 cgd unp2 = sotounpcb(so2);
507 1.1 cgd unp3 = sotounpcb(so3);
508 1.26 thorpej if (unp2->unp_addr) {
509 1.26 thorpej unp3->unp_addr = malloc(unp2->unp_addrlen,
510 1.26 thorpej M_SONAME, M_WAITOK);
511 1.26 thorpej bcopy(unp2->unp_addr, unp3->unp_addr,
512 1.26 thorpej unp2->unp_addrlen);
513 1.26 thorpej unp3->unp_addrlen = unp2->unp_addrlen;
514 1.26 thorpej }
515 1.1 cgd so2 = so3;
516 1.1 cgd }
517 1.1 cgd error = unp_connect2(so, so2);
518 1.1 cgd bad:
519 1.1 cgd vput(vp);
520 1.1 cgd return (error);
521 1.1 cgd }
522 1.1 cgd
523 1.5 andrew int
524 1.1 cgd unp_connect2(so, so2)
525 1.1 cgd register struct socket *so;
526 1.1 cgd register struct socket *so2;
527 1.1 cgd {
528 1.1 cgd register struct unpcb *unp = sotounpcb(so);
529 1.1 cgd register struct unpcb *unp2;
530 1.1 cgd
531 1.1 cgd if (so2->so_type != so->so_type)
532 1.1 cgd return (EPROTOTYPE);
533 1.1 cgd unp2 = sotounpcb(so2);
534 1.1 cgd unp->unp_conn = unp2;
535 1.1 cgd switch (so->so_type) {
536 1.1 cgd
537 1.1 cgd case SOCK_DGRAM:
538 1.1 cgd unp->unp_nextref = unp2->unp_refs;
539 1.1 cgd unp2->unp_refs = unp;
540 1.1 cgd soisconnected(so);
541 1.1 cgd break;
542 1.1 cgd
543 1.1 cgd case SOCK_STREAM:
544 1.1 cgd unp2->unp_conn = unp;
545 1.1 cgd soisconnected(so);
546 1.1 cgd soisconnected(so2);
547 1.1 cgd break;
548 1.1 cgd
549 1.1 cgd default:
550 1.1 cgd panic("unp_connect2");
551 1.1 cgd }
552 1.1 cgd return (0);
553 1.1 cgd }
554 1.1 cgd
555 1.5 andrew void
556 1.1 cgd unp_disconnect(unp)
557 1.1 cgd struct unpcb *unp;
558 1.1 cgd {
559 1.1 cgd register struct unpcb *unp2 = unp->unp_conn;
560 1.1 cgd
561 1.1 cgd if (unp2 == 0)
562 1.1 cgd return;
563 1.1 cgd unp->unp_conn = 0;
564 1.1 cgd switch (unp->unp_socket->so_type) {
565 1.1 cgd
566 1.1 cgd case SOCK_DGRAM:
567 1.1 cgd if (unp2->unp_refs == unp)
568 1.1 cgd unp2->unp_refs = unp->unp_nextref;
569 1.1 cgd else {
570 1.1 cgd unp2 = unp2->unp_refs;
571 1.1 cgd for (;;) {
572 1.1 cgd if (unp2 == 0)
573 1.1 cgd panic("unp_disconnect");
574 1.1 cgd if (unp2->unp_nextref == unp)
575 1.1 cgd break;
576 1.1 cgd unp2 = unp2->unp_nextref;
577 1.1 cgd }
578 1.1 cgd unp2->unp_nextref = unp->unp_nextref;
579 1.1 cgd }
580 1.1 cgd unp->unp_nextref = 0;
581 1.1 cgd unp->unp_socket->so_state &= ~SS_ISCONNECTED;
582 1.1 cgd break;
583 1.1 cgd
584 1.1 cgd case SOCK_STREAM:
585 1.1 cgd soisdisconnected(unp->unp_socket);
586 1.1 cgd unp2->unp_conn = 0;
587 1.1 cgd soisdisconnected(unp2->unp_socket);
588 1.1 cgd break;
589 1.1 cgd }
590 1.1 cgd }
591 1.1 cgd
592 1.1 cgd #ifdef notdef
593 1.1 cgd unp_abort(unp)
594 1.1 cgd struct unpcb *unp;
595 1.1 cgd {
596 1.1 cgd
597 1.1 cgd unp_detach(unp);
598 1.1 cgd }
599 1.1 cgd #endif
600 1.1 cgd
601 1.5 andrew void
602 1.1 cgd unp_shutdown(unp)
603 1.1 cgd struct unpcb *unp;
604 1.1 cgd {
605 1.1 cgd struct socket *so;
606 1.1 cgd
607 1.1 cgd if (unp->unp_socket->so_type == SOCK_STREAM && unp->unp_conn &&
608 1.1 cgd (so = unp->unp_conn->unp_socket))
609 1.1 cgd socantrcvmore(so);
610 1.1 cgd }
611 1.1 cgd
612 1.5 andrew void
613 1.1 cgd unp_drop(unp, errno)
614 1.1 cgd struct unpcb *unp;
615 1.1 cgd int errno;
616 1.1 cgd {
617 1.1 cgd struct socket *so = unp->unp_socket;
618 1.1 cgd
619 1.1 cgd so->so_error = errno;
620 1.1 cgd unp_disconnect(unp);
621 1.1 cgd if (so->so_head) {
622 1.15 mycroft so->so_pcb = 0;
623 1.14 mycroft sofree(so);
624 1.20 mycroft if (unp->unp_addr)
625 1.26 thorpej free(unp->unp_addr, M_SONAME);
626 1.14 mycroft free(unp, M_PCB);
627 1.1 cgd }
628 1.1 cgd }
629 1.1 cgd
630 1.1 cgd #ifdef notdef
631 1.1 cgd unp_drain()
632 1.1 cgd {
633 1.1 cgd
634 1.1 cgd }
635 1.1 cgd #endif
636 1.1 cgd
637 1.5 andrew int
638 1.1 cgd unp_externalize(rights)
639 1.1 cgd struct mbuf *rights;
640 1.1 cgd {
641 1.1 cgd struct proc *p = curproc; /* XXX */
642 1.1 cgd register struct cmsghdr *cm = mtod(rights, struct cmsghdr *);
643 1.24 cgd register int i, *fdp = (int *)(cm + 1);
644 1.24 cgd register struct file **rp = (struct file **)ALIGN(cm + 1);
645 1.1 cgd register struct file *fp;
646 1.24 cgd int nfds = (cm->cmsg_len - ALIGN(sizeof(*cm))) / sizeof (struct file *);
647 1.1 cgd int f;
648 1.1 cgd
649 1.24 cgd /* Make sure that the recipient has space */
650 1.24 cgd if (!fdavail(p, nfds)) {
651 1.24 cgd for (i = 0; i < nfds; i++) {
652 1.1 cgd fp = *rp;
653 1.1 cgd unp_discard(fp);
654 1.1 cgd *rp++ = 0;
655 1.1 cgd }
656 1.1 cgd return (EMSGSIZE);
657 1.1 cgd }
658 1.24 cgd
659 1.24 cgd /*
660 1.24 cgd * Add file to the recipient's open file table, converting them
661 1.24 cgd * to integer file descriptors as we go. Done in forward order
662 1.24 cgd * because an integer will always come in the same place or before
663 1.24 cgd * its corresponding struct file pointer.
664 1.24 cgd */
665 1.24 cgd for (i = 0; i < nfds; i++) {
666 1.1 cgd if (fdalloc(p, 0, &f))
667 1.1 cgd panic("unp_externalize");
668 1.1 cgd fp = *rp;
669 1.1 cgd p->p_fd->fd_ofiles[f] = fp;
670 1.1 cgd fp->f_msgcount--;
671 1.1 cgd unp_rights--;
672 1.24 cgd *fdp++ = f;
673 1.1 cgd }
674 1.24 cgd
675 1.24 cgd /*
676 1.24 cgd * Adjust length, in case of transition from large struct file
677 1.24 cgd * pointers to ints.
678 1.24 cgd */
679 1.24 cgd cm->cmsg_len = sizeof(*cm) + (nfds * sizeof(int));
680 1.24 cgd rights->m_len = cm->cmsg_len;
681 1.1 cgd return (0);
682 1.1 cgd }
683 1.1 cgd
684 1.5 andrew int
685 1.1 cgd unp_internalize(control, p)
686 1.1 cgd struct mbuf *control;
687 1.1 cgd struct proc *p;
688 1.1 cgd {
689 1.24 cgd struct filedesc *fdescp = p->p_fd;
690 1.1 cgd register struct cmsghdr *cm = mtod(control, struct cmsghdr *);
691 1.1 cgd register struct file **rp;
692 1.1 cgd register struct file *fp;
693 1.24 cgd register int i, fd, *fdp;
694 1.24 cgd int nfds;
695 1.24 cgd u_int neededspace;
696 1.1 cgd
697 1.24 cgd /* Sanity check the control message header */
698 1.1 cgd if (cm->cmsg_type != SCM_RIGHTS || cm->cmsg_level != SOL_SOCKET ||
699 1.1 cgd cm->cmsg_len != control->m_len)
700 1.1 cgd return (EINVAL);
701 1.24 cgd
702 1.24 cgd /* Verify that the file descriptors are valid */
703 1.24 cgd nfds = (cm->cmsg_len - sizeof (*cm)) / sizeof (int);
704 1.24 cgd fdp = (int *)(cm + 1);
705 1.24 cgd for (i = 0; i < nfds; i++) {
706 1.24 cgd fd = *fdp++;
707 1.24 cgd if ((unsigned)fd >= fdescp->fd_nfiles ||
708 1.24 cgd fdescp->fd_ofiles[fd] == NULL)
709 1.1 cgd return (EBADF);
710 1.1 cgd }
711 1.24 cgd
712 1.24 cgd /* Make sure we have room for the struct file pointers */
713 1.24 cgd morespace:
714 1.24 cgd neededspace = (ALIGN(sizeof (*cm)) + nfds * sizeof (struct file *)) -
715 1.24 cgd control->m_len;
716 1.24 cgd if (neededspace > M_TRAILINGSPACE(control)) {
717 1.24 cgd
718 1.24 cgd /* if we already have a cluster, the message is just too big */
719 1.24 cgd if (control->m_flags & M_EXT)
720 1.24 cgd return (E2BIG);
721 1.24 cgd
722 1.24 cgd /* allocate a cluster and try again */
723 1.24 cgd MCLGET(control, M_WAIT);
724 1.24 cgd if ((control->m_flags & M_EXT) == 0)
725 1.24 cgd return (ENOBUFS); /* allocation failed */
726 1.24 cgd
727 1.24 cgd /* copy the data to the cluster */
728 1.24 cgd bcopy(cm, mtod(control, char *), cm->cmsg_len);
729 1.24 cgd cm = mtod(control, struct cmsghdr *);
730 1.24 cgd goto morespace;
731 1.24 cgd }
732 1.24 cgd
733 1.24 cgd /* adjust message & mbuf to note amount of space actually used. */
734 1.24 cgd cm->cmsg_len += neededspace;
735 1.24 cgd control->m_len = cm->cmsg_len;
736 1.24 cgd
737 1.24 cgd /*
738 1.24 cgd * Transform the file descriptors into struct file pointers, in
739 1.24 cgd * reverse order so that if pointers are bigger than ints, the
740 1.24 cgd * int won't get until we're done.
741 1.24 cgd */
742 1.24 cgd fdp = ((int *)(cm + 1)) + nfds - 1;
743 1.24 cgd rp = ((struct file **)ALIGN(cm + 1)) + nfds - 1;
744 1.24 cgd for (i = 0; i < nfds; i++) {
745 1.24 cgd fp = fdescp->fd_ofiles[*fdp];
746 1.24 cgd *rp-- = fp;
747 1.1 cgd fp->f_count++;
748 1.1 cgd fp->f_msgcount++;
749 1.1 cgd unp_rights++;
750 1.1 cgd }
751 1.1 cgd return (0);
752 1.1 cgd }
753 1.1 cgd
754 1.1 cgd int unp_defer, unp_gcing;
755 1.1 cgd extern struct domain unixdomain;
756 1.1 cgd
757 1.5 andrew void
758 1.1 cgd unp_gc()
759 1.1 cgd {
760 1.8 mycroft register struct file *fp, *nextfp;
761 1.1 cgd register struct socket *so;
762 1.8 mycroft struct file **extra_ref, **fpp;
763 1.8 mycroft int nunref, i;
764 1.1 cgd
765 1.1 cgd if (unp_gcing)
766 1.1 cgd return;
767 1.1 cgd unp_gcing = 1;
768 1.1 cgd unp_defer = 0;
769 1.11 mycroft for (fp = filehead.lh_first; fp != 0; fp = fp->f_list.le_next)
770 1.1 cgd fp->f_flag &= ~(FMARK|FDEFER);
771 1.1 cgd do {
772 1.11 mycroft for (fp = filehead.lh_first; fp != 0; fp = fp->f_list.le_next) {
773 1.1 cgd if (fp->f_count == 0)
774 1.1 cgd continue;
775 1.1 cgd if (fp->f_flag & FDEFER) {
776 1.1 cgd fp->f_flag &= ~FDEFER;
777 1.1 cgd unp_defer--;
778 1.1 cgd } else {
779 1.1 cgd if (fp->f_flag & FMARK)
780 1.1 cgd continue;
781 1.1 cgd if (fp->f_count == fp->f_msgcount)
782 1.1 cgd continue;
783 1.1 cgd fp->f_flag |= FMARK;
784 1.1 cgd }
785 1.1 cgd if (fp->f_type != DTYPE_SOCKET ||
786 1.1 cgd (so = (struct socket *)fp->f_data) == 0)
787 1.1 cgd continue;
788 1.1 cgd if (so->so_proto->pr_domain != &unixdomain ||
789 1.1 cgd (so->so_proto->pr_flags&PR_RIGHTS) == 0)
790 1.1 cgd continue;
791 1.1 cgd #ifdef notdef
792 1.1 cgd if (so->so_rcv.sb_flags & SB_LOCK) {
793 1.1 cgd /*
794 1.1 cgd * This is problematical; it's not clear
795 1.1 cgd * we need to wait for the sockbuf to be
796 1.1 cgd * unlocked (on a uniprocessor, at least),
797 1.1 cgd * and it's also not clear what to do
798 1.1 cgd * if sbwait returns an error due to receipt
799 1.1 cgd * of a signal. If sbwait does return
800 1.1 cgd * an error, we'll go into an infinite
801 1.1 cgd * loop. Delete all of this for now.
802 1.1 cgd */
803 1.1 cgd (void) sbwait(&so->so_rcv);
804 1.1 cgd goto restart;
805 1.1 cgd }
806 1.1 cgd #endif
807 1.1 cgd unp_scan(so->so_rcv.sb_mb, unp_mark);
808 1.1 cgd }
809 1.1 cgd } while (unp_defer);
810 1.8 mycroft /*
811 1.8 mycroft * We grab an extra reference to each of the file table entries
812 1.8 mycroft * that are not otherwise accessible and then free the rights
813 1.8 mycroft * that are stored in messages on them.
814 1.8 mycroft *
815 1.8 mycroft * The bug in the orginal code is a little tricky, so I'll describe
816 1.8 mycroft * what's wrong with it here.
817 1.8 mycroft *
818 1.8 mycroft * It is incorrect to simply unp_discard each entry for f_msgcount
819 1.8 mycroft * times -- consider the case of sockets A and B that contain
820 1.8 mycroft * references to each other. On a last close of some other socket,
821 1.8 mycroft * we trigger a gc since the number of outstanding rights (unp_rights)
822 1.8 mycroft * is non-zero. If during the sweep phase the gc code un_discards,
823 1.8 mycroft * we end up doing a (full) closef on the descriptor. A closef on A
824 1.8 mycroft * results in the following chain. Closef calls soo_close, which
825 1.8 mycroft * calls soclose. Soclose calls first (through the switch
826 1.8 mycroft * uipc_usrreq) unp_detach, which re-invokes unp_gc. Unp_gc simply
827 1.8 mycroft * returns because the previous instance had set unp_gcing, and
828 1.8 mycroft * we return all the way back to soclose, which marks the socket
829 1.8 mycroft * with SS_NOFDREF, and then calls sofree. Sofree calls sorflush
830 1.8 mycroft * to free up the rights that are queued in messages on the socket A,
831 1.8 mycroft * i.e., the reference on B. The sorflush calls via the dom_dispose
832 1.8 mycroft * switch unp_dispose, which unp_scans with unp_discard. This second
833 1.8 mycroft * instance of unp_discard just calls closef on B.
834 1.8 mycroft *
835 1.8 mycroft * Well, a similar chain occurs on B, resulting in a sorflush on B,
836 1.8 mycroft * which results in another closef on A. Unfortunately, A is already
837 1.8 mycroft * being closed, and the descriptor has already been marked with
838 1.8 mycroft * SS_NOFDREF, and soclose panics at this point.
839 1.8 mycroft *
840 1.8 mycroft * Here, we first take an extra reference to each inaccessible
841 1.8 mycroft * descriptor. Then, we call sorflush ourself, since we know
842 1.8 mycroft * it is a Unix domain socket anyhow. After we destroy all the
843 1.8 mycroft * rights carried in messages, we do a last closef to get rid
844 1.8 mycroft * of our extra reference. This is the last close, and the
845 1.8 mycroft * unp_detach etc will shut down the socket.
846 1.8 mycroft *
847 1.8 mycroft * 91/09/19, bsy (at) cs.cmu.edu
848 1.8 mycroft */
849 1.8 mycroft extra_ref = malloc(nfiles * sizeof(struct file *), M_FILE, M_WAITOK);
850 1.11 mycroft for (nunref = 0, fp = filehead.lh_first, fpp = extra_ref; fp != 0;
851 1.11 mycroft fp = nextfp) {
852 1.11 mycroft nextfp = fp->f_list.le_next;
853 1.1 cgd if (fp->f_count == 0)
854 1.1 cgd continue;
855 1.8 mycroft if (fp->f_count == fp->f_msgcount && !(fp->f_flag & FMARK)) {
856 1.8 mycroft *fpp++ = fp;
857 1.8 mycroft nunref++;
858 1.8 mycroft fp->f_count++;
859 1.8 mycroft }
860 1.1 cgd }
861 1.8 mycroft for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp)
862 1.8 mycroft sorflush((struct socket *)(*fpp)->f_data);
863 1.8 mycroft for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp)
864 1.13 mycroft (void) closef(*fpp, (struct proc *)0);
865 1.8 mycroft free((caddr_t)extra_ref, M_FILE);
866 1.1 cgd unp_gcing = 0;
867 1.1 cgd }
868 1.1 cgd
869 1.5 andrew void
870 1.1 cgd unp_dispose(m)
871 1.1 cgd struct mbuf *m;
872 1.1 cgd {
873 1.8 mycroft
874 1.1 cgd if (m)
875 1.1 cgd unp_scan(m, unp_discard);
876 1.1 cgd }
877 1.1 cgd
878 1.5 andrew void
879 1.1 cgd unp_scan(m0, op)
880 1.1 cgd register struct mbuf *m0;
881 1.5 andrew void (*op) __P((struct file *));
882 1.1 cgd {
883 1.1 cgd register struct mbuf *m;
884 1.1 cgd register struct file **rp;
885 1.1 cgd register struct cmsghdr *cm;
886 1.1 cgd register int i;
887 1.1 cgd int qfds;
888 1.1 cgd
889 1.1 cgd while (m0) {
890 1.1 cgd for (m = m0; m; m = m->m_next)
891 1.1 cgd if (m->m_type == MT_CONTROL &&
892 1.1 cgd m->m_len >= sizeof(*cm)) {
893 1.1 cgd cm = mtod(m, struct cmsghdr *);
894 1.1 cgd if (cm->cmsg_level != SOL_SOCKET ||
895 1.1 cgd cm->cmsg_type != SCM_RIGHTS)
896 1.1 cgd continue;
897 1.1 cgd qfds = (cm->cmsg_len - sizeof *cm)
898 1.1 cgd / sizeof (struct file *);
899 1.1 cgd rp = (struct file **)(cm + 1);
900 1.1 cgd for (i = 0; i < qfds; i++)
901 1.1 cgd (*op)(*rp++);
902 1.1 cgd break; /* XXX, but saves time */
903 1.1 cgd }
904 1.1 cgd m0 = m0->m_act;
905 1.1 cgd }
906 1.1 cgd }
907 1.1 cgd
908 1.5 andrew void
909 1.1 cgd unp_mark(fp)
910 1.1 cgd struct file *fp;
911 1.1 cgd {
912 1.1 cgd
913 1.1 cgd if (fp->f_flag & FMARK)
914 1.1 cgd return;
915 1.1 cgd unp_defer++;
916 1.1 cgd fp->f_flag |= (FMARK|FDEFER);
917 1.1 cgd }
918 1.1 cgd
919 1.5 andrew void
920 1.1 cgd unp_discard(fp)
921 1.1 cgd struct file *fp;
922 1.1 cgd {
923 1.8 mycroft
924 1.1 cgd fp->f_msgcount--;
925 1.1 cgd unp_rights--;
926 1.13 mycroft (void) closef(fp, (struct proc *)0);
927 1.1 cgd }
928