]> git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/kpi_ipfilter.h
xnu-792.13.8.tar.gz
[apple/xnu.git] / bsd / netinet / kpi_ipfilter.h
1 /*
2 * Copyright (c) 2003-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
5 *
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
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
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.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30 /*!
31 @header kpi_ipfilter.h
32 This header defines an API to attach IP filters. IP filters may be
33 attached to intercept either IPv4 or IPv6 packets. The filters can
34 intercept all IP packets in to and out of the host regardless of
35 interface.
36 */
37
38 #ifndef __KPI_IPFILTER__
39 #define __KPI_IPFILTER__
40
41 #include <sys/kernel_types.h>
42
43 /*
44 * ipf_pktopts
45 *
46 * Options for outgoing packets. The options need to be preserved when
47 * re-injecting a packet.
48 */
49 struct ipf_pktopts {
50 u_int32_t ippo_flags;
51 ifnet_t ippo_mcast_ifnet;
52 int ippo_mcast_loop;
53 u_int8_t ippo_mcast_ttl;
54 };
55 #define IPPOF_MCAST_OPTS 0x1
56
57 typedef struct ipf_pktopts* ipf_pktopts_t;
58
59 /*!
60 @typedef ipf_input_func
61
62 @discussion ipf_input_func is used to filter incoming ip packets.
63 The IP filter is called for packets from all interfaces. The
64 filter is called between when the general IP processing is
65 handled and when the packet is passed up to the next layer
66 protocol such as udp or tcp. In the case of encapsulation, such
67 as UDP in ESP (IPSec), your filter will be called once for ESP
68 and then again for UDP. This will give your filter an
69 opportunity to process the ESP header as well as the decrypted
70 packet. Offset and protocol are used to determine where in the
71 packet processing is currently occuring. If you're only
72 interested in TCP or UDP packets, just return 0 if protocol
73 doesn't match TCP or UDP.
74 @param cookie The cookie specified when your filter was attached.
75 @param data The reassembled ip packet, data will start at the ip
76 header.
77 @param offset An offset to the next header
78 (udp/tcp/icmp/esp/etc...).
79 @param protocol The protocol type (udp/tcp/icmp/etc...) of the IP packet
80 @result Return:
81 0 - The caller will continue with normal processing of the packet.
82 EJUSTRETURN - The caller will stop processing the packet, the packet will not be freed.
83 Anything Else - The caller will free the packet and stop processing.
84 */
85 typedef errno_t (*ipf_input_func)(void* cookie, mbuf_t *data, int offset, u_int8_t protocol);
86
87 /*!
88 @typedef ipf_output_func
89
90 @discussion ipf_output_func is used to filter outbound ip packets.
91 The IP filter is called for packets to all interfaces. The
92 filter is called before fragmentation and IPSec processing. If
93 you need to change the destination IP address, call
94 ipf_inject_output and return EJUSTRETURN.
95 @param cookie The cookie specified when your filter was attached.
96 @param data The ip packet, will contain an IP header followed by the
97 rest of the IP packet.
98 @result Return:
99 0 - The caller will continue with normal processing of the packet.
100 EJUSTRETURN - The caller will stop processing the packet, the packet will not be freed.
101 Anything Else - The caller will free the packet and stop processing.
102 */
103 typedef errno_t (*ipf_output_func)(void* cookie, mbuf_t *data, ipf_pktopts_t options);
104
105 /*!
106 @typedef ipf_detach_func
107
108 @discussion ipf_detach_func is called to notify your filter that it
109 has been detached.
110 @param cookie The cookie specified when your filter was attached.
111 */
112 typedef void (*ipf_detach_func)(void* cookie);
113
114 /*!
115 @typedef ipf_filter
116 @discussion This structure is used to define an IP filter for
117 use with the ipf_addv4 or ipf_addv6 function.
118 @field cookie A kext defined cookie that will be passed to all
119 filter functions.
120 @field name A filter name used for debugging purposes.
121 @field ipf_input The filter function to handle inbound packets.
122 @field ipf_output The filter function to handle outbound packets.
123 @field ipf_detach The filter function to notify of a detach.
124 */
125 struct ipf_filter {
126 void* cookie;
127 const char* name;
128 ipf_input_func ipf_input;
129 ipf_output_func ipf_output;
130 ipf_detach_func ipf_detach;
131 };
132
133 struct opaque_ipfilter;
134 typedef struct opaque_ipfilter* ipfilter_t;
135
136 /*!
137 @function ipf_addv4
138 @discussion Attaches an IPv4 ip filter.
139 @param filter A structure defining the filter.
140 @param filter_ref A reference to the filter used to detach it.
141 @result 0 on success otherwise the errno error.
142 */
143 errno_t ipf_addv4(const struct ipf_filter* filter, ipfilter_t *filter_ref);
144
145 /*!
146 @function ipf_addv6
147 @discussion Attaches an IPv6 ip filter.
148 @param filter A structure defining the filter.
149 @param filter_ref A reference to the filter used to detach it.
150 @result 0 on success otherwise the errno error.
151 */
152 errno_t ipf_addv6(const struct ipf_filter* filter, ipfilter_t *filter_ref);
153
154 /*!
155 @function ipf_remove
156 @discussion Detaches an IPv4 or IPv6 filter.
157 @param filter_ref The reference to the filter returned from ipf_addv4 or
158 ipf_addv6.
159 @result 0 on success otherwise the errno error.
160 */
161 errno_t ipf_remove(ipfilter_t filter_ref);
162
163 /*!
164 @function ipf_inject_input
165 @discussion Inject an IP packet as though it had just been
166 reassembled in ip_input. When re-injecting a packet intercepted
167 by the filter's ipf_input function, an IP filter can pass its
168 reference to avoid processing the packet twice. This also
169 prevents ip filters installed before this filter from
170 getting a chance to process the packet. If the filter modified
171 the packet, it should not specify the filter ref to give other
172 filters a chance to process the new packet.
173
174 Caller is responsible for freeing mbuf chain in the event that
175 ipf_inject_input returns an error.
176 @param data The complete IPv4 or IPv6 packet, receive interface must
177 be set.
178 @param filter_ref The reference to the filter injecting the data
179 @result 0 on success otherwise the errno error.
180 */
181 errno_t ipf_inject_input(mbuf_t data, ipfilter_t filter_ref);
182
183 /*!
184 @function ipf_inject_output
185 @discussion Inject an IP packet as though it had just been sent to
186 ip_output. When re-injecting a packet intercepted by the
187 filter's ipf_output function, an IP filter can pass its
188 reference to avoid processing the packet twice. This also
189 prevents ip filters installed before this filter from getting a
190 chance to process the packet. If the filter modified the packet,
191 it should not specify the filter ref to give other filters a
192 chance to process the new packet.
193 @param data The complete IPv4 or IPv6 packet.
194 @param filter_ref The reference to the filter injecting the data
195 @param options Output options for the packet
196 @result 0 on success otherwise the errno error. ipf_inject_output
197 will always free the mbuf.
198 */
199 errno_t ipf_inject_output(mbuf_t data, ipfilter_t filter_ref, ipf_pktopts_t options);
200
201 #endif /* __KPI_IPFILTER__ */