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