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