2 * Copyright (c) 1999-2017 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
30 * Kernel Control domain - allows control connections to
31 * and to read/write data.
33 * Vincent Lubet, 040506
34 * Christophe Allie, 010928
35 * Justin C. Walker, 990319
38 #include <sys/types.h>
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/syslog.h>
42 #include <sys/socket.h>
43 #include <sys/socketvar.h>
44 #include <sys/protosw.h>
45 #include <sys/domain.h>
46 #include <sys/malloc.h>
48 #include <sys/sys_domain.h>
49 #include <sys/kern_event.h>
50 #include <sys/kern_control.h>
51 #include <sys/kauth.h>
52 #include <sys/sysctl.h>
53 #include <sys/proc_info.h>
54 #include <net/if_var.h>
56 #include <mach/vm_types.h>
58 #include <kern/thread.h>
61 TAILQ_ENTRY(kctl
) next
; /* controller chain */
64 /* controller information provided when registering */
65 char name
[MAX_KCTL_NAME
]; /* unique identifier */
69 /* misc communication information */
70 u_int32_t flags
; /* support flags */
71 u_int32_t recvbufsize
; /* request more than the default buffer size */
72 u_int32_t sendbufsize
; /* request more than the default buffer size */
74 /* Dispatch functions */
75 ctl_bind_func bind
; /* Prepare contact */
76 ctl_connect_func connect
; /* Make contact */
77 ctl_disconnect_func disconnect
; /* Break contact */
78 ctl_send_func send
; /* Send data to nke */
79 ctl_send_list_func send_list
; /* Send list of packets */
80 ctl_setopt_func setopt
; /* set kctl configuration */
81 ctl_getopt_func getopt
; /* get kctl configuration */
82 ctl_rcvd_func rcvd
; /* Notify nke when client reads data */
84 TAILQ_HEAD(, ctl_cb
) kcb_head
;
88 #if DEVELOPMENT || DEBUG
90 KCTL_DISCONNECTED
= 0,
94 #endif /* DEVELOPMENT || DEBUG */
97 TAILQ_ENTRY(ctl_cb
) next
; /* controller chain */
99 struct socket
*so
; /* controlling socket */
100 struct kctl
*kctl
; /* back pointer to controller */
102 struct sockaddr_ctl sac
;
104 u_int32_t kcb_usecount
;
105 u_int32_t require_clearing_count
;
106 #if DEVELOPMENT || DEBUG
107 enum ctl_status status
;
108 #endif /* DEVELOPMENT || DEBUG */
112 #define ROUNDUP64(x) P2ROUNDUP((x), sizeof (u_int64_t))
116 #define ADVANCE64(p, n) (void*)((char *)(p) + ROUNDUP64(n))
120 * Definitions and vars for we support
123 #define CTL_SENDSIZE (2 * 1024) /* default buffer size */
124 #define CTL_RECVSIZE (8 * 1024) /* default buffer size */
127 * Definitions and vars for we support
130 static u_int32_t ctl_maxunit
= 65536;
131 static lck_grp_attr_t
*ctl_lck_grp_attr
= 0;
132 static lck_attr_t
*ctl_lck_attr
= 0;
133 static lck_grp_t
*ctl_lck_grp
= 0;
134 static lck_mtx_t
*ctl_mtx
;
136 /* all the controllers are chained */
137 TAILQ_HEAD(kctl_list
, kctl
) ctl_head
;
139 static int ctl_attach(struct socket
*, int, struct proc
*);
140 static int ctl_detach(struct socket
*);
141 static int ctl_sofreelastref(struct socket
*so
);
142 static int ctl_bind(struct socket
*, struct sockaddr
*, struct proc
*);
143 static int ctl_connect(struct socket
*, struct sockaddr
*, struct proc
*);
144 static int ctl_disconnect(struct socket
*);
145 static int ctl_ioctl(struct socket
*so
, u_long cmd
, caddr_t data
,
146 struct ifnet
*ifp
, struct proc
*p
);
147 static int ctl_send(struct socket
*, int, struct mbuf
*,
148 struct sockaddr
*, struct mbuf
*, struct proc
*);
149 static int ctl_send_list(struct socket
*, int, struct mbuf
*,
150 struct sockaddr
*, struct mbuf
*, struct proc
*);
151 static int ctl_ctloutput(struct socket
*, struct sockopt
*);
152 static int ctl_peeraddr(struct socket
*so
, struct sockaddr
**nam
);
153 static int ctl_usr_rcvd(struct socket
*so
, int flags
);
155 static struct kctl
*ctl_find_by_name(const char *);
156 static struct kctl
*ctl_find_by_id_unit(u_int32_t id
, u_int32_t unit
);
158 static struct socket
*kcb_find_socket(kern_ctl_ref kctlref
, u_int32_t unit
,
160 static struct ctl_cb
*kcb_find(struct kctl
*, u_int32_t unit
);
161 static void ctl_post_msg(u_int32_t event_code
, u_int32_t id
);
163 static int ctl_lock(struct socket
*, int, void *);
164 static int ctl_unlock(struct socket
*, int, void *);
165 static lck_mtx_t
* ctl_getlock(struct socket
*, int);
167 static struct pr_usrreqs ctl_usrreqs
= {
168 .pru_attach
= ctl_attach
,
169 .pru_bind
= ctl_bind
,
170 .pru_connect
= ctl_connect
,
171 .pru_control
= ctl_ioctl
,
172 .pru_detach
= ctl_detach
,
173 .pru_disconnect
= ctl_disconnect
,
174 .pru_peeraddr
= ctl_peeraddr
,
175 .pru_rcvd
= ctl_usr_rcvd
,
176 .pru_send
= ctl_send
,
177 .pru_send_list
= ctl_send_list
,
178 .pru_sosend
= sosend
,
179 .pru_sosend_list
= sosend_list
,
180 .pru_soreceive
= soreceive
,
181 .pru_soreceive_list
= soreceive_list
,
184 static struct protosw kctlsw
[] = {
186 .pr_type
= SOCK_DGRAM
,
187 .pr_protocol
= SYSPROTO_CONTROL
,
188 .pr_flags
= PR_ATOMIC
| PR_CONNREQUIRED
| PR_PCBLOCK
| PR_WANTRCVD
,
189 .pr_ctloutput
= ctl_ctloutput
,
190 .pr_usrreqs
= &ctl_usrreqs
,
192 .pr_unlock
= ctl_unlock
,
193 .pr_getlock
= ctl_getlock
,
196 .pr_type
= SOCK_STREAM
,
197 .pr_protocol
= SYSPROTO_CONTROL
,
198 .pr_flags
= PR_CONNREQUIRED
| PR_PCBLOCK
| PR_WANTRCVD
,
199 .pr_ctloutput
= ctl_ctloutput
,
200 .pr_usrreqs
= &ctl_usrreqs
,
202 .pr_unlock
= ctl_unlock
,
203 .pr_getlock
= ctl_getlock
,
207 __private_extern__
int kctl_reg_list SYSCTL_HANDLER_ARGS
;
208 __private_extern__
int kctl_pcblist SYSCTL_HANDLER_ARGS
;
209 __private_extern__
int kctl_getstat SYSCTL_HANDLER_ARGS
;
212 SYSCTL_NODE(_net_systm
, OID_AUTO
, kctl
,
213 CTLFLAG_RW
| CTLFLAG_LOCKED
, 0, "Kernel control family");
215 struct kctlstat kctlstat
;
216 SYSCTL_PROC(_net_systm_kctl
, OID_AUTO
, stats
,
217 CTLTYPE_STRUCT
| CTLFLAG_RD
| CTLFLAG_LOCKED
, 0, 0,
218 kctl_getstat
, "S,kctlstat", "");
220 SYSCTL_PROC(_net_systm_kctl
, OID_AUTO
, reg_list
,
221 CTLTYPE_STRUCT
| CTLFLAG_RD
| CTLFLAG_LOCKED
, 0, 0,
222 kctl_reg_list
, "S,xkctl_reg", "");
224 SYSCTL_PROC(_net_systm_kctl
, OID_AUTO
, pcblist
,
225 CTLTYPE_STRUCT
| CTLFLAG_RD
| CTLFLAG_LOCKED
, 0, 0,
226 kctl_pcblist
, "S,xkctlpcb", "");
228 u_int32_t ctl_autorcvbuf_max
= 256 * 1024;
229 SYSCTL_INT(_net_systm_kctl
, OID_AUTO
, autorcvbufmax
,
230 CTLFLAG_RW
| CTLFLAG_LOCKED
, &ctl_autorcvbuf_max
, 0, "");
232 u_int32_t ctl_autorcvbuf_high
= 0;
233 SYSCTL_INT(_net_systm_kctl
, OID_AUTO
, autorcvbufhigh
,
234 CTLFLAG_RD
| CTLFLAG_LOCKED
, &ctl_autorcvbuf_high
, 0, "");
236 u_int32_t ctl_debug
= 0;
237 SYSCTL_INT(_net_systm_kctl
, OID_AUTO
, debug
,
238 CTLFLAG_RW
| CTLFLAG_LOCKED
, &ctl_debug
, 0, "");
240 #if DEVELOPMENT || DEBUG
241 u_int32_t ctl_panic_debug
= 0;
242 SYSCTL_INT(_net_systm_kctl
, OID_AUTO
, panicdebug
,
243 CTLFLAG_RW
| CTLFLAG_LOCKED
, &ctl_panic_debug
, 0, "");
244 #endif /* DEVELOPMENT || DEBUG */
246 #define KCTL_TBL_INC 16
248 static uintptr_t kctl_tbl_size
= 0;
249 static u_int32_t kctl_tbl_growing
= 0;
250 static u_int32_t kctl_tbl_growing_waiting
= 0;
251 static uintptr_t kctl_tbl_count
= 0;
252 static struct kctl
**kctl_table
= NULL
;
253 static uintptr_t kctl_ref_gencnt
= 0;
255 static void kctl_tbl_grow(void);
256 static kern_ctl_ref
kctl_make_ref(struct kctl
*kctl
);
257 static void kctl_delete_ref(kern_ctl_ref
);
258 static struct kctl
*kctl_from_ref(kern_ctl_ref
);
261 * Install the protosw's for the Kernel Control manager.
263 __private_extern__
void
264 kern_control_init(struct domain
*dp
)
268 int kctl_proto_count
= (sizeof(kctlsw
) / sizeof(struct protosw
));
270 VERIFY(!(dp
->dom_flags
& DOM_INITIALIZED
));
271 VERIFY(dp
== systemdomain
);
273 ctl_lck_grp_attr
= lck_grp_attr_alloc_init();
274 if (ctl_lck_grp_attr
== NULL
) {
275 panic("%s: lck_grp_attr_alloc_init failed\n", __func__
);
279 ctl_lck_grp
= lck_grp_alloc_init("Kernel Control Protocol",
281 if (ctl_lck_grp
== NULL
) {
282 panic("%s: lck_grp_alloc_init failed\n", __func__
);
286 ctl_lck_attr
= lck_attr_alloc_init();
287 if (ctl_lck_attr
== NULL
) {
288 panic("%s: lck_attr_alloc_init failed\n", __func__
);
292 ctl_mtx
= lck_mtx_alloc_init(ctl_lck_grp
, ctl_lck_attr
);
293 if (ctl_mtx
== NULL
) {
294 panic("%s: lck_mtx_alloc_init failed\n", __func__
);
297 TAILQ_INIT(&ctl_head
);
299 for (i
= 0, pr
= &kctlsw
[0]; i
< kctl_proto_count
; i
++, pr
++) {
300 net_add_proto(pr
, dp
, 1);
305 kcb_delete(struct ctl_cb
*kcb
)
309 lck_mtx_free(kcb
->mtx
, ctl_lck_grp
);
316 * Kernel Controller user-request functions
317 * attach function must exist and succeed
318 * detach not necessary
319 * we need a pcb for the per socket mutex
322 ctl_attach(struct socket
*so
, int proto
, struct proc
*p
)
324 #pragma unused(proto, p)
326 struct ctl_cb
*kcb
= 0;
328 MALLOC(kcb
, struct ctl_cb
*, sizeof(struct ctl_cb
), M_TEMP
, M_WAITOK
);
333 bzero(kcb
, sizeof(struct ctl_cb
));
335 kcb
->mtx
= lck_mtx_alloc_init(ctl_lck_grp
, ctl_lck_attr
);
336 if (kcb
->mtx
== NULL
) {
341 so
->so_pcb
= (caddr_t
)kcb
;
352 ctl_sofreelastref(struct socket
*so
)
354 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
360 if ((kctl
= kcb
->kctl
) != 0) {
361 lck_mtx_lock(ctl_mtx
);
362 TAILQ_REMOVE(&kctl
->kcb_head
, kcb
, next
);
363 kctlstat
.kcs_pcbcount
--;
364 kctlstat
.kcs_gencnt
++;
365 lck_mtx_unlock(ctl_mtx
);
369 sofreelastref(so
, 1);
374 * Use this function and ctl_kcb_require_clearing to serialize
375 * critical calls into the kctl subsystem
378 ctl_kcb_increment_use_count(struct ctl_cb
*kcb
, lck_mtx_t
*mutex_held
)
380 LCK_MTX_ASSERT(mutex_held
, LCK_MTX_ASSERT_OWNED
);
381 while (kcb
->require_clearing_count
> 0) {
382 msleep(&kcb
->require_clearing_count
, mutex_held
, PSOCK
| PCATCH
, "kcb_require_clearing", NULL
);
388 ctl_kcb_require_clearing(struct ctl_cb
*kcb
, lck_mtx_t
*mutex_held
)
390 assert(kcb
->kcb_usecount
!= 0);
391 kcb
->require_clearing_count
++;
393 while (kcb
->kcb_usecount
> 0) { // we need to wait until no one else is running
394 msleep(&kcb
->kcb_usecount
, mutex_held
, PSOCK
| PCATCH
, "kcb_usecount", NULL
);
400 ctl_kcb_done_clearing(struct ctl_cb
*kcb
)
402 assert(kcb
->require_clearing_count
!= 0);
403 kcb
->require_clearing_count
--;
404 wakeup((caddr_t
)&kcb
->require_clearing_count
);
408 ctl_kcb_decrement_use_count(struct ctl_cb
*kcb
)
410 assert(kcb
->kcb_usecount
!= 0);
412 wakeup((caddr_t
)&kcb
->kcb_usecount
);
416 ctl_detach(struct socket
*so
)
418 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
424 lck_mtx_t
*mtx_held
= socket_getlock(so
, PR_F_WILLUNLOCK
);
425 ctl_kcb_increment_use_count(kcb
, mtx_held
);
426 ctl_kcb_require_clearing(kcb
, mtx_held
);
428 if (kcb
->kctl
!= NULL
&& kcb
->kctl
->bind
!= NULL
&&
429 kcb
->userdata
!= NULL
&& !(so
->so_state
& SS_ISCONNECTED
)) {
430 // The unit was bound, but not connected
431 // Invoke the disconnected call to cleanup
432 if (kcb
->kctl
->disconnect
!= NULL
) {
433 socket_unlock(so
, 0);
434 (*kcb
->kctl
->disconnect
)(kcb
->kctl
->kctlref
,
435 kcb
->sac
.sc_unit
, kcb
->userdata
);
440 soisdisconnected(so
);
441 #if DEVELOPMENT || DEBUG
442 kcb
->status
= KCTL_DISCONNECTED
;
443 #endif /* DEVELOPMENT || DEBUG */
444 so
->so_flags
|= SOF_PCBCLEARING
;
445 ctl_kcb_done_clearing(kcb
);
446 ctl_kcb_decrement_use_count(kcb
);
451 ctl_setup_kctl(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
453 struct kctl
*kctl
= NULL
;
455 struct sockaddr_ctl sa
;
456 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
457 struct ctl_cb
*kcb_next
= NULL
;
459 u_int32_t recvbufsize
, sendbufsize
;
462 panic("ctl_setup_kctl so_pcb null\n");
465 if (kcb
->kctl
!= NULL
) {
466 // Already set up, skip
470 if (nam
->sa_len
!= sizeof(struct sockaddr_ctl
)) {
474 bcopy(nam
, &sa
, sizeof(struct sockaddr_ctl
));
476 lck_mtx_lock(ctl_mtx
);
477 kctl
= ctl_find_by_id_unit(sa
.sc_id
, sa
.sc_unit
);
479 lck_mtx_unlock(ctl_mtx
);
483 if (((kctl
->flags
& CTL_FLAG_REG_SOCK_STREAM
) &&
484 (so
->so_type
!= SOCK_STREAM
)) ||
485 (!(kctl
->flags
& CTL_FLAG_REG_SOCK_STREAM
) &&
486 (so
->so_type
!= SOCK_DGRAM
))) {
487 lck_mtx_unlock(ctl_mtx
);
491 if (kctl
->flags
& CTL_FLAG_PRIVILEGED
) {
493 lck_mtx_unlock(ctl_mtx
);
496 if (kauth_cred_issuser(kauth_cred_get()) == 0) {
497 lck_mtx_unlock(ctl_mtx
);
502 if ((kctl
->flags
& CTL_FLAG_REG_ID_UNIT
) || sa
.sc_unit
!= 0) {
503 if (kcb_find(kctl
, sa
.sc_unit
) != NULL
) {
504 lck_mtx_unlock(ctl_mtx
);
508 /* Find an unused ID, assumes control IDs are in order */
511 TAILQ_FOREACH(kcb_next
, &kctl
->kcb_head
, next
) {
512 if (kcb_next
->sac
.sc_unit
> unit
) {
513 /* Found a gap, lets fill it in */
516 unit
= kcb_next
->sac
.sc_unit
+ 1;
517 if (unit
== ctl_maxunit
) {
522 if (unit
== ctl_maxunit
) {
523 lck_mtx_unlock(ctl_mtx
);
530 bcopy(&sa
, &kcb
->sac
, sizeof(struct sockaddr_ctl
));
532 if (kcb_next
!= NULL
) {
533 TAILQ_INSERT_BEFORE(kcb_next
, kcb
, next
);
535 TAILQ_INSERT_TAIL(&kctl
->kcb_head
, kcb
, next
);
537 kctlstat
.kcs_pcbcount
++;
538 kctlstat
.kcs_gencnt
++;
539 kctlstat
.kcs_connections
++;
540 lck_mtx_unlock(ctl_mtx
);
543 * rdar://15526688: Limit the send and receive sizes to sb_max
544 * by using the same scaling as sbreserve()
546 sbmaxsize
= (u_quad_t
)sb_max
* MCLBYTES
/ (MSIZE
+ MCLBYTES
);
548 if (kctl
->sendbufsize
> sbmaxsize
) {
549 sendbufsize
= sbmaxsize
;
551 sendbufsize
= kctl
->sendbufsize
;
554 if (kctl
->recvbufsize
> sbmaxsize
) {
555 recvbufsize
= sbmaxsize
;
557 recvbufsize
= kctl
->recvbufsize
;
560 error
= soreserve(so
, sendbufsize
, recvbufsize
);
563 printf("%s - soreserve(%llx, %u, %u) error %d\n",
564 __func__
, (uint64_t)VM_KERNEL_ADDRPERM(so
),
565 sendbufsize
, recvbufsize
, error
);
572 soisdisconnected(so
);
573 #if DEVELOPMENT || DEBUG
574 kcb
->status
= KCTL_DISCONNECTED
;
575 #endif /* DEVELOPMENT || DEBUG */
576 lck_mtx_lock(ctl_mtx
);
577 TAILQ_REMOVE(&kctl
->kcb_head
, kcb
, next
);
579 kcb
->sac
.sc_unit
= 0;
580 kctlstat
.kcs_pcbcount
--;
581 kctlstat
.kcs_gencnt
++;
582 kctlstat
.kcs_conn_fail
++;
583 lck_mtx_unlock(ctl_mtx
);
589 ctl_bind(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
592 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
595 panic("ctl_bind so_pcb null\n");
598 lck_mtx_t
*mtx_held
= socket_getlock(so
, PR_F_WILLUNLOCK
);
599 ctl_kcb_increment_use_count(kcb
, mtx_held
);
600 ctl_kcb_require_clearing(kcb
, mtx_held
);
602 error
= ctl_setup_kctl(so
, nam
, p
);
607 if (kcb
->kctl
== NULL
) {
608 panic("ctl_bind kctl null\n");
611 if (kcb
->kctl
->bind
== NULL
) {
616 socket_unlock(so
, 0);
617 error
= (*kcb
->kctl
->bind
)(kcb
->kctl
->kctlref
, &kcb
->sac
, &kcb
->userdata
);
621 ctl_kcb_done_clearing(kcb
);
622 ctl_kcb_decrement_use_count(kcb
);
627 ctl_connect(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
630 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
633 panic("ctl_connect so_pcb null\n");
636 lck_mtx_t
*mtx_held
= socket_getlock(so
, PR_F_WILLUNLOCK
);
637 ctl_kcb_increment_use_count(kcb
, mtx_held
);
638 ctl_kcb_require_clearing(kcb
, mtx_held
);
640 #if DEVELOPMENT || DEBUG
641 if (kcb
->status
!= KCTL_DISCONNECTED
&& ctl_panic_debug
) {
642 panic("kctl already connecting/connected");
644 kcb
->status
= KCTL_CONNECTING
;
645 #endif /* DEVELOPMENT || DEBUG */
647 error
= ctl_setup_kctl(so
, nam
, p
);
652 if (kcb
->kctl
== NULL
) {
653 panic("ctl_connect kctl null\n");
657 socket_unlock(so
, 0);
658 error
= (*kcb
->kctl
->connect
)(kcb
->kctl
->kctlref
, &kcb
->sac
, &kcb
->userdata
);
664 #if DEVELOPMENT || DEBUG
665 kcb
->status
= KCTL_CONNECTED
;
666 #endif /* DEVELOPMENT || DEBUG */
669 if (error
&& kcb
->kctl
->disconnect
) {
671 * XXX Make sure we Don't check the return value
672 * of disconnect here.
673 * ipsec/utun_ctl_disconnect will return error when
674 * disconnect gets called after connect failure.
675 * However if we decide to check for disconnect return
676 * value here. Please make sure to revisit
677 * ipsec/utun_ctl_disconnect.
679 socket_unlock(so
, 0);
680 (*kcb
->kctl
->disconnect
)(kcb
->kctl
->kctlref
, kcb
->sac
.sc_unit
, kcb
->userdata
);
684 soisdisconnected(so
);
685 #if DEVELOPMENT || DEBUG
686 kcb
->status
= KCTL_DISCONNECTED
;
687 #endif /* DEVELOPMENT || DEBUG */
688 lck_mtx_lock(ctl_mtx
);
689 TAILQ_REMOVE(&kcb
->kctl
->kcb_head
, kcb
, next
);
691 kcb
->sac
.sc_unit
= 0;
692 kctlstat
.kcs_pcbcount
--;
693 kctlstat
.kcs_gencnt
++;
694 kctlstat
.kcs_conn_fail
++;
695 lck_mtx_unlock(ctl_mtx
);
698 ctl_kcb_done_clearing(kcb
);
699 ctl_kcb_decrement_use_count(kcb
);
704 ctl_disconnect(struct socket
*so
)
706 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
708 if ((kcb
= (struct ctl_cb
*)so
->so_pcb
)) {
709 lck_mtx_t
*mtx_held
= socket_getlock(so
, PR_F_WILLUNLOCK
);
710 ctl_kcb_increment_use_count(kcb
, mtx_held
);
711 ctl_kcb_require_clearing(kcb
, mtx_held
);
712 struct kctl
*kctl
= kcb
->kctl
;
714 if (kctl
&& kctl
->disconnect
) {
715 socket_unlock(so
, 0);
716 (*kctl
->disconnect
)(kctl
->kctlref
, kcb
->sac
.sc_unit
,
721 soisdisconnected(so
);
722 #if DEVELOPMENT || DEBUG
723 kcb
->status
= KCTL_DISCONNECTED
;
724 #endif /* DEVELOPMENT || DEBUG */
726 socket_unlock(so
, 0);
727 lck_mtx_lock(ctl_mtx
);
729 kcb
->sac
.sc_unit
= 0;
730 while (kcb
->usecount
!= 0) {
731 msleep(&kcb
->usecount
, ctl_mtx
, 0, "kcb->usecount", 0);
733 TAILQ_REMOVE(&kctl
->kcb_head
, kcb
, next
);
734 kctlstat
.kcs_pcbcount
--;
735 kctlstat
.kcs_gencnt
++;
736 lck_mtx_unlock(ctl_mtx
);
738 ctl_kcb_done_clearing(kcb
);
739 ctl_kcb_decrement_use_count(kcb
);
745 ctl_peeraddr(struct socket
*so
, struct sockaddr
**nam
)
747 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
749 struct sockaddr_ctl sc
;
751 if (kcb
== NULL
) { /* sanity check */
755 if ((kctl
= kcb
->kctl
) == NULL
) {
759 bzero(&sc
, sizeof(struct sockaddr_ctl
));
760 sc
.sc_len
= sizeof(struct sockaddr_ctl
);
761 sc
.sc_family
= AF_SYSTEM
;
762 sc
.ss_sysaddr
= AF_SYS_CONTROL
;
764 sc
.sc_unit
= kcb
->sac
.sc_unit
;
766 *nam
= dup_sockaddr((struct sockaddr
*)&sc
, 1);
772 ctl_sbrcv_trim(struct socket
*so
)
774 struct sockbuf
*sb
= &so
->so_rcv
;
776 if (sb
->sb_hiwat
> sb
->sb_idealsize
) {
781 * The difference between the ideal size and the
782 * current size is the upper bound of the trimage
784 diff
= sb
->sb_hiwat
- sb
->sb_idealsize
;
786 * We cannot trim below the outstanding data
788 trim
= sb
->sb_hiwat
- sb
->sb_cc
;
790 trim
= imin(trim
, (int32_t)diff
);
793 sbreserve(sb
, (sb
->sb_hiwat
- trim
));
796 printf("%s - shrunk to %d\n",
797 __func__
, sb
->sb_hiwat
);
804 ctl_usr_rcvd(struct socket
*so
, int flags
)
807 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
814 lck_mtx_t
*mtx_held
= socket_getlock(so
, PR_F_WILLUNLOCK
);
815 ctl_kcb_increment_use_count(kcb
, mtx_held
);
817 if ((kctl
= kcb
->kctl
) == NULL
) {
823 socket_unlock(so
, 0);
824 (*kctl
->rcvd
)(kctl
->kctlref
, kcb
->sac
.sc_unit
, kcb
->userdata
, flags
);
831 ctl_kcb_decrement_use_count(kcb
);
836 ctl_send(struct socket
*so
, int flags
, struct mbuf
*m
,
837 struct sockaddr
*addr
, struct mbuf
*control
,
840 #pragma unused(addr, p)
842 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
849 if (kcb
== NULL
) { /* sanity check */
853 lck_mtx_t
*mtx_held
= socket_getlock(so
, PR_F_WILLUNLOCK
);
854 ctl_kcb_increment_use_count(kcb
, mtx_held
);
856 if (error
== 0 && (kctl
= kcb
->kctl
) == NULL
) {
860 if (error
== 0 && kctl
->send
) {
861 so_tc_update_stats(m
, so
, m_get_service_class(m
));
862 socket_unlock(so
, 0);
863 error
= (*kctl
->send
)(kctl
->kctlref
, kcb
->sac
.sc_unit
, kcb
->userdata
,
873 OSIncrementAtomic64((SInt64
*)&kctlstat
.kcs_send_fail
);
875 ctl_kcb_decrement_use_count(kcb
);
881 ctl_send_list(struct socket
*so
, int flags
, struct mbuf
*m
,
882 __unused
struct sockaddr
*addr
, struct mbuf
*control
,
883 __unused
struct proc
*p
)
886 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
890 m_freem_list(control
);
893 if (kcb
== NULL
) { /* sanity check */
897 lck_mtx_t
*mtx_held
= socket_getlock(so
, PR_F_WILLUNLOCK
);
898 ctl_kcb_increment_use_count(kcb
, mtx_held
);
900 if (error
== 0 && (kctl
= kcb
->kctl
) == NULL
) {
904 if (error
== 0 && kctl
->send_list
) {
907 for (nxt
= m
; nxt
!= NULL
; nxt
= nxt
->m_nextpkt
) {
908 so_tc_update_stats(nxt
, so
, m_get_service_class(nxt
));
911 socket_unlock(so
, 0);
912 error
= (*kctl
->send_list
)(kctl
->kctlref
, kcb
->sac
.sc_unit
,
913 kcb
->userdata
, m
, flags
);
915 } else if (error
== 0 && kctl
->send
) {
916 while (m
!= NULL
&& error
== 0) {
917 struct mbuf
*nextpkt
= m
->m_nextpkt
;
920 so_tc_update_stats(m
, so
, m_get_service_class(m
));
921 socket_unlock(so
, 0);
922 error
= (*kctl
->send
)(kctl
->kctlref
, kcb
->sac
.sc_unit
,
923 kcb
->userdata
, m
, flags
);
937 OSIncrementAtomic64((SInt64
*)&kctlstat
.kcs_send_list_fail
);
939 ctl_kcb_decrement_use_count(kcb
);
945 ctl_rcvbspace(struct socket
*so
, u_int32_t datasize
,
946 u_int32_t kctlflags
, u_int32_t flags
)
948 struct sockbuf
*sb
= &so
->so_rcv
;
949 u_int32_t space
= sbspace(sb
);
952 if ((kctlflags
& CTL_FLAG_REG_CRIT
) == 0) {
953 if ((u_int32_t
) space
>= datasize
) {
958 } else if ((flags
& CTL_DATA_CRIT
) == 0) {
960 * Reserve 25% for critical messages
962 if (space
< (sb
->sb_hiwat
>> 2) ||
969 u_int32_t autorcvbuf_max
;
972 * Allow overcommit of 25%
974 autorcvbuf_max
= min(sb
->sb_idealsize
+ (sb
->sb_idealsize
>> 2),
977 if ((u_int32_t
) space
>= datasize
) {
979 } else if (tcp_cansbgrow(sb
) &&
980 sb
->sb_hiwat
< autorcvbuf_max
) {
982 * Grow with a little bit of leeway
984 u_int32_t grow
= datasize
- space
+ MSIZE
;
987 min((sb
->sb_hiwat
+ grow
), autorcvbuf_max
)) == 1) {
988 if (sb
->sb_hiwat
> ctl_autorcvbuf_high
) {
989 ctl_autorcvbuf_high
= sb
->sb_hiwat
;
995 if ((u_int32_t
) sbspace(sb
) >= datasize
) {
1002 printf("%s - grown to %d error %d\n",
1003 __func__
, sb
->sb_hiwat
, error
);
1016 ctl_enqueuembuf(kern_ctl_ref kctlref
, u_int32_t unit
, struct mbuf
*m
,
1021 int len
= m
->m_pkthdr
.len
;
1022 u_int32_t kctlflags
;
1024 so
= kcb_find_socket(kctlref
, unit
, &kctlflags
);
1029 if (ctl_rcvbspace(so
, len
, kctlflags
, flags
) != 0) {
1031 OSIncrementAtomic64((SInt64
*)&kctlstat
.kcs_enqueue_fullsock
);
1034 if ((flags
& CTL_DATA_EOR
)) {
1035 m
->m_flags
|= M_EOR
;
1038 so_recv_data_stat(so
, m
, 0);
1039 if (sbappend(&so
->so_rcv
, m
) != 0) {
1040 if ((flags
& CTL_DATA_NOWAKEUP
) == 0) {
1045 OSIncrementAtomic64((SInt64
*)&kctlstat
.kcs_enqueue_fullsock
);
1048 if (ctl_debug
&& error
!= 0 && (flags
& CTL_DATA_CRIT
)) {
1049 printf("%s - crit data err %d len %d hiwat %d cc: %d\n",
1050 __func__
, error
, len
,
1051 so
->so_rcv
.sb_hiwat
, so
->so_rcv
.sb_cc
);
1054 socket_unlock(so
, 1);
1056 OSIncrementAtomic64((SInt64
*)&kctlstat
.kcs_enqueue_fail
);
1063 * Compute space occupied by mbuf like sbappendrecord
1066 m_space(struct mbuf
*m
)
1071 for (nxt
= m
; nxt
!= NULL
; nxt
= nxt
->m_next
) {
1072 space
+= nxt
->m_len
;
1079 ctl_enqueuembuf_list(void *kctlref
, u_int32_t unit
, struct mbuf
*m_list
,
1080 u_int32_t flags
, struct mbuf
**m_remain
)
1082 struct socket
*so
= NULL
;
1084 struct mbuf
*m
, *nextpkt
;
1087 u_int32_t kctlflags
;
1090 * Need to point the beginning of the list in case of early exit
1095 * kcb_find_socket takes the socket lock with a reference
1097 so
= kcb_find_socket(kctlref
, unit
, &kctlflags
);
1103 if (kctlflags
& CTL_FLAG_REG_SOCK_STREAM
) {
1107 if (flags
& CTL_DATA_EOR
) {
1112 for (m
= m_list
; m
!= NULL
; m
= nextpkt
) {
1113 nextpkt
= m
->m_nextpkt
;
1115 if (m
->m_pkthdr
.len
== 0 && ctl_debug
) {
1116 printf("%s: %llx m_pkthdr.len is 0",
1117 __func__
, (uint64_t)VM_KERNEL_ADDRPERM(m
));
1121 * The mbuf is either appended or freed by sbappendrecord()
1122 * so it's not reliable from a data standpoint
1125 if (ctl_rcvbspace(so
, len
, kctlflags
, flags
) != 0) {
1127 OSIncrementAtomic64(
1128 (SInt64
*)&kctlstat
.kcs_enqueue_fullsock
);
1132 * Unlink from the list, m is on its own
1134 m
->m_nextpkt
= NULL
;
1135 so_recv_data_stat(so
, m
, 0);
1136 if (sbappendrecord(&so
->so_rcv
, m
) != 0) {
1140 * We free or return the remaining
1145 OSIncrementAtomic64(
1146 (SInt64
*)&kctlstat
.kcs_enqueue_fullsock
);
1151 if (needwakeup
&& (flags
& CTL_DATA_NOWAKEUP
) == 0) {
1157 if (ctl_debug
&& error
!= 0 && (flags
& CTL_DATA_CRIT
)) {
1158 printf("%s - crit data err %d len %d hiwat %d cc: %d\n",
1159 __func__
, error
, len
,
1160 so
->so_rcv
.sb_hiwat
, so
->so_rcv
.sb_cc
);
1163 socket_unlock(so
, 1);
1168 if (m
!= NULL
&& socket_debug
&& so
!= NULL
&&
1169 (so
->so_options
& SO_DEBUG
)) {
1172 printf("%s m_list %llx\n", __func__
,
1173 (uint64_t) VM_KERNEL_ADDRPERM(m_list
));
1174 for (n
= m
; n
!= NULL
; n
= n
->m_nextpkt
) {
1175 printf(" remain %llx m_next %llx\n",
1176 (uint64_t) VM_KERNEL_ADDRPERM(n
),
1177 (uint64_t) VM_KERNEL_ADDRPERM(n
->m_next
));
1186 OSIncrementAtomic64((SInt64
*)&kctlstat
.kcs_enqueue_fail
);
1192 ctl_enqueuedata(void *kctlref
, u_int32_t unit
, void *data
, size_t len
,
1198 unsigned int num_needed
;
1201 u_int32_t kctlflags
;
1203 so
= kcb_find_socket(kctlref
, unit
, &kctlflags
);
1208 if (ctl_rcvbspace(so
, len
, kctlflags
, flags
) != 0) {
1210 OSIncrementAtomic64((SInt64
*)&kctlstat
.kcs_enqueue_fullsock
);
1215 m
= m_allocpacket_internal(&num_needed
, len
, NULL
, M_NOWAIT
, 1, 0);
1217 kctlstat
.kcs_enqdata_mb_alloc_fail
++;
1219 printf("%s: m_allocpacket_internal(%lu) failed\n",
1226 for (n
= m
; n
!= NULL
; n
= n
->m_next
) {
1227 size_t mlen
= mbuf_maxlen(n
);
1229 if (mlen
+ curlen
> len
) {
1230 mlen
= len
- curlen
;
1233 bcopy((char *)data
+ curlen
, n
->m_data
, mlen
);
1236 mbuf_pkthdr_setlen(m
, curlen
);
1238 if ((flags
& CTL_DATA_EOR
)) {
1239 m
->m_flags
|= M_EOR
;
1241 so_recv_data_stat(so
, m
, 0);
1242 if (sbappend(&so
->so_rcv
, m
) != 0) {
1243 if ((flags
& CTL_DATA_NOWAKEUP
) == 0) {
1247 kctlstat
.kcs_enqdata_sbappend_fail
++;
1249 OSIncrementAtomic64((SInt64
*)&kctlstat
.kcs_enqueue_fullsock
);
1253 if (ctl_debug
&& error
!= 0 && (flags
& CTL_DATA_CRIT
)) {
1254 printf("%s - crit data err %d len %d hiwat %d cc: %d\n",
1255 __func__
, error
, (int)len
,
1256 so
->so_rcv
.sb_hiwat
, so
->so_rcv
.sb_cc
);
1259 socket_unlock(so
, 1);
1261 OSIncrementAtomic64((SInt64
*)&kctlstat
.kcs_enqueue_fail
);
1267 ctl_getenqueuepacketcount(kern_ctl_ref kctlref
, u_int32_t unit
, u_int32_t
*pcnt
)
1277 so
= kcb_find_socket(kctlref
, unit
, NULL
);
1283 m1
= so
->so_rcv
.sb_mb
;
1284 while (m1
!= NULL
) {
1285 if (m1
->m_type
== MT_DATA
||
1286 m1
->m_type
== MT_HEADER
||
1287 m1
->m_type
== MT_OOBDATA
) {
1294 socket_unlock(so
, 1);
1300 ctl_getenqueuespace(kern_ctl_ref kctlref
, u_int32_t unit
, size_t *space
)
1305 if (space
== NULL
) {
1309 so
= kcb_find_socket(kctlref
, unit
, NULL
);
1314 avail
= sbspace(&so
->so_rcv
);
1315 *space
= (avail
< 0) ? 0 : avail
;
1316 socket_unlock(so
, 1);
1322 ctl_getenqueuereadable(kern_ctl_ref kctlref
, u_int32_t unit
,
1323 u_int32_t
*difference
)
1327 if (difference
== NULL
) {
1331 so
= kcb_find_socket(kctlref
, unit
, NULL
);
1336 if (so
->so_rcv
.sb_cc
>= so
->so_rcv
.sb_lowat
) {
1339 *difference
= (so
->so_rcv
.sb_lowat
- so
->so_rcv
.sb_cc
);
1341 socket_unlock(so
, 1);
1347 ctl_ctloutput(struct socket
*so
, struct sockopt
*sopt
)
1349 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
1355 if (sopt
->sopt_level
!= SYSPROTO_CONTROL
) {
1359 if (kcb
== NULL
) { /* sanity check */
1363 if ((kctl
= kcb
->kctl
) == NULL
) {
1367 lck_mtx_t
*mtx_held
= socket_getlock(so
, PR_F_WILLUNLOCK
);
1368 ctl_kcb_increment_use_count(kcb
, mtx_held
);
1370 switch (sopt
->sopt_dir
) {
1372 if (kctl
->setopt
== NULL
) {
1376 if (sopt
->sopt_valsize
!= 0) {
1377 MALLOC(data
, void *, sopt
->sopt_valsize
, M_TEMP
,
1383 error
= sooptcopyin(sopt
, data
,
1384 sopt
->sopt_valsize
, sopt
->sopt_valsize
);
1387 socket_unlock(so
, 0);
1388 error
= (*kctl
->setopt
)(kctl
->kctlref
,
1389 kcb
->sac
.sc_unit
, kcb
->userdata
, sopt
->sopt_name
,
1390 data
, sopt
->sopt_valsize
);
1400 if (kctl
->getopt
== NULL
) {
1405 if (sopt
->sopt_valsize
&& sopt
->sopt_val
) {
1406 MALLOC(data
, void *, sopt
->sopt_valsize
, M_TEMP
,
1413 * 4108337 - copy user data in case the
1414 * kernel control needs it
1416 error
= sooptcopyin(sopt
, data
,
1417 sopt
->sopt_valsize
, sopt
->sopt_valsize
);
1421 len
= sopt
->sopt_valsize
;
1422 socket_unlock(so
, 0);
1423 error
= (*kctl
->getopt
)(kctl
->kctlref
, kcb
->sac
.sc_unit
,
1424 kcb
->userdata
, sopt
->sopt_name
,
1426 if (data
!= NULL
&& len
> sopt
->sopt_valsize
) {
1427 panic_plain("ctl_ctloutput: ctl %s returned "
1428 "len (%lu) > sopt_valsize (%lu)\n",
1429 kcb
->kctl
->name
, len
,
1430 sopt
->sopt_valsize
);
1435 error
= sooptcopyout(sopt
, data
, len
);
1437 sopt
->sopt_valsize
= len
;
1448 ctl_kcb_decrement_use_count(kcb
);
1453 ctl_ioctl(struct socket
*so
, u_long cmd
, caddr_t data
,
1454 struct ifnet
*ifp
, struct proc
*p
)
1456 #pragma unused(so, ifp, p)
1457 int error
= ENOTSUP
;
1460 /* get the number of controllers */
1461 case CTLIOCGCOUNT
: {
1465 lck_mtx_lock(ctl_mtx
);
1466 TAILQ_FOREACH(kctl
, &ctl_head
, next
)
1468 lck_mtx_unlock(ctl_mtx
);
1470 bcopy(&n
, data
, sizeof(n
));
1475 struct ctl_info ctl_info
;
1476 struct kctl
*kctl
= 0;
1479 bcopy(data
, &ctl_info
, sizeof(ctl_info
));
1480 name_len
= strnlen(ctl_info
.ctl_name
, MAX_KCTL_NAME
);
1482 if (name_len
== 0 || name_len
+ 1 > MAX_KCTL_NAME
) {
1486 lck_mtx_lock(ctl_mtx
);
1487 kctl
= ctl_find_by_name(ctl_info
.ctl_name
);
1488 lck_mtx_unlock(ctl_mtx
);
1493 ctl_info
.ctl_id
= kctl
->id
;
1494 bcopy(&ctl_info
, data
, sizeof(ctl_info
));
1499 /* add controls to get list of NKEs */
1508 struct kctl
**new_table
;
1511 lck_mtx_assert(ctl_mtx
, LCK_MTX_ASSERT_OWNED
);
1513 if (kctl_tbl_growing
) {
1514 /* Another thread is allocating */
1515 kctl_tbl_growing_waiting
++;
1518 (void) msleep((caddr_t
) &kctl_tbl_growing
, ctl_mtx
,
1519 PSOCK
| PCATCH
, "kctl_tbl_growing", 0);
1520 } while (kctl_tbl_growing
);
1521 kctl_tbl_growing_waiting
--;
1523 /* Another thread grew the table */
1524 if (kctl_table
!= NULL
&& kctl_tbl_count
< kctl_tbl_size
) {
1528 /* Verify we have a sane size */
1529 if (kctl_tbl_size
+ KCTL_TBL_INC
>= UINT16_MAX
) {
1530 kctlstat
.kcs_tbl_size_too_big
++;
1532 printf("%s kctl_tbl_size %lu too big\n",
1533 __func__
, kctl_tbl_size
);
1537 kctl_tbl_growing
= 1;
1539 new_size
= kctl_tbl_size
+ KCTL_TBL_INC
;
1541 lck_mtx_unlock(ctl_mtx
);
1542 new_table
= _MALLOC(sizeof(struct kctl
*) * new_size
,
1543 M_TEMP
, M_WAIT
| M_ZERO
);
1544 lck_mtx_lock(ctl_mtx
);
1546 if (new_table
!= NULL
) {
1547 if (kctl_table
!= NULL
) {
1548 bcopy(kctl_table
, new_table
,
1549 kctl_tbl_size
* sizeof(struct kctl
*));
1551 _FREE(kctl_table
, M_TEMP
);
1553 kctl_table
= new_table
;
1554 kctl_tbl_size
= new_size
;
1557 kctl_tbl_growing
= 0;
1559 if (kctl_tbl_growing_waiting
) {
1560 wakeup(&kctl_tbl_growing
);
1564 #define KCTLREF_INDEX_MASK 0x0000FFFF
1565 #define KCTLREF_GENCNT_MASK 0xFFFF0000
1566 #define KCTLREF_GENCNT_SHIFT 16
1569 kctl_make_ref(struct kctl
*kctl
)
1573 lck_mtx_assert(ctl_mtx
, LCK_MTX_ASSERT_OWNED
);
1575 if (kctl_tbl_count
>= kctl_tbl_size
) {
1579 kctl
->kctlref
= NULL
;
1580 for (i
= 0; i
< kctl_tbl_size
; i
++) {
1581 if (kctl_table
[i
] == NULL
) {
1585 * Reference is index plus one
1587 kctl_ref_gencnt
+= 1;
1590 * Add generation count as salt to reference to prevent
1591 * use after deregister
1593 ref
= ((kctl_ref_gencnt
<< KCTLREF_GENCNT_SHIFT
) &
1594 KCTLREF_GENCNT_MASK
) +
1595 ((i
+ 1) & KCTLREF_INDEX_MASK
);
1597 kctl
->kctlref
= (void *)(ref
);
1598 kctl_table
[i
] = kctl
;
1604 if (kctl
->kctlref
== NULL
) {
1605 panic("%s no space in table", __func__
);
1608 if (ctl_debug
> 0) {
1609 printf("%s %p for %p\n",
1610 __func__
, kctl
->kctlref
, kctl
);
1613 return kctl
->kctlref
;
1617 kctl_delete_ref(kern_ctl_ref kctlref
)
1620 * Reference is index plus one
1622 uintptr_t i
= (((uintptr_t)kctlref
) & KCTLREF_INDEX_MASK
) - 1;
1624 lck_mtx_assert(ctl_mtx
, LCK_MTX_ASSERT_OWNED
);
1626 if (i
< kctl_tbl_size
) {
1627 struct kctl
*kctl
= kctl_table
[i
];
1629 if (kctl
->kctlref
== kctlref
) {
1630 kctl_table
[i
] = NULL
;
1633 kctlstat
.kcs_bad_kctlref
++;
1636 kctlstat
.kcs_bad_kctlref
++;
1640 static struct kctl
*
1641 kctl_from_ref(kern_ctl_ref kctlref
)
1644 * Reference is index plus one
1646 uintptr_t i
= (((uintptr_t)kctlref
) & KCTLREF_INDEX_MASK
) - 1;
1647 struct kctl
*kctl
= NULL
;
1649 lck_mtx_assert(ctl_mtx
, LCK_MTX_ASSERT_OWNED
);
1651 if (i
>= kctl_tbl_size
) {
1652 kctlstat
.kcs_bad_kctlref
++;
1655 kctl
= kctl_table
[i
];
1656 if (kctl
->kctlref
!= kctlref
) {
1657 kctlstat
.kcs_bad_kctlref
++;
1664 * Register/unregister a NKE
1667 ctl_register(struct kern_ctl_reg
*userkctl
, kern_ctl_ref
*kctlref
)
1669 struct kctl
*kctl
= NULL
;
1670 struct kctl
*kctl_next
= NULL
;
1673 int is_extended
= 0;
1675 if (userkctl
== NULL
) { /* sanity check */
1678 if (userkctl
->ctl_connect
== NULL
) {
1681 name_len
= strlen(userkctl
->ctl_name
);
1682 if (name_len
== 0 || name_len
+ 1 > MAX_KCTL_NAME
) {
1686 MALLOC(kctl
, struct kctl
*, sizeof(*kctl
), M_TEMP
, M_WAITOK
);
1690 bzero((char *)kctl
, sizeof(*kctl
));
1692 lck_mtx_lock(ctl_mtx
);
1694 if (kctl_make_ref(kctl
) == NULL
) {
1695 lck_mtx_unlock(ctl_mtx
);
1701 * Kernel Control IDs
1703 * CTL_FLAG_REG_ID_UNIT indicates the control ID and unit number are
1704 * static. If they do not exist, add them to the list in order. If the
1705 * flag is not set, we must find a new unique value. We assume the
1706 * list is in order. We find the last item in the list and add one. If
1707 * this leads to wrapping the id around, we start at the front of the
1708 * list and look for a gap.
1711 if ((userkctl
->ctl_flags
& CTL_FLAG_REG_ID_UNIT
) == 0) {
1712 /* Must dynamically assign an unused ID */
1714 /* Verify the same name isn't already registered */
1715 if (ctl_find_by_name(userkctl
->ctl_name
) != NULL
) {
1716 kctl_delete_ref(kctl
->kctlref
);
1717 lck_mtx_unlock(ctl_mtx
);
1722 /* Start with 1 in case the list is empty */
1724 kctl_next
= TAILQ_LAST(&ctl_head
, kctl_list
);
1726 if (kctl_next
!= NULL
) {
1727 /* List was not empty, add one to the last item */
1728 id
= kctl_next
->id
+ 1;
1732 * If this wrapped the id number, start looking at
1733 * the front of the list for an unused id.
1736 /* Find the next unused ID */
1739 TAILQ_FOREACH(kctl_next
, &ctl_head
, next
) {
1740 if (kctl_next
->id
> id
) {
1741 /* We found a gap */
1745 id
= kctl_next
->id
+ 1;
1750 userkctl
->ctl_id
= id
;
1752 kctl
->reg_unit
= -1;
1754 TAILQ_FOREACH(kctl_next
, &ctl_head
, next
) {
1755 if (kctl_next
->id
> userkctl
->ctl_id
) {
1760 if (ctl_find_by_id_unit(userkctl
->ctl_id
, userkctl
->ctl_unit
)) {
1761 kctl_delete_ref(kctl
->kctlref
);
1762 lck_mtx_unlock(ctl_mtx
);
1766 kctl
->id
= userkctl
->ctl_id
;
1767 kctl
->reg_unit
= userkctl
->ctl_unit
;
1770 is_extended
= (userkctl
->ctl_flags
& CTL_FLAG_REG_EXTENDED
);
1772 strlcpy(kctl
->name
, userkctl
->ctl_name
, MAX_KCTL_NAME
);
1773 kctl
->flags
= userkctl
->ctl_flags
;
1776 * Let the caller know the default send and receive sizes
1778 if (userkctl
->ctl_sendsize
== 0) {
1779 kctl
->sendbufsize
= CTL_SENDSIZE
;
1780 userkctl
->ctl_sendsize
= kctl
->sendbufsize
;
1782 kctl
->sendbufsize
= userkctl
->ctl_sendsize
;
1784 if (userkctl
->ctl_recvsize
== 0) {
1785 kctl
->recvbufsize
= CTL_RECVSIZE
;
1786 userkctl
->ctl_recvsize
= kctl
->recvbufsize
;
1788 kctl
->recvbufsize
= userkctl
->ctl_recvsize
;
1791 kctl
->bind
= userkctl
->ctl_bind
;
1792 kctl
->connect
= userkctl
->ctl_connect
;
1793 kctl
->disconnect
= userkctl
->ctl_disconnect
;
1794 kctl
->send
= userkctl
->ctl_send
;
1795 kctl
->setopt
= userkctl
->ctl_setopt
;
1796 kctl
->getopt
= userkctl
->ctl_getopt
;
1798 kctl
->rcvd
= userkctl
->ctl_rcvd
;
1799 kctl
->send_list
= userkctl
->ctl_send_list
;
1802 TAILQ_INIT(&kctl
->kcb_head
);
1805 TAILQ_INSERT_BEFORE(kctl_next
, kctl
, next
);
1807 TAILQ_INSERT_TAIL(&ctl_head
, kctl
, next
);
1810 kctlstat
.kcs_reg_count
++;
1811 kctlstat
.kcs_gencnt
++;
1813 lck_mtx_unlock(ctl_mtx
);
1815 *kctlref
= kctl
->kctlref
;
1817 ctl_post_msg(KEV_CTL_REGISTERED
, kctl
->id
);
1822 ctl_deregister(void *kctlref
)
1826 lck_mtx_lock(ctl_mtx
);
1827 if ((kctl
= kctl_from_ref(kctlref
)) == NULL
) {
1828 kctlstat
.kcs_bad_kctlref
++;
1829 lck_mtx_unlock(ctl_mtx
);
1830 if (ctl_debug
!= 0) {
1831 printf("%s invalid kctlref %p\n",
1837 if (!TAILQ_EMPTY(&kctl
->kcb_head
)) {
1838 lck_mtx_unlock(ctl_mtx
);
1842 TAILQ_REMOVE(&ctl_head
, kctl
, next
);
1844 kctlstat
.kcs_reg_count
--;
1845 kctlstat
.kcs_gencnt
++;
1847 kctl_delete_ref(kctl
->kctlref
);
1848 lck_mtx_unlock(ctl_mtx
);
1850 ctl_post_msg(KEV_CTL_DEREGISTERED
, kctl
->id
);
1856 * Must be called with global ctl_mtx lock taked
1858 static struct kctl
*
1859 ctl_find_by_name(const char *name
)
1863 lck_mtx_assert(ctl_mtx
, LCK_MTX_ASSERT_OWNED
);
1865 TAILQ_FOREACH(kctl
, &ctl_head
, next
)
1866 if (strncmp(kctl
->name
, name
, sizeof(kctl
->name
)) == 0) {
1874 ctl_id_by_name(const char *name
)
1876 u_int32_t ctl_id
= 0;
1879 lck_mtx_lock(ctl_mtx
);
1880 kctl
= ctl_find_by_name(name
);
1884 lck_mtx_unlock(ctl_mtx
);
1890 ctl_name_by_id(u_int32_t id
, char *out_name
, size_t maxsize
)
1895 lck_mtx_lock(ctl_mtx
);
1896 TAILQ_FOREACH(kctl
, &ctl_head
, next
) {
1897 if (kctl
->id
== id
) {
1903 if (maxsize
> MAX_KCTL_NAME
) {
1904 maxsize
= MAX_KCTL_NAME
;
1906 strlcpy(out_name
, kctl
->name
, maxsize
);
1909 lck_mtx_unlock(ctl_mtx
);
1911 return found
? 0 : ENOENT
;
1915 * Must be called with global ctl_mtx lock taked
1918 static struct kctl
*
1919 ctl_find_by_id_unit(u_int32_t id
, u_int32_t unit
)
1923 lck_mtx_assert(ctl_mtx
, LCK_MTX_ASSERT_OWNED
);
1925 TAILQ_FOREACH(kctl
, &ctl_head
, next
) {
1926 if (kctl
->id
== id
&& (kctl
->flags
& CTL_FLAG_REG_ID_UNIT
) == 0) {
1928 } else if (kctl
->id
== id
&& kctl
->reg_unit
== unit
) {
1936 * Must be called with kernel controller lock taken
1938 static struct ctl_cb
*
1939 kcb_find(struct kctl
*kctl
, u_int32_t unit
)
1943 lck_mtx_assert(ctl_mtx
, LCK_MTX_ASSERT_OWNED
);
1945 TAILQ_FOREACH(kcb
, &kctl
->kcb_head
, next
)
1946 if (kcb
->sac
.sc_unit
== unit
) {
1953 static struct socket
*
1954 kcb_find_socket(kern_ctl_ref kctlref
, u_int32_t unit
, u_int32_t
*kctlflags
)
1956 struct socket
*so
= NULL
;
1962 lr_saved
= __builtin_return_address(0);
1964 lck_mtx_lock(ctl_mtx
);
1966 * First validate the kctlref
1968 if ((kctl
= kctl_from_ref(kctlref
)) == NULL
) {
1969 kctlstat
.kcs_bad_kctlref
++;
1970 lck_mtx_unlock(ctl_mtx
);
1971 if (ctl_debug
!= 0) {
1972 printf("%s invalid kctlref %p\n",
1978 kcb
= kcb_find(kctl
, unit
);
1979 if (kcb
== NULL
|| kcb
->kctl
!= kctl
|| (so
= kcb
->so
) == NULL
) {
1980 lck_mtx_unlock(ctl_mtx
);
1984 * This prevents the socket from being closed
1988 * Respect lock ordering: socket before ctl_mtx
1990 lck_mtx_unlock(ctl_mtx
);
1994 * The socket lock history is more useful if we store
1995 * the address of the caller.
1997 i
= (so
->next_lock_lr
+ SO_LCKDBG_MAX
- 1) % SO_LCKDBG_MAX
;
1998 so
->lock_lr
[i
] = lr_saved
;
2000 lck_mtx_lock(ctl_mtx
);
2002 if ((kctl
= kctl_from_ref(kctlref
)) == NULL
|| kcb
->kctl
== NULL
) {
2003 lck_mtx_unlock(ctl_mtx
);
2004 socket_unlock(so
, 1);
2006 lck_mtx_lock(ctl_mtx
);
2007 } else if (kctlflags
!= NULL
) {
2008 *kctlflags
= kctl
->flags
;
2012 if (kcb
->usecount
== 0) {
2013 wakeup((event_t
)&kcb
->usecount
);
2016 lck_mtx_unlock(ctl_mtx
);
2022 ctl_post_msg(u_int32_t event_code
, u_int32_t id
)
2024 struct ctl_event_data ctl_ev_data
;
2025 struct kev_msg ev_msg
;
2027 lck_mtx_assert(ctl_mtx
, LCK_MTX_ASSERT_NOTOWNED
);
2029 bzero(&ev_msg
, sizeof(struct kev_msg
));
2030 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
2032 ev_msg
.kev_class
= KEV_SYSTEM_CLASS
;
2033 ev_msg
.kev_subclass
= KEV_CTL_SUBCLASS
;
2034 ev_msg
.event_code
= event_code
;
2036 /* common nke subclass data */
2037 bzero(&ctl_ev_data
, sizeof(ctl_ev_data
));
2038 ctl_ev_data
.ctl_id
= id
;
2039 ev_msg
.dv
[0].data_ptr
= &ctl_ev_data
;
2040 ev_msg
.dv
[0].data_length
= sizeof(ctl_ev_data
);
2042 ev_msg
.dv
[1].data_length
= 0;
2044 kev_post_msg(&ev_msg
);
2048 ctl_lock(struct socket
*so
, int refcount
, void *lr
)
2053 lr_saved
= __builtin_return_address(0);
2058 if (so
->so_pcb
!= NULL
) {
2059 lck_mtx_lock(((struct ctl_cb
*)so
->so_pcb
)->mtx
);
2061 panic("ctl_lock: so=%p NO PCB! lr=%p lrh= %s\n",
2062 so
, lr_saved
, solockhistory_nr(so
));
2066 if (so
->so_usecount
< 0) {
2067 panic("ctl_lock: so=%p so_pcb=%p lr=%p ref=%x lrh= %s\n",
2068 so
, so
->so_pcb
, lr_saved
, so
->so_usecount
,
2069 solockhistory_nr(so
));
2077 so
->lock_lr
[so
->next_lock_lr
] = lr_saved
;
2078 so
->next_lock_lr
= (so
->next_lock_lr
+ 1) % SO_LCKDBG_MAX
;
2083 ctl_unlock(struct socket
*so
, int refcount
, void *lr
)
2086 lck_mtx_t
*mutex_held
;
2089 lr_saved
= __builtin_return_address(0);
2094 #if (MORE_KCTLLOCK_DEBUG && (DEVELOPMENT || DEBUG))
2095 printf("ctl_unlock: so=%llx sopcb=%x lock=%llx ref=%u lr=%llx\n",
2096 (uint64_t)VM_KERNEL_ADDRPERM(so
),
2097 (uint64_t)VM_KERNEL_ADDRPERM(so
->so_pcb
,
2098 (uint64_t)VM_KERNEL_ADDRPERM(((struct ctl_cb
*)so
->so_pcb
)->mtx
),
2099 so
->so_usecount
, (uint64_t)VM_KERNEL_ADDRPERM(lr_saved
));
2100 #endif /* (MORE_KCTLLOCK_DEBUG && (DEVELOPMENT || DEBUG)) */
2105 if (so
->so_usecount
< 0) {
2106 panic("ctl_unlock: so=%p usecount=%x lrh= %s\n",
2107 so
, so
->so_usecount
, solockhistory_nr(so
));
2110 if (so
->so_pcb
== NULL
) {
2111 panic("ctl_unlock: so=%p NO PCB usecount=%x lr=%p lrh= %s\n",
2112 so
, so
->so_usecount
, (void *)lr_saved
,
2113 solockhistory_nr(so
));
2116 mutex_held
= ((struct ctl_cb
*)so
->so_pcb
)->mtx
;
2118 lck_mtx_assert(mutex_held
, LCK_MTX_ASSERT_OWNED
);
2119 so
->unlock_lr
[so
->next_unlock_lr
] = lr_saved
;
2120 so
->next_unlock_lr
= (so
->next_unlock_lr
+ 1) % SO_LCKDBG_MAX
;
2121 lck_mtx_unlock(mutex_held
);
2123 if (so
->so_usecount
== 0) {
2124 ctl_sofreelastref(so
);
2131 ctl_getlock(struct socket
*so
, int flags
)
2133 #pragma unused(flags)
2134 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
2137 if (so
->so_usecount
< 0) {
2138 panic("ctl_getlock: so=%p usecount=%x lrh= %s\n",
2139 so
, so
->so_usecount
, solockhistory_nr(so
));
2143 panic("ctl_getlock: so=%p NULL NO so_pcb %s\n",
2144 so
, solockhistory_nr(so
));
2145 return so
->so_proto
->pr_domain
->dom_mtx
;
2149 __private_extern__
int
2150 kctl_reg_list SYSCTL_HANDLER_ARGS
2152 #pragma unused(oidp, arg1, arg2)
2155 struct xsystmgen xsg
;
2158 size_t item_size
= ROUNDUP64(sizeof(struct xkctl_reg
));
2160 buf
= _MALLOC(item_size
, M_TEMP
, M_WAITOK
| M_ZERO
);
2165 lck_mtx_lock(ctl_mtx
);
2167 n
= kctlstat
.kcs_reg_count
;
2169 if (req
->oldptr
== USER_ADDR_NULL
) {
2170 req
->oldidx
= (n
+ n
/ 8) * sizeof(struct xkctl_reg
);
2173 if (req
->newptr
!= USER_ADDR_NULL
) {
2177 bzero(&xsg
, sizeof(xsg
));
2178 xsg
.xg_len
= sizeof(xsg
);
2180 xsg
.xg_gen
= kctlstat
.kcs_gencnt
;
2181 xsg
.xg_sogen
= so_gencnt
;
2182 error
= SYSCTL_OUT(req
, &xsg
, sizeof(xsg
));
2187 * We are done if there is no pcb
2194 for (i
= 0, kctl
= TAILQ_FIRST(&ctl_head
);
2195 i
< n
&& kctl
!= NULL
;
2196 i
++, kctl
= TAILQ_NEXT(kctl
, next
)) {
2197 struct xkctl_reg
*xkr
= (struct xkctl_reg
*)buf
;
2199 u_int32_t pcbcount
= 0;
2201 TAILQ_FOREACH(kcb
, &kctl
->kcb_head
, next
)
2204 bzero(buf
, item_size
);
2206 xkr
->xkr_len
= sizeof(struct xkctl_reg
);
2207 xkr
->xkr_kind
= XSO_KCREG
;
2208 xkr
->xkr_id
= kctl
->id
;
2209 xkr
->xkr_reg_unit
= kctl
->reg_unit
;
2210 xkr
->xkr_flags
= kctl
->flags
;
2211 xkr
->xkr_kctlref
= (uint64_t)(kctl
->kctlref
);
2212 xkr
->xkr_recvbufsize
= kctl
->recvbufsize
;
2213 xkr
->xkr_sendbufsize
= kctl
->sendbufsize
;
2214 xkr
->xkr_lastunit
= kctl
->lastunit
;
2215 xkr
->xkr_pcbcount
= pcbcount
;
2216 xkr
->xkr_connect
= (uint64_t)VM_KERNEL_UNSLIDE(kctl
->connect
);
2217 xkr
->xkr_disconnect
=
2218 (uint64_t)VM_KERNEL_UNSLIDE(kctl
->disconnect
);
2219 xkr
->xkr_send
= (uint64_t)VM_KERNEL_UNSLIDE(kctl
->send
);
2220 xkr
->xkr_send_list
=
2221 (uint64_t)VM_KERNEL_UNSLIDE(kctl
->send_list
);
2222 xkr
->xkr_setopt
= (uint64_t)VM_KERNEL_UNSLIDE(kctl
->setopt
);
2223 xkr
->xkr_getopt
= (uint64_t)VM_KERNEL_UNSLIDE(kctl
->getopt
);
2224 xkr
->xkr_rcvd
= (uint64_t)VM_KERNEL_UNSLIDE(kctl
->rcvd
);
2225 strlcpy(xkr
->xkr_name
, kctl
->name
, sizeof(xkr
->xkr_name
));
2227 error
= SYSCTL_OUT(req
, buf
, item_size
);
2232 * Give the user an updated idea of our state.
2233 * If the generation differs from what we told
2234 * her before, she knows that something happened
2235 * while we were processing this request, and it
2236 * might be necessary to retry.
2238 bzero(&xsg
, sizeof(xsg
));
2239 xsg
.xg_len
= sizeof(xsg
);
2241 xsg
.xg_gen
= kctlstat
.kcs_gencnt
;
2242 xsg
.xg_sogen
= so_gencnt
;
2243 error
= SYSCTL_OUT(req
, &xsg
, sizeof(xsg
));
2250 lck_mtx_unlock(ctl_mtx
);
2259 __private_extern__
int
2260 kctl_pcblist SYSCTL_HANDLER_ARGS
2262 #pragma unused(oidp, arg1, arg2)
2265 struct xsystmgen xsg
;
2268 size_t item_size
= ROUNDUP64(sizeof(struct xkctlpcb
)) +
2269 ROUNDUP64(sizeof(struct xsocket_n
)) +
2270 2 * ROUNDUP64(sizeof(struct xsockbuf_n
)) +
2271 ROUNDUP64(sizeof(struct xsockstat_n
));
2273 buf
= _MALLOC(item_size
, M_TEMP
, M_WAITOK
| M_ZERO
);
2278 lck_mtx_lock(ctl_mtx
);
2280 n
= kctlstat
.kcs_pcbcount
;
2282 if (req
->oldptr
== USER_ADDR_NULL
) {
2283 req
->oldidx
= (n
+ n
/ 8) * item_size
;
2286 if (req
->newptr
!= USER_ADDR_NULL
) {
2290 bzero(&xsg
, sizeof(xsg
));
2291 xsg
.xg_len
= sizeof(xsg
);
2293 xsg
.xg_gen
= kctlstat
.kcs_gencnt
;
2294 xsg
.xg_sogen
= so_gencnt
;
2295 error
= SYSCTL_OUT(req
, &xsg
, sizeof(xsg
));
2300 * We are done if there is no pcb
2307 for (i
= 0, kctl
= TAILQ_FIRST(&ctl_head
);
2308 i
< n
&& kctl
!= NULL
;
2309 kctl
= TAILQ_NEXT(kctl
, next
)) {
2312 for (kcb
= TAILQ_FIRST(&kctl
->kcb_head
);
2313 i
< n
&& kcb
!= NULL
;
2314 i
++, kcb
= TAILQ_NEXT(kcb
, next
)) {
2315 struct xkctlpcb
*xk
= (struct xkctlpcb
*)buf
;
2316 struct xsocket_n
*xso
= (struct xsocket_n
*)
2317 ADVANCE64(xk
, sizeof(*xk
));
2318 struct xsockbuf_n
*xsbrcv
= (struct xsockbuf_n
*)
2319 ADVANCE64(xso
, sizeof(*xso
));
2320 struct xsockbuf_n
*xsbsnd
= (struct xsockbuf_n
*)
2321 ADVANCE64(xsbrcv
, sizeof(*xsbrcv
));
2322 struct xsockstat_n
*xsostats
= (struct xsockstat_n
*)
2323 ADVANCE64(xsbsnd
, sizeof(*xsbsnd
));
2325 bzero(buf
, item_size
);
2327 xk
->xkp_len
= sizeof(struct xkctlpcb
);
2328 xk
->xkp_kind
= XSO_KCB
;
2329 xk
->xkp_unit
= kcb
->sac
.sc_unit
;
2330 xk
->xkp_kctpcb
= (uint64_t)VM_KERNEL_ADDRPERM(kcb
);
2331 xk
->xkp_kctlref
= (uint64_t)VM_KERNEL_ADDRPERM(kctl
);
2332 xk
->xkp_kctlid
= kctl
->id
;
2333 strlcpy(xk
->xkp_kctlname
, kctl
->name
,
2334 sizeof(xk
->xkp_kctlname
));
2336 sotoxsocket_n(kcb
->so
, xso
);
2337 sbtoxsockbuf_n(kcb
->so
?
2338 &kcb
->so
->so_rcv
: NULL
, xsbrcv
);
2339 sbtoxsockbuf_n(kcb
->so
?
2340 &kcb
->so
->so_snd
: NULL
, xsbsnd
);
2341 sbtoxsockstat_n(kcb
->so
, xsostats
);
2343 error
= SYSCTL_OUT(req
, buf
, item_size
);
2349 * Give the user an updated idea of our state.
2350 * If the generation differs from what we told
2351 * her before, she knows that something happened
2352 * while we were processing this request, and it
2353 * might be necessary to retry.
2355 bzero(&xsg
, sizeof(xsg
));
2356 xsg
.xg_len
= sizeof(xsg
);
2358 xsg
.xg_gen
= kctlstat
.kcs_gencnt
;
2359 xsg
.xg_sogen
= so_gencnt
;
2360 error
= SYSCTL_OUT(req
, &xsg
, sizeof(xsg
));
2367 lck_mtx_unlock(ctl_mtx
);
2373 kctl_getstat SYSCTL_HANDLER_ARGS
2375 #pragma unused(oidp, arg1, arg2)
2378 lck_mtx_lock(ctl_mtx
);
2380 if (req
->newptr
!= USER_ADDR_NULL
) {
2384 if (req
->oldptr
== USER_ADDR_NULL
) {
2385 req
->oldidx
= sizeof(struct kctlstat
);
2389 error
= SYSCTL_OUT(req
, &kctlstat
,
2390 MIN(sizeof(struct kctlstat
), req
->oldlen
));
2392 lck_mtx_unlock(ctl_mtx
);
2397 kctl_fill_socketinfo(struct socket
*so
, struct socket_info
*si
)
2399 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
2400 struct kern_ctl_info
*kcsi
=
2401 &si
->soi_proto
.pri_kern_ctl
;
2402 struct kctl
*kctl
= kcb
->kctl
;
2404 si
->soi_kind
= SOCKINFO_KERN_CTL
;
2410 kcsi
->kcsi_id
= kctl
->id
;
2411 kcsi
->kcsi_reg_unit
= kctl
->reg_unit
;
2412 kcsi
->kcsi_flags
= kctl
->flags
;
2413 kcsi
->kcsi_recvbufsize
= kctl
->recvbufsize
;
2414 kcsi
->kcsi_sendbufsize
= kctl
->sendbufsize
;
2415 kcsi
->kcsi_unit
= kcb
->sac
.sc_unit
;
2416 strlcpy(kcsi
->kcsi_name
, kctl
->name
, MAX_KCTL_NAME
);