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