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