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