]>
Commit | Line | Data |
---|---|---|
91447636 | 1 | /* |
6d2010ae | 2 | * Copyright (c) 2008-2010 Apple Inc. All rights reserved. |
91447636 | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
91447636 | 5 | * |
2d21ac55 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. The rights granted to you under the License | |
10 | * may not be used to create, or enable the creation or redistribution of, | |
11 | * unlawful or unlicensed copies of an Apple operating system, or to | |
12 | * circumvent, violate, or enable the circumvention or violation of, any | |
13 | * terms of an Apple operating system software license agreement. | |
8f6c56a5 | 14 | * |
2d21ac55 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
17 | * | |
18 | * The Original Code and all software distributed under the License are | |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
23 | * Please see the License for the specific language governing rights and | |
24 | * limitations under the License. | |
8f6c56a5 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
91447636 A |
27 | */ |
28 | /*! | |
29 | @header kpi_ipfilter.h | |
30 | This header defines an API to attach IP filters. IP filters may be | |
31 | attached to intercept either IPv4 or IPv6 packets. The filters can | |
32 | intercept all IP packets in to and out of the host regardless of | |
33 | interface. | |
34 | */ | |
35 | ||
36 | #ifndef __KPI_IPFILTER__ | |
37 | #define __KPI_IPFILTER__ | |
38 | ||
39 | #include <sys/kernel_types.h> | |
40 | ||
41 | /* | |
42 | * ipf_pktopts | |
43 | * | |
44 | * Options for outgoing packets. The options need to be preserved when | |
45 | * re-injecting a packet. | |
46 | */ | |
47 | struct ipf_pktopts { | |
48 | u_int32_t ippo_flags; | |
49 | ifnet_t ippo_mcast_ifnet; | |
b0d623f7 | 50 | int ippo_mcast_loop; |
91447636 A |
51 | u_int8_t ippo_mcast_ttl; |
52 | }; | |
6d2010ae A |
53 | #define IPPOF_MCAST_OPTS 0x1 |
54 | #ifdef PRIVATE | |
55 | #define IPPOF_BOUND_IF 0x2 | |
56 | #define IPPOF_NO_IFT_CELLULAR 0x4 | |
57 | #define IPPOF_SHIFT_IFSCOPE 16 | |
58 | #endif /* PRIVATE */ | |
91447636 | 59 | |
b0d623f7 | 60 | typedef struct ipf_pktopts *ipf_pktopts_t; |
91447636 | 61 | |
2d21ac55 A |
62 | __BEGIN_DECLS |
63 | ||
91447636 A |
64 | /*! |
65 | @typedef ipf_input_func | |
b0d623f7 | 66 | |
91447636 A |
67 | @discussion ipf_input_func is used to filter incoming ip packets. |
68 | The IP filter is called for packets from all interfaces. The | |
69 | filter is called between when the general IP processing is | |
70 | handled and when the packet is passed up to the next layer | |
71 | protocol such as udp or tcp. In the case of encapsulation, such | |
72 | as UDP in ESP (IPSec), your filter will be called once for ESP | |
73 | and then again for UDP. This will give your filter an | |
74 | opportunity to process the ESP header as well as the decrypted | |
75 | packet. Offset and protocol are used to determine where in the | |
76 | packet processing is currently occuring. If you're only | |
77 | interested in TCP or UDP packets, just return 0 if protocol | |
78 | doesn't match TCP or UDP. | |
79 | @param cookie The cookie specified when your filter was attached. | |
80 | @param data The reassembled ip packet, data will start at the ip | |
81 | header. | |
82 | @param offset An offset to the next header | |
83 | (udp/tcp/icmp/esp/etc...). | |
84 | @param protocol The protocol type (udp/tcp/icmp/etc...) of the IP packet | |
85 | @result Return: | |
b0d623f7 A |
86 | 0 - The caller will continue with normal processing of the |
87 | packet. | |
88 | EJUSTRETURN - The caller will stop processing the packet, | |
89 | the packet will not be freed. | |
90 | Anything Else - The caller will free the packet and stop | |
91 | processing. | |
91447636 | 92 | */ |
b0d623f7 A |
93 | typedef errno_t (*ipf_input_func)(void *cookie, mbuf_t *data, int offset, |
94 | u_int8_t protocol); | |
91447636 A |
95 | |
96 | /*! | |
97 | @typedef ipf_output_func | |
b0d623f7 | 98 | |
91447636 A |
99 | @discussion ipf_output_func is used to filter outbound ip packets. |
100 | The IP filter is called for packets to all interfaces. The | |
101 | filter is called before fragmentation and IPSec processing. If | |
102 | you need to change the destination IP address, call | |
103 | ipf_inject_output and return EJUSTRETURN. | |
104 | @param cookie The cookie specified when your filter was attached. | |
105 | @param data The ip packet, will contain an IP header followed by the | |
106 | rest of the IP packet. | |
107 | @result Return: | |
b0d623f7 A |
108 | 0 - The caller will continue with normal processing of the |
109 | packet. | |
110 | EJUSTRETURN - The caller will stop processing the packet, | |
111 | the packet will not be freed. | |
112 | Anything Else - The caller will free the packet and stop | |
113 | processing. | |
91447636 | 114 | */ |
b0d623f7 A |
115 | typedef errno_t (*ipf_output_func)(void *cookie, mbuf_t *data, |
116 | ipf_pktopts_t options); | |
91447636 A |
117 | |
118 | /*! | |
119 | @typedef ipf_detach_func | |
b0d623f7 | 120 | |
91447636 A |
121 | @discussion ipf_detach_func is called to notify your filter that it |
122 | has been detached. | |
123 | @param cookie The cookie specified when your filter was attached. | |
124 | */ | |
b0d623f7 | 125 | typedef void (*ipf_detach_func)(void *cookie); |
91447636 A |
126 | |
127 | /*! | |
128 | @typedef ipf_filter | |
129 | @discussion This structure is used to define an IP filter for | |
130 | use with the ipf_addv4 or ipf_addv6 function. | |
131 | @field cookie A kext defined cookie that will be passed to all | |
132 | filter functions. | |
133 | @field name A filter name used for debugging purposes. | |
134 | @field ipf_input The filter function to handle inbound packets. | |
135 | @field ipf_output The filter function to handle outbound packets. | |
136 | @field ipf_detach The filter function to notify of a detach. | |
137 | */ | |
138 | struct ipf_filter { | |
b0d623f7 A |
139 | void *cookie; |
140 | const char *name; | |
91447636 A |
141 | ipf_input_func ipf_input; |
142 | ipf_output_func ipf_output; | |
143 | ipf_detach_func ipf_detach; | |
144 | }; | |
145 | ||
146 | struct opaque_ipfilter; | |
b0d623f7 | 147 | typedef struct opaque_ipfilter *ipfilter_t; |
91447636 A |
148 | |
149 | /*! | |
150 | @function ipf_addv4 | |
151 | @discussion Attaches an IPv4 ip filter. | |
152 | @param filter A structure defining the filter. | |
153 | @param filter_ref A reference to the filter used to detach it. | |
154 | @result 0 on success otherwise the errno error. | |
155 | */ | |
b0d623f7 A |
156 | extern errno_t ipf_addv4(const struct ipf_filter *filter, |
157 | ipfilter_t *filter_ref); | |
91447636 A |
158 | |
159 | /*! | |
160 | @function ipf_addv6 | |
161 | @discussion Attaches an IPv6 ip filter. | |
162 | @param filter A structure defining the filter. | |
163 | @param filter_ref A reference to the filter used to detach it. | |
164 | @result 0 on success otherwise the errno error. | |
165 | */ | |
b0d623f7 A |
166 | extern errno_t ipf_addv6(const struct ipf_filter *filter, |
167 | ipfilter_t *filter_ref); | |
91447636 A |
168 | |
169 | /*! | |
170 | @function ipf_remove | |
171 | @discussion Detaches an IPv4 or IPv6 filter. | |
172 | @param filter_ref The reference to the filter returned from ipf_addv4 or | |
173 | ipf_addv6. | |
174 | @result 0 on success otherwise the errno error. | |
175 | */ | |
b0d623f7 | 176 | extern errno_t ipf_remove(ipfilter_t filter_ref); |
91447636 A |
177 | |
178 | /*! | |
179 | @function ipf_inject_input | |
180 | @discussion Inject an IP packet as though it had just been | |
181 | reassembled in ip_input. When re-injecting a packet intercepted | |
182 | by the filter's ipf_input function, an IP filter can pass its | |
183 | reference to avoid processing the packet twice. This also | |
184 | prevents ip filters installed before this filter from | |
185 | getting a chance to process the packet. If the filter modified | |
186 | the packet, it should not specify the filter ref to give other | |
187 | filters a chance to process the new packet. | |
b0d623f7 | 188 | |
91447636 A |
189 | Caller is responsible for freeing mbuf chain in the event that |
190 | ipf_inject_input returns an error. | |
191 | @param data The complete IPv4 or IPv6 packet, receive interface must | |
192 | be set. | |
193 | @param filter_ref The reference to the filter injecting the data | |
194 | @result 0 on success otherwise the errno error. | |
195 | */ | |
b0d623f7 | 196 | extern errno_t ipf_inject_input(mbuf_t data, ipfilter_t filter_ref); |
91447636 A |
197 | |
198 | /*! | |
199 | @function ipf_inject_output | |
200 | @discussion Inject an IP packet as though it had just been sent to | |
201 | ip_output. When re-injecting a packet intercepted by the | |
202 | filter's ipf_output function, an IP filter can pass its | |
203 | reference to avoid processing the packet twice. This also | |
204 | prevents ip filters installed before this filter from getting a | |
205 | chance to process the packet. If the filter modified the packet, | |
206 | it should not specify the filter ref to give other filters a | |
207 | chance to process the new packet. | |
208 | @param data The complete IPv4 or IPv6 packet. | |
209 | @param filter_ref The reference to the filter injecting the data | |
210 | @param options Output options for the packet | |
211 | @result 0 on success otherwise the errno error. ipf_inject_output | |
212 | will always free the mbuf. | |
213 | */ | |
b0d623f7 A |
214 | extern errno_t ipf_inject_output(mbuf_t data, ipfilter_t filter_ref, |
215 | ipf_pktopts_t options); | |
91447636 | 216 | |
2d21ac55 | 217 | __END_DECLS |
91447636 | 218 | #endif /* __KPI_IPFILTER__ */ |