xref: /xnu-8792.81.2/bsd/net/kpi_interfacefilter.h (revision 19c3b8c28c31cb8130e034cfb5df6bf9ba342d90)
1*19c3b8c2SApple OSS Distributions /*
2*19c3b8c2SApple OSS Distributions  * Copyright (c) 2003,2008,2017 Apple Computer, Inc. All rights reserved.
3*19c3b8c2SApple OSS Distributions  *
4*19c3b8c2SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*19c3b8c2SApple OSS Distributions  *
6*19c3b8c2SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*19c3b8c2SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*19c3b8c2SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*19c3b8c2SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*19c3b8c2SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*19c3b8c2SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*19c3b8c2SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*19c3b8c2SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*19c3b8c2SApple OSS Distributions  *
15*19c3b8c2SApple OSS Distributions  * Please obtain a copy of the License at
16*19c3b8c2SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*19c3b8c2SApple OSS Distributions  *
18*19c3b8c2SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*19c3b8c2SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*19c3b8c2SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*19c3b8c2SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*19c3b8c2SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*19c3b8c2SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*19c3b8c2SApple OSS Distributions  * limitations under the License.
25*19c3b8c2SApple OSS Distributions  *
26*19c3b8c2SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*19c3b8c2SApple OSS Distributions  */
28*19c3b8c2SApple OSS Distributions /*!
29*19c3b8c2SApple OSS Distributions  *       @header kpi_interfacefilter.h
30*19c3b8c2SApple OSS Distributions  *       This header defines an API to attach interface filters. Interface
31*19c3b8c2SApple OSS Distributions  *       filters may be attached to a specific interface. The filters can
32*19c3b8c2SApple OSS Distributions  *       intercept all packets in to and out of the specific interface. In
33*19c3b8c2SApple OSS Distributions  *       addition, the filters may intercept interface specific events and
34*19c3b8c2SApple OSS Distributions  *       ioctls.
35*19c3b8c2SApple OSS Distributions  */
36*19c3b8c2SApple OSS Distributions 
37*19c3b8c2SApple OSS Distributions #ifndef __KPI_INTERFACEFILTER__
38*19c3b8c2SApple OSS Distributions #define __KPI_INTERFACEFILTER__
39*19c3b8c2SApple OSS Distributions #include <sys/kernel_types.h>
40*19c3b8c2SApple OSS Distributions #include <net/kpi_interface.h>
41*19c3b8c2SApple OSS Distributions 
42*19c3b8c2SApple OSS Distributions #ifndef PRIVATE
43*19c3b8c2SApple OSS Distributions #include <Availability.h>
44*19c3b8c2SApple OSS Distributions #define __NKE_API_DEPRECATED __API_DEPRECATED("Network Kernel Extension KPI is deprecated", macos(10.4, 10.15.4))
45*19c3b8c2SApple OSS Distributions #else
46*19c3b8c2SApple OSS Distributions #define __NKE_API_DEPRECATED
47*19c3b8c2SApple OSS Distributions #endif /* PRIVATE */
48*19c3b8c2SApple OSS Distributions 
49*19c3b8c2SApple OSS Distributions struct kev_msg;
50*19c3b8c2SApple OSS Distributions 
51*19c3b8c2SApple OSS Distributions __BEGIN_DECLS
52*19c3b8c2SApple OSS Distributions 
53*19c3b8c2SApple OSS Distributions /*!
54*19c3b8c2SApple OSS Distributions  *       @typedef iff_input_func
55*19c3b8c2SApple OSS Distributions  *
56*19c3b8c2SApple OSS Distributions  *       @discussion iff_input_func is used to filter incoming packets. The
57*19c3b8c2SApple OSS Distributions  *               interface is only valid for the duration of the filter call. If
58*19c3b8c2SApple OSS Distributions  *               you need to keep a reference to the interface, be sure to call
59*19c3b8c2SApple OSS Distributions  *               ifnet_reference and ifnet_release. The packets passed to the
60*19c3b8c2SApple OSS Distributions  *               inbound filter are different from those passed to the outbound
61*19c3b8c2SApple OSS Distributions  *               filter. Packets to the inbound filter have the frame header
62*19c3b8c2SApple OSS Distributions  *               passed in separately from the rest of the packet. The outbound
63*19c3b8c2SApple OSS Distributions  *               data filters is passed the whole packet including the frame
64*19c3b8c2SApple OSS Distributions  *               header.
65*19c3b8c2SApple OSS Distributions  *
66*19c3b8c2SApple OSS Distributions  *               The frame header usually preceeds the data in the mbuf. This
67*19c3b8c2SApple OSS Distributions  *               ensures that the frame header will be a valid pointer as long as
68*19c3b8c2SApple OSS Distributions  *               the mbuf is not freed. If you need to change the frame header to
69*19c3b8c2SApple OSS Distributions  *               point somewhere else, the recommended method is to prepend a new
70*19c3b8c2SApple OSS Distributions  *               frame header to the mbuf chain (mbuf_prepend), set the header to
71*19c3b8c2SApple OSS Distributions  *               point to that data, then call mbuf_adj to move the mbuf data
72*19c3b8c2SApple OSS Distributions  *               pointer back to the start of the packet payload.
73*19c3b8c2SApple OSS Distributions  *       @param cookie The cookie specified when this filter was attached.
74*19c3b8c2SApple OSS Distributions  *       @param interface The interface the packet was recieved on.
75*19c3b8c2SApple OSS Distributions  *       @param protocol The protocol of this packet. If you specified a
76*19c3b8c2SApple OSS Distributions  *               protocol when attaching your filter, the protocol will only ever
77*19c3b8c2SApple OSS Distributions  *               be the protocol you specified.
78*19c3b8c2SApple OSS Distributions  *       @param data The inbound packet, after the frame header as determined
79*19c3b8c2SApple OSS Distributions  *               by the interface.
80*19c3b8c2SApple OSS Distributions  *       @param frame_ptr A pointer to the pointer to the frame header. The
81*19c3b8c2SApple OSS Distributions  *               frame header length can be found by inspecting the interface's
82*19c3b8c2SApple OSS Distributions  *               frame header length (ifnet_hdrlen).
83*19c3b8c2SApple OSS Distributions  *       @result Return:
84*19c3b8c2SApple OSS Distributions  *               0 - The caller will continue with normal processing of the
85*19c3b8c2SApple OSS Distributions  *                       packet.
86*19c3b8c2SApple OSS Distributions  *               EJUSTRETURN - The caller will stop processing the packet,
87*19c3b8c2SApple OSS Distributions  *                       the packet will not be freed.
88*19c3b8c2SApple OSS Distributions  *               Anything Else - The caller will free the packet and stop
89*19c3b8c2SApple OSS Distributions  *                       processing.
90*19c3b8c2SApple OSS Distributions  */
91*19c3b8c2SApple OSS Distributions typedef errno_t (*iff_input_func)(void *cookie, ifnet_t interface,
92*19c3b8c2SApple OSS Distributions     protocol_family_t protocol, mbuf_t *data, char **frame_ptr);
93*19c3b8c2SApple OSS Distributions 
94*19c3b8c2SApple OSS Distributions /*!
95*19c3b8c2SApple OSS Distributions  *       @typedef iff_output_func
96*19c3b8c2SApple OSS Distributions  *
97*19c3b8c2SApple OSS Distributions  *       @discussion iff_output_func is used to filter fully formed outbound
98*19c3b8c2SApple OSS Distributions  *               packets. The interface is only valid for the duration of the
99*19c3b8c2SApple OSS Distributions  *               filter call. If you need to keep a reference to the interface,
100*19c3b8c2SApple OSS Distributions  *               be sure to call ifnet_reference and ifnet_release.
101*19c3b8c2SApple OSS Distributions  *       @param cookie The cookie specified when this filter was attached.
102*19c3b8c2SApple OSS Distributions  *       @param interface The interface the packet is being transmitted on.
103*19c3b8c2SApple OSS Distributions  *       @param data The fully formed outbound packet in a chain of mbufs.
104*19c3b8c2SApple OSS Distributions  *               The frame header is already included. The filter function may
105*19c3b8c2SApple OSS Distributions  *               modify the packet or return a different mbuf chain.
106*19c3b8c2SApple OSS Distributions  *       @result Return:
107*19c3b8c2SApple OSS Distributions  *               0 - The caller will continue with normal processing of the
108*19c3b8c2SApple OSS Distributions  *                       packet.
109*19c3b8c2SApple OSS Distributions  *               EJUSTRETURN - The caller will stop processing the packet,
110*19c3b8c2SApple OSS Distributions  *                       the packet will not be freed.
111*19c3b8c2SApple OSS Distributions  *               Anything Else - The caller will free the packet and stop
112*19c3b8c2SApple OSS Distributions  *                       processing.
113*19c3b8c2SApple OSS Distributions  */
114*19c3b8c2SApple OSS Distributions typedef errno_t (*iff_output_func)(void *cookie, ifnet_t interface,
115*19c3b8c2SApple OSS Distributions     protocol_family_t protocol, mbuf_t *data);
116*19c3b8c2SApple OSS Distributions 
117*19c3b8c2SApple OSS Distributions /*!
118*19c3b8c2SApple OSS Distributions  *       @typedef iff_event_func
119*19c3b8c2SApple OSS Distributions  *
120*19c3b8c2SApple OSS Distributions  *       @discussion iff_event_func is used to filter interface specific
121*19c3b8c2SApple OSS Distributions  *               events. The interface is only valid for the duration of the
122*19c3b8c2SApple OSS Distributions  *               filter call. If you need to keep a reference to the interface,
123*19c3b8c2SApple OSS Distributions  *               be sure to call ifnet_reference and ifnet_release.
124*19c3b8c2SApple OSS Distributions  *       @param cookie The cookie specified when this filter was attached.
125*19c3b8c2SApple OSS Distributions  *       @param interface The interface the packet is being transmitted on.
126*19c3b8c2SApple OSS Distributions  *       @param event_msg The kernel event, may not be changed.
127*19c3b8c2SApple OSS Distributions  */
128*19c3b8c2SApple OSS Distributions typedef void (*iff_event_func)(void *cookie, ifnet_t interface,
129*19c3b8c2SApple OSS Distributions     protocol_family_t protocol, const struct kev_msg *event_msg);
130*19c3b8c2SApple OSS Distributions 
131*19c3b8c2SApple OSS Distributions /*!
132*19c3b8c2SApple OSS Distributions  *       @typedef iff_ioctl_func
133*19c3b8c2SApple OSS Distributions  *
134*19c3b8c2SApple OSS Distributions  *       @discussion iff_ioctl_func is used to filter ioctls sent to an
135*19c3b8c2SApple OSS Distributions  *               interface. The interface is only valid for the duration of the
136*19c3b8c2SApple OSS Distributions  *               filter call. If you need to keep a reference to the interface,
137*19c3b8c2SApple OSS Distributions  *               be sure to call ifnet_reference and ifnet_release.
138*19c3b8c2SApple OSS Distributions  *
139*19c3b8c2SApple OSS Distributions  *               All undefined ioctls are reserved for future use by Apple. If
140*19c3b8c2SApple OSS Distributions  *               you need to communicate with your kext using an ioctl, please
141*19c3b8c2SApple OSS Distributions  *               use SIOCSIFKPI and SIOCGIFKPI.
142*19c3b8c2SApple OSS Distributions  *       @param cookie The cookie specified when this filter was attached.
143*19c3b8c2SApple OSS Distributions  *       @param interface The interface the packet is being transmitted on.
144*19c3b8c2SApple OSS Distributions  *       @param ioctl_cmd The ioctl command.
145*19c3b8c2SApple OSS Distributions  *       @param ioctl_arg A pointer to the ioctl argument.
146*19c3b8c2SApple OSS Distributions  *       @result Return:
147*19c3b8c2SApple OSS Distributions  *               0 - This filter function handled the ioctl.
148*19c3b8c2SApple OSS Distributions  *               EOPNOTSUPP - This filter function does not understand/did not
149*19c3b8c2SApple OSS Distributions  *                       handle this ioctl.
150*19c3b8c2SApple OSS Distributions  *               EJUSTRETURN - This filter function handled the ioctl,
151*19c3b8c2SApple OSS Distributions  *                       processing should stop.
152*19c3b8c2SApple OSS Distributions  *               Anything Else - Processing will stop, the error will be
153*19c3b8c2SApple OSS Distributions  *                       returned.
154*19c3b8c2SApple OSS Distributions  */
155*19c3b8c2SApple OSS Distributions typedef errno_t (*iff_ioctl_func)(void *cookie, ifnet_t interface,
156*19c3b8c2SApple OSS Distributions     protocol_family_t protocol, unsigned long ioctl_cmd, void *ioctl_arg);
157*19c3b8c2SApple OSS Distributions 
158*19c3b8c2SApple OSS Distributions /*!
159*19c3b8c2SApple OSS Distributions  *       @typedef iff_detached_func
160*19c3b8c2SApple OSS Distributions  *
161*19c3b8c2SApple OSS Distributions  *       @discussion iff_detached_func is called to notify the filter that it
162*19c3b8c2SApple OSS Distributions  *               has been detached from an interface. This is the last call to
163*19c3b8c2SApple OSS Distributions  *               the filter that will be made. A filter may be detached if the
164*19c3b8c2SApple OSS Distributions  *               interface is detached or the detach filter function is called.
165*19c3b8c2SApple OSS Distributions  *               In the case that the interface is being detached, your filter's
166*19c3b8c2SApple OSS Distributions  *               event function will be called with the interface detaching event
167*19c3b8c2SApple OSS Distributions  *               before the your detached function will be called.
168*19c3b8c2SApple OSS Distributions  *       @param cookie The cookie specified when this filter was attached.
169*19c3b8c2SApple OSS Distributions  *       @param interface The interface this filter was detached from.
170*19c3b8c2SApple OSS Distributions  */
171*19c3b8c2SApple OSS Distributions typedef void (*iff_detached_func)(void *cookie, ifnet_t interface);
172*19c3b8c2SApple OSS Distributions 
173*19c3b8c2SApple OSS Distributions /*!
174*19c3b8c2SApple OSS Distributions  *       @struct iff_filter
175*19c3b8c2SApple OSS Distributions  *       @discussion This structure is used to define an interface filter for
176*19c3b8c2SApple OSS Distributions  *               use with the iflt_attach function.
177*19c3b8c2SApple OSS Distributions  *       @field iff_cookie A kext defined cookie that will be passed to all
178*19c3b8c2SApple OSS Distributions  *               filter functions.
179*19c3b8c2SApple OSS Distributions  *       @field iff_name A filter name used for debugging purposes.
180*19c3b8c2SApple OSS Distributions  *       @field iff_protocol The protocol of the packets this filter is
181*19c3b8c2SApple OSS Distributions  *               interested in. If you specify zero, packets from all protocols
182*19c3b8c2SApple OSS Distributions  *               will be passed to the filter.
183*19c3b8c2SApple OSS Distributions  *       @field iff_input The filter function to handle inbound packets, may
184*19c3b8c2SApple OSS Distributions  *               be NULL.
185*19c3b8c2SApple OSS Distributions  *       @field iff_output The filter function to handle outbound packets,
186*19c3b8c2SApple OSS Distributions  *               may be NULL.
187*19c3b8c2SApple OSS Distributions  *       @field iff_event The filter function to handle interface events, may
188*19c3b8c2SApple OSS Distributions  *               be null.
189*19c3b8c2SApple OSS Distributions  *       @field iff_ioctl The filter function to handle interface ioctls, may
190*19c3b8c2SApple OSS Distributions  *               be null.
191*19c3b8c2SApple OSS Distributions  *       @field iff_detached The filter function used to notify the filter that
192*19c3b8c2SApple OSS Distributions  *               it has been detached.
193*19c3b8c2SApple OSS Distributions  */
194*19c3b8c2SApple OSS Distributions 
195*19c3b8c2SApple OSS Distributions struct iff_filter {
196*19c3b8c2SApple OSS Distributions 	void                    *iff_cookie;
197*19c3b8c2SApple OSS Distributions 	const char              *iff_name;
198*19c3b8c2SApple OSS Distributions 	protocol_family_t       iff_protocol;
199*19c3b8c2SApple OSS Distributions 	iff_input_func          iff_input;
200*19c3b8c2SApple OSS Distributions 	iff_output_func         iff_output;
201*19c3b8c2SApple OSS Distributions 	iff_event_func          iff_event;
202*19c3b8c2SApple OSS Distributions 	iff_ioctl_func          iff_ioctl;
203*19c3b8c2SApple OSS Distributions 	iff_detached_func       iff_detached;
204*19c3b8c2SApple OSS Distributions };
205*19c3b8c2SApple OSS Distributions 
206*19c3b8c2SApple OSS Distributions /*!
207*19c3b8c2SApple OSS Distributions  *       @function iflt_attach
208*19c3b8c2SApple OSS Distributions  *       @discussion Attaches an interface filter to an interface.
209*19c3b8c2SApple OSS Distributions  *       @param interface The interface the filter should be attached to.
210*19c3b8c2SApple OSS Distributions  *       @param filter A structure defining the filter.
211*19c3b8c2SApple OSS Distributions  *       @param filter_ref A reference to the filter used to detach.
212*19c3b8c2SApple OSS Distributions  *       @result 0 on success otherwise the errno error.
213*19c3b8c2SApple OSS Distributions  */
214*19c3b8c2SApple OSS Distributions #ifdef KERNEL_PRIVATE
215*19c3b8c2SApple OSS Distributions extern errno_t iflt_attach_internal(ifnet_t interface, const struct iff_filter *filter,
216*19c3b8c2SApple OSS Distributions     interface_filter_t *filter_ref);
217*19c3b8c2SApple OSS Distributions 
218*19c3b8c2SApple OSS Distributions #define iflt_attach(interface, filter, filter_ref) \
219*19c3b8c2SApple OSS Distributions 	iflt_attach_internal((interface), (filter), (filter_ref))
220*19c3b8c2SApple OSS Distributions #else
221*19c3b8c2SApple OSS Distributions extern errno_t iflt_attach(ifnet_t interface, const struct iff_filter *filter,
222*19c3b8c2SApple OSS Distributions     interface_filter_t *filter_ref)
223*19c3b8c2SApple OSS Distributions __NKE_API_DEPRECATED;
224*19c3b8c2SApple OSS Distributions #endif /* KERNEL_PRIVATE */
225*19c3b8c2SApple OSS Distributions 
226*19c3b8c2SApple OSS Distributions /*!
227*19c3b8c2SApple OSS Distributions  *       @function iflt_detach
228*19c3b8c2SApple OSS Distributions  *       @discussion Detaches an interface filter from an interface.
229*19c3b8c2SApple OSS Distributions  *       @param filter_ref The reference to the filter from iflt_attach.
230*19c3b8c2SApple OSS Distributions  */
231*19c3b8c2SApple OSS Distributions extern void iflt_detach(interface_filter_t filter_ref)
232*19c3b8c2SApple OSS Distributions __NKE_API_DEPRECATED;
233*19c3b8c2SApple OSS Distributions 
234*19c3b8c2SApple OSS Distributions __END_DECLS
235*19c3b8c2SApple OSS Distributions #undef __NKE_API_DEPRECATED
236*19c3b8c2SApple OSS Distributions #endif /* __KPI_INTERFACEFILTER__ */
237