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