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