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