2 * Copyright (c) 1999-2004 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 <net/if_var.h>
53 #include <mach/vm_types.h>
54 #include <mach/kmod.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_last_id
= 0;
70 static u_int32_t ctl_max
= 256;
71 static u_int32_t ctl_maxunit
= 65536;
72 static lck_grp_attr_t
*ctl_lck_grp_attr
= 0;
73 static lck_attr_t
*ctl_lck_attr
= 0;
74 static lck_grp_t
*ctl_lck_grp
= 0;
75 static lck_mtx_t
*ctl_mtx
;
78 * internal structure maintained for each register controller
85 TAILQ_ENTRY(kctl
) next
; /* controller chain */
87 /* controller information provided when registering */
88 char name
[MAX_KCTL_NAME
]; /* unique nke identifier, provided by DTS */
92 /* misc communication information */
93 u_int32_t flags
; /* support flags */
94 u_int32_t recvbufsize
; /* request more than the default buffer size */
95 u_int32_t sendbufsize
; /* request more than the default buffer size */
97 /* Dispatch functions */
98 ctl_connect_func connect
; /* Make contact */
99 ctl_disconnect_func disconnect
; /* Break contact */
100 ctl_send_func send
; /* Send data to nke */
101 ctl_setopt_func setopt
; /* set kctl configuration */
102 ctl_getopt_func getopt
; /* get kctl configuration */
104 TAILQ_HEAD(, ctl_cb
) kcb_head
;
109 TAILQ_ENTRY(ctl_cb
) next
; /* controller chain */
111 struct socket
*so
; /* controlling socket */
112 struct kctl
*kctl
; /* back pointer to controller */
117 /* all the controllers are chained */
118 TAILQ_HEAD(, kctl
) ctl_head
;
120 static int ctl_attach(struct socket
*, int, struct proc
*);
121 static int ctl_detach(struct socket
*);
122 static int ctl_sofreelastref(struct socket
*so
);
123 static int ctl_connect(struct socket
*, struct sockaddr
*, struct proc
*);
124 static int ctl_disconnect(struct socket
*);
125 static int ctl_ioctl(struct socket
*so
, u_long cmd
, caddr_t data
,
126 struct ifnet
*ifp
, struct proc
*p
);
127 static int ctl_send(struct socket
*, int, struct mbuf
*,
128 struct sockaddr
*, struct mbuf
*, struct proc
*);
129 static int ctl_ctloutput(struct socket
*, struct sockopt
*);
130 static int ctl_peeraddr(struct socket
*so
, struct sockaddr
**nam
);
132 static struct kctl
*ctl_find_by_id(u_int32_t
);
133 static struct kctl
*ctl_find_by_name(const char *);
134 static struct kctl
*ctl_find_by_id_unit(u_int32_t id
, u_int32_t unit
);
136 static struct ctl_cb
*kcb_find(struct kctl
*, u_int32_t unit
);
137 static void ctl_post_msg(u_long event_code
, u_int32_t id
);
139 static int ctl_lock(struct socket
*, int, int);
140 static int ctl_unlock(struct socket
*, int, int);
141 static lck_mtx_t
* ctl_getlock(struct socket
*, int);
143 static struct pr_usrreqs ctl_usrreqs
=
145 pru_abort_notsupp
, pru_accept_notsupp
, ctl_attach
, pru_bind_notsupp
,
146 ctl_connect
, pru_connect2_notsupp
, ctl_ioctl
, ctl_detach
,
147 ctl_disconnect
, pru_listen_notsupp
, ctl_peeraddr
,
148 pru_rcvd_notsupp
, pru_rcvoob_notsupp
, ctl_send
,
149 pru_sense_null
, pru_shutdown_notsupp
, pru_sockaddr_notsupp
,
150 sosend
, soreceive
, pru_sopoll_notsupp
153 static struct protosw kctlswk_dgram
=
155 SOCK_DGRAM
, &systemdomain
, SYSPROTO_CONTROL
,
156 PR_ATOMIC
|PR_CONNREQUIRED
|PR_PCBLOCK
,
157 NULL
, NULL
, NULL
, ctl_ctloutput
,
159 NULL
, NULL
, NULL
, NULL
, &ctl_usrreqs
,
160 ctl_lock
, ctl_unlock
, ctl_getlock
, { 0, 0 } , 0, { 0 }
163 static struct protosw kctlswk_stream
=
165 SOCK_STREAM
, &systemdomain
, SYSPROTO_CONTROL
,
166 PR_CONNREQUIRED
|PR_PCBLOCK
,
167 NULL
, NULL
, NULL
, ctl_ctloutput
,
169 NULL
, NULL
, NULL
, NULL
, &ctl_usrreqs
,
170 ctl_lock
, ctl_unlock
, ctl_getlock
, { 0, 0 } , 0, { 0 }
175 * Install the protosw's for the Kernel Control manager.
177 __private_extern__
int
178 kern_control_init(void)
182 ctl_lck_grp_attr
= lck_grp_attr_alloc_init();
183 if (ctl_lck_grp_attr
== 0) {
184 printf(": lck_grp_attr_alloc_init failed\n");
188 lck_grp_attr_setdefault(ctl_lck_grp_attr
);
190 ctl_lck_grp
= lck_grp_alloc_init("Kernel Control Protocol", ctl_lck_grp_attr
);
191 if (ctl_lck_grp
== 0) {
192 printf("kern_control_init: lck_grp_alloc_init failed\n");
197 ctl_lck_attr
= lck_attr_alloc_init();
198 if (ctl_lck_attr
== 0) {
199 printf("kern_control_init: lck_attr_alloc_init failed\n");
203 lck_attr_setdefault(ctl_lck_attr
);
205 ctl_mtx
= lck_mtx_alloc_init(ctl_lck_grp
, ctl_lck_attr
);
207 printf("kern_control_init: lck_mtx_alloc_init failed\n");
211 TAILQ_INIT(&ctl_head
);
213 error
= net_add_proto(&kctlswk_dgram
, &systemdomain
);
215 log(LOG_WARNING
, "kern_control_init: net_add_proto dgram failed (%d)\n", error
);
217 error
= net_add_proto(&kctlswk_stream
, &systemdomain
);
219 log(LOG_WARNING
, "kern_control_init: net_add_proto stream failed (%d)\n", error
);
225 lck_mtx_free(ctl_mtx
, ctl_lck_grp
);
229 lck_grp_free(ctl_lck_grp
);
232 if (ctl_lck_grp_attr
) {
233 lck_grp_attr_free(ctl_lck_grp_attr
);
234 ctl_lck_grp_attr
= 0;
237 lck_attr_free(ctl_lck_attr
);
245 kcb_delete(struct ctl_cb
*kcb
)
249 lck_mtx_free(kcb
->mtx
, ctl_lck_grp
);
256 * Kernel Controller user-request functions
257 * attach function must exist and succeed
258 * detach not necessary
259 * we need a pcb for the per socket mutex
262 ctl_attach(__unused
struct socket
*so
, __unused
int proto
, __unused
struct proc
*p
)
265 struct ctl_cb
*kcb
= 0;
267 MALLOC(kcb
, struct ctl_cb
*, sizeof(struct ctl_cb
), M_TEMP
, M_WAITOK
);
272 bzero(kcb
, sizeof(struct ctl_cb
));
274 kcb
->mtx
= lck_mtx_alloc_init(ctl_lck_grp
, ctl_lck_attr
);
275 if (kcb
->mtx
== NULL
) {
280 so
->so_pcb
= (caddr_t
)kcb
;
291 ctl_sofreelastref(struct socket
*so
)
293 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
299 if ((kctl
= kcb
->kctl
) != 0) {
300 lck_mtx_lock(ctl_mtx
);
301 TAILQ_REMOVE(&kctl
->kcb_head
, kcb
, next
);
302 lck_mtx_lock(ctl_mtx
);
310 ctl_detach(struct socket
*so
)
312 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
317 soisdisconnected(so
);
318 so
->so_flags
|= SOF_PCBCLEARING
;
324 ctl_connect(struct socket
*so
, struct sockaddr
*nam
, __unused
struct proc
*p
)
328 struct sockaddr_ctl sa
;
329 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
332 panic("ctl_connect so_pcb null\n");
334 if (nam
->sa_len
!= sizeof(struct sockaddr_ctl
))
337 bcopy(nam
, &sa
, sizeof(struct sockaddr_ctl
));
339 lck_mtx_lock(ctl_mtx
);
340 kctl
= ctl_find_by_id_unit(sa
.sc_id
, sa
.sc_unit
);
342 lck_mtx_unlock(ctl_mtx
);
346 if (((kctl
->flags
& CTL_FLAG_REG_SOCK_STREAM
) && (so
->so_type
!= SOCK_STREAM
)) ||
347 (!(kctl
->flags
& CTL_FLAG_REG_SOCK_STREAM
) && (so
->so_type
!= SOCK_DGRAM
))) {
348 lck_mtx_unlock(ctl_mtx
);
352 if (kctl
->flags
& CTL_FLAG_PRIVILEGED
) {
354 lck_mtx_unlock(ctl_mtx
);
357 if ((error
= proc_suser(p
))) {
358 lck_mtx_unlock(ctl_mtx
);
363 if ((kctl
->flags
& CTL_FLAG_REG_ID_UNIT
) || sa
.sc_unit
!= 0) {
364 if (kcb_find(kctl
, sa
.sc_unit
) != NULL
) {
365 lck_mtx_unlock(ctl_mtx
);
369 u_int32_t unit
= kctl
->lastunit
+ 1;
372 if (unit
== ctl_maxunit
)
374 if (kcb_find(kctl
, unit
) == NULL
) {
375 kctl
->lastunit
= sa
.sc_unit
= unit
;
378 if (unit
++ == kctl
->lastunit
) {
379 lck_mtx_unlock(ctl_mtx
);
385 kcb
->unit
= sa
.sc_unit
;
387 TAILQ_INSERT_TAIL(&kctl
->kcb_head
, kcb
, next
);
388 lck_mtx_unlock(ctl_mtx
);
390 error
= soreserve(so
, kctl
->sendbufsize
, kctl
->recvbufsize
);
395 socket_unlock(so
, 0);
396 error
= (*kctl
->connect
)(kctl
, &sa
, &kcb
->userdata
);
405 soisdisconnected(so
);
406 lck_mtx_lock(ctl_mtx
);
409 TAILQ_REMOVE(&kctl
->kcb_head
, kcb
, next
);
410 lck_mtx_unlock(ctl_mtx
);
416 ctl_disconnect(struct socket
*so
)
418 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
420 if ((kcb
= (struct ctl_cb
*)so
->so_pcb
)) {
421 struct kctl
*kctl
= kcb
->kctl
;
423 if (kctl
&& kctl
->disconnect
) {
424 socket_unlock(so
, 0);
425 (*kctl
->disconnect
)(kctl
, kcb
->unit
, kcb
->userdata
);
428 lck_mtx_lock(ctl_mtx
);
431 TAILQ_REMOVE(&kctl
->kcb_head
, kcb
, next
);
432 soisdisconnected(so
);
433 lck_mtx_unlock(ctl_mtx
);
439 ctl_peeraddr(struct socket
*so
, struct sockaddr
**nam
)
441 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
443 struct sockaddr_ctl sc
;
445 if (kcb
== NULL
) /* sanity check */
448 if ((kctl
= kcb
->kctl
) == NULL
)
451 bzero(&sc
, sizeof(struct sockaddr_ctl
));
452 sc
.sc_len
= sizeof(struct sockaddr_ctl
);
453 sc
.sc_family
= AF_SYSTEM
;
454 sc
.ss_sysaddr
= AF_SYS_CONTROL
;
456 sc
.sc_unit
= kcb
->unit
;
458 *nam
= dup_sockaddr((struct sockaddr
*)&sc
, 1);
464 ctl_send(struct socket
*so
, int flags
, struct mbuf
*m
,
465 __unused
struct sockaddr
*addr
, __unused
struct mbuf
*control
,
466 __unused
struct proc
*p
)
469 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
472 if (kcb
== NULL
) /* sanity check */
475 if ((kctl
= kcb
->kctl
) == NULL
)
479 socket_unlock(so
, 0);
480 error
= (*kctl
->send
)(kctl
, kcb
->unit
, kcb
->userdata
, m
, flags
);
487 ctl_enqueuembuf(void *kctlref
, u_int32_t unit
, struct mbuf
*m
, u_int32_t flags
)
492 struct kctl
*kctl
= (struct kctl
*)kctlref
;
497 kcb
= kcb_find(kctl
, unit
);
501 so
= (struct socket
*)kcb
->so
;
506 if (sbspace(&so
->so_rcv
) < m
->m_pkthdr
.len
) {
510 if ((flags
& CTL_DATA_EOR
))
512 if (sbappend(&so
->so_rcv
, m
) && (flags
& CTL_DATA_NOWAKEUP
) == 0)
515 socket_unlock(so
, 1);
520 ctl_enqueuedata(void *kctlref
, u_int32_t unit
, void *data
, size_t len
, u_int32_t flags
)
526 struct kctl
*kctl
= (struct kctl
*)kctlref
;
527 unsigned int num_needed
;
534 kcb
= kcb_find(kctl
, unit
);
538 so
= (struct socket
*)kcb
->so
;
543 if ((size_t)sbspace(&so
->so_rcv
) < len
) {
549 m
= m_allocpacket_internal(&num_needed
, len
, NULL
, M_NOWAIT
, 1, 0);
551 printf("ctl_enqueuedata: m_allocpacket_internal(%lu) failed\n", len
);
556 for (n
= m
; n
!= NULL
; n
= n
->m_next
) {
557 size_t mlen
= mbuf_maxlen(n
);
559 if (mlen
+ curlen
> len
)
562 bcopy((char *)data
+ curlen
, n
->m_data
, mlen
);
565 mbuf_pkthdr_setlen(m
, curlen
);
567 if ((flags
& CTL_DATA_EOR
))
569 if (sbappend(&so
->so_rcv
, m
) && (flags
& CTL_DATA_NOWAKEUP
) == 0)
572 socket_unlock(so
, 1);
578 ctl_getenqueuespace(kern_ctl_ref kctlref
, u_int32_t unit
, size_t *space
)
581 struct kctl
*kctl
= (struct kctl
*)kctlref
;
584 if (kctlref
== NULL
|| space
== NULL
)
587 kcb
= kcb_find(kctl
, unit
);
591 so
= (struct socket
*)kcb
->so
;
596 *space
= sbspace(&so
->so_rcv
);
597 socket_unlock(so
, 1);
603 ctl_ctloutput(struct socket
*so
, struct sockopt
*sopt
)
605 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
611 if (sopt
->sopt_level
!= SYSPROTO_CONTROL
) {
615 if (kcb
== NULL
) /* sanity check */
618 if ((kctl
= kcb
->kctl
) == NULL
)
621 switch (sopt
->sopt_dir
) {
623 if (kctl
->setopt
== NULL
)
625 MALLOC(data
, void *, sopt
->sopt_valsize
, M_TEMP
, M_WAITOK
);
628 error
= sooptcopyin(sopt
, data
, sopt
->sopt_valsize
, sopt
->sopt_valsize
);
630 socket_unlock(so
, 0);
631 error
= (*kctl
->setopt
)(kcb
->kctl
, kcb
->unit
, kcb
->userdata
, sopt
->sopt_name
,
632 data
, sopt
->sopt_valsize
);
639 if (kctl
->getopt
== NULL
)
642 if (sopt
->sopt_valsize
&& sopt
->sopt_val
) {
643 MALLOC(data
, void *, sopt
->sopt_valsize
, M_TEMP
, M_WAITOK
);
646 /* 4108337 - copy in data for get socket option */
647 error
= sooptcopyin(sopt
, data
, sopt
->sopt_valsize
, sopt
->sopt_valsize
);
649 len
= sopt
->sopt_valsize
;
650 socket_unlock(so
, 0);
651 error
= (*kctl
->getopt
)(kcb
->kctl
, kcb
->unit
, kcb
->userdata
, sopt
->sopt_name
,
656 error
= sooptcopyout(sopt
, data
, len
);
658 sopt
->sopt_valsize
= len
;
668 ctl_ioctl(__unused
struct socket
*so
, u_long cmd
, caddr_t data
,
669 __unused
struct ifnet
*ifp
, __unused
struct proc
*p
)
674 /* get the number of controllers */
679 lck_mtx_lock(ctl_mtx
);
680 TAILQ_FOREACH(kctl
, &ctl_head
, next
)
682 lck_mtx_unlock(ctl_mtx
);
684 *(u_int32_t
*)data
= n
;
689 struct ctl_info
*ctl_info
= (struct ctl_info
*)data
;
690 struct kctl
*kctl
= 0;
691 size_t name_len
= strlen(ctl_info
->ctl_name
);
693 if (name_len
== 0 || name_len
+ 1 > MAX_KCTL_NAME
) {
697 lck_mtx_lock(ctl_mtx
);
698 kctl
= ctl_find_by_name(ctl_info
->ctl_name
);
699 lck_mtx_unlock(ctl_mtx
);
704 ctl_info
->ctl_id
= kctl
->id
;
709 /* add controls to get list of NKEs */
717 * Register/unregister a NKE
720 ctl_register(struct kern_ctl_reg
*userkctl
, kern_ctl_ref
*kctlref
)
722 struct kctl
*kctl
= 0;
727 if (userkctl
== NULL
) /* sanity check */
729 if (userkctl
->ctl_connect
== NULL
)
731 name_len
= strlen(userkctl
->ctl_name
);
732 if (name_len
== 0 || name_len
+ 1 > MAX_KCTL_NAME
)
735 MALLOC(kctl
, struct kctl
*, sizeof(*kctl
), M_TEMP
, M_WAITOK
);
738 bzero((char *)kctl
, sizeof(*kctl
));
740 lck_mtx_lock(ctl_mtx
);
742 if ((userkctl
->ctl_flags
& CTL_FLAG_REG_ID_UNIT
) == 0) {
743 if (ctl_find_by_name(userkctl
->ctl_name
) != NULL
) {
744 lck_mtx_unlock(ctl_mtx
);
748 for (n
= 0, id
= ctl_last_id
+ 1; n
< ctl_max
; id
++, n
++) {
753 if (ctl_find_by_id(id
) == 0)
757 lck_mtx_unlock(ctl_mtx
);
761 userkctl
->ctl_id
=id
;
765 if (ctl_find_by_id_unit(userkctl
->ctl_id
, userkctl
->ctl_unit
) != NULL
) {
766 lck_mtx_unlock(ctl_mtx
);
770 kctl
->id
= userkctl
->ctl_id
;
771 kctl
->reg_unit
= userkctl
->ctl_unit
;
773 strcpy(kctl
->name
, userkctl
->ctl_name
);
774 kctl
->flags
= userkctl
->ctl_flags
;
776 /* Let the caller know the default send and receive sizes */
777 if (userkctl
->ctl_sendsize
== 0)
778 userkctl
->ctl_sendsize
= CTL_SENDSIZE
;
779 kctl
->sendbufsize
= userkctl
->ctl_sendsize
;
781 if (userkctl
->ctl_recvsize
== 0)
782 userkctl
->ctl_recvsize
= CTL_RECVSIZE
;
783 kctl
->recvbufsize
= userkctl
->ctl_recvsize
;
785 kctl
->connect
= userkctl
->ctl_connect
;
786 kctl
->disconnect
= userkctl
->ctl_disconnect
;
787 kctl
->send
= userkctl
->ctl_send
;
788 kctl
->setopt
= userkctl
->ctl_setopt
;
789 kctl
->getopt
= userkctl
->ctl_getopt
;
791 TAILQ_INIT(&kctl
->kcb_head
);
793 TAILQ_INSERT_TAIL(&ctl_head
, kctl
, next
);
796 lck_mtx_unlock(ctl_mtx
);
800 ctl_post_msg(KEV_CTL_REGISTERED
, kctl
->id
);
805 ctl_deregister(void *kctlref
)
809 if (kctlref
== NULL
) /* sanity check */
812 lck_mtx_lock(ctl_mtx
);
813 TAILQ_FOREACH(kctl
, &ctl_head
, next
) {
814 if (kctl
== (struct kctl
*)kctlref
)
817 if (kctl
!= (struct kctl
*)kctlref
) {
818 lck_mtx_unlock(ctl_mtx
);
821 if (!TAILQ_EMPTY(&kctl
->kcb_head
)) {
822 lck_mtx_unlock(ctl_mtx
);
826 TAILQ_REMOVE(&ctl_head
, kctl
, next
);
829 lck_mtx_unlock(ctl_mtx
);
831 ctl_post_msg(KEV_CTL_DEREGISTERED
, kctl
->id
);
837 * Must be called with global lock taked
840 ctl_find_by_id(u_int32_t id
)
844 TAILQ_FOREACH(kctl
, &ctl_head
, next
)
852 * Must be called with global ctl_mtx lock taked
855 ctl_find_by_name(const char *name
)
859 TAILQ_FOREACH(kctl
, &ctl_head
, next
)
860 if (strcmp(kctl
->name
, name
) == 0)
867 * Must be called with global ctl_mtx lock taked
871 ctl_find_by_id_unit(u_int32_t id
, u_int32_t unit
)
875 TAILQ_FOREACH(kctl
, &ctl_head
, next
) {
876 if (kctl
->id
== id
&& (kctl
->flags
& CTL_FLAG_REG_ID_UNIT
) == 0)
878 else if (kctl
->id
== id
&& kctl
->reg_unit
== unit
)
885 * Must be called with kernel controller lock taken
887 static struct ctl_cb
*
888 kcb_find(struct kctl
*kctl
, u_int32_t unit
)
892 TAILQ_FOREACH(kcb
, &kctl
->kcb_head
, next
)
893 if ((kcb
->unit
== unit
))
900 * Must be called witout lock
903 ctl_post_msg(u_long event_code
, u_int32_t id
)
905 struct ctl_event_data ctl_ev_data
;
906 struct kev_msg ev_msg
;
908 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
910 ev_msg
.kev_class
= KEV_SYSTEM_CLASS
;
911 ev_msg
.kev_subclass
= KEV_CTL_SUBCLASS
;
912 ev_msg
.event_code
= event_code
;
914 /* common nke subclass data */
915 bzero(&ctl_ev_data
, sizeof(ctl_ev_data
));
916 ctl_ev_data
.ctl_id
= id
;
917 ev_msg
.dv
[0].data_ptr
= &ctl_ev_data
;
918 ev_msg
.dv
[0].data_length
= sizeof(ctl_ev_data
);
920 ev_msg
.dv
[1].data_length
= 0;
922 kev_post_msg(&ev_msg
);
926 ctl_lock(struct socket
*so
, int refcount
, int lr
)
931 __asm__
volatile("mflr %0" : "=r" (lr_saved
));
937 lck_mtx_lock(((struct ctl_cb
*)so
->so_pcb
)->mtx
);
939 panic("ctl_lock: so=%x NO PCB! lr=%x\n", so
, lr_saved
);
940 lck_mtx_lock(so
->so_proto
->pr_domain
->dom_mtx
);
943 if (so
->so_usecount
< 0)
944 panic("ctl_lock: so=%x so_pcb=%x lr=%x ref=%x\n",
945 so
, so
->so_pcb
, lr_saved
, so
->so_usecount
);
949 so
->reserved3
= (void *)lr_saved
;
954 ctl_unlock(struct socket
*so
, int refcount
, int lr
)
957 lck_mtx_t
* mutex_held
;
961 __asm__
volatile("mflr %0" : "=r" (lr_saved
));
966 #ifdef MORE_KCTLLOCK_DEBUG
967 printf("ctl_unlock: so=%x sopcb=%x lock=%x ref=%x lr=%x\n",
968 so
, so
->so_pcb
, ((struct ctl_cb
*)so
->so_pcb
)->mtx
, so
->so_usecount
, lr_saved
);
973 if (so
->so_usecount
< 0)
974 panic("ctl_unlock: so=%x usecount=%x\n", so
, so
->so_usecount
);
975 if (so
->so_pcb
== NULL
) {
976 panic("ctl_unlock: so=%x NO PCB usecount=%x lr=%x\n", so
, so
->so_usecount
, lr_saved
);
977 mutex_held
= so
->so_proto
->pr_domain
->dom_mtx
;
979 mutex_held
= ((struct ctl_cb
*)so
->so_pcb
)->mtx
;
981 lck_mtx_assert(mutex_held
, LCK_MTX_ASSERT_OWNED
);
982 lck_mtx_unlock(mutex_held
);
983 so
->reserved4
= (void *)lr_saved
;
985 if (so
->so_usecount
== 0)
986 ctl_sofreelastref(so
);
992 ctl_getlock(struct socket
*so
, __unused
int locktype
)
994 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
997 if (so
->so_usecount
< 0)
998 panic("ctl_getlock: so=%x usecount=%x\n", so
, so
->so_usecount
);
1001 panic("ctl_getlock: so=%x NULL so_pcb\n", so
);
1002 return (so
->so_proto
->pr_domain
->dom_mtx
);