2 * Copyright (c) 2002 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
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
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
21 * @APPLE_LICENSE_HEADER_END@
24 @header kpi_socketfilter.h
25 This header defines an API for intercepting communications at the
28 For the most part, socket filters want to do three things: Filter
29 data in and out, watch for state changes, and intercept a few calls
30 for security. The number of function pointers supplied by a socket
31 filter has been significantly reduced. The filter no longer has any
32 knowledge of socket buffers. The filter no longer intercepts nearly
33 every internal socket call. There are two data filters, an in
34 filter, and an out filter. The in filter occurs before data is
35 placed in the receive socket buffer. This is done to avoid waking
36 the process unnecessarily. The out filter occurs before the data is
37 appended to the send socket buffer. This should cover inbound and
38 outbound data. For monitoring state changes, we've added a notify
39 function that will be called when various events that the filter can
40 not intercept occur. In addition, we've added a few functions that a
41 filter may use to intercept common operations. These functions are:
42 connect (inbound), connect (outbound), bind, set socket option,
43 get socket option, and listen. Bind, listen, connect in, and connect
44 out could be used together to build a fairly comprehensive firewall
45 without having to do much with individual packets.
47 #ifndef __KPI_SOCKETFILTER__
48 #define __KPI_SOCKETFILTER__
50 #include <sys/kernel_types.h>
51 #include <sys/kpi_socket.h>
57 @abstract Constants defining mbuf flags. Only the flags listed below
58 can be set or retreieved.
59 @constant SFLT_GLOBAL Indicates this socket filter should be
60 attached to all new sockets when they're created.
61 @constant SFLT_PROG Indicates this socket filter should be attached
62 only when request by the application using the SO_NKE socket
69 typedef u_int32_t sflt_flags
;
73 @abstract A 4 byte identifier used with the SO_NKE socket option to
74 identify the socket filter to be attached.
76 typedef u_int32_t sflt_handle
;
80 @abstract Events notify a filter of state changes and other various
81 events related to the socket. These events can not be prevented
82 or intercepted, only observed.
83 @constant sock_evt_connected Indicates this socket has moved to the
85 @constant sock_evt_disconnected Indicates this socket has moved to
86 the disconnected state.
87 @constant sock_evt_flush_read The read socket buffer has been
89 @constant sock_evt_shutdown The read and or write side(s) of the
90 connection have been shutdown. The param will point to an
91 integer that indicates the direction that has been shutdown. See
92 'man 2 shutdown' for more information.
93 @constant sock_evt_cantrecvmore Indicates the socket can not receive
95 @constant sock_evt_cantsendmore Indicates the socket can not send
97 @constant sock_evt_closing Indicates the socket is closing.
100 sock_evt_connecting
= 1,
101 sock_evt_connected
= 2,
102 sock_evt_disconnecting
= 3,
103 sock_evt_disconnected
= 4,
104 sock_evt_flush_read
= 5,
105 sock_evt_shutdown
= 6, /* param points to an integer specifying how (read, write, or both) see man 2 shutdown */
106 sock_evt_cantrecvmore
= 7,
107 sock_evt_cantsendmore
= 8,
110 typedef u_int32_t sflt_event_t
;
113 @enum sflt_data_flag_t
114 @abstract Inbound and outbound data filters may handle many
115 different types of incoming and outgoing data. These flags help
116 distinguish between normal data, out-of-band data, and records.
117 @constant sock_data_filt_flag_oob Indicates this data is out-of-band
119 @constant sock_data_filt_flag_record Indicates this data is a
120 record. This flag is only ever seen on inbound data.
123 sock_data_filt_flag_oob
= 1,
124 sock_data_filt_flag_record
= 2
126 typedef u_int32_t sflt_data_flag_t
;
129 @typedef sf_unregistered_func
131 @discussion sf_unregistered_func is called to notify the filter it
132 has been unregistered. This is the last function the stack will
133 call and this function will only be called once all other
134 function calls in to your filter have completed. Once this
135 function has been called, your kext may safely unload.
136 @param handle The socket filter handle used to identify this filter.
138 typedef void (*sf_unregistered_func
)(sflt_handle handle
);
141 @typedef sf_attach_func
143 @discussion sf_attach_func is called to notify the filter it has
144 been attached to a socket. The filter may allocate memory for
145 this attachment and use the cookie to track it. This filter is
146 called in one of two cases:
147 1) You've installed a global filter and a new socket was created.
148 2) Your non-global socket filter is being attached using the SO_NKE
150 @param cookie Used to allow the socket filter to set the cookie for
152 @param so The socket the filter is being attached to.
153 @result If you return a non-zero value, your filter will not be
154 attached to this socket.
156 typedef errno_t (*sf_attach_func
)(void **cookie
, socket_t so
);
159 @typedef sf_detach_func
161 @discussion sf_detach_func is called to notify the filter it has
162 been detached from a socket. If the filter allocated any memory
163 for this attachment, it should be freed. This function will
164 be called when the socket is disposed of.
165 @param cookie Cookie value specified when the filter attach was
167 @param so The socket the filter is attached to.
168 @result If you return a non-zero value, your filter will not be
169 attached to this socket.
171 typedef void (*sf_detach_func
)(void *cookie
, socket_t so
);
174 @typedef sf_notify_func
176 @discussion sf_notify_func is called to notify the filter of various
177 state changes and other events occuring on the socket.
178 @param cookie Cookie value specified when the filter attach was
180 @param so The socket the filter is attached to.
181 @param event The type of event that has occurred.
182 @param param Additional information about the event.
184 typedef void (*sf_notify_func
)(void *cookie
, socket_t so
,
185 sflt_event_t event
, void *param
);
188 @typedef sf_getpeername_func
190 @discussion sf_getpeername_func is called to allow a filter to
191 to intercept the getpeername function. When called, sa will
192 point to a pointer to a socket address that was malloced
193 in zone M_SONAME. If you want to replace this address, either
194 modify the currenty copy or allocate a new one and free the
196 @param cookie Cookie value specified when the filter attach was
198 @param so The socket the filter is attached to.
199 @param sa A pointer to a socket address pointer.
200 @result If you return a non-zero value, processing will stop. If
201 you return EJUSTRETURN, no further filters will be called
202 but a result of zero will be returned to the caller of
205 typedef int (*sf_getpeername_func
)(void *cookie
, socket_t so
,
206 struct sockaddr
**sa
);
209 @typedef sf_getsockname_func
211 @discussion sf_getsockname_func is called to allow a filter to
212 to intercept the getsockname function. When called, sa will
213 point to a pointer to a socket address that was malloced
214 in zone M_SONAME. If you want to replace this address, either
215 modify the currenty copy or allocate a new one and free the
217 @param cookie Cookie value specified when the filter attach was
219 @param so The socket the filter is attached to.
220 @param sa A pointer to a socket address pointer.
221 @result If you return a non-zero value, processing will stop. If
222 you return EJUSTRETURN, no further filters will be called
223 but a result of zero will be returned to the caller of
226 typedef int (*sf_getsockname_func
)(void *cookie
, socket_t so
,
227 struct sockaddr
**sa
);
230 @typedef sf_data_in_func
232 @discussion sf_data_in_func is called to filter incoming data. If your
233 filter intercepts data for later reinjection, it must queue all incoming
234 data to preserve the order of the data. Use sock_inject_data_in to later
235 reinject this data if you return EJUSTRETURN. Warning: This filter is on
236 the data path. Do not spend excesive time. Do not wait for data on
238 @param cookie Cookie value specified when the filter attach was
240 @param so The socket the filter is attached to.
241 @param from The addres the data is from, may be NULL if the socket
243 @param data The data being received. Control data may appear in the
244 mbuf chain, be sure to check the mbuf types to find control
246 @param control Control data being passed separately from the data.
247 @param flags Flags to indicate if this is out of band data or a
250 0 - The caller will continue with normal processing of the data.
251 EJUSTRETURN - The caller will stop processing the data, the data will not be freed.
252 Anything Else - The caller will free the data and stop processing.
254 typedef errno_t (*sf_data_in_func
)(void *cookie
, socket_t so
,
255 const struct sockaddr
*from
, mbuf_t
*data
,
256 mbuf_t
*control
, sflt_data_flag_t flags
);
259 @typedef sf_data_out_func
261 @discussion sf_data_out_func is called to filter outbound data. If
262 your filter intercepts data for later reinjection, it must queue
263 all outbound data to preserve the order of the data when
264 reinjecting. Use sock_inject_data_out to later reinject this
266 @param cookie Cookie value specified when the filter attach was
268 @param so The socket the filter is attached to.
269 @param from The address the data is from, may be NULL if the socket
271 @param data The data being received. Control data may appear in the
272 mbuf chain, be sure to check the mbuf types to find control
274 @param control Control data being passed separately from the data.
275 @param flags Flags to indicate if this is out of band data or a
278 0 - The caller will continue with normal processing of the data.
279 EJUSTRETURN - The caller will stop processing the data, the data will not be freed.
280 Anything Else - The caller will free the data and stop processing.
282 typedef errno_t (*sf_data_out_func
)(void *cookie
, socket_t so
,
283 const struct sockaddr
*to
, mbuf_t
*data
,
284 mbuf_t
*control
, sflt_data_flag_t flags
);
287 @typedef sf_connect_in_func
289 @discussion sf_connect_in_func is called to filter inbound connections. A
290 protocol will call this before accepting an incoming connection and
291 placing it on the queue of completed connections. Warning: This filter
292 is on the data path. Do not spend excesive time. Do not wait for data on
294 @param cookie Cookie value specified when the filter attach was
296 @param so The socket the filter is attached to.
297 @param from The address the incoming connection is from.
299 0 - The caller will continue with normal processing of the connection.
300 Anything Else - The caller will rejecting the incoming connection.
302 typedef errno_t (*sf_connect_in_func
)(void *cookie
, socket_t so
,
303 const struct sockaddr
*from
);
306 @typedef sf_connect_out_func
308 @discussion sf_connect_out_func is called to filter outbound
309 connections. A protocol will call this before initiating an
311 @param cookie Cookie value specified when the filter attach was
313 @param so The socket the filter is attached to.
314 @param to The remote address of the outbound connection.
316 0 - The caller will continue with normal processing of the connection.
317 Anything Else - The caller will rejecting the outbound connection.
319 typedef errno_t (*sf_connect_out_func
)(void *cookie
, socket_t so
,
320 const struct sockaddr
*to
);
323 @typedef sf_bind_func
325 @discussion sf_bind_func is called before performing a bind
326 operation on a socket.
327 @param cookie Cookie value specified when the filter attach was
329 @param so The socket the filter is attached to.
330 @param to The local address of the socket will be bound to.
332 0 - The caller will continue with normal processing of the bind.
333 Anything Else - The caller will rejecting the bind.
335 typedef errno_t (*sf_bind_func
)(void *cookie
, socket_t so
,
336 const struct sockaddr
*to
);
339 @typedef sf_setoption_func
341 @discussion sf_setoption_func is called before performing setsockopt
343 @param cookie Cookie value specified when the filter attach was
345 @param so The socket the filter is attached to.
346 @param opt The socket option to set.
348 0 - The caller will continue with normal processing of the setsockopt.
349 Anything Else - The caller will stop processing and return this error.
351 typedef errno_t (*sf_setoption_func
)(void *cookie
, socket_t so
,
355 @typedef sf_getoption_func
357 @discussion sf_getoption_func is called before performing getsockopt
359 @param cookie Cookie value specified when the filter attach was
361 @param so The socket the filter is attached to.
362 @param opt The socket option to get.
364 0 - The caller will continue with normal processing of the getsockopt.
365 Anything Else - The caller will stop processing and return this error.
367 typedef errno_t (*sf_getoption_func
)(void *cookie
, socket_t so
,
371 @typedef sf_listen_func
373 @discussion sf_listen_func is called before performing listen
375 @param cookie Cookie value specified when the filter attach was
377 @param so The socket the filter is attached to.
379 0 - The caller will continue with normal processing of listen.
380 Anything Else - The caller will stop processing and return this error.
382 typedef errno_t (*sf_listen_func
)(void *cookie
, socket_t so
);
385 @typedef sf_ioctl_func
387 @discussion sf_ioctl_func is called before performing an ioctl
389 @param cookie Cookie value specified when the filter attach was
391 @param so The socket the filter is attached to.
392 @param request The ioctl name.
393 @param argp A pointer to the ioctl parameter.
395 0 - The caller will continue with normal processing of this ioctl.
396 Anything Else - The caller will stop processing and return this error.
398 typedef errno_t (*sf_ioctl_func
)(void *cookie
, socket_t so
,
399 u_int32_t request
, const char* argp
);
403 @discussion This structure is used to define a socket filter.
404 @field sf_handle A value used to find socket filters by
405 applications. An application can use this value to specify that
406 this filter should be attached when using the SO_NKE socket
408 @field sf_flags Indicate whether this filter should be attached to
409 all new sockets or just those that request the filter be
410 attached using the SO_NKE socket option.
411 @field sf_name A name used for debug purposes.
412 @field sf_unregistered Your function for being notified when your
413 filter has been unregistered.
414 @field sf_attach Your function for handling attaches to sockets.
415 @field sf_detach Your function for handling detaches from sockets.
416 @field sf_notify Your function for handling events. May be null.
417 @field sf_data_in Your function for handling incoming data. May be
419 @field sf_data_out Your function for handling outgoing data. May be
421 @field sf_connect_in Your function for handling inbound
422 connections. May be null.
423 @field sf_connect_in Your function for handling outbound
424 connections. May be null.
425 @field sf_bind Your function for handling binds. May be null.
426 @field sf_setoption Your function for handling setsockopt. May be null.
427 @field sf_getoption Your function for handling getsockopt. May be null.
428 @field sf_listen Your function for handling listen. May be null.
429 @field sf_ioctl Your function for handling ioctls. May be null.
432 sflt_handle sf_handle
;
436 sf_unregistered_func sf_unregistered
;
437 sf_attach_func sf_attach
;
438 sf_detach_func sf_detach
;
440 sf_notify_func sf_notify
;
441 sf_getpeername_func sf_getpeername
;
442 sf_getsockname_func sf_getsockname
;
443 sf_data_in_func sf_data_in
;
444 sf_data_out_func sf_data_out
;
445 sf_connect_in_func sf_connect_in
;
446 sf_connect_out_func sf_connect_out
;
447 sf_bind_func sf_bind
;
448 sf_setoption_func sf_setoption
;
449 sf_getoption_func sf_getoption
;
450 sf_listen_func sf_listen
;
451 sf_ioctl_func sf_ioctl
;
455 @function sflt_register
456 @discussion Registers a socket filter. See 'man 2 socket' for a
457 desciption of domain, type, and protocol.
458 @param filter A structure describing the filter.
459 @param domain The protocol domain these filters will be attached to.
460 @param type The socket type these filters will be attached to.
461 @param protocol The protocol these filters will be attached to.
462 @result 0 on success otherwise the errno error.
464 errno_t
sflt_register(const struct sflt_filter
*filter
, int domain
,
465 int type
, int protocol
);
468 @function sflt_unregister
469 @discussion Unregisters a socket filter. This will not detach the
470 socket filter from all sockets it may be attached to at the
471 time, it will just prevent the socket filter from being attached
473 @param handle The sf_handle of the socket filter to unregister.
474 @result 0 on success otherwise the errno error.
476 errno_t
sflt_unregister(sflt_handle handle
);
479 @function sflt_attach
480 @discussion Attaches a socket filter to the specified socket. A
481 filter must be registered before it can be attached.
482 @param socket The socket the filter should be attached to.
483 @param handle The handle of the registered filter to be attached.
484 @result 0 on success otherwise the errno error.
486 errno_t
sflt_attach(socket_t socket
, sflt_handle
);
489 @function sflt_detach
490 @discussion Detaches a socket filter from a specified socket.
491 @param socket The socket the filter should be detached from.
492 @param handle The handle of the registered filter to be detached.
493 @result 0 on success otherwise the errno error.
495 errno_t
sflt_detach(socket_t socket
, sflt_handle
);
497 /* Functions for manipulating sockets */
499 * Inject data in to the receive buffer of the socket as if it
500 * had come from the network.
502 * flags should match sflt_data_flag_t
506 @function sock_inject_data_in
507 @discussion Inject data in to the receive buffer of the socket as if
508 it had come from the network.
509 @param so The socket to inject the data on.
510 @param from The address the data is from, only necessary on
511 un-connected sockets. A copy of the address will be made, caller
512 is responsible for freeing the address after calling this
514 @param data The data and possibly control mbufs.
515 @param control The separate control mbufs.
516 @param flags Flags indicating the type of data.
517 @result 0 on success otherwise the errno error. If the function
518 returns an error, the caller is responsible for freeing the
521 errno_t
sock_inject_data_in(socket_t so
, const struct sockaddr
* from
,
522 mbuf_t data
, mbuf_t control
, sflt_data_flag_t flags
);
525 @function sock_inject_data_out
526 @discussion Inject data in to the send buffer of the socket as if it
527 had come from the client.
528 @param so The socket to inject the data on.
529 @param to The address the data should be sent to, only necessary on
530 un-connected sockets. The caller is responsible for freeing the
531 to address after sock_inject_data_out returns.
532 @param data The data and possibly control mbufs.
533 @param control The separate control mbufs.
534 @param flags Flags indicating the type of data.
535 @result 0 on success otherwise the errno error. The data and control
536 values are always freed regardless of return value.
538 errno_t
sock_inject_data_out(socket_t so
, const struct sockaddr
* to
,
539 mbuf_t data
, mbuf_t control
, sflt_data_flag_t flags
);
543 * sockopt_t accessors
550 typedef u_int8_t sockopt_dir
;
553 @function sockopt_direction
554 @discussion Retreives the direction of the socket option (Get or
556 @param sopt The socket option.
557 @result sock_opt_get or sock_opt_set.
559 sockopt_dir
sockopt_direction(sockopt_t sopt
);
562 @function sockopt_level
563 @discussion Retreives the socket option level. (SOL_SOCKET, etc).
564 @param sopt The socket option.
565 @result The socket option level. See man 2 setsockopt
567 int sockopt_level(sockopt_t sopt
);
570 @function sockopt_name
571 @discussion Retreives the socket option name. (SO_SNDBUF, etc).
572 @param sopt The socket option.
573 @result The socket option name. See man 2 setsockopt
575 int sockopt_name(sockopt_t sopt
);
578 @function sockopt_valsize
579 @discussion Retreives the size of the socket option data.
580 @param sopt The socket option.
581 @result The length, in bytes, of the data.
583 size_t sockopt_valsize(sockopt_t sopt
);
586 @function sockopt_copyin
587 @discussion Copies the data from the socket option to a buffer.
588 @param sopt The socket option.
589 @param data A pointer to the buffer to copy the data in to.
590 @param length The number of bytes to copy.
591 @result An errno error or zero upon success.
593 errno_t
sockopt_copyin(sockopt_t sopt
, void *data
, size_t length
);
596 @function sockopt_copyout
597 @discussion Copies the data from a buffer to a socket option.
598 @param sopt The socket option.
599 @param data A pointer to the buffer to copy the data out of.
600 @param length The number of bytes to copy.
601 @result An errno error or zero upon success.
603 errno_t
sockopt_copyout(sockopt_t sopt
, void *data
, size_t length
);