2 * Copyright (c) 1999-2014 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 <net/if_var.h>
55 #include <mach/vm_types.h>
57 #include <kern/thread.h>
60 #define ROUNDUP64(x) P2ROUNDUP((x), sizeof (u_int64_t))
64 #define ADVANCE64(p, n) (void*)((char *)(p) + ROUNDUP64(n))
68 * Definitions and vars for we support
71 #define CTL_SENDSIZE (2 * 1024) /* default buffer size */
72 #define CTL_RECVSIZE (8 * 1024) /* default buffer size */
75 * Definitions and vars for we support
78 static u_int32_t ctl_maxunit
= 65536;
79 static lck_grp_attr_t
*ctl_lck_grp_attr
= 0;
80 static lck_attr_t
*ctl_lck_attr
= 0;
81 static lck_grp_t
*ctl_lck_grp
= 0;
82 static lck_mtx_t
*ctl_mtx
;
84 /* all the controllers are chained */
85 TAILQ_HEAD(kctl_list
, kctl
) ctl_head
;
88 static int ctl_attach(struct socket
*, int, struct proc
*);
89 static int ctl_detach(struct socket
*);
90 static int ctl_sofreelastref(struct socket
*so
);
91 static int ctl_connect(struct socket
*, struct sockaddr
*, struct proc
*);
92 static int ctl_disconnect(struct socket
*);
93 static int ctl_ioctl(struct socket
*so
, u_long cmd
, caddr_t data
,
94 struct ifnet
*ifp
, struct proc
*p
);
95 static int ctl_send(struct socket
*, int, struct mbuf
*,
96 struct sockaddr
*, struct mbuf
*, struct proc
*);
97 static int ctl_send_list(struct socket
*, int, struct mbuf
*,
98 struct sockaddr
*, struct mbuf
*, struct proc
*);
99 static int ctl_ctloutput(struct socket
*, struct sockopt
*);
100 static int ctl_peeraddr(struct socket
*so
, struct sockaddr
**nam
);
101 static int ctl_usr_rcvd(struct socket
*so
, int flags
);
103 static struct kctl
*ctl_find_by_name(const char *);
104 static struct kctl
*ctl_find_by_id_unit(u_int32_t id
, u_int32_t unit
);
106 static struct socket
*kcb_find_socket(struct kctl
*, u_int32_t unit
);
107 static struct ctl_cb
*kcb_find(struct kctl
*, u_int32_t unit
);
108 static void ctl_post_msg(u_int32_t event_code
, u_int32_t id
);
110 static int ctl_lock(struct socket
*, int, void *);
111 static int ctl_unlock(struct socket
*, int, void *);
112 static lck_mtx_t
* ctl_getlock(struct socket
*, int);
114 static struct pr_usrreqs ctl_usrreqs
= {
115 .pru_attach
= ctl_attach
,
116 .pru_connect
= ctl_connect
,
117 .pru_control
= ctl_ioctl
,
118 .pru_detach
= ctl_detach
,
119 .pru_disconnect
= ctl_disconnect
,
120 .pru_peeraddr
= ctl_peeraddr
,
121 .pru_rcvd
= ctl_usr_rcvd
,
122 .pru_send
= ctl_send
,
123 .pru_send_list
= ctl_send_list
,
124 .pru_sosend
= sosend
,
125 .pru_sosend_list
= sosend_list
,
126 .pru_soreceive
= soreceive
,
127 .pru_soreceive_list
= soreceive_list
,
130 static struct protosw kctlsw
[] = {
132 .pr_type
= SOCK_DGRAM
,
133 .pr_protocol
= SYSPROTO_CONTROL
,
134 .pr_flags
= PR_ATOMIC
|PR_CONNREQUIRED
|PR_PCBLOCK
|PR_WANTRCVD
,
135 .pr_ctloutput
= ctl_ctloutput
,
136 .pr_usrreqs
= &ctl_usrreqs
,
138 .pr_unlock
= ctl_unlock
,
139 .pr_getlock
= ctl_getlock
,
142 .pr_type
= SOCK_STREAM
,
143 .pr_protocol
= SYSPROTO_CONTROL
,
144 .pr_flags
= PR_CONNREQUIRED
|PR_PCBLOCK
|PR_WANTRCVD
,
145 .pr_ctloutput
= ctl_ctloutput
,
146 .pr_usrreqs
= &ctl_usrreqs
,
148 .pr_unlock
= ctl_unlock
,
149 .pr_getlock
= ctl_getlock
,
153 __private_extern__
int kctl_reg_list SYSCTL_HANDLER_ARGS
;
154 __private_extern__
int kctl_pcblist SYSCTL_HANDLER_ARGS
;
155 __private_extern__
int kctl_getstat SYSCTL_HANDLER_ARGS
;
157 static int kctl_proto_count
= (sizeof (kctlsw
) / sizeof (struct protosw
));
159 SYSCTL_NODE(_net_systm
, OID_AUTO
, kctl
,
160 CTLFLAG_RW
|CTLFLAG_LOCKED
, 0, "Kernel control family");
162 struct kctlstat kctlstat
;
163 SYSCTL_PROC(_net_systm_kctl
, OID_AUTO
, stats
,
164 CTLTYPE_STRUCT
| CTLFLAG_RD
| CTLFLAG_LOCKED
, 0, 0,
165 kctl_getstat
, "S,kctlstat", "");
167 SYSCTL_PROC(_net_systm_kctl
, OID_AUTO
, reg_list
,
168 CTLTYPE_STRUCT
| CTLFLAG_RD
| CTLFLAG_LOCKED
, 0, 0,
169 kctl_reg_list
, "S,xkctl_reg", "");
171 SYSCTL_PROC(_net_systm_kctl
, OID_AUTO
, pcblist
,
172 CTLTYPE_STRUCT
| CTLFLAG_RD
| CTLFLAG_LOCKED
, 0, 0,
173 kctl_pcblist
, "S,xkctlpcb", "");
175 u_int32_t ctl_autorcvbuf_max
= 256 * 1024;
176 SYSCTL_INT(_net_systm_kctl
, OID_AUTO
, autorcvbufmax
,
177 CTLFLAG_RW
| CTLFLAG_LOCKED
, &ctl_autorcvbuf_max
, 0, "");
179 u_int32_t ctl_autorcvbuf_high
= 0;
180 SYSCTL_INT(_net_systm_kctl
, OID_AUTO
, autorcvbufhigh
,
181 CTLFLAG_RD
| CTLFLAG_LOCKED
, &ctl_autorcvbuf_high
, 0, "");
183 u_int32_t ctl_debug
= 0;
184 SYSCTL_INT(_net_systm_kctl
, OID_AUTO
, debug
,
185 CTLFLAG_RW
| CTLFLAG_LOCKED
, &ctl_debug
, 0, "");
188 * Install the protosw's for the Kernel Control manager.
190 __private_extern__
void
191 kern_control_init(struct domain
*dp
)
196 VERIFY(!(dp
->dom_flags
& DOM_INITIALIZED
));
197 VERIFY(dp
== systemdomain
);
199 ctl_lck_grp_attr
= lck_grp_attr_alloc_init();
200 if (ctl_lck_grp_attr
== NULL
) {
201 panic("%s: lck_grp_attr_alloc_init failed\n", __func__
);
205 ctl_lck_grp
= lck_grp_alloc_init("Kernel Control Protocol",
207 if (ctl_lck_grp
== NULL
) {
208 panic("%s: lck_grp_alloc_init failed\n", __func__
);
212 ctl_lck_attr
= lck_attr_alloc_init();
213 if (ctl_lck_attr
== NULL
) {
214 panic("%s: lck_attr_alloc_init failed\n", __func__
);
218 ctl_mtx
= lck_mtx_alloc_init(ctl_lck_grp
, ctl_lck_attr
);
219 if (ctl_mtx
== NULL
) {
220 panic("%s: lck_mtx_alloc_init failed\n", __func__
);
223 TAILQ_INIT(&ctl_head
);
225 for (i
= 0, pr
= &kctlsw
[0]; i
< kctl_proto_count
; i
++, pr
++)
226 net_add_proto(pr
, dp
, 1);
230 kcb_delete(struct ctl_cb
*kcb
)
234 lck_mtx_free(kcb
->mtx
, ctl_lck_grp
);
240 * Kernel Controller user-request functions
241 * attach function must exist and succeed
242 * detach not necessary
243 * we need a pcb for the per socket mutex
246 ctl_attach(struct socket
*so
, int proto
, struct proc
*p
)
248 #pragma unused(proto, p)
250 struct ctl_cb
*kcb
= 0;
252 MALLOC(kcb
, struct ctl_cb
*, sizeof(struct ctl_cb
), M_TEMP
, M_WAITOK
);
257 bzero(kcb
, sizeof(struct ctl_cb
));
259 kcb
->mtx
= lck_mtx_alloc_init(ctl_lck_grp
, ctl_lck_attr
);
260 if (kcb
->mtx
== NULL
) {
265 so
->so_pcb
= (caddr_t
)kcb
;
276 ctl_sofreelastref(struct socket
*so
)
278 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
284 if ((kctl
= kcb
->kctl
) != 0) {
285 lck_mtx_lock(ctl_mtx
);
286 TAILQ_REMOVE(&kctl
->kcb_head
, kcb
, next
);
287 kctlstat
.kcs_pcbcount
--;
288 kctlstat
.kcs_gencnt
++;
289 lck_mtx_unlock(ctl_mtx
);
293 sofreelastref(so
, 1);
298 ctl_detach(struct socket
*so
)
300 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
305 soisdisconnected(so
);
306 so
->so_flags
|= SOF_PCBCLEARING
;
312 ctl_connect(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
317 struct sockaddr_ctl sa
;
318 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
319 struct ctl_cb
*kcb_next
= NULL
;
322 panic("ctl_connect so_pcb null\n");
324 if (nam
->sa_len
!= sizeof(struct sockaddr_ctl
))
327 bcopy(nam
, &sa
, sizeof(struct sockaddr_ctl
));
329 lck_mtx_lock(ctl_mtx
);
330 kctl
= ctl_find_by_id_unit(sa
.sc_id
, sa
.sc_unit
);
332 lck_mtx_unlock(ctl_mtx
);
336 if (((kctl
->flags
& CTL_FLAG_REG_SOCK_STREAM
) &&
337 (so
->so_type
!= SOCK_STREAM
)) ||
338 (!(kctl
->flags
& CTL_FLAG_REG_SOCK_STREAM
) &&
339 (so
->so_type
!= SOCK_DGRAM
))) {
340 lck_mtx_unlock(ctl_mtx
);
344 if (kctl
->flags
& CTL_FLAG_PRIVILEGED
) {
346 lck_mtx_unlock(ctl_mtx
);
349 if (kauth_cred_issuser(kauth_cred_get()) == 0) {
350 lck_mtx_unlock(ctl_mtx
);
355 if ((kctl
->flags
& CTL_FLAG_REG_ID_UNIT
) || sa
.sc_unit
!= 0) {
356 if (kcb_find(kctl
, sa
.sc_unit
) != NULL
) {
357 lck_mtx_unlock(ctl_mtx
);
361 /* Find an unused ID, assumes control IDs are in order */
364 TAILQ_FOREACH(kcb_next
, &kctl
->kcb_head
, next
) {
365 if (kcb_next
->unit
> unit
) {
366 /* Found a gap, lets fill it in */
369 unit
= kcb_next
->unit
+ 1;
370 if (unit
== ctl_maxunit
)
374 if (unit
== ctl_maxunit
) {
375 lck_mtx_unlock(ctl_mtx
);
382 kcb
->unit
= sa
.sc_unit
;
384 if (kcb_next
!= NULL
) {
385 TAILQ_INSERT_BEFORE(kcb_next
, kcb
, next
);
387 TAILQ_INSERT_TAIL(&kctl
->kcb_head
, kcb
, next
);
389 kctlstat
.kcs_pcbcount
++;
390 kctlstat
.kcs_gencnt
++;
391 kctlstat
.kcs_connections
++;
392 lck_mtx_unlock(ctl_mtx
);
394 error
= soreserve(so
, kctl
->sendbufsize
, kctl
->recvbufsize
);
396 printf("%s - soreserve(%llx, %u, %u) error %d\n", __func__
,
397 (uint64_t)VM_KERNEL_ADDRPERM(so
),
398 kctl
->sendbufsize
, kctl
->recvbufsize
, error
);
403 socket_unlock(so
, 0);
404 error
= (*kctl
->connect
)(kctl
, &sa
, &kcb
->userdata
);
412 if (error
&& kctl
->disconnect
) {
413 socket_unlock(so
, 0);
414 (*kctl
->disconnect
)(kctl
, kcb
->unit
, kcb
->userdata
);
419 soisdisconnected(so
);
420 lck_mtx_lock(ctl_mtx
);
423 TAILQ_REMOVE(&kctl
->kcb_head
, kcb
, next
);
424 kctlstat
.kcs_pcbcount
--;
425 kctlstat
.kcs_gencnt
++;
426 kctlstat
.kcs_conn_fail
++;
427 lck_mtx_unlock(ctl_mtx
);
433 ctl_disconnect(struct socket
*so
)
435 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
437 if ((kcb
= (struct ctl_cb
*)so
->so_pcb
)) {
438 struct kctl
*kctl
= kcb
->kctl
;
440 if (kctl
&& kctl
->disconnect
) {
441 socket_unlock(so
, 0);
442 (*kctl
->disconnect
)(kctl
, kcb
->unit
, kcb
->userdata
);
446 soisdisconnected(so
);
448 socket_unlock(so
, 0);
449 lck_mtx_lock(ctl_mtx
);
452 while (kcb
->usecount
!= 0) {
453 msleep(&kcb
->usecount
, ctl_mtx
, 0, "kcb->usecount", 0);
455 TAILQ_REMOVE(&kctl
->kcb_head
, kcb
, next
);
456 kctlstat
.kcs_pcbcount
--;
457 kctlstat
.kcs_gencnt
++;
458 lck_mtx_unlock(ctl_mtx
);
465 ctl_peeraddr(struct socket
*so
, struct sockaddr
**nam
)
467 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
469 struct sockaddr_ctl sc
;
471 if (kcb
== NULL
) /* sanity check */
474 if ((kctl
= kcb
->kctl
) == NULL
)
477 bzero(&sc
, sizeof(struct sockaddr_ctl
));
478 sc
.sc_len
= sizeof(struct sockaddr_ctl
);
479 sc
.sc_family
= AF_SYSTEM
;
480 sc
.ss_sysaddr
= AF_SYS_CONTROL
;
482 sc
.sc_unit
= kcb
->unit
;
484 *nam
= dup_sockaddr((struct sockaddr
*)&sc
, 1);
490 ctl_sbrcv_trim(struct socket
*so
)
492 struct sockbuf
*sb
= &so
->so_rcv
;
494 if (sb
->sb_hiwat
> sb
->sb_idealsize
) {
499 * The difference between the ideal size and the
500 * current size is the upper bound of the trimage
502 diff
= sb
->sb_hiwat
- sb
->sb_idealsize
;
504 * We cannot trim below the outstanding data
506 trim
= sb
->sb_hiwat
- sb
->sb_cc
;
508 trim
= imin(trim
, (int32_t)diff
);
511 sbreserve(sb
, (sb
->sb_hiwat
- trim
));
514 printf("%s - shrunk to %d\n",
515 __func__
, sb
->sb_hiwat
);
521 ctl_usr_rcvd(struct socket
*so
, int flags
)
523 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
526 if ((kctl
= kcb
->kctl
) == NULL
) {
531 socket_unlock(so
, 0);
532 (*kctl
->rcvd
)(kctl
, kcb
->unit
, kcb
->userdata
, flags
);
542 ctl_send(struct socket
*so
, int flags
, struct mbuf
*m
,
543 struct sockaddr
*addr
, struct mbuf
*control
,
546 #pragma unused(addr, p)
548 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
554 if (kcb
== NULL
) /* sanity check */
557 if (error
== 0 && (kctl
= kcb
->kctl
) == NULL
)
560 if (error
== 0 && kctl
->send
) {
561 so_tc_update_stats(m
, so
, m_get_service_class(m
));
562 socket_unlock(so
, 0);
563 error
= (*kctl
->send
)(kctl
, kcb
->unit
, kcb
->userdata
, m
, flags
);
571 OSIncrementAtomic64((SInt64
*)&kctlstat
.kcs_send_fail
);
576 ctl_send_list(struct socket
*so
, int flags
, struct mbuf
*m
,
577 __unused
struct sockaddr
*addr
, struct mbuf
*control
,
578 __unused
struct proc
*p
)
581 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
585 m_freem_list(control
);
587 if (kcb
== NULL
) /* sanity check */
590 if (error
== 0 && (kctl
= kcb
->kctl
) == NULL
)
593 if (error
== 0 && kctl
->send_list
) {
596 for (nxt
= m
; nxt
!= NULL
; nxt
= nxt
->m_nextpkt
)
597 so_tc_update_stats(nxt
, so
, m_get_service_class(nxt
));
599 socket_unlock(so
, 0);
600 error
= (*kctl
->send_list
)(kctl
, kcb
->unit
, kcb
->userdata
, m
,
603 } else if (error
== 0 && kctl
->send
) {
604 while (m
!= NULL
&& error
== 0) {
605 struct mbuf
*nextpkt
= m
->m_nextpkt
;
608 so_tc_update_stats(m
, so
, m_get_service_class(m
));
609 socket_unlock(so
, 0);
610 error
= (*kctl
->send
)(kctl
, kcb
->unit
, kcb
->userdata
, m
,
623 OSIncrementAtomic64((SInt64
*)&kctlstat
.kcs_send_list_fail
);
628 ctl_rcvbspace(struct kctl
*kctl
, struct socket
*so
, u_int32_t datasize
,
631 struct sockbuf
*sb
= &so
->so_rcv
;
632 u_int32_t space
= sbspace(sb
);
635 if ((kctl
->flags
& CTL_FLAG_REG_CRIT
) == 0) {
636 if ((u_int32_t
) space
>= datasize
)
640 } else if ((flags
& CTL_DATA_CRIT
) == 0) {
642 * Reserve 25% for critical messages
644 if (space
< (sb
->sb_hiwat
>> 2) ||
650 u_int32_t autorcvbuf_max
;
653 * Allow overcommit of 25%
655 autorcvbuf_max
= min(sb
->sb_idealsize
+ (sb
->sb_idealsize
>> 2),
658 if ((u_int32_t
) space
>= datasize
) {
660 } else if (tcp_cansbgrow(sb
) &&
661 sb
->sb_hiwat
< autorcvbuf_max
) {
663 * Grow with a little bit of leeway
665 u_int32_t grow
= datasize
- space
+ MSIZE
;
668 min((sb
->sb_hiwat
+ grow
), autorcvbuf_max
)) == 1) {
670 if (sb
->sb_hiwat
> ctl_autorcvbuf_high
)
671 ctl_autorcvbuf_high
= sb
->sb_hiwat
;
674 printf("%s - grown to %d\n",
675 __func__
, sb
->sb_hiwat
);
688 ctl_enqueuembuf(void *kctlref
, u_int32_t unit
, struct mbuf
*m
, u_int32_t flags
)
692 struct kctl
*kctl
= (struct kctl
*)kctlref
;
693 int len
= m
->m_pkthdr
.len
;
698 so
= kcb_find_socket(kctl
, unit
);
703 if (ctl_rcvbspace(kctl
, so
, len
, flags
) != 0) {
705 OSIncrementAtomic64((SInt64
*)&kctlstat
.kcs_enqueue_fullsock
);
708 if ((flags
& CTL_DATA_EOR
))
711 so_recv_data_stat(so
, m
, 0);
712 if (sbappend(&so
->so_rcv
, m
) != 0) {
713 if ((flags
& CTL_DATA_NOWAKEUP
) == 0)
717 OSIncrementAtomic64((SInt64
*)&kctlstat
.kcs_enqueue_fullsock
);
720 if (ctl_debug
&& error
!= 0 && (flags
& CTL_DATA_CRIT
))
721 printf("%s - crit data err %d len %d hiwat %d cc: %d\n",
722 __func__
, error
, len
,
723 so
->so_rcv
.sb_hiwat
, so
->so_rcv
.sb_cc
);
725 socket_unlock(so
, 1);
727 OSIncrementAtomic64((SInt64
*)&kctlstat
.kcs_enqueue_fail
);
733 * Compute space occupied by mbuf like sbappendrecord
736 m_space(struct mbuf
*m
)
741 for (nxt
= m
; nxt
!= NULL
; nxt
= nxt
->m_next
)
748 ctl_enqueuembuf_list(void *kctlref
, u_int32_t unit
, struct mbuf
*m_list
,
749 u_int32_t flags
, struct mbuf
**m_remain
)
751 struct socket
*so
= NULL
;
753 struct kctl
*kctl
= (struct kctl
*)kctlref
;
754 struct mbuf
*m
, *nextpkt
;
759 * Need to point the beginning of the list in case of early exit
767 if (kctl
->flags
& CTL_FLAG_REG_SOCK_STREAM
) {
771 if (flags
& CTL_DATA_EOR
) {
776 * kcb_find_socket takes the socket lock with a reference
778 so
= kcb_find_socket(kctl
, unit
);
784 for (m
= m_list
; m
!= NULL
; m
= nextpkt
) {
785 nextpkt
= m
->m_nextpkt
;
787 if (m
->m_pkthdr
.len
== 0)
788 printf("%s: %llx m_pkthdr.len is 0",
789 __func__
, (uint64_t)VM_KERNEL_ADDRPERM(m
));
792 * The mbuf is either appended or freed by sbappendrecord()
793 * so it's not reliable from a data standpoint
796 if (ctl_rcvbspace(kctl
, so
, len
, flags
) != 0) {
799 (SInt64
*)&kctlstat
.kcs_enqueue_fullsock
);
803 * Unlink from the list, m is on its own
806 so_recv_data_stat(so
, m
, 0);
807 if (sbappendrecord(&so
->so_rcv
, m
) != 0) {
811 * We free or return the remaining
817 (SInt64
*)&kctlstat
.kcs_enqueue_fullsock
);
822 if (needwakeup
&& (flags
& CTL_DATA_NOWAKEUP
) == 0)
827 if (ctl_debug
&& error
!= 0 && (flags
& CTL_DATA_CRIT
))
828 printf("%s - crit data err %d len %d hiwat %d cc: %d\n",
829 __func__
, error
, len
,
830 so
->so_rcv
.sb_hiwat
, so
->so_rcv
.sb_cc
);
832 socket_unlock(so
, 1);
837 if (m
!= NULL
&& socket_debug
&& so
!= NULL
&&
838 (so
->so_options
& SO_DEBUG
)) {
841 printf("%s m_list %llx\n", __func__
,
842 (uint64_t) VM_KERNEL_ADDRPERM(m_list
));
843 for (n
= m
; n
!= NULL
; n
= n
->m_nextpkt
)
844 printf(" remain %llx m_next %llx\n",
845 (uint64_t) VM_KERNEL_ADDRPERM(n
),
846 (uint64_t) VM_KERNEL_ADDRPERM(n
->m_next
));
853 OSIncrementAtomic64((SInt64
*)&kctlstat
.kcs_enqueue_fail
);
858 ctl_enqueuedata(void *kctlref
, u_int32_t unit
, void *data
, size_t len
,
864 struct kctl
*kctl
= (struct kctl
*)kctlref
;
865 unsigned int num_needed
;
872 so
= kcb_find_socket(kctl
, unit
);
876 if (ctl_rcvbspace(kctl
, so
, len
, flags
) != 0) {
878 OSIncrementAtomic64((SInt64
*)&kctlstat
.kcs_enqueue_fullsock
);
883 m
= m_allocpacket_internal(&num_needed
, len
, NULL
, M_NOWAIT
, 1, 0);
885 printf("ctl_enqueuedata: m_allocpacket_internal(%lu) failed\n",
891 for (n
= m
; n
!= NULL
; n
= n
->m_next
) {
892 size_t mlen
= mbuf_maxlen(n
);
894 if (mlen
+ curlen
> len
)
897 bcopy((char *)data
+ curlen
, n
->m_data
, mlen
);
900 mbuf_pkthdr_setlen(m
, curlen
);
902 if ((flags
& CTL_DATA_EOR
))
904 so_recv_data_stat(so
, m
, 0);
905 if (sbappend(&so
->so_rcv
, m
) != 0) {
906 if ((flags
& CTL_DATA_NOWAKEUP
) == 0)
910 OSIncrementAtomic64((SInt64
*)&kctlstat
.kcs_enqueue_fullsock
);
914 if (ctl_debug
&& error
!= 0 && (flags
& CTL_DATA_CRIT
))
915 printf("%s - crit data err %d len %d hiwat %d cc: %d\n",
916 __func__
, error
, (int)len
,
917 so
->so_rcv
.sb_hiwat
, so
->so_rcv
.sb_cc
);
919 socket_unlock(so
, 1);
921 OSIncrementAtomic64((SInt64
*)&kctlstat
.kcs_enqueue_fail
);
927 ctl_getenqueuespace(kern_ctl_ref kctlref
, u_int32_t unit
, size_t *space
)
929 struct kctl
*kctl
= (struct kctl
*)kctlref
;
933 if (kctlref
== NULL
|| space
== NULL
)
936 so
= kcb_find_socket(kctl
, unit
);
940 avail
= sbspace(&so
->so_rcv
);
941 *space
= (avail
< 0) ? 0 : avail
;
942 socket_unlock(so
, 1);
948 ctl_getenqueuereadable(kern_ctl_ref kctlref
, u_int32_t unit
,
949 u_int32_t
*difference
)
951 struct kctl
*kctl
= (struct kctl
*)kctlref
;
954 if (kctlref
== NULL
|| difference
== NULL
)
957 so
= kcb_find_socket(kctl
, unit
);
961 if (so
->so_rcv
.sb_cc
>= so
->so_rcv
.sb_lowat
) {
964 *difference
= (so
->so_rcv
.sb_lowat
- so
->so_rcv
.sb_cc
);
966 socket_unlock(so
, 1);
972 ctl_ctloutput(struct socket
*so
, struct sockopt
*sopt
)
974 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
980 if (sopt
->sopt_level
!= SYSPROTO_CONTROL
) {
984 if (kcb
== NULL
) /* sanity check */
987 if ((kctl
= kcb
->kctl
) == NULL
)
990 switch (sopt
->sopt_dir
) {
992 if (kctl
->setopt
== NULL
)
994 if (sopt
->sopt_valsize
== 0) {
997 MALLOC(data
, void *, sopt
->sopt_valsize
, M_TEMP
,
1001 error
= sooptcopyin(sopt
, data
,
1003 sopt
->sopt_valsize
);
1006 socket_unlock(so
, 0);
1007 error
= (*kctl
->setopt
)(kcb
->kctl
, kcb
->unit
,
1011 sopt
->sopt_valsize
);
1018 if (kctl
->getopt
== NULL
)
1021 if (sopt
->sopt_valsize
&& sopt
->sopt_val
) {
1022 MALLOC(data
, void *, sopt
->sopt_valsize
, M_TEMP
,
1027 * 4108337 - copy user data in case the
1028 * kernel control needs it
1030 error
= sooptcopyin(sopt
, data
,
1031 sopt
->sopt_valsize
, sopt
->sopt_valsize
);
1033 len
= sopt
->sopt_valsize
;
1034 socket_unlock(so
, 0);
1035 error
= (*kctl
->getopt
)(kcb
->kctl
, kcb
->unit
,
1036 kcb
->userdata
, sopt
->sopt_name
,
1038 if (data
!= NULL
&& len
> sopt
->sopt_valsize
)
1039 panic_plain("ctl_ctloutput: ctl %s returned "
1040 "len (%lu) > sopt_valsize (%lu)\n",
1041 kcb
->kctl
->name
, len
,
1042 sopt
->sopt_valsize
);
1046 error
= sooptcopyout(sopt
, data
, len
);
1048 sopt
->sopt_valsize
= len
;
1058 ctl_ioctl(struct socket
*so
, u_long cmd
, caddr_t data
,
1059 struct ifnet
*ifp
, struct proc
*p
)
1061 #pragma unused(so, ifp, p)
1062 int error
= ENOTSUP
;
1065 /* get the number of controllers */
1066 case CTLIOCGCOUNT
: {
1070 lck_mtx_lock(ctl_mtx
);
1071 TAILQ_FOREACH(kctl
, &ctl_head
, next
)
1073 lck_mtx_unlock(ctl_mtx
);
1075 bcopy(&n
, data
, sizeof (n
));
1080 struct ctl_info ctl_info
;
1081 struct kctl
*kctl
= 0;
1084 bcopy(data
, &ctl_info
, sizeof (ctl_info
));
1085 name_len
= strnlen(ctl_info
.ctl_name
, MAX_KCTL_NAME
);
1087 if (name_len
== 0 || name_len
+ 1 > MAX_KCTL_NAME
) {
1091 lck_mtx_lock(ctl_mtx
);
1092 kctl
= ctl_find_by_name(ctl_info
.ctl_name
);
1093 lck_mtx_unlock(ctl_mtx
);
1098 ctl_info
.ctl_id
= kctl
->id
;
1099 bcopy(&ctl_info
, data
, sizeof (ctl_info
));
1104 /* add controls to get list of NKEs */
1112 * Register/unregister a NKE
1115 ctl_register(struct kern_ctl_reg
*userkctl
, kern_ctl_ref
*kctlref
)
1117 struct kctl
*kctl
= NULL
;
1118 struct kctl
*kctl_next
= NULL
;
1121 int is_extended
= 0;
1124 if (userkctl
== NULL
) /* sanity check */
1126 if (userkctl
->ctl_connect
== NULL
)
1128 name_len
= strlen(userkctl
->ctl_name
);
1129 if (name_len
== 0 || name_len
+ 1 > MAX_KCTL_NAME
)
1132 MALLOC(kctl
, struct kctl
*, sizeof(*kctl
), M_TEMP
, M_WAITOK
);
1135 bzero((char *)kctl
, sizeof(*kctl
));
1137 lck_mtx_lock(ctl_mtx
);
1140 * Kernel Control IDs
1142 * CTL_FLAG_REG_ID_UNIT indicates the control ID and unit number are
1143 * static. If they do not exist, add them to the list in order. If the
1144 * flag is not set, we must find a new unique value. We assume the
1145 * list is in order. We find the last item in the list and add one. If
1146 * this leads to wrapping the id around, we start at the front of the
1147 * list and look for a gap.
1150 if ((userkctl
->ctl_flags
& CTL_FLAG_REG_ID_UNIT
) == 0) {
1151 /* Must dynamically assign an unused ID */
1153 /* Verify the same name isn't already registered */
1154 if (ctl_find_by_name(userkctl
->ctl_name
) != NULL
) {
1155 lck_mtx_unlock(ctl_mtx
);
1160 /* Start with 1 in case the list is empty */
1162 kctl_next
= TAILQ_LAST(&ctl_head
, kctl_list
);
1164 if (kctl_next
!= NULL
) {
1165 /* List was not empty, add one to the last item */
1166 id
= kctl_next
->id
+ 1;
1170 * If this wrapped the id number, start looking at
1171 * the front of the list for an unused id.
1174 /* Find the next unused ID */
1177 TAILQ_FOREACH(kctl_next
, &ctl_head
, next
) {
1178 if (kctl_next
->id
> id
) {
1179 /* We found a gap */
1183 id
= kctl_next
->id
+ 1;
1188 userkctl
->ctl_id
= id
;
1190 kctl
->reg_unit
= -1;
1192 TAILQ_FOREACH(kctl_next
, &ctl_head
, next
) {
1193 if (kctl_next
->id
> userkctl
->ctl_id
)
1197 if (ctl_find_by_id_unit(userkctl
->ctl_id
, userkctl
->ctl_unit
)) {
1198 lck_mtx_unlock(ctl_mtx
);
1202 kctl
->id
= userkctl
->ctl_id
;
1203 kctl
->reg_unit
= userkctl
->ctl_unit
;
1206 is_extended
= (userkctl
->ctl_flags
& CTL_FLAG_REG_EXTENDED
);
1208 strlcpy(kctl
->name
, userkctl
->ctl_name
, MAX_KCTL_NAME
);
1209 kctl
->flags
= userkctl
->ctl_flags
;
1212 * Let the caller know the default send and receive sizes
1214 * rdar://15526688: Limit the send and receive sizes to sb_max
1215 * by using the same scaling as sbreserve()
1217 sbmaxsize
= (u_quad_t
)sb_max
* MCLBYTES
/ (MSIZE
+ MCLBYTES
);
1219 if (userkctl
->ctl_sendsize
== 0)
1220 kctl
->sendbufsize
= CTL_SENDSIZE
;
1221 else if (userkctl
->ctl_sendsize
> sbmaxsize
)
1222 kctl
->sendbufsize
= sbmaxsize
;
1224 kctl
->sendbufsize
= userkctl
->ctl_sendsize
;
1225 userkctl
->ctl_sendsize
= kctl
->sendbufsize
;
1227 if (userkctl
->ctl_recvsize
== 0)
1228 kctl
->recvbufsize
= CTL_RECVSIZE
;
1229 else if (userkctl
->ctl_recvsize
> sbmaxsize
)
1230 kctl
->recvbufsize
= sbmaxsize
;
1232 kctl
->recvbufsize
= userkctl
->ctl_recvsize
;
1233 userkctl
->ctl_recvsize
= kctl
->recvbufsize
;
1235 kctl
->connect
= userkctl
->ctl_connect
;
1236 kctl
->disconnect
= userkctl
->ctl_disconnect
;
1237 kctl
->send
= userkctl
->ctl_send
;
1238 kctl
->setopt
= userkctl
->ctl_setopt
;
1239 kctl
->getopt
= userkctl
->ctl_getopt
;
1241 kctl
->rcvd
= userkctl
->ctl_rcvd
;
1242 kctl
->send_list
= userkctl
->ctl_send_list
;
1245 TAILQ_INIT(&kctl
->kcb_head
);
1248 TAILQ_INSERT_BEFORE(kctl_next
, kctl
, next
);
1250 TAILQ_INSERT_TAIL(&ctl_head
, kctl
, next
);
1252 kctlstat
.kcs_reg_count
++;
1253 kctlstat
.kcs_gencnt
++;
1255 lck_mtx_unlock(ctl_mtx
);
1259 ctl_post_msg(KEV_CTL_REGISTERED
, kctl
->id
);
1264 ctl_deregister(void *kctlref
)
1268 if (kctlref
== NULL
) /* sanity check */
1271 lck_mtx_lock(ctl_mtx
);
1272 TAILQ_FOREACH(kctl
, &ctl_head
, next
) {
1273 if (kctl
== (struct kctl
*)kctlref
)
1276 if (kctl
!= (struct kctl
*)kctlref
) {
1277 lck_mtx_unlock(ctl_mtx
);
1280 if (!TAILQ_EMPTY(&kctl
->kcb_head
)) {
1281 lck_mtx_unlock(ctl_mtx
);
1285 TAILQ_REMOVE(&ctl_head
, kctl
, next
);
1287 kctlstat
.kcs_reg_count
--;
1288 kctlstat
.kcs_gencnt
++;
1290 lck_mtx_unlock(ctl_mtx
);
1292 ctl_post_msg(KEV_CTL_DEREGISTERED
, kctl
->id
);
1298 * Must be called with global ctl_mtx lock taked
1300 static struct kctl
*
1301 ctl_find_by_name(const char *name
)
1305 lck_mtx_assert(ctl_mtx
, LCK_MTX_ASSERT_OWNED
);
1307 TAILQ_FOREACH(kctl
, &ctl_head
, next
)
1308 if (strncmp(kctl
->name
, name
, sizeof(kctl
->name
)) == 0)
1315 ctl_id_by_name(const char *name
)
1317 u_int32_t ctl_id
= 0;
1320 lck_mtx_lock(ctl_mtx
);
1321 kctl
= ctl_find_by_name(name
);
1324 lck_mtx_unlock(ctl_mtx
);
1330 ctl_name_by_id(u_int32_t id
, char *out_name
, size_t maxsize
)
1335 lck_mtx_lock(ctl_mtx
);
1336 TAILQ_FOREACH(kctl
, &ctl_head
, next
) {
1341 if (kctl
&& kctl
->name
) {
1342 if (maxsize
> MAX_KCTL_NAME
)
1343 maxsize
= MAX_KCTL_NAME
;
1344 strlcpy(out_name
, kctl
->name
, maxsize
);
1347 lck_mtx_unlock(ctl_mtx
);
1349 return (found
? 0 : ENOENT
);
1353 * Must be called with global ctl_mtx lock taked
1356 static struct kctl
*
1357 ctl_find_by_id_unit(u_int32_t id
, u_int32_t unit
)
1361 lck_mtx_assert(ctl_mtx
, LCK_MTX_ASSERT_OWNED
);
1363 TAILQ_FOREACH(kctl
, &ctl_head
, next
) {
1364 if (kctl
->id
== id
&& (kctl
->flags
& CTL_FLAG_REG_ID_UNIT
) == 0)
1366 else if (kctl
->id
== id
&& kctl
->reg_unit
== unit
)
1373 * Must be called with kernel controller lock taken
1375 static struct ctl_cb
*
1376 kcb_find(struct kctl
*kctl
, u_int32_t unit
)
1380 lck_mtx_assert(ctl_mtx
, LCK_MTX_ASSERT_OWNED
);
1382 TAILQ_FOREACH(kcb
, &kctl
->kcb_head
, next
)
1383 if (kcb
->unit
== unit
)
1389 static struct socket
*
1390 kcb_find_socket(struct kctl
*kctl
, u_int32_t unit
)
1392 struct socket
*so
= NULL
;
1396 lr_saved
= __builtin_return_address(0);
1398 lck_mtx_lock(ctl_mtx
);
1399 kcb
= kcb_find(kctl
, unit
);
1400 if (kcb
&& kcb
->kctl
== kctl
) {
1406 lck_mtx_unlock(ctl_mtx
);
1414 lck_mtx_lock(ctl_mtx
);
1415 if (kcb
->kctl
== NULL
) {
1416 lck_mtx_unlock(ctl_mtx
);
1417 socket_unlock(so
, 1);
1419 lck_mtx_lock(ctl_mtx
);
1422 * The socket lock history is more useful if we store
1423 * the address of the caller.
1425 int i
= (so
->next_lock_lr
+ SO_LCKDBG_MAX
- 1) % SO_LCKDBG_MAX
;
1427 so
->lock_lr
[i
] = lr_saved
;
1430 if (kcb
->usecount
== 0)
1431 wakeup((event_t
)&kcb
->usecount
);
1432 lck_mtx_unlock(ctl_mtx
);
1438 ctl_post_msg(u_int32_t event_code
, u_int32_t id
)
1440 struct ctl_event_data ctl_ev_data
;
1441 struct kev_msg ev_msg
;
1443 lck_mtx_assert(ctl_mtx
, LCK_MTX_ASSERT_NOTOWNED
);
1445 bzero(&ev_msg
, sizeof(struct kev_msg
));
1446 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
1448 ev_msg
.kev_class
= KEV_SYSTEM_CLASS
;
1449 ev_msg
.kev_subclass
= KEV_CTL_SUBCLASS
;
1450 ev_msg
.event_code
= event_code
;
1452 /* common nke subclass data */
1453 bzero(&ctl_ev_data
, sizeof(ctl_ev_data
));
1454 ctl_ev_data
.ctl_id
= id
;
1455 ev_msg
.dv
[0].data_ptr
= &ctl_ev_data
;
1456 ev_msg
.dv
[0].data_length
= sizeof(ctl_ev_data
);
1458 ev_msg
.dv
[1].data_length
= 0;
1460 kev_post_msg(&ev_msg
);
1464 ctl_lock(struct socket
*so
, int refcount
, void *lr
)
1469 lr_saved
= __builtin_return_address(0);
1473 if (so
->so_pcb
!= NULL
) {
1474 lck_mtx_lock(((struct ctl_cb
*)so
->so_pcb
)->mtx
);
1476 panic("ctl_lock: so=%p NO PCB! lr=%p lrh= %s\n",
1477 so
, lr_saved
, solockhistory_nr(so
));
1481 if (so
->so_usecount
< 0) {
1482 panic("ctl_lock: so=%p so_pcb=%p lr=%p ref=%x lrh= %s\n",
1483 so
, so
->so_pcb
, lr_saved
, so
->so_usecount
,
1484 solockhistory_nr(so
));
1491 so
->lock_lr
[so
->next_lock_lr
] = lr_saved
;
1492 so
->next_lock_lr
= (so
->next_lock_lr
+1) % SO_LCKDBG_MAX
;
1497 ctl_unlock(struct socket
*so
, int refcount
, void *lr
)
1500 lck_mtx_t
*mutex_held
;
1503 lr_saved
= __builtin_return_address(0);
1507 #ifdef MORE_KCTLLOCK_DEBUG
1508 printf("ctl_unlock: so=%llx sopcb=%x lock=%llx ref=%u lr=%llx\n",
1509 (uint64_t)VM_KERNEL_ADDRPERM(so
),
1510 (uint64_t)VM_KERNEL_ADDRPERM(so
->so_pcb
,
1511 (uint64_t)VM_KERNEL_ADDRPERM(((struct ctl_cb
*)so
->so_pcb
)->mtx
),
1512 so
->so_usecount
, (uint64_t)VM_KERNEL_ADDRPERM(lr_saved
));
1517 if (so
->so_usecount
< 0) {
1518 panic("ctl_unlock: so=%p usecount=%x lrh= %s\n",
1519 so
, so
->so_usecount
, solockhistory_nr(so
));
1522 if (so
->so_pcb
== NULL
) {
1523 panic("ctl_unlock: so=%p NO PCB usecount=%x lr=%p lrh= %s\n",
1524 so
, so
->so_usecount
, (void *)lr_saved
,
1525 solockhistory_nr(so
));
1528 mutex_held
= ((struct ctl_cb
*)so
->so_pcb
)->mtx
;
1530 lck_mtx_assert(mutex_held
, LCK_MTX_ASSERT_OWNED
);
1531 so
->unlock_lr
[so
->next_unlock_lr
] = lr_saved
;
1532 so
->next_unlock_lr
= (so
->next_unlock_lr
+1) % SO_LCKDBG_MAX
;
1533 lck_mtx_unlock(mutex_held
);
1535 if (so
->so_usecount
== 0)
1536 ctl_sofreelastref(so
);
1542 ctl_getlock(struct socket
*so
, int locktype
)
1544 #pragma unused(locktype)
1545 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
1548 if (so
->so_usecount
< 0)
1549 panic("ctl_getlock: so=%p usecount=%x lrh= %s\n",
1550 so
, so
->so_usecount
, solockhistory_nr(so
));
1553 panic("ctl_getlock: so=%p NULL NO so_pcb %s\n",
1554 so
, solockhistory_nr(so
));
1555 return (so
->so_proto
->pr_domain
->dom_mtx
);
1559 __private_extern__
int
1560 kctl_reg_list SYSCTL_HANDLER_ARGS
1562 #pragma unused(oidp, arg1, arg2)
1565 struct xsystmgen xsg
;
1568 size_t item_size
= ROUNDUP64(sizeof (struct xkctl_reg
));
1570 buf
= _MALLOC(item_size
, M_TEMP
, M_WAITOK
| M_ZERO
);
1574 lck_mtx_lock(ctl_mtx
);
1576 n
= kctlstat
.kcs_reg_count
;
1578 if (req
->oldptr
== USER_ADDR_NULL
) {
1579 req
->oldidx
= (n
+ n
/8) * sizeof(struct xkctl_reg
);
1582 if (req
->newptr
!= USER_ADDR_NULL
) {
1586 bzero(&xsg
, sizeof (xsg
));
1587 xsg
.xg_len
= sizeof (xsg
);
1589 xsg
.xg_gen
= kctlstat
.kcs_gencnt
;
1590 xsg
.xg_sogen
= so_gencnt
;
1591 error
= SYSCTL_OUT(req
, &xsg
, sizeof (xsg
));
1596 * We are done if there is no pcb
1603 for (i
= 0, kctl
= TAILQ_FIRST(&ctl_head
);
1604 i
< n
&& kctl
!= NULL
;
1605 i
++, kctl
= TAILQ_NEXT(kctl
, next
)) {
1606 struct xkctl_reg
*xkr
= (struct xkctl_reg
*)buf
;
1608 u_int32_t pcbcount
= 0;
1610 TAILQ_FOREACH(kcb
, &kctl
->kcb_head
, next
)
1613 bzero(buf
, item_size
);
1615 xkr
->xkr_len
= sizeof(struct xkctl_reg
);
1616 xkr
->xkr_kind
= XSO_KCREG
;
1617 xkr
->xkr_id
= kctl
->id
;
1618 xkr
->xkr_reg_unit
= kctl
->reg_unit
;
1619 xkr
->xkr_flags
= kctl
->flags
;
1620 xkr
->xkr_kctlref
= (uint64_t)VM_KERNEL_ADDRPERM(kctl
);
1621 xkr
->xkr_recvbufsize
= kctl
->recvbufsize
;
1622 xkr
->xkr_sendbufsize
= kctl
->sendbufsize
;
1623 xkr
->xkr_lastunit
= kctl
->lastunit
;
1624 xkr
->xkr_pcbcount
= pcbcount
;
1625 xkr
->xkr_connect
= (uint64_t)VM_KERNEL_ADDRPERM(kctl
->connect
);
1626 xkr
->xkr_disconnect
=
1627 (uint64_t)VM_KERNEL_ADDRPERM(kctl
->disconnect
);
1628 xkr
->xkr_send
= (uint64_t)VM_KERNEL_ADDRPERM(kctl
->send
);
1629 xkr
->xkr_send_list
=
1630 (uint64_t)VM_KERNEL_ADDRPERM(kctl
->send_list
);
1631 xkr
->xkr_setopt
= (uint64_t)VM_KERNEL_ADDRPERM(kctl
->setopt
);
1632 xkr
->xkr_getopt
= (uint64_t)VM_KERNEL_ADDRPERM(kctl
->getopt
);
1633 xkr
->xkr_rcvd
= (uint64_t)VM_KERNEL_ADDRPERM(kctl
->rcvd
);
1634 strlcpy(xkr
->xkr_name
, kctl
->name
, sizeof(xkr
->xkr_name
));
1636 error
= SYSCTL_OUT(req
, buf
, item_size
);
1641 * Give the user an updated idea of our state.
1642 * If the generation differs from what we told
1643 * her before, she knows that something happened
1644 * while we were processing this request, and it
1645 * might be necessary to retry.
1647 bzero(&xsg
, sizeof (xsg
));
1648 xsg
.xg_len
= sizeof (xsg
);
1650 xsg
.xg_gen
= kctlstat
.kcs_gencnt
;
1651 xsg
.xg_sogen
= so_gencnt
;
1652 error
= SYSCTL_OUT(req
, &xsg
, sizeof (xsg
));
1659 lck_mtx_unlock(ctl_mtx
);
1667 __private_extern__
int
1668 kctl_pcblist SYSCTL_HANDLER_ARGS
1670 #pragma unused(oidp, arg1, arg2)
1673 struct xsystmgen xsg
;
1676 size_t item_size
= ROUNDUP64(sizeof (struct xkctlpcb
)) +
1677 ROUNDUP64(sizeof (struct xsocket_n
)) +
1678 2 * ROUNDUP64(sizeof (struct xsockbuf_n
)) +
1679 ROUNDUP64(sizeof (struct xsockstat_n
));
1681 buf
= _MALLOC(item_size
, M_TEMP
, M_WAITOK
| M_ZERO
);
1685 lck_mtx_lock(ctl_mtx
);
1687 n
= kctlstat
.kcs_pcbcount
;
1689 if (req
->oldptr
== USER_ADDR_NULL
) {
1690 req
->oldidx
= (n
+ n
/8) * item_size
;
1693 if (req
->newptr
!= USER_ADDR_NULL
) {
1697 bzero(&xsg
, sizeof (xsg
));
1698 xsg
.xg_len
= sizeof (xsg
);
1700 xsg
.xg_gen
= kctlstat
.kcs_gencnt
;
1701 xsg
.xg_sogen
= so_gencnt
;
1702 error
= SYSCTL_OUT(req
, &xsg
, sizeof (xsg
));
1707 * We are done if there is no pcb
1714 for (i
= 0, kctl
= TAILQ_FIRST(&ctl_head
);
1715 i
< n
&& kctl
!= NULL
;
1716 kctl
= TAILQ_NEXT(kctl
, next
)) {
1719 for (kcb
= TAILQ_FIRST(&kctl
->kcb_head
);
1720 i
< n
&& kcb
!= NULL
;
1721 i
++, kcb
= TAILQ_NEXT(kcb
, next
)) {
1722 struct xkctlpcb
*xk
= (struct xkctlpcb
*)buf
;
1723 struct xsocket_n
*xso
= (struct xsocket_n
*)
1724 ADVANCE64(xk
, sizeof (*xk
));
1725 struct xsockbuf_n
*xsbrcv
= (struct xsockbuf_n
*)
1726 ADVANCE64(xso
, sizeof (*xso
));
1727 struct xsockbuf_n
*xsbsnd
= (struct xsockbuf_n
*)
1728 ADVANCE64(xsbrcv
, sizeof (*xsbrcv
));
1729 struct xsockstat_n
*xsostats
= (struct xsockstat_n
*)
1730 ADVANCE64(xsbsnd
, sizeof (*xsbsnd
));
1732 bzero(buf
, item_size
);
1734 xk
->xkp_len
= sizeof(struct xkctlpcb
);
1735 xk
->xkp_kind
= XSO_KCB
;
1736 xk
->xkp_unit
= kcb
->unit
;
1737 xk
->xkp_kctpcb
= (uint64_t)VM_KERNEL_ADDRPERM(kcb
);
1738 xk
->xkp_kctlref
= (uint64_t)VM_KERNEL_ADDRPERM(kctl
);
1739 xk
->xkp_kctlid
= kctl
->id
;
1740 strlcpy(xk
->xkp_kctlname
, kctl
->name
,
1741 sizeof(xk
->xkp_kctlname
));
1743 sotoxsocket_n(kcb
->so
, xso
);
1744 sbtoxsockbuf_n(kcb
->so
?
1745 &kcb
->so
->so_rcv
: NULL
, xsbrcv
);
1746 sbtoxsockbuf_n(kcb
->so
?
1747 &kcb
->so
->so_snd
: NULL
, xsbsnd
);
1748 sbtoxsockstat_n(kcb
->so
, xsostats
);
1750 error
= SYSCTL_OUT(req
, buf
, item_size
);
1756 * Give the user an updated idea of our state.
1757 * If the generation differs from what we told
1758 * her before, she knows that something happened
1759 * while we were processing this request, and it
1760 * might be necessary to retry.
1762 bzero(&xsg
, sizeof (xsg
));
1763 xsg
.xg_len
= sizeof (xsg
);
1765 xsg
.xg_gen
= kctlstat
.kcs_gencnt
;
1766 xsg
.xg_sogen
= so_gencnt
;
1767 error
= SYSCTL_OUT(req
, &xsg
, sizeof (xsg
));
1774 lck_mtx_unlock(ctl_mtx
);
1780 kctl_getstat SYSCTL_HANDLER_ARGS
1782 #pragma unused(oidp, arg1, arg2)
1785 lck_mtx_lock(ctl_mtx
);
1787 if (req
->newptr
!= USER_ADDR_NULL
) {
1791 if (req
->oldptr
== USER_ADDR_NULL
) {
1792 req
->oldidx
= sizeof(struct kctlstat
);
1796 error
= SYSCTL_OUT(req
, &kctlstat
,
1797 MIN(sizeof(struct kctlstat
), req
->oldlen
));
1799 lck_mtx_unlock(ctl_mtx
);