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
;
89 TAILQ_ENTRY(ctl_cb
) next
; /* controller chain */
91 struct socket
*so
; /* controlling socket */
92 struct kctl
*kctl
; /* back pointer to controller */
94 struct sockaddr_ctl sac
;
99 #define ROUNDUP64(x) P2ROUNDUP((x), sizeof (u_int64_t))
103 #define ADVANCE64(p, n) (void*)((char *)(p) + ROUNDUP64(n))
107 * Definitions and vars for we support
110 #define CTL_SENDSIZE (2 * 1024) /* default buffer size */
111 #define CTL_RECVSIZE (8 * 1024) /* default buffer size */
114 * Definitions and vars for we support
117 static u_int32_t ctl_maxunit
= 65536;
118 static lck_grp_attr_t
*ctl_lck_grp_attr
= 0;
119 static lck_attr_t
*ctl_lck_attr
= 0;
120 static lck_grp_t
*ctl_lck_grp
= 0;
121 static lck_mtx_t
*ctl_mtx
;
123 /* all the controllers are chained */
124 TAILQ_HEAD(kctl_list
, kctl
) ctl_head
;
126 static int ctl_attach(struct socket
*, int, struct proc
*);
127 static int ctl_detach(struct socket
*);
128 static int ctl_sofreelastref(struct socket
*so
);
129 static int ctl_bind(struct socket
*, struct sockaddr
*, struct proc
*);
130 static int ctl_connect(struct socket
*, struct sockaddr
*, struct proc
*);
131 static int ctl_disconnect(struct socket
*);
132 static int ctl_ioctl(struct socket
*so
, u_long cmd
, caddr_t data
,
133 struct ifnet
*ifp
, struct proc
*p
);
134 static int ctl_send(struct socket
*, int, struct mbuf
*,
135 struct sockaddr
*, struct mbuf
*, struct proc
*);
136 static int ctl_send_list(struct socket
*, int, struct mbuf
*,
137 struct sockaddr
*, struct mbuf
*, struct proc
*);
138 static int ctl_ctloutput(struct socket
*, struct sockopt
*);
139 static int ctl_peeraddr(struct socket
*so
, struct sockaddr
**nam
);
140 static int ctl_usr_rcvd(struct socket
*so
, int flags
);
142 static struct kctl
*ctl_find_by_name(const char *);
143 static struct kctl
*ctl_find_by_id_unit(u_int32_t id
, u_int32_t unit
);
145 static struct socket
*kcb_find_socket(kern_ctl_ref kctlref
, u_int32_t unit
,
147 static struct ctl_cb
*kcb_find(struct kctl
*, u_int32_t unit
);
148 static void ctl_post_msg(u_int32_t event_code
, u_int32_t id
);
150 static int ctl_lock(struct socket
*, int, void *);
151 static int ctl_unlock(struct socket
*, int, void *);
152 static lck_mtx_t
* ctl_getlock(struct socket
*, int);
154 static struct pr_usrreqs ctl_usrreqs
= {
155 .pru_attach
= ctl_attach
,
156 .pru_bind
= ctl_bind
,
157 .pru_connect
= ctl_connect
,
158 .pru_control
= ctl_ioctl
,
159 .pru_detach
= ctl_detach
,
160 .pru_disconnect
= ctl_disconnect
,
161 .pru_peeraddr
= ctl_peeraddr
,
162 .pru_rcvd
= ctl_usr_rcvd
,
163 .pru_send
= ctl_send
,
164 .pru_send_list
= ctl_send_list
,
165 .pru_sosend
= sosend
,
166 .pru_sosend_list
= sosend_list
,
167 .pru_soreceive
= soreceive
,
168 .pru_soreceive_list
= soreceive_list
,
171 static struct protosw kctlsw
[] = {
173 .pr_type
= SOCK_DGRAM
,
174 .pr_protocol
= SYSPROTO_CONTROL
,
175 .pr_flags
= PR_ATOMIC
|PR_CONNREQUIRED
|PR_PCBLOCK
|PR_WANTRCVD
,
176 .pr_ctloutput
= ctl_ctloutput
,
177 .pr_usrreqs
= &ctl_usrreqs
,
179 .pr_unlock
= ctl_unlock
,
180 .pr_getlock
= ctl_getlock
,
183 .pr_type
= SOCK_STREAM
,
184 .pr_protocol
= SYSPROTO_CONTROL
,
185 .pr_flags
= PR_CONNREQUIRED
|PR_PCBLOCK
|PR_WANTRCVD
,
186 .pr_ctloutput
= ctl_ctloutput
,
187 .pr_usrreqs
= &ctl_usrreqs
,
189 .pr_unlock
= ctl_unlock
,
190 .pr_getlock
= ctl_getlock
,
194 __private_extern__
int kctl_reg_list SYSCTL_HANDLER_ARGS
;
195 __private_extern__
int kctl_pcblist SYSCTL_HANDLER_ARGS
;
196 __private_extern__
int kctl_getstat SYSCTL_HANDLER_ARGS
;
199 SYSCTL_NODE(_net_systm
, OID_AUTO
, kctl
,
200 CTLFLAG_RW
|CTLFLAG_LOCKED
, 0, "Kernel control family");
202 struct kctlstat kctlstat
;
203 SYSCTL_PROC(_net_systm_kctl
, OID_AUTO
, stats
,
204 CTLTYPE_STRUCT
| CTLFLAG_RD
| CTLFLAG_LOCKED
, 0, 0,
205 kctl_getstat
, "S,kctlstat", "");
207 SYSCTL_PROC(_net_systm_kctl
, OID_AUTO
, reg_list
,
208 CTLTYPE_STRUCT
| CTLFLAG_RD
| CTLFLAG_LOCKED
, 0, 0,
209 kctl_reg_list
, "S,xkctl_reg", "");
211 SYSCTL_PROC(_net_systm_kctl
, OID_AUTO
, pcblist
,
212 CTLTYPE_STRUCT
| CTLFLAG_RD
| CTLFLAG_LOCKED
, 0, 0,
213 kctl_pcblist
, "S,xkctlpcb", "");
215 u_int32_t ctl_autorcvbuf_max
= 256 * 1024;
216 SYSCTL_INT(_net_systm_kctl
, OID_AUTO
, autorcvbufmax
,
217 CTLFLAG_RW
| CTLFLAG_LOCKED
, &ctl_autorcvbuf_max
, 0, "");
219 u_int32_t ctl_autorcvbuf_high
= 0;
220 SYSCTL_INT(_net_systm_kctl
, OID_AUTO
, autorcvbufhigh
,
221 CTLFLAG_RD
| CTLFLAG_LOCKED
, &ctl_autorcvbuf_high
, 0, "");
223 u_int32_t ctl_debug
= 0;
224 SYSCTL_INT(_net_systm_kctl
, OID_AUTO
, debug
,
225 CTLFLAG_RW
| CTLFLAG_LOCKED
, &ctl_debug
, 0, "");
227 #define KCTL_TBL_INC 16
229 static uintptr_t kctl_tbl_size
= 0;
230 static u_int32_t kctl_tbl_growing
= 0;
231 static u_int32_t kctl_tbl_growing_waiting
= 0;
232 static uintptr_t kctl_tbl_count
= 0;
233 static struct kctl
**kctl_table
= NULL
;
234 static uintptr_t kctl_ref_gencnt
= 0;
236 static void kctl_tbl_grow(void);
237 static kern_ctl_ref
kctl_make_ref(struct kctl
*kctl
);
238 static void kctl_delete_ref(kern_ctl_ref
);
239 static struct kctl
*kctl_from_ref(kern_ctl_ref
);
242 * Install the protosw's for the Kernel Control manager.
244 __private_extern__
void
245 kern_control_init(struct domain
*dp
)
249 int kctl_proto_count
= (sizeof (kctlsw
) / sizeof (struct protosw
));
251 VERIFY(!(dp
->dom_flags
& DOM_INITIALIZED
));
252 VERIFY(dp
== systemdomain
);
254 ctl_lck_grp_attr
= lck_grp_attr_alloc_init();
255 if (ctl_lck_grp_attr
== NULL
) {
256 panic("%s: lck_grp_attr_alloc_init failed\n", __func__
);
260 ctl_lck_grp
= lck_grp_alloc_init("Kernel Control Protocol",
262 if (ctl_lck_grp
== NULL
) {
263 panic("%s: lck_grp_alloc_init failed\n", __func__
);
267 ctl_lck_attr
= lck_attr_alloc_init();
268 if (ctl_lck_attr
== NULL
) {
269 panic("%s: lck_attr_alloc_init failed\n", __func__
);
273 ctl_mtx
= lck_mtx_alloc_init(ctl_lck_grp
, ctl_lck_attr
);
274 if (ctl_mtx
== NULL
) {
275 panic("%s: lck_mtx_alloc_init failed\n", __func__
);
278 TAILQ_INIT(&ctl_head
);
280 for (i
= 0, pr
= &kctlsw
[0]; i
< kctl_proto_count
; i
++, pr
++)
281 net_add_proto(pr
, dp
, 1);
285 kcb_delete(struct ctl_cb
*kcb
)
289 lck_mtx_free(kcb
->mtx
, ctl_lck_grp
);
295 * Kernel Controller user-request functions
296 * attach function must exist and succeed
297 * detach not necessary
298 * we need a pcb for the per socket mutex
301 ctl_attach(struct socket
*so
, int proto
, struct proc
*p
)
303 #pragma unused(proto, p)
305 struct ctl_cb
*kcb
= 0;
307 MALLOC(kcb
, struct ctl_cb
*, sizeof(struct ctl_cb
), M_TEMP
, M_WAITOK
);
312 bzero(kcb
, sizeof(struct ctl_cb
));
314 kcb
->mtx
= lck_mtx_alloc_init(ctl_lck_grp
, ctl_lck_attr
);
315 if (kcb
->mtx
== NULL
) {
320 so
->so_pcb
= (caddr_t
)kcb
;
331 ctl_sofreelastref(struct socket
*so
)
333 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
339 if ((kctl
= kcb
->kctl
) != 0) {
340 lck_mtx_lock(ctl_mtx
);
341 TAILQ_REMOVE(&kctl
->kcb_head
, kcb
, next
);
342 kctlstat
.kcs_pcbcount
--;
343 kctlstat
.kcs_gencnt
++;
344 lck_mtx_unlock(ctl_mtx
);
348 sofreelastref(so
, 1);
353 ctl_detach(struct socket
*so
)
355 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
360 if (kcb
->kctl
!= NULL
&& kcb
->kctl
->bind
!= NULL
&&
361 kcb
->userdata
!= NULL
&& !(so
->so_state
& SS_ISCONNECTED
)) {
362 // The unit was bound, but not connected
363 // Invoke the disconnected call to cleanup
364 if (kcb
->kctl
->disconnect
!= NULL
) {
365 socket_unlock(so
, 0);
366 (*kcb
->kctl
->disconnect
)(kcb
->kctl
->kctlref
,
367 kcb
->sac
.sc_unit
, kcb
->userdata
);
372 soisdisconnected(so
);
373 so
->so_flags
|= SOF_PCBCLEARING
;
378 ctl_setup_kctl(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
380 struct kctl
*kctl
= NULL
;
382 struct sockaddr_ctl sa
;
383 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
384 struct ctl_cb
*kcb_next
= NULL
;
386 u_int32_t recvbufsize
, sendbufsize
;
389 panic("ctl_setup_kctl so_pcb null\n");
392 if (kcb
->kctl
!= NULL
) {
393 // Already set up, skip
397 if (nam
->sa_len
!= sizeof(struct sockaddr_ctl
)) {
401 bcopy(nam
, &sa
, sizeof(struct sockaddr_ctl
));
403 lck_mtx_lock(ctl_mtx
);
404 kctl
= ctl_find_by_id_unit(sa
.sc_id
, sa
.sc_unit
);
406 lck_mtx_unlock(ctl_mtx
);
410 if (((kctl
->flags
& CTL_FLAG_REG_SOCK_STREAM
) &&
411 (so
->so_type
!= SOCK_STREAM
)) ||
412 (!(kctl
->flags
& CTL_FLAG_REG_SOCK_STREAM
) &&
413 (so
->so_type
!= SOCK_DGRAM
))) {
414 lck_mtx_unlock(ctl_mtx
);
418 if (kctl
->flags
& CTL_FLAG_PRIVILEGED
) {
420 lck_mtx_unlock(ctl_mtx
);
423 if (kauth_cred_issuser(kauth_cred_get()) == 0) {
424 lck_mtx_unlock(ctl_mtx
);
429 if ((kctl
->flags
& CTL_FLAG_REG_ID_UNIT
) || sa
.sc_unit
!= 0) {
430 if (kcb_find(kctl
, sa
.sc_unit
) != NULL
) {
431 lck_mtx_unlock(ctl_mtx
);
435 /* Find an unused ID, assumes control IDs are in order */
438 TAILQ_FOREACH(kcb_next
, &kctl
->kcb_head
, next
) {
439 if (kcb_next
->sac
.sc_unit
> unit
) {
440 /* Found a gap, lets fill it in */
443 unit
= kcb_next
->sac
.sc_unit
+ 1;
444 if (unit
== ctl_maxunit
) {
449 if (unit
== ctl_maxunit
) {
450 lck_mtx_unlock(ctl_mtx
);
457 bcopy(&sa
, &kcb
->sac
, sizeof(struct sockaddr_ctl
));
459 if (kcb_next
!= NULL
) {
460 TAILQ_INSERT_BEFORE(kcb_next
, kcb
, next
);
462 TAILQ_INSERT_TAIL(&kctl
->kcb_head
, kcb
, next
);
464 kctlstat
.kcs_pcbcount
++;
465 kctlstat
.kcs_gencnt
++;
466 kctlstat
.kcs_connections
++;
467 lck_mtx_unlock(ctl_mtx
);
470 * rdar://15526688: Limit the send and receive sizes to sb_max
471 * by using the same scaling as sbreserve()
473 sbmaxsize
= (u_quad_t
)sb_max
* MCLBYTES
/ (MSIZE
+ MCLBYTES
);
475 if (kctl
->sendbufsize
> sbmaxsize
) {
476 sendbufsize
= sbmaxsize
;
478 sendbufsize
= kctl
->sendbufsize
;
481 if (kctl
->recvbufsize
> sbmaxsize
) {
482 recvbufsize
= sbmaxsize
;
484 recvbufsize
= kctl
->recvbufsize
;
487 error
= soreserve(so
, sendbufsize
, recvbufsize
);
490 printf("%s - soreserve(%llx, %u, %u) error %d\n",
491 __func__
, (uint64_t)VM_KERNEL_ADDRPERM(so
),
492 sendbufsize
, recvbufsize
, error
);
498 soisdisconnected(so
);
499 lck_mtx_lock(ctl_mtx
);
500 TAILQ_REMOVE(&kctl
->kcb_head
, kcb
, next
);
502 kcb
->sac
.sc_unit
= 0;
503 kctlstat
.kcs_pcbcount
--;
504 kctlstat
.kcs_gencnt
++;
505 kctlstat
.kcs_conn_fail
++;
506 lck_mtx_unlock(ctl_mtx
);
512 ctl_bind(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
515 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
518 panic("ctl_bind so_pcb null\n");
521 error
= ctl_setup_kctl(so
, nam
, p
);
526 if (kcb
->kctl
== NULL
) {
527 panic("ctl_bind kctl null\n");
530 if (kcb
->kctl
->bind
== NULL
) {
534 socket_unlock(so
, 0);
535 error
= (*kcb
->kctl
->bind
)(kcb
->kctl
->kctlref
, &kcb
->sac
, &kcb
->userdata
);
542 ctl_connect(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
545 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
548 panic("ctl_connect so_pcb null\n");
551 error
= ctl_setup_kctl(so
, nam
, p
);
556 if (kcb
->kctl
== NULL
) {
557 panic("ctl_connect kctl null\n");
561 socket_unlock(so
, 0);
562 error
= (*kcb
->kctl
->connect
)(kcb
->kctl
->kctlref
, &kcb
->sac
, &kcb
->userdata
);
570 if (error
&& kcb
->kctl
->disconnect
) {
572 * XXX Make sure we Don't check the return value
573 * of disconnect here.
574 * ipsec/utun_ctl_disconnect will return error when
575 * disconnect gets called after connect failure.
576 * However if we decide to check for disconnect return
577 * value here. Please make sure to revisit
578 * ipsec/utun_ctl_disconnect.
580 socket_unlock(so
, 0);
581 (*kcb
->kctl
->disconnect
)(kcb
->kctl
->kctlref
, kcb
->sac
.sc_unit
, kcb
->userdata
);
585 soisdisconnected(so
);
586 lck_mtx_lock(ctl_mtx
);
587 TAILQ_REMOVE(&kcb
->kctl
->kcb_head
, kcb
, next
);
589 kcb
->sac
.sc_unit
= 0;
590 kctlstat
.kcs_pcbcount
--;
591 kctlstat
.kcs_gencnt
++;
592 kctlstat
.kcs_conn_fail
++;
593 lck_mtx_unlock(ctl_mtx
);
599 ctl_disconnect(struct socket
*so
)
601 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
603 if ((kcb
= (struct ctl_cb
*)so
->so_pcb
)) {
604 struct kctl
*kctl
= kcb
->kctl
;
606 if (kctl
&& kctl
->disconnect
) {
607 socket_unlock(so
, 0);
608 (*kctl
->disconnect
)(kctl
->kctlref
, kcb
->sac
.sc_unit
,
613 soisdisconnected(so
);
615 socket_unlock(so
, 0);
616 lck_mtx_lock(ctl_mtx
);
618 kcb
->sac
.sc_unit
= 0;
619 while (kcb
->usecount
!= 0) {
620 msleep(&kcb
->usecount
, ctl_mtx
, 0, "kcb->usecount", 0);
622 TAILQ_REMOVE(&kctl
->kcb_head
, kcb
, next
);
623 kctlstat
.kcs_pcbcount
--;
624 kctlstat
.kcs_gencnt
++;
625 lck_mtx_unlock(ctl_mtx
);
632 ctl_peeraddr(struct socket
*so
, struct sockaddr
**nam
)
634 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
636 struct sockaddr_ctl sc
;
638 if (kcb
== NULL
) /* sanity check */
641 if ((kctl
= kcb
->kctl
) == NULL
)
644 bzero(&sc
, sizeof(struct sockaddr_ctl
));
645 sc
.sc_len
= sizeof(struct sockaddr_ctl
);
646 sc
.sc_family
= AF_SYSTEM
;
647 sc
.ss_sysaddr
= AF_SYS_CONTROL
;
649 sc
.sc_unit
= kcb
->sac
.sc_unit
;
651 *nam
= dup_sockaddr((struct sockaddr
*)&sc
, 1);
657 ctl_sbrcv_trim(struct socket
*so
)
659 struct sockbuf
*sb
= &so
->so_rcv
;
661 if (sb
->sb_hiwat
> sb
->sb_idealsize
) {
666 * The difference between the ideal size and the
667 * current size is the upper bound of the trimage
669 diff
= sb
->sb_hiwat
- sb
->sb_idealsize
;
671 * We cannot trim below the outstanding data
673 trim
= sb
->sb_hiwat
- sb
->sb_cc
;
675 trim
= imin(trim
, (int32_t)diff
);
678 sbreserve(sb
, (sb
->sb_hiwat
- trim
));
681 printf("%s - shrunk to %d\n",
682 __func__
, sb
->sb_hiwat
);
688 ctl_usr_rcvd(struct socket
*so
, int flags
)
690 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
693 if ((kctl
= kcb
->kctl
) == NULL
) {
698 socket_unlock(so
, 0);
699 (*kctl
->rcvd
)(kctl
->kctlref
, kcb
->sac
.sc_unit
, kcb
->userdata
, flags
);
709 ctl_send(struct socket
*so
, int flags
, struct mbuf
*m
,
710 struct sockaddr
*addr
, struct mbuf
*control
,
713 #pragma unused(addr, p)
715 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
721 if (kcb
== NULL
) /* sanity check */
724 if (error
== 0 && (kctl
= kcb
->kctl
) == NULL
)
727 if (error
== 0 && kctl
->send
) {
728 so_tc_update_stats(m
, so
, m_get_service_class(m
));
729 socket_unlock(so
, 0);
730 error
= (*kctl
->send
)(kctl
->kctlref
, kcb
->sac
.sc_unit
, kcb
->userdata
,
739 OSIncrementAtomic64((SInt64
*)&kctlstat
.kcs_send_fail
);
744 ctl_send_list(struct socket
*so
, int flags
, struct mbuf
*m
,
745 __unused
struct sockaddr
*addr
, struct mbuf
*control
,
746 __unused
struct proc
*p
)
749 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
753 m_freem_list(control
);
755 if (kcb
== NULL
) /* sanity check */
758 if (error
== 0 && (kctl
= kcb
->kctl
) == NULL
)
761 if (error
== 0 && kctl
->send_list
) {
764 for (nxt
= m
; nxt
!= NULL
; nxt
= nxt
->m_nextpkt
)
765 so_tc_update_stats(nxt
, so
, m_get_service_class(nxt
));
767 socket_unlock(so
, 0);
768 error
= (*kctl
->send_list
)(kctl
->kctlref
, kcb
->sac
.sc_unit
,
769 kcb
->userdata
, m
, flags
);
771 } else if (error
== 0 && kctl
->send
) {
772 while (m
!= NULL
&& error
== 0) {
773 struct mbuf
*nextpkt
= m
->m_nextpkt
;
776 so_tc_update_stats(m
, so
, m_get_service_class(m
));
777 socket_unlock(so
, 0);
778 error
= (*kctl
->send
)(kctl
->kctlref
, kcb
->sac
.sc_unit
,
779 kcb
->userdata
, m
, flags
);
791 OSIncrementAtomic64((SInt64
*)&kctlstat
.kcs_send_list_fail
);
796 ctl_rcvbspace(struct socket
*so
, u_int32_t datasize
,
797 u_int32_t kctlflags
, u_int32_t flags
)
799 struct sockbuf
*sb
= &so
->so_rcv
;
800 u_int32_t space
= sbspace(sb
);
803 if ((kctlflags
& CTL_FLAG_REG_CRIT
) == 0) {
804 if ((u_int32_t
) space
>= datasize
)
808 } else if ((flags
& CTL_DATA_CRIT
) == 0) {
810 * Reserve 25% for critical messages
812 if (space
< (sb
->sb_hiwat
>> 2) ||
818 u_int32_t autorcvbuf_max
;
821 * Allow overcommit of 25%
823 autorcvbuf_max
= min(sb
->sb_idealsize
+ (sb
->sb_idealsize
>> 2),
826 if ((u_int32_t
) space
>= datasize
) {
828 } else if (tcp_cansbgrow(sb
) &&
829 sb
->sb_hiwat
< autorcvbuf_max
) {
831 * Grow with a little bit of leeway
833 u_int32_t grow
= datasize
- space
+ MSIZE
;
836 min((sb
->sb_hiwat
+ grow
), autorcvbuf_max
)) == 1) {
838 if (sb
->sb_hiwat
> ctl_autorcvbuf_high
)
839 ctl_autorcvbuf_high
= sb
->sb_hiwat
;
844 if ((u_int32_t
) sbspace(sb
) >= datasize
) {
851 printf("%s - grown to %d error %d\n",
852 __func__
, sb
->sb_hiwat
, error
);
864 ctl_enqueuembuf(kern_ctl_ref kctlref
, u_int32_t unit
, struct mbuf
*m
,
869 int len
= m
->m_pkthdr
.len
;
872 so
= kcb_find_socket(kctlref
, unit
, &kctlflags
);
877 if (ctl_rcvbspace(so
, len
, kctlflags
, flags
) != 0) {
879 OSIncrementAtomic64((SInt64
*)&kctlstat
.kcs_enqueue_fullsock
);
882 if ((flags
& CTL_DATA_EOR
))
885 so_recv_data_stat(so
, m
, 0);
886 if (sbappend(&so
->so_rcv
, m
) != 0) {
887 if ((flags
& CTL_DATA_NOWAKEUP
) == 0)
891 OSIncrementAtomic64((SInt64
*)&kctlstat
.kcs_enqueue_fullsock
);
894 if (ctl_debug
&& error
!= 0 && (flags
& CTL_DATA_CRIT
))
895 printf("%s - crit data err %d len %d hiwat %d cc: %d\n",
896 __func__
, error
, len
,
897 so
->so_rcv
.sb_hiwat
, so
->so_rcv
.sb_cc
);
899 socket_unlock(so
, 1);
901 OSIncrementAtomic64((SInt64
*)&kctlstat
.kcs_enqueue_fail
);
907 * Compute space occupied by mbuf like sbappendrecord
910 m_space(struct mbuf
*m
)
915 for (nxt
= m
; nxt
!= NULL
; nxt
= nxt
->m_next
)
922 ctl_enqueuembuf_list(void *kctlref
, u_int32_t unit
, struct mbuf
*m_list
,
923 u_int32_t flags
, struct mbuf
**m_remain
)
925 struct socket
*so
= NULL
;
927 struct mbuf
*m
, *nextpkt
;
933 * Need to point the beginning of the list in case of early exit
938 * kcb_find_socket takes the socket lock with a reference
940 so
= kcb_find_socket(kctlref
, unit
, &kctlflags
);
946 if (kctlflags
& CTL_FLAG_REG_SOCK_STREAM
) {
950 if (flags
& CTL_DATA_EOR
) {
955 for (m
= m_list
; m
!= NULL
; m
= nextpkt
) {
956 nextpkt
= m
->m_nextpkt
;
958 if (m
->m_pkthdr
.len
== 0 && ctl_debug
)
959 printf("%s: %llx m_pkthdr.len is 0",
960 __func__
, (uint64_t)VM_KERNEL_ADDRPERM(m
));
963 * The mbuf is either appended or freed by sbappendrecord()
964 * so it's not reliable from a data standpoint
967 if (ctl_rcvbspace(so
, len
, kctlflags
, flags
) != 0) {
970 (SInt64
*)&kctlstat
.kcs_enqueue_fullsock
);
974 * Unlink from the list, m is on its own
977 so_recv_data_stat(so
, m
, 0);
978 if (sbappendrecord(&so
->so_rcv
, m
) != 0) {
982 * We free or return the remaining
988 (SInt64
*)&kctlstat
.kcs_enqueue_fullsock
);
993 if (needwakeup
&& (flags
& CTL_DATA_NOWAKEUP
) == 0)
998 if (ctl_debug
&& error
!= 0 && (flags
& CTL_DATA_CRIT
))
999 printf("%s - crit data err %d len %d hiwat %d cc: %d\n",
1000 __func__
, error
, len
,
1001 so
->so_rcv
.sb_hiwat
, so
->so_rcv
.sb_cc
);
1003 socket_unlock(so
, 1);
1008 if (m
!= NULL
&& socket_debug
&& so
!= NULL
&&
1009 (so
->so_options
& SO_DEBUG
)) {
1012 printf("%s m_list %llx\n", __func__
,
1013 (uint64_t) VM_KERNEL_ADDRPERM(m_list
));
1014 for (n
= m
; n
!= NULL
; n
= n
->m_nextpkt
)
1015 printf(" remain %llx m_next %llx\n",
1016 (uint64_t) VM_KERNEL_ADDRPERM(n
),
1017 (uint64_t) VM_KERNEL_ADDRPERM(n
->m_next
));
1024 OSIncrementAtomic64((SInt64
*)&kctlstat
.kcs_enqueue_fail
);
1029 ctl_enqueuedata(void *kctlref
, u_int32_t unit
, void *data
, size_t len
,
1035 unsigned int num_needed
;
1038 u_int32_t kctlflags
;
1040 so
= kcb_find_socket(kctlref
, unit
, &kctlflags
);
1045 if (ctl_rcvbspace(so
, len
, kctlflags
, flags
) != 0) {
1047 OSIncrementAtomic64((SInt64
*)&kctlstat
.kcs_enqueue_fullsock
);
1052 m
= m_allocpacket_internal(&num_needed
, len
, NULL
, M_NOWAIT
, 1, 0);
1054 kctlstat
.kcs_enqdata_mb_alloc_fail
++;
1056 printf("%s: m_allocpacket_internal(%lu) failed\n",
1062 for (n
= m
; n
!= NULL
; n
= n
->m_next
) {
1063 size_t mlen
= mbuf_maxlen(n
);
1065 if (mlen
+ curlen
> len
)
1066 mlen
= len
- curlen
;
1068 bcopy((char *)data
+ curlen
, n
->m_data
, mlen
);
1071 mbuf_pkthdr_setlen(m
, curlen
);
1073 if ((flags
& CTL_DATA_EOR
))
1074 m
->m_flags
|= M_EOR
;
1075 so_recv_data_stat(so
, m
, 0);
1076 if (sbappend(&so
->so_rcv
, m
) != 0) {
1077 if ((flags
& CTL_DATA_NOWAKEUP
) == 0)
1080 kctlstat
.kcs_enqdata_sbappend_fail
++;
1082 OSIncrementAtomic64((SInt64
*)&kctlstat
.kcs_enqueue_fullsock
);
1086 if (ctl_debug
&& error
!= 0 && (flags
& CTL_DATA_CRIT
))
1087 printf("%s - crit data err %d len %d hiwat %d cc: %d\n",
1088 __func__
, error
, (int)len
,
1089 so
->so_rcv
.sb_hiwat
, so
->so_rcv
.sb_cc
);
1091 socket_unlock(so
, 1);
1093 OSIncrementAtomic64((SInt64
*)&kctlstat
.kcs_enqueue_fail
);
1098 ctl_getenqueuepacketcount(kern_ctl_ref kctlref
, u_int32_t unit
, u_int32_t
*pcnt
)
1107 so
= kcb_find_socket(kctlref
, unit
, NULL
);
1113 m1
= so
->so_rcv
.sb_mb
;
1114 while (m1
!= NULL
) {
1115 if (m1
->m_type
== MT_DATA
||
1116 m1
->m_type
== MT_HEADER
||
1117 m1
->m_type
== MT_OOBDATA
)
1123 socket_unlock(so
, 1);
1129 ctl_getenqueuespace(kern_ctl_ref kctlref
, u_int32_t unit
, size_t *space
)
1137 so
= kcb_find_socket(kctlref
, unit
, NULL
);
1142 avail
= sbspace(&so
->so_rcv
);
1143 *space
= (avail
< 0) ? 0 : avail
;
1144 socket_unlock(so
, 1);
1150 ctl_getenqueuereadable(kern_ctl_ref kctlref
, u_int32_t unit
,
1151 u_int32_t
*difference
)
1155 if (difference
== NULL
)
1158 so
= kcb_find_socket(kctlref
, unit
, NULL
);
1163 if (so
->so_rcv
.sb_cc
>= so
->so_rcv
.sb_lowat
) {
1166 *difference
= (so
->so_rcv
.sb_lowat
- so
->so_rcv
.sb_cc
);
1168 socket_unlock(so
, 1);
1174 ctl_ctloutput(struct socket
*so
, struct sockopt
*sopt
)
1176 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
1182 if (sopt
->sopt_level
!= SYSPROTO_CONTROL
) {
1186 if (kcb
== NULL
) /* sanity check */
1189 if ((kctl
= kcb
->kctl
) == NULL
)
1192 switch (sopt
->sopt_dir
) {
1194 if (kctl
->setopt
== NULL
)
1196 if (sopt
->sopt_valsize
!= 0) {
1197 MALLOC(data
, void *, sopt
->sopt_valsize
, M_TEMP
,
1201 error
= sooptcopyin(sopt
, data
,
1202 sopt
->sopt_valsize
, sopt
->sopt_valsize
);
1205 socket_unlock(so
, 0);
1206 error
= (*kctl
->setopt
)(kctl
->kctlref
,
1207 kcb
->sac
.sc_unit
, kcb
->userdata
, sopt
->sopt_name
,
1208 data
, sopt
->sopt_valsize
);
1217 if (kctl
->getopt
== NULL
)
1220 if (sopt
->sopt_valsize
&& sopt
->sopt_val
) {
1221 MALLOC(data
, void *, sopt
->sopt_valsize
, M_TEMP
,
1226 * 4108337 - copy user data in case the
1227 * kernel control needs it
1229 error
= sooptcopyin(sopt
, data
,
1230 sopt
->sopt_valsize
, sopt
->sopt_valsize
);
1234 len
= sopt
->sopt_valsize
;
1235 socket_unlock(so
, 0);
1236 error
= (*kctl
->getopt
)(kctl
->kctlref
, kcb
->sac
.sc_unit
,
1237 kcb
->userdata
, sopt
->sopt_name
,
1239 if (data
!= NULL
&& len
> sopt
->sopt_valsize
)
1240 panic_plain("ctl_ctloutput: ctl %s returned "
1241 "len (%lu) > sopt_valsize (%lu)\n",
1242 kcb
->kctl
->name
, len
,
1243 sopt
->sopt_valsize
);
1247 error
= sooptcopyout(sopt
, data
, len
);
1249 sopt
->sopt_valsize
= len
;
1260 ctl_ioctl(struct socket
*so
, u_long cmd
, caddr_t data
,
1261 struct ifnet
*ifp
, struct proc
*p
)
1263 #pragma unused(so, ifp, p)
1264 int error
= ENOTSUP
;
1267 /* get the number of controllers */
1268 case CTLIOCGCOUNT
: {
1272 lck_mtx_lock(ctl_mtx
);
1273 TAILQ_FOREACH(kctl
, &ctl_head
, next
)
1275 lck_mtx_unlock(ctl_mtx
);
1277 bcopy(&n
, data
, sizeof (n
));
1282 struct ctl_info ctl_info
;
1283 struct kctl
*kctl
= 0;
1286 bcopy(data
, &ctl_info
, sizeof (ctl_info
));
1287 name_len
= strnlen(ctl_info
.ctl_name
, MAX_KCTL_NAME
);
1289 if (name_len
== 0 || name_len
+ 1 > MAX_KCTL_NAME
) {
1293 lck_mtx_lock(ctl_mtx
);
1294 kctl
= ctl_find_by_name(ctl_info
.ctl_name
);
1295 lck_mtx_unlock(ctl_mtx
);
1300 ctl_info
.ctl_id
= kctl
->id
;
1301 bcopy(&ctl_info
, data
, sizeof (ctl_info
));
1306 /* add controls to get list of NKEs */
1316 struct kctl
**new_table
;
1319 lck_mtx_assert(ctl_mtx
, LCK_MTX_ASSERT_OWNED
);
1321 if (kctl_tbl_growing
) {
1322 /* Another thread is allocating */
1323 kctl_tbl_growing_waiting
++;
1326 (void) msleep((caddr_t
) &kctl_tbl_growing
, ctl_mtx
,
1327 PSOCK
| PCATCH
, "kctl_tbl_growing", 0);
1328 } while (kctl_tbl_growing
);
1329 kctl_tbl_growing_waiting
--;
1331 /* Another thread grew the table */
1332 if (kctl_table
!= NULL
&& kctl_tbl_count
< kctl_tbl_size
)
1335 /* Verify we have a sane size */
1336 if (kctl_tbl_size
+ KCTL_TBL_INC
>= UINT16_MAX
) {
1337 kctlstat
.kcs_tbl_size_too_big
++;
1339 printf("%s kctl_tbl_size %lu too big\n",
1340 __func__
, kctl_tbl_size
);
1343 kctl_tbl_growing
= 1;
1345 new_size
= kctl_tbl_size
+ KCTL_TBL_INC
;
1347 lck_mtx_unlock(ctl_mtx
);
1348 new_table
= _MALLOC(sizeof(struct kctl
*) * new_size
,
1349 M_TEMP
, M_WAIT
| M_ZERO
);
1350 lck_mtx_lock(ctl_mtx
);
1352 if (new_table
!= NULL
) {
1353 if (kctl_table
!= NULL
) {
1354 bcopy(kctl_table
, new_table
,
1355 kctl_tbl_size
* sizeof(struct kctl
*));
1357 _FREE(kctl_table
, M_TEMP
);
1359 kctl_table
= new_table
;
1360 kctl_tbl_size
= new_size
;
1363 kctl_tbl_growing
= 0;
1365 if (kctl_tbl_growing_waiting
) {
1366 wakeup(&kctl_tbl_growing
);
1370 #define KCTLREF_INDEX_MASK 0x0000FFFF
1371 #define KCTLREF_GENCNT_MASK 0xFFFF0000
1372 #define KCTLREF_GENCNT_SHIFT 16
1375 kctl_make_ref(struct kctl
*kctl
)
1379 lck_mtx_assert(ctl_mtx
, LCK_MTX_ASSERT_OWNED
);
1381 if (kctl_tbl_count
>= kctl_tbl_size
)
1384 kctl
->kctlref
= NULL
;
1385 for (i
= 0; i
< kctl_tbl_size
; i
++) {
1386 if (kctl_table
[i
] == NULL
) {
1390 * Reference is index plus one
1392 kctl_ref_gencnt
+= 1;
1395 * Add generation count as salt to reference to prevent
1396 * use after deregister
1398 ref
= ((kctl_ref_gencnt
<< KCTLREF_GENCNT_SHIFT
) &
1399 KCTLREF_GENCNT_MASK
) +
1400 ((i
+ 1) & KCTLREF_INDEX_MASK
);
1402 kctl
->kctlref
= (void *)(ref
);
1403 kctl_table
[i
] = kctl
;
1409 if (kctl
->kctlref
== NULL
)
1410 panic("%s no space in table", __func__
);
1413 printf("%s %p for %p\n",
1414 __func__
, kctl
->kctlref
, kctl
);
1416 return (kctl
->kctlref
);
1420 kctl_delete_ref(kern_ctl_ref kctlref
)
1423 * Reference is index plus one
1425 uintptr_t i
= (((uintptr_t)kctlref
) & KCTLREF_INDEX_MASK
) - 1;
1427 lck_mtx_assert(ctl_mtx
, LCK_MTX_ASSERT_OWNED
);
1429 if (i
< kctl_tbl_size
) {
1430 struct kctl
*kctl
= kctl_table
[i
];
1432 if (kctl
->kctlref
== kctlref
) {
1433 kctl_table
[i
] = NULL
;
1436 kctlstat
.kcs_bad_kctlref
++;
1439 kctlstat
.kcs_bad_kctlref
++;
1443 static struct kctl
*
1444 kctl_from_ref(kern_ctl_ref kctlref
)
1447 * Reference is index plus one
1449 uintptr_t i
= (((uintptr_t)kctlref
) & KCTLREF_INDEX_MASK
) - 1;
1450 struct kctl
*kctl
= NULL
;
1452 lck_mtx_assert(ctl_mtx
, LCK_MTX_ASSERT_OWNED
);
1454 if (i
>= kctl_tbl_size
) {
1455 kctlstat
.kcs_bad_kctlref
++;
1458 kctl
= kctl_table
[i
];
1459 if (kctl
->kctlref
!= kctlref
) {
1460 kctlstat
.kcs_bad_kctlref
++;
1467 * Register/unregister a NKE
1470 ctl_register(struct kern_ctl_reg
*userkctl
, kern_ctl_ref
*kctlref
)
1472 struct kctl
*kctl
= NULL
;
1473 struct kctl
*kctl_next
= NULL
;
1476 int is_extended
= 0;
1478 if (userkctl
== NULL
) /* sanity check */
1480 if (userkctl
->ctl_connect
== NULL
)
1482 name_len
= strlen(userkctl
->ctl_name
);
1483 if (name_len
== 0 || name_len
+ 1 > MAX_KCTL_NAME
)
1486 MALLOC(kctl
, struct kctl
*, sizeof(*kctl
), M_TEMP
, M_WAITOK
);
1489 bzero((char *)kctl
, sizeof(*kctl
));
1491 lck_mtx_lock(ctl_mtx
);
1493 if (kctl_make_ref(kctl
) == NULL
) {
1494 lck_mtx_unlock(ctl_mtx
);
1500 * Kernel Control IDs
1502 * CTL_FLAG_REG_ID_UNIT indicates the control ID and unit number are
1503 * static. If they do not exist, add them to the list in order. If the
1504 * flag is not set, we must find a new unique value. We assume the
1505 * list is in order. We find the last item in the list and add one. If
1506 * this leads to wrapping the id around, we start at the front of the
1507 * list and look for a gap.
1510 if ((userkctl
->ctl_flags
& CTL_FLAG_REG_ID_UNIT
) == 0) {
1511 /* Must dynamically assign an unused ID */
1513 /* Verify the same name isn't already registered */
1514 if (ctl_find_by_name(userkctl
->ctl_name
) != NULL
) {
1515 kctl_delete_ref(kctl
->kctlref
);
1516 lck_mtx_unlock(ctl_mtx
);
1521 /* Start with 1 in case the list is empty */
1523 kctl_next
= TAILQ_LAST(&ctl_head
, kctl_list
);
1525 if (kctl_next
!= NULL
) {
1526 /* List was not empty, add one to the last item */
1527 id
= kctl_next
->id
+ 1;
1531 * If this wrapped the id number, start looking at
1532 * the front of the list for an unused id.
1535 /* Find the next unused ID */
1538 TAILQ_FOREACH(kctl_next
, &ctl_head
, next
) {
1539 if (kctl_next
->id
> id
) {
1540 /* We found a gap */
1544 id
= kctl_next
->id
+ 1;
1549 userkctl
->ctl_id
= id
;
1551 kctl
->reg_unit
= -1;
1553 TAILQ_FOREACH(kctl_next
, &ctl_head
, next
) {
1554 if (kctl_next
->id
> userkctl
->ctl_id
)
1558 if (ctl_find_by_id_unit(userkctl
->ctl_id
, userkctl
->ctl_unit
)) {
1559 kctl_delete_ref(kctl
->kctlref
);
1560 lck_mtx_unlock(ctl_mtx
);
1564 kctl
->id
= userkctl
->ctl_id
;
1565 kctl
->reg_unit
= userkctl
->ctl_unit
;
1568 is_extended
= (userkctl
->ctl_flags
& CTL_FLAG_REG_EXTENDED
);
1570 strlcpy(kctl
->name
, userkctl
->ctl_name
, MAX_KCTL_NAME
);
1571 kctl
->flags
= userkctl
->ctl_flags
;
1574 * Let the caller know the default send and receive sizes
1576 if (userkctl
->ctl_sendsize
== 0) {
1577 kctl
->sendbufsize
= CTL_SENDSIZE
;
1578 userkctl
->ctl_sendsize
= kctl
->sendbufsize
;
1580 kctl
->sendbufsize
= userkctl
->ctl_sendsize
;
1582 if (userkctl
->ctl_recvsize
== 0) {
1583 kctl
->recvbufsize
= CTL_RECVSIZE
;
1584 userkctl
->ctl_recvsize
= kctl
->recvbufsize
;
1586 kctl
->recvbufsize
= userkctl
->ctl_recvsize
;
1589 kctl
->bind
= userkctl
->ctl_bind
;
1590 kctl
->connect
= userkctl
->ctl_connect
;
1591 kctl
->disconnect
= userkctl
->ctl_disconnect
;
1592 kctl
->send
= userkctl
->ctl_send
;
1593 kctl
->setopt
= userkctl
->ctl_setopt
;
1594 kctl
->getopt
= userkctl
->ctl_getopt
;
1596 kctl
->rcvd
= userkctl
->ctl_rcvd
;
1597 kctl
->send_list
= userkctl
->ctl_send_list
;
1600 TAILQ_INIT(&kctl
->kcb_head
);
1603 TAILQ_INSERT_BEFORE(kctl_next
, kctl
, next
);
1605 TAILQ_INSERT_TAIL(&ctl_head
, kctl
, next
);
1607 kctlstat
.kcs_reg_count
++;
1608 kctlstat
.kcs_gencnt
++;
1610 lck_mtx_unlock(ctl_mtx
);
1612 *kctlref
= kctl
->kctlref
;
1614 ctl_post_msg(KEV_CTL_REGISTERED
, kctl
->id
);
1619 ctl_deregister(void *kctlref
)
1623 lck_mtx_lock(ctl_mtx
);
1624 if ((kctl
= kctl_from_ref(kctlref
)) == NULL
) {
1625 kctlstat
.kcs_bad_kctlref
++;
1626 lck_mtx_unlock(ctl_mtx
);
1628 printf("%s invalid kctlref %p\n",
1633 if (!TAILQ_EMPTY(&kctl
->kcb_head
)) {
1634 lck_mtx_unlock(ctl_mtx
);
1638 TAILQ_REMOVE(&ctl_head
, kctl
, next
);
1640 kctlstat
.kcs_reg_count
--;
1641 kctlstat
.kcs_gencnt
++;
1643 kctl_delete_ref(kctl
->kctlref
);
1644 lck_mtx_unlock(ctl_mtx
);
1646 ctl_post_msg(KEV_CTL_DEREGISTERED
, kctl
->id
);
1652 * Must be called with global ctl_mtx lock taked
1654 static struct kctl
*
1655 ctl_find_by_name(const char *name
)
1659 lck_mtx_assert(ctl_mtx
, LCK_MTX_ASSERT_OWNED
);
1661 TAILQ_FOREACH(kctl
, &ctl_head
, next
)
1662 if (strncmp(kctl
->name
, name
, sizeof(kctl
->name
)) == 0)
1669 ctl_id_by_name(const char *name
)
1671 u_int32_t ctl_id
= 0;
1674 lck_mtx_lock(ctl_mtx
);
1675 kctl
= ctl_find_by_name(name
);
1678 lck_mtx_unlock(ctl_mtx
);
1684 ctl_name_by_id(u_int32_t id
, char *out_name
, size_t maxsize
)
1689 lck_mtx_lock(ctl_mtx
);
1690 TAILQ_FOREACH(kctl
, &ctl_head
, next
) {
1696 if (maxsize
> MAX_KCTL_NAME
)
1697 maxsize
= MAX_KCTL_NAME
;
1698 strlcpy(out_name
, kctl
->name
, maxsize
);
1701 lck_mtx_unlock(ctl_mtx
);
1703 return (found
? 0 : ENOENT
);
1707 * Must be called with global ctl_mtx lock taked
1710 static struct kctl
*
1711 ctl_find_by_id_unit(u_int32_t id
, u_int32_t unit
)
1715 lck_mtx_assert(ctl_mtx
, LCK_MTX_ASSERT_OWNED
);
1717 TAILQ_FOREACH(kctl
, &ctl_head
, next
) {
1718 if (kctl
->id
== id
&& (kctl
->flags
& CTL_FLAG_REG_ID_UNIT
) == 0)
1720 else if (kctl
->id
== id
&& kctl
->reg_unit
== unit
)
1727 * Must be called with kernel controller lock taken
1729 static struct ctl_cb
*
1730 kcb_find(struct kctl
*kctl
, u_int32_t unit
)
1734 lck_mtx_assert(ctl_mtx
, LCK_MTX_ASSERT_OWNED
);
1736 TAILQ_FOREACH(kcb
, &kctl
->kcb_head
, next
)
1737 if (kcb
->sac
.sc_unit
== unit
)
1743 static struct socket
*
1744 kcb_find_socket(kern_ctl_ref kctlref
, u_int32_t unit
, u_int32_t
*kctlflags
)
1746 struct socket
*so
= NULL
;
1752 lr_saved
= __builtin_return_address(0);
1754 lck_mtx_lock(ctl_mtx
);
1756 * First validate the kctlref
1758 if ((kctl
= kctl_from_ref(kctlref
)) == NULL
) {
1759 kctlstat
.kcs_bad_kctlref
++;
1760 lck_mtx_unlock(ctl_mtx
);
1762 printf("%s invalid kctlref %p\n",
1767 kcb
= kcb_find(kctl
, unit
);
1768 if (kcb
== NULL
|| kcb
->kctl
!= kctl
|| (so
= kcb
->so
) == NULL
) {
1769 lck_mtx_unlock(ctl_mtx
);
1773 * This prevents the socket from being closed
1777 * Respect lock ordering: socket before ctl_mtx
1779 lck_mtx_unlock(ctl_mtx
);
1783 * The socket lock history is more useful if we store
1784 * the address of the caller.
1786 i
= (so
->next_lock_lr
+ SO_LCKDBG_MAX
- 1) % SO_LCKDBG_MAX
;
1787 so
->lock_lr
[i
] = lr_saved
;
1789 lck_mtx_lock(ctl_mtx
);
1791 if ((kctl
= kctl_from_ref(kctlref
)) == NULL
|| kcb
->kctl
== NULL
) {
1792 lck_mtx_unlock(ctl_mtx
);
1793 socket_unlock(so
, 1);
1795 lck_mtx_lock(ctl_mtx
);
1796 } else if (kctlflags
!= NULL
) {
1797 *kctlflags
= kctl
->flags
;
1801 if (kcb
->usecount
== 0)
1802 wakeup((event_t
)&kcb
->usecount
);
1804 lck_mtx_unlock(ctl_mtx
);
1810 ctl_post_msg(u_int32_t event_code
, u_int32_t id
)
1812 struct ctl_event_data ctl_ev_data
;
1813 struct kev_msg ev_msg
;
1815 lck_mtx_assert(ctl_mtx
, LCK_MTX_ASSERT_NOTOWNED
);
1817 bzero(&ev_msg
, sizeof(struct kev_msg
));
1818 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
1820 ev_msg
.kev_class
= KEV_SYSTEM_CLASS
;
1821 ev_msg
.kev_subclass
= KEV_CTL_SUBCLASS
;
1822 ev_msg
.event_code
= event_code
;
1824 /* common nke subclass data */
1825 bzero(&ctl_ev_data
, sizeof(ctl_ev_data
));
1826 ctl_ev_data
.ctl_id
= id
;
1827 ev_msg
.dv
[0].data_ptr
= &ctl_ev_data
;
1828 ev_msg
.dv
[0].data_length
= sizeof(ctl_ev_data
);
1830 ev_msg
.dv
[1].data_length
= 0;
1832 kev_post_msg(&ev_msg
);
1836 ctl_lock(struct socket
*so
, int refcount
, void *lr
)
1841 lr_saved
= __builtin_return_address(0);
1845 if (so
->so_pcb
!= NULL
) {
1846 lck_mtx_lock(((struct ctl_cb
*)so
->so_pcb
)->mtx
);
1848 panic("ctl_lock: so=%p NO PCB! lr=%p lrh= %s\n",
1849 so
, lr_saved
, solockhistory_nr(so
));
1853 if (so
->so_usecount
< 0) {
1854 panic("ctl_lock: so=%p so_pcb=%p lr=%p ref=%x lrh= %s\n",
1855 so
, so
->so_pcb
, lr_saved
, so
->so_usecount
,
1856 solockhistory_nr(so
));
1863 so
->lock_lr
[so
->next_lock_lr
] = lr_saved
;
1864 so
->next_lock_lr
= (so
->next_lock_lr
+1) % SO_LCKDBG_MAX
;
1869 ctl_unlock(struct socket
*so
, int refcount
, void *lr
)
1872 lck_mtx_t
*mutex_held
;
1875 lr_saved
= __builtin_return_address(0);
1879 #if (MORE_KCTLLOCK_DEBUG && (DEVELOPMENT || DEBUG))
1880 printf("ctl_unlock: so=%llx sopcb=%x lock=%llx ref=%u lr=%llx\n",
1881 (uint64_t)VM_KERNEL_ADDRPERM(so
),
1882 (uint64_t)VM_KERNEL_ADDRPERM(so
->so_pcb
,
1883 (uint64_t)VM_KERNEL_ADDRPERM(((struct ctl_cb
*)so
->so_pcb
)->mtx
),
1884 so
->so_usecount
, (uint64_t)VM_KERNEL_ADDRPERM(lr_saved
));
1885 #endif /* (MORE_KCTLLOCK_DEBUG && (DEVELOPMENT || DEBUG)) */
1889 if (so
->so_usecount
< 0) {
1890 panic("ctl_unlock: so=%p usecount=%x lrh= %s\n",
1891 so
, so
->so_usecount
, solockhistory_nr(so
));
1894 if (so
->so_pcb
== NULL
) {
1895 panic("ctl_unlock: so=%p NO PCB usecount=%x lr=%p lrh= %s\n",
1896 so
, so
->so_usecount
, (void *)lr_saved
,
1897 solockhistory_nr(so
));
1900 mutex_held
= ((struct ctl_cb
*)so
->so_pcb
)->mtx
;
1902 lck_mtx_assert(mutex_held
, LCK_MTX_ASSERT_OWNED
);
1903 so
->unlock_lr
[so
->next_unlock_lr
] = lr_saved
;
1904 so
->next_unlock_lr
= (so
->next_unlock_lr
+1) % SO_LCKDBG_MAX
;
1905 lck_mtx_unlock(mutex_held
);
1907 if (so
->so_usecount
== 0)
1908 ctl_sofreelastref(so
);
1914 ctl_getlock(struct socket
*so
, int flags
)
1916 #pragma unused(flags)
1917 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
1920 if (so
->so_usecount
< 0)
1921 panic("ctl_getlock: so=%p usecount=%x lrh= %s\n",
1922 so
, so
->so_usecount
, solockhistory_nr(so
));
1925 panic("ctl_getlock: so=%p NULL NO so_pcb %s\n",
1926 so
, solockhistory_nr(so
));
1927 return (so
->so_proto
->pr_domain
->dom_mtx
);
1931 __private_extern__
int
1932 kctl_reg_list SYSCTL_HANDLER_ARGS
1934 #pragma unused(oidp, arg1, arg2)
1937 struct xsystmgen xsg
;
1940 size_t item_size
= ROUNDUP64(sizeof (struct xkctl_reg
));
1942 buf
= _MALLOC(item_size
, M_TEMP
, M_WAITOK
| M_ZERO
);
1946 lck_mtx_lock(ctl_mtx
);
1948 n
= kctlstat
.kcs_reg_count
;
1950 if (req
->oldptr
== USER_ADDR_NULL
) {
1951 req
->oldidx
= (n
+ n
/8) * sizeof(struct xkctl_reg
);
1954 if (req
->newptr
!= USER_ADDR_NULL
) {
1958 bzero(&xsg
, sizeof (xsg
));
1959 xsg
.xg_len
= sizeof (xsg
);
1961 xsg
.xg_gen
= kctlstat
.kcs_gencnt
;
1962 xsg
.xg_sogen
= so_gencnt
;
1963 error
= SYSCTL_OUT(req
, &xsg
, sizeof (xsg
));
1968 * We are done if there is no pcb
1975 for (i
= 0, kctl
= TAILQ_FIRST(&ctl_head
);
1976 i
< n
&& kctl
!= NULL
;
1977 i
++, kctl
= TAILQ_NEXT(kctl
, next
)) {
1978 struct xkctl_reg
*xkr
= (struct xkctl_reg
*)buf
;
1980 u_int32_t pcbcount
= 0;
1982 TAILQ_FOREACH(kcb
, &kctl
->kcb_head
, next
)
1985 bzero(buf
, item_size
);
1987 xkr
->xkr_len
= sizeof(struct xkctl_reg
);
1988 xkr
->xkr_kind
= XSO_KCREG
;
1989 xkr
->xkr_id
= kctl
->id
;
1990 xkr
->xkr_reg_unit
= kctl
->reg_unit
;
1991 xkr
->xkr_flags
= kctl
->flags
;
1992 xkr
->xkr_kctlref
= (uint64_t)(kctl
->kctlref
);
1993 xkr
->xkr_recvbufsize
= kctl
->recvbufsize
;
1994 xkr
->xkr_sendbufsize
= kctl
->sendbufsize
;
1995 xkr
->xkr_lastunit
= kctl
->lastunit
;
1996 xkr
->xkr_pcbcount
= pcbcount
;
1997 xkr
->xkr_connect
= (uint64_t)VM_KERNEL_UNSLIDE(kctl
->connect
);
1998 xkr
->xkr_disconnect
=
1999 (uint64_t)VM_KERNEL_UNSLIDE(kctl
->disconnect
);
2000 xkr
->xkr_send
= (uint64_t)VM_KERNEL_UNSLIDE(kctl
->send
);
2001 xkr
->xkr_send_list
=
2002 (uint64_t)VM_KERNEL_UNSLIDE(kctl
->send_list
);
2003 xkr
->xkr_setopt
= (uint64_t)VM_KERNEL_UNSLIDE(kctl
->setopt
);
2004 xkr
->xkr_getopt
= (uint64_t)VM_KERNEL_UNSLIDE(kctl
->getopt
);
2005 xkr
->xkr_rcvd
= (uint64_t)VM_KERNEL_UNSLIDE(kctl
->rcvd
);
2006 strlcpy(xkr
->xkr_name
, kctl
->name
, sizeof(xkr
->xkr_name
));
2008 error
= SYSCTL_OUT(req
, buf
, item_size
);
2013 * Give the user an updated idea of our state.
2014 * If the generation differs from what we told
2015 * her before, she knows that something happened
2016 * while we were processing this request, and it
2017 * might be necessary to retry.
2019 bzero(&xsg
, sizeof (xsg
));
2020 xsg
.xg_len
= sizeof (xsg
);
2022 xsg
.xg_gen
= kctlstat
.kcs_gencnt
;
2023 xsg
.xg_sogen
= so_gencnt
;
2024 error
= SYSCTL_OUT(req
, &xsg
, sizeof (xsg
));
2031 lck_mtx_unlock(ctl_mtx
);
2039 __private_extern__
int
2040 kctl_pcblist SYSCTL_HANDLER_ARGS
2042 #pragma unused(oidp, arg1, arg2)
2045 struct xsystmgen xsg
;
2048 size_t item_size
= ROUNDUP64(sizeof (struct xkctlpcb
)) +
2049 ROUNDUP64(sizeof (struct xsocket_n
)) +
2050 2 * ROUNDUP64(sizeof (struct xsockbuf_n
)) +
2051 ROUNDUP64(sizeof (struct xsockstat_n
));
2053 buf
= _MALLOC(item_size
, M_TEMP
, M_WAITOK
| M_ZERO
);
2057 lck_mtx_lock(ctl_mtx
);
2059 n
= kctlstat
.kcs_pcbcount
;
2061 if (req
->oldptr
== USER_ADDR_NULL
) {
2062 req
->oldidx
= (n
+ n
/8) * item_size
;
2065 if (req
->newptr
!= USER_ADDR_NULL
) {
2069 bzero(&xsg
, sizeof (xsg
));
2070 xsg
.xg_len
= sizeof (xsg
);
2072 xsg
.xg_gen
= kctlstat
.kcs_gencnt
;
2073 xsg
.xg_sogen
= so_gencnt
;
2074 error
= SYSCTL_OUT(req
, &xsg
, sizeof (xsg
));
2079 * We are done if there is no pcb
2086 for (i
= 0, kctl
= TAILQ_FIRST(&ctl_head
);
2087 i
< n
&& kctl
!= NULL
;
2088 kctl
= TAILQ_NEXT(kctl
, next
)) {
2091 for (kcb
= TAILQ_FIRST(&kctl
->kcb_head
);
2092 i
< n
&& kcb
!= NULL
;
2093 i
++, kcb
= TAILQ_NEXT(kcb
, next
)) {
2094 struct xkctlpcb
*xk
= (struct xkctlpcb
*)buf
;
2095 struct xsocket_n
*xso
= (struct xsocket_n
*)
2096 ADVANCE64(xk
, sizeof (*xk
));
2097 struct xsockbuf_n
*xsbrcv
= (struct xsockbuf_n
*)
2098 ADVANCE64(xso
, sizeof (*xso
));
2099 struct xsockbuf_n
*xsbsnd
= (struct xsockbuf_n
*)
2100 ADVANCE64(xsbrcv
, sizeof (*xsbrcv
));
2101 struct xsockstat_n
*xsostats
= (struct xsockstat_n
*)
2102 ADVANCE64(xsbsnd
, sizeof (*xsbsnd
));
2104 bzero(buf
, item_size
);
2106 xk
->xkp_len
= sizeof(struct xkctlpcb
);
2107 xk
->xkp_kind
= XSO_KCB
;
2108 xk
->xkp_unit
= kcb
->sac
.sc_unit
;
2109 xk
->xkp_kctpcb
= (uint64_t)VM_KERNEL_ADDRPERM(kcb
);
2110 xk
->xkp_kctlref
= (uint64_t)VM_KERNEL_ADDRPERM(kctl
);
2111 xk
->xkp_kctlid
= kctl
->id
;
2112 strlcpy(xk
->xkp_kctlname
, kctl
->name
,
2113 sizeof(xk
->xkp_kctlname
));
2115 sotoxsocket_n(kcb
->so
, xso
);
2116 sbtoxsockbuf_n(kcb
->so
?
2117 &kcb
->so
->so_rcv
: NULL
, xsbrcv
);
2118 sbtoxsockbuf_n(kcb
->so
?
2119 &kcb
->so
->so_snd
: NULL
, xsbsnd
);
2120 sbtoxsockstat_n(kcb
->so
, xsostats
);
2122 error
= SYSCTL_OUT(req
, buf
, item_size
);
2128 * Give the user an updated idea of our state.
2129 * If the generation differs from what we told
2130 * her before, she knows that something happened
2131 * while we were processing this request, and it
2132 * might be necessary to retry.
2134 bzero(&xsg
, sizeof (xsg
));
2135 xsg
.xg_len
= sizeof (xsg
);
2137 xsg
.xg_gen
= kctlstat
.kcs_gencnt
;
2138 xsg
.xg_sogen
= so_gencnt
;
2139 error
= SYSCTL_OUT(req
, &xsg
, sizeof (xsg
));
2146 lck_mtx_unlock(ctl_mtx
);
2152 kctl_getstat SYSCTL_HANDLER_ARGS
2154 #pragma unused(oidp, arg1, arg2)
2157 lck_mtx_lock(ctl_mtx
);
2159 if (req
->newptr
!= USER_ADDR_NULL
) {
2163 if (req
->oldptr
== USER_ADDR_NULL
) {
2164 req
->oldidx
= sizeof(struct kctlstat
);
2168 error
= SYSCTL_OUT(req
, &kctlstat
,
2169 MIN(sizeof(struct kctlstat
), req
->oldlen
));
2171 lck_mtx_unlock(ctl_mtx
);
2176 kctl_fill_socketinfo(struct socket
*so
, struct socket_info
*si
)
2178 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
2179 struct kern_ctl_info
*kcsi
=
2180 &si
->soi_proto
.pri_kern_ctl
;
2181 struct kctl
*kctl
= kcb
->kctl
;
2183 si
->soi_kind
= SOCKINFO_KERN_CTL
;
2188 kcsi
->kcsi_id
= kctl
->id
;
2189 kcsi
->kcsi_reg_unit
= kctl
->reg_unit
;
2190 kcsi
->kcsi_flags
= kctl
->flags
;
2191 kcsi
->kcsi_recvbufsize
= kctl
->recvbufsize
;
2192 kcsi
->kcsi_sendbufsize
= kctl
->sendbufsize
;
2193 kcsi
->kcsi_unit
= kcb
->sac
.sc_unit
;
2194 strlcpy(kcsi
->kcsi_name
, kctl
->name
, MAX_KCTL_NAME
);