xref: /xnu-12377.1.9/bsd/sys/kern_control.h (revision f6217f891ac0bb64f3d375211650a4c1ff8ca1ea)
1*f6217f89SApple OSS Distributions /*
2*f6217f89SApple OSS Distributions  * Copyright (c) 2000-2004, 2012-2025 Apple Inc. All rights reserved.
3*f6217f89SApple OSS Distributions  *
4*f6217f89SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*f6217f89SApple OSS Distributions  *
6*f6217f89SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*f6217f89SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*f6217f89SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*f6217f89SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*f6217f89SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*f6217f89SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*f6217f89SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*f6217f89SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*f6217f89SApple OSS Distributions  *
15*f6217f89SApple OSS Distributions  * Please obtain a copy of the License at
16*f6217f89SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*f6217f89SApple OSS Distributions  *
18*f6217f89SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*f6217f89SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*f6217f89SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*f6217f89SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*f6217f89SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*f6217f89SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*f6217f89SApple OSS Distributions  * limitations under the License.
25*f6217f89SApple OSS Distributions  *
26*f6217f89SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*f6217f89SApple OSS Distributions  */
28*f6217f89SApple OSS Distributions /*!
29*f6217f89SApple OSS Distributions  *       @header kern_control.h
30*f6217f89SApple OSS Distributions  *       This header defines an API to communicate between a kernel
31*f6217f89SApple OSS Distributions  *       extension and a process outside of the kernel.
32*f6217f89SApple OSS Distributions  */
33*f6217f89SApple OSS Distributions 
34*f6217f89SApple OSS Distributions #ifndef KPI_KERN_CONTROL_H
35*f6217f89SApple OSS Distributions #define KPI_KERN_CONTROL_H
36*f6217f89SApple OSS Distributions 
37*f6217f89SApple OSS Distributions 
38*f6217f89SApple OSS Distributions #include <sys/appleapiopts.h>
39*f6217f89SApple OSS Distributions #include <sys/_types/_u_char.h>
40*f6217f89SApple OSS Distributions #include <sys/_types/_u_int16_t.h>
41*f6217f89SApple OSS Distributions #include <sys/_types/_u_int32_t.h>
42*f6217f89SApple OSS Distributions #include <sys/_types/_u_int64_t.h>
43*f6217f89SApple OSS Distributions 
44*f6217f89SApple OSS Distributions /*
45*f6217f89SApple OSS Distributions  * Define Controller event subclass, and associated events.
46*f6217f89SApple OSS Distributions  * Subclass of KEV_SYSTEM_CLASS
47*f6217f89SApple OSS Distributions  */
48*f6217f89SApple OSS Distributions 
49*f6217f89SApple OSS Distributions /*!
50*f6217f89SApple OSS Distributions  *       @defined KEV_CTL_SUBCLASS
51*f6217f89SApple OSS Distributions  *   @discussion The kernel event subclass for kernel control events.
52*f6217f89SApple OSS Distributions  */
53*f6217f89SApple OSS Distributions #define KEV_CTL_SUBCLASS        2
54*f6217f89SApple OSS Distributions 
55*f6217f89SApple OSS Distributions /*!
56*f6217f89SApple OSS Distributions  *       @defined KEV_CTL_REGISTERED
57*f6217f89SApple OSS Distributions  *   @discussion The event code indicating a new controller was
58*f6217f89SApple OSS Distributions  *       registered. The data portion will contain a ctl_event_data.
59*f6217f89SApple OSS Distributions  */
60*f6217f89SApple OSS Distributions #define KEV_CTL_REGISTERED      1       /* a new controller appears */
61*f6217f89SApple OSS Distributions 
62*f6217f89SApple OSS Distributions /*!
63*f6217f89SApple OSS Distributions  *       @defined KEV_CTL_DEREGISTERED
64*f6217f89SApple OSS Distributions  *   @discussion The event code indicating a controller was unregistered.
65*f6217f89SApple OSS Distributions  *       The data portion will contain a ctl_event_data.
66*f6217f89SApple OSS Distributions  */
67*f6217f89SApple OSS Distributions #define KEV_CTL_DEREGISTERED    2       /* a controller disappears */
68*f6217f89SApple OSS Distributions 
69*f6217f89SApple OSS Distributions /*!
70*f6217f89SApple OSS Distributions  *       @struct ctl_event_data
71*f6217f89SApple OSS Distributions  *       @discussion This structure is used for KEV_CTL_SUBCLASS kernel
72*f6217f89SApple OSS Distributions  *               events.
73*f6217f89SApple OSS Distributions  *       @field ctl_id The kernel control id.
74*f6217f89SApple OSS Distributions  *       @field ctl_unit The kernel control unit.
75*f6217f89SApple OSS Distributions  */
76*f6217f89SApple OSS Distributions struct ctl_event_data {
77*f6217f89SApple OSS Distributions 	u_int32_t   ctl_id;             /* Kernel Controller ID */
78*f6217f89SApple OSS Distributions 	u_int32_t   ctl_unit;
79*f6217f89SApple OSS Distributions };
80*f6217f89SApple OSS Distributions 
81*f6217f89SApple OSS Distributions /*
82*f6217f89SApple OSS Distributions  * Controls destined to the Controller Manager.
83*f6217f89SApple OSS Distributions  */
84*f6217f89SApple OSS Distributions 
85*f6217f89SApple OSS Distributions /*!
86*f6217f89SApple OSS Distributions  *       @defined CTLIOCGCOUNT
87*f6217f89SApple OSS Distributions  *   @discussion The CTLIOCGCOUNT ioctl can be used to determine the
88*f6217f89SApple OSS Distributions  *       number of kernel controllers registered.
89*f6217f89SApple OSS Distributions  */
90*f6217f89SApple OSS Distributions #define CTLIOCGCOUNT    _IOR('N', 2, int)               /* get number of control structures registered */
91*f6217f89SApple OSS Distributions 
92*f6217f89SApple OSS Distributions /*!
93*f6217f89SApple OSS Distributions  *       @defined CTLIOCGINFO
94*f6217f89SApple OSS Distributions  *   @discussion The CTLIOCGINFO ioctl can be used to convert a kernel
95*f6217f89SApple OSS Distributions  *       control name to a kernel control id.
96*f6217f89SApple OSS Distributions  */
97*f6217f89SApple OSS Distributions #define CTLIOCGINFO     _IOWR('N', 3, struct ctl_info)  /* get id from name */
98*f6217f89SApple OSS Distributions 
99*f6217f89SApple OSS Distributions 
100*f6217f89SApple OSS Distributions /*!
101*f6217f89SApple OSS Distributions  *       @defined MAX_KCTL_NAME
102*f6217f89SApple OSS Distributions  *   @discussion Kernel control names must be no longer than
103*f6217f89SApple OSS Distributions  *       MAX_KCTL_NAME.
104*f6217f89SApple OSS Distributions  */
105*f6217f89SApple OSS Distributions #define MAX_KCTL_NAME   96
106*f6217f89SApple OSS Distributions 
107*f6217f89SApple OSS Distributions /*
108*f6217f89SApple OSS Distributions  * Controls destined to the Controller Manager.
109*f6217f89SApple OSS Distributions  */
110*f6217f89SApple OSS Distributions 
111*f6217f89SApple OSS Distributions /*!
112*f6217f89SApple OSS Distributions  *       @struct ctl_info
113*f6217f89SApple OSS Distributions  *       @discussion This structure is used with the CTLIOCGINFO ioctl to
114*f6217f89SApple OSS Distributions  *               translate from a kernel control name to a control id.
115*f6217f89SApple OSS Distributions  *       @field ctl_id The kernel control id, filled out upon return.
116*f6217f89SApple OSS Distributions  *       @field ctl_name The kernel control name to find.
117*f6217f89SApple OSS Distributions  */
118*f6217f89SApple OSS Distributions struct ctl_info {
119*f6217f89SApple OSS Distributions 	u_int32_t   ctl_id;                             /* Kernel Controller ID  */
120*f6217f89SApple OSS Distributions 	char        ctl_name[MAX_KCTL_NAME];            /* Kernel Controller Name (a C string) */
121*f6217f89SApple OSS Distributions };
122*f6217f89SApple OSS Distributions 
123*f6217f89SApple OSS Distributions 
124*f6217f89SApple OSS Distributions /*!
125*f6217f89SApple OSS Distributions  *       @struct sockaddr_ctl
126*f6217f89SApple OSS Distributions  *       @discussion The controller address structure is used to establish
127*f6217f89SApple OSS Distributions  *               contact between a user client and a kernel controller. The
128*f6217f89SApple OSS Distributions  *               sc_id/sc_unit uniquely identify each controller. sc_id is a
129*f6217f89SApple OSS Distributions  *               unique identifier assigned to the controller. The identifier can
130*f6217f89SApple OSS Distributions  *               be assigned by the system at registration time or be a 32-bit
131*f6217f89SApple OSS Distributions  *               creator code obtained from Apple Computer. sc_unit is a unit
132*f6217f89SApple OSS Distributions  *               number for this sc_id, and is privately used by the kernel
133*f6217f89SApple OSS Distributions  *               controller to identify several instances of the controller.
134*f6217f89SApple OSS Distributions  *       @field sc_len The length of the structure.
135*f6217f89SApple OSS Distributions  *       @field sc_family AF_SYSTEM.
136*f6217f89SApple OSS Distributions  *       @field ss_sysaddr AF_SYS_KERNCONTROL.
137*f6217f89SApple OSS Distributions  *       @field sc_id Controller unique identifier.
138*f6217f89SApple OSS Distributions  *       @field sc_unit Kernel controller private unit number.
139*f6217f89SApple OSS Distributions  *       @field sc_reserved Reserved, must be set to zero.
140*f6217f89SApple OSS Distributions  */
141*f6217f89SApple OSS Distributions struct sockaddr_ctl {
142*f6217f89SApple OSS Distributions 	u_char      sc_len;     /* depends on size of bundle ID string */
143*f6217f89SApple OSS Distributions 	u_char      sc_family;  /* AF_SYSTEM */
144*f6217f89SApple OSS Distributions 	u_int16_t   ss_sysaddr; /* AF_SYS_KERNCONTROL */
145*f6217f89SApple OSS Distributions 	u_int32_t   sc_id;      /* Controller unique identifier  */
146*f6217f89SApple OSS Distributions 	u_int32_t   sc_unit;    /* Developer private unit number */
147*f6217f89SApple OSS Distributions 	u_int32_t   sc_reserved[5];
148*f6217f89SApple OSS Distributions };
149*f6217f89SApple OSS Distributions 
150*f6217f89SApple OSS Distributions #ifdef KERNEL
151*f6217f89SApple OSS Distributions 
152*f6217f89SApple OSS Distributions #include <sys/kpi_mbuf.h>
153*f6217f89SApple OSS Distributions 
154*f6217f89SApple OSS Distributions /*!
155*f6217f89SApple OSS Distributions  *       @typedef kern_ctl_ref
156*f6217f89SApple OSS Distributions  *       @discussion A control reference is used to track an attached kernel
157*f6217f89SApple OSS Distributions  *               control. Registering a kernel control will create a kernel
158*f6217f89SApple OSS Distributions  *               control reference. This reference is required for sending data
159*f6217f89SApple OSS Distributions  *               or removing the kernel control. This reference will be passed to
160*f6217f89SApple OSS Distributions  *               callbacks for that kernel control.
161*f6217f89SApple OSS Distributions  */
162*f6217f89SApple OSS Distributions typedef void * kern_ctl_ref;
163*f6217f89SApple OSS Distributions 
164*f6217f89SApple OSS Distributions /*!
165*f6217f89SApple OSS Distributions  *       @defined CTL_FLAG_PRIVILEGED
166*f6217f89SApple OSS Distributions  *   @discussion The CTL_FLAG_PRIVILEGED flag is passed in ctl_flags. If
167*f6217f89SApple OSS Distributions  *       this flag is set, only privileged processes may attach to this
168*f6217f89SApple OSS Distributions  *       kernel control.
169*f6217f89SApple OSS Distributions  */
170*f6217f89SApple OSS Distributions #define CTL_FLAG_PRIVILEGED     0x1
171*f6217f89SApple OSS Distributions /*!
172*f6217f89SApple OSS Distributions  *       @defined CTL_FLAG_REG_ID_UNIT
173*f6217f89SApple OSS Distributions  *   @discussion The CTL_FLAG_REG_ID_UNIT flag is passed to indicate that
174*f6217f89SApple OSS Distributions  *       the ctl_id specified should be used. If this flag is not
175*f6217f89SApple OSS Distributions  *       present, a unique ctl_id will be dynamically assigned to your
176*f6217f89SApple OSS Distributions  *       kernel control. The CTLIOCGINFO ioctl can be used by the client
177*f6217f89SApple OSS Distributions  *       to find the dynamically assigned id based on the control name
178*f6217f89SApple OSS Distributions  *       specified in ctl_name.
179*f6217f89SApple OSS Distributions  */
180*f6217f89SApple OSS Distributions #define CTL_FLAG_REG_ID_UNIT    0x2
181*f6217f89SApple OSS Distributions /*!
182*f6217f89SApple OSS Distributions  *       @defined CTL_FLAG_REG_SOCK_STREAM
183*f6217f89SApple OSS Distributions  *   @discussion Use the CTL_FLAG_REG_SOCK_STREAM flag when client need to open
184*f6217f89SApple OSS Distributions  *       socket of type SOCK_STREAM to communicate with the kernel control.
185*f6217f89SApple OSS Distributions  *       By default kernel control sockets are of type SOCK_DGRAM.
186*f6217f89SApple OSS Distributions  */
187*f6217f89SApple OSS Distributions #define CTL_FLAG_REG_SOCK_STREAM        0x4
188*f6217f89SApple OSS Distributions 
189*f6217f89SApple OSS Distributions /* Data flags for controllers */
190*f6217f89SApple OSS Distributions /*!
191*f6217f89SApple OSS Distributions  *       @defined CTL_DATA_NOWAKEUP
192*f6217f89SApple OSS Distributions  *   @discussion The CTL_DATA_NOWAKEUP flag can be used for the enqueue
193*f6217f89SApple OSS Distributions  *       data and enqueue mbuf functions to indicate that the process
194*f6217f89SApple OSS Distributions  *       should not be woken up yet. This is useful when you want to
195*f6217f89SApple OSS Distributions  *       enqueue data using more than one call but only want to wake up
196*f6217f89SApple OSS Distributions  *       the client after all of the data has been enqueued.
197*f6217f89SApple OSS Distributions  */
198*f6217f89SApple OSS Distributions #define CTL_DATA_NOWAKEUP       0x1
199*f6217f89SApple OSS Distributions 
200*f6217f89SApple OSS Distributions /*!
201*f6217f89SApple OSS Distributions  *       @defined CTL_DATA_EOR
202*f6217f89SApple OSS Distributions  *   @discussion The CTL_DATA_EOR flag can be used for the enqueue
203*f6217f89SApple OSS Distributions  *       data and enqueue mbuf functions to mark the end of a record.
204*f6217f89SApple OSS Distributions  */
205*f6217f89SApple OSS Distributions #define CTL_DATA_EOR            0x2
206*f6217f89SApple OSS Distributions 
207*f6217f89SApple OSS Distributions __BEGIN_DECLS
208*f6217f89SApple OSS Distributions 
209*f6217f89SApple OSS Distributions /*!
210*f6217f89SApple OSS Distributions  *       @typedef ctl_connect_func
211*f6217f89SApple OSS Distributions  *       @discussion The ctl_connect_func is used to receive
212*f6217f89SApple OSS Distributions  *               notification of a client connecting to the kernel control.
213*f6217f89SApple OSS Distributions  *       @param kctlref The control ref for the kernel control the client is
214*f6217f89SApple OSS Distributions  *               connecting to.
215*f6217f89SApple OSS Distributions  *       @param sac The address used to connect to this control. The field sc_unit
216*f6217f89SApple OSS Distributions  *               contains the unit number of the kernel control instance the client is
217*f6217f89SApple OSS Distributions  *               connecting to. If CTL_FLAG_REG_ID_UNIT was set when the kernel control
218*f6217f89SApple OSS Distributions  *               was registered, sc_unit is the ctl_unit of the kern_ctl_reg structure.
219*f6217f89SApple OSS Distributions  *               If CTL_FLAG_REG_ID_UNIT was not set when the kernel control was
220*f6217f89SApple OSS Distributions  *               registered, sc_unit is the dynamically allocated unit number of
221*f6217f89SApple OSS Distributions  *               the new kernel control instance that is used for this connection.
222*f6217f89SApple OSS Distributions  *       @param unitinfo A placeholder for a pointer to the optional user-defined
223*f6217f89SApple OSS Distributions  *               private data associated with this kernel control instance.  This
224*f6217f89SApple OSS Distributions  *               opaque info will be provided to the user when the rest of the
225*f6217f89SApple OSS Distributions  *               callback routines are executed.  For example, it can be used
226*f6217f89SApple OSS Distributions  *               to pass a pointer to an instance-specific data structure in
227*f6217f89SApple OSS Distributions  *               order for the user to keep track of the states related to this
228*f6217f89SApple OSS Distributions  *               kernel control instance.
229*f6217f89SApple OSS Distributions  */
230*f6217f89SApple OSS Distributions typedef errno_t (*ctl_connect_func)(kern_ctl_ref kctlref,
231*f6217f89SApple OSS Distributions     struct sockaddr_ctl *sac,
232*f6217f89SApple OSS Distributions     void **unitinfo);
233*f6217f89SApple OSS Distributions 
234*f6217f89SApple OSS Distributions /*!
235*f6217f89SApple OSS Distributions  *       @typedef ctl_disconnect_func
236*f6217f89SApple OSS Distributions  *       @discussion The ctl_disconnect_func is used to receive notification
237*f6217f89SApple OSS Distributions  *               that a client has disconnected from the kernel control. This
238*f6217f89SApple OSS Distributions  *               usually happens when the socket is closed. If this is the last
239*f6217f89SApple OSS Distributions  *               socket attached to your kernel control, you may unregister your
240*f6217f89SApple OSS Distributions  *               kernel control from this callback.
241*f6217f89SApple OSS Distributions  *       @param kctlref The control ref for the kernel control instance the client has
242*f6217f89SApple OSS Distributions  *               disconnected from.
243*f6217f89SApple OSS Distributions  *       @param unit The unit number of the kernel control instance the client has
244*f6217f89SApple OSS Distributions  *               disconnected from.
245*f6217f89SApple OSS Distributions  *       @param unitinfo The user-defined private data initialized by the
246*f6217f89SApple OSS Distributions  *               ctl_connect_func callback.
247*f6217f89SApple OSS Distributions  */
248*f6217f89SApple OSS Distributions typedef errno_t (*ctl_disconnect_func)(kern_ctl_ref kctlref, u_int32_t unit, void *unitinfo);
249*f6217f89SApple OSS Distributions 
250*f6217f89SApple OSS Distributions /*!
251*f6217f89SApple OSS Distributions  *       @typedef ctl_send_func
252*f6217f89SApple OSS Distributions  *       @discussion The ctl_send_func is used to receive data sent from
253*f6217f89SApple OSS Distributions  *               the client to the kernel control.
254*f6217f89SApple OSS Distributions  *       @param kctlref The control ref of the kernel control.
255*f6217f89SApple OSS Distributions  *       @param unit The unit number of the kernel control instance the client has
256*f6217f89SApple OSS Distributions  *               connected to.
257*f6217f89SApple OSS Distributions  *       @param unitinfo The user-defined private data initialized by the
258*f6217f89SApple OSS Distributions  *               ctl_connect_func callback.
259*f6217f89SApple OSS Distributions  *       @param m The data sent by the client to the kernel control in an
260*f6217f89SApple OSS Distributions  *               mbuf chain. Your function is responsible for releasing the
261*f6217f89SApple OSS Distributions  *               mbuf chain.
262*f6217f89SApple OSS Distributions  *       @param flags The flags specified by the client when calling
263*f6217f89SApple OSS Distributions  *               send/sendto/sendmsg (MSG_OOB/MSG_DONTROUTE).
264*f6217f89SApple OSS Distributions  */
265*f6217f89SApple OSS Distributions typedef errno_t (*ctl_send_func)(kern_ctl_ref kctlref, u_int32_t unit, void *unitinfo,
266*f6217f89SApple OSS Distributions     mbuf_t m, int flags);
267*f6217f89SApple OSS Distributions 
268*f6217f89SApple OSS Distributions /*!
269*f6217f89SApple OSS Distributions  *       @typedef ctl_setopt_func
270*f6217f89SApple OSS Distributions  *       @discussion The ctl_setopt_func is used to handle set socket option
271*f6217f89SApple OSS Distributions  *               calls for the SYSPROTO_CONTROL option level.
272*f6217f89SApple OSS Distributions  *       @param kctlref The control ref of the kernel control.
273*f6217f89SApple OSS Distributions  *       @param unit The unit number of the kernel control instance.
274*f6217f89SApple OSS Distributions  *       @param unitinfo The user-defined private data initialized by the
275*f6217f89SApple OSS Distributions  *               ctl_connect_func callback.
276*f6217f89SApple OSS Distributions  *       @param opt The socket option.
277*f6217f89SApple OSS Distributions  *       @param data A pointer to the socket option data. The data has
278*f6217f89SApple OSS Distributions  *               already been copied in to the kernel for you.
279*f6217f89SApple OSS Distributions  *       @param len The length of the socket option data.
280*f6217f89SApple OSS Distributions  */
281*f6217f89SApple OSS Distributions typedef errno_t (*ctl_setopt_func)(kern_ctl_ref kctlref, u_int32_t unit, void *unitinfo,
282*f6217f89SApple OSS Distributions     int opt, void *data, size_t len);
283*f6217f89SApple OSS Distributions 
284*f6217f89SApple OSS Distributions /*!
285*f6217f89SApple OSS Distributions  *       @typedef ctl_getopt_func
286*f6217f89SApple OSS Distributions  *       @discussion The ctl_getopt_func is used to handle client get socket
287*f6217f89SApple OSS Distributions  *               option requests for the SYSPROTO_CONTROL option level. A buffer
288*f6217f89SApple OSS Distributions  *               is allocated for storage and passed to your function. The length
289*f6217f89SApple OSS Distributions  *               of that buffer is also passed. Upon return, you should set *len
290*f6217f89SApple OSS Distributions  *               to length of the buffer used. In some cases, data may be NULL.
291*f6217f89SApple OSS Distributions  *               When this happens, *len should be set to the length you would
292*f6217f89SApple OSS Distributions  *               have returned had data not been NULL. If the buffer is too small,
293*f6217f89SApple OSS Distributions  *               return an error.
294*f6217f89SApple OSS Distributions  *       @param kctlref The control ref of the kernel control.
295*f6217f89SApple OSS Distributions  *       @param unit The unit number of the kernel control instance.
296*f6217f89SApple OSS Distributions  *       @param unitinfo The user-defined private data initialized by the
297*f6217f89SApple OSS Distributions  *               ctl_connect_func callback.
298*f6217f89SApple OSS Distributions  *       @param opt The socket option.
299*f6217f89SApple OSS Distributions  *       @param data A buffer to copy the results in to. May be NULL, see
300*f6217f89SApple OSS Distributions  *               discussion.
301*f6217f89SApple OSS Distributions  *       @param len A pointer to the length of the buffer. This should be set
302*f6217f89SApple OSS Distributions  *               to the length of the buffer used before returning.
303*f6217f89SApple OSS Distributions  */
304*f6217f89SApple OSS Distributions typedef errno_t (*ctl_getopt_func)(kern_ctl_ref kctlref, u_int32_t unit, void *unitinfo,
305*f6217f89SApple OSS Distributions     int opt, void *data, size_t *len);
306*f6217f89SApple OSS Distributions 
307*f6217f89SApple OSS Distributions #ifdef KERNEL_PRIVATE
308*f6217f89SApple OSS Distributions /*
309*f6217f89SApple OSS Distributions  * KERN_CTL_REG_OPAQUE means that kern_ctl_reg is an opaque structure
310*f6217f89SApple OSS Distributions  * in the public header, and redeclared in kern_control_private.h.
311*f6217f89SApple OSS Distributions  */
312*f6217f89SApple OSS Distributions #define KERN_CTL_REG_OPAQUE
313*f6217f89SApple OSS Distributions #endif /* KERNEL_PRIVATE */
314*f6217f89SApple OSS Distributions #ifdef KERN_CTL_REG_OPAQUE
315*f6217f89SApple OSS Distributions /*!
316*f6217f89SApple OSS Distributions  *       @struct kern_ctl_reg
317*f6217f89SApple OSS Distributions  *       @discussion This structure defines the properties of a kernel
318*f6217f89SApple OSS Distributions  *               control being registered.
319*f6217f89SApple OSS Distributions  */
320*f6217f89SApple OSS Distributions struct kern_ctl_reg;
321*f6217f89SApple OSS Distributions #else
322*f6217f89SApple OSS Distributions /*!
323*f6217f89SApple OSS Distributions  *       @struct kern_ctl_reg
324*f6217f89SApple OSS Distributions  *       @discussion This structure defines the properties of a kernel
325*f6217f89SApple OSS Distributions  *               control being registered.
326*f6217f89SApple OSS Distributions  *       @field ctl_name A Bundle ID string of up to MAX_KCTL_NAME bytes (including the ending zero).
327*f6217f89SApple OSS Distributions  *               This string should not be empty.
328*f6217f89SApple OSS Distributions  *       @field ctl_id The control ID may be dynamically assigned or it can be a
329*f6217f89SApple OSS Distributions  *               32-bit creator code assigned by DTS.
330*f6217f89SApple OSS Distributions  *               For a DTS assigned creator code the CTL_FLAG_REG_ID_UNIT flag must be set.
331*f6217f89SApple OSS Distributions  *               For a dynamically assigned control ID, do not set the CTL_FLAG_REG_ID_UNIT flag.
332*f6217f89SApple OSS Distributions  *               The  value of the dynamically assigned control ID is set to this field
333*f6217f89SApple OSS Distributions  *               when the registration succeeds.
334*f6217f89SApple OSS Distributions  *       @field ctl_unit A separate unit number to register multiple units that
335*f6217f89SApple OSS Distributions  *               share the same control ID with DTS assigned creator code when
336*f6217f89SApple OSS Distributions  *               the CTL_FLAG_REG_ID_UNIT flag is set.
337*f6217f89SApple OSS Distributions  *               This field is ignored for a dynamically assigned control ID.
338*f6217f89SApple OSS Distributions  *       @field ctl_flags CTL_FLAG_PRIVILEGED and/or CTL_FLAG_REG_ID_UNIT.
339*f6217f89SApple OSS Distributions  *       @field ctl_sendsize Override the default send size. If set to zero,
340*f6217f89SApple OSS Distributions  *               the default send size will be used, and this default value
341*f6217f89SApple OSS Distributions  *               is set to this field to be retrieved by the caller.
342*f6217f89SApple OSS Distributions  *       @field ctl_recvsize Override the default receive size. If set to
343*f6217f89SApple OSS Distributions  *               zero, the default receive size will be used, and this default value
344*f6217f89SApple OSS Distributions  *               is set to this field to be retrieved by the caller.
345*f6217f89SApple OSS Distributions  *       @field ctl_connect Specify the  function to be called whenever a client
346*f6217f89SApple OSS Distributions  *               connects to the kernel control. This field must be specified.
347*f6217f89SApple OSS Distributions  *       @field ctl_disconnect Specify a function to be called whenever a
348*f6217f89SApple OSS Distributions  *               client disconnects from the kernel control.
349*f6217f89SApple OSS Distributions  *       @field ctl_send Specify a function to handle data send from the
350*f6217f89SApple OSS Distributions  *               client to the kernel control.
351*f6217f89SApple OSS Distributions  *       @field ctl_setopt Specify a function to handle set socket option
352*f6217f89SApple OSS Distributions  *               operations for the kernel control.
353*f6217f89SApple OSS Distributions  *       @field ctl_getopt Specify a function to handle get socket option
354*f6217f89SApple OSS Distributions  *               operations for the kernel control.
355*f6217f89SApple OSS Distributions  */
356*f6217f89SApple OSS Distributions struct kern_ctl_reg {
357*f6217f89SApple OSS Distributions 	/* control information */
358*f6217f89SApple OSS Distributions 	char            ctl_name[MAX_KCTL_NAME];
359*f6217f89SApple OSS Distributions 	u_int32_t       ctl_id;
360*f6217f89SApple OSS Distributions 	u_int32_t       ctl_unit;
361*f6217f89SApple OSS Distributions 
362*f6217f89SApple OSS Distributions 	/* control settings */
363*f6217f89SApple OSS Distributions 	u_int32_t   ctl_flags;
364*f6217f89SApple OSS Distributions 	u_int32_t   ctl_sendsize;
365*f6217f89SApple OSS Distributions 	u_int32_t   ctl_recvsize;
366*f6217f89SApple OSS Distributions 
367*f6217f89SApple OSS Distributions 	/* Dispatch functions */
368*f6217f89SApple OSS Distributions 	ctl_connect_func    ctl_connect;
369*f6217f89SApple OSS Distributions 	ctl_disconnect_func ctl_disconnect;
370*f6217f89SApple OSS Distributions 	ctl_send_func               ctl_send;
371*f6217f89SApple OSS Distributions 	ctl_setopt_func             ctl_setopt;
372*f6217f89SApple OSS Distributions 	ctl_getopt_func             ctl_getopt;
373*f6217f89SApple OSS Distributions };
374*f6217f89SApple OSS Distributions #endif /* KERN_CTL_REG_OPAQUE */
375*f6217f89SApple OSS Distributions 
376*f6217f89SApple OSS Distributions /*!
377*f6217f89SApple OSS Distributions  *       @function ctl_register
378*f6217f89SApple OSS Distributions  *       @discussion Register a kernel control. This will enable clients to
379*f6217f89SApple OSS Distributions  *               connect to the kernel control using a PF_SYSTEM socket.
380*f6217f89SApple OSS Distributions  *       @param userkctl A structure defining the kernel control to be
381*f6217f89SApple OSS Distributions  *               attached. The ctl_connect callback must be specified, the other callbacks
382*f6217f89SApple OSS Distributions  *               are optional. If ctl_connect is set to zero, ctl_register fails with
383*f6217f89SApple OSS Distributions  *               the error code EINVAL.
384*f6217f89SApple OSS Distributions  *       @param kctlref Upon successful return, the kctlref will contain a
385*f6217f89SApple OSS Distributions  *               reference to the attached kernel control. This reference is used
386*f6217f89SApple OSS Distributions  *               to unregister the kernel control. This reference will also be
387*f6217f89SApple OSS Distributions  *               passed in to the callbacks each time they are called.
388*f6217f89SApple OSS Distributions  *       @result 0 - Kernel control was registered.
389*f6217f89SApple OSS Distributions  *               EINVAL - The registration structure was not valid.
390*f6217f89SApple OSS Distributions  *               ENOMEM - There was insufficient memory.
391*f6217f89SApple OSS Distributions  *               EEXIST - A controller with that id/unit is already registered.
392*f6217f89SApple OSS Distributions  */
393*f6217f89SApple OSS Distributions errno_t
394*f6217f89SApple OSS Distributions ctl_register(struct kern_ctl_reg *userkctl, kern_ctl_ref *kctlref);
395*f6217f89SApple OSS Distributions 
396*f6217f89SApple OSS Distributions /*!
397*f6217f89SApple OSS Distributions  *       @function ctl_deregister
398*f6217f89SApple OSS Distributions  *       @discussion Unregister a kernel control. A kernel extension must
399*f6217f89SApple OSS Distributions  *               unregister it's kernel control(s) before unloading. If a kernel
400*f6217f89SApple OSS Distributions  *               control has clients attached, this call will fail.
401*f6217f89SApple OSS Distributions  *       @param kctlref The control reference of the control to unregister.
402*f6217f89SApple OSS Distributions  *       @result 0 - Kernel control was unregistered.
403*f6217f89SApple OSS Distributions  *               EINVAL - The kernel control reference was invalid.
404*f6217f89SApple OSS Distributions  *               EBUSY - The kernel control has clients still attached.
405*f6217f89SApple OSS Distributions  */
406*f6217f89SApple OSS Distributions errno_t
407*f6217f89SApple OSS Distributions ctl_deregister(kern_ctl_ref kctlref);
408*f6217f89SApple OSS Distributions 
409*f6217f89SApple OSS Distributions /*!
410*f6217f89SApple OSS Distributions  *       @function ctl_enqueuedata
411*f6217f89SApple OSS Distributions  *       @discussion Send data from the kernel control to the client.
412*f6217f89SApple OSS Distributions  *       @param kctlref The control reference of the kernel control.
413*f6217f89SApple OSS Distributions  *       @param unit The unit number of the kernel control instance.
414*f6217f89SApple OSS Distributions  *       @param data A pointer to the data to send.
415*f6217f89SApple OSS Distributions  *       @param len The length of data to send.
416*f6217f89SApple OSS Distributions  *       @param flags Send flags. CTL_DATA_NOWAKEUP and CTL_DATA_EOR are currently
417*f6217f89SApple OSS Distributions  *               the only supported flags.
418*f6217f89SApple OSS Distributions  *       @result 0 - Data was enqueued to be read by the client.
419*f6217f89SApple OSS Distributions  *               EINVAL - Invalid parameters.
420*f6217f89SApple OSS Distributions  *               EMSGSIZE - The buffer is too large.
421*f6217f89SApple OSS Distributions  *               ENOBUFS - The queue is full or there are no free mbufs.
422*f6217f89SApple OSS Distributions  */
423*f6217f89SApple OSS Distributions errno_t
424*f6217f89SApple OSS Distributions     ctl_enqueuedata(kern_ctl_ref kctlref, u_int32_t unit, void *__sized_by(len) data,
425*f6217f89SApple OSS Distributions     size_t len, u_int32_t flags);
426*f6217f89SApple OSS Distributions 
427*f6217f89SApple OSS Distributions /*!
428*f6217f89SApple OSS Distributions  *       @function ctl_enqueuembuf
429*f6217f89SApple OSS Distributions  *       @discussion Send data stored in an mbuf chain from the kernel
430*f6217f89SApple OSS Distributions  *               control to the client. The caller is responsible for freeing
431*f6217f89SApple OSS Distributions  *               the mbuf chain if ctl_enqueuembuf returns an error.
432*f6217f89SApple OSS Distributions  *       @param kctlref The control reference of the kernel control.
433*f6217f89SApple OSS Distributions  *       @param unit The unit number of the kernel control instance.
434*f6217f89SApple OSS Distributions  *       @param m An mbuf chain containing the data to send to the client.
435*f6217f89SApple OSS Distributions  *       @param flags Send flags. CTL_DATA_NOWAKEUP and CTL_DATA_EOR are currently
436*f6217f89SApple OSS Distributions  *               the only supported flags.
437*f6217f89SApple OSS Distributions  *       @result 0 - Data was enqueued to be read by the client.
438*f6217f89SApple OSS Distributions  *               EINVAL - Invalid parameters.
439*f6217f89SApple OSS Distributions  *               ENOBUFS - The queue is full.
440*f6217f89SApple OSS Distributions  */
441*f6217f89SApple OSS Distributions errno_t
442*f6217f89SApple OSS Distributions ctl_enqueuembuf(kern_ctl_ref kctlref, u_int32_t unit, mbuf_t m, u_int32_t flags);
443*f6217f89SApple OSS Distributions 
444*f6217f89SApple OSS Distributions /*!
445*f6217f89SApple OSS Distributions  *       @function ctl_getenqueuespace
446*f6217f89SApple OSS Distributions  *       @discussion Retrieve the amount of space currently available for data to be sent
447*f6217f89SApple OSS Distributions  *               from the kernel control to the client.
448*f6217f89SApple OSS Distributions  *       @param kctlref The control reference of the kernel control.
449*f6217f89SApple OSS Distributions  *       @param unit The unit number of the kernel control instance.
450*f6217f89SApple OSS Distributions  *       @param space The address where to return the current space available
451*f6217f89SApple OSS Distributions  *       @result 0 - Success; the amount of space is returned to caller.
452*f6217f89SApple OSS Distributions  *               EINVAL - Invalid parameters.
453*f6217f89SApple OSS Distributions  */
454*f6217f89SApple OSS Distributions errno_t
455*f6217f89SApple OSS Distributions ctl_getenqueuespace(kern_ctl_ref kctlref, u_int32_t unit, size_t *space);
456*f6217f89SApple OSS Distributions 
457*f6217f89SApple OSS Distributions /*!
458*f6217f89SApple OSS Distributions  *  @function ctl_getenqueuereadable
459*f6217f89SApple OSS Distributions  *  @discussion Retrieve the difference between enqueued bytes and
460*f6217f89SApple OSS Distributions  *       low-water mark for the socket receive buffer.
461*f6217f89SApple OSS Distributions  *  @param kctlref The control reference of the kernel control.
462*f6217f89SApple OSS Distributions  *  @param unit The unit number of the kernel control instance.
463*f6217f89SApple OSS Distributions  *  @param difference The address at which to return the current difference
464*f6217f89SApple OSS Distributions  *       between the low-water mark for the socket and the number of bytes
465*f6217f89SApple OSS Distributions  *       enqueued. 0 indicates that the socket is readable by the client
466*f6217f89SApple OSS Distributions  *       (the number of bytes in the buffer is above the low-water mark).
467*f6217f89SApple OSS Distributions  *  @result 0 - Success; the difference is returned to caller.
468*f6217f89SApple OSS Distributions  *  EINVAL - Invalid parameters.
469*f6217f89SApple OSS Distributions  */
470*f6217f89SApple OSS Distributions errno_t
471*f6217f89SApple OSS Distributions ctl_getenqueuereadable(kern_ctl_ref kctlref, u_int32_t unit, u_int32_t *difference);
472*f6217f89SApple OSS Distributions 
473*f6217f89SApple OSS Distributions __END_DECLS
474*f6217f89SApple OSS Distributions #endif /* KERNEL */
475*f6217f89SApple OSS Distributions 
476*f6217f89SApple OSS Distributions #if defined(PRIVATE) && !defined(MODULES_SUPPORTED)
477*f6217f89SApple OSS Distributions #include <sys/kern_control_private.h>
478*f6217f89SApple OSS Distributions #endif /* PRIVATE && !MODULES_SUPPORTED */
479*f6217f89SApple OSS Distributions 
480*f6217f89SApple OSS Distributions #endif /* KPI_KERN_CONTROL_H */
481