2 * Copyright (c) 1999-2008 Apple Computer, 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 <net/if_var.h>
54 #include <mach/vm_types.h>
56 #include <kern/thread.h>
59 * Definitions and vars for we support
62 #define CTL_SENDSIZE (2 * 1024) /* default buffer size */
63 #define CTL_RECVSIZE (8 * 1024) /* default buffer size */
66 * Definitions and vars for we support
69 static u_int32_t ctl_maxunit
= 65536;
70 static lck_grp_attr_t
*ctl_lck_grp_attr
= 0;
71 static lck_attr_t
*ctl_lck_attr
= 0;
72 static lck_grp_t
*ctl_lck_grp
= 0;
73 static lck_mtx_t
*ctl_mtx
;
76 /* all the controllers are chained */
77 TAILQ_HEAD(kctl_list
, kctl
) ctl_head
;
79 static int ctl_attach(struct socket
*, int, struct proc
*);
80 static int ctl_detach(struct socket
*);
81 static int ctl_sofreelastref(struct socket
*so
);
82 static int ctl_connect(struct socket
*, struct sockaddr
*, struct proc
*);
83 static int ctl_disconnect(struct socket
*);
84 static int ctl_ioctl(struct socket
*so
, u_long cmd
, caddr_t data
,
85 struct ifnet
*ifp
, struct proc
*p
);
86 static int ctl_send(struct socket
*, int, struct mbuf
*,
87 struct sockaddr
*, struct mbuf
*, struct proc
*);
88 static int ctl_ctloutput(struct socket
*, struct sockopt
*);
89 static int ctl_peeraddr(struct socket
*so
, struct sockaddr
**nam
);
91 static struct kctl
*ctl_find_by_name(const char *);
92 static struct kctl
*ctl_find_by_id_unit(u_int32_t id
, u_int32_t unit
);
94 static struct socket
*kcb_find_socket(struct kctl
*, u_int32_t unit
);
95 static struct ctl_cb
*kcb_find(struct kctl
*, u_int32_t unit
);
96 static void ctl_post_msg(u_int32_t event_code
, u_int32_t id
);
98 static int ctl_lock(struct socket
*, int, void *);
99 static int ctl_unlock(struct socket
*, int, void *);
100 static lck_mtx_t
* ctl_getlock(struct socket
*, int);
102 static struct pr_usrreqs ctl_usrreqs
=
104 pru_abort_notsupp
, pru_accept_notsupp
, ctl_attach
, pru_bind_notsupp
,
105 ctl_connect
, pru_connect2_notsupp
, ctl_ioctl
, ctl_detach
,
106 ctl_disconnect
, pru_listen_notsupp
, ctl_peeraddr
,
107 pru_rcvd_notsupp
, pru_rcvoob_notsupp
, ctl_send
,
108 pru_sense_null
, pru_shutdown_notsupp
, pru_sockaddr_notsupp
,
109 sosend
, soreceive
, pru_sopoll_notsupp
112 static struct protosw kctlswk_dgram
=
114 SOCK_DGRAM
, &systemdomain
, SYSPROTO_CONTROL
,
115 PR_ATOMIC
|PR_CONNREQUIRED
|PR_PCBLOCK
,
116 NULL
, NULL
, NULL
, ctl_ctloutput
,
118 NULL
, NULL
, NULL
, NULL
, &ctl_usrreqs
,
119 ctl_lock
, ctl_unlock
, ctl_getlock
, { 0, 0 } , 0, { 0 }
122 static struct protosw kctlswk_stream
=
124 SOCK_STREAM
, &systemdomain
, SYSPROTO_CONTROL
,
125 PR_CONNREQUIRED
|PR_PCBLOCK
,
126 NULL
, NULL
, NULL
, ctl_ctloutput
,
128 NULL
, NULL
, NULL
, NULL
, &ctl_usrreqs
,
129 ctl_lock
, ctl_unlock
, ctl_getlock
, { 0, 0 } , 0, { 0 }
134 * Install the protosw's for the Kernel Control manager.
136 __private_extern__
int
137 kern_control_init(void)
141 ctl_lck_grp_attr
= lck_grp_attr_alloc_init();
142 if (ctl_lck_grp_attr
== 0) {
143 printf(": lck_grp_attr_alloc_init failed\n");
148 ctl_lck_grp
= lck_grp_alloc_init("Kernel Control Protocol", ctl_lck_grp_attr
);
149 if (ctl_lck_grp
== 0) {
150 printf("kern_control_init: lck_grp_alloc_init failed\n");
155 ctl_lck_attr
= lck_attr_alloc_init();
156 if (ctl_lck_attr
== 0) {
157 printf("kern_control_init: lck_attr_alloc_init failed\n");
162 ctl_mtx
= lck_mtx_alloc_init(ctl_lck_grp
, ctl_lck_attr
);
164 printf("kern_control_init: lck_mtx_alloc_init failed\n");
168 TAILQ_INIT(&ctl_head
);
170 error
= net_add_proto(&kctlswk_dgram
, &systemdomain
);
172 log(LOG_WARNING
, "kern_control_init: net_add_proto dgram failed (%d)\n", error
);
174 error
= net_add_proto(&kctlswk_stream
, &systemdomain
);
176 log(LOG_WARNING
, "kern_control_init: net_add_proto stream failed (%d)\n", error
);
182 lck_mtx_free(ctl_mtx
, ctl_lck_grp
);
186 lck_grp_free(ctl_lck_grp
);
189 if (ctl_lck_grp_attr
) {
190 lck_grp_attr_free(ctl_lck_grp_attr
);
191 ctl_lck_grp_attr
= 0;
194 lck_attr_free(ctl_lck_attr
);
202 kcb_delete(struct ctl_cb
*kcb
)
206 lck_mtx_free(kcb
->mtx
, ctl_lck_grp
);
213 * Kernel Controller user-request functions
214 * attach function must exist and succeed
215 * detach not necessary
216 * we need a pcb for the per socket mutex
219 ctl_attach(__unused
struct socket
*so
, __unused
int proto
, __unused
struct proc
*p
)
222 struct ctl_cb
*kcb
= 0;
224 MALLOC(kcb
, struct ctl_cb
*, sizeof(struct ctl_cb
), M_TEMP
, M_WAITOK
);
229 bzero(kcb
, sizeof(struct ctl_cb
));
231 kcb
->mtx
= lck_mtx_alloc_init(ctl_lck_grp
, ctl_lck_attr
);
232 if (kcb
->mtx
== NULL
) {
237 so
->so_pcb
= (caddr_t
)kcb
;
248 ctl_sofreelastref(struct socket
*so
)
250 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
256 if ((kctl
= kcb
->kctl
) != 0) {
257 lck_mtx_lock(ctl_mtx
);
258 TAILQ_REMOVE(&kctl
->kcb_head
, kcb
, next
);
259 lck_mtx_unlock(ctl_mtx
);
263 sofreelastref(so
, 1);
268 ctl_detach(struct socket
*so
)
270 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
275 soisdisconnected(so
);
276 so
->so_flags
|= SOF_PCBCLEARING
;
282 ctl_connect(struct socket
*so
, struct sockaddr
*nam
, __unused
struct proc
*p
)
286 struct sockaddr_ctl sa
;
287 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
288 struct ctl_cb
*kcb_next
= NULL
;
291 panic("ctl_connect so_pcb null\n");
293 if (nam
->sa_len
!= sizeof(struct sockaddr_ctl
))
296 bcopy(nam
, &sa
, sizeof(struct sockaddr_ctl
));
298 lck_mtx_lock(ctl_mtx
);
299 kctl
= ctl_find_by_id_unit(sa
.sc_id
, sa
.sc_unit
);
301 lck_mtx_unlock(ctl_mtx
);
305 if (((kctl
->flags
& CTL_FLAG_REG_SOCK_STREAM
) && (so
->so_type
!= SOCK_STREAM
)) ||
306 (!(kctl
->flags
& CTL_FLAG_REG_SOCK_STREAM
) && (so
->so_type
!= SOCK_DGRAM
))) {
307 lck_mtx_unlock(ctl_mtx
);
311 if (kctl
->flags
& CTL_FLAG_PRIVILEGED
) {
313 lck_mtx_unlock(ctl_mtx
);
316 if (kauth_cred_issuser(kauth_cred_get()) == 0) {
317 lck_mtx_unlock(ctl_mtx
);
322 if ((kctl
->flags
& CTL_FLAG_REG_ID_UNIT
) || sa
.sc_unit
!= 0) {
323 if (kcb_find(kctl
, sa
.sc_unit
) != NULL
) {
324 lck_mtx_unlock(ctl_mtx
);
328 /* Find an unused ID, assumes control IDs are listed in order */
331 TAILQ_FOREACH(kcb_next
, &kctl
->kcb_head
, next
) {
332 if (kcb_next
->unit
> unit
) {
333 /* Found a gap, lets fill it in */
336 unit
= kcb_next
->unit
+ 1;
337 if (unit
== ctl_maxunit
)
341 if (unit
== ctl_maxunit
) {
342 lck_mtx_unlock(ctl_mtx
);
349 kcb
->unit
= sa
.sc_unit
;
351 if (kcb_next
!= NULL
) {
352 TAILQ_INSERT_BEFORE(kcb_next
, kcb
, next
);
355 TAILQ_INSERT_TAIL(&kctl
->kcb_head
, kcb
, next
);
357 lck_mtx_unlock(ctl_mtx
);
359 error
= soreserve(so
, kctl
->sendbufsize
, kctl
->recvbufsize
);
364 socket_unlock(so
, 0);
365 error
= (*kctl
->connect
)(kctl
, &sa
, &kcb
->userdata
);
373 if (error
&& kctl
->disconnect
) {
374 socket_unlock(so
, 0);
375 (*kctl
->disconnect
)(kctl
, kcb
->unit
, kcb
->userdata
);
380 soisdisconnected(so
);
381 lck_mtx_lock(ctl_mtx
);
384 TAILQ_REMOVE(&kctl
->kcb_head
, kcb
, next
);
385 lck_mtx_unlock(ctl_mtx
);
391 ctl_disconnect(struct socket
*so
)
393 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
395 if ((kcb
= (struct ctl_cb
*)so
->so_pcb
)) {
396 struct kctl
*kctl
= kcb
->kctl
;
398 if (kctl
&& kctl
->disconnect
) {
399 socket_unlock(so
, 0);
400 (*kctl
->disconnect
)(kctl
, kcb
->unit
, kcb
->userdata
);
404 soisdisconnected(so
);
406 socket_unlock(so
, 0);
407 lck_mtx_lock(ctl_mtx
);
410 while (kcb
->usecount
!= 0) {
411 msleep(&kcb
->usecount
, ctl_mtx
, 0, "kcb->usecount", 0);
413 TAILQ_REMOVE(&kctl
->kcb_head
, kcb
, next
);
414 lck_mtx_unlock(ctl_mtx
);
421 ctl_peeraddr(struct socket
*so
, struct sockaddr
**nam
)
423 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
425 struct sockaddr_ctl sc
;
427 if (kcb
== NULL
) /* sanity check */
430 if ((kctl
= kcb
->kctl
) == NULL
)
433 bzero(&sc
, sizeof(struct sockaddr_ctl
));
434 sc
.sc_len
= sizeof(struct sockaddr_ctl
);
435 sc
.sc_family
= AF_SYSTEM
;
436 sc
.ss_sysaddr
= AF_SYS_CONTROL
;
438 sc
.sc_unit
= kcb
->unit
;
440 *nam
= dup_sockaddr((struct sockaddr
*)&sc
, 1);
446 ctl_send(struct socket
*so
, int flags
, struct mbuf
*m
,
447 __unused
struct sockaddr
*addr
, struct mbuf
*control
,
448 __unused
struct proc
*p
)
451 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
454 if (control
) m_freem(control
);
456 if (kcb
== NULL
) /* sanity check */
459 if (error
== 0 && (kctl
= kcb
->kctl
) == NULL
)
462 if (error
== 0 && kctl
->send
) {
463 socket_unlock(so
, 0);
464 error
= (*kctl
->send
)(kctl
, kcb
->unit
, kcb
->userdata
, m
, flags
);
475 ctl_enqueuembuf(void *kctlref
, u_int32_t unit
, struct mbuf
*m
, u_int32_t flags
)
479 struct kctl
*kctl
= (struct kctl
*)kctlref
;
484 so
= kcb_find_socket(kctl
, unit
);
489 if (sbspace(&so
->so_rcv
) < m
->m_pkthdr
.len
) {
493 if ((flags
& CTL_DATA_EOR
))
495 if (sbappend(&so
->so_rcv
, m
) && (flags
& CTL_DATA_NOWAKEUP
) == 0)
498 socket_unlock(so
, 1);
503 ctl_enqueuedata(void *kctlref
, u_int32_t unit
, void *data
, size_t len
, u_int32_t flags
)
508 struct kctl
*kctl
= (struct kctl
*)kctlref
;
509 unsigned int num_needed
;
516 so
= kcb_find_socket(kctl
, unit
);
520 if (sbspace(&so
->so_rcv
) < (int)len
) {
526 m
= m_allocpacket_internal(&num_needed
, len
, NULL
, M_NOWAIT
, 1, 0);
528 printf("ctl_enqueuedata: m_allocpacket_internal(%lu) failed\n", len
);
533 for (n
= m
; n
!= NULL
; n
= n
->m_next
) {
534 size_t mlen
= mbuf_maxlen(n
);
536 if (mlen
+ curlen
> len
)
539 bcopy((char *)data
+ curlen
, n
->m_data
, mlen
);
542 mbuf_pkthdr_setlen(m
, curlen
);
544 if ((flags
& CTL_DATA_EOR
))
546 if (sbappend(&so
->so_rcv
, m
) && (flags
& CTL_DATA_NOWAKEUP
) == 0)
549 socket_unlock(so
, 1);
555 ctl_getenqueuespace(kern_ctl_ref kctlref
, u_int32_t unit
, size_t *space
)
557 struct kctl
*kctl
= (struct kctl
*)kctlref
;
561 if (kctlref
== NULL
|| space
== NULL
)
564 so
= kcb_find_socket(kctl
, unit
);
568 avail
= sbspace(&so
->so_rcv
);
569 *space
= (avail
< 0) ? 0 : avail
;
570 socket_unlock(so
, 1);
576 ctl_ctloutput(struct socket
*so
, struct sockopt
*sopt
)
578 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
584 if (sopt
->sopt_level
!= SYSPROTO_CONTROL
) {
588 if (kcb
== NULL
) /* sanity check */
591 if ((kctl
= kcb
->kctl
) == NULL
)
594 switch (sopt
->sopt_dir
) {
596 if (kctl
->setopt
== NULL
)
598 if (sopt
->sopt_valsize
== 0) {
601 MALLOC(data
, void *, sopt
->sopt_valsize
, M_TEMP
, M_WAITOK
);
604 error
= sooptcopyin(sopt
, data
, sopt
->sopt_valsize
, sopt
->sopt_valsize
);
607 socket_unlock(so
, 0);
608 error
= (*kctl
->setopt
)(kcb
->kctl
, kcb
->unit
, kcb
->userdata
, sopt
->sopt_name
,
609 data
, sopt
->sopt_valsize
);
616 if (kctl
->getopt
== NULL
)
619 if (sopt
->sopt_valsize
&& sopt
->sopt_val
) {
620 MALLOC(data
, void *, sopt
->sopt_valsize
, M_TEMP
, M_WAITOK
);
623 /* 4108337 - copy in data for get socket option */
624 error
= sooptcopyin(sopt
, data
, sopt
->sopt_valsize
, sopt
->sopt_valsize
);
626 len
= sopt
->sopt_valsize
;
627 socket_unlock(so
, 0);
628 error
= (*kctl
->getopt
)(kcb
->kctl
, kcb
->unit
, kcb
->userdata
, sopt
->sopt_name
,
630 if (data
!= NULL
&& len
> sopt
->sopt_valsize
)
631 panic_plain("ctl_ctloutput: ctl %s returned len (%lu) > sopt_valsize (%lu)\n",
632 kcb
->kctl
->name
, len
, sopt
->sopt_valsize
);
636 error
= sooptcopyout(sopt
, data
, len
);
638 sopt
->sopt_valsize
= len
;
648 ctl_ioctl(__unused
struct socket
*so
, u_long cmd
, caddr_t data
,
649 __unused
struct ifnet
*ifp
, __unused
struct proc
*p
)
654 /* get the number of controllers */
659 lck_mtx_lock(ctl_mtx
);
660 TAILQ_FOREACH(kctl
, &ctl_head
, next
)
662 lck_mtx_unlock(ctl_mtx
);
664 *(u_int32_t
*)data
= n
;
669 struct ctl_info
*ctl_info
= (struct ctl_info
*)data
;
670 struct kctl
*kctl
= 0;
671 size_t name_len
= strlen(ctl_info
->ctl_name
);
673 if (name_len
== 0 || name_len
+ 1 > MAX_KCTL_NAME
) {
677 lck_mtx_lock(ctl_mtx
);
678 kctl
= ctl_find_by_name(ctl_info
->ctl_name
);
679 lck_mtx_unlock(ctl_mtx
);
684 ctl_info
->ctl_id
= kctl
->id
;
689 /* add controls to get list of NKEs */
697 * Register/unregister a NKE
700 ctl_register(struct kern_ctl_reg
*userkctl
, kern_ctl_ref
*kctlref
)
702 struct kctl
*kctl
= NULL
;
703 struct kctl
*kctl_next
= NULL
;
707 if (userkctl
== NULL
) /* sanity check */
709 if (userkctl
->ctl_connect
== NULL
)
711 name_len
= strlen(userkctl
->ctl_name
);
712 if (name_len
== 0 || name_len
+ 1 > MAX_KCTL_NAME
)
715 MALLOC(kctl
, struct kctl
*, sizeof(*kctl
), M_TEMP
, M_WAITOK
);
718 bzero((char *)kctl
, sizeof(*kctl
));
720 lck_mtx_lock(ctl_mtx
);
725 * CTL_FLAG_REG_ID_UNIT indicates the control ID and unit number are
726 * static. If they do not exist, add them to the list in order. If the
727 * flag is not set, we must find a new unique value. We assume the
728 * list is in order. We find the last item in the list and add one. If
729 * this leads to wrapping the id around, we start at the front of the
730 * list and look for a gap.
733 if ((userkctl
->ctl_flags
& CTL_FLAG_REG_ID_UNIT
) == 0) {
734 /* Must dynamically assign an unused ID */
736 /* Verify the same name isn't already registered */
737 if (ctl_find_by_name(userkctl
->ctl_name
) != NULL
) {
738 lck_mtx_unlock(ctl_mtx
);
743 /* Start with 1 in case the list is empty */
745 kctl_next
= TAILQ_LAST(&ctl_head
, kctl_list
);
747 if (kctl_next
!= NULL
) {
748 /* List was not empty, add one to the last item in the list */
749 id
= kctl_next
->id
+ 1;
753 * If this wrapped the id number, start looking at the front
754 * of the list for an unused id.
757 /* Find the next unused ID */
760 TAILQ_FOREACH(kctl_next
, &ctl_head
, next
) {
761 if (kctl_next
->id
> id
) {
766 id
= kctl_next
->id
+ 1;
771 userkctl
->ctl_id
= id
;
775 TAILQ_FOREACH(kctl_next
, &ctl_head
, next
) {
776 if (kctl_next
->id
> userkctl
->ctl_id
)
780 if (ctl_find_by_id_unit(userkctl
->ctl_id
, userkctl
->ctl_unit
) != NULL
) {
781 lck_mtx_unlock(ctl_mtx
);
785 kctl
->id
= userkctl
->ctl_id
;
786 kctl
->reg_unit
= userkctl
->ctl_unit
;
788 strlcpy(kctl
->name
, userkctl
->ctl_name
, MAX_KCTL_NAME
);
789 kctl
->flags
= userkctl
->ctl_flags
;
791 /* Let the caller know the default send and receive sizes */
792 if (userkctl
->ctl_sendsize
== 0)
793 userkctl
->ctl_sendsize
= CTL_SENDSIZE
;
794 kctl
->sendbufsize
= userkctl
->ctl_sendsize
;
796 if (userkctl
->ctl_recvsize
== 0)
797 userkctl
->ctl_recvsize
= CTL_RECVSIZE
;
798 kctl
->recvbufsize
= userkctl
->ctl_recvsize
;
800 kctl
->connect
= userkctl
->ctl_connect
;
801 kctl
->disconnect
= userkctl
->ctl_disconnect
;
802 kctl
->send
= userkctl
->ctl_send
;
803 kctl
->setopt
= userkctl
->ctl_setopt
;
804 kctl
->getopt
= userkctl
->ctl_getopt
;
806 TAILQ_INIT(&kctl
->kcb_head
);
809 TAILQ_INSERT_BEFORE(kctl_next
, kctl
, next
);
811 TAILQ_INSERT_TAIL(&ctl_head
, kctl
, next
);
813 lck_mtx_unlock(ctl_mtx
);
817 ctl_post_msg(KEV_CTL_REGISTERED
, kctl
->id
);
822 ctl_deregister(void *kctlref
)
826 if (kctlref
== NULL
) /* sanity check */
829 lck_mtx_lock(ctl_mtx
);
830 TAILQ_FOREACH(kctl
, &ctl_head
, next
) {
831 if (kctl
== (struct kctl
*)kctlref
)
834 if (kctl
!= (struct kctl
*)kctlref
) {
835 lck_mtx_unlock(ctl_mtx
);
838 if (!TAILQ_EMPTY(&kctl
->kcb_head
)) {
839 lck_mtx_unlock(ctl_mtx
);
843 TAILQ_REMOVE(&ctl_head
, kctl
, next
);
845 lck_mtx_unlock(ctl_mtx
);
847 ctl_post_msg(KEV_CTL_DEREGISTERED
, kctl
->id
);
853 * Must be called with global ctl_mtx lock taked
856 ctl_find_by_name(const char *name
)
860 TAILQ_FOREACH(kctl
, &ctl_head
, next
)
861 if (strncmp(kctl
->name
, name
, sizeof(kctl
->name
)) == 0)
868 ctl_id_by_name(const char *name
)
870 u_int32_t ctl_id
= 0;
872 lck_mtx_lock(ctl_mtx
);
873 struct kctl
*kctl
= ctl_find_by_name(name
);
874 if (kctl
) ctl_id
= kctl
->id
;
875 lck_mtx_unlock(ctl_mtx
);
888 lck_mtx_lock(ctl_mtx
);
890 TAILQ_FOREACH(kctl
, &ctl_head
, next
) {
895 if (kctl
&& kctl
->name
)
897 if (maxsize
> MAX_KCTL_NAME
)
898 maxsize
= MAX_KCTL_NAME
;
899 strlcpy(out_name
, kctl
->name
, maxsize
);
902 lck_mtx_unlock(ctl_mtx
);
904 return found
? 0 : ENOENT
;
908 * Must be called with global ctl_mtx lock taked
912 ctl_find_by_id_unit(u_int32_t id
, u_int32_t unit
)
916 TAILQ_FOREACH(kctl
, &ctl_head
, next
) {
917 if (kctl
->id
== id
&& (kctl
->flags
& CTL_FLAG_REG_ID_UNIT
) == 0)
919 else if (kctl
->id
== id
&& kctl
->reg_unit
== unit
)
926 * Must be called with kernel controller lock taken
928 static struct ctl_cb
*
929 kcb_find(struct kctl
*kctl
, u_int32_t unit
)
933 TAILQ_FOREACH(kcb
, &kctl
->kcb_head
, next
)
934 if (kcb
->unit
== unit
)
940 static struct socket
*
941 kcb_find_socket(struct kctl
*kctl
, u_int32_t unit
)
943 struct socket
*so
= NULL
;
945 lck_mtx_lock(ctl_mtx
);
946 struct ctl_cb
*kcb
= kcb_find(kctl
, unit
);
947 if (kcb
&& kcb
->kctl
== kctl
) {
953 lck_mtx_unlock(ctl_mtx
);
961 lck_mtx_lock(ctl_mtx
);
962 if (kcb
->kctl
== NULL
)
964 lck_mtx_unlock(ctl_mtx
);
965 socket_unlock(so
, 1);
967 lck_mtx_lock(ctl_mtx
);
970 if (kcb
->usecount
== 0)
971 wakeup((event_t
)&kcb
->usecount
);
972 lck_mtx_unlock(ctl_mtx
);
978 ctl_post_msg(u_int32_t event_code
, u_int32_t id
)
980 struct ctl_event_data ctl_ev_data
;
981 struct kev_msg ev_msg
;
983 lck_mtx_assert(ctl_mtx
, LCK_MTX_ASSERT_NOTOWNED
);
985 bzero(&ev_msg
, sizeof(struct kev_msg
));
986 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
988 ev_msg
.kev_class
= KEV_SYSTEM_CLASS
;
989 ev_msg
.kev_subclass
= KEV_CTL_SUBCLASS
;
990 ev_msg
.event_code
= event_code
;
992 /* common nke subclass data */
993 bzero(&ctl_ev_data
, sizeof(ctl_ev_data
));
994 ctl_ev_data
.ctl_id
= id
;
995 ev_msg
.dv
[0].data_ptr
= &ctl_ev_data
;
996 ev_msg
.dv
[0].data_length
= sizeof(ctl_ev_data
);
998 ev_msg
.dv
[1].data_length
= 0;
1000 kev_post_msg(&ev_msg
);
1004 ctl_lock(struct socket
*so
, int refcount
, void *lr
)
1009 lr_saved
= __builtin_return_address(0);
1013 if (so
->so_pcb
!= NULL
) {
1014 lck_mtx_lock(((struct ctl_cb
*)so
->so_pcb
)->mtx
);
1016 panic("ctl_lock: so=%p NO PCB! lr=%p lrh= %s\n",
1017 so
, lr_saved
, solockhistory_nr(so
));
1021 if (so
->so_usecount
< 0) {
1022 panic("ctl_lock: so=%p so_pcb=%p lr=%p ref=%x lrh= %s\n",
1023 so
, so
->so_pcb
, lr_saved
, so
->so_usecount
, solockhistory_nr(so
));
1030 so
->lock_lr
[so
->next_lock_lr
] = lr_saved
;
1031 so
->next_lock_lr
= (so
->next_lock_lr
+1) % SO_LCKDBG_MAX
;
1036 ctl_unlock(struct socket
*so
, int refcount
, void *lr
)
1039 lck_mtx_t
*mutex_held
;
1042 lr_saved
= __builtin_return_address(0);
1046 #ifdef MORE_KCTLLOCK_DEBUG
1047 printf("ctl_unlock: so=%x sopcb=%x lock=%x ref=%x lr=%p\n",
1048 so
, so
->so_pcb
, ((struct ctl_cb
*)so
->so_pcb
)->mtx
,
1049 so
->so_usecount
, lr_saved
);
1054 if (so
->so_usecount
< 0) {
1055 panic("ctl_unlock: so=%p usecount=%x lrh= %s\n",
1056 so
, so
->so_usecount
, solockhistory_nr(so
));
1059 if (so
->so_pcb
== NULL
) {
1060 panic("ctl_unlock: so=%p NO PCB usecount=%x lr=%p lrh= %s\n",
1061 so
, so
->so_usecount
, (void *)lr_saved
, solockhistory_nr(so
));
1064 mutex_held
= ((struct ctl_cb
*)so
->so_pcb
)->mtx
;
1066 lck_mtx_assert(mutex_held
, LCK_MTX_ASSERT_OWNED
);
1067 so
->unlock_lr
[so
->next_unlock_lr
] = lr_saved
;
1068 so
->next_unlock_lr
= (so
->next_unlock_lr
+1) % SO_LCKDBG_MAX
;
1069 lck_mtx_unlock(mutex_held
);
1071 if (so
->so_usecount
== 0)
1072 ctl_sofreelastref(so
);
1078 ctl_getlock(struct socket
*so
, __unused
int locktype
)
1080 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
1083 if (so
->so_usecount
< 0)
1084 panic("ctl_getlock: so=%p usecount=%x lrh= %s\n",
1085 so
, so
->so_usecount
, solockhistory_nr(so
));
1088 panic("ctl_getlock: so=%p NULL NO so_pcb %s\n",
1089 so
, solockhistory_nr(so
));
1090 return (so
->so_proto
->pr_domain
->dom_mtx
);