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