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