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