2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
31 @header kern_control.h
32 This header defines an API to communicate between a kernel
33 extension and a process outside of the kernel.
36 #ifndef KPI_KERN_CONTROL_H
37 #define KPI_KERN_CONTROL_H
40 #include <sys/appleapiopts.h>
43 * Define Controller event subclass, and associated events.
44 * Subclass of KEV_SYSTEM_CLASS
48 @defined KEV_CTL_SUBCLASS
49 @discussion The kernel event subclass for kernel control events.
51 #define KEV_CTL_SUBCLASS 2
54 @defined KEV_CTL_REGISTERED
55 @discussion The event code indicating a new controller was
56 registered. The data portion will contain a ctl_event_data.
58 #define KEV_CTL_REGISTERED 1 /* a new controller appears */
61 @defined KEV_CTL_DEREGISTERED
62 @discussion The event code indicating a controller was unregistered.
63 The data portion will contain a ctl_event_data.
65 #define KEV_CTL_DEREGISTERED 2 /* a controller disappears */
68 @struct ctl_event_data
69 @discussion This structure is used for KEV_CTL_SUBCLASS kernel
71 @field ctl_id The kernel control id.
72 @field ctl_unit The kernel control unit.
74 struct ctl_event_data
{
75 u_int32_t ctl_id
; /* Kernel Controller ID */
80 * Controls destined to the Controller Manager.
85 @discussion The CTLIOCGCOUNT ioctl can be used to determine the
86 number of kernel controllers registered.
88 #define CTLIOCGCOUNT _IOR('N', 2, int) /* get number of control structures registered */
92 @discussion The CTLIOCGINFO ioctl can be used to convert a kernel
93 control name to a kernel control id.
95 #define CTLIOCGINFO _IOWR('N', 3, struct ctl_info) /* get id from name */
99 @defined MAX_KCTL_NAME
100 @discussion Kernel control names must be no longer than
103 #define MAX_KCTL_NAME 96
106 * Controls destined to the Controller Manager.
111 @discussion This structure is used with the CTLIOCGINFO ioctl to
112 translate from a kernel control name to a control id.
113 @field ctl_id The kernel control id, filled out upon return.
114 @field ctl_name The kernel control name to find.
117 u_int32_t ctl_id
; /* Kernel Controller ID */
118 char ctl_name
[MAX_KCTL_NAME
]; /* Kernel Controller Name (a C string) */
124 @discussion The controller address structure is used to establish
125 contact between a user client and a kernel controller. The
126 sc_id/sc_unit uniquely identify each controller. sc_id is a
127 unique identifier assigned to the controller. The identifier can
128 be assigned by the system at registration time or be a 32-bit
129 creator code obtained from Apple Computer. sc_unit is a unit
130 number for this sc_id, and is privately used by the kernel
131 controller to identify several instances of the controller.
132 @field sc_len The length of the structure.
133 @field sc_family AF_SYSTEM.
134 @field ss_sysaddr AF_SYS_KERNCONTROL.
135 @field sc_id Controller unique identifier.
136 @field sc_unit Kernel controller private unit number.
137 @field sc_reserved Reserved, must be set to zero.
139 struct sockaddr_ctl
{
140 u_char sc_len
; /* depends on size of bundle ID string */
141 u_char sc_family
; /* AF_SYSTEM */
142 u_int16_t ss_sysaddr
; /* AF_SYS_KERNCONTROL */
143 u_int32_t sc_id
; /* Controller unique identifier */
144 u_int32_t sc_unit
; /* Developer private unit number */
145 u_int32_t sc_reserved
[5];
150 #include <sys/kpi_mbuf.h>
153 @typedef kern_ctl_ref
154 @discussion A control reference is used to track an attached kernel
155 control. Registering a kernel control will create a kernel
156 control reference. This reference is required for sending data
157 or removing the kernel control. This reference will be passed to
158 callbacks for that kernel control.
160 typedef void * kern_ctl_ref
;
163 @defined CTL_FLAG_PRIVILEGED
164 @discussion The CTL_FLAG_PRIVILEGED flag is passed in ctl_flags. If
165 this flag is set, only privileged processes may attach to this
168 #define CTL_FLAG_PRIVILEGED 0x1
170 @defined CTL_FLAG_REG_ID_UNIT
171 @discussion The CTL_FLAG_REG_ID_UNIT flag is passed to indicate that
172 the ctl_id specified should be used. If this flag is not
173 present, a unique ctl_id will be dynamically assigned to your
174 kernel control. The CTLIOCGINFO ioctl can be used by the client
175 to find the dynamically assigned id based on the control name
176 specified in ctl_name.
178 #define CTL_FLAG_REG_ID_UNIT 0x2
180 @defined CTL_FLAG_REG_SOCK_STREAM
181 @discussion Use the CTL_FLAG_REG_SOCK_STREAM flag when client need to open
182 socket of type SOCK_STREAM to communicate with the kernel control.
183 By default kernel control sockets are of type SOCK_DGRAM.
185 #define CTL_FLAG_REG_SOCK_STREAM 0x4
187 /* Data flags for controllers */
189 @defined CTL_DATA_NOWAKEUP
190 @discussion The CTL_DATA_NOWAKEUP flag can be used for the enqueue
191 data and enqueue mbuf functions to indicate that the process
192 should not be woken up yet. This is useful when you want to
193 enqueue data using more than one call but only want to wake up
194 the client after all of the data has been enqueued.
196 #define CTL_DATA_NOWAKEUP 0x1
198 @defined CTL_DATA_EOR
199 @discussion The CTL_DATA_NOWAKEUP flag can be used for the enqueue
200 data and enqueue mbuf functions to mark the end of a record.
202 #define CTL_DATA_EOR 0x2
205 @typedef ctl_connect_func
206 @discussion The ctl_connect_func is used to receive
207 notification of a client connecting to the kernel control.
208 @param kctlref The control ref for the kernel control the client is
210 @param sac The address used to connect to this control. The field sc_unit
211 contains the unit number of the kernel control instance the client is
212 connecting to. If CTL_FLAG_REG_ID_UNIT was set when the kernel control
213 was registered, sc_unit is the ctl_unit of the kern_ctl_reg structure.
214 If CTL_FLAG_REG_ID_UNIT was not set when the kernel control was
215 registered, sc_unit is the dynamically allocated unit number of
216 the new kernel control instance that is used for this connection.
217 @param unitinfo A place for the kernel control to store a pointer to
220 typedef errno_t (*ctl_connect_func
)(kern_ctl_ref kctlref
,
221 struct sockaddr_ctl
*sac
,
225 @typedef ctl_disconnect_func
226 @discussion The ctl_disconnect_func is used to receive notification
227 that a client has disconnected from the kernel control. This
228 usually happens when the socket is closed. If this is the last
229 socket attached to your kernel control, you may unregister your
230 kernel control from this callback.
231 @param kctlref The control ref for the kernel control instance the client has
233 @param unit The unit number of the kernel control instance the client has
235 @param unitinfo The unitinfo value specified by the connect function
236 when the client connected.
238 typedef errno_t (*ctl_disconnect_func
)(kern_ctl_ref kctlref
, u_int32_t unit
, void *unitinfo
);
241 @typedef ctl_send_func
242 @discussion The ctl_send_func is used to receive data sent from
243 the client to the kernel control.
244 @param kctlref The control ref of the kernel control.
245 @param unit The unit number of the kernel control instance the client has
247 @param unitinfo The unitinfo value specified by the connect function
248 when the client connected.
249 @param m The data sent by the client to the kernel control in an
251 @param flags The flags specified by the client when calling
252 send/sendto/sendmsg (MSG_OOB/MSG_DONTROUTE).
254 typedef errno_t (*ctl_send_func
)(kern_ctl_ref kctlref
, u_int32_t unit
, void *unitinfo
,
255 mbuf_t m
, int flags
);
258 @typedef ctl_setopt_func
259 @discussion The ctl_setopt_func is used to handle set socket option
260 calls for the SYSPROTO_CONTROL option level.
261 @param kctlref The control ref of the kernel control.
262 @param unit The unit number of the kernel control instance.
263 @param unitinfo The unitinfo value specified by the connect function
264 when the client connected.
265 @param opt The socket option.
266 @param data A pointer to the socket option data. The data has
267 already been copied in to the kernel for you.
268 @param len The length of the socket option data.
270 typedef errno_t (*ctl_setopt_func
)(kern_ctl_ref kctlref
, u_int32_t unit
, void *unitinfo
,
271 int opt
, void *data
, size_t len
);
274 @typedef ctl_getopt_func
275 @discussion The ctl_getopt_func is used to handle client get socket
276 option requests for the SYSPROTO_CONTROL option level. A buffer
277 is allocated for storage and passed to your function. The length
278 of that buffer is also passed. Upon return, you should set *len
279 to length of the buffer used. In some cases, data may be NULL.
280 When this happens, *len should be set to the length you would
281 have returned had data not been NULL. If the buffer is too small,
283 @param kctlref The control ref of the kernel control.
284 @param unit The unit number of the kernel control instance.
285 @param unitinfo The unitinfo value specified by the connect function
286 when the client connected.
287 @param opt The socket option.
288 @param data A buffer to copy the results in to. May be NULL, see
290 @param len A pointer to the length of the buffer. This should be set
291 to the length of the buffer used before returning.
293 typedef errno_t (*ctl_getopt_func
)(kern_ctl_ref kctlref
, u_int32_t unit
, void *unitinfo
,
294 int opt
, void *data
, size_t *len
);
298 @discussion This structure defines the properties of a kernel
299 control being registered.
300 @field ctl_name A Bundle ID string of up to MAX_KCTL_NAME bytes (including the ending zero).
301 This string should not be empty.
302 @field ctl_id The control ID may be dynamically assigned or it can be a
303 32-bit creator code assigned by DTS.
304 For a DTS assigned creator code the CTL_FLAG_REG_ID_UNIT flag must be set.
305 For a dynamically assigned control ID, do not set the CTL_FLAG_REG_ID_UNIT flag.
306 The value of the dynamically assigned control ID is set to this field
307 when the registration succeeds.
308 @field ctl_unit A separate unit number to register multiple units that
309 share the same control ID with DTS assigned creator code when
310 the CTL_FLAG_REG_ID_UNIT flag is set.
311 This field is ignored for a dynamically assigned control ID.
312 @field ctl_flags CTL_FLAG_PRIVILEGED and/or CTL_FLAG_REG_ID_UNIT.
313 @field ctl_sendsize Override the default send size. If set to zero,
314 the default send size will be used, and this default value
315 is set to this field to be retrieved by the caller.
316 @field ctl_recvsize Override the default receive size. If set to
317 zero, the default receive size will be used, and this default value
318 is set to this field to be retrieved by the caller.
319 @field ctl_connect Specify the function to be called whenever a client
320 connects to the kernel control. This field must be specified.
321 @field ctl_disconnect Specify a function to be called whenever a
322 client disconnects from the kernel control.
323 @field ctl_send Specify a function to handle data send from the
324 client to the kernel control.
325 @field ctl_setopt Specify a function to handle set socket option
326 operations for the kernel control.
327 @field ctl_getopt Specify a function to handle get socket option
328 operations for the kernel control.
332 /* control information */
333 char ctl_name
[MAX_KCTL_NAME
];
337 /* control settings */
339 u_int32_t ctl_sendsize
;
340 u_int32_t ctl_recvsize
;
342 /* Dispatch functions */
343 ctl_connect_func ctl_connect
;
344 ctl_disconnect_func ctl_disconnect
;
345 ctl_send_func ctl_send
;
346 ctl_setopt_func ctl_setopt
;
347 ctl_getopt_func ctl_getopt
;
351 @function ctl_register
352 @discussion Register a kernel control. This will enable clients to
353 connect to the kernel control using a PF_SYSTEM socket.
354 @param userkctl A structure defining the kernel control to be
355 attached. The ctl_connect callback must be specified, the other callbacks
356 are optional. If ctl_connect is set to zero, ctl_register fails with
357 the error code EINVAL.
358 @param kctlref Upon successful return, the kctlref will contain a
359 reference to the attached kernel control. This reference is used
360 to unregister the kernel control. This reference will also be
361 passed in to the callbacks each time they are called.
362 @result 0 - Kernel control was registered.
363 EINVAL - The registration structure was not valid.
364 ENOMEM - There was insufficient memory.
365 EEXIST - A controller with that id/unit is already registered.
368 ctl_register(struct kern_ctl_reg
*userkctl
, kern_ctl_ref
*kctlref
);
371 @function ctl_deregister
372 @discussion Unregister a kernel control. A kernel extension must
373 unregister it's kernel control(s) before unloading. If a kernel
374 control has clients attached, this call will fail.
375 @param kctlref The control reference of the control to unregister.
376 @result 0 - Kernel control was unregistered.
377 EINVAL - The kernel control reference was invalid.
378 EBUSY - The kernel control has clients still attached.
381 ctl_deregister(kern_ctl_ref kctlref
);
384 @function ctl_enqueuedata
385 @discussion Send data from the kernel control to the client.
386 @param kctlref The control reference of the kernel control.
387 @param unit The unit number of the kernel control instance.
388 @param data A pointer to the data to send.
389 @param len The length of data to send.
390 @param flags Send flags. CTL_DATA_NOWAKEUP is currently the only
392 @result 0 - Data was enqueued to be read by the client.
393 EINVAL - Invalid parameters.
394 EMSGSIZE - The buffer is too large.
395 ENOBUFS - The queue is full or there are no free mbufs.
398 ctl_enqueuedata(kern_ctl_ref kctlref
, u_int32_t unit
, void *data
, size_t len
, u_int32_t flags
);
401 @function ctl_enqueuembuf
402 @discussion Send data stored in an mbuf chain from the kernel
403 control to the client. The caller is responsible for freeing
404 the mbuf chain if ctl_enqueuembuf returns an error.
405 @param kctlref The control reference of the kernel control.
406 @param unit The unit number of the kernel control instance.
407 @param m An mbuf chain containing the data to send to the client.
408 @param flags Send flags. CTL_DATA_NOWAKEUP is currently the only
410 @result 0 - Data was enqueued to be read by the client.
411 EINVAL - Invalid parameters.
412 ENOBUFS - The queue is full.
415 ctl_enqueuembuf(kern_ctl_ref kctlref
, u_int32_t unit
, mbuf_t m
, u_int32_t flags
);
419 @function ctl_getenqueuespace
420 @discussion Retrieve the amount of space currently available for data to be sent
421 from the kernel control to the client.
422 @param kctlref The control reference of the kernel control.
423 @param unit The unit number of the kernel control instance.
424 @param space The address where to return the current space available
425 @result 0 - Data was enqueued to be read by the client.
426 EINVAL - Invalid parameters.
429 ctl_getenqueuespace(kern_ctl_ref kctlref
, u_int32_t unit
, size_t *space
);
434 #endif /* KPI_KERN_CONTROL_H */