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