]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/uipc_usrreq.c
xnu-517.11.1.tar.gz
[apple/xnu.git] / bsd / kern / uipc_usrreq.c
1 /*
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /*
23 * Copyright (c) 1982, 1986, 1989, 1991, 1993
24 * The Regents of the University of California. All rights reserved.
25 *
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
28 * are met:
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * 3. All advertising materials mentioning features or use of this software
35 * must display the following acknowledgement:
36 * This product includes software developed by the University of
37 * California, Berkeley and its contributors.
38 * 4. Neither the name of the University nor the names of its contributors
39 * may be used to endorse or promote products derived from this software
40 * without specific prior written permission.
41 *
42 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * SUCH DAMAGE.
53 *
54 * From: @(#)uipc_usrreq.c 8.3 (Berkeley) 1/4/94
55 */
56
57 #include <sys/param.h>
58 #include <sys/systm.h>
59 #include <sys/kernel.h>
60 #include <sys/domain.h>
61 #include <sys/fcntl.h>
62 #include <sys/malloc.h> /* XXX must be before <sys/file.h> */
63 #include <sys/file.h>
64 #include <sys/filedesc.h>
65 #include <sys/lock.h>
66 #include <sys/mbuf.h>
67 #include <sys/namei.h>
68 #include <sys/proc.h>
69 #include <sys/protosw.h>
70 #include <sys/socket.h>
71 #include <sys/socketvar.h>
72 #include <sys/stat.h>
73 #include <sys/sysctl.h>
74 #include <sys/un.h>
75 #include <sys/unpcb.h>
76 #include <sys/vnode.h>
77
78 #include <kern/zalloc.h>
79
80 struct zone *unp_zone;
81 static unp_gen_t unp_gencnt;
82 static u_int unp_count;
83
84 static struct unp_head unp_shead, unp_dhead;
85
86 /*
87 * Unix communications domain.
88 *
89 * TODO:
90 * SEQPACKET, RDM
91 * rethink name space problems
92 * need a proper out-of-band
93 * lock pushdown
94 */
95 static struct sockaddr sun_noname = { sizeof(sun_noname), AF_LOCAL };
96 static ino_t unp_ino; /* prototype for fake inode numbers */
97
98 static int unp_attach __P((struct socket *));
99 static void unp_detach __P((struct unpcb *));
100 static int unp_bind __P((struct unpcb *,struct sockaddr *, struct proc *));
101 static int unp_connect __P((struct socket *,struct sockaddr *,
102 struct proc *));
103 static void unp_disconnect __P((struct unpcb *));
104 static void unp_shutdown __P((struct unpcb *));
105 static void unp_drop __P((struct unpcb *, int));
106 static void unp_gc __P((void));
107 static void unp_scan __P((struct mbuf *, void (*)(struct file *)));
108 static void unp_mark __P((struct file *));
109 static void unp_discard __P((struct file *));
110 static int unp_internalize __P((struct mbuf *, struct proc *));
111
112 static int
113 uipc_abort(struct socket *so)
114 {
115 struct unpcb *unp = sotounpcb(so);
116
117 if (unp == 0)
118 return EINVAL;
119 unp_drop(unp, ECONNABORTED);
120 return 0;
121 }
122
123 static int
124 uipc_accept(struct socket *so, struct sockaddr **nam)
125 {
126 struct unpcb *unp = sotounpcb(so);
127
128 if (unp == 0)
129 return EINVAL;
130
131 /*
132 * Pass back name of connected socket,
133 * if it was bound and we are still connected
134 * (our peer may have closed already!).
135 */
136 if (unp->unp_conn && unp->unp_conn->unp_addr) {
137 *nam = dup_sockaddr((struct sockaddr *)unp->unp_conn->unp_addr,
138 1);
139 } else {
140 *nam = dup_sockaddr((struct sockaddr *)&sun_noname, 1);
141 }
142 return 0;
143 }
144
145 static int
146 uipc_attach(struct socket *so, int proto, struct proc *p)
147 {
148 struct unpcb *unp = sotounpcb(so);
149
150 if (unp != 0)
151 return EISCONN;
152 return unp_attach(so);
153 }
154
155 static int
156 uipc_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
157 {
158 struct unpcb *unp = sotounpcb(so);
159
160 if (unp == 0)
161 return EINVAL;
162
163 return unp_bind(unp, nam, p);
164 }
165
166 static int
167 uipc_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
168 {
169 struct unpcb *unp = sotounpcb(so);
170
171 if (unp == 0)
172 return EINVAL;
173 return unp_connect(so, nam, p);
174 }
175
176 static int
177 uipc_connect2(struct socket *so1, struct socket *so2)
178 {
179 struct unpcb *unp = sotounpcb(so1);
180
181 if (unp == 0)
182 return EINVAL;
183
184 return unp_connect2(so1, so2);
185 }
186
187 /* control is EOPNOTSUPP */
188
189 static int
190 uipc_detach(struct socket *so)
191 {
192 struct unpcb *unp = sotounpcb(so);
193
194 if (unp == 0)
195 return EINVAL;
196
197 unp_detach(unp);
198 return 0;
199 }
200
201 static int
202 uipc_disconnect(struct socket *so)
203 {
204 struct unpcb *unp = sotounpcb(so);
205
206 if (unp == 0)
207 return EINVAL;
208 unp_disconnect(unp);
209 return 0;
210 }
211
212 static int
213 uipc_listen(struct socket *so, struct proc *p)
214 {
215 struct unpcb *unp = sotounpcb(so);
216
217 if (unp == 0 || unp->unp_vnode == 0)
218 return EINVAL;
219 return 0;
220 }
221
222 static int
223 uipc_peeraddr(struct socket *so, struct sockaddr **nam)
224 {
225 struct unpcb *unp = sotounpcb(so);
226
227 if (unp == 0)
228 return EINVAL;
229 if (unp->unp_conn && unp->unp_conn->unp_addr)
230 *nam = dup_sockaddr((struct sockaddr *)unp->unp_conn->unp_addr,
231 1);
232 return 0;
233 }
234
235 static int
236 uipc_rcvd(struct socket *so, int flags)
237 {
238 struct unpcb *unp = sotounpcb(so);
239 struct socket *so2;
240
241 if (unp == 0)
242 return EINVAL;
243 switch (so->so_type) {
244 case SOCK_DGRAM:
245 panic("uipc_rcvd DGRAM?");
246 /*NOTREACHED*/
247
248 case SOCK_STREAM:
249 #define rcv (&so->so_rcv)
250 #define snd (&so2->so_snd)
251 if (unp->unp_conn == 0)
252 break;
253 so2 = unp->unp_conn->unp_socket;
254 /*
255 * Adjust backpressure on sender
256 * and wakeup any waiting to write.
257 */
258 snd->sb_mbmax += unp->unp_mbcnt - rcv->sb_mbcnt;
259 unp->unp_mbcnt = rcv->sb_mbcnt;
260 snd->sb_hiwat += unp->unp_cc - rcv->sb_cc;
261 unp->unp_cc = rcv->sb_cc;
262 sowwakeup(so2);
263 #undef snd
264 #undef rcv
265 break;
266
267 default:
268 panic("uipc_rcvd unknown socktype");
269 }
270 return 0;
271 }
272
273 /* pru_rcvoob is EOPNOTSUPP */
274
275 static int
276 uipc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
277 struct mbuf *control, struct proc *p)
278 {
279 int error = 0;
280 struct unpcb *unp = sotounpcb(so);
281 struct socket *so2;
282
283 if (unp == 0) {
284 error = EINVAL;
285 goto release;
286 }
287 if (flags & PRUS_OOB) {
288 error = EOPNOTSUPP;
289 goto release;
290 }
291
292 if (control && (error = unp_internalize(control, p)))
293 goto release;
294
295 switch (so->so_type) {
296 case SOCK_DGRAM:
297 {
298 struct sockaddr *from;
299
300 if (nam) {
301 if (unp->unp_conn) {
302 error = EISCONN;
303 break;
304 }
305 error = unp_connect(so, nam, p);
306 if (error)
307 break;
308 } else {
309 if (unp->unp_conn == 0) {
310 error = ENOTCONN;
311 break;
312 }
313 }
314 so2 = unp->unp_conn->unp_socket;
315 if (unp->unp_addr)
316 from = (struct sockaddr *)unp->unp_addr;
317 else
318 from = &sun_noname;
319 if (sbappendaddr(&so2->so_rcv, from, m, control)) {
320 sorwakeup(so2);
321 m = 0;
322 control = 0;
323 } else
324 error = ENOBUFS;
325 if (nam)
326 unp_disconnect(unp);
327 break;
328 }
329
330 case SOCK_STREAM:
331 #define rcv (&so2->so_rcv)
332 #define snd (&so->so_snd)
333 /* Connect if not connected yet. */
334 /*
335 * Note: A better implementation would complain
336 * if not equal to the peer's address.
337 */
338 if ((so->so_state & SS_ISCONNECTED) == 0) {
339 if (nam) {
340 error = unp_connect(so, nam, p);
341 if (error)
342 break; /* XXX */
343 } else {
344 error = ENOTCONN;
345 break;
346 }
347 }
348
349 if (so->so_state & SS_CANTSENDMORE) {
350 error = EPIPE;
351 break;
352 }
353 if (unp->unp_conn == 0)
354 panic("uipc_send connected but no connection?");
355 so2 = unp->unp_conn->unp_socket;
356 /*
357 * Send to paired receive port, and then reduce
358 * send buffer hiwater marks to maintain backpressure.
359 * Wake up readers.
360 */
361 if (control) {
362 if (sbappendcontrol(rcv, m, control))
363 control = 0;
364 } else
365 sbappend(rcv, m);
366 snd->sb_mbmax -=
367 rcv->sb_mbcnt - unp->unp_conn->unp_mbcnt;
368 unp->unp_conn->unp_mbcnt = rcv->sb_mbcnt;
369 snd->sb_hiwat -= rcv->sb_cc - unp->unp_conn->unp_cc;
370 unp->unp_conn->unp_cc = rcv->sb_cc;
371 sorwakeup(so2);
372 m = 0;
373 #undef snd
374 #undef rcv
375 break;
376
377 default:
378 panic("uipc_send unknown socktype");
379 }
380
381 /*
382 * SEND_EOF is equivalent to a SEND followed by
383 * a SHUTDOWN.
384 */
385 if (flags & PRUS_EOF) {
386 socantsendmore(so);
387 unp_shutdown(unp);
388 }
389
390 release:
391 if (control)
392 m_freem(control);
393 if (m)
394 m_freem(m);
395 return error;
396 }
397
398 static int
399 uipc_sense(struct socket *so, struct stat *sb)
400 {
401 struct unpcb *unp = sotounpcb(so);
402 struct socket *so2;
403
404 if (unp == 0)
405 return EINVAL;
406 sb->st_blksize = so->so_snd.sb_hiwat;
407 if (so->so_type == SOCK_STREAM && unp->unp_conn != 0) {
408 so2 = unp->unp_conn->unp_socket;
409 sb->st_blksize += so2->so_rcv.sb_cc;
410 }
411 sb->st_dev = NODEV;
412 if (unp->unp_ino == 0)
413 unp->unp_ino = unp_ino++;
414 sb->st_ino = unp->unp_ino;
415 return (0);
416 }
417
418 static int
419 uipc_shutdown(struct socket *so)
420 {
421 struct unpcb *unp = sotounpcb(so);
422
423 if (unp == 0)
424 return EINVAL;
425 socantsendmore(so);
426 unp_shutdown(unp);
427 return 0;
428 }
429
430 static int
431 uipc_sockaddr(struct socket *so, struct sockaddr **nam)
432 {
433 struct unpcb *unp = sotounpcb(so);
434
435 if (unp == 0)
436 return EINVAL;
437 if (unp->unp_addr)
438 *nam = dup_sockaddr((struct sockaddr *)unp->unp_addr, 1);
439 return 0;
440 }
441
442 struct pr_usrreqs uipc_usrreqs = {
443 uipc_abort, uipc_accept, uipc_attach, uipc_bind, uipc_connect,
444 uipc_connect2, pru_control_notsupp, uipc_detach, uipc_disconnect,
445 uipc_listen, uipc_peeraddr, uipc_rcvd, pru_rcvoob_notsupp,
446 uipc_send, uipc_sense, uipc_shutdown, uipc_sockaddr,
447 sosend, soreceive, sopoll
448 };
449
450 /*
451 * Both send and receive buffers are allocated PIPSIZ bytes of buffering
452 * for stream sockets, although the total for sender and receiver is
453 * actually only PIPSIZ.
454 * Datagram sockets really use the sendspace as the maximum datagram size,
455 * and don't really want to reserve the sendspace. Their recvspace should
456 * be large enough for at least one max-size datagram plus address.
457 */
458 #ifndef PIPSIZ
459 #define PIPSIZ 8192
460 #endif
461 static u_long unpst_sendspace = PIPSIZ;
462 static u_long unpst_recvspace = PIPSIZ;
463 static u_long unpdg_sendspace = 2*1024; /* really max datagram size */
464 static u_long unpdg_recvspace = 4*1024;
465
466 static int unp_rights; /* file descriptors in flight */
467
468 SYSCTL_DECL(_net_local_stream);
469 SYSCTL_INT(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW,
470 &unpst_sendspace, 0, "");
471 SYSCTL_INT(_net_local_stream, OID_AUTO, recvspace, CTLFLAG_RW,
472 &unpst_recvspace, 0, "");
473 SYSCTL_DECL(_net_local_dgram);
474 SYSCTL_INT(_net_local_dgram, OID_AUTO, maxdgram, CTLFLAG_RW,
475 &unpdg_sendspace, 0, "");
476 SYSCTL_INT(_net_local_dgram, OID_AUTO, recvspace, CTLFLAG_RW,
477 &unpdg_recvspace, 0, "");
478 SYSCTL_DECL(_net_local);
479 SYSCTL_INT(_net_local, OID_AUTO, inflight, CTLFLAG_RD, &unp_rights, 0, "");
480
481 static int
482 unp_attach(so)
483 struct socket *so;
484 {
485 register struct unpcb *unp;
486 int error;
487
488 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
489 switch (so->so_type) {
490
491 case SOCK_STREAM:
492 error = soreserve(so, unpst_sendspace, unpst_recvspace);
493 break;
494
495 case SOCK_DGRAM:
496 error = soreserve(so, unpdg_sendspace, unpdg_recvspace);
497 break;
498
499 default:
500 panic("unp_attach");
501 }
502 if (error)
503 return (error);
504 }
505 unp = (struct unpcb*)zalloc(unp_zone);
506 if (unp == NULL)
507 return (ENOBUFS);
508 bzero(unp, sizeof *unp);
509 unp->unp_gencnt = ++unp_gencnt;
510 unp_count++;
511 LIST_INIT(&unp->unp_refs);
512 unp->unp_socket = so;
513 LIST_INSERT_HEAD(so->so_type == SOCK_DGRAM ? &unp_dhead
514 : &unp_shead, unp, unp_link);
515 so->so_pcb = (caddr_t)unp;
516 return (0);
517 }
518
519 static void
520 unp_detach(unp)
521 register struct unpcb *unp;
522 {
523 LIST_REMOVE(unp, unp_link);
524 unp->unp_gencnt = ++unp_gencnt;
525 --unp_count;
526 if (unp->unp_vnode) {
527 struct vnode *tvp = unp->unp_vnode;
528 unp->unp_vnode->v_socket = 0;
529 unp->unp_vnode = 0;
530 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
531 vrele(tvp);
532 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
533 }
534 if (unp->unp_conn)
535 unp_disconnect(unp);
536 while (unp->unp_refs.lh_first)
537 unp_drop(unp->unp_refs.lh_first, ECONNRESET);
538 soisdisconnected(unp->unp_socket);
539 unp->unp_socket->so_pcb = 0;
540 if (unp_rights) {
541 /*
542 * Normally the receive buffer is flushed later,
543 * in sofree, but if our receive buffer holds references
544 * to descriptors that are now garbage, we will dispose
545 * of those descriptor references after the garbage collector
546 * gets them (resulting in a "panic: closef: count < 0").
547 */
548 sorflush(unp->unp_socket);
549 unp_gc();
550 }
551 if (unp->unp_addr)
552 FREE(unp->unp_addr, M_SONAME);
553 zfree(unp_zone, (vm_offset_t)unp);
554 }
555
556 static int
557 unp_bind(unp, nam, p)
558 struct unpcb *unp;
559 struct sockaddr *nam;
560 struct proc *p;
561 {
562 struct sockaddr_un *soun = (struct sockaddr_un *)nam;
563 register struct vnode *vp;
564 struct vattr vattr;
565 int error, namelen;
566 struct nameidata nd;
567 char buf[SOCK_MAXADDRLEN];
568
569 if (unp->unp_vnode != NULL)
570 return (EINVAL);
571 namelen = soun->sun_len - offsetof(struct sockaddr_un, sun_path);
572 if (namelen <= 0)
573 return EINVAL;
574 strncpy(buf, soun->sun_path, namelen);
575 buf[namelen] = 0; /* null-terminate the string */
576 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
577 NDINIT(&nd, CREATE, FOLLOW | LOCKPARENT, UIO_SYSSPACE,
578 buf, p);
579 /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */
580 error = namei(&nd);
581 if (error) {
582 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
583 return (error);
584 }
585 vp = nd.ni_vp;
586 if (vp != NULL) {
587 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
588 if (nd.ni_dvp == vp)
589 vrele(nd.ni_dvp);
590 else
591 vput(nd.ni_dvp);
592 vrele(vp);
593 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
594 return (EADDRINUSE);
595 }
596 VATTR_NULL(&vattr);
597 vattr.va_type = VSOCK;
598 vattr.va_mode = (ACCESSPERMS & ~p->p_fd->fd_cmask);
599 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
600 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
601 #if 0
602 /* In FreeBSD create leave s parent held ; not here */
603 vput(nd.ni_dvp);
604 #endif
605 if (error) {
606 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
607 return (error);
608 }
609 vp = nd.ni_vp;
610 vp->v_socket = unp->unp_socket;
611 unp->unp_vnode = vp;
612 unp->unp_addr = (struct sockaddr_un *)dup_sockaddr(nam, 1);
613 VOP_UNLOCK(vp, 0, p);
614 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
615 return (0);
616 }
617
618 static int
619 unp_connect(so, nam, p)
620 struct socket *so;
621 struct sockaddr *nam;
622 struct proc *p;
623 {
624 register struct sockaddr_un *soun = (struct sockaddr_un *)nam;
625 register struct vnode *vp;
626 register struct socket *so2, *so3;
627 struct unpcb *unp2, *unp3;
628 int error, len;
629 struct nameidata nd;
630 char buf[SOCK_MAXADDRLEN];
631
632 len = nam->sa_len - offsetof(struct sockaddr_un, sun_path);
633 if (len <= 0)
634 return EINVAL;
635 strncpy(buf, soun->sun_path, len);
636 buf[len] = 0;
637
638 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
639 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, buf, p);
640 error = namei(&nd);
641 if (error) {
642 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
643 return (error);
644 }
645 vp = nd.ni_vp;
646 if (vp->v_type != VSOCK) {
647 error = ENOTSOCK;
648 goto bad;
649 }
650 error = VOP_ACCESS(vp, VWRITE, p->p_ucred, p);
651 if (error)
652 goto bad;
653 so2 = vp->v_socket;
654 if (so2 == 0) {
655 error = ECONNREFUSED;
656 goto bad;
657 }
658 if (so->so_type != so2->so_type) {
659 error = EPROTOTYPE;
660 goto bad;
661 }
662 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
663
664 /*
665 * Check if socket was connected while we were trying to
666 * acquire the funnel.
667 * XXX - probably shouldn't return an error for SOCK_DGRAM
668 */
669 if ((so->so_state & SS_ISCONNECTED) != 0) {
670 error = EISCONN;
671 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
672 goto bad;
673 }
674
675 if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
676 if ((so2->so_options & SO_ACCEPTCONN) == 0 ||
677 (so3 = sonewconn(so2, 0)) == 0) {
678 error = ECONNREFUSED;
679 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
680 goto bad;
681 }
682 unp2 = sotounpcb(so2);
683 unp3 = sotounpcb(so3);
684 if (unp2->unp_addr)
685 unp3->unp_addr = (struct sockaddr_un *)
686 dup_sockaddr((struct sockaddr *)
687 unp2->unp_addr, 1);
688 so2 = so3;
689 }
690 error = unp_connect2(so, so2);
691 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
692 bad:
693 vput(vp);
694 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
695 return (error);
696 }
697
698 int
699 unp_connect2(so, so2)
700 register struct socket *so;
701 register struct socket *so2;
702 {
703 register struct unpcb *unp = sotounpcb(so);
704 register struct unpcb *unp2;
705
706 if (so2->so_type != so->so_type)
707 return (EPROTOTYPE);
708 unp2 = sotounpcb(so2);
709
710 /* Verify both sockets are still opened */
711 if (unp == 0 || unp2 == 0)
712 return (EINVAL);
713
714 unp->unp_conn = unp2;
715 switch (so->so_type) {
716
717 case SOCK_DGRAM:
718 LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink);
719 soisconnected(so);
720 break;
721
722 case SOCK_STREAM:
723 unp2->unp_conn = unp;
724 soisconnected(so);
725 soisconnected(so2);
726 break;
727
728 default:
729 panic("unp_connect2");
730 }
731 return (0);
732 }
733
734 static void
735 unp_disconnect(unp)
736 struct unpcb *unp;
737 {
738 register struct unpcb *unp2 = unp->unp_conn;
739
740 if (unp2 == 0)
741 return;
742 unp->unp_conn = 0;
743 switch (unp->unp_socket->so_type) {
744
745 case SOCK_DGRAM:
746 LIST_REMOVE(unp, unp_reflink);
747 unp->unp_socket->so_state &= ~SS_ISCONNECTED;
748 break;
749
750 case SOCK_STREAM:
751 soisdisconnected(unp->unp_socket);
752 unp2->unp_conn = 0;
753 soisdisconnected(unp2->unp_socket);
754 break;
755 }
756 }
757
758 #ifdef notdef
759 void
760 unp_abort(unp)
761 struct unpcb *unp;
762 {
763
764 unp_detach(unp);
765 }
766 #endif
767
768 static int
769 unp_pcblist SYSCTL_HANDLER_ARGS
770 {
771 int error, i, n;
772 struct unpcb *unp, **unp_list;
773 unp_gen_t gencnt;
774 struct xunpgen xug;
775 struct unp_head *head;
776
777 head = ((intptr_t)arg1 == SOCK_DGRAM ? &unp_dhead : &unp_shead);
778
779 /*
780 * The process of preparing the PCB list is too time-consuming and
781 * resource-intensive to repeat twice on every request.
782 */
783 if (req->oldptr == 0) {
784 n = unp_count;
785 req->oldidx = 2 * (sizeof xug)
786 + (n + n/8) * sizeof(struct xunpcb);
787 return 0;
788 }
789
790 if (req->newptr != 0)
791 return EPERM;
792
793 /*
794 * OK, now we're committed to doing something.
795 */
796 gencnt = unp_gencnt;
797 n = unp_count;
798
799 xug.xug_len = sizeof xug;
800 xug.xug_count = n;
801 xug.xug_gen = gencnt;
802 xug.xug_sogen = so_gencnt;
803 error = SYSCTL_OUT(req, &xug, sizeof xug);
804 if (error)
805 return error;
806
807 /*
808 * We are done if there is no pcb
809 */
810 if (n == 0)
811 return 0;
812
813 unp_list = _MALLOC(n * sizeof *unp_list, M_TEMP, M_WAITOK);
814 if (unp_list == 0)
815 return ENOMEM;
816
817 for (unp = head->lh_first, i = 0; unp && i < n;
818 unp = unp->unp_link.le_next) {
819 if (unp->unp_gencnt <= gencnt)
820 unp_list[i++] = unp;
821 }
822 n = i; /* in case we lost some during malloc */
823
824 error = 0;
825 for (i = 0; i < n; i++) {
826 unp = unp_list[i];
827 if (unp->unp_gencnt <= gencnt) {
828 struct xunpcb xu;
829 xu.xu_len = sizeof xu;
830 xu.xu_unpp = unp;
831 /*
832 * XXX - need more locking here to protect against
833 * connect/disconnect races for SMP.
834 */
835 if (unp->unp_addr)
836 bcopy(unp->unp_addr, &xu.xu_addr,
837 unp->unp_addr->sun_len);
838 if (unp->unp_conn && unp->unp_conn->unp_addr)
839 bcopy(unp->unp_conn->unp_addr,
840 &xu.xu_caddr,
841 unp->unp_conn->unp_addr->sun_len);
842 bcopy(unp, &xu.xu_unp, sizeof *unp);
843 sotoxsocket(unp->unp_socket, &xu.xu_socket);
844 error = SYSCTL_OUT(req, &xu, sizeof xu);
845 }
846 }
847 if (!error) {
848 /*
849 * Give the user an updated idea of our state.
850 * If the generation differs from what we told
851 * her before, she knows that something happened
852 * while we were processing this request, and it
853 * might be necessary to retry.
854 */
855 xug.xug_gen = unp_gencnt;
856 xug.xug_sogen = so_gencnt;
857 xug.xug_count = unp_count;
858 error = SYSCTL_OUT(req, &xug, sizeof xug);
859 }
860 FREE(unp_list, M_TEMP);
861 return error;
862 }
863
864 SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLFLAG_RD,
865 (caddr_t)(long)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb",
866 "List of active local datagram sockets");
867 SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, CTLFLAG_RD,
868 (caddr_t)(long)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb",
869 "List of active local stream sockets");
870
871 static void
872 unp_shutdown(unp)
873 struct unpcb *unp;
874 {
875 struct socket *so;
876
877 if (unp->unp_socket->so_type == SOCK_STREAM && unp->unp_conn &&
878 (so = unp->unp_conn->unp_socket))
879 socantrcvmore(so);
880 }
881
882 static void
883 unp_drop(unp, errno)
884 struct unpcb *unp;
885 int errno;
886 {
887 struct socket *so = unp->unp_socket;
888
889 so->so_error = errno;
890 unp_disconnect(unp);
891 if (so->so_head) {
892 LIST_REMOVE(unp, unp_link);
893 unp->unp_gencnt = ++unp_gencnt;
894 unp_count--;
895 so->so_pcb = (caddr_t) 0;
896 if (unp->unp_addr)
897 FREE(unp->unp_addr, M_SONAME);
898 zfree(unp_zone, (vm_offset_t)unp);
899 sofree(so);
900 }
901 }
902
903 #ifdef notdef
904 void
905 unp_drain()
906 {
907
908 }
909 #endif
910
911 int
912 unp_externalize(rights)
913 struct mbuf *rights;
914 {
915 struct proc *p = current_proc(); /* XXX */
916 register int i;
917 register struct cmsghdr *cm = mtod(rights, struct cmsghdr *);
918 register struct file **rp = (struct file **)(cm + 1);
919 register struct file *fp;
920 int newfds = (cm->cmsg_len - sizeof(*cm)) / sizeof (int);
921 int f;
922
923
924 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
925
926 /*
927 * if the new FD's will not fit, then we free them all
928 */
929 if (!fdavail(p, newfds)) {
930 for (i = 0; i < newfds; i++) {
931 fp = *rp;
932 unp_discard(fp);
933 *rp++ = 0;
934 }
935
936 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
937 return (EMSGSIZE);
938 }
939 /*
940 * now change each pointer to an fd in the global table to
941 * an integer that is the index to the local fd table entry
942 * that we set up to point to the global one we are transferring.
943 * XXX this assumes a pointer and int are the same size...!
944 */
945 for (i = 0; i < newfds; i++) {
946 if (fdalloc(p, 0, &f))
947 panic("unp_externalize");
948 fp = *rp;
949 p->p_fd->fd_ofiles[f] = fp;
950 *fdflags(p, f) &= ~UF_RESERVED;
951 fp->f_msgcount--;
952 unp_rights--;
953 *(int *)rp++ = f;
954 }
955
956 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
957 return (0);
958 }
959
960 void
961 unp_init(void)
962 {
963 unp_zone = zinit(sizeof(struct unpcb),
964 (nmbclusters * sizeof(struct unpcb)),
965 4096, "unpzone");
966 if (unp_zone == 0)
967 panic("unp_init");
968 LIST_INIT(&unp_dhead);
969 LIST_INIT(&unp_shead);
970 }
971
972 #ifndef MIN
973 #define MIN(a,b) (((a)<(b))?(a):(b))
974 #endif
975
976 static int
977 unp_internalize(control, p)
978 struct mbuf *control;
979 struct proc *p;
980 {
981 register struct cmsghdr *cm = mtod(control, struct cmsghdr *);
982 register struct file **rp;
983 struct file *fp;
984 register int i, error;
985 int oldfds;
986
987 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
988 if (cm->cmsg_type != SCM_RIGHTS || cm->cmsg_level != SOL_SOCKET ||
989 cm->cmsg_len != control->m_len) {
990 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
991 return (EINVAL);
992 }
993
994 oldfds = (cm->cmsg_len - sizeof (*cm)) / sizeof (int);
995 rp = (struct file **)(cm + 1);
996 for (i = 0; i < oldfds; i++)
997 if (error = fdgetf(p, *(int *)rp++, 0)) {
998
999 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
1000 return (error);
1001 }
1002
1003 rp = (struct file **)(cm + 1);
1004 for (i = 0; i < oldfds; i++) {
1005 (void) fdgetf(p, *(int *)rp, &fp);
1006 *rp++ = fp;
1007 fref(fp);
1008 fp->f_msgcount++;
1009 unp_rights++;
1010 }
1011
1012 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
1013 return (0);
1014 }
1015
1016 static int unp_defer, unp_gcing;
1017
1018 static void
1019 unp_gc()
1020 {
1021 register struct file *fp, *nextfp;
1022 register struct socket *so;
1023 struct file **extra_ref, **fpp;
1024 int nunref, i;
1025
1026 if (unp_gcing)
1027 return;
1028 unp_gcing = 1;
1029 unp_defer = 0;
1030 /*
1031 * before going through all this, set all FDs to
1032 * be NOT defered and NOT externally accessible
1033 */
1034 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
1035 for (fp = filehead.lh_first; fp != 0; fp = fp->f_list.le_next)
1036 fp->f_flag &= ~(FMARK|FDEFER);
1037 do {
1038 for (fp = filehead.lh_first; fp != 0; fp = fp->f_list.le_next) {
1039 /*
1040 * If the file is not open, skip it
1041 */
1042 if (fcount(fp) == 0)
1043 continue;
1044 /*
1045 * If we already marked it as 'defer' in a
1046 * previous pass, then try process it this time
1047 * and un-mark it
1048 */
1049 if (fp->f_flag & FDEFER) {
1050 fp->f_flag &= ~FDEFER;
1051 unp_defer--;
1052 } else {
1053 /*
1054 * if it's not defered, then check if it's
1055 * already marked.. if so skip it
1056 */
1057 if (fp->f_flag & FMARK)
1058 continue;
1059 /*
1060 * If all references are from messages
1061 * in transit, then skip it. it's not
1062 * externally accessible.
1063 */
1064 if (fcount(fp) == fp->f_msgcount)
1065 continue;
1066 /*
1067 * If it got this far then it must be
1068 * externally accessible.
1069 */
1070 fp->f_flag |= FMARK;
1071 }
1072 /*
1073 * either it was defered, or it is externally
1074 * accessible and not already marked so.
1075 * Now check if it is possibly one of OUR sockets.
1076 */
1077 if (fp->f_type != DTYPE_SOCKET ||
1078 (so = (struct socket *)fp->f_data) == 0)
1079 continue;
1080 if (so->so_proto->pr_domain != &localdomain ||
1081 (so->so_proto->pr_flags&PR_RIGHTS) == 0)
1082 continue;
1083 #ifdef notdef
1084 /* if this code is enabled need to run under network funnel */
1085 if (so->so_rcv.sb_flags & SB_LOCK) {
1086 /*
1087 * This is problematical; it's not clear
1088 * we need to wait for the sockbuf to be
1089 * unlocked (on a uniprocessor, at least),
1090 * and it's also not clear what to do
1091 * if sbwait returns an error due to receipt
1092 * of a signal. If sbwait does return
1093 * an error, we'll go into an infinite
1094 * loop. Delete all of this for now.
1095 */
1096 (void) sbwait(&so->so_rcv);
1097 goto restart;
1098 }
1099 #endif
1100 /*
1101 * So, Ok, it's one of our sockets and it IS externally
1102 * accessible (or was defered). Now we look
1103 * to see if we hold any file descriptors in its
1104 * message buffers. Follow those links and mark them
1105 * as accessible too.
1106 */
1107 unp_scan(so->so_rcv.sb_mb, unp_mark);
1108 }
1109 } while (unp_defer);
1110 /*
1111 * We grab an extra reference to each of the file table entries
1112 * that are not otherwise accessible and then free the rights
1113 * that are stored in messages on them.
1114 *
1115 * The bug in the orginal code is a little tricky, so I'll describe
1116 * what's wrong with it here.
1117 *
1118 * It is incorrect to simply unp_discard each entry for f_msgcount
1119 * times -- consider the case of sockets A and B that contain
1120 * references to each other. On a last close of some other socket,
1121 * we trigger a gc since the number of outstanding rights (unp_rights)
1122 * is non-zero. If during the sweep phase the gc code un_discards,
1123 * we end up doing a (full) closef on the descriptor. A closef on A
1124 * results in the following chain. Closef calls soo_close, which
1125 * calls soclose. Soclose calls first (through the switch
1126 * uipc_usrreq) unp_detach, which re-invokes unp_gc. Unp_gc simply
1127 * returns because the previous instance had set unp_gcing, and
1128 * we return all the way back to soclose, which marks the socket
1129 * with SS_NOFDREF, and then calls sofree. Sofree calls sorflush
1130 * to free up the rights that are queued in messages on the socket A,
1131 * i.e., the reference on B. The sorflush calls via the dom_dispose
1132 * switch unp_dispose, which unp_scans with unp_discard. This second
1133 * instance of unp_discard just calls closef on B.
1134 *
1135 * Well, a similar chain occurs on B, resulting in a sorflush on B,
1136 * which results in another closef on A. Unfortunately, A is already
1137 * being closed, and the descriptor has already been marked with
1138 * SS_NOFDREF, and soclose panics at this point.
1139 *
1140 * Here, we first take an extra reference to each inaccessible
1141 * descriptor. Then, we call sorflush ourself, since we know
1142 * it is a Unix domain socket anyhow. After we destroy all the
1143 * rights carried in messages, we do a last closef to get rid
1144 * of our extra reference. This is the last close, and the
1145 * unp_detach etc will shut down the socket.
1146 *
1147 * 91/09/19, bsy@cs.cmu.edu
1148 */
1149 extra_ref = _MALLOC(nfiles * sizeof(struct file *), M_FILE, M_WAITOK);
1150 for (nunref = 0, fp = filehead.lh_first, fpp = extra_ref; fp != 0;
1151 fp = nextfp) {
1152 nextfp = fp->f_list.le_next;
1153 /*
1154 * If it's not open, skip it
1155 */
1156 if (fcount(fp) == 0)
1157 continue;
1158 /*
1159 * If all refs are from msgs, and it's not marked accessible
1160 * then it must be referenced from some unreachable cycle
1161 * of (shut-down) FDs, so include it in our
1162 * list of FDs to remove
1163 */
1164 if (fcount(fp) == fp->f_msgcount && !(fp->f_flag & FMARK)) {
1165 *fpp++ = fp;
1166 nunref++;
1167 fref(fp);
1168 }
1169 }
1170 /*
1171 * for each FD on our hit list, do the following two things
1172 */
1173 for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) {
1174 struct file *tfp = *fpp;
1175 if (tfp->f_type == DTYPE_SOCKET && tfp->f_data != NULL) {
1176 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
1177 sorflush((struct socket *)(tfp->f_data));
1178 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
1179 }
1180 }
1181
1182
1183 for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp)
1184 closef(*fpp, (struct proc *) NULL);
1185 FREE((caddr_t)extra_ref, M_FILE);
1186 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
1187
1188 unp_gcing = 0;
1189 }
1190
1191 void
1192 unp_dispose(m)
1193 struct mbuf *m;
1194 {
1195
1196 if (m) {
1197 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
1198 unp_scan(m, unp_discard);
1199 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
1200 }
1201 }
1202
1203 /* should run under kernel funnel */
1204 static void
1205 unp_scan(m0, op)
1206 register struct mbuf *m0;
1207 void (*op) __P((struct file *));
1208 {
1209 register struct mbuf *m;
1210 register struct file **rp;
1211 register struct cmsghdr *cm;
1212 register int i;
1213 int qfds;
1214
1215 while (m0) {
1216 for (m = m0; m; m = m->m_next)
1217 if (m->m_type == MT_CONTROL &&
1218 m->m_len >= sizeof(*cm)) {
1219 cm = mtod(m, struct cmsghdr *);
1220 if (cm->cmsg_level != SOL_SOCKET ||
1221 cm->cmsg_type != SCM_RIGHTS)
1222 continue;
1223 qfds = (cm->cmsg_len - sizeof *cm)
1224 / sizeof (struct file *);
1225 rp = (struct file **)(cm + 1);
1226 for (i = 0; i < qfds; i++)
1227 (*op)(*rp++);
1228 break; /* XXX, but saves time */
1229 }
1230 m0 = m0->m_act;
1231 }
1232 }
1233
1234 /* should run under kernel funnel */
1235 static void
1236 unp_mark(fp)
1237 struct file *fp;
1238 {
1239
1240 if (fp->f_flag & FMARK)
1241 return;
1242 unp_defer++;
1243 fp->f_flag |= (FMARK|FDEFER);
1244 }
1245
1246 /* should run under kernel funnel */
1247 static void
1248 unp_discard(fp)
1249 struct file *fp;
1250 {
1251
1252 fp->f_msgcount--;
1253 unp_rights--;
1254 (void) closef(fp, (struct proc *)NULL);
1255 }