]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/uipc_usrreq.c
xnu-3789.60.24.tar.gz
[apple/xnu.git] / bsd / kern / uipc_usrreq.c
CommitLineData
1c79356b 1/*
490019cf 2 * Copyright (c) 2000-2015 Apple Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
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.
8f6c56a5 14 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
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 */
2d21ac55
A
62/*
63 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
64 * support for mandatory and extensible security protections. This notice
65 * is included in support of clause 2.2 (b) of the Apple Public License,
66 * Version 2.0.
67 */
1c79356b
A
68
69#include <sys/param.h>
70#include <sys/systm.h>
71#include <sys/kernel.h>
72#include <sys/domain.h>
73#include <sys/fcntl.h>
74#include <sys/malloc.h> /* XXX must be before <sys/file.h> */
91447636 75#include <sys/file_internal.h>
39236c6e 76#include <sys/guarded.h>
1c79356b
A
77#include <sys/filedesc.h>
78#include <sys/lock.h>
79#include <sys/mbuf.h>
80#include <sys/namei.h>
91447636
A
81#include <sys/proc_internal.h>
82#include <sys/kauth.h>
1c79356b
A
83#include <sys/protosw.h>
84#include <sys/socket.h>
85#include <sys/socketvar.h>
86#include <sys/stat.h>
87#include <sys/sysctl.h>
88#include <sys/un.h>
89#include <sys/unpcb.h>
91447636
A
90#include <sys/vnode_internal.h>
91#include <sys/kdebug.h>
3e170ce0 92#include <sys/mcache.h>
1c79356b
A
93
94#include <kern/zalloc.h>
91447636 95#include <kern/locks.h>
1c79356b 96
b0d623f7 97#if CONFIG_MACF
2d21ac55 98#include <security/mac_framework.h>
b0d623f7 99#endif /* CONFIG_MACF */
2d21ac55 100
316670eb
A
101#include <mach/vm_param.h>
102
3e170ce0
A
103/*
104 * Maximum number of FDs that can be passed in an mbuf
105 */
106#define UIPC_MAX_CMSG_FD 512
107
2d21ac55
A
108#define f_msgcount f_fglob->fg_msgcount
109#define f_cred f_fglob->fg_cred
110#define f_ops f_fglob->fg_ops
111#define f_offset f_fglob->fg_offset
112#define f_data f_fglob->fg_data
1c79356b
A
113struct zone *unp_zone;
114static unp_gen_t unp_gencnt;
115static u_int unp_count;
37839358 116
2d21ac55
A
117static lck_attr_t *unp_mtx_attr;
118static lck_grp_t *unp_mtx_grp;
119static lck_grp_attr_t *unp_mtx_grp_attr;
120static lck_rw_t *unp_list_mtx;
1c79356b 121
b0d623f7
A
122static lck_mtx_t *unp_disconnect_lock;
123static lck_mtx_t *unp_connect_lock;
124static u_int disconnect_in_progress;
125
2d21ac55 126extern lck_mtx_t *uipc_lock;
1c79356b
A
127static struct unp_head unp_shead, unp_dhead;
128
6d2010ae
A
129/*
130 * mDNSResponder tracing. When enabled, endpoints connected to
131 * /var/run/mDNSResponder will be traced; during each send on
132 * the traced socket, we log the PID and process name of the
133 * sending process. We also print out a bit of info related
134 * to the data itself; this assumes ipc_msg_hdr in dnssd_ipc.h
135 * of mDNSResponder stays the same.
136 */
137#define MDNSRESPONDER_PATH "/var/run/mDNSResponder"
138
139static int unpst_tracemdns; /* enable tracing */
140
141#define MDNS_IPC_MSG_HDR_VERSION_1 1
142
143struct mdns_ipc_msg_hdr {
144 uint32_t version;
145 uint32_t datalen;
146 uint32_t ipc_flags;
147 uint32_t op;
148 union {
149 void *context;
150 uint32_t u32[2];
151 } __attribute__((packed));
152 uint32_t reg_index;
153} __attribute__((packed));
154
1c79356b
A
155/*
156 * Unix communications domain.
157 *
158 * TODO:
159 * SEQPACKET, RDM
160 * rethink name space problems
161 * need a proper out-of-band
162 * lock pushdown
163 */
2d21ac55 164static struct sockaddr sun_noname = { sizeof (sun_noname), AF_LOCAL, { 0 } };
1c79356b
A
165static ino_t unp_ino; /* prototype for fake inode numbers */
166
2d21ac55
A
167static int unp_attach(struct socket *);
168static void unp_detach(struct unpcb *);
169static int unp_bind(struct unpcb *, struct sockaddr *, proc_t);
170static int unp_connect(struct socket *, struct sockaddr *, proc_t);
171static void unp_disconnect(struct unpcb *);
172static void unp_shutdown(struct unpcb *);
173static void unp_drop(struct unpcb *, int);
e2fac8b1 174__private_extern__ void unp_gc(void);
3e170ce0
A
175static void unp_scan(struct mbuf *, void (*)(struct fileglob *, void *arg), void *arg);
176static void unp_mark(struct fileglob *, __unused void *);
177static void unp_discard(struct fileglob *, void *);
2d21ac55
A
178static int unp_internalize(struct mbuf *, proc_t);
179static int unp_listen(struct unpcb *, proc_t);
b0d623f7
A
180static void unpcb_to_compat(struct unpcb *, struct unpcb_compat *);
181static void unp_get_locks_in_order(struct socket *so, struct socket *conn_so);
182
183static void
184unp_get_locks_in_order(struct socket *so, struct socket *conn_so)
185{
186 if (so < conn_so) {
187 socket_lock(conn_so, 1);
188 } else {
189 struct unpcb *unp = sotounpcb(so);
190 unp->unp_flags |= UNP_DONTDISCONNECT;
191 unp->rw_thrcount++;
192 socket_unlock(so, 0);
2d21ac55 193
b0d623f7
A
194 /* Get the locks in the correct order */
195 socket_lock(conn_so, 1);
196 socket_lock(so, 0);
197 unp->rw_thrcount--;
198 if (unp->rw_thrcount == 0) {
199 unp->unp_flags &= ~UNP_DONTDISCONNECT;
200 wakeup(unp);
201 }
202 }
203}
1c79356b
A
204
205static int
206uipc_abort(struct socket *so)
207{
208 struct unpcb *unp = sotounpcb(so);
209
210 if (unp == 0)
2d21ac55 211 return (EINVAL);
1c79356b 212 unp_drop(unp, ECONNABORTED);
91447636
A
213 unp_detach(unp);
214 sofree(so);
2d21ac55 215 return (0);
1c79356b
A
216}
217
218static int
219uipc_accept(struct socket *so, struct sockaddr **nam)
220{
221 struct unpcb *unp = sotounpcb(so);
222
223 if (unp == 0)
2d21ac55 224 return (EINVAL);
1c79356b
A
225
226 /*
227 * Pass back name of connected socket,
228 * if it was bound and we are still connected
229 * (our peer may have closed already!).
230 */
231 if (unp->unp_conn && unp->unp_conn->unp_addr) {
2d21ac55
A
232 *nam = dup_sockaddr((struct sockaddr *)
233 unp->unp_conn->unp_addr, 1);
1c79356b
A
234 } else {
235 *nam = dup_sockaddr((struct sockaddr *)&sun_noname, 1);
236 }
2d21ac55 237 return (0);
1c79356b
A
238}
239
2d21ac55
A
240/*
241 * Returns: 0 Success
242 * EISCONN
243 * unp_attach:
244 */
1c79356b 245static int
2d21ac55 246uipc_attach(struct socket *so, __unused int proto, __unused proc_t p)
1c79356b
A
247{
248 struct unpcb *unp = sotounpcb(so);
249
250 if (unp != 0)
2d21ac55
A
251 return (EISCONN);
252 return (unp_attach(so));
1c79356b
A
253}
254
255static int
2d21ac55 256uipc_bind(struct socket *so, struct sockaddr *nam, proc_t p)
1c79356b
A
257{
258 struct unpcb *unp = sotounpcb(so);
259
260 if (unp == 0)
2d21ac55 261 return (EINVAL);
1c79356b 262
2d21ac55 263 return (unp_bind(unp, nam, p));
1c79356b
A
264}
265
2d21ac55
A
266/*
267 * Returns: 0 Success
268 * EINVAL
269 * unp_connect:??? [See elsewhere in this file]
270 */
1c79356b 271static int
2d21ac55 272uipc_connect(struct socket *so, struct sockaddr *nam, proc_t p)
1c79356b
A
273{
274 struct unpcb *unp = sotounpcb(so);
275
276 if (unp == 0)
2d21ac55
A
277 return (EINVAL);
278 return (unp_connect(so, nam, p));
1c79356b
A
279}
280
2d21ac55
A
281/*
282 * Returns: 0 Success
283 * EINVAL
284 * unp_connect2:EPROTOTYPE Protocol wrong type for socket
285 * unp_connect2:EINVAL Invalid argument
286 */
1c79356b
A
287static int
288uipc_connect2(struct socket *so1, struct socket *so2)
289{
290 struct unpcb *unp = sotounpcb(so1);
291
292 if (unp == 0)
2d21ac55 293 return (EINVAL);
1c79356b 294
2d21ac55 295 return (unp_connect2(so1, so2));
1c79356b
A
296}
297
298/* control is EOPNOTSUPP */
299
300static int
301uipc_detach(struct socket *so)
302{
303 struct unpcb *unp = sotounpcb(so);
304
305 if (unp == 0)
2d21ac55 306 return (EINVAL);
1c79356b 307
6d2010ae 308 lck_mtx_assert(&unp->unp_mtx, LCK_MTX_ASSERT_OWNED);
1c79356b 309 unp_detach(unp);
2d21ac55 310 return (0);
1c79356b
A
311}
312
313static int
314uipc_disconnect(struct socket *so)
315{
316 struct unpcb *unp = sotounpcb(so);
317
318 if (unp == 0)
2d21ac55 319 return (EINVAL);
1c79356b 320 unp_disconnect(unp);
2d21ac55 321 return (0);
1c79356b
A
322}
323
2d21ac55
A
324/*
325 * Returns: 0 Success
326 * EINVAL
327 */
1c79356b 328static int
2d21ac55 329uipc_listen(struct socket *so, __unused proc_t p)
1c79356b
A
330{
331 struct unpcb *unp = sotounpcb(so);
332
333 if (unp == 0 || unp->unp_vnode == 0)
2d21ac55
A
334 return (EINVAL);
335 return (unp_listen(unp, p));
1c79356b
A
336}
337
338static int
339uipc_peeraddr(struct socket *so, struct sockaddr **nam)
340{
341 struct unpcb *unp = sotounpcb(so);
342
2d21ac55
A
343 if (unp == NULL)
344 return (EINVAL);
345 if (unp->unp_conn != NULL && unp->unp_conn->unp_addr != NULL) {
346 *nam = dup_sockaddr((struct sockaddr *)
347 unp->unp_conn->unp_addr, 1);
348 } else {
349 *nam = dup_sockaddr((struct sockaddr *)&sun_noname, 1);
350 }
351 return (0);
1c79356b
A
352}
353
354static int
91447636 355uipc_rcvd(struct socket *so, __unused int flags)
1c79356b
A
356{
357 struct unpcb *unp = sotounpcb(so);
358 struct socket *so2;
359
360 if (unp == 0)
2d21ac55 361 return (EINVAL);
1c79356b
A
362 switch (so->so_type) {
363 case SOCK_DGRAM:
364 panic("uipc_rcvd DGRAM?");
365 /*NOTREACHED*/
366
367 case SOCK_STREAM:
368#define rcv (&so->so_rcv)
2d21ac55 369#define snd (&so2->so_snd)
1c79356b
A
370 if (unp->unp_conn == 0)
371 break;
b0d623f7 372
1c79356b 373 so2 = unp->unp_conn->unp_socket;
b0d623f7 374 unp_get_locks_in_order(so, so2);
1c79356b
A
375 /*
376 * Adjust backpressure on sender
377 * and wakeup any waiting to write.
378 */
379 snd->sb_mbmax += unp->unp_mbcnt - rcv->sb_mbcnt;
380 unp->unp_mbcnt = rcv->sb_mbcnt;
381 snd->sb_hiwat += unp->unp_cc - rcv->sb_cc;
382 unp->unp_cc = rcv->sb_cc;
383 sowwakeup(so2);
b0d623f7
A
384
385 socket_unlock(so2, 1);
386
1c79356b
A
387#undef snd
388#undef rcv
389 break;
390
391 default:
392 panic("uipc_rcvd unknown socktype");
393 }
2d21ac55 394 return (0);
1c79356b
A
395}
396
397/* pru_rcvoob is EOPNOTSUPP */
398
2d21ac55
A
399/*
400 * Returns: 0 Success
401 * EINVAL
402 * EOPNOTSUPP
403 * EPIPE
404 * ENOTCONN
405 * EISCONN
406 * unp_internalize:EINVAL
407 * unp_internalize:EBADF
408 * unp_connect:EAFNOSUPPORT Address family not supported
409 * unp_connect:EINVAL Invalid argument
410 * unp_connect:ENOTSOCK Not a socket
411 * unp_connect:ECONNREFUSED Connection refused
412 * unp_connect:EISCONN Socket is connected
413 * unp_connect:EPROTOTYPE Protocol wrong type for socket
414 * unp_connect:???
415 * sbappendaddr:ENOBUFS [5th argument, contents modified]
416 * sbappendaddr:??? [whatever a filter author chooses]
417 */
1c79356b
A
418static int
419uipc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
2d21ac55 420 struct mbuf *control, proc_t p)
1c79356b
A
421{
422 int error = 0;
423 struct unpcb *unp = sotounpcb(so);
424 struct socket *so2;
425
426 if (unp == 0) {
427 error = EINVAL;
428 goto release;
429 }
430 if (flags & PRUS_OOB) {
431 error = EOPNOTSUPP;
432 goto release;
433 }
434
13fec989 435 if (control) {
b0d623f7 436 /* release lock to avoid deadlock (4436174) */
2d21ac55 437 socket_unlock(so, 0);
13fec989
A
438 error = unp_internalize(control, p);
439 socket_lock(so, 0);
440 if (error)
441 goto release;
442 }
1c79356b
A
443
444 switch (so->so_type) {
2d21ac55 445 case SOCK_DGRAM:
1c79356b
A
446 {
447 struct sockaddr *from;
448
449 if (nam) {
450 if (unp->unp_conn) {
451 error = EISCONN;
452 break;
453 }
454 error = unp_connect(so, nam, p);
455 if (error)
456 break;
457 } else {
458 if (unp->unp_conn == 0) {
459 error = ENOTCONN;
460 break;
461 }
462 }
b0d623f7 463
1c79356b 464 so2 = unp->unp_conn->unp_socket;
6d2010ae
A
465 if (so != so2)
466 unp_get_locks_in_order(so, so2);
b0d623f7 467
1c79356b
A
468 if (unp->unp_addr)
469 from = (struct sockaddr *)unp->unp_addr;
470 else
471 from = &sun_noname;
2d21ac55
A
472 /*
473 * sbappendaddr() will fail when the receiver runs out of
474 * space; in contrast to SOCK_STREAM, we will lose messages
475 * for the SOCK_DGRAM case when the receiver's queue overflows.
476 * SB_UNIX on the socket buffer implies that the callee will
477 * not free the control message, if any, because we would need
478 * to call unp_dispose() on it.
479 */
91447636 480 if (sbappendaddr(&so2->so_rcv, from, m, control, &error)) {
2d21ac55 481 control = NULL;
1c79356b 482 sorwakeup(so2);
2d21ac55
A
483 } else if (control != NULL && error == 0) {
484 /* A socket filter took control; don't touch it */
485 control = NULL;
91447636 486 }
b0d623f7 487
6d2010ae
A
488 if (so != so2)
489 socket_unlock(so2, 1);
b0d623f7 490
2d21ac55 491 m = NULL;
1c79356b
A
492 if (nam)
493 unp_disconnect(unp);
494 break;
495 }
496
91447636
A
497 case SOCK_STREAM: {
498 int didreceive = 0;
1c79356b
A
499#define rcv (&so2->so_rcv)
500#define snd (&so->so_snd)
501 /* Connect if not connected yet. */
502 /*
503 * Note: A better implementation would complain
504 * if not equal to the peer's address.
505 */
506 if ((so->so_state & SS_ISCONNECTED) == 0) {
507 if (nam) {
508 error = unp_connect(so, nam, p);
509 if (error)
510 break; /* XXX */
511 } else {
512 error = ENOTCONN;
513 break;
514 }
515 }
516
517 if (so->so_state & SS_CANTSENDMORE) {
518 error = EPIPE;
519 break;
520 }
521 if (unp->unp_conn == 0)
522 panic("uipc_send connected but no connection?");
b0d623f7 523
1c79356b 524 so2 = unp->unp_conn->unp_socket;
b0d623f7
A
525 unp_get_locks_in_order(so, so2);
526
527 /* Check socket state again as we might have unlocked the socket
528 * while trying to get the locks in order
529 */
530
531 if ((so->so_state & SS_CANTSENDMORE)) {
532 error = EPIPE;
533 socket_unlock(so2, 1);
534 break;
535 }
536
6d2010ae
A
537 if (unp->unp_flags & UNP_TRACE_MDNS) {
538 struct mdns_ipc_msg_hdr hdr;
539
540 if (mbuf_copydata(m, 0, sizeof (hdr), &hdr) == 0 &&
541 hdr.version == ntohl(MDNS_IPC_MSG_HDR_VERSION_1)) {
542 printf("%s[mDNSResponder] pid=%d (%s): op=0x%x\n",
543 __func__, p->p_pid, p->p_comm, ntohl(hdr.op));
544 }
545 }
546
1c79356b 547 /*
2d21ac55
A
548 * Send to paired receive port, and then reduce send buffer
549 * hiwater marks to maintain backpressure. Wake up readers.
550 * SB_UNIX flag will allow new record to be appended to the
551 * receiver's queue even when it is already full. It is
552 * possible, however, that append might fail. In that case,
553 * we will need to call unp_dispose() on the control message;
554 * the callee will not free it since SB_UNIX is set.
1c79356b 555 */
2d21ac55
A
556 didreceive = control ?
557 sbappendcontrol(rcv, m, control, &error) : sbappend(rcv, m);
558
559 snd->sb_mbmax -= rcv->sb_mbcnt - unp->unp_conn->unp_mbcnt;
1c79356b 560 unp->unp_conn->unp_mbcnt = rcv->sb_mbcnt;
fe8ab488
A
561 if ((int32_t)snd->sb_hiwat >=
562 (int32_t)(rcv->sb_cc - unp->unp_conn->unp_cc)) {
563 snd->sb_hiwat -= rcv->sb_cc - unp->unp_conn->unp_cc;
564 } else {
565 snd->sb_hiwat = 0;
566 }
1c79356b 567 unp->unp_conn->unp_cc = rcv->sb_cc;
2d21ac55
A
568 if (didreceive) {
569 control = NULL;
91447636 570 sorwakeup(so2);
2d21ac55
A
571 } else if (control != NULL && error == 0) {
572 /* A socket filter took control; don't touch it */
573 control = NULL;
574 }
b0d623f7
A
575
576 socket_unlock(so2, 1);
2d21ac55 577 m = NULL;
1c79356b
A
578#undef snd
579#undef rcv
91447636 580 }
1c79356b
A
581 break;
582
583 default:
584 panic("uipc_send unknown socktype");
585 }
586
587 /*
588 * SEND_EOF is equivalent to a SEND followed by
589 * a SHUTDOWN.
590 */
591 if (flags & PRUS_EOF) {
592 socantsendmore(so);
593 unp_shutdown(unp);
594 }
595
2d21ac55
A
596 if (control && error != 0) {
597 socket_unlock(so, 0);
91447636 598 unp_dispose(control);
2d21ac55
A
599 socket_lock(so, 0);
600 }
91447636 601
1c79356b
A
602release:
603 if (control)
604 m_freem(control);
605 if (m)
606 m_freem(m);
2d21ac55 607 return (error);
1c79356b
A
608}
609
610static int
2d21ac55 611uipc_sense(struct socket *so, void *ub, int isstat64)
1c79356b
A
612{
613 struct unpcb *unp = sotounpcb(so);
614 struct socket *so2;
2d21ac55 615 blksize_t blksize;
1c79356b
A
616
617 if (unp == 0)
2d21ac55
A
618 return (EINVAL);
619
620 blksize = so->so_snd.sb_hiwat;
1c79356b
A
621 if (so->so_type == SOCK_STREAM && unp->unp_conn != 0) {
622 so2 = unp->unp_conn->unp_socket;
2d21ac55 623 blksize += so2->so_rcv.sb_cc;
1c79356b 624 }
1c79356b
A
625 if (unp->unp_ino == 0)
626 unp->unp_ino = unp_ino++;
2d21ac55
A
627
628 if (isstat64 != 0) {
629 struct stat64 *sb64;
630
631 sb64 = (struct stat64 *)ub;
632 sb64->st_blksize = blksize;
633 sb64->st_dev = NODEV;
634 sb64->st_ino = (ino64_t)unp->unp_ino;
635 } else {
636 struct stat *sb;
637
638 sb = (struct stat *)ub;
639 sb->st_blksize = blksize;
640 sb->st_dev = NODEV;
b0d623f7 641 sb->st_ino = (ino_t)(uintptr_t)unp->unp_ino;
2d21ac55
A
642 }
643
1c79356b
A
644 return (0);
645}
646
2d21ac55
A
647/*
648 * Returns: 0 Success
649 * EINVAL
650 *
651 * Notes: This is not strictly correct, as unp_shutdown() also calls
652 * socantrcvmore(). These should maybe both be conditionalized
653 * on the 'how' argument in soshutdown() as called from the
654 * shutdown() system call.
655 */
1c79356b
A
656static int
657uipc_shutdown(struct socket *so)
658{
659 struct unpcb *unp = sotounpcb(so);
660
661 if (unp == 0)
2d21ac55 662 return (EINVAL);
1c79356b
A
663 socantsendmore(so);
664 unp_shutdown(unp);
2d21ac55 665 return (0);
1c79356b
A
666}
667
2d21ac55
A
668/*
669 * Returns: 0 Success
670 * EINVAL Invalid argument
671 */
1c79356b
A
672static int
673uipc_sockaddr(struct socket *so, struct sockaddr **nam)
674{
675 struct unpcb *unp = sotounpcb(so);
676
2d21ac55
A
677 if (unp == NULL)
678 return (EINVAL);
679 if (unp->unp_addr != NULL) {
1c79356b 680 *nam = dup_sockaddr((struct sockaddr *)unp->unp_addr, 1);
2d21ac55
A
681 } else {
682 *nam = dup_sockaddr((struct sockaddr *)&sun_noname, 1);
683 }
684 return (0);
1c79356b
A
685}
686
687struct pr_usrreqs uipc_usrreqs = {
39236c6e
A
688 .pru_abort = uipc_abort,
689 .pru_accept = uipc_accept,
690 .pru_attach = uipc_attach,
691 .pru_bind = uipc_bind,
692 .pru_connect = uipc_connect,
693 .pru_connect2 = uipc_connect2,
694 .pru_detach = uipc_detach,
695 .pru_disconnect = uipc_disconnect,
696 .pru_listen = uipc_listen,
697 .pru_peeraddr = uipc_peeraddr,
698 .pru_rcvd = uipc_rcvd,
699 .pru_send = uipc_send,
700 .pru_sense = uipc_sense,
701 .pru_shutdown = uipc_shutdown,
702 .pru_sockaddr = uipc_sockaddr,
703 .pru_sosend = sosend,
704 .pru_soreceive = soreceive,
1c79356b 705};
91447636
A
706
707int
2d21ac55 708uipc_ctloutput(struct socket *so, struct sockopt *sopt)
91447636
A
709{
710 struct unpcb *unp = sotounpcb(so);
39236c6e
A
711 int error = 0;
712 pid_t peerpid;
713 struct socket *peerso;
91447636
A
714
715 switch (sopt->sopt_dir) {
716 case SOPT_GET:
717 switch (sopt->sopt_name) {
718 case LOCAL_PEERCRED:
2d21ac55 719 if (unp->unp_flags & UNP_HAVEPC) {
91447636 720 error = sooptcopyout(sopt, &unp->unp_peercred,
2d21ac55
A
721 sizeof (unp->unp_peercred));
722 } else {
91447636
A
723 if (so->so_type == SOCK_STREAM)
724 error = ENOTCONN;
725 else
726 error = EINVAL;
727 }
728 break;
316670eb 729 case LOCAL_PEERPID:
39236c6e
A
730 case LOCAL_PEEREPID:
731 if (unp->unp_conn == NULL) {
316670eb 732 error = ENOTCONN;
39236c6e 733 break;
316670eb 734 }
39236c6e
A
735 peerso = unp->unp_conn->unp_socket;
736 if (peerso == NULL)
737 panic("peer is connected but has no socket?");
738 unp_get_locks_in_order(so, peerso);
739 if (sopt->sopt_name == LOCAL_PEEREPID &&
740 peerso->so_flags & SOF_DELEGATED)
741 peerpid = peerso->e_pid;
742 else
743 peerpid = peerso->last_pid;
744 socket_unlock(peerso, 1);
745 error = sooptcopyout(sopt, &peerpid, sizeof (peerpid));
746 break;
747 case LOCAL_PEERUUID:
748 case LOCAL_PEEREUUID:
749 if (unp->unp_conn == NULL) {
750 error = ENOTCONN;
751 break;
752 }
753 peerso = unp->unp_conn->unp_socket;
754 if (peerso == NULL)
755 panic("peer is connected but has no socket?");
756 unp_get_locks_in_order(so, peerso);
757 if (sopt->sopt_name == LOCAL_PEEREUUID &&
758 peerso->so_flags & SOF_DELEGATED)
759 error = sooptcopyout(sopt, &peerso->e_uuid,
760 sizeof (peerso->e_uuid));
761 else
762 error = sooptcopyout(sopt, &peerso->last_uuid,
763 sizeof (peerso->last_uuid));
764 socket_unlock(peerso, 1);
316670eb 765 break;
91447636
A
766 default:
767 error = EOPNOTSUPP;
768 break;
769 }
770 break;
771 case SOPT_SET:
772 default:
773 error = EOPNOTSUPP;
774 break;
775 }
39236c6e 776
91447636
A
777 return (error);
778}
2d21ac55 779
1c79356b
A
780/*
781 * Both send and receive buffers are allocated PIPSIZ bytes of buffering
782 * for stream sockets, although the total for sender and receiver is
783 * actually only PIPSIZ.
784 * Datagram sockets really use the sendspace as the maximum datagram size,
785 * and don't really want to reserve the sendspace. Their recvspace should
786 * be large enough for at least one max-size datagram plus address.
787 */
788#ifndef PIPSIZ
789#define PIPSIZ 8192
790#endif
b0d623f7
A
791static u_int32_t unpst_sendspace = PIPSIZ;
792static u_int32_t unpst_recvspace = PIPSIZ;
793static u_int32_t unpdg_sendspace = 2*1024; /* really max datagram size */
794static u_int32_t unpdg_recvspace = 4*1024;
1c79356b
A
795
796static int unp_rights; /* file descriptors in flight */
2d21ac55 797static int unp_disposed; /* discarded file descriptors */
1c79356b
A
798
799SYSCTL_DECL(_net_local_stream);
6d2010ae 800SYSCTL_INT(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW | CTLFLAG_LOCKED,
2d21ac55 801 &unpst_sendspace, 0, "");
6d2010ae 802SYSCTL_INT(_net_local_stream, OID_AUTO, recvspace, CTLFLAG_RW | CTLFLAG_LOCKED,
2d21ac55 803 &unpst_recvspace, 0, "");
6d2010ae
A
804SYSCTL_INT(_net_local_stream, OID_AUTO, tracemdns, CTLFLAG_RW | CTLFLAG_LOCKED,
805 &unpst_tracemdns, 0, "");
1c79356b 806SYSCTL_DECL(_net_local_dgram);
6d2010ae 807SYSCTL_INT(_net_local_dgram, OID_AUTO, maxdgram, CTLFLAG_RW | CTLFLAG_LOCKED,
2d21ac55 808 &unpdg_sendspace, 0, "");
6d2010ae 809SYSCTL_INT(_net_local_dgram, OID_AUTO, recvspace, CTLFLAG_RW | CTLFLAG_LOCKED,
2d21ac55 810 &unpdg_recvspace, 0, "");
1c79356b 811SYSCTL_DECL(_net_local);
6d2010ae 812SYSCTL_INT(_net_local, OID_AUTO, inflight, CTLFLAG_RD | CTLFLAG_LOCKED, &unp_rights, 0, "");
1c79356b 813
2d21ac55
A
814/*
815 * Returns: 0 Success
816 * ENOBUFS
817 * soreserve:ENOBUFS
818 */
1c79356b 819static int
91447636 820unp_attach(struct socket *so)
1c79356b 821{
91447636
A
822 struct unpcb *unp;
823 int error = 0;
1c79356b
A
824
825 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
826 switch (so->so_type) {
827
828 case SOCK_STREAM:
829 error = soreserve(so, unpst_sendspace, unpst_recvspace);
830 break;
831
832 case SOCK_DGRAM:
833 error = soreserve(so, unpdg_sendspace, unpdg_recvspace);
834 break;
835
836 default:
837 panic("unp_attach");
838 }
839 if (error)
840 return (error);
841 }
2d21ac55 842 unp = (struct unpcb *)zalloc(unp_zone);
1c79356b
A
843 if (unp == NULL)
844 return (ENOBUFS);
2d21ac55 845 bzero(unp, sizeof (*unp));
b0d623f7 846
6d2010ae
A
847 lck_mtx_init(&unp->unp_mtx,
848 unp_mtx_grp, unp_mtx_attr);
b0d623f7 849
37839358 850 lck_rw_lock_exclusive(unp_list_mtx);
1c79356b
A
851 LIST_INIT(&unp->unp_refs);
852 unp->unp_socket = so;
91447636
A
853 unp->unp_gencnt = ++unp_gencnt;
854 unp_count++;
2d21ac55
A
855 LIST_INSERT_HEAD(so->so_type == SOCK_DGRAM ?
856 &unp_dhead : &unp_shead, unp, unp_link);
b0d623f7 857 lck_rw_done(unp_list_mtx);
1c79356b 858 so->so_pcb = (caddr_t)unp;
2d21ac55
A
859 /*
860 * Mark AF_UNIX socket buffers accordingly so that:
861 *
862 * a. In the SOCK_STREAM case, socket buffer append won't fail due to
863 * the lack of space; this essentially loosens the sbspace() check,
864 * since there is disconnect between sosend() and uipc_send() with
865 * respect to flow control that might result in our dropping the
866 * data in uipc_send(). By setting this, we allow for slightly
867 * more records to be appended to the receiving socket to avoid
868 * losing data (which we can't afford in the SOCK_STREAM case).
869 * Flow control still takes place since we adjust the sender's
870 * hiwat during each send. This doesn't affect the SOCK_DGRAM
871 * case and append would still fail when the queue overflows.
872 *
873 * b. In the presence of control messages containing internalized
874 * file descriptors, the append routines will not free them since
875 * we'd need to undo the work first via unp_dispose().
876 */
877 so->so_rcv.sb_flags |= SB_UNIX;
878 so->so_snd.sb_flags |= SB_UNIX;
1c79356b
A
879 return (0);
880}
881
882static void
91447636 883unp_detach(struct unpcb *unp)
1c79356b 884{
b7266188
A
885 int so_locked = 1;
886
37839358 887 lck_rw_lock_exclusive(unp_list_mtx);
1c79356b 888 LIST_REMOVE(unp, unp_link);
316670eb
A
889 --unp_count;
890 ++unp_gencnt;
37839358 891 lck_rw_done(unp_list_mtx);
1c79356b 892 if (unp->unp_vnode) {
b0d623f7
A
893 struct vnode *tvp = NULL;
894 socket_unlock(unp->unp_socket, 0);
895
896 /* Holding unp_connect_lock will avoid a race between
897 * a thread closing the listening socket and a thread
898 * connecting to it.
899 */
900 lck_mtx_lock(unp_connect_lock);
901 socket_lock(unp->unp_socket, 0);
902 if (unp->unp_vnode) {
903 tvp = unp->unp_vnode;
904 unp->unp_vnode->v_socket = NULL;
905 unp->unp_vnode = NULL;
906 }
907 lck_mtx_unlock(unp_connect_lock);
908 if (tvp != NULL)
909 vnode_rele(tvp); /* drop the usecount */
1c79356b
A
910 }
911 if (unp->unp_conn)
912 unp_disconnect(unp);
b0d623f7 913 while (unp->unp_refs.lh_first) {
b7266188
A
914 struct unpcb *unp2 = NULL;
915
916 /* This datagram socket is connected to one or more
917 * sockets. In order to avoid a race condition between removing
918 * this reference and closing the connected socket, we need
919 * to check disconnect_in_progress
920 */
921 if (so_locked == 1) {
922 socket_unlock(unp->unp_socket, 0);
923 so_locked = 0;
924 }
925 lck_mtx_lock(unp_disconnect_lock);
926 while (disconnect_in_progress != 0) {
927 (void)msleep((caddr_t)&disconnect_in_progress, unp_disconnect_lock,
928 PSOCK, "disconnect", NULL);
929 }
930 disconnect_in_progress = 1;
931 lck_mtx_unlock(unp_disconnect_lock);
932
933 /* Now we are sure that any unpcb socket disconnect is not happening */
934 if (unp->unp_refs.lh_first != NULL) {
935 unp2 = unp->unp_refs.lh_first;
936 socket_lock(unp2->unp_socket, 1);
937 }
938
939 lck_mtx_lock(unp_disconnect_lock);
940 disconnect_in_progress = 0;
941 wakeup(&disconnect_in_progress);
942 lck_mtx_unlock(unp_disconnect_lock);
943
944 if (unp2 != NULL) {
945 /* We already locked this socket and have a reference on it */
946 unp_drop(unp2, ECONNRESET);
947 socket_unlock(unp2->unp_socket, 1);
948 }
949 }
950
951 if (so_locked == 0) {
b0d623f7 952 socket_lock(unp->unp_socket, 0);
b7266188 953 so_locked = 1;
b0d623f7 954 }
1c79356b 955 soisdisconnected(unp->unp_socket);
2d21ac55
A
956 /* makes sure we're getting dealloced */
957 unp->unp_socket->so_flags |= SOF_PCBCLEARING;
1c79356b
A
958}
959
2d21ac55
A
960/*
961 * Returns: 0 Success
962 * EAFNOSUPPORT
963 * EINVAL
964 * EADDRINUSE
965 * namei:??? [anything namei can return]
966 * vnode_authorize:??? [anything vnode_authorize can return]
967 *
968 * Notes: p at this point is the current process, as this function is
969 * only called by sobind().
970 */
1c79356b 971static int
91447636
A
972unp_bind(
973 struct unpcb *unp,
974 struct sockaddr *nam,
2d21ac55 975 proc_t p)
1c79356b
A
976{
977 struct sockaddr_un *soun = (struct sockaddr_un *)nam;
91447636
A
978 struct vnode *vp, *dvp;
979 struct vnode_attr va;
2d21ac55 980 vfs_context_t ctx = vfs_context_current();
1c79356b
A
981 int error, namelen;
982 struct nameidata nd;
b0d623f7 983 struct socket *so = unp->unp_socket;
1c79356b
A
984 char buf[SOCK_MAXADDRLEN];
985
2d21ac55
A
986 if (nam->sa_family != 0 && nam->sa_family != AF_UNIX) {
987 return (EAFNOSUPPORT);
988 }
91447636 989
2d21ac55 990 if (unp->unp_vnode != NULL)
1c79356b 991 return (EINVAL);
1c79356b 992 namelen = soun->sun_len - offsetof(struct sockaddr_un, sun_path);
2d21ac55
A
993 if (namelen <= 0)
994 return (EINVAL);
490019cf
A
995 /*
996 * Note: sun_path is not a zero terminated "C" string
997 */
998 ASSERT(namelen < SOCK_MAXADDRLEN);
999 bcopy(soun->sun_path, buf, namelen);
1000 buf[namelen] = 0;
1001
b0d623f7
A
1002 socket_unlock(so, 0);
1003
6d2010ae 1004 NDINIT(&nd, CREATE, OP_MKFIFO, FOLLOW | LOCKPARENT, UIO_SYSSPACE,
2d21ac55
A
1005 CAST_USER_ADDR_T(buf), ctx);
1006 /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */
1c79356b
A
1007 error = namei(&nd);
1008 if (error) {
b0d623f7 1009 socket_lock(so, 0);
1c79356b
A
1010 return (error);
1011 }
91447636 1012 dvp = nd.ni_dvp;
1c79356b 1013 vp = nd.ni_vp;
91447636 1014
1c79356b 1015 if (vp != NULL) {
2d21ac55 1016 /*
91447636
A
1017 * need to do this before the vnode_put of dvp
1018 * since we may have to release an fs_nodelock
1019 */
1020 nameidone(&nd);
1021
1022 vnode_put(dvp);
1023 vnode_put(vp);
1024
b0d623f7 1025 socket_lock(so, 0);
1c79356b
A
1026 return (EADDRINUSE);
1027 }
91447636 1028
2d21ac55
A
1029 VATTR_INIT(&va);
1030 VATTR_SET(&va, va_type, VSOCK);
1031 VATTR_SET(&va, va_mode, (ACCESSPERMS & ~p->p_fd->fd_cmask));
1032
b0d623f7 1033#if CONFIG_MACF
2d21ac55
A
1034 error = mac_vnode_check_create(ctx,
1035 nd.ni_dvp, &nd.ni_cnd, &va);
1036
1037 if (error == 0)
b0d623f7
A
1038#endif /* CONFIG_MACF */
1039#if CONFIG_MACF_SOCKET_SUBSET
1040 error = mac_vnode_check_uipc_bind(ctx,
1041 nd.ni_dvp, &nd.ni_cnd, &va);
1042
1043 if (error == 0)
1044#endif /* MAC_SOCKET_SUBSET */
91447636 1045 /* authorize before creating */
2d21ac55 1046 error = vnode_authorize(dvp, NULL, KAUTH_VNODE_ADD_FILE, ctx);
91447636
A
1047
1048 if (!error) {
91447636 1049 /* create the socket */
6d2010ae 1050 error = vn_create(dvp, &vp, &nd, &va, 0, 0, NULL, ctx);
91447636 1051 }
2d21ac55 1052
91447636
A
1053 nameidone(&nd);
1054 vnode_put(dvp);
1055
1c79356b 1056 if (error) {
b0d623f7 1057 socket_lock(so, 0);
1c79356b
A
1058 return (error);
1059 }
91447636 1060 vnode_ref(vp); /* gain a longterm reference */
b0d623f7 1061 socket_lock(so, 0);
1c79356b
A
1062 vp->v_socket = unp->unp_socket;
1063 unp->unp_vnode = vp;
1064 unp->unp_addr = (struct sockaddr_un *)dup_sockaddr(nam, 1);
91447636
A
1065 vnode_put(vp); /* drop the iocount */
1066
1c79356b
A
1067 return (0);
1068}
1069
2d21ac55
A
1070
1071/*
1072 * Returns: 0 Success
1073 * EAFNOSUPPORT Address family not supported
1074 * EINVAL Invalid argument
1075 * ENOTSOCK Not a socket
1076 * ECONNREFUSED Connection refused
1077 * EPROTOTYPE Protocol wrong type for socket
1078 * EISCONN Socket is connected
1079 * unp_connect2:EPROTOTYPE Protocol wrong type for socket
1080 * unp_connect2:EINVAL Invalid argument
1081 * namei:??? [anything namei can return]
1082 * vnode_authorize:???? [anything vnode_authorize can return]
1083 *
1084 * Notes: p at this point is the current process, as this function is
1085 * only called by sosend(), sendfile(), and soconnectlock().
1086 */
1c79356b 1087static int
2d21ac55 1088unp_connect(struct socket *so, struct sockaddr *nam, __unused proc_t p)
1c79356b 1089{
91447636
A
1090 struct sockaddr_un *soun = (struct sockaddr_un *)nam;
1091 struct vnode *vp;
b0d623f7 1092 struct socket *so2, *so3, *list_so=NULL;
91447636 1093 struct unpcb *unp, *unp2, *unp3;
2d21ac55 1094 vfs_context_t ctx = vfs_context_current();
1c79356b
A
1095 int error, len;
1096 struct nameidata nd;
1097 char buf[SOCK_MAXADDRLEN];
1098
2d21ac55
A
1099 if (nam->sa_family != 0 && nam->sa_family != AF_UNIX) {
1100 return (EAFNOSUPPORT);
1101 }
1102
b0d623f7 1103 unp = sotounpcb(so);
cc9f6e38 1104 so2 = so3 = NULL;
91447636 1105
1c79356b 1106 len = nam->sa_len - offsetof(struct sockaddr_un, sun_path);
2d21ac55
A
1107 if (len <= 0)
1108 return (EINVAL);
490019cf
A
1109 /*
1110 * Note: sun_path is not a zero terminated "C" string
1111 */
1112 ASSERT(len < SOCK_MAXADDRLEN);
1113 bcopy(soun->sun_path, buf, len);
1114 buf[len] = 0;
2d21ac55 1115
b0d623f7 1116 socket_unlock(so, 0);
1c79356b 1117
6d2010ae 1118 NDINIT(&nd, LOOKUP, OP_LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE,
2d21ac55 1119 CAST_USER_ADDR_T(buf), ctx);
1c79356b
A
1120 error = namei(&nd);
1121 if (error) {
b0d623f7 1122 socket_lock(so, 0);
1c79356b
A
1123 return (error);
1124 }
91447636 1125 nameidone(&nd);
1c79356b
A
1126 vp = nd.ni_vp;
1127 if (vp->v_type != VSOCK) {
1128 error = ENOTSOCK;
b0d623f7
A
1129 socket_lock(so, 0);
1130 goto out;
1c79356b 1131 }
91447636 1132
b0d623f7 1133#if CONFIG_MACF_SOCKET_SUBSET
39037602 1134 error = mac_vnode_check_uipc_connect(ctx, vp, so);
b0d623f7
A
1135 if (error) {
1136 socket_lock(so, 0);
1137 goto out;
1138 }
1139#endif /* MAC_SOCKET_SUBSET */
1140
2d21ac55 1141 error = vnode_authorize(vp, NULL, KAUTH_VNODE_WRITE_DATA, ctx);
b0d623f7
A
1142 if (error) {
1143 socket_lock(so, 0);
1144 goto out;
1145 }
1146
1147 lck_mtx_lock(unp_connect_lock);
1148
1149 if (vp->v_socket == 0) {
1150 lck_mtx_unlock(unp_connect_lock);
1c79356b 1151 error = ECONNREFUSED;
b0d623f7
A
1152 socket_lock(so, 0);
1153 goto out;
1c79356b 1154 }
91447636 1155
b0d623f7
A
1156 socket_lock(vp->v_socket, 1); /* Get a reference on the listening socket */
1157 so2 = vp->v_socket;
1158 lck_mtx_unlock(unp_connect_lock);
91447636 1159
b0d623f7
A
1160
1161 if (so2->so_pcb == NULL) {
1162 error = ECONNREFUSED;
6d2010ae
A
1163 if (so != so2) {
1164 socket_unlock(so2, 1);
1165 socket_lock(so, 0);
1166 } else {
1167 /* Release the reference held for the listen socket */
d190cdc3 1168 VERIFY(so2->so_usecount > 0);
6d2010ae
A
1169 so2->so_usecount--;
1170 }
b0d623f7 1171 goto out;
1c79356b 1172 }
2d21ac55 1173
b0d623f7
A
1174 if (so < so2) {
1175 socket_unlock(so2, 0);
1176 socket_lock(so, 0);
1177 socket_lock(so2, 0);
6d2010ae 1178 } else if (so > so2) {
b0d623f7
A
1179 socket_lock(so, 0);
1180 }
55e303ae
A
1181 /*
1182 * Check if socket was connected while we were trying to
b0d623f7 1183 * get the socket locks in order.
55e303ae
A
1184 * XXX - probably shouldn't return an error for SOCK_DGRAM
1185 */
1186 if ((so->so_state & SS_ISCONNECTED) != 0) {
1187 error = EISCONN;
6d2010ae 1188 goto decref_out;
b0d623f7
A
1189 }
1190
1191 if (so->so_type != so2->so_type) {
b0d623f7 1192 error = EPROTOTYPE;
6d2010ae 1193 goto decref_out;
55e303ae 1194 }
2d21ac55 1195
1c79356b 1196 if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
b0d623f7
A
1197 /* Release the incoming socket but keep a reference */
1198 socket_unlock(so, 0);
1199
1c79356b 1200 if ((so2->so_options & SO_ACCEPTCONN) == 0 ||
91447636 1201 (so3 = sonewconn(so2, 0, nam)) == 0) {
1c79356b 1202 error = ECONNREFUSED;
316670eb
A
1203 if (so != so2) {
1204 socket_unlock(so2, 1);
1205 socket_lock(so, 0);
1206 } else {
1207 socket_lock(so, 0);
1208 /* Release the reference held for
1209 * listen socket.
1210 */
d190cdc3 1211 VERIFY(so2->so_usecount > 0);
316670eb
A
1212 so2->so_usecount--;
1213 }
b0d623f7 1214 goto out;
1c79356b
A
1215 }
1216 unp2 = sotounpcb(so2);
1217 unp3 = sotounpcb(so3);
1218 if (unp2->unp_addr)
1219 unp3->unp_addr = (struct sockaddr_un *)
2d21ac55 1220 dup_sockaddr((struct sockaddr *)unp2->unp_addr, 1);
91447636
A
1221
1222 /*
1223 * unp_peercred management:
1224 *
1225 * The connecter's (client's) credentials are copied
1226 * from its process structure at the time of connect()
1227 * (which is now).
1228 */
2d21ac55 1229 cru2x(vfs_context_ucred(ctx), &unp3->unp_peercred);
91447636
A
1230 unp3->unp_flags |= UNP_HAVEPC;
1231 /*
1232 * The receiver's (server's) credentials are copied
1233 * from the unp_peercred member of socket on which the
1234 * former called listen(); unp_listen() cached that
1235 * process's credentials at that time so we can use
1236 * them now.
1237 */
1238 KASSERT(unp2->unp_flags & UNP_HAVEPCCACHED,
1239 ("unp_connect: listener without cached peercred"));
b0d623f7
A
1240
1241 /* Here we need to have both so and so2 locks and so2
1242 * is already locked. Lock ordering is required.
1243 */
1244 if (so < so2) {
1245 socket_unlock(so2, 0);
1246 socket_lock(so, 0);
1247 socket_lock(so2, 0);
1248 } else {
1249 socket_lock(so, 0);
1250 }
1251
1252 /* Check again if the socket state changed when its lock was released */
1253 if ((so->so_state & SS_ISCONNECTED) != 0) {
1254 error = EISCONN;
1255 socket_unlock(so2, 1);
1256 socket_lock(so3, 0);
1257 sofreelastref(so3, 1);
1258 goto out;
1259 }
91447636 1260 memcpy(&unp->unp_peercred, &unp2->unp_peercred,
2d21ac55 1261 sizeof (unp->unp_peercred));
91447636
A
1262 unp->unp_flags |= UNP_HAVEPC;
1263
2d21ac55
A
1264#if CONFIG_MACF_SOCKET
1265 /* XXXMAC: recursive lock: SOCK_LOCK(so); */
1266 mac_socketpeer_label_associate_socket(so, so3);
1267 mac_socketpeer_label_associate_socket(so3, so);
1268 /* XXXMAC: SOCK_UNLOCK(so); */
1269#endif /* MAC_SOCKET */
b0d623f7
A
1270
1271 /* Hold the reference on listening socket until the end */
1272 socket_unlock(so2, 0);
1273 list_so = so2;
1274
1275 /* Lock ordering doesn't matter because so3 was just created */
1276 socket_lock(so3, 1);
1c79356b 1277 so2 = so3;
b0d623f7 1278
6d2010ae
A
1279 /*
1280 * Enable tracing for mDNSResponder endpoints. (The use
1281 * of sizeof instead of strlen below takes the null
1282 * terminating character into account.)
1283 */
1284 if (unpst_tracemdns &&
1285 !strncmp(soun->sun_path, MDNSRESPONDER_PATH,
1286 sizeof (MDNSRESPONDER_PATH))) {
1287 unp->unp_flags |= UNP_TRACE_MDNS;
1288 unp2->unp_flags |= UNP_TRACE_MDNS;
1289 }
1c79356b 1290 }
b0d623f7 1291
1c79356b 1292 error = unp_connect2(so, so2);
6d2010ae
A
1293
1294decref_out:
b0d623f7 1295 if (so2 != NULL) {
6d2010ae
A
1296 if (so != so2) {
1297 socket_unlock(so2, 1);
1298 } else {
1299 /* Release the extra reference held for the listen socket.
1300 * This is possible only for SOCK_DGRAM sockets. We refuse
1301 * connecting to the same socket for SOCK_STREAM sockets.
1302 */
d190cdc3 1303 VERIFY(so2->so_usecount > 0);
6d2010ae
A
1304 so2->so_usecount--;
1305 }
b0d623f7
A
1306 }
1307
1308 if (list_so != NULL) {
1309 socket_lock(list_so, 0);
1310 socket_unlock(list_so, 1);
1311 }
6d2010ae 1312
b0d623f7 1313out:
6d2010ae 1314 lck_mtx_assert(&unp->unp_mtx, LCK_MTX_ASSERT_OWNED);
91447636 1315 vnode_put(vp);
1c79356b
A
1316 return (error);
1317}
1318
2d21ac55
A
1319/*
1320 * Returns: 0 Success
1321 * EPROTOTYPE Protocol wrong type for socket
1322 * EINVAL Invalid argument
1323 */
1c79356b 1324int
2d21ac55 1325unp_connect2(struct socket *so, struct socket *so2)
1c79356b 1326{
91447636
A
1327 struct unpcb *unp = sotounpcb(so);
1328 struct unpcb *unp2;
1c79356b
A
1329
1330 if (so2->so_type != so->so_type)
1331 return (EPROTOTYPE);
b0d623f7 1332
1c79356b 1333 unp2 = sotounpcb(so2);
0b4e3aa0 1334
6d2010ae
A
1335 lck_mtx_assert(&unp->unp_mtx, LCK_MTX_ASSERT_OWNED);
1336 lck_mtx_assert(&unp2->unp_mtx, LCK_MTX_ASSERT_OWNED);
b0d623f7 1337
0b4e3aa0
A
1338 /* Verify both sockets are still opened */
1339 if (unp == 0 || unp2 == 0)
1340 return (EINVAL);
1341
1c79356b 1342 unp->unp_conn = unp2;
b0d623f7
A
1343 so2->so_usecount++;
1344
1c79356b
A
1345 switch (so->so_type) {
1346
1347 case SOCK_DGRAM:
1348 LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink);
b0d623f7 1349
6d2010ae
A
1350 if (so != so2) {
1351 /* Avoid lock order reversals due to drop/acquire in soisconnected. */
1352 /* Keep an extra reference on so2 that will be dropped
1353 * soon after getting the locks in order
1354 */
1355 socket_unlock(so2, 0);
1356 soisconnected(so);
1357 unp_get_locks_in_order(so, so2);
d190cdc3 1358 VERIFY(so2->so_usecount > 0);
6d2010ae
A
1359 so2->so_usecount--;
1360 } else {
1361 soisconnected(so);
1362 }
b0d623f7 1363
1c79356b
A
1364 break;
1365
1366 case SOCK_STREAM:
2d21ac55
A
1367 /* This takes care of socketpair */
1368 if (!(unp->unp_flags & UNP_HAVEPC) &&
1369 !(unp2->unp_flags & UNP_HAVEPC)) {
1370 cru2x(kauth_cred_get(), &unp->unp_peercred);
1371 unp->unp_flags |= UNP_HAVEPC;
1372
1373 cru2x(kauth_cred_get(), &unp2->unp_peercred);
1374 unp2->unp_flags |= UNP_HAVEPC;
1375 }
1c79356b 1376 unp2->unp_conn = unp;
b0d623f7
A
1377 so->so_usecount++;
1378
1379 /* Avoid lock order reversals due to drop/acquire in soisconnected. */
1380 socket_unlock(so, 0);
1c79356b 1381 soisconnected(so2);
b0d623f7
A
1382
1383 /* Keep an extra reference on so2, that will be dropped soon after
1384 * getting the locks in order again.
1385 */
1386 socket_unlock(so2, 0);
1387
1388 socket_lock(so, 0);
1389 soisconnected(so);
1390
1391 unp_get_locks_in_order(so, so2);
1392 /* Decrement the extra reference left before */
d190cdc3 1393 VERIFY(so2->so_usecount > 0);
b0d623f7 1394 so2->so_usecount--;
1c79356b
A
1395 break;
1396
1397 default:
b0d623f7 1398 panic("unknown socket type %d in unp_connect2", so->so_type);
1c79356b 1399 }
6d2010ae
A
1400 lck_mtx_assert(&unp->unp_mtx, LCK_MTX_ASSERT_OWNED);
1401 lck_mtx_assert(&unp2->unp_mtx, LCK_MTX_ASSERT_OWNED);
1c79356b
A
1402 return (0);
1403}
1404
1405static void
91447636 1406unp_disconnect(struct unpcb *unp)
1c79356b 1407{
b0d623f7
A
1408 struct unpcb *unp2 = NULL;
1409 struct socket *so2 = NULL, *so;
1410 struct socket *waitso;
1411 int so_locked = 1, strdisconn = 0;
1c79356b 1412
b0d623f7
A
1413 so = unp->unp_socket;
1414 if (unp->unp_conn == NULL) {
1c79356b 1415 return;
b0d623f7
A
1416 }
1417 lck_mtx_lock(unp_disconnect_lock);
1418 while (disconnect_in_progress != 0) {
1419 if (so_locked == 1) {
1420 socket_unlock(so, 0);
1421 so_locked = 0;
1422 }
1423 (void)msleep((caddr_t)&disconnect_in_progress, unp_disconnect_lock,
1424 PSOCK, "disconnect", NULL);
1425 }
1426 disconnect_in_progress = 1;
1427 lck_mtx_unlock(unp_disconnect_lock);
1428
1429 if (so_locked == 0) {
1430 socket_lock(so, 0);
1431 so_locked = 1;
1432 }
1433
1434 unp2 = unp->unp_conn;
1435
1436 if (unp2 == 0 || unp2->unp_socket == NULL) {
1437 goto out;
1438 }
1439 so2 = unp2->unp_socket;
1440
1441try_again:
6d2010ae
A
1442 if (so == so2) {
1443 if (so_locked == 0) {
1444 socket_lock(so, 0);
1445 }
1446 waitso = so;
1447 } else if (so < so2) {
b0d623f7
A
1448 if (so_locked == 0) {
1449 socket_lock(so, 0);
1450 }
1451 socket_lock(so2, 1);
1452 waitso = so2;
1453 } else {
1454 if (so_locked == 1) {
1455 socket_unlock(so, 0);
1456 }
1457 socket_lock(so2, 1);
1458 socket_lock(so, 0);
1459 waitso = so;
1460 }
6d2010ae 1461 so_locked = 1;
b0d623f7 1462
6d2010ae
A
1463 lck_mtx_assert(&unp->unp_mtx, LCK_MTX_ASSERT_OWNED);
1464 lck_mtx_assert(&unp2->unp_mtx, LCK_MTX_ASSERT_OWNED);
b0d623f7
A
1465
1466 /* Check for the UNP_DONTDISCONNECT flag, if it
1467 * is set, release both sockets and go to sleep
1468 */
1469
1470 if ((((struct unpcb *)waitso->so_pcb)->unp_flags & UNP_DONTDISCONNECT) != 0) {
6d2010ae
A
1471 if (so != so2) {
1472 socket_unlock(so2, 1);
1473 }
b0d623f7
A
1474 so_locked = 0;
1475
6d2010ae 1476 (void)msleep(waitso->so_pcb, &unp->unp_mtx,
b0d623f7
A
1477 PSOCK | PDROP, "unpdisconnect", NULL);
1478 goto try_again;
1479 }
1480
1481 if (unp->unp_conn == NULL) {
1482 panic("unp_conn became NULL after sleep");
1483 }
1484
2d21ac55 1485 unp->unp_conn = NULL;
d190cdc3 1486 VERIFY(so2->so_usecount > 0);
b0d623f7
A
1487 so2->so_usecount--;
1488
6d2010ae
A
1489 if (unp->unp_flags & UNP_TRACE_MDNS)
1490 unp->unp_flags &= ~UNP_TRACE_MDNS;
1491
1c79356b
A
1492 switch (unp->unp_socket->so_type) {
1493
1494 case SOCK_DGRAM:
1495 LIST_REMOVE(unp, unp_reflink);
1496 unp->unp_socket->so_state &= ~SS_ISCONNECTED;
6d2010ae
A
1497 if (so != so2)
1498 socket_unlock(so2, 1);
1c79356b
A
1499 break;
1500
1501 case SOCK_STREAM:
2d21ac55 1502 unp2->unp_conn = NULL;
d190cdc3 1503 VERIFY(so2->so_usecount > 0);
b0d623f7
A
1504 so->so_usecount--;
1505
1506 /* Set the socket state correctly but do a wakeup later when
1507 * we release all locks except the socket lock, this will avoid
1508 * a deadlock.
1509 */
1510 unp->unp_socket->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING);
1511 unp->unp_socket->so_state |= (SS_CANTRCVMORE|SS_CANTSENDMORE|SS_ISDISCONNECTED);
1512
1513 unp2->unp_socket->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING);
1514 unp->unp_socket->so_state |= (SS_CANTRCVMORE|SS_CANTSENDMORE|SS_ISDISCONNECTED);
6d2010ae
A
1515
1516 if (unp2->unp_flags & UNP_TRACE_MDNS)
1517 unp2->unp_flags &= ~UNP_TRACE_MDNS;
1518
b0d623f7 1519 strdisconn = 1;
1c79356b 1520 break;
b0d623f7
A
1521 default:
1522 panic("unknown socket type %d", so->so_type);
1c79356b 1523 }
b0d623f7
A
1524out:
1525 lck_mtx_lock(unp_disconnect_lock);
1526 disconnect_in_progress = 0;
1527 wakeup(&disconnect_in_progress);
1528 lck_mtx_unlock(unp_disconnect_lock);
1c79356b 1529
b0d623f7
A
1530 if (strdisconn) {
1531 socket_unlock(so, 0);
1532 soisdisconnected(so2);
1533 socket_unlock(so2, 1);
1c79356b 1534
b0d623f7
A
1535 socket_lock(so,0);
1536 soisdisconnected(so);
1537 }
6d2010ae 1538 lck_mtx_assert(&unp->unp_mtx, LCK_MTX_ASSERT_OWNED);
b0d623f7 1539 return;
1c79356b 1540}
b0d623f7
A
1541
1542/*
1543 * unpcb_to_compat copies specific bits of a unpcb to a unpcb_compat format.
1544 * The unpcb_compat data structure is passed to user space and must not change.
1545 */
1546static void
1547unpcb_to_compat(struct unpcb *up, struct unpcb_compat *cp)
1548{
1549#if defined(__LP64__)
316670eb
A
1550 cp->unp_link.le_next = (u_int32_t)
1551 VM_KERNEL_ADDRPERM(up->unp_link.le_next);
1552 cp->unp_link.le_prev = (u_int32_t)
1553 VM_KERNEL_ADDRPERM(up->unp_link.le_prev);
b0d623f7 1554#else
316670eb
A
1555 cp->unp_link.le_next = (struct unpcb_compat *)
1556 VM_KERNEL_ADDRPERM(up->unp_link.le_next);
1557 cp->unp_link.le_prev = (struct unpcb_compat **)
1558 VM_KERNEL_ADDRPERM(up->unp_link.le_prev);
b0d623f7 1559#endif
316670eb
A
1560 cp->unp_socket = (_UNPCB_PTR(struct socket *))
1561 VM_KERNEL_ADDRPERM(up->unp_socket);
1562 cp->unp_vnode = (_UNPCB_PTR(struct vnode *))
1563 VM_KERNEL_ADDRPERM(up->unp_vnode);
b0d623f7
A
1564 cp->unp_ino = up->unp_ino;
1565 cp->unp_conn = (_UNPCB_PTR(struct unpcb_compat *))
316670eb
A
1566 VM_KERNEL_ADDRPERM(up->unp_conn);
1567 cp->unp_refs = (u_int32_t)VM_KERNEL_ADDRPERM(up->unp_refs.lh_first);
b0d623f7
A
1568#if defined(__LP64__)
1569 cp->unp_reflink.le_next =
316670eb 1570 (u_int32_t)VM_KERNEL_ADDRPERM(up->unp_reflink.le_next);
b0d623f7 1571 cp->unp_reflink.le_prev =
316670eb 1572 (u_int32_t)VM_KERNEL_ADDRPERM(up->unp_reflink.le_prev);
b0d623f7
A
1573#else
1574 cp->unp_reflink.le_next =
316670eb 1575 (struct unpcb_compat *)VM_KERNEL_ADDRPERM(up->unp_reflink.le_next);
b0d623f7 1576 cp->unp_reflink.le_prev =
316670eb 1577 (struct unpcb_compat **)VM_KERNEL_ADDRPERM(up->unp_reflink.le_prev);
1c79356b 1578#endif
b0d623f7 1579 cp->unp_addr = (_UNPCB_PTR(struct sockaddr_un *))
316670eb 1580 VM_KERNEL_ADDRPERM(up->unp_addr);
b0d623f7
A
1581 cp->unp_cc = up->unp_cc;
1582 cp->unp_mbcnt = up->unp_mbcnt;
1583 cp->unp_gencnt = up->unp_gencnt;
1584}
1c79356b
A
1585
1586static int
1587unp_pcblist SYSCTL_HANDLER_ARGS
1588{
2d21ac55 1589#pragma unused(oidp,arg2)
1c79356b
A
1590 int error, i, n;
1591 struct unpcb *unp, **unp_list;
1592 unp_gen_t gencnt;
1593 struct xunpgen xug;
1594 struct unp_head *head;
1595
37839358 1596 lck_rw_lock_shared(unp_list_mtx);
1c79356b
A
1597 head = ((intptr_t)arg1 == SOCK_DGRAM ? &unp_dhead : &unp_shead);
1598
1599 /*
1600 * The process of preparing the PCB list is too time-consuming and
1601 * resource-intensive to repeat twice on every request.
1602 */
91447636 1603 if (req->oldptr == USER_ADDR_NULL) {
1c79356b 1604 n = unp_count;
2d21ac55
A
1605 req->oldidx = 2 * sizeof (xug) + (n + n / 8) *
1606 sizeof (struct xunpcb);
37839358 1607 lck_rw_done(unp_list_mtx);
2d21ac55 1608 return (0);
1c79356b
A
1609 }
1610
91447636 1611 if (req->newptr != USER_ADDR_NULL) {
37839358 1612 lck_rw_done(unp_list_mtx);
2d21ac55 1613 return (EPERM);
91447636 1614 }
1c79356b
A
1615
1616 /*
1617 * OK, now we're committed to doing something.
1618 */
1619 gencnt = unp_gencnt;
1620 n = unp_count;
1621
2d21ac55
A
1622 bzero(&xug, sizeof (xug));
1623 xug.xug_len = sizeof (xug);
1c79356b
A
1624 xug.xug_count = n;
1625 xug.xug_gen = gencnt;
1626 xug.xug_sogen = so_gencnt;
2d21ac55 1627 error = SYSCTL_OUT(req, &xug, sizeof (xug));
91447636 1628 if (error) {
37839358 1629 lck_rw_done(unp_list_mtx);
2d21ac55 1630 return (error);
91447636 1631 }
1c79356b 1632
0b4e3aa0
A
1633 /*
1634 * We are done if there is no pcb
1635 */
91447636 1636 if (n == 0) {
2d21ac55
A
1637 lck_rw_done(unp_list_mtx);
1638 return (0);
91447636 1639 }
0b4e3aa0 1640
2d21ac55
A
1641 MALLOC(unp_list, struct unpcb **, n * sizeof (*unp_list),
1642 M_TEMP, M_WAITOK);
91447636 1643 if (unp_list == 0) {
37839358 1644 lck_rw_done(unp_list_mtx);
2d21ac55 1645 return (ENOMEM);
91447636 1646 }
2d21ac55 1647
1c79356b 1648 for (unp = head->lh_first, i = 0; unp && i < n;
2d21ac55 1649 unp = unp->unp_link.le_next) {
1c79356b
A
1650 if (unp->unp_gencnt <= gencnt)
1651 unp_list[i++] = unp;
1652 }
1653 n = i; /* in case we lost some during malloc */
1654
1655 error = 0;
1656 for (i = 0; i < n; i++) {
1657 unp = unp_list[i];
1658 if (unp->unp_gencnt <= gencnt) {
1659 struct xunpcb xu;
3a60a9f5 1660
2d21ac55
A
1661 bzero(&xu, sizeof (xu));
1662 xu.xu_len = sizeof (xu);
b0d623f7 1663 xu.xu_unpp = (_UNPCB_PTR(struct unpcb_compat *))
316670eb 1664 VM_KERNEL_ADDRPERM(unp);
1c79356b
A
1665 /*
1666 * XXX - need more locking here to protect against
1667 * connect/disconnect races for SMP.
1668 */
1669 if (unp->unp_addr)
2d21ac55
A
1670 bcopy(unp->unp_addr, &xu.xu_addr,
1671 unp->unp_addr->sun_len);
1c79356b
A
1672 if (unp->unp_conn && unp->unp_conn->unp_addr)
1673 bcopy(unp->unp_conn->unp_addr,
2d21ac55
A
1674 &xu.xu_caddr,
1675 unp->unp_conn->unp_addr->sun_len);
b0d623f7 1676 unpcb_to_compat(unp, &xu.xu_unp);
1c79356b 1677 sotoxsocket(unp->unp_socket, &xu.xu_socket);
2d21ac55 1678 error = SYSCTL_OUT(req, &xu, sizeof (xu));
1c79356b
A
1679 }
1680 }
1681 if (!error) {
1682 /*
1683 * Give the user an updated idea of our state.
1684 * If the generation differs from what we told
1685 * her before, she knows that something happened
1686 * while we were processing this request, and it
1687 * might be necessary to retry.
1688 */
2d21ac55
A
1689 bzero(&xug, sizeof (xug));
1690 xug.xug_len = sizeof (xug);
1c79356b
A
1691 xug.xug_gen = unp_gencnt;
1692 xug.xug_sogen = so_gencnt;
1693 xug.xug_count = unp_count;
2d21ac55 1694 error = SYSCTL_OUT(req, &xug, sizeof (xug));
1c79356b
A
1695 }
1696 FREE(unp_list, M_TEMP);
37839358 1697 lck_rw_done(unp_list_mtx);
2d21ac55 1698 return (error);
1c79356b
A
1699}
1700
fe8ab488
A
1701SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist,
1702 CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_LOCKED,
b0d623f7
A
1703 (caddr_t)(long)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb",
1704 "List of active local datagram sockets");
fe8ab488
A
1705SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist,
1706 CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_LOCKED,
b0d623f7
A
1707 (caddr_t)(long)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb",
1708 "List of active local stream sockets");
1709
b0d623f7
A
1710
1711static int
1712unp_pcblist64 SYSCTL_HANDLER_ARGS
1713{
1714#pragma unused(oidp,arg2)
1715 int error, i, n;
1716 struct unpcb *unp, **unp_list;
1717 unp_gen_t gencnt;
1718 struct xunpgen xug;
1719 struct unp_head *head;
1720
1721 lck_rw_lock_shared(unp_list_mtx);
1722 head = ((intptr_t)arg1 == SOCK_DGRAM ? &unp_dhead : &unp_shead);
1723
1724 /*
1725 * The process of preparing the PCB list is too time-consuming and
1726 * resource-intensive to repeat twice on every request.
1727 */
1728 if (req->oldptr == USER_ADDR_NULL) {
1729 n = unp_count;
1730 req->oldidx = 2 * sizeof (xug) + (n + n / 8) *
1731 (sizeof (struct xunpcb64));
1732 lck_rw_done(unp_list_mtx);
1733 return (0);
1734 }
1735
1736 if (req->newptr != USER_ADDR_NULL) {
1737 lck_rw_done(unp_list_mtx);
1738 return (EPERM);
1739 }
1740
1741 /*
1742 * OK, now we're committed to doing something.
1743 */
1744 gencnt = unp_gencnt;
1745 n = unp_count;
1746
1747 bzero(&xug, sizeof (xug));
1748 xug.xug_len = sizeof (xug);
1749 xug.xug_count = n;
1750 xug.xug_gen = gencnt;
1751 xug.xug_sogen = so_gencnt;
1752 error = SYSCTL_OUT(req, &xug, sizeof (xug));
1753 if (error) {
1754 lck_rw_done(unp_list_mtx);
1755 return (error);
1756 }
1757
1758 /*
1759 * We are done if there is no pcb
1760 */
1761 if (n == 0) {
1762 lck_rw_done(unp_list_mtx);
1763 return (0);
1764 }
1765
1766 MALLOC(unp_list, struct unpcb **, n * sizeof (*unp_list),
1767 M_TEMP, M_WAITOK);
1768 if (unp_list == 0) {
1769 lck_rw_done(unp_list_mtx);
1770 return (ENOMEM);
1771 }
1772
1773 for (unp = head->lh_first, i = 0; unp && i < n;
1774 unp = unp->unp_link.le_next) {
1775 if (unp->unp_gencnt <= gencnt)
1776 unp_list[i++] = unp;
1777 }
1778 n = i; /* in case we lost some during malloc */
1779
1780 error = 0;
1781 for (i = 0; i < n; i++) {
1782 unp = unp_list[i];
1783 if (unp->unp_gencnt <= gencnt) {
1784 struct xunpcb64 xu;
1785 size_t xu_len = sizeof(struct xunpcb64);
1786
1787 bzero(&xu, xu_len);
1788 xu.xu_len = xu_len;
316670eb
A
1789 xu.xu_unpp = (u_int64_t)VM_KERNEL_ADDRPERM(unp);
1790 xu.xunp_link.le_next = (u_int64_t)
1791 VM_KERNEL_ADDRPERM(unp->unp_link.le_next);
1792 xu.xunp_link.le_prev = (u_int64_t)
1793 VM_KERNEL_ADDRPERM(unp->unp_link.le_prev);
1794 xu.xunp_socket = (u_int64_t)
1795 VM_KERNEL_ADDRPERM(unp->unp_socket);
1796 xu.xunp_vnode = (u_int64_t)
1797 VM_KERNEL_ADDRPERM(unp->unp_vnode);
b0d623f7 1798 xu.xunp_ino = unp->unp_ino;
316670eb
A
1799 xu.xunp_conn = (u_int64_t)
1800 VM_KERNEL_ADDRPERM(unp->unp_conn);
1801 xu.xunp_refs = (u_int64_t)
1802 VM_KERNEL_ADDRPERM(unp->unp_refs.lh_first);
1803 xu.xunp_reflink.le_next = (u_int64_t)
1804 VM_KERNEL_ADDRPERM(unp->unp_reflink.le_next);
1805 xu.xunp_reflink.le_prev = (u_int64_t)
1806 VM_KERNEL_ADDRPERM(unp->unp_reflink.le_prev);
b0d623f7
A
1807 xu.xunp_cc = unp->unp_cc;
1808 xu.xunp_mbcnt = unp->unp_mbcnt;
1809 xu.xunp_gencnt = unp->unp_gencnt;
1810
1811 if (unp->unp_socket)
1812 sotoxsocket64(unp->unp_socket, &xu.xu_socket);
1813
1814 /*
1815 * XXX - need more locking here to protect against
1816 * connect/disconnect races for SMP.
1817 */
1818 if (unp->unp_addr)
1819 bcopy(unp->unp_addr, &xu.xunp_addr,
1820 unp->unp_addr->sun_len);
1821 if (unp->unp_conn && unp->unp_conn->unp_addr)
1822 bcopy(unp->unp_conn->unp_addr,
1823 &xu.xunp_caddr,
1824 unp->unp_conn->unp_addr->sun_len);
1825
1826 error = SYSCTL_OUT(req, &xu, xu_len);
1827 }
1828 }
1829 if (!error) {
1830 /*
1831 * Give the user an updated idea of our state.
1832 * If the generation differs from what we told
1833 * her before, she knows that something happened
1834 * while we were processing this request, and it
1835 * might be necessary to retry.
1836 */
1837 bzero(&xug, sizeof (xug));
1838 xug.xug_len = sizeof (xug);
1839 xug.xug_gen = unp_gencnt;
1840 xug.xug_sogen = so_gencnt;
1841 xug.xug_count = unp_count;
1842 error = SYSCTL_OUT(req, &xug, sizeof (xug));
1843 }
1844 FREE(unp_list, M_TEMP);
1845 lck_rw_done(unp_list_mtx);
1846 return (error);
1847}
1848
fe8ab488
A
1849SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist64,
1850 CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_LOCKED,
b0d623f7
A
1851 (caddr_t)(long)SOCK_DGRAM, 0, unp_pcblist64, "S,xunpcb64",
1852 "List of active local datagram sockets 64 bit");
fe8ab488
A
1853SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist64,
1854 CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_LOCKED,
b0d623f7
A
1855 (caddr_t)(long)SOCK_STREAM, 0, unp_pcblist64, "S,xunpcb64",
1856 "List of active local stream sockets 64 bit");
1857
1c79356b
A
1858
1859static void
91447636 1860unp_shutdown(struct unpcb *unp)
1c79356b 1861{
b0d623f7
A
1862 struct socket *so = unp->unp_socket;
1863 struct socket *so2;
1864 if (unp->unp_socket->so_type == SOCK_STREAM && unp->unp_conn) {
1865 so2 = unp->unp_conn->unp_socket;
1866 unp_get_locks_in_order(so, so2);
1867 socantrcvmore(so2);
1868 socket_unlock(so2, 1);
1869 }
1c79356b
A
1870}
1871
1872static void
2d21ac55 1873unp_drop(struct unpcb *unp, int errno)
1c79356b
A
1874{
1875 struct socket *so = unp->unp_socket;
1876
1877 so->so_error = errno;
1878 unp_disconnect(unp);
1c79356b
A
1879}
1880
2d21ac55
A
1881/*
1882 * Returns: 0 Success
1883 * EMSGSIZE The new fd's will not fit
1884 * ENOBUFS Cannot alloc struct fileproc
1885 */
1c79356b 1886int
91447636 1887unp_externalize(struct mbuf *rights)
1c79356b 1888{
2d21ac55 1889 proc_t p = current_proc(); /* XXX */
91447636
A
1890 int i;
1891 struct cmsghdr *cm = mtod(rights, struct cmsghdr *);
1892 struct fileglob **rp = (struct fileglob **)(cm + 1);
b0d623f7 1893 int *fds = (int *)(cm + 1);
91447636 1894 struct fileproc *fp;
00867663 1895 struct fileproc **fileproc_l;
2d21ac55 1896 int newfds = (cm->cmsg_len - sizeof (*cm)) / sizeof (int);
3e170ce0
A
1897 int f, error = 0;
1898
00867663
A
1899 MALLOC(fileproc_l, struct fileproc **,
1900 newfds * sizeof (struct fileproc *), M_TEMP, M_WAITOK);
1901 if (fileproc_l == NULL) {
3e170ce0
A
1902 error = ENOMEM;
1903 goto discard;
1904 }
1c79356b 1905
91447636 1906 proc_fdlock(p);
1c79356b
A
1907
1908 /*
1909 * if the new FD's will not fit, then we free them all
1910 */
1911 if (!fdavail(p, newfds)) {
91447636 1912 proc_fdunlock(p);
3e170ce0
A
1913 error = EMSGSIZE;
1914 goto discard;
1c79356b
A
1915 }
1916 /*
2d21ac55 1917 * now change each pointer to an fd in the global table to
1c79356b
A
1918 * an integer that is the index to the local fd table entry
1919 * that we set up to point to the global one we are transferring.
b0d623f7
A
1920 * XXX (1) this assumes a pointer and int are the same size,
1921 * XXX or the mbuf can hold the expansion
2d21ac55 1922 * XXX (2) allocation failures should be non-fatal
1c79356b
A
1923 */
1924 for (i = 0; i < newfds; i++) {
2d21ac55
A
1925#if CONFIG_MACF_SOCKET
1926 /*
1927 * If receive access is denied, don't pass along
1928 * and error message, just discard the descriptor.
1929 */
3e170ce0
A
1930 if (mac_file_check_receive(kauth_cred_get(), rp[i])) {
1931 proc_fdunlock(p);
1932 unp_discard(rp[i], p);
1933 fds[i] = 0;
1934 proc_fdlock(p);
2d21ac55
A
1935 continue;
1936 }
1937#endif
1c79356b 1938 if (fdalloc(p, 0, &f))
2d21ac55 1939 panic("unp_externalize:fdalloc");
39236c6e 1940 fp = fileproc_alloc_init(NULL);
2d21ac55
A
1941 if (fp == NULL)
1942 panic("unp_externalize: MALLOC_ZONE");
91447636 1943 fp->f_iocount = 0;
3e170ce0 1944 fp->f_fglob = rp[i];
00867663
A
1945 if (fg_removeuipc_mark(rp[i])) {
1946
1947 /*
1948 * Take an iocount on the fp for completing the
1949 * removal from the global msg queue
1950 */
1951 fp->f_iocount++;
1952 fileproc_l[i] = fp;
1953 } else {
1954 fileproc_l[i] = NULL;
1955 }
6601e61a 1956 procfdtbl_releasefd(p, f, fp);
b0d623f7 1957 fds[i] = f;
1c79356b 1958 }
91447636 1959 proc_fdunlock(p);
1c79356b 1960
3e170ce0 1961 for (i = 0; i < newfds; i++) {
00867663
A
1962 if (fileproc_l[i] != NULL) {
1963 VERIFY(fileproc_l[i]->f_fglob != NULL &&
1964 (fileproc_l[i]->f_fglob->fg_lflags & FG_RMMSGQ));
1965 VERIFY(fds[i] > 0);
1966 fg_removeuipc(fileproc_l[i]->f_fglob);
1967
1968 /* Drop the iocount */
1969 fp_drop(p, fds[i], fileproc_l[i], 0);
1970 fileproc_l[i] = NULL;
3e170ce0 1971 }
00867663 1972 if (fds[i] != 0)
3e170ce0
A
1973 (void) OSAddAtomic(-1, &unp_rights);
1974 }
1975
1976discard:
00867663
A
1977 if (fileproc_l != NULL)
1978 FREE(fileproc_l, M_TEMP);
3e170ce0
A
1979 if (error) {
1980 for (i = 0; i < newfds; i++) {
1981 unp_discard(*rp, p);
1982 *rp++ = NULL;
1983 }
1984 }
1985 return (error);
1c79356b
A
1986}
1987
1988void
1989unp_init(void)
1990{
3e170ce0 1991 _CASSERT(UIPC_MAX_CMSG_FD >= (MCLBYTES / sizeof(int)));
2d21ac55
A
1992 unp_zone = zinit(sizeof (struct unpcb),
1993 (nmbclusters * sizeof (struct unpcb)), 4096, "unpzone");
1994
1c79356b
A
1995 if (unp_zone == 0)
1996 panic("unp_init");
1997 LIST_INIT(&unp_dhead);
1998 LIST_INIT(&unp_shead);
2d21ac55 1999
37839358
A
2000 /*
2001 * allocate lock group attribute and group for udp pcb mutexes
2002 */
2003 unp_mtx_grp_attr = lck_grp_attr_alloc_init();
2004
2005 unp_mtx_grp = lck_grp_alloc_init("unp_list", unp_mtx_grp_attr);
2d21ac55 2006
37839358
A
2007 unp_mtx_attr = lck_attr_alloc_init();
2008
2d21ac55
A
2009 if ((unp_list_mtx = lck_rw_alloc_init(unp_mtx_grp,
2010 unp_mtx_attr)) == NULL)
37839358
A
2011 return; /* pretty much dead if this fails... */
2012
b0d623f7
A
2013 if ((unp_disconnect_lock = lck_mtx_alloc_init(unp_mtx_grp,
2014 unp_mtx_attr)) == NULL)
2015 return;
2016
2017 if ((unp_connect_lock = lck_mtx_alloc_init(unp_mtx_grp,
2018 unp_mtx_attr)) == NULL)
2019 return;
1c79356b
A
2020}
2021
2022#ifndef MIN
2d21ac55 2023#define MIN(a, b) (((a) < (b)) ? (a) : (b))
1c79356b
A
2024#endif
2025
2d21ac55
A
2026/*
2027 * Returns: 0 Success
2028 * EINVAL
2029 * fdgetf_noref:EBADF
2030 */
1c79356b 2031static int
2d21ac55 2032unp_internalize(struct mbuf *control, proc_t p)
1c79356b 2033{
91447636 2034 struct cmsghdr *cm = mtod(control, struct cmsghdr *);
b0d623f7 2035 int *fds;
91447636
A
2036 struct fileglob **rp;
2037 struct fileproc *fp;
2d21ac55 2038 int i, error;
1c79356b 2039 int oldfds;
3e170ce0 2040 uint8_t fg_ins[UIPC_MAX_CMSG_FD / 8];
1c79356b 2041
2d21ac55 2042 /* 64bit: cmsg_len is 'uint32_t', m_len is 'long' */
1c79356b 2043 if (cm->cmsg_type != SCM_RIGHTS || cm->cmsg_level != SOL_SOCKET ||
b0d623f7 2044 (socklen_t)cm->cmsg_len != (socklen_t)control->m_len) {
2d21ac55 2045 return (EINVAL);
1c79356b 2046 }
1c79356b 2047 oldfds = (cm->cmsg_len - sizeof (*cm)) / sizeof (int);
3e170ce0 2048 bzero(fg_ins, sizeof(fg_ins));
1c79356b 2049
91447636 2050 proc_fdlock(p);
b0d623f7 2051 fds = (int *)(cm + 1);
91447636
A
2052
2053 for (i = 0; i < oldfds; i++) {
b0d623f7
A
2054 struct fileproc *tmpfp;
2055 if (((error = fdgetf_noref(p, fds[i], &tmpfp)) != 0)) {
2d21ac55
A
2056 proc_fdunlock(p);
2057 return (error);
3e170ce0 2058 } else if (!file_issendable(p, tmpfp)) {
b0d623f7
A
2059 proc_fdunlock(p);
2060 return (EINVAL);
39236c6e
A
2061 } else if (FP_ISGUARDED(tmpfp, GUARD_SOCKET_IPC)) {
2062 error = fp_guard_exception(p,
2063 fds[i], tmpfp, kGUARD_EXC_SOCKET_IPC);
2064 proc_fdunlock(p);
2065 return (error);
2d21ac55 2066 }
91447636
A
2067 }
2068 rp = (struct fileglob **)(cm + 1);
1c79356b 2069
b0d623f7
A
2070 /* On K64 we need to walk backwards because a fileglob * is twice the size of an fd
2071 * and doing them in-order would result in stomping over unprocessed fd's
2072 */
2073 for (i = (oldfds - 1); i >= 0; i--) {
2074 (void) fdgetf_noref(p, fds[i], &fp);
3e170ce0
A
2075 if (fg_insertuipc_mark(fp->f_fglob))
2076 fg_ins[i / 8] |= 0x80 >> (i % 8);
b0d623f7 2077 rp[i] = fp->f_fglob;
1c79356b 2078 }
91447636 2079 proc_fdunlock(p);
1c79356b 2080
3e170ce0
A
2081 for (i = 0; i < oldfds; i++) {
2082 if (fg_ins[i / 8] & (0x80 >> (i % 8))) {
2083 VERIFY(rp[i]->fg_lflags & FG_INSMSGQ);
2084 fg_insertuipc(rp[i]);
2085 }
2086 (void) OSAddAtomic(1, &unp_rights);
2087 }
2088
1c79356b
A
2089 return (0);
2090}
2091
6601e61a 2092static int unp_defer, unp_gcing, unp_gcwait;
e2fac8b1 2093static thread_t unp_gcthread = NULL;
2d21ac55 2094
6601e61a
A
2095/* always called under uipc_lock */
2096void
2097unp_gc_wait(void)
2098{
e2fac8b1
A
2099 if (unp_gcthread == current_thread())
2100 return;
2101
6601e61a
A
2102 while (unp_gcing != 0) {
2103 unp_gcwait = 1;
2104 msleep(&unp_gcing, uipc_lock, 0 , "unp_gc_wait", NULL);
2105 }
2106}
1c79356b 2107
2d21ac55 2108
e2fac8b1 2109__private_extern__ void
2d21ac55 2110unp_gc(void)
1c79356b 2111{
2d21ac55
A
2112 struct fileglob *fg, *nextfg;
2113 struct socket *so;
e2fac8b1 2114 static struct fileglob **extra_ref;
b0d623f7 2115 struct fileglob **fpp;
1c79356b 2116 int nunref, i;
6601e61a 2117 int need_gcwakeup = 0;
2d21ac55 2118
91447636
A
2119 lck_mtx_lock(uipc_lock);
2120 if (unp_gcing) {
2121 lck_mtx_unlock(uipc_lock);
1c79356b 2122 return;
91447636 2123 }
1c79356b
A
2124 unp_gcing = 1;
2125 unp_defer = 0;
e2fac8b1 2126 unp_gcthread = current_thread();
91447636 2127 lck_mtx_unlock(uipc_lock);
2d21ac55
A
2128 /*
2129 * before going through all this, set all FDs to
1c79356b
A
2130 * be NOT defered and NOT externally accessible
2131 */
91447636
A
2132 for (fg = fmsghead.lh_first; fg != 0; fg = fg->f_msglist.le_next) {
2133 lck_mtx_lock(&fg->fg_lock);
2134 fg->fg_flag &= ~(FMARK|FDEFER);
2135 lck_mtx_unlock(&fg->fg_lock);
2136 }
1c79356b 2137 do {
2d21ac55
A
2138 for (fg = fmsghead.lh_first; fg != 0;
2139 fg = fg->f_msglist.le_next) {
91447636 2140 lck_mtx_lock(&fg->fg_lock);
1c79356b
A
2141 /*
2142 * If the file is not open, skip it
2143 */
91447636
A
2144 if (fg->fg_count == 0) {
2145 lck_mtx_unlock(&fg->fg_lock);
1c79356b 2146 continue;
91447636 2147 }
1c79356b
A
2148 /*
2149 * If we already marked it as 'defer' in a
2150 * previous pass, then try process it this time
2151 * and un-mark it
2152 */
91447636
A
2153 if (fg->fg_flag & FDEFER) {
2154 fg->fg_flag &= ~FDEFER;
1c79356b
A
2155 unp_defer--;
2156 } else {
2157 /*
2158 * if it's not defered, then check if it's
2159 * already marked.. if so skip it
2160 */
2d21ac55 2161 if (fg->fg_flag & FMARK) {
91447636 2162 lck_mtx_unlock(&fg->fg_lock);
1c79356b 2163 continue;
91447636 2164 }
2d21ac55 2165 /*
1c79356b 2166 * If all references are from messages
2d21ac55 2167 * in transit, then skip it. it's not
1c79356b 2168 * externally accessible.
2d21ac55 2169 */
91447636
A
2170 if (fg->fg_count == fg->fg_msgcount) {
2171 lck_mtx_unlock(&fg->fg_lock);
1c79356b 2172 continue;
91447636 2173 }
2d21ac55 2174 /*
1c79356b
A
2175 * If it got this far then it must be
2176 * externally accessible.
2177 */
91447636 2178 fg->fg_flag |= FMARK;
1c79356b
A
2179 }
2180 /*
2d21ac55 2181 * either it was defered, or it is externally
1c79356b
A
2182 * accessible and not already marked so.
2183 * Now check if it is possibly one of OUR sockets.
2d21ac55 2184 */
39236c6e 2185 if (FILEGLOB_DTYPE(fg) != DTYPE_SOCKET ||
91447636
A
2186 (so = (struct socket *)fg->fg_data) == 0) {
2187 lck_mtx_unlock(&fg->fg_lock);
1c79356b 2188 continue;
91447636 2189 }
39236c6e 2190 if (so->so_proto->pr_domain != localdomain ||
91447636
A
2191 (so->so_proto->pr_flags&PR_RIGHTS) == 0) {
2192 lck_mtx_unlock(&fg->fg_lock);
1c79356b 2193 continue;
91447636 2194 }
1c79356b 2195#ifdef notdef
1c79356b
A
2196 if (so->so_rcv.sb_flags & SB_LOCK) {
2197 /*
2198 * This is problematical; it's not clear
2199 * we need to wait for the sockbuf to be
2200 * unlocked (on a uniprocessor, at least),
2201 * and it's also not clear what to do
2202 * if sbwait returns an error due to receipt
2203 * of a signal. If sbwait does return
2204 * an error, we'll go into an infinite
2205 * loop. Delete all of this for now.
2206 */
2207 (void) sbwait(&so->so_rcv);
2208 goto restart;
2209 }
2210#endif
2211 /*
2212 * So, Ok, it's one of our sockets and it IS externally
2213 * accessible (or was defered). Now we look
2214 * to see if we hold any file descriptors in its
2d21ac55 2215 * message buffers. Follow those links and mark them
1c79356b 2216 * as accessible too.
e2fac8b1 2217 *
b0d623f7 2218 * In case a file is passed onto itself we need to
e2fac8b1 2219 * release the file lock.
1c79356b 2220 */
91447636 2221 lck_mtx_unlock(&fg->fg_lock);
e2fac8b1 2222
3e170ce0 2223 unp_scan(so->so_rcv.sb_mb, unp_mark, 0);
1c79356b
A
2224 }
2225 } while (unp_defer);
2226 /*
2227 * We grab an extra reference to each of the file table entries
2228 * that are not otherwise accessible and then free the rights
2229 * that are stored in messages on them.
2230 *
2231 * The bug in the orginal code is a little tricky, so I'll describe
2232 * what's wrong with it here.
2233 *
2234 * It is incorrect to simply unp_discard each entry for f_msgcount
2235 * times -- consider the case of sockets A and B that contain
2236 * references to each other. On a last close of some other socket,
2237 * we trigger a gc since the number of outstanding rights (unp_rights)
2238 * is non-zero. If during the sweep phase the gc code un_discards,
2239 * we end up doing a (full) closef on the descriptor. A closef on A
2240 * results in the following chain. Closef calls soo_close, which
2241 * calls soclose. Soclose calls first (through the switch
2242 * uipc_usrreq) unp_detach, which re-invokes unp_gc. Unp_gc simply
2243 * returns because the previous instance had set unp_gcing, and
2244 * we return all the way back to soclose, which marks the socket
2245 * with SS_NOFDREF, and then calls sofree. Sofree calls sorflush
2246 * to free up the rights that are queued in messages on the socket A,
2247 * i.e., the reference on B. The sorflush calls via the dom_dispose
2248 * switch unp_dispose, which unp_scans with unp_discard. This second
2249 * instance of unp_discard just calls closef on B.
2250 *
2251 * Well, a similar chain occurs on B, resulting in a sorflush on B,
2252 * which results in another closef on A. Unfortunately, A is already
2253 * being closed, and the descriptor has already been marked with
2254 * SS_NOFDREF, and soclose panics at this point.
2255 *
2256 * Here, we first take an extra reference to each inaccessible
2257 * descriptor. Then, we call sorflush ourself, since we know
2258 * it is a Unix domain socket anyhow. After we destroy all the
2259 * rights carried in messages, we do a last closef to get rid
2260 * of our extra reference. This is the last close, and the
2261 * unp_detach etc will shut down the socket.
2262 *
2263 * 91/09/19, bsy@cs.cmu.edu
2264 */
2d21ac55
A
2265 extra_ref = _MALLOC(nfiles * sizeof (struct fileglob *),
2266 M_FILEGLOB, M_WAITOK);
b0d623f7
A
2267 if (extra_ref == NULL)
2268 goto bail;
91447636
A
2269 for (nunref = 0, fg = fmsghead.lh_first, fpp = extra_ref; fg != 0;
2270 fg = nextfg) {
2271 lck_mtx_lock(&fg->fg_lock);
2272
2273 nextfg = fg->f_msglist.le_next;
2d21ac55 2274 /*
1c79356b
A
2275 * If it's not open, skip it
2276 */
91447636
A
2277 if (fg->fg_count == 0) {
2278 lck_mtx_unlock(&fg->fg_lock);
1c79356b 2279 continue;
91447636 2280 }
2d21ac55 2281 /*
1c79356b
A
2282 * If all refs are from msgs, and it's not marked accessible
2283 * then it must be referenced from some unreachable cycle
2284 * of (shut-down) FDs, so include it in our
2285 * list of FDs to remove
2286 */
91447636
A
2287 if (fg->fg_count == fg->fg_msgcount && !(fg->fg_flag & FMARK)) {
2288 fg->fg_count++;
2289 *fpp++ = fg;
1c79356b 2290 nunref++;
1c79356b 2291 }
91447636 2292 lck_mtx_unlock(&fg->fg_lock);
1c79356b 2293 }
2d21ac55 2294 /*
1c79356b
A
2295 * for each FD on our hit list, do the following two things
2296 */
2297 for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) {
91447636 2298 struct fileglob *tfg;
1c79356b 2299
91447636 2300 tfg = *fpp;
1c79356b 2301
39236c6e
A
2302 if (FILEGLOB_DTYPE(tfg) == DTYPE_SOCKET &&
2303 tfg->fg_data != NULL) {
2d21ac55
A
2304 so = (struct socket *)(tfg->fg_data);
2305
e2fac8b1 2306 socket_lock(so, 0);
b0d623f7 2307
2d21ac55
A
2308 sorflush(so);
2309
e2fac8b1 2310 socket_unlock(so, 0);
91447636
A
2311 }
2312 }
1c79356b 2313 for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp)
2d21ac55
A
2314 closef_locked((struct fileproc *)0, *fpp, (proc_t)NULL);
2315
b0d623f7
A
2316 FREE((caddr_t)extra_ref, M_FILEGLOB);
2317bail:
2d21ac55 2318 lck_mtx_lock(uipc_lock);
1c79356b 2319 unp_gcing = 0;
e2fac8b1 2320 unp_gcthread = NULL;
6601e61a
A
2321
2322 if (unp_gcwait != 0) {
2323 unp_gcwait = 0;
2324 need_gcwakeup = 1;
2325 }
2326 lck_mtx_unlock(uipc_lock);
2327
2328 if (need_gcwakeup != 0)
2329 wakeup(&unp_gcing);
1c79356b
A
2330}
2331
2332void
91447636 2333unp_dispose(struct mbuf *m)
1c79356b 2334{
1c79356b 2335 if (m) {
3e170ce0 2336 unp_scan(m, unp_discard, NULL);
1c79356b
A
2337 }
2338}
2339
2d21ac55
A
2340/*
2341 * Returns: 0 Success
2342 */
91447636 2343static int
2d21ac55 2344unp_listen(struct unpcb *unp, proc_t p)
91447636 2345{
0c530ab8
A
2346 kauth_cred_t safecred = kauth_cred_proc_ref(p);
2347 cru2x(safecred, &unp->unp_peercred);
2348 kauth_cred_unref(&safecred);
91447636
A
2349 unp->unp_flags |= UNP_HAVEPCCACHED;
2350 return (0);
2351}
2352
1c79356b 2353static void
3e170ce0 2354unp_scan(struct mbuf *m0, void (*op)(struct fileglob *, void *arg), void *arg)
1c79356b 2355{
91447636
A
2356 struct mbuf *m;
2357 struct fileglob **rp;
2358 struct cmsghdr *cm;
2359 int i;
1c79356b
A
2360 int qfds;
2361
2362 while (m0) {
2363 for (m = m0; m; m = m->m_next)
2364 if (m->m_type == MT_CONTROL &&
2d21ac55 2365 (size_t)m->m_len >= sizeof (*cm)) {
1c79356b
A
2366 cm = mtod(m, struct cmsghdr *);
2367 if (cm->cmsg_level != SOL_SOCKET ||
2368 cm->cmsg_type != SCM_RIGHTS)
2369 continue;
2d21ac55 2370 qfds = (cm->cmsg_len - sizeof (*cm)) /
b0d623f7 2371 sizeof (int);
91447636 2372 rp = (struct fileglob **)(cm + 1);
1c79356b 2373 for (i = 0; i < qfds; i++)
3e170ce0 2374 (*op)(*rp++, arg);
1c79356b
A
2375 break; /* XXX, but saves time */
2376 }
2377 m0 = m0->m_act;
2378 }
2379}
2380
1c79356b 2381static void
3e170ce0 2382unp_mark(struct fileglob *fg, __unused void *arg)
1c79356b 2383{
2d21ac55 2384 lck_mtx_lock(&fg->fg_lock);
1c79356b 2385
91447636 2386 if (fg->fg_flag & FMARK) {
2d21ac55 2387 lck_mtx_unlock(&fg->fg_lock);
1c79356b 2388 return;
91447636
A
2389 }
2390 fg->fg_flag |= (FMARK|FDEFER);
2391
2d21ac55 2392 lck_mtx_unlock(&fg->fg_lock);
91447636 2393
1c79356b 2394 unp_defer++;
1c79356b
A
2395}
2396
1c79356b 2397static void
3e170ce0 2398unp_discard(struct fileglob *fg, void *p)
1c79356b 2399{
3e170ce0
A
2400 if (p == NULL)
2401 p = current_proc(); /* XXX */
2d21ac55 2402
b0d623f7 2403 (void) OSAddAtomic(1, &unp_disposed);
3e170ce0
A
2404 if (fg_removeuipc_mark(fg)) {
2405 VERIFY(fg->fg_lflags & FG_RMMSGQ);
2406 fg_removeuipc(fg);
2407 }
2408 (void) OSAddAtomic(-1, &unp_rights);
91447636
A
2409
2410 proc_fdlock(p);
91447636 2411 (void) closef_locked((struct fileproc *)0, fg, p);
3e170ce0 2412 proc_fdunlock(p);
1c79356b 2413}
b0d623f7
A
2414
2415int
2416unp_lock(struct socket *so, int refcount, void * lr)
2417 {
2418 void * lr_saved;
2419 if (lr == 0)
2420 lr_saved = (void *) __builtin_return_address(0);
2421 else lr_saved = lr;
2422
2423 if (so->so_pcb) {
6d2010ae 2424 lck_mtx_lock(&((struct unpcb *)so->so_pcb)->unp_mtx);
b0d623f7
A
2425 } else {
2426 panic("unp_lock: so=%p NO PCB! lr=%p ref=0x%x\n",
2427 so, lr_saved, so->so_usecount);
2428 }
2429
2430 if (so->so_usecount < 0)
2431 panic("unp_lock: so=%p so_pcb=%p lr=%p ref=0x%x\n",
2432 so, so->so_pcb, lr_saved, so->so_usecount);
2433
d190cdc3
A
2434 if (refcount) {
2435 VERIFY(so->so_usecount > 0);
2436 so->so_usecount++;
2437 }
b0d623f7
A
2438 so->lock_lr[so->next_lock_lr] = lr_saved;
2439 so->next_lock_lr = (so->next_lock_lr+1) % SO_LCKDBG_MAX;
2440 return (0);
2441}
2442
2443int
2444unp_unlock(struct socket *so, int refcount, void * lr)
2445{
2446 void * lr_saved;
2447 lck_mtx_t * mutex_held = NULL;
2448 struct unpcb *unp = sotounpcb(so);
2449
2450 if (lr == 0)
2451 lr_saved = (void *) __builtin_return_address(0);
2452 else lr_saved = lr;
2453
2454 if (refcount)
2455 so->so_usecount--;
2456
2457 if (so->so_usecount < 0)
2458 panic("unp_unlock: so=%p usecount=%x\n", so, so->so_usecount);
2459 if (so->so_pcb == NULL) {
2460 panic("unp_unlock: so=%p NO PCB usecount=%x\n", so, so->so_usecount);
2461 } else {
6d2010ae 2462 mutex_held = &((struct unpcb *)so->so_pcb)->unp_mtx;
b0d623f7
A
2463 }
2464 lck_mtx_assert(mutex_held, LCK_MTX_ASSERT_OWNED);
2465 so->unlock_lr[so->next_unlock_lr] = lr_saved;
2466 so->next_unlock_lr = (so->next_unlock_lr+1) % SO_LCKDBG_MAX;
2467
2468 if (so->so_usecount == 0 && (so->so_flags & SOF_PCBCLEARING)) {
2469 sofreelastref(so, 1);
2470
2471 if (unp->unp_addr)
2472 FREE(unp->unp_addr, M_SONAME);
2473
2474 lck_mtx_unlock(mutex_held);
b0d623f7 2475
316670eb 2476 lck_mtx_destroy(&unp->unp_mtx, unp_mtx_grp);
b0d623f7 2477 zfree(unp_zone, unp);
b0d623f7
A
2478
2479 unp_gc();
2480 } else {
2481 lck_mtx_unlock(mutex_held);
2482 }
2483
2484 return (0);
2485}
2486
2487lck_mtx_t *
2488unp_getlock(struct socket *so, __unused int locktype)
2489{
2490 struct unpcb *unp = (struct unpcb *)so->so_pcb;
2491
2492
2493 if (so->so_pcb) {
2494 if (so->so_usecount < 0)
2495 panic("unp_getlock: so=%p usecount=%x\n", so, so->so_usecount);
6d2010ae 2496 return(&unp->unp_mtx);
b0d623f7
A
2497 } else {
2498 panic("unp_getlock: so=%p NULL so_pcb\n", so);
2499 return (so->so_proto->pr_domain->dom_mtx);
2500 }
2501}
2502