2 * Copyright (c) 1999-2020 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_setup_func setup
; /* Setup contact */
76 ctl_bind_func bind
; /* Prepare contact */
77 ctl_connect_func connect
; /* Make contact */
78 ctl_disconnect_func disconnect
; /* Break contact */
79 ctl_send_func send
; /* Send data to nke */
80 ctl_send_list_func send_list
; /* Send list of packets */
81 ctl_setopt_func setopt
; /* set kctl configuration */
82 ctl_getopt_func getopt
; /* get kctl configuration */
83 ctl_rcvd_func rcvd
; /* Notify nke when client reads data */
85 TAILQ_HEAD(, ctl_cb
) kcb_head
;
89 #if DEVELOPMENT || DEBUG
91 KCTL_DISCONNECTED
= 0,
95 #endif /* DEVELOPMENT || DEBUG */
98 TAILQ_ENTRY(ctl_cb
) next
; /* controller chain */
100 struct socket
*so
; /* controlling socket */
101 struct kctl
*kctl
; /* back pointer to controller */
103 struct sockaddr_ctl sac
;
105 u_int32_t kcb_usecount
;
106 u_int32_t require_clearing_count
;
107 #if DEVELOPMENT || DEBUG
108 enum ctl_status status
;
109 #endif /* DEVELOPMENT || DEBUG */
113 #define ROUNDUP64(x) P2ROUNDUP((x), sizeof (u_int64_t))
117 #define ADVANCE64(p, n) (void*)((char *)(p) + ROUNDUP64(n))
121 * Definitions and vars for we support
124 #define CTL_SENDSIZE (2 * 1024) /* default buffer size */
125 #define CTL_RECVSIZE (8 * 1024) /* default buffer size */
128 * Definitions and vars for we support
131 const u_int32_t ctl_maxunit
= 65536;
132 static lck_grp_attr_t
*ctl_lck_grp_attr
= 0;
133 static lck_attr_t
*ctl_lck_attr
= 0;
134 static lck_grp_t
*ctl_lck_grp
= 0;
135 static lck_mtx_t
*ctl_mtx
;
137 /* all the controllers are chained */
138 TAILQ_HEAD(kctl_list
, kctl
) ctl_head
;
140 static int ctl_attach(struct socket
*, int, struct proc
*);
141 static int ctl_detach(struct socket
*);
142 static int ctl_sofreelastref(struct socket
*so
);
143 static int ctl_bind(struct socket
*, struct sockaddr
*, struct proc
*);
144 static int ctl_connect(struct socket
*, struct sockaddr
*, struct proc
*);
145 static int ctl_disconnect(struct socket
*);
146 static int ctl_ioctl(struct socket
*so
, u_long cmd
, caddr_t data
,
147 struct ifnet
*ifp
, struct proc
*p
);
148 static int ctl_send(struct socket
*, int, struct mbuf
*,
149 struct sockaddr
*, struct mbuf
*, struct proc
*);
150 static int ctl_send_list(struct socket
*, int, struct mbuf
*,
151 struct sockaddr
*, struct mbuf
*, struct proc
*);
152 static int ctl_ctloutput(struct socket
*, struct sockopt
*);
153 static int ctl_peeraddr(struct socket
*so
, struct sockaddr
**nam
);
154 static int ctl_usr_rcvd(struct socket
*so
, int flags
);
156 static struct kctl
*ctl_find_by_name(const char *);
157 static struct kctl
*ctl_find_by_id_unit(u_int32_t id
, u_int32_t unit
);
159 static struct socket
*kcb_find_socket(kern_ctl_ref kctlref
, u_int32_t unit
,
161 static struct ctl_cb
*kcb_find(struct kctl
*, u_int32_t unit
);
162 static void ctl_post_msg(u_int32_t event_code
, u_int32_t id
);
164 static int ctl_lock(struct socket
*, int, void *);
165 static int ctl_unlock(struct socket
*, int, void *);
166 static lck_mtx_t
* ctl_getlock(struct socket
*, int);
168 static struct pr_usrreqs ctl_usrreqs
= {
169 .pru_attach
= ctl_attach
,
170 .pru_bind
= ctl_bind
,
171 .pru_connect
= ctl_connect
,
172 .pru_control
= ctl_ioctl
,
173 .pru_detach
= ctl_detach
,
174 .pru_disconnect
= ctl_disconnect
,
175 .pru_peeraddr
= ctl_peeraddr
,
176 .pru_rcvd
= ctl_usr_rcvd
,
177 .pru_send
= ctl_send
,
178 .pru_send_list
= ctl_send_list
,
179 .pru_sosend
= sosend
,
180 .pru_sosend_list
= sosend_list
,
181 .pru_soreceive
= soreceive
,
182 .pru_soreceive_list
= soreceive_list
,
185 static struct protosw kctlsw
[] = {
187 .pr_type
= SOCK_DGRAM
,
188 .pr_protocol
= SYSPROTO_CONTROL
,
189 .pr_flags
= PR_ATOMIC
| PR_CONNREQUIRED
| PR_PCBLOCK
| PR_WANTRCVD
,
190 .pr_ctloutput
= ctl_ctloutput
,
191 .pr_usrreqs
= &ctl_usrreqs
,
193 .pr_unlock
= ctl_unlock
,
194 .pr_getlock
= ctl_getlock
,
197 .pr_type
= SOCK_STREAM
,
198 .pr_protocol
= SYSPROTO_CONTROL
,
199 .pr_flags
= PR_CONNREQUIRED
| PR_PCBLOCK
| PR_WANTRCVD
,
200 .pr_ctloutput
= ctl_ctloutput
,
201 .pr_usrreqs
= &ctl_usrreqs
,
203 .pr_unlock
= ctl_unlock
,
204 .pr_getlock
= ctl_getlock
,
208 __private_extern__
int kctl_reg_list SYSCTL_HANDLER_ARGS
;
209 __private_extern__
int kctl_pcblist SYSCTL_HANDLER_ARGS
;
210 __private_extern__
int kctl_getstat SYSCTL_HANDLER_ARGS
;
213 SYSCTL_NODE(_net_systm
, OID_AUTO
, kctl
,
214 CTLFLAG_RW
| CTLFLAG_LOCKED
, 0, "Kernel control family");
216 struct kctlstat kctlstat
;
217 SYSCTL_PROC(_net_systm_kctl
, OID_AUTO
, stats
,
218 CTLTYPE_STRUCT
| CTLFLAG_RD
| CTLFLAG_LOCKED
, 0, 0,
219 kctl_getstat
, "S,kctlstat", "");
221 SYSCTL_PROC(_net_systm_kctl
, OID_AUTO
, reg_list
,
222 CTLTYPE_STRUCT
| CTLFLAG_RD
| CTLFLAG_LOCKED
, 0, 0,
223 kctl_reg_list
, "S,xkctl_reg", "");
225 SYSCTL_PROC(_net_systm_kctl
, OID_AUTO
, pcblist
,
226 CTLTYPE_STRUCT
| CTLFLAG_RD
| CTLFLAG_LOCKED
, 0, 0,
227 kctl_pcblist
, "S,xkctlpcb", "");
229 u_int32_t ctl_autorcvbuf_max
= 256 * 1024;
230 SYSCTL_INT(_net_systm_kctl
, OID_AUTO
, autorcvbufmax
,
231 CTLFLAG_RW
| CTLFLAG_LOCKED
, &ctl_autorcvbuf_max
, 0, "");
233 u_int32_t ctl_autorcvbuf_high
= 0;
234 SYSCTL_INT(_net_systm_kctl
, OID_AUTO
, autorcvbufhigh
,
235 CTLFLAG_RD
| CTLFLAG_LOCKED
, &ctl_autorcvbuf_high
, 0, "");
237 u_int32_t ctl_debug
= 0;
238 SYSCTL_INT(_net_systm_kctl
, OID_AUTO
, debug
,
239 CTLFLAG_RW
| CTLFLAG_LOCKED
, &ctl_debug
, 0, "");
241 #if DEVELOPMENT || DEBUG
242 u_int32_t ctl_panic_debug
= 0;
243 SYSCTL_INT(_net_systm_kctl
, OID_AUTO
, panicdebug
,
244 CTLFLAG_RW
| CTLFLAG_LOCKED
, &ctl_panic_debug
, 0, "");
245 #endif /* DEVELOPMENT || DEBUG */
247 #define KCTL_TBL_INC 16
249 static uintptr_t kctl_tbl_size
= 0;
250 static u_int32_t kctl_tbl_growing
= 0;
251 static u_int32_t kctl_tbl_growing_waiting
= 0;
252 static uintptr_t kctl_tbl_count
= 0;
253 static struct kctl
**kctl_table
= NULL
;
254 static uintptr_t kctl_ref_gencnt
= 0;
256 static void kctl_tbl_grow(void);
257 static kern_ctl_ref
kctl_make_ref(struct kctl
*kctl
);
258 static void kctl_delete_ref(kern_ctl_ref
);
259 static struct kctl
*kctl_from_ref(kern_ctl_ref
);
262 * Install the protosw's for the Kernel Control manager.
264 __private_extern__
void
265 kern_control_init(struct domain
*dp
)
269 int kctl_proto_count
= (sizeof(kctlsw
) / sizeof(struct protosw
));
271 VERIFY(!(dp
->dom_flags
& DOM_INITIALIZED
));
272 VERIFY(dp
== systemdomain
);
274 ctl_lck_grp_attr
= lck_grp_attr_alloc_init();
275 if (ctl_lck_grp_attr
== NULL
) {
276 panic("%s: lck_grp_attr_alloc_init failed\n", __func__
);
280 ctl_lck_grp
= lck_grp_alloc_init("Kernel Control Protocol",
282 if (ctl_lck_grp
== NULL
) {
283 panic("%s: lck_grp_alloc_init failed\n", __func__
);
287 ctl_lck_attr
= lck_attr_alloc_init();
288 if (ctl_lck_attr
== NULL
) {
289 panic("%s: lck_attr_alloc_init failed\n", __func__
);
293 ctl_mtx
= lck_mtx_alloc_init(ctl_lck_grp
, ctl_lck_attr
);
294 if (ctl_mtx
== NULL
) {
295 panic("%s: lck_mtx_alloc_init failed\n", __func__
);
298 TAILQ_INIT(&ctl_head
);
300 for (i
= 0, pr
= &kctlsw
[0]; i
< kctl_proto_count
; i
++, pr
++) {
301 net_add_proto(pr
, dp
, 1);
306 kcb_delete(struct ctl_cb
*kcb
)
310 lck_mtx_free(kcb
->mtx
, ctl_lck_grp
);
317 * Kernel Controller user-request functions
318 * attach function must exist and succeed
319 * detach not necessary
320 * we need a pcb for the per socket mutex
323 ctl_attach(struct socket
*so
, int proto
, struct proc
*p
)
325 #pragma unused(proto, p)
327 struct ctl_cb
*kcb
= 0;
329 MALLOC(kcb
, struct ctl_cb
*, sizeof(struct ctl_cb
), M_TEMP
, M_WAITOK
);
334 bzero(kcb
, sizeof(struct ctl_cb
));
336 kcb
->mtx
= lck_mtx_alloc_init(ctl_lck_grp
, ctl_lck_attr
);
337 if (kcb
->mtx
== NULL
) {
342 so
->so_pcb
= (caddr_t
)kcb
;
353 ctl_sofreelastref(struct socket
*so
)
355 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
361 if ((kctl
= kcb
->kctl
) != 0) {
362 lck_mtx_lock(ctl_mtx
);
363 TAILQ_REMOVE(&kctl
->kcb_head
, kcb
, next
);
364 kctlstat
.kcs_pcbcount
--;
365 kctlstat
.kcs_gencnt
++;
366 lck_mtx_unlock(ctl_mtx
);
370 sofreelastref(so
, 1);
375 * Use this function and ctl_kcb_require_clearing to serialize
376 * critical calls into the kctl subsystem
379 ctl_kcb_increment_use_count(struct ctl_cb
*kcb
, lck_mtx_t
*mutex_held
)
381 LCK_MTX_ASSERT(mutex_held
, LCK_MTX_ASSERT_OWNED
);
382 while (kcb
->require_clearing_count
> 0) {
383 msleep(&kcb
->require_clearing_count
, mutex_held
, PSOCK
| PCATCH
, "kcb_require_clearing", NULL
);
389 ctl_kcb_require_clearing(struct ctl_cb
*kcb
, lck_mtx_t
*mutex_held
)
391 assert(kcb
->kcb_usecount
!= 0);
392 kcb
->require_clearing_count
++;
394 while (kcb
->kcb_usecount
> 0) { // we need to wait until no one else is running
395 msleep(&kcb
->kcb_usecount
, mutex_held
, PSOCK
| PCATCH
, "kcb_usecount", NULL
);
401 ctl_kcb_done_clearing(struct ctl_cb
*kcb
)
403 assert(kcb
->require_clearing_count
!= 0);
404 kcb
->require_clearing_count
--;
405 wakeup((caddr_t
)&kcb
->require_clearing_count
);
409 ctl_kcb_decrement_use_count(struct ctl_cb
*kcb
)
411 assert(kcb
->kcb_usecount
!= 0);
413 wakeup((caddr_t
)&kcb
->kcb_usecount
);
417 ctl_detach(struct socket
*so
)
419 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
425 lck_mtx_t
*mtx_held
= socket_getlock(so
, PR_F_WILLUNLOCK
);
426 ctl_kcb_increment_use_count(kcb
, mtx_held
);
427 ctl_kcb_require_clearing(kcb
, mtx_held
);
429 if (kcb
->kctl
!= NULL
&& kcb
->kctl
->bind
!= NULL
&&
430 kcb
->userdata
!= NULL
&& !(so
->so_state
& SS_ISCONNECTED
)) {
431 // The unit was bound, but not connected
432 // Invoke the disconnected call to cleanup
433 if (kcb
->kctl
->disconnect
!= NULL
) {
434 socket_unlock(so
, 0);
435 (*kcb
->kctl
->disconnect
)(kcb
->kctl
->kctlref
,
436 kcb
->sac
.sc_unit
, kcb
->userdata
);
441 soisdisconnected(so
);
442 #if DEVELOPMENT || DEBUG
443 kcb
->status
= KCTL_DISCONNECTED
;
444 #endif /* DEVELOPMENT || DEBUG */
445 so
->so_flags
|= SOF_PCBCLEARING
;
446 ctl_kcb_done_clearing(kcb
);
447 ctl_kcb_decrement_use_count(kcb
);
452 ctl_setup_kctl(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
454 struct kctl
*kctl
= NULL
;
456 struct sockaddr_ctl sa
;
457 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
458 struct ctl_cb
*kcb_next
= NULL
;
460 u_int32_t recvbufsize
, sendbufsize
;
463 panic("ctl_setup_kctl so_pcb null\n");
466 if (kcb
->kctl
!= NULL
) {
467 // Already set up, skip
471 if (nam
->sa_len
!= sizeof(struct sockaddr_ctl
)) {
475 bcopy(nam
, &sa
, sizeof(struct sockaddr_ctl
));
477 lck_mtx_lock(ctl_mtx
);
478 kctl
= ctl_find_by_id_unit(sa
.sc_id
, sa
.sc_unit
);
480 lck_mtx_unlock(ctl_mtx
);
484 if (((kctl
->flags
& CTL_FLAG_REG_SOCK_STREAM
) &&
485 (so
->so_type
!= SOCK_STREAM
)) ||
486 (!(kctl
->flags
& CTL_FLAG_REG_SOCK_STREAM
) &&
487 (so
->so_type
!= SOCK_DGRAM
))) {
488 lck_mtx_unlock(ctl_mtx
);
492 if (kctl
->flags
& CTL_FLAG_PRIVILEGED
) {
494 lck_mtx_unlock(ctl_mtx
);
497 if (kauth_cred_issuser(kauth_cred_get()) == 0) {
498 lck_mtx_unlock(ctl_mtx
);
503 if ((kctl
->flags
& CTL_FLAG_REG_ID_UNIT
) || sa
.sc_unit
!= 0) {
504 if (kcb_find(kctl
, sa
.sc_unit
) != NULL
) {
505 lck_mtx_unlock(ctl_mtx
);
508 } else if (kctl
->setup
!= NULL
) {
509 error
= (*kctl
->setup
)(&sa
.sc_unit
, &kcb
->userdata
);
511 lck_mtx_unlock(ctl_mtx
);
515 /* Find an unused ID, assumes control IDs are in order */
518 TAILQ_FOREACH(kcb_next
, &kctl
->kcb_head
, next
) {
519 if (kcb_next
->sac
.sc_unit
> unit
) {
520 /* Found a gap, lets fill it in */
523 unit
= kcb_next
->sac
.sc_unit
+ 1;
524 if (unit
== ctl_maxunit
) {
529 if (unit
== ctl_maxunit
) {
530 lck_mtx_unlock(ctl_mtx
);
537 bcopy(&sa
, &kcb
->sac
, sizeof(struct sockaddr_ctl
));
539 if (kcb_next
!= NULL
) {
540 TAILQ_INSERT_BEFORE(kcb_next
, kcb
, next
);
542 TAILQ_INSERT_TAIL(&kctl
->kcb_head
, kcb
, next
);
544 kctlstat
.kcs_pcbcount
++;
545 kctlstat
.kcs_gencnt
++;
546 kctlstat
.kcs_connections
++;
547 lck_mtx_unlock(ctl_mtx
);
550 * rdar://15526688: Limit the send and receive sizes to sb_max
551 * by using the same scaling as sbreserve()
553 sbmaxsize
= (u_quad_t
)sb_max
* MCLBYTES
/ (MSIZE
+ MCLBYTES
);
555 if (kctl
->sendbufsize
> sbmaxsize
) {
556 sendbufsize
= (u_int32_t
)sbmaxsize
;
558 sendbufsize
= kctl
->sendbufsize
;
561 if (kctl
->recvbufsize
> sbmaxsize
) {
562 recvbufsize
= (u_int32_t
)sbmaxsize
;
564 recvbufsize
= kctl
->recvbufsize
;
567 error
= soreserve(so
, sendbufsize
, recvbufsize
);
570 printf("%s - soreserve(%llx, %u, %u) error %d\n",
571 __func__
, (uint64_t)VM_KERNEL_ADDRPERM(so
),
572 sendbufsize
, recvbufsize
, error
);
579 soisdisconnected(so
);
580 #if DEVELOPMENT || DEBUG
581 kcb
->status
= KCTL_DISCONNECTED
;
582 #endif /* DEVELOPMENT || DEBUG */
583 lck_mtx_lock(ctl_mtx
);
584 TAILQ_REMOVE(&kctl
->kcb_head
, kcb
, next
);
586 kcb
->sac
.sc_unit
= 0;
587 kctlstat
.kcs_pcbcount
--;
588 kctlstat
.kcs_gencnt
++;
589 kctlstat
.kcs_conn_fail
++;
590 lck_mtx_unlock(ctl_mtx
);
596 ctl_bind(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
599 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
602 panic("ctl_bind so_pcb null\n");
605 lck_mtx_t
*mtx_held
= socket_getlock(so
, PR_F_WILLUNLOCK
);
606 ctl_kcb_increment_use_count(kcb
, mtx_held
);
607 ctl_kcb_require_clearing(kcb
, mtx_held
);
609 error
= ctl_setup_kctl(so
, nam
, p
);
614 if (kcb
->kctl
== NULL
) {
615 panic("ctl_bind kctl null\n");
618 if (kcb
->kctl
->bind
== NULL
) {
623 socket_unlock(so
, 0);
624 error
= (*kcb
->kctl
->bind
)(kcb
->kctl
->kctlref
, &kcb
->sac
, &kcb
->userdata
);
628 ctl_kcb_done_clearing(kcb
);
629 ctl_kcb_decrement_use_count(kcb
);
634 ctl_connect(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
637 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
640 panic("ctl_connect so_pcb null\n");
643 lck_mtx_t
*mtx_held
= socket_getlock(so
, PR_F_WILLUNLOCK
);
644 ctl_kcb_increment_use_count(kcb
, mtx_held
);
645 ctl_kcb_require_clearing(kcb
, mtx_held
);
647 #if DEVELOPMENT || DEBUG
648 if (kcb
->status
!= KCTL_DISCONNECTED
&& ctl_panic_debug
) {
649 panic("kctl already connecting/connected");
651 kcb
->status
= KCTL_CONNECTING
;
652 #endif /* DEVELOPMENT || DEBUG */
654 error
= ctl_setup_kctl(so
, nam
, p
);
659 if (kcb
->kctl
== NULL
) {
660 panic("ctl_connect kctl null\n");
664 socket_unlock(so
, 0);
665 error
= (*kcb
->kctl
->connect
)(kcb
->kctl
->kctlref
, &kcb
->sac
, &kcb
->userdata
);
671 #if DEVELOPMENT || DEBUG
672 kcb
->status
= KCTL_CONNECTED
;
673 #endif /* DEVELOPMENT || DEBUG */
676 if (error
&& kcb
->kctl
->disconnect
) {
678 * XXX Make sure we Don't check the return value
679 * of disconnect here.
680 * ipsec/utun_ctl_disconnect will return error when
681 * disconnect gets called after connect failure.
682 * However if we decide to check for disconnect return
683 * value here. Please make sure to revisit
684 * ipsec/utun_ctl_disconnect.
686 socket_unlock(so
, 0);
687 (*kcb
->kctl
->disconnect
)(kcb
->kctl
->kctlref
, kcb
->sac
.sc_unit
, kcb
->userdata
);
691 soisdisconnected(so
);
692 #if DEVELOPMENT || DEBUG
693 kcb
->status
= KCTL_DISCONNECTED
;
694 #endif /* DEVELOPMENT || DEBUG */
695 lck_mtx_lock(ctl_mtx
);
696 TAILQ_REMOVE(&kcb
->kctl
->kcb_head
, kcb
, next
);
698 kcb
->sac
.sc_unit
= 0;
699 kctlstat
.kcs_pcbcount
--;
700 kctlstat
.kcs_gencnt
++;
701 kctlstat
.kcs_conn_fail
++;
702 lck_mtx_unlock(ctl_mtx
);
705 ctl_kcb_done_clearing(kcb
);
706 ctl_kcb_decrement_use_count(kcb
);
711 ctl_disconnect(struct socket
*so
)
713 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
715 if ((kcb
= (struct ctl_cb
*)so
->so_pcb
)) {
716 lck_mtx_t
*mtx_held
= socket_getlock(so
, PR_F_WILLUNLOCK
);
717 ctl_kcb_increment_use_count(kcb
, mtx_held
);
718 ctl_kcb_require_clearing(kcb
, mtx_held
);
719 struct kctl
*kctl
= kcb
->kctl
;
721 if (kctl
&& kctl
->disconnect
) {
722 socket_unlock(so
, 0);
723 (*kctl
->disconnect
)(kctl
->kctlref
, kcb
->sac
.sc_unit
,
728 soisdisconnected(so
);
729 #if DEVELOPMENT || DEBUG
730 kcb
->status
= KCTL_DISCONNECTED
;
731 #endif /* DEVELOPMENT || DEBUG */
733 socket_unlock(so
, 0);
734 lck_mtx_lock(ctl_mtx
);
736 kcb
->sac
.sc_unit
= 0;
737 while (kcb
->usecount
!= 0) {
738 msleep(&kcb
->usecount
, ctl_mtx
, 0, "kcb->usecount", 0);
740 TAILQ_REMOVE(&kctl
->kcb_head
, kcb
, next
);
741 kctlstat
.kcs_pcbcount
--;
742 kctlstat
.kcs_gencnt
++;
743 lck_mtx_unlock(ctl_mtx
);
745 ctl_kcb_done_clearing(kcb
);
746 ctl_kcb_decrement_use_count(kcb
);
752 ctl_peeraddr(struct socket
*so
, struct sockaddr
**nam
)
754 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
756 struct sockaddr_ctl sc
;
758 if (kcb
== NULL
) { /* sanity check */
762 if ((kctl
= kcb
->kctl
) == NULL
) {
766 bzero(&sc
, sizeof(struct sockaddr_ctl
));
767 sc
.sc_len
= sizeof(struct sockaddr_ctl
);
768 sc
.sc_family
= AF_SYSTEM
;
769 sc
.ss_sysaddr
= AF_SYS_CONTROL
;
771 sc
.sc_unit
= kcb
->sac
.sc_unit
;
773 *nam
= dup_sockaddr((struct sockaddr
*)&sc
, 1);
779 ctl_sbrcv_trim(struct socket
*so
)
781 struct sockbuf
*sb
= &so
->so_rcv
;
783 if (sb
->sb_hiwat
> sb
->sb_idealsize
) {
788 * The difference between the ideal size and the
789 * current size is the upper bound of the trimage
791 diff
= sb
->sb_hiwat
- sb
->sb_idealsize
;
793 * We cannot trim below the outstanding data
795 trim
= sb
->sb_hiwat
- sb
->sb_cc
;
797 trim
= imin(trim
, (int32_t)diff
);
800 sbreserve(sb
, (sb
->sb_hiwat
- trim
));
803 printf("%s - shrunk to %d\n",
804 __func__
, sb
->sb_hiwat
);
811 ctl_usr_rcvd(struct socket
*so
, int flags
)
814 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
821 lck_mtx_t
*mtx_held
= socket_getlock(so
, PR_F_WILLUNLOCK
);
822 ctl_kcb_increment_use_count(kcb
, mtx_held
);
824 if ((kctl
= kcb
->kctl
) == NULL
) {
830 socket_unlock(so
, 0);
831 (*kctl
->rcvd
)(kctl
->kctlref
, kcb
->sac
.sc_unit
, kcb
->userdata
, flags
);
838 ctl_kcb_decrement_use_count(kcb
);
843 ctl_send(struct socket
*so
, int flags
, struct mbuf
*m
,
844 struct sockaddr
*addr
, struct mbuf
*control
,
847 #pragma unused(addr, p)
849 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
856 if (kcb
== NULL
) { /* sanity check */
860 lck_mtx_t
*mtx_held
= socket_getlock(so
, PR_F_WILLUNLOCK
);
861 ctl_kcb_increment_use_count(kcb
, mtx_held
);
863 if (error
== 0 && (kctl
= kcb
->kctl
) == NULL
) {
867 if (error
== 0 && kctl
->send
) {
868 so_tc_update_stats(m
, so
, m_get_service_class(m
));
869 socket_unlock(so
, 0);
870 error
= (*kctl
->send
)(kctl
->kctlref
, kcb
->sac
.sc_unit
, kcb
->userdata
,
880 OSIncrementAtomic64((SInt64
*)&kctlstat
.kcs_send_fail
);
882 ctl_kcb_decrement_use_count(kcb
);
888 ctl_send_list(struct socket
*so
, int flags
, struct mbuf
*m
,
889 __unused
struct sockaddr
*addr
, struct mbuf
*control
,
890 __unused
struct proc
*p
)
893 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
897 m_freem_list(control
);
900 if (kcb
== NULL
) { /* sanity check */
904 lck_mtx_t
*mtx_held
= socket_getlock(so
, PR_F_WILLUNLOCK
);
905 ctl_kcb_increment_use_count(kcb
, mtx_held
);
907 if (error
== 0 && (kctl
= kcb
->kctl
) == NULL
) {
911 if (error
== 0 && kctl
->send_list
) {
914 for (nxt
= m
; nxt
!= NULL
; nxt
= nxt
->m_nextpkt
) {
915 so_tc_update_stats(nxt
, so
, m_get_service_class(nxt
));
918 socket_unlock(so
, 0);
919 error
= (*kctl
->send_list
)(kctl
->kctlref
, kcb
->sac
.sc_unit
,
920 kcb
->userdata
, m
, flags
);
922 } else if (error
== 0 && kctl
->send
) {
923 while (m
!= NULL
&& error
== 0) {
924 struct mbuf
*nextpkt
= m
->m_nextpkt
;
927 so_tc_update_stats(m
, so
, m_get_service_class(m
));
928 socket_unlock(so
, 0);
929 error
= (*kctl
->send
)(kctl
->kctlref
, kcb
->sac
.sc_unit
,
930 kcb
->userdata
, m
, flags
);
944 OSIncrementAtomic64((SInt64
*)&kctlstat
.kcs_send_list_fail
);
946 ctl_kcb_decrement_use_count(kcb
);
952 ctl_rcvbspace(struct socket
*so
, size_t datasize
,
953 u_int32_t kctlflags
, u_int32_t flags
)
955 struct sockbuf
*sb
= &so
->so_rcv
;
956 u_int32_t space
= sbspace(sb
);
959 if ((kctlflags
& CTL_FLAG_REG_CRIT
) == 0) {
960 if ((u_int32_t
) space
>= datasize
) {
965 } else if ((flags
& CTL_DATA_CRIT
) == 0) {
967 * Reserve 25% for critical messages
969 if (space
< (sb
->sb_hiwat
>> 2) ||
976 size_t autorcvbuf_max
;
979 * Allow overcommit of 25%
981 autorcvbuf_max
= min(sb
->sb_idealsize
+ (sb
->sb_idealsize
>> 2),
984 if ((u_int32_t
) space
>= datasize
) {
986 } else if (tcp_cansbgrow(sb
) &&
987 sb
->sb_hiwat
< autorcvbuf_max
) {
989 * Grow with a little bit of leeway
991 size_t grow
= datasize
- space
+ MSIZE
;
992 u_int32_t cc
= (u_int32_t
)MIN(MIN((sb
->sb_hiwat
+ grow
), autorcvbuf_max
), UINT32_MAX
);
994 if (sbreserve(sb
, cc
) == 1) {
995 if (sb
->sb_hiwat
> ctl_autorcvbuf_high
) {
996 ctl_autorcvbuf_high
= sb
->sb_hiwat
;
1002 if ((u_int32_t
) sbspace(sb
) >= datasize
) {
1009 printf("%s - grown to %d error %d\n",
1010 __func__
, sb
->sb_hiwat
, error
);
1023 ctl_enqueuembuf(kern_ctl_ref kctlref
, u_int32_t unit
, struct mbuf
*m
,
1028 int len
= m
->m_pkthdr
.len
;
1029 u_int32_t kctlflags
;
1031 so
= kcb_find_socket(kctlref
, unit
, &kctlflags
);
1036 if (ctl_rcvbspace(so
, len
, kctlflags
, flags
) != 0) {
1038 OSIncrementAtomic64((SInt64
*)&kctlstat
.kcs_enqueue_fullsock
);
1041 if ((flags
& CTL_DATA_EOR
)) {
1042 m
->m_flags
|= M_EOR
;
1045 so_recv_data_stat(so
, m
, 0);
1046 if (sbappend_nodrop(&so
->so_rcv
, m
) != 0) {
1047 if ((flags
& CTL_DATA_NOWAKEUP
) == 0) {
1052 OSIncrementAtomic64((SInt64
*)&kctlstat
.kcs_enqueue_fullsock
);
1055 if (ctl_debug
&& error
!= 0 && (flags
& CTL_DATA_CRIT
)) {
1056 printf("%s - crit data err %d len %d hiwat %d cc: %d\n",
1057 __func__
, error
, len
,
1058 so
->so_rcv
.sb_hiwat
, so
->so_rcv
.sb_cc
);
1061 socket_unlock(so
, 1);
1063 OSIncrementAtomic64((SInt64
*)&kctlstat
.kcs_enqueue_fail
);
1070 * Compute space occupied by mbuf like sbappendrecord
1073 m_space(struct mbuf
*m
)
1078 for (nxt
= m
; nxt
!= NULL
; nxt
= nxt
->m_next
) {
1079 space
+= nxt
->m_len
;
1086 ctl_enqueuembuf_list(void *kctlref
, u_int32_t unit
, struct mbuf
*m_list
,
1087 u_int32_t flags
, struct mbuf
**m_remain
)
1089 struct socket
*so
= NULL
;
1091 struct mbuf
*m
, *nextpkt
;
1094 u_int32_t kctlflags
;
1097 * Need to point the beginning of the list in case of early exit
1102 * kcb_find_socket takes the socket lock with a reference
1104 so
= kcb_find_socket(kctlref
, unit
, &kctlflags
);
1110 if (kctlflags
& CTL_FLAG_REG_SOCK_STREAM
) {
1114 if (flags
& CTL_DATA_EOR
) {
1119 for (m
= m_list
; m
!= NULL
; m
= nextpkt
) {
1120 nextpkt
= m
->m_nextpkt
;
1122 if (m
->m_pkthdr
.len
== 0 && ctl_debug
) {
1123 printf("%s: %llx m_pkthdr.len is 0",
1124 __func__
, (uint64_t)VM_KERNEL_ADDRPERM(m
));
1128 * The mbuf is either appended or freed by sbappendrecord()
1129 * so it's not reliable from a data standpoint
1132 if (ctl_rcvbspace(so
, len
, kctlflags
, flags
) != 0) {
1134 OSIncrementAtomic64(
1135 (SInt64
*)&kctlstat
.kcs_enqueue_fullsock
);
1139 * Unlink from the list, m is on its own
1141 m
->m_nextpkt
= NULL
;
1142 so_recv_data_stat(so
, m
, 0);
1143 if (sbappendrecord_nodrop(&so
->so_rcv
, m
) != 0) {
1147 * We free or return the remaining
1152 OSIncrementAtomic64(
1153 (SInt64
*)&kctlstat
.kcs_enqueue_fullsock
);
1158 if (needwakeup
&& (flags
& CTL_DATA_NOWAKEUP
) == 0) {
1164 if (ctl_debug
&& error
!= 0 && (flags
& CTL_DATA_CRIT
)) {
1165 printf("%s - crit data err %d len %d hiwat %d cc: %d\n",
1166 __func__
, error
, len
,
1167 so
->so_rcv
.sb_hiwat
, so
->so_rcv
.sb_cc
);
1170 socket_unlock(so
, 1);
1175 if (m
!= NULL
&& socket_debug
&& so
!= NULL
&&
1176 (so
->so_options
& SO_DEBUG
)) {
1179 printf("%s m_list %llx\n", __func__
,
1180 (uint64_t) VM_KERNEL_ADDRPERM(m_list
));
1181 for (n
= m
; n
!= NULL
; n
= n
->m_nextpkt
) {
1182 printf(" remain %llx m_next %llx\n",
1183 (uint64_t) VM_KERNEL_ADDRPERM(n
),
1184 (uint64_t) VM_KERNEL_ADDRPERM(n
->m_next
));
1193 OSIncrementAtomic64((SInt64
*)&kctlstat
.kcs_enqueue_fail
);
1199 ctl_enqueuedata(void *kctlref
, u_int32_t unit
, void *data
, size_t len
,
1205 unsigned int num_needed
;
1208 u_int32_t kctlflags
;
1210 so
= kcb_find_socket(kctlref
, unit
, &kctlflags
);
1215 if (ctl_rcvbspace(so
, len
, kctlflags
, flags
) != 0) {
1217 OSIncrementAtomic64((SInt64
*)&kctlstat
.kcs_enqueue_fullsock
);
1222 m
= m_allocpacket_internal(&num_needed
, len
, NULL
, M_NOWAIT
, 1, 0);
1224 kctlstat
.kcs_enqdata_mb_alloc_fail
++;
1226 printf("%s: m_allocpacket_internal(%lu) failed\n",
1233 for (n
= m
; n
!= NULL
; n
= n
->m_next
) {
1234 size_t mlen
= mbuf_maxlen(n
);
1236 if (mlen
+ curlen
> len
) {
1237 mlen
= len
- curlen
;
1239 n
->m_len
= (int32_t)mlen
;
1240 bcopy((char *)data
+ curlen
, n
->m_data
, mlen
);
1243 mbuf_pkthdr_setlen(m
, curlen
);
1245 if ((flags
& CTL_DATA_EOR
)) {
1246 m
->m_flags
|= M_EOR
;
1248 so_recv_data_stat(so
, m
, 0);
1250 * No need to call the "nodrop" variant of sbappend
1251 * because the mbuf is local to the scope of the function
1253 if (sbappend(&so
->so_rcv
, m
) != 0) {
1254 if ((flags
& CTL_DATA_NOWAKEUP
) == 0) {
1258 kctlstat
.kcs_enqdata_sbappend_fail
++;
1260 OSIncrementAtomic64((SInt64
*)&kctlstat
.kcs_enqueue_fullsock
);
1264 if (ctl_debug
&& error
!= 0 && (flags
& CTL_DATA_CRIT
)) {
1265 printf("%s - crit data err %d len %d hiwat %d cc: %d\n",
1266 __func__
, error
, (int)len
,
1267 so
->so_rcv
.sb_hiwat
, so
->so_rcv
.sb_cc
);
1270 socket_unlock(so
, 1);
1272 OSIncrementAtomic64((SInt64
*)&kctlstat
.kcs_enqueue_fail
);
1278 ctl_getenqueuepacketcount(kern_ctl_ref kctlref
, u_int32_t unit
, u_int32_t
*pcnt
)
1288 so
= kcb_find_socket(kctlref
, unit
, NULL
);
1294 m1
= so
->so_rcv
.sb_mb
;
1295 while (m1
!= NULL
) {
1296 if (m1
->m_type
== MT_DATA
||
1297 m1
->m_type
== MT_HEADER
||
1298 m1
->m_type
== MT_OOBDATA
) {
1305 socket_unlock(so
, 1);
1311 ctl_getenqueuespace(kern_ctl_ref kctlref
, u_int32_t unit
, size_t *space
)
1316 if (space
== NULL
) {
1320 so
= kcb_find_socket(kctlref
, unit
, NULL
);
1325 avail
= sbspace(&so
->so_rcv
);
1326 *space
= (avail
< 0) ? 0 : avail
;
1327 socket_unlock(so
, 1);
1333 ctl_getenqueuereadable(kern_ctl_ref kctlref
, u_int32_t unit
,
1334 u_int32_t
*difference
)
1338 if (difference
== NULL
) {
1342 so
= kcb_find_socket(kctlref
, unit
, NULL
);
1347 if (so
->so_rcv
.sb_cc
>= so
->so_rcv
.sb_lowat
) {
1350 *difference
= (so
->so_rcv
.sb_lowat
- so
->so_rcv
.sb_cc
);
1352 socket_unlock(so
, 1);
1358 ctl_ctloutput(struct socket
*so
, struct sockopt
*sopt
)
1360 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
1366 if (sopt
->sopt_level
!= SYSPROTO_CONTROL
) {
1370 if (kcb
== NULL
) { /* sanity check */
1374 if ((kctl
= kcb
->kctl
) == NULL
) {
1378 lck_mtx_t
*mtx_held
= socket_getlock(so
, PR_F_WILLUNLOCK
);
1379 ctl_kcb_increment_use_count(kcb
, mtx_held
);
1381 switch (sopt
->sopt_dir
) {
1383 if (kctl
->setopt
== NULL
) {
1387 if (sopt
->sopt_valsize
!= 0) {
1388 MALLOC(data
, void *, sopt
->sopt_valsize
, M_TEMP
,
1394 error
= sooptcopyin(sopt
, data
,
1395 sopt
->sopt_valsize
, sopt
->sopt_valsize
);
1398 socket_unlock(so
, 0);
1399 error
= (*kctl
->setopt
)(kctl
->kctlref
,
1400 kcb
->sac
.sc_unit
, kcb
->userdata
, sopt
->sopt_name
,
1401 data
, sopt
->sopt_valsize
);
1411 if (kctl
->getopt
== NULL
) {
1416 if (sopt
->sopt_valsize
&& sopt
->sopt_val
) {
1417 MALLOC(data
, void *, sopt
->sopt_valsize
, M_TEMP
,
1424 * 4108337 - copy user data in case the
1425 * kernel control needs it
1427 error
= sooptcopyin(sopt
, data
,
1428 sopt
->sopt_valsize
, sopt
->sopt_valsize
);
1432 len
= sopt
->sopt_valsize
;
1433 socket_unlock(so
, 0);
1434 error
= (*kctl
->getopt
)(kctl
->kctlref
, kcb
->sac
.sc_unit
,
1435 kcb
->userdata
, sopt
->sopt_name
,
1437 if (data
!= NULL
&& len
> sopt
->sopt_valsize
) {
1438 panic_plain("ctl_ctloutput: ctl %s returned "
1439 "len (%lu) > sopt_valsize (%lu)\n",
1440 kcb
->kctl
->name
, len
,
1441 sopt
->sopt_valsize
);
1446 error
= sooptcopyout(sopt
, data
, len
);
1448 sopt
->sopt_valsize
= len
;
1459 ctl_kcb_decrement_use_count(kcb
);
1464 ctl_ioctl(struct socket
*so
, u_long cmd
, caddr_t data
,
1465 struct ifnet
*ifp
, struct proc
*p
)
1467 #pragma unused(so, ifp, p)
1468 int error
= ENOTSUP
;
1471 /* get the number of controllers */
1472 case CTLIOCGCOUNT
: {
1476 lck_mtx_lock(ctl_mtx
);
1477 TAILQ_FOREACH(kctl
, &ctl_head
, next
)
1479 lck_mtx_unlock(ctl_mtx
);
1481 bcopy(&n
, data
, sizeof(n
));
1486 struct ctl_info ctl_info
;
1487 struct kctl
*kctl
= 0;
1490 bcopy(data
, &ctl_info
, sizeof(ctl_info
));
1491 name_len
= strnlen(ctl_info
.ctl_name
, MAX_KCTL_NAME
);
1493 if (name_len
== 0 || name_len
+ 1 > MAX_KCTL_NAME
) {
1497 lck_mtx_lock(ctl_mtx
);
1498 kctl
= ctl_find_by_name(ctl_info
.ctl_name
);
1499 lck_mtx_unlock(ctl_mtx
);
1504 ctl_info
.ctl_id
= kctl
->id
;
1505 bcopy(&ctl_info
, data
, sizeof(ctl_info
));
1510 /* add controls to get list of NKEs */
1519 struct kctl
**new_table
;
1522 lck_mtx_assert(ctl_mtx
, LCK_MTX_ASSERT_OWNED
);
1524 if (kctl_tbl_growing
) {
1525 /* Another thread is allocating */
1526 kctl_tbl_growing_waiting
++;
1529 (void) msleep((caddr_t
) &kctl_tbl_growing
, ctl_mtx
,
1530 PSOCK
| PCATCH
, "kctl_tbl_growing", 0);
1531 } while (kctl_tbl_growing
);
1532 kctl_tbl_growing_waiting
--;
1534 /* Another thread grew the table */
1535 if (kctl_table
!= NULL
&& kctl_tbl_count
< kctl_tbl_size
) {
1539 /* Verify we have a sane size */
1540 if (kctl_tbl_size
+ KCTL_TBL_INC
>= UINT16_MAX
) {
1541 kctlstat
.kcs_tbl_size_too_big
++;
1543 printf("%s kctl_tbl_size %lu too big\n",
1544 __func__
, kctl_tbl_size
);
1548 kctl_tbl_growing
= 1;
1550 new_size
= kctl_tbl_size
+ KCTL_TBL_INC
;
1552 lck_mtx_unlock(ctl_mtx
);
1553 new_table
= _MALLOC(sizeof(struct kctl
*) * new_size
,
1554 M_TEMP
, M_WAIT
| M_ZERO
);
1555 lck_mtx_lock(ctl_mtx
);
1557 if (new_table
!= NULL
) {
1558 if (kctl_table
!= NULL
) {
1559 bcopy(kctl_table
, new_table
,
1560 kctl_tbl_size
* sizeof(struct kctl
*));
1562 _FREE(kctl_table
, M_TEMP
);
1564 kctl_table
= new_table
;
1565 kctl_tbl_size
= new_size
;
1568 kctl_tbl_growing
= 0;
1570 if (kctl_tbl_growing_waiting
) {
1571 wakeup(&kctl_tbl_growing
);
1575 #define KCTLREF_INDEX_MASK 0x0000FFFF
1576 #define KCTLREF_GENCNT_MASK 0xFFFF0000
1577 #define KCTLREF_GENCNT_SHIFT 16
1580 kctl_make_ref(struct kctl
*kctl
)
1584 lck_mtx_assert(ctl_mtx
, LCK_MTX_ASSERT_OWNED
);
1586 if (kctl_tbl_count
>= kctl_tbl_size
) {
1590 kctl
->kctlref
= NULL
;
1591 for (i
= 0; i
< kctl_tbl_size
; i
++) {
1592 if (kctl_table
[i
] == NULL
) {
1596 * Reference is index plus one
1598 kctl_ref_gencnt
+= 1;
1601 * Add generation count as salt to reference to prevent
1602 * use after deregister
1604 ref
= ((kctl_ref_gencnt
<< KCTLREF_GENCNT_SHIFT
) &
1605 KCTLREF_GENCNT_MASK
) +
1606 ((i
+ 1) & KCTLREF_INDEX_MASK
);
1608 kctl
->kctlref
= (void *)(ref
);
1609 kctl_table
[i
] = kctl
;
1615 if (kctl
->kctlref
== NULL
) {
1616 panic("%s no space in table", __func__
);
1619 if (ctl_debug
> 0) {
1620 printf("%s %p for %p\n",
1621 __func__
, kctl
->kctlref
, kctl
);
1624 return kctl
->kctlref
;
1628 kctl_delete_ref(kern_ctl_ref kctlref
)
1631 * Reference is index plus one
1633 uintptr_t i
= (((uintptr_t)kctlref
) & KCTLREF_INDEX_MASK
) - 1;
1635 lck_mtx_assert(ctl_mtx
, LCK_MTX_ASSERT_OWNED
);
1637 if (i
< kctl_tbl_size
) {
1638 struct kctl
*kctl
= kctl_table
[i
];
1640 if (kctl
->kctlref
== kctlref
) {
1641 kctl_table
[i
] = NULL
;
1644 kctlstat
.kcs_bad_kctlref
++;
1647 kctlstat
.kcs_bad_kctlref
++;
1651 static struct kctl
*
1652 kctl_from_ref(kern_ctl_ref kctlref
)
1655 * Reference is index plus one
1657 uintptr_t i
= (((uintptr_t)kctlref
) & KCTLREF_INDEX_MASK
) - 1;
1658 struct kctl
*kctl
= NULL
;
1660 lck_mtx_assert(ctl_mtx
, LCK_MTX_ASSERT_OWNED
);
1662 if (i
>= kctl_tbl_size
) {
1663 kctlstat
.kcs_bad_kctlref
++;
1666 kctl
= kctl_table
[i
];
1667 if (kctl
->kctlref
!= kctlref
) {
1668 kctlstat
.kcs_bad_kctlref
++;
1675 * Register/unregister a NKE
1678 ctl_register(struct kern_ctl_reg
*userkctl
, kern_ctl_ref
*kctlref
)
1680 struct kctl
*kctl
= NULL
;
1681 struct kctl
*kctl_next
= NULL
;
1684 int is_extended
= 0;
1687 if (userkctl
== NULL
) { /* sanity check */
1690 if (userkctl
->ctl_connect
== NULL
) {
1693 name_len
= strlen(userkctl
->ctl_name
);
1694 if (name_len
== 0 || name_len
+ 1 > MAX_KCTL_NAME
) {
1698 MALLOC(kctl
, struct kctl
*, sizeof(*kctl
), M_TEMP
, M_WAITOK
);
1702 bzero((char *)kctl
, sizeof(*kctl
));
1704 lck_mtx_lock(ctl_mtx
);
1706 if (kctl_make_ref(kctl
) == NULL
) {
1707 lck_mtx_unlock(ctl_mtx
);
1713 * Kernel Control IDs
1715 * CTL_FLAG_REG_ID_UNIT indicates the control ID and unit number are
1716 * static. If they do not exist, add them to the list in order. If the
1717 * flag is not set, we must find a new unique value. We assume the
1718 * list is in order. We find the last item in the list and add one. If
1719 * this leads to wrapping the id around, we start at the front of the
1720 * list and look for a gap.
1723 if ((userkctl
->ctl_flags
& CTL_FLAG_REG_ID_UNIT
) == 0) {
1724 /* Must dynamically assign an unused ID */
1726 /* Verify the same name isn't already registered */
1727 if (ctl_find_by_name(userkctl
->ctl_name
) != NULL
) {
1728 kctl_delete_ref(kctl
->kctlref
);
1729 lck_mtx_unlock(ctl_mtx
);
1734 /* Start with 1 in case the list is empty */
1736 kctl_next
= TAILQ_LAST(&ctl_head
, kctl_list
);
1738 if (kctl_next
!= NULL
) {
1739 /* List was not empty, add one to the last item */
1740 id
= kctl_next
->id
+ 1;
1744 * If this wrapped the id number, start looking at
1745 * the front of the list for an unused id.
1748 /* Find the next unused ID */
1751 TAILQ_FOREACH(kctl_next
, &ctl_head
, next
) {
1752 if (kctl_next
->id
> id
) {
1753 /* We found a gap */
1757 id
= kctl_next
->id
+ 1;
1762 userkctl
->ctl_id
= id
;
1764 kctl
->reg_unit
= -1;
1766 TAILQ_FOREACH(kctl_next
, &ctl_head
, next
) {
1767 if (kctl_next
->id
> userkctl
->ctl_id
) {
1772 if (ctl_find_by_id_unit(userkctl
->ctl_id
, userkctl
->ctl_unit
)) {
1773 kctl_delete_ref(kctl
->kctlref
);
1774 lck_mtx_unlock(ctl_mtx
);
1778 kctl
->id
= userkctl
->ctl_id
;
1779 kctl
->reg_unit
= userkctl
->ctl_unit
;
1782 is_extended
= (userkctl
->ctl_flags
& CTL_FLAG_REG_EXTENDED
);
1783 is_setup
= (userkctl
->ctl_flags
& CTL_FLAG_REG_SETUP
);
1785 strlcpy(kctl
->name
, userkctl
->ctl_name
, MAX_KCTL_NAME
);
1786 kctl
->flags
= userkctl
->ctl_flags
;
1789 * Let the caller know the default send and receive sizes
1791 if (userkctl
->ctl_sendsize
== 0) {
1792 kctl
->sendbufsize
= CTL_SENDSIZE
;
1793 userkctl
->ctl_sendsize
= kctl
->sendbufsize
;
1795 kctl
->sendbufsize
= userkctl
->ctl_sendsize
;
1797 if (userkctl
->ctl_recvsize
== 0) {
1798 kctl
->recvbufsize
= CTL_RECVSIZE
;
1799 userkctl
->ctl_recvsize
= kctl
->recvbufsize
;
1801 kctl
->recvbufsize
= userkctl
->ctl_recvsize
;
1805 kctl
->setup
= userkctl
->ctl_setup
;
1807 kctl
->bind
= userkctl
->ctl_bind
;
1808 kctl
->connect
= userkctl
->ctl_connect
;
1809 kctl
->disconnect
= userkctl
->ctl_disconnect
;
1810 kctl
->send
= userkctl
->ctl_send
;
1811 kctl
->setopt
= userkctl
->ctl_setopt
;
1812 kctl
->getopt
= userkctl
->ctl_getopt
;
1814 kctl
->rcvd
= userkctl
->ctl_rcvd
;
1815 kctl
->send_list
= userkctl
->ctl_send_list
;
1818 TAILQ_INIT(&kctl
->kcb_head
);
1821 TAILQ_INSERT_BEFORE(kctl_next
, kctl
, next
);
1823 TAILQ_INSERT_TAIL(&ctl_head
, kctl
, next
);
1826 kctlstat
.kcs_reg_count
++;
1827 kctlstat
.kcs_gencnt
++;
1829 lck_mtx_unlock(ctl_mtx
);
1831 *kctlref
= kctl
->kctlref
;
1833 ctl_post_msg(KEV_CTL_REGISTERED
, kctl
->id
);
1838 ctl_deregister(void *kctlref
)
1842 lck_mtx_lock(ctl_mtx
);
1843 if ((kctl
= kctl_from_ref(kctlref
)) == NULL
) {
1844 kctlstat
.kcs_bad_kctlref
++;
1845 lck_mtx_unlock(ctl_mtx
);
1846 if (ctl_debug
!= 0) {
1847 printf("%s invalid kctlref %p\n",
1853 if (!TAILQ_EMPTY(&kctl
->kcb_head
)) {
1854 lck_mtx_unlock(ctl_mtx
);
1858 TAILQ_REMOVE(&ctl_head
, kctl
, next
);
1860 kctlstat
.kcs_reg_count
--;
1861 kctlstat
.kcs_gencnt
++;
1863 kctl_delete_ref(kctl
->kctlref
);
1864 lck_mtx_unlock(ctl_mtx
);
1866 ctl_post_msg(KEV_CTL_DEREGISTERED
, kctl
->id
);
1872 * Must be called with global ctl_mtx lock taked
1874 static struct kctl
*
1875 ctl_find_by_name(const char *name
)
1879 lck_mtx_assert(ctl_mtx
, LCK_MTX_ASSERT_OWNED
);
1881 TAILQ_FOREACH(kctl
, &ctl_head
, next
)
1882 if (strncmp(kctl
->name
, name
, sizeof(kctl
->name
)) == 0) {
1890 ctl_id_by_name(const char *name
)
1892 u_int32_t ctl_id
= 0;
1895 lck_mtx_lock(ctl_mtx
);
1896 kctl
= ctl_find_by_name(name
);
1900 lck_mtx_unlock(ctl_mtx
);
1906 ctl_name_by_id(u_int32_t id
, char *out_name
, size_t maxsize
)
1911 lck_mtx_lock(ctl_mtx
);
1912 TAILQ_FOREACH(kctl
, &ctl_head
, next
) {
1913 if (kctl
->id
== id
) {
1919 if (maxsize
> MAX_KCTL_NAME
) {
1920 maxsize
= MAX_KCTL_NAME
;
1922 strlcpy(out_name
, kctl
->name
, maxsize
);
1925 lck_mtx_unlock(ctl_mtx
);
1927 return found
? 0 : ENOENT
;
1931 * Must be called with global ctl_mtx lock taked
1934 static struct kctl
*
1935 ctl_find_by_id_unit(u_int32_t id
, u_int32_t unit
)
1939 lck_mtx_assert(ctl_mtx
, LCK_MTX_ASSERT_OWNED
);
1941 TAILQ_FOREACH(kctl
, &ctl_head
, next
) {
1942 if (kctl
->id
== id
&& (kctl
->flags
& CTL_FLAG_REG_ID_UNIT
) == 0) {
1944 } else if (kctl
->id
== id
&& kctl
->reg_unit
== unit
) {
1952 * Must be called with kernel controller lock taken
1954 static struct ctl_cb
*
1955 kcb_find(struct kctl
*kctl
, u_int32_t unit
)
1959 lck_mtx_assert(ctl_mtx
, LCK_MTX_ASSERT_OWNED
);
1961 TAILQ_FOREACH(kcb
, &kctl
->kcb_head
, next
)
1962 if (kcb
->sac
.sc_unit
== unit
) {
1969 static struct socket
*
1970 kcb_find_socket(kern_ctl_ref kctlref
, u_int32_t unit
, u_int32_t
*kctlflags
)
1972 struct socket
*so
= NULL
;
1978 lr_saved
= __builtin_return_address(0);
1980 lck_mtx_lock(ctl_mtx
);
1982 * First validate the kctlref
1984 if ((kctl
= kctl_from_ref(kctlref
)) == NULL
) {
1985 kctlstat
.kcs_bad_kctlref
++;
1986 lck_mtx_unlock(ctl_mtx
);
1987 if (ctl_debug
!= 0) {
1988 printf("%s invalid kctlref %p\n",
1994 kcb
= kcb_find(kctl
, unit
);
1995 if (kcb
== NULL
|| kcb
->kctl
!= kctl
|| (so
= kcb
->so
) == NULL
) {
1996 lck_mtx_unlock(ctl_mtx
);
2000 * This prevents the socket from being closed
2004 * Respect lock ordering: socket before ctl_mtx
2006 lck_mtx_unlock(ctl_mtx
);
2010 * The socket lock history is more useful if we store
2011 * the address of the caller.
2013 i
= (so
->next_lock_lr
+ SO_LCKDBG_MAX
- 1) % SO_LCKDBG_MAX
;
2014 so
->lock_lr
[i
] = lr_saved
;
2016 lck_mtx_lock(ctl_mtx
);
2018 if ((kctl
= kctl_from_ref(kctlref
)) == NULL
|| kcb
->kctl
== NULL
) {
2019 lck_mtx_unlock(ctl_mtx
);
2020 socket_unlock(so
, 1);
2022 lck_mtx_lock(ctl_mtx
);
2023 } else if (kctlflags
!= NULL
) {
2024 *kctlflags
= kctl
->flags
;
2028 if (kcb
->usecount
== 0) {
2029 wakeup((event_t
)&kcb
->usecount
);
2032 lck_mtx_unlock(ctl_mtx
);
2038 ctl_post_msg(u_int32_t event_code
, u_int32_t id
)
2040 struct ctl_event_data ctl_ev_data
;
2041 struct kev_msg ev_msg
;
2043 lck_mtx_assert(ctl_mtx
, LCK_MTX_ASSERT_NOTOWNED
);
2045 bzero(&ev_msg
, sizeof(struct kev_msg
));
2046 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
2048 ev_msg
.kev_class
= KEV_SYSTEM_CLASS
;
2049 ev_msg
.kev_subclass
= KEV_CTL_SUBCLASS
;
2050 ev_msg
.event_code
= event_code
;
2052 /* common nke subclass data */
2053 bzero(&ctl_ev_data
, sizeof(ctl_ev_data
));
2054 ctl_ev_data
.ctl_id
= id
;
2055 ev_msg
.dv
[0].data_ptr
= &ctl_ev_data
;
2056 ev_msg
.dv
[0].data_length
= sizeof(ctl_ev_data
);
2058 ev_msg
.dv
[1].data_length
= 0;
2060 kev_post_msg(&ev_msg
);
2064 ctl_lock(struct socket
*so
, int refcount
, void *lr
)
2069 lr_saved
= __builtin_return_address(0);
2074 if (so
->so_pcb
!= NULL
) {
2075 lck_mtx_lock(((struct ctl_cb
*)so
->so_pcb
)->mtx
);
2077 panic("ctl_lock: so=%p NO PCB! lr=%p lrh= %s\n",
2078 so
, lr_saved
, solockhistory_nr(so
));
2082 if (so
->so_usecount
< 0) {
2083 panic("ctl_lock: so=%p so_pcb=%p lr=%p ref=%x lrh= %s\n",
2084 so
, so
->so_pcb
, lr_saved
, so
->so_usecount
,
2085 solockhistory_nr(so
));
2093 so
->lock_lr
[so
->next_lock_lr
] = lr_saved
;
2094 so
->next_lock_lr
= (so
->next_lock_lr
+ 1) % SO_LCKDBG_MAX
;
2099 ctl_unlock(struct socket
*so
, int refcount
, void *lr
)
2102 lck_mtx_t
*mutex_held
;
2105 lr_saved
= __builtin_return_address(0);
2110 #if (MORE_KCTLLOCK_DEBUG && (DEVELOPMENT || DEBUG))
2111 printf("ctl_unlock: so=%llx sopcb=%x lock=%llx ref=%u lr=%llx\n",
2112 (uint64_t)VM_KERNEL_ADDRPERM(so
),
2113 (uint64_t)VM_KERNEL_ADDRPERM(so
->so_pcb
,
2114 (uint64_t)VM_KERNEL_ADDRPERM(((struct ctl_cb
*)so
->so_pcb
)->mtx
),
2115 so
->so_usecount
, (uint64_t)VM_KERNEL_ADDRPERM(lr_saved
));
2116 #endif /* (MORE_KCTLLOCK_DEBUG && (DEVELOPMENT || DEBUG)) */
2121 if (so
->so_usecount
< 0) {
2122 panic("ctl_unlock: so=%p usecount=%x lrh= %s\n",
2123 so
, so
->so_usecount
, solockhistory_nr(so
));
2126 if (so
->so_pcb
== NULL
) {
2127 panic("ctl_unlock: so=%p NO PCB usecount=%x lr=%p lrh= %s\n",
2128 so
, so
->so_usecount
, (void *)lr_saved
,
2129 solockhistory_nr(so
));
2132 mutex_held
= ((struct ctl_cb
*)so
->so_pcb
)->mtx
;
2134 lck_mtx_assert(mutex_held
, LCK_MTX_ASSERT_OWNED
);
2135 so
->unlock_lr
[so
->next_unlock_lr
] = lr_saved
;
2136 so
->next_unlock_lr
= (so
->next_unlock_lr
+ 1) % SO_LCKDBG_MAX
;
2137 lck_mtx_unlock(mutex_held
);
2139 if (so
->so_usecount
== 0) {
2140 ctl_sofreelastref(so
);
2147 ctl_getlock(struct socket
*so
, int flags
)
2149 #pragma unused(flags)
2150 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
2153 if (so
->so_usecount
< 0) {
2154 panic("ctl_getlock: so=%p usecount=%x lrh= %s\n",
2155 so
, so
->so_usecount
, solockhistory_nr(so
));
2159 panic("ctl_getlock: so=%p NULL NO so_pcb %s\n",
2160 so
, solockhistory_nr(so
));
2161 return so
->so_proto
->pr_domain
->dom_mtx
;
2165 __private_extern__
int
2166 kctl_reg_list SYSCTL_HANDLER_ARGS
2168 #pragma unused(oidp, arg1, arg2)
2171 struct xsystmgen xsg
;
2174 size_t item_size
= ROUNDUP64(sizeof(struct xkctl_reg
));
2176 buf
= _MALLOC(item_size
, M_TEMP
, M_WAITOK
| M_ZERO
);
2181 lck_mtx_lock(ctl_mtx
);
2183 n
= kctlstat
.kcs_reg_count
;
2185 if (req
->oldptr
== USER_ADDR_NULL
) {
2186 req
->oldidx
= (size_t)(n
+ n
/ 8) * sizeof(struct xkctl_reg
);
2189 if (req
->newptr
!= USER_ADDR_NULL
) {
2193 bzero(&xsg
, sizeof(xsg
));
2194 xsg
.xg_len
= sizeof(xsg
);
2196 xsg
.xg_gen
= kctlstat
.kcs_gencnt
;
2197 xsg
.xg_sogen
= so_gencnt
;
2198 error
= SYSCTL_OUT(req
, &xsg
, sizeof(xsg
));
2203 * We are done if there is no pcb
2209 for (i
= 0, kctl
= TAILQ_FIRST(&ctl_head
);
2210 i
< n
&& kctl
!= NULL
;
2211 i
++, kctl
= TAILQ_NEXT(kctl
, next
)) {
2212 struct xkctl_reg
*xkr
= (struct xkctl_reg
*)buf
;
2214 u_int32_t pcbcount
= 0;
2216 TAILQ_FOREACH(kcb
, &kctl
->kcb_head
, next
)
2219 bzero(buf
, item_size
);
2221 xkr
->xkr_len
= sizeof(struct xkctl_reg
);
2222 xkr
->xkr_kind
= XSO_KCREG
;
2223 xkr
->xkr_id
= kctl
->id
;
2224 xkr
->xkr_reg_unit
= kctl
->reg_unit
;
2225 xkr
->xkr_flags
= kctl
->flags
;
2226 xkr
->xkr_kctlref
= (uint64_t)(kctl
->kctlref
);
2227 xkr
->xkr_recvbufsize
= kctl
->recvbufsize
;
2228 xkr
->xkr_sendbufsize
= kctl
->sendbufsize
;
2229 xkr
->xkr_lastunit
= kctl
->lastunit
;
2230 xkr
->xkr_pcbcount
= pcbcount
;
2231 xkr
->xkr_connect
= (uint64_t)VM_KERNEL_UNSLIDE(kctl
->connect
);
2232 xkr
->xkr_disconnect
=
2233 (uint64_t)VM_KERNEL_UNSLIDE(kctl
->disconnect
);
2234 xkr
->xkr_send
= (uint64_t)VM_KERNEL_UNSLIDE(kctl
->send
);
2235 xkr
->xkr_send_list
=
2236 (uint64_t)VM_KERNEL_UNSLIDE(kctl
->send_list
);
2237 xkr
->xkr_setopt
= (uint64_t)VM_KERNEL_UNSLIDE(kctl
->setopt
);
2238 xkr
->xkr_getopt
= (uint64_t)VM_KERNEL_UNSLIDE(kctl
->getopt
);
2239 xkr
->xkr_rcvd
= (uint64_t)VM_KERNEL_UNSLIDE(kctl
->rcvd
);
2240 strlcpy(xkr
->xkr_name
, kctl
->name
, sizeof(xkr
->xkr_name
));
2242 error
= SYSCTL_OUT(req
, buf
, item_size
);
2247 * Give the user an updated idea of our state.
2248 * If the generation differs from what we told
2249 * her before, she knows that something happened
2250 * while we were processing this request, and it
2251 * might be necessary to retry.
2253 bzero(&xsg
, sizeof(xsg
));
2254 xsg
.xg_len
= sizeof(xsg
);
2256 xsg
.xg_gen
= kctlstat
.kcs_gencnt
;
2257 xsg
.xg_sogen
= so_gencnt
;
2258 error
= SYSCTL_OUT(req
, &xsg
, sizeof(xsg
));
2265 lck_mtx_unlock(ctl_mtx
);
2274 __private_extern__
int
2275 kctl_pcblist SYSCTL_HANDLER_ARGS
2277 #pragma unused(oidp, arg1, arg2)
2280 struct xsystmgen xsg
;
2283 size_t item_size
= ROUNDUP64(sizeof(struct xkctlpcb
)) +
2284 ROUNDUP64(sizeof(struct xsocket_n
)) +
2285 2 * ROUNDUP64(sizeof(struct xsockbuf_n
)) +
2286 ROUNDUP64(sizeof(struct xsockstat_n
));
2288 buf
= _MALLOC(item_size
, M_TEMP
, M_WAITOK
| M_ZERO
);
2293 lck_mtx_lock(ctl_mtx
);
2295 n
= kctlstat
.kcs_pcbcount
;
2297 if (req
->oldptr
== USER_ADDR_NULL
) {
2298 req
->oldidx
= (size_t)(n
+ n
/ 8) * item_size
;
2301 if (req
->newptr
!= USER_ADDR_NULL
) {
2305 bzero(&xsg
, sizeof(xsg
));
2306 xsg
.xg_len
= sizeof(xsg
);
2308 xsg
.xg_gen
= kctlstat
.kcs_gencnt
;
2309 xsg
.xg_sogen
= so_gencnt
;
2310 error
= SYSCTL_OUT(req
, &xsg
, sizeof(xsg
));
2315 * We are done if there is no pcb
2321 for (i
= 0, kctl
= TAILQ_FIRST(&ctl_head
);
2322 i
< n
&& kctl
!= NULL
;
2323 kctl
= TAILQ_NEXT(kctl
, next
)) {
2326 for (kcb
= TAILQ_FIRST(&kctl
->kcb_head
);
2327 i
< n
&& kcb
!= NULL
;
2328 i
++, kcb
= TAILQ_NEXT(kcb
, next
)) {
2329 struct xkctlpcb
*xk
= (struct xkctlpcb
*)buf
;
2330 struct xsocket_n
*xso
= (struct xsocket_n
*)
2331 ADVANCE64(xk
, sizeof(*xk
));
2332 struct xsockbuf_n
*xsbrcv
= (struct xsockbuf_n
*)
2333 ADVANCE64(xso
, sizeof(*xso
));
2334 struct xsockbuf_n
*xsbsnd
= (struct xsockbuf_n
*)
2335 ADVANCE64(xsbrcv
, sizeof(*xsbrcv
));
2336 struct xsockstat_n
*xsostats
= (struct xsockstat_n
*)
2337 ADVANCE64(xsbsnd
, sizeof(*xsbsnd
));
2339 bzero(buf
, item_size
);
2341 xk
->xkp_len
= sizeof(struct xkctlpcb
);
2342 xk
->xkp_kind
= XSO_KCB
;
2343 xk
->xkp_unit
= kcb
->sac
.sc_unit
;
2344 xk
->xkp_kctpcb
= (uint64_t)VM_KERNEL_ADDRPERM(kcb
);
2345 xk
->xkp_kctlref
= (uint64_t)VM_KERNEL_ADDRPERM(kctl
);
2346 xk
->xkp_kctlid
= kctl
->id
;
2347 strlcpy(xk
->xkp_kctlname
, kctl
->name
,
2348 sizeof(xk
->xkp_kctlname
));
2350 sotoxsocket_n(kcb
->so
, xso
);
2351 sbtoxsockbuf_n(kcb
->so
?
2352 &kcb
->so
->so_rcv
: NULL
, xsbrcv
);
2353 sbtoxsockbuf_n(kcb
->so
?
2354 &kcb
->so
->so_snd
: NULL
, xsbsnd
);
2355 sbtoxsockstat_n(kcb
->so
, xsostats
);
2357 error
= SYSCTL_OUT(req
, buf
, item_size
);
2363 * Give the user an updated idea of our state.
2364 * If the generation differs from what we told
2365 * her before, she knows that something happened
2366 * while we were processing this request, and it
2367 * might be necessary to retry.
2369 bzero(&xsg
, sizeof(xsg
));
2370 xsg
.xg_len
= sizeof(xsg
);
2372 xsg
.xg_gen
= kctlstat
.kcs_gencnt
;
2373 xsg
.xg_sogen
= so_gencnt
;
2374 error
= SYSCTL_OUT(req
, &xsg
, sizeof(xsg
));
2381 lck_mtx_unlock(ctl_mtx
);
2387 kctl_getstat SYSCTL_HANDLER_ARGS
2389 #pragma unused(oidp, arg1, arg2)
2392 lck_mtx_lock(ctl_mtx
);
2394 if (req
->newptr
!= USER_ADDR_NULL
) {
2398 if (req
->oldptr
== USER_ADDR_NULL
) {
2399 req
->oldidx
= sizeof(struct kctlstat
);
2403 error
= SYSCTL_OUT(req
, &kctlstat
,
2404 MIN(sizeof(struct kctlstat
), req
->oldlen
));
2406 lck_mtx_unlock(ctl_mtx
);
2411 kctl_fill_socketinfo(struct socket
*so
, struct socket_info
*si
)
2413 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
2414 struct kern_ctl_info
*kcsi
=
2415 &si
->soi_proto
.pri_kern_ctl
;
2416 struct kctl
*kctl
= kcb
->kctl
;
2418 si
->soi_kind
= SOCKINFO_KERN_CTL
;
2424 kcsi
->kcsi_id
= kctl
->id
;
2425 kcsi
->kcsi_reg_unit
= kctl
->reg_unit
;
2426 kcsi
->kcsi_flags
= kctl
->flags
;
2427 kcsi
->kcsi_recvbufsize
= kctl
->recvbufsize
;
2428 kcsi
->kcsi_sendbufsize
= kctl
->sendbufsize
;
2429 kcsi
->kcsi_unit
= kcb
->sac
.sc_unit
;
2430 strlcpy(kcsi
->kcsi_name
, kctl
->name
, MAX_KCTL_NAME
);