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