1*8d741a5dSApple OSS Distributions /* 2*8d741a5dSApple OSS Distributions * Copyright (c) 2008-2021 Apple Computer, Inc. All rights reserved. 3*8d741a5dSApple OSS Distributions * 4*8d741a5dSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ 5*8d741a5dSApple OSS Distributions * 6*8d741a5dSApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code 7*8d741a5dSApple OSS Distributions * as defined in and that are subject to the Apple Public Source License 8*8d741a5dSApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in 9*8d741a5dSApple OSS Distributions * compliance with the License. The rights granted to you under the License 10*8d741a5dSApple OSS Distributions * may not be used to create, or enable the creation or redistribution of, 11*8d741a5dSApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to 12*8d741a5dSApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any 13*8d741a5dSApple OSS Distributions * terms of an Apple operating system software license agreement. 14*8d741a5dSApple OSS Distributions * 15*8d741a5dSApple OSS Distributions * Please obtain a copy of the License at 16*8d741a5dSApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file. 17*8d741a5dSApple OSS Distributions * 18*8d741a5dSApple OSS Distributions * The Original Code and all software distributed under the License are 19*8d741a5dSApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 20*8d741a5dSApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 21*8d741a5dSApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 22*8d741a5dSApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 23*8d741a5dSApple OSS Distributions * Please see the License for the specific language governing rights and 24*8d741a5dSApple OSS Distributions * limitations under the License. 25*8d741a5dSApple OSS Distributions * 26*8d741a5dSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ 27*8d741a5dSApple OSS Distributions */ 28*8d741a5dSApple OSS Distributions /*! 29*8d741a5dSApple OSS Distributions * @header kpi_socketfilter.h 30*8d741a5dSApple OSS Distributions * This header defines an API for intercepting communications at the 31*8d741a5dSApple OSS Distributions * socket layer. 32*8d741a5dSApple OSS Distributions * 33*8d741a5dSApple OSS Distributions * For the most part, socket filters want to do three things: Filter 34*8d741a5dSApple OSS Distributions * data in and out, watch for state changes, and intercept a few calls 35*8d741a5dSApple OSS Distributions * for security. The number of function pointers supplied by a socket 36*8d741a5dSApple OSS Distributions * filter has been significantly reduced. The filter no longer has any 37*8d741a5dSApple OSS Distributions * knowledge of socket buffers. The filter no longer intercepts nearly 38*8d741a5dSApple OSS Distributions * every internal socket call. There are two data filters, an in 39*8d741a5dSApple OSS Distributions * filter, and an out filter. The in filter occurs before data is 40*8d741a5dSApple OSS Distributions * placed in the receive socket buffer. This is done to avoid waking 41*8d741a5dSApple OSS Distributions * the process unnecessarily. The out filter occurs before the data is 42*8d741a5dSApple OSS Distributions * appended to the send socket buffer. This should cover inbound and 43*8d741a5dSApple OSS Distributions * outbound data. For monitoring state changes, we've added a notify 44*8d741a5dSApple OSS Distributions * function that will be called when various events that the filter can 45*8d741a5dSApple OSS Distributions * not intercept occur. In addition, we've added a few functions that a 46*8d741a5dSApple OSS Distributions * filter may use to intercept common operations. These functions are: 47*8d741a5dSApple OSS Distributions * connect (inbound), connect (outbound), bind, set socket option, 48*8d741a5dSApple OSS Distributions * get socket option, and listen. Bind, listen, connect in, and connect 49*8d741a5dSApple OSS Distributions * out could be used together to build a fairly comprehensive firewall 50*8d741a5dSApple OSS Distributions * without having to do much with individual packets. 51*8d741a5dSApple OSS Distributions */ 52*8d741a5dSApple OSS Distributions #ifndef __KPI_SOCKETFILTER__ 53*8d741a5dSApple OSS Distributions #define __KPI_SOCKETFILTER__ 54*8d741a5dSApple OSS Distributions 55*8d741a5dSApple OSS Distributions #include <sys/kernel_types.h> 56*8d741a5dSApple OSS Distributions #include <sys/kpi_socket.h> 57*8d741a5dSApple OSS Distributions #include <sys/ioccom.h> 58*8d741a5dSApple OSS Distributions 59*8d741a5dSApple OSS Distributions #ifndef PRIVATE 60*8d741a5dSApple OSS Distributions #include <Availability.h> 61*8d741a5dSApple OSS Distributions #define __NKE_API_DEPRECATED __API_DEPRECATED("Network Kernel Extension KPI is deprecated", macos(10.4, 10.15)) 62*8d741a5dSApple OSS Distributions #else 63*8d741a5dSApple OSS Distributions #define __NKE_API_DEPRECATED 64*8d741a5dSApple OSS Distributions #endif /* PRIVATE */ 65*8d741a5dSApple OSS Distributions 66*8d741a5dSApple OSS Distributions struct sockaddr; 67*8d741a5dSApple OSS Distributions 68*8d741a5dSApple OSS Distributions /*! 69*8d741a5dSApple OSS Distributions * @enum sflt_flags 70*8d741a5dSApple OSS Distributions * @abstract Constants defining mbuf flags. Only the flags listed below 71*8d741a5dSApple OSS Distributions * can be set or retrieved. 72*8d741a5dSApple OSS Distributions * @constant SFLT_GLOBAL Indicates this socket filter should be 73*8d741a5dSApple OSS Distributions * attached to all new sockets when they're created. 74*8d741a5dSApple OSS Distributions * @constant SFLT_PROG Indicates this socket filter should be attached 75*8d741a5dSApple OSS Distributions * only when request by the application using the SO_NKE socket 76*8d741a5dSApple OSS Distributions * option. 77*8d741a5dSApple OSS Distributions * @constant SFLT_EXTENDED Indicates that this socket filter utilizes 78*8d741a5dSApple OSS Distributions * the extended fields within the sflt_filter structure. 79*8d741a5dSApple OSS Distributions * @constant SFLT_EXTENDED_REGISTRY Indicates that this socket filter 80*8d741a5dSApple OSS Distributions * wants to attach to all the sockets already present on the 81*8d741a5dSApple OSS Distributions * system. It will also receive notifications for these sockets. 82*8d741a5dSApple OSS Distributions */ 83*8d741a5dSApple OSS Distributions enum { 84*8d741a5dSApple OSS Distributions SFLT_GLOBAL = 0x01, 85*8d741a5dSApple OSS Distributions SFLT_PROG = 0x02, 86*8d741a5dSApple OSS Distributions SFLT_EXTENDED = 0x04, 87*8d741a5dSApple OSS Distributions SFLT_EXTENDED_REGISTRY = 0x08 88*8d741a5dSApple OSS Distributions }; 89*8d741a5dSApple OSS Distributions typedef u_int32_t sflt_flags; 90*8d741a5dSApple OSS Distributions 91*8d741a5dSApple OSS Distributions /*! 92*8d741a5dSApple OSS Distributions * @typedef sflt_handle 93*8d741a5dSApple OSS Distributions * @abstract A 4 byte identifier used with the SO_NKE socket option to 94*8d741a5dSApple OSS Distributions * identify the socket filter to be attached. 95*8d741a5dSApple OSS Distributions */ 96*8d741a5dSApple OSS Distributions typedef u_int32_t sflt_handle; 97*8d741a5dSApple OSS Distributions 98*8d741a5dSApple OSS Distributions /*! 99*8d741a5dSApple OSS Distributions * @enum sflt_event_t 100*8d741a5dSApple OSS Distributions * @abstract Events notify a filter of state changes and other various 101*8d741a5dSApple OSS Distributions * events related to the socket. These events cannot be prevented 102*8d741a5dSApple OSS Distributions * or intercepted, only observed. 103*8d741a5dSApple OSS Distributions * @constant sock_evt_connected Indicates this socket has moved to the 104*8d741a5dSApple OSS Distributions * connected state. 105*8d741a5dSApple OSS Distributions * @constant sock_evt_disconnected Indicates this socket has moved to 106*8d741a5dSApple OSS Distributions * the disconnected state. 107*8d741a5dSApple OSS Distributions * @constant sock_evt_flush_read The read socket buffer has been 108*8d741a5dSApple OSS Distributions * flushed. 109*8d741a5dSApple OSS Distributions * @constant sock_evt_shutdown The read and or write side(s) of the 110*8d741a5dSApple OSS Distributions * connection have been shutdown. The param will point to an 111*8d741a5dSApple OSS Distributions * integer that indicates the direction that has been shutdown. See 112*8d741a5dSApple OSS Distributions * 'man 2 shutdown' for more information. 113*8d741a5dSApple OSS Distributions * @constant sock_evt_cantrecvmore Indicates the socket cannot receive 114*8d741a5dSApple OSS Distributions * more data. 115*8d741a5dSApple OSS Distributions * @constant sock_evt_cantsendmore Indicates the socket cannot send 116*8d741a5dSApple OSS Distributions * more data. 117*8d741a5dSApple OSS Distributions * @constant sock_evt_closing Indicates the socket is closing. 118*8d741a5dSApple OSS Distributions * @constant sock_evt_bound Indicates this socket has moved to the 119*8d741a5dSApple OSS Distributions * bound state (only for PF_INET/PF_INET6 domain). 120*8d741a5dSApple OSS Distributions */ 121*8d741a5dSApple OSS Distributions enum { 122*8d741a5dSApple OSS Distributions sock_evt_connecting = 1, 123*8d741a5dSApple OSS Distributions sock_evt_connected = 2, 124*8d741a5dSApple OSS Distributions sock_evt_disconnecting = 3, 125*8d741a5dSApple OSS Distributions sock_evt_disconnected = 4, 126*8d741a5dSApple OSS Distributions sock_evt_flush_read = 5, 127*8d741a5dSApple OSS Distributions sock_evt_shutdown = 6, /* param points to an integer specifying how (read, write, or both) see man 2 shutdown */ 128*8d741a5dSApple OSS Distributions sock_evt_cantrecvmore = 7, 129*8d741a5dSApple OSS Distributions sock_evt_cantsendmore = 8, 130*8d741a5dSApple OSS Distributions sock_evt_closing = 9, 131*8d741a5dSApple OSS Distributions sock_evt_bound = 10 132*8d741a5dSApple OSS Distributions }; 133*8d741a5dSApple OSS Distributions typedef u_int32_t sflt_event_t; 134*8d741a5dSApple OSS Distributions 135*8d741a5dSApple OSS Distributions /*! 136*8d741a5dSApple OSS Distributions * @enum sflt_data_flag_t 137*8d741a5dSApple OSS Distributions * @abstract Inbound and outbound data filters may handle many 138*8d741a5dSApple OSS Distributions * different types of incoming and outgoing data. These flags help 139*8d741a5dSApple OSS Distributions * distinguish between normal data, out-of-band data, and records. 140*8d741a5dSApple OSS Distributions * @constant sock_data_filt_flag_oob Indicates this data is out-of-band 141*8d741a5dSApple OSS Distributions * data. 142*8d741a5dSApple OSS Distributions * @constant sock_data_filt_flag_record Indicates this data is a 143*8d741a5dSApple OSS Distributions * record. This flag is only ever seen on inbound data. 144*8d741a5dSApple OSS Distributions */ 145*8d741a5dSApple OSS Distributions enum { 146*8d741a5dSApple OSS Distributions sock_data_filt_flag_oob = 1, 147*8d741a5dSApple OSS Distributions sock_data_filt_flag_record = 2 148*8d741a5dSApple OSS Distributions }; 149*8d741a5dSApple OSS Distributions typedef u_int32_t sflt_data_flag_t; 150*8d741a5dSApple OSS Distributions 151*8d741a5dSApple OSS Distributions __BEGIN_DECLS 152*8d741a5dSApple OSS Distributions 153*8d741a5dSApple OSS Distributions /*! 154*8d741a5dSApple OSS Distributions * @typedef sf_unregistered_func 155*8d741a5dSApple OSS Distributions * 156*8d741a5dSApple OSS Distributions * @discussion sf_unregistered_func is called to notify the filter it 157*8d741a5dSApple OSS Distributions * has been unregistered. This is the last function the stack will 158*8d741a5dSApple OSS Distributions * call and this function will only be called once all other 159*8d741a5dSApple OSS Distributions * function calls in to your filter have completed. Once this 160*8d741a5dSApple OSS Distributions * function has been called, your kext may safely unload. 161*8d741a5dSApple OSS Distributions * @param handle The socket filter handle used to identify this filter. 162*8d741a5dSApple OSS Distributions */ 163*8d741a5dSApple OSS Distributions typedef void (*sf_unregistered_func)(sflt_handle handle); 164*8d741a5dSApple OSS Distributions 165*8d741a5dSApple OSS Distributions /*! 166*8d741a5dSApple OSS Distributions * @typedef sf_attach_func 167*8d741a5dSApple OSS Distributions * 168*8d741a5dSApple OSS Distributions * @discussion sf_attach_func is called to notify the filter it has 169*8d741a5dSApple OSS Distributions * been attached to a socket. The filter may allocate memory for 170*8d741a5dSApple OSS Distributions * this attachment and use the cookie to track it. This filter is 171*8d741a5dSApple OSS Distributions * called in one of two cases: 172*8d741a5dSApple OSS Distributions * 1) You've installed a global filter and a new socket was created. 173*8d741a5dSApple OSS Distributions * 2) Your non-global socket filter is being attached using the SO_NKE 174*8d741a5dSApple OSS Distributions * socket option. 175*8d741a5dSApple OSS Distributions * @param cookie Used to allow the socket filter to set the cookie for 176*8d741a5dSApple OSS Distributions * this attachment. 177*8d741a5dSApple OSS Distributions * @param so The socket the filter is being attached to. 178*8d741a5dSApple OSS Distributions * @result If you return a non-zero value, your filter will not be 179*8d741a5dSApple OSS Distributions * attached to this socket. 180*8d741a5dSApple OSS Distributions */ 181*8d741a5dSApple OSS Distributions typedef errno_t (*sf_attach_func)(void **cookie, socket_t so); 182*8d741a5dSApple OSS Distributions 183*8d741a5dSApple OSS Distributions /*! 184*8d741a5dSApple OSS Distributions * @typedef sf_detach_func 185*8d741a5dSApple OSS Distributions * 186*8d741a5dSApple OSS Distributions * @discussion sf_detach_func is called to notify the filter it has 187*8d741a5dSApple OSS Distributions * been detached from a socket. If the filter allocated any memory 188*8d741a5dSApple OSS Distributions * for this attachment, it should be freed. This function will 189*8d741a5dSApple OSS Distributions * be called when the socket is disposed of. 190*8d741a5dSApple OSS Distributions * @param cookie Cookie value specified when the filter attach was 191*8d741a5dSApple OSS Distributions * called. 192*8d741a5dSApple OSS Distributions * @param so The socket the filter is attached to. 193*8d741a5dSApple OSS Distributions * @discussion If you return a non-zero value, your filter will not be 194*8d741a5dSApple OSS Distributions * attached to this socket. 195*8d741a5dSApple OSS Distributions */ 196*8d741a5dSApple OSS Distributions typedef void (*sf_detach_func)(void *cookie, socket_t so); 197*8d741a5dSApple OSS Distributions 198*8d741a5dSApple OSS Distributions /*! 199*8d741a5dSApple OSS Distributions * @typedef sf_notify_func 200*8d741a5dSApple OSS Distributions * 201*8d741a5dSApple OSS Distributions * @discussion sf_notify_func is called to notify the filter of various 202*8d741a5dSApple OSS Distributions * state changes and other events occuring on the socket. 203*8d741a5dSApple OSS Distributions * @param cookie Cookie value specified when the filter attach was 204*8d741a5dSApple OSS Distributions * called. 205*8d741a5dSApple OSS Distributions * @param so The socket the filter is attached to. 206*8d741a5dSApple OSS Distributions * @param event The type of event that has occurred. 207*8d741a5dSApple OSS Distributions * @param param Additional information about the event. 208*8d741a5dSApple OSS Distributions */ 209*8d741a5dSApple OSS Distributions typedef void (*sf_notify_func)(void *cookie, socket_t so, sflt_event_t event, 210*8d741a5dSApple OSS Distributions void *param); 211*8d741a5dSApple OSS Distributions 212*8d741a5dSApple OSS Distributions /*! 213*8d741a5dSApple OSS Distributions * @typedef sf_getpeername_func 214*8d741a5dSApple OSS Distributions * 215*8d741a5dSApple OSS Distributions * @discussion sf_getpeername_func is called to allow a filter to 216*8d741a5dSApple OSS Distributions * to intercept the getpeername function. When called, sa will 217*8d741a5dSApple OSS Distributions * point to a pointer to a socket address that was malloced 218*8d741a5dSApple OSS Distributions * in zone M_SONAME. If you want to replace this address, either 219*8d741a5dSApple OSS Distributions * modify the currenty copy or allocate a new one and free the 220*8d741a5dSApple OSS Distributions * old one. 221*8d741a5dSApple OSS Distributions * @param cookie Cookie value specified when the filter attach was 222*8d741a5dSApple OSS Distributions * called. 223*8d741a5dSApple OSS Distributions * @param so The socket the filter is attached to. 224*8d741a5dSApple OSS Distributions * @param sa A pointer to a socket address pointer. 225*8d741a5dSApple OSS Distributions * @result If you return a non-zero value, processing will stop. If 226*8d741a5dSApple OSS Distributions * you return EJUSTRETURN, no further filters will be called 227*8d741a5dSApple OSS Distributions * but a result of zero will be returned to the caller of 228*8d741a5dSApple OSS Distributions * getpeername. 229*8d741a5dSApple OSS Distributions */ 230*8d741a5dSApple OSS Distributions typedef int (*sf_getpeername_func)(void *cookie, socket_t so, 231*8d741a5dSApple OSS Distributions struct sockaddr **sa); 232*8d741a5dSApple OSS Distributions 233*8d741a5dSApple OSS Distributions /*! 234*8d741a5dSApple OSS Distributions * @typedef sf_getsockname_func 235*8d741a5dSApple OSS Distributions * 236*8d741a5dSApple OSS Distributions * @discussion sf_getsockname_func is called to allow a filter to 237*8d741a5dSApple OSS Distributions * to intercept the getsockname function. When called, sa will 238*8d741a5dSApple OSS Distributions * point to a pointer to a socket address that was malloced 239*8d741a5dSApple OSS Distributions * in zone M_SONAME. If you want to replace this address, either 240*8d741a5dSApple OSS Distributions * modify the currenty copy or allocate a new one and free the 241*8d741a5dSApple OSS Distributions * old one. 242*8d741a5dSApple OSS Distributions * @param cookie Cookie value specified when the filter attach was 243*8d741a5dSApple OSS Distributions * called. 244*8d741a5dSApple OSS Distributions * @param so The socket the filter is attached to. 245*8d741a5dSApple OSS Distributions * @param sa A pointer to a socket address pointer. 246*8d741a5dSApple OSS Distributions * @result If you return a non-zero value, processing will stop. If 247*8d741a5dSApple OSS Distributions * you return EJUSTRETURN, no further filters will be called 248*8d741a5dSApple OSS Distributions * but a result of zero will be returned to the caller of 249*8d741a5dSApple OSS Distributions * getsockname. 250*8d741a5dSApple OSS Distributions */ 251*8d741a5dSApple OSS Distributions typedef int (*sf_getsockname_func)(void *cookie, socket_t so, 252*8d741a5dSApple OSS Distributions struct sockaddr **sa); 253*8d741a5dSApple OSS Distributions 254*8d741a5dSApple OSS Distributions /*! 255*8d741a5dSApple OSS Distributions * @typedef sf_data_in_func 256*8d741a5dSApple OSS Distributions * 257*8d741a5dSApple OSS Distributions * @discussion sf_data_in_func is called to filter incoming data. If your 258*8d741a5dSApple OSS Distributions * filter intercepts data for later reinjection, it must queue 259*8d741a5dSApple OSS Distributions * all incoming data to preserve the order of the data. Use 260*8d741a5dSApple OSS Distributions * sock_inject_data_in to later reinject this data if you return 261*8d741a5dSApple OSS Distributions * EJUSTRETURN. Warning: This filter is on the data path. Do not 262*8d741a5dSApple OSS Distributions * spend excesive time. Do not wait for data on another socket. 263*8d741a5dSApple OSS Distributions * @param cookie Cookie value specified when the filter attach was 264*8d741a5dSApple OSS Distributions * called. 265*8d741a5dSApple OSS Distributions * @param so The socket the filter is attached to. 266*8d741a5dSApple OSS Distributions * @param from The addres the data is from, may be NULL if the socket 267*8d741a5dSApple OSS Distributions * is connected. 268*8d741a5dSApple OSS Distributions * @param data The data being received. Control data may appear in the 269*8d741a5dSApple OSS Distributions * mbuf chain, be sure to check the mbuf types to find control 270*8d741a5dSApple OSS Distributions * data. 271*8d741a5dSApple OSS Distributions * @param control Control data being passed separately from the data. 272*8d741a5dSApple OSS Distributions * @param flags Flags to indicate if this is out of band data or a 273*8d741a5dSApple OSS Distributions * record. 274*8d741a5dSApple OSS Distributions * @result Return: 275*8d741a5dSApple OSS Distributions * 0 - The caller will continue with normal processing of the data. 276*8d741a5dSApple OSS Distributions * EJUSTRETURN - The caller will stop processing the data, the 277*8d741a5dSApple OSS Distributions * data will not be freed. 278*8d741a5dSApple OSS Distributions * Anything Else - The caller will free the data and stop 279*8d741a5dSApple OSS Distributions * processing. 280*8d741a5dSApple OSS Distributions */ 281*8d741a5dSApple OSS Distributions typedef errno_t (*sf_data_in_func)(void *cookie, socket_t so, 282*8d741a5dSApple OSS Distributions const struct sockaddr *from, mbuf_t *data, mbuf_t *control, 283*8d741a5dSApple OSS Distributions sflt_data_flag_t flags); 284*8d741a5dSApple OSS Distributions 285*8d741a5dSApple OSS Distributions /*! 286*8d741a5dSApple OSS Distributions * @typedef sf_data_out_func 287*8d741a5dSApple OSS Distributions * 288*8d741a5dSApple OSS Distributions * @discussion sf_data_out_func is called to filter outbound data. If 289*8d741a5dSApple OSS Distributions * your filter intercepts data for later reinjection, it must queue 290*8d741a5dSApple OSS Distributions * all outbound data to preserve the order of the data when 291*8d741a5dSApple OSS Distributions * reinjecting. Use sock_inject_data_out to later reinject this 292*8d741a5dSApple OSS Distributions * data. 293*8d741a5dSApple OSS Distributions * @param cookie Cookie value specified when the filter attach was 294*8d741a5dSApple OSS Distributions * called. 295*8d741a5dSApple OSS Distributions * @param so The socket the filter is attached to. 296*8d741a5dSApple OSS Distributions * @param to The address the data is to, may be NULL if the socket 297*8d741a5dSApple OSS Distributions * is connected. 298*8d741a5dSApple OSS Distributions * @param data The data being received. Control data may appear in the 299*8d741a5dSApple OSS Distributions * mbuf chain, be sure to check the mbuf types to find control 300*8d741a5dSApple OSS Distributions * data. 301*8d741a5dSApple OSS Distributions * @param control Control data being passed separately from the data. 302*8d741a5dSApple OSS Distributions * @param flags Flags to indicate if this is out of band data or a 303*8d741a5dSApple OSS Distributions * record. 304*8d741a5dSApple OSS Distributions * @result Return: 305*8d741a5dSApple OSS Distributions * 0 - The caller will continue with normal processing of the data. 306*8d741a5dSApple OSS Distributions * EJUSTRETURN - The caller will stop processing the data, 307*8d741a5dSApple OSS Distributions * the data will not be freed. 308*8d741a5dSApple OSS Distributions * Anything Else - The caller will free the data and stop 309*8d741a5dSApple OSS Distributions * processing. 310*8d741a5dSApple OSS Distributions */ 311*8d741a5dSApple OSS Distributions typedef errno_t (*sf_data_out_func)(void *cookie, socket_t so, 312*8d741a5dSApple OSS Distributions const struct sockaddr *to, mbuf_t *data, mbuf_t *control, 313*8d741a5dSApple OSS Distributions sflt_data_flag_t flags); 314*8d741a5dSApple OSS Distributions 315*8d741a5dSApple OSS Distributions /*! 316*8d741a5dSApple OSS Distributions * @typedef sf_connect_in_func 317*8d741a5dSApple OSS Distributions * 318*8d741a5dSApple OSS Distributions * @discussion sf_connect_in_func is called to filter inbound connections. 319*8d741a5dSApple OSS Distributions * A protocol will call this before accepting an incoming 320*8d741a5dSApple OSS Distributions * connection and placing it on the queue of completed connections. 321*8d741a5dSApple OSS Distributions * Warning: This filter is on the data path. Do not spend excesive 322*8d741a5dSApple OSS Distributions * time. Do not wait for data on another socket. 323*8d741a5dSApple OSS Distributions * @param cookie Cookie value specified when the filter attach was 324*8d741a5dSApple OSS Distributions * called. 325*8d741a5dSApple OSS Distributions * @param so The socket the filter is attached to. 326*8d741a5dSApple OSS Distributions * @param from The address the incoming connection is from. 327*8d741a5dSApple OSS Distributions * @result Return: 328*8d741a5dSApple OSS Distributions * 0 - The caller will continue with normal processing of the 329*8d741a5dSApple OSS Distributions * connection. 330*8d741a5dSApple OSS Distributions * Anything Else - The caller will rejecting the incoming 331*8d741a5dSApple OSS Distributions * connection. 332*8d741a5dSApple OSS Distributions */ 333*8d741a5dSApple OSS Distributions typedef errno_t (*sf_connect_in_func)(void *cookie, socket_t so, 334*8d741a5dSApple OSS Distributions const struct sockaddr *from); 335*8d741a5dSApple OSS Distributions 336*8d741a5dSApple OSS Distributions /*! 337*8d741a5dSApple OSS Distributions * @typedef sf_connect_out_func 338*8d741a5dSApple OSS Distributions * 339*8d741a5dSApple OSS Distributions * @discussion sf_connect_out_func is called to filter outbound 340*8d741a5dSApple OSS Distributions * connections. A protocol will call this before initiating an 341*8d741a5dSApple OSS Distributions * outbound connection. 342*8d741a5dSApple OSS Distributions * @param cookie Cookie value specified when the filter attach was 343*8d741a5dSApple OSS Distributions * called. 344*8d741a5dSApple OSS Distributions * @param so The socket the filter is attached to. 345*8d741a5dSApple OSS Distributions * @param to The remote address of the outbound connection. 346*8d741a5dSApple OSS Distributions * @result Return: 347*8d741a5dSApple OSS Distributions * 0 - The caller will continue with normal processing of the 348*8d741a5dSApple OSS Distributions * connection. 349*8d741a5dSApple OSS Distributions * EJUSTRETURN - The caller will return with a value of 0 (no error) 350*8d741a5dSApple OSS Distributions * from that point without further processing the connect command. The 351*8d741a5dSApple OSS Distributions * protocol layer will not see the call. 352*8d741a5dSApple OSS Distributions * Anything Else - The caller will rejecting the outbound 353*8d741a5dSApple OSS Distributions * connection. 354*8d741a5dSApple OSS Distributions */ 355*8d741a5dSApple OSS Distributions typedef errno_t (*sf_connect_out_func)(void *cookie, socket_t so, 356*8d741a5dSApple OSS Distributions const struct sockaddr *to); 357*8d741a5dSApple OSS Distributions 358*8d741a5dSApple OSS Distributions /*! 359*8d741a5dSApple OSS Distributions * @typedef sf_bind_func 360*8d741a5dSApple OSS Distributions * 361*8d741a5dSApple OSS Distributions * @discussion sf_bind_func is called before performing a bind 362*8d741a5dSApple OSS Distributions * operation on a socket. 363*8d741a5dSApple OSS Distributions * @param cookie Cookie value specified when the filter attach was 364*8d741a5dSApple OSS Distributions * called. 365*8d741a5dSApple OSS Distributions * @param so The socket the filter is attached to. 366*8d741a5dSApple OSS Distributions * @param to The local address of the socket will be bound to. 367*8d741a5dSApple OSS Distributions * @result Return: 368*8d741a5dSApple OSS Distributions * 0 - The caller will continue with normal processing of the bind. 369*8d741a5dSApple OSS Distributions * EJUSTRETURN - The caller will return with a value of 0 (no error) 370*8d741a5dSApple OSS Distributions * from that point without further processing the bind command. The 371*8d741a5dSApple OSS Distributions * protocol layer will not see the call. 372*8d741a5dSApple OSS Distributions * Anything Else - The caller will rejecting the bind. 373*8d741a5dSApple OSS Distributions */ 374*8d741a5dSApple OSS Distributions typedef errno_t (*sf_bind_func)(void *cookie, socket_t so, 375*8d741a5dSApple OSS Distributions const struct sockaddr *to); 376*8d741a5dSApple OSS Distributions 377*8d741a5dSApple OSS Distributions /*! 378*8d741a5dSApple OSS Distributions * @typedef sf_setoption_func 379*8d741a5dSApple OSS Distributions * 380*8d741a5dSApple OSS Distributions * @discussion sf_setoption_func is called before performing setsockopt 381*8d741a5dSApple OSS Distributions * on a socket. 382*8d741a5dSApple OSS Distributions * @param cookie Cookie value specified when the filter attach was 383*8d741a5dSApple OSS Distributions * called. 384*8d741a5dSApple OSS Distributions * @param so The socket the filter is attached to. 385*8d741a5dSApple OSS Distributions * @param opt The socket option to set. 386*8d741a5dSApple OSS Distributions * @result Return: 387*8d741a5dSApple OSS Distributions * 0 - The caller will continue with normal processing of the 388*8d741a5dSApple OSS Distributions * setsockopt. 389*8d741a5dSApple OSS Distributions * EJUSTRETURN - The caller will return with a value of 0 (no error) 390*8d741a5dSApple OSS Distributions * from that point without further propagating the set option 391*8d741a5dSApple OSS Distributions * command. The socket and protocol layers will not see the call. 392*8d741a5dSApple OSS Distributions * Anything Else - The caller will stop processing and return 393*8d741a5dSApple OSS Distributions * this error. 394*8d741a5dSApple OSS Distributions */ 395*8d741a5dSApple OSS Distributions typedef errno_t (*sf_setoption_func)(void *cookie, socket_t so, sockopt_t opt); 396*8d741a5dSApple OSS Distributions 397*8d741a5dSApple OSS Distributions /*! 398*8d741a5dSApple OSS Distributions * @typedef sf_getoption_func 399*8d741a5dSApple OSS Distributions * 400*8d741a5dSApple OSS Distributions * @discussion sf_getoption_func is called before performing getsockopt 401*8d741a5dSApple OSS Distributions * on a socket. 402*8d741a5dSApple OSS Distributions * @param cookie Cookie value specified when the filter attach was 403*8d741a5dSApple OSS Distributions * called. 404*8d741a5dSApple OSS Distributions * @param so The socket the filter is attached to. 405*8d741a5dSApple OSS Distributions * @param opt The socket option to get. 406*8d741a5dSApple OSS Distributions * @result Return: 407*8d741a5dSApple OSS Distributions * 0 - The caller will continue with normal processing of the 408*8d741a5dSApple OSS Distributions * getsockopt. 409*8d741a5dSApple OSS Distributions * EJUSTRETURN - The caller will return with a value of 0 (no error) 410*8d741a5dSApple OSS Distributions * from that point without further propagating the get option 411*8d741a5dSApple OSS Distributions * command. The socket and protocol layers will not see the call. 412*8d741a5dSApple OSS Distributions * Anything Else - The caller will stop processing and return 413*8d741a5dSApple OSS Distributions * this error. 414*8d741a5dSApple OSS Distributions */ 415*8d741a5dSApple OSS Distributions typedef errno_t (*sf_getoption_func)(void *cookie, socket_t so, sockopt_t opt); 416*8d741a5dSApple OSS Distributions 417*8d741a5dSApple OSS Distributions /*! 418*8d741a5dSApple OSS Distributions * @typedef sf_listen_func 419*8d741a5dSApple OSS Distributions * 420*8d741a5dSApple OSS Distributions * @discussion sf_listen_func is called before performing listen 421*8d741a5dSApple OSS Distributions * on a socket. 422*8d741a5dSApple OSS Distributions * @param cookie Cookie value specified when the filter attach was 423*8d741a5dSApple OSS Distributions * called. 424*8d741a5dSApple OSS Distributions * @param so The socket the filter is attached to. 425*8d741a5dSApple OSS Distributions * @result Return: 426*8d741a5dSApple OSS Distributions * 0 - The caller will continue with normal processing of listen. 427*8d741a5dSApple OSS Distributions * EJUSTRETURN - The caller will return with a value of 0 (no error) 428*8d741a5dSApple OSS Distributions * from that point without further processing the listen command. The 429*8d741a5dSApple OSS Distributions * protocol will not see the call. 430*8d741a5dSApple OSS Distributions * Anything Else - The caller will stop processing and return 431*8d741a5dSApple OSS Distributions * this error. 432*8d741a5dSApple OSS Distributions */ 433*8d741a5dSApple OSS Distributions typedef errno_t (*sf_listen_func)(void *cookie, socket_t so); 434*8d741a5dSApple OSS Distributions 435*8d741a5dSApple OSS Distributions /*! 436*8d741a5dSApple OSS Distributions * @typedef sf_ioctl_func 437*8d741a5dSApple OSS Distributions * 438*8d741a5dSApple OSS Distributions * @discussion sf_ioctl_func is called before performing an ioctl 439*8d741a5dSApple OSS Distributions * on a socket. 440*8d741a5dSApple OSS Distributions * 441*8d741a5dSApple OSS Distributions * All undefined ioctls are reserved for future use by Apple. If 442*8d741a5dSApple OSS Distributions * you need to communicate with your kext using an ioctl, please 443*8d741a5dSApple OSS Distributions * use SIOCSIFKPI and SIOCGIFKPI. 444*8d741a5dSApple OSS Distributions * @param cookie Cookie value specified when the filter attach was 445*8d741a5dSApple OSS Distributions * called. 446*8d741a5dSApple OSS Distributions * @param so The socket the filter is attached to. 447*8d741a5dSApple OSS Distributions * @param request The ioctl name. 448*8d741a5dSApple OSS Distributions * @param argp A pointer to the ioctl parameter. 449*8d741a5dSApple OSS Distributions * @result Return: 450*8d741a5dSApple OSS Distributions * 0 - The caller will continue with normal processing of 451*8d741a5dSApple OSS Distributions * this ioctl. 452*8d741a5dSApple OSS Distributions * EJUSTRETURN - The caller will return with a value of 0 (no error) 453*8d741a5dSApple OSS Distributions * from that point without further processing or propogating 454*8d741a5dSApple OSS Distributions * the ioctl. 455*8d741a5dSApple OSS Distributions * Anything Else - The caller will stop processing and return 456*8d741a5dSApple OSS Distributions * this error. 457*8d741a5dSApple OSS Distributions */ 458*8d741a5dSApple OSS Distributions typedef errno_t (*sf_ioctl_func)(void *cookie, socket_t so, 459*8d741a5dSApple OSS Distributions unsigned long request, const char*__sized_by(IOCPARM_LEN(request)) argp); 460*8d741a5dSApple OSS Distributions 461*8d741a5dSApple OSS Distributions /*! 462*8d741a5dSApple OSS Distributions * @typedef sf_accept_func 463*8d741a5dSApple OSS Distributions * 464*8d741a5dSApple OSS Distributions * @discussion sf_accept_func is called after a socket is dequeued 465*8d741a5dSApple OSS Distributions * off the completed (incoming) connection list and before 466*8d741a5dSApple OSS Distributions * the file descriptor is associated with it. A filter can 467*8d741a5dSApple OSS Distributions * utilize this callback to intercept the accepted socket 468*8d741a5dSApple OSS Distributions * in order to examine it, prior to returning the socket to 469*8d741a5dSApple OSS Distributions * the caller of accept. Such a filter may also choose to 470*8d741a5dSApple OSS Distributions * discard the accepted socket if it wishes to do so. 471*8d741a5dSApple OSS Distributions * @param cookie Cookie value specified when the filter attach was called. 472*8d741a5dSApple OSS Distributions * @param so_listen The listening socket. 473*8d741a5dSApple OSS Distributions * @param so The socket that is about to be accepted. 474*8d741a5dSApple OSS Distributions * @param local The local address of the about to be accepted socket. 475*8d741a5dSApple OSS Distributions * @param remote The remote address of the about to be accepted socket. 476*8d741a5dSApple OSS Distributions * @result Return: 477*8d741a5dSApple OSS Distributions * 0 - The caller will continue with normal processing of accept. 478*8d741a5dSApple OSS Distributions * EJUSTRETURN - The to be accepted socket will be disconnected 479*8d741a5dSApple OSS Distributions * prior to being returned to the caller of accept. No further 480*8d741a5dSApple OSS Distributions * control or data operations on the socket will be allowed. 481*8d741a5dSApple OSS Distributions * This is the recommended return value as it has the least 482*8d741a5dSApple OSS Distributions * amount of impact, especially to applications which don't 483*8d741a5dSApple OSS Distributions * check the error value returned by accept. 484*8d741a5dSApple OSS Distributions * Anything Else - The to be accepted socket will be closed and 485*8d741a5dSApple OSS Distributions * the error will be returned to the caller of accept. 486*8d741a5dSApple OSS Distributions * Note that socket filter developers are advised to exercise 487*8d741a5dSApple OSS Distributions * caution when returning non-zero values to the caller, 488*8d741a5dSApple OSS Distributions * since some applications don't check the error value 489*8d741a5dSApple OSS Distributions * returned by accept and therefore risk breakage. 490*8d741a5dSApple OSS Distributions */ 491*8d741a5dSApple OSS Distributions typedef errno_t (*sf_accept_func)(void *cookie, socket_t so_listen, socket_t so, 492*8d741a5dSApple OSS Distributions const struct sockaddr *local, const struct sockaddr *remote); 493*8d741a5dSApple OSS Distributions 494*8d741a5dSApple OSS Distributions /*! 495*8d741a5dSApple OSS Distributions * @struct sflt_filter 496*8d741a5dSApple OSS Distributions * @discussion This structure is used to define a socket filter. 497*8d741a5dSApple OSS Distributions * @field sf_handle A value used to find socket filters by 498*8d741a5dSApple OSS Distributions * applications. An application can use this value to specify that 499*8d741a5dSApple OSS Distributions * this filter should be attached when using the SO_NKE socket 500*8d741a5dSApple OSS Distributions * option. 501*8d741a5dSApple OSS Distributions * @field sf_flags Indicate whether this filter should be attached to 502*8d741a5dSApple OSS Distributions * all new sockets or just those that request the filter be 503*8d741a5dSApple OSS Distributions * attached using the SO_NKE socket option. If this filter 504*8d741a5dSApple OSS Distributions * utilizes the socket filter extension fields, it must also 505*8d741a5dSApple OSS Distributions * set SFLT_EXTENDED. 506*8d741a5dSApple OSS Distributions * @field sf_name A name used for debug purposes. 507*8d741a5dSApple OSS Distributions * @field sf_unregistered Your function for being notified when your 508*8d741a5dSApple OSS Distributions * filter has been unregistered. 509*8d741a5dSApple OSS Distributions * @field sf_attach Your function for handling attaches to sockets. 510*8d741a5dSApple OSS Distributions * @field sf_detach Your function for handling detaches from sockets. 511*8d741a5dSApple OSS Distributions * @field sf_notify Your function for handling events. May be null. 512*8d741a5dSApple OSS Distributions * @field sf_data_in Your function for handling incoming data. May be 513*8d741a5dSApple OSS Distributions * null. 514*8d741a5dSApple OSS Distributions * @field sf_data_out Your function for handling outgoing data. May be 515*8d741a5dSApple OSS Distributions * null. 516*8d741a5dSApple OSS Distributions * @field sf_connect_in Your function for handling inbound 517*8d741a5dSApple OSS Distributions * connections. May be null. 518*8d741a5dSApple OSS Distributions * @field sf_connect_out Your function for handling outbound 519*8d741a5dSApple OSS Distributions * connections. May be null. 520*8d741a5dSApple OSS Distributions * @field sf_bind Your function for handling binds. May be null. 521*8d741a5dSApple OSS Distributions * @field sf_setoption Your function for handling setsockopt. May be null. 522*8d741a5dSApple OSS Distributions * @field sf_getoption Your function for handling getsockopt. May be null. 523*8d741a5dSApple OSS Distributions * @field sf_listen Your function for handling listen. May be null. 524*8d741a5dSApple OSS Distributions * @field sf_ioctl Your function for handling ioctls. May be null. 525*8d741a5dSApple OSS Distributions * @field sf_len Length of socket filter extension structure; developers 526*8d741a5dSApple OSS Distributions * must initialize this to sizeof sflt_filter_ext structure. 527*8d741a5dSApple OSS Distributions * This field and all fields following it will only be valid 528*8d741a5dSApple OSS Distributions * if SFLT_EXTENDED flag is set in sf_flags field. 529*8d741a5dSApple OSS Distributions * @field sf_ext_accept Your function for handling inbound connections 530*8d741a5dSApple OSS Distributions * at accept time. May be null. 531*8d741a5dSApple OSS Distributions * @field sf_ext_rsvd Reserved for future use; you must initialize 532*8d741a5dSApple OSS Distributions * the reserved fields with zeroes. 533*8d741a5dSApple OSS Distributions */ 534*8d741a5dSApple OSS Distributions struct sflt_filter { 535*8d741a5dSApple OSS Distributions sflt_handle sf_handle; 536*8d741a5dSApple OSS Distributions int sf_flags; 537*8d741a5dSApple OSS Distributions char *sf_name; 538*8d741a5dSApple OSS Distributions 539*8d741a5dSApple OSS Distributions sf_unregistered_func sf_unregistered; 540*8d741a5dSApple OSS Distributions sf_attach_func sf_attach; 541*8d741a5dSApple OSS Distributions sf_detach_func sf_detach; 542*8d741a5dSApple OSS Distributions 543*8d741a5dSApple OSS Distributions sf_notify_func sf_notify; 544*8d741a5dSApple OSS Distributions sf_getpeername_func sf_getpeername; 545*8d741a5dSApple OSS Distributions sf_getsockname_func sf_getsockname; 546*8d741a5dSApple OSS Distributions sf_data_in_func sf_data_in; 547*8d741a5dSApple OSS Distributions sf_data_out_func sf_data_out; 548*8d741a5dSApple OSS Distributions sf_connect_in_func sf_connect_in; 549*8d741a5dSApple OSS Distributions sf_connect_out_func sf_connect_out; 550*8d741a5dSApple OSS Distributions sf_bind_func sf_bind; 551*8d741a5dSApple OSS Distributions sf_setoption_func sf_setoption; 552*8d741a5dSApple OSS Distributions sf_getoption_func sf_getoption; 553*8d741a5dSApple OSS Distributions sf_listen_func sf_listen; 554*8d741a5dSApple OSS Distributions sf_ioctl_func sf_ioctl; 555*8d741a5dSApple OSS Distributions /* 556*8d741a5dSApple OSS Distributions * The following are valid only if SFLT_EXTENDED flag is set. 557*8d741a5dSApple OSS Distributions * Initialize sf_ext_len to sizeof sflt_filter_ext structure. 558*8d741a5dSApple OSS Distributions * Filters must also initialize reserved fields with zeroes. 559*8d741a5dSApple OSS Distributions */ 560*8d741a5dSApple OSS Distributions struct sflt_filter_ext { 561*8d741a5dSApple OSS Distributions unsigned int sf_ext_len; 562*8d741a5dSApple OSS Distributions sf_accept_func sf_ext_accept; 563*8d741a5dSApple OSS Distributions void *sf_ext_rsvd[5]; /* Reserved */ 564*8d741a5dSApple OSS Distributions } sf_ext; 565*8d741a5dSApple OSS Distributions #define sf_len sf_ext.sf_ext_len 566*8d741a5dSApple OSS Distributions #define sf_accept sf_ext.sf_ext_accept 567*8d741a5dSApple OSS Distributions }; 568*8d741a5dSApple OSS Distributions 569*8d741a5dSApple OSS Distributions /*! 570*8d741a5dSApple OSS Distributions * @function sflt_register 571*8d741a5dSApple OSS Distributions * @discussion Registers a socket filter. See 'man 2 socket' for a 572*8d741a5dSApple OSS Distributions * desciption of domain, type, and protocol. 573*8d741a5dSApple OSS Distributions * @param filter A structure describing the filter. 574*8d741a5dSApple OSS Distributions * @param domain The protocol domain these filters will be attached to. 575*8d741a5dSApple OSS Distributions * Only PF_INET & PF_INET6 domains are supported. 576*8d741a5dSApple OSS Distributions * @param type The socket type these filters will be attached to. 577*8d741a5dSApple OSS Distributions * @param protocol The protocol these filters will be attached to. 578*8d741a5dSApple OSS Distributions * @result 0 on success otherwise the errno error. 579*8d741a5dSApple OSS Distributions */ 580*8d741a5dSApple OSS Distributions #ifdef KERNEL_PRIVATE 581*8d741a5dSApple OSS Distributions extern errno_t sflt_register_internal(const struct sflt_filter *filter, 582*8d741a5dSApple OSS Distributions int domain, int type, int protocol); 583*8d741a5dSApple OSS Distributions 584*8d741a5dSApple OSS Distributions #define sflt_register(filter, domain, type, protocol) \ 585*8d741a5dSApple OSS Distributions sflt_register_internal((filter), (domain), (type), (protocol)) 586*8d741a5dSApple OSS Distributions #else 587*8d741a5dSApple OSS Distributions extern errno_t sflt_register(const struct sflt_filter *filter, int domain, 588*8d741a5dSApple OSS Distributions int type, int protocol) 589*8d741a5dSApple OSS Distributions __NKE_API_DEPRECATED; 590*8d741a5dSApple OSS Distributions #endif /* KERNEL_PRIVATE */ 591*8d741a5dSApple OSS Distributions 592*8d741a5dSApple OSS Distributions /*! 593*8d741a5dSApple OSS Distributions * @function sflt_unregister 594*8d741a5dSApple OSS Distributions * @discussion Unregisters a socket filter. This will not detach the 595*8d741a5dSApple OSS Distributions * socket filter from all sockets it may be attached to at the 596*8d741a5dSApple OSS Distributions * time, it will just prevent the socket filter from being attached 597*8d741a5dSApple OSS Distributions * to any new sockets. 598*8d741a5dSApple OSS Distributions * @param handle The sf_handle of the socket filter to unregister. 599*8d741a5dSApple OSS Distributions * @result 0 on success otherwise the errno error. 600*8d741a5dSApple OSS Distributions */ 601*8d741a5dSApple OSS Distributions extern errno_t sflt_unregister(sflt_handle handle) 602*8d741a5dSApple OSS Distributions __NKE_API_DEPRECATED; 603*8d741a5dSApple OSS Distributions 604*8d741a5dSApple OSS Distributions /*! 605*8d741a5dSApple OSS Distributions * @function sflt_attach 606*8d741a5dSApple OSS Distributions * @discussion Attaches a socket filter to the specified socket. A 607*8d741a5dSApple OSS Distributions * filter must be registered before it can be attached. 608*8d741a5dSApple OSS Distributions * @param socket The socket the filter should be attached to. 609*8d741a5dSApple OSS Distributions * @param handle The handle of the registered filter to be attached. 610*8d741a5dSApple OSS Distributions * @result 0 on success otherwise the errno error. 611*8d741a5dSApple OSS Distributions */ 612*8d741a5dSApple OSS Distributions extern errno_t sflt_attach(socket_t socket, sflt_handle handle) 613*8d741a5dSApple OSS Distributions __NKE_API_DEPRECATED; 614*8d741a5dSApple OSS Distributions 615*8d741a5dSApple OSS Distributions /*! 616*8d741a5dSApple OSS Distributions * @function sflt_detach 617*8d741a5dSApple OSS Distributions * @discussion Detaches a socket filter from a specified socket. 618*8d741a5dSApple OSS Distributions * @param socket The socket the filter should be detached from. 619*8d741a5dSApple OSS Distributions * @param handle The handle of the registered filter to be detached. 620*8d741a5dSApple OSS Distributions * @result 0 on success otherwise the errno error. 621*8d741a5dSApple OSS Distributions */ 622*8d741a5dSApple OSS Distributions extern errno_t sflt_detach(socket_t socket, sflt_handle handle) 623*8d741a5dSApple OSS Distributions __NKE_API_DEPRECATED; 624*8d741a5dSApple OSS Distributions 625*8d741a5dSApple OSS Distributions /* Functions for manipulating sockets */ 626*8d741a5dSApple OSS Distributions /* 627*8d741a5dSApple OSS Distributions * Inject data in to the receive buffer of the socket as if it 628*8d741a5dSApple OSS Distributions * had come from the network. 629*8d741a5dSApple OSS Distributions * 630*8d741a5dSApple OSS Distributions * flags should match sflt_data_flag_t 631*8d741a5dSApple OSS Distributions */ 632*8d741a5dSApple OSS Distributions 633*8d741a5dSApple OSS Distributions /*! 634*8d741a5dSApple OSS Distributions * @function sock_inject_data_in 635*8d741a5dSApple OSS Distributions * @discussion Inject data in to the receive buffer of the socket as if 636*8d741a5dSApple OSS Distributions * it had come from the network. 637*8d741a5dSApple OSS Distributions * @param so The socket to inject the data on. 638*8d741a5dSApple OSS Distributions * @param from The address the data is from, only necessary on 639*8d741a5dSApple OSS Distributions * un-connected sockets. A copy of the address will be made, caller 640*8d741a5dSApple OSS Distributions * is responsible for freeing the address after calling this 641*8d741a5dSApple OSS Distributions * function. 642*8d741a5dSApple OSS Distributions * @param data The data and possibly control mbufs. 643*8d741a5dSApple OSS Distributions * @param control The separate control mbufs. 644*8d741a5dSApple OSS Distributions * @param flags Flags indicating the type of data. 645*8d741a5dSApple OSS Distributions * @result 0 on success otherwise the errno error. If the function 646*8d741a5dSApple OSS Distributions * returns an error, the caller is responsible for freeing the 647*8d741a5dSApple OSS Distributions * mbuf. 648*8d741a5dSApple OSS Distributions */ 649*8d741a5dSApple OSS Distributions extern errno_t sock_inject_data_in(socket_t so, const struct sockaddr *from, 650*8d741a5dSApple OSS Distributions mbuf_t data, mbuf_t control, sflt_data_flag_t flags) 651*8d741a5dSApple OSS Distributions __NKE_API_DEPRECATED; 652*8d741a5dSApple OSS Distributions 653*8d741a5dSApple OSS Distributions /*! 654*8d741a5dSApple OSS Distributions * @function sock_inject_data_out 655*8d741a5dSApple OSS Distributions * @discussion Inject data in to the send buffer of the socket as if it 656*8d741a5dSApple OSS Distributions * had come from the client. 657*8d741a5dSApple OSS Distributions * @param so The socket to inject the data on. 658*8d741a5dSApple OSS Distributions * @param to The address the data should be sent to, only necessary on 659*8d741a5dSApple OSS Distributions * un-connected sockets. The caller is responsible for freeing the 660*8d741a5dSApple OSS Distributions * to address after sock_inject_data_out returns. 661*8d741a5dSApple OSS Distributions * @param data The data and possibly control mbufs. 662*8d741a5dSApple OSS Distributions * @param control The separate control mbufs. 663*8d741a5dSApple OSS Distributions * @param flags Flags indicating the type of data. 664*8d741a5dSApple OSS Distributions * @result 0 on success otherwise the errno error. The data and control 665*8d741a5dSApple OSS Distributions * values are always freed regardless of return value. 666*8d741a5dSApple OSS Distributions */ 667*8d741a5dSApple OSS Distributions extern errno_t sock_inject_data_out(socket_t so, const struct sockaddr *to, 668*8d741a5dSApple OSS Distributions mbuf_t data, mbuf_t control, sflt_data_flag_t flags) 669*8d741a5dSApple OSS Distributions __NKE_API_DEPRECATED; 670*8d741a5dSApple OSS Distributions 671*8d741a5dSApple OSS Distributions 672*8d741a5dSApple OSS Distributions /* 673*8d741a5dSApple OSS Distributions * sockopt_t accessors 674*8d741a5dSApple OSS Distributions */ 675*8d741a5dSApple OSS Distributions 676*8d741a5dSApple OSS Distributions enum { 677*8d741a5dSApple OSS Distributions sockopt_get = 1, 678*8d741a5dSApple OSS Distributions sockopt_set = 2 679*8d741a5dSApple OSS Distributions }; 680*8d741a5dSApple OSS Distributions typedef u_int8_t sockopt_dir; 681*8d741a5dSApple OSS Distributions 682*8d741a5dSApple OSS Distributions /*! 683*8d741a5dSApple OSS Distributions * @function sockopt_direction 684*8d741a5dSApple OSS Distributions * @discussion Retrieves the direction of the socket option (Get or 685*8d741a5dSApple OSS Distributions * Set). 686*8d741a5dSApple OSS Distributions * @param sopt The socket option. 687*8d741a5dSApple OSS Distributions * @result sock_opt_get or sock_opt_set. 688*8d741a5dSApple OSS Distributions */ 689*8d741a5dSApple OSS Distributions extern sockopt_dir sockopt_direction(sockopt_t sopt) 690*8d741a5dSApple OSS Distributions __NKE_API_DEPRECATED; 691*8d741a5dSApple OSS Distributions 692*8d741a5dSApple OSS Distributions /*! 693*8d741a5dSApple OSS Distributions * @function sockopt_level 694*8d741a5dSApple OSS Distributions * @discussion Retrieves the socket option level. (SOL_SOCKET, etc). 695*8d741a5dSApple OSS Distributions * @param sopt The socket option. 696*8d741a5dSApple OSS Distributions * @result The socket option level. See man 2 setsockopt 697*8d741a5dSApple OSS Distributions */ 698*8d741a5dSApple OSS Distributions extern int sockopt_level(sockopt_t sopt) 699*8d741a5dSApple OSS Distributions __NKE_API_DEPRECATED; 700*8d741a5dSApple OSS Distributions 701*8d741a5dSApple OSS Distributions /*! 702*8d741a5dSApple OSS Distributions * @function sockopt_name 703*8d741a5dSApple OSS Distributions * @discussion Retrieves the socket option name. (SO_SNDBUF, etc). 704*8d741a5dSApple OSS Distributions * @param sopt The socket option. 705*8d741a5dSApple OSS Distributions * @result The socket option name. See man 2 setsockopt 706*8d741a5dSApple OSS Distributions */ 707*8d741a5dSApple OSS Distributions extern int sockopt_name(sockopt_t sopt) 708*8d741a5dSApple OSS Distributions __NKE_API_DEPRECATED; 709*8d741a5dSApple OSS Distributions 710*8d741a5dSApple OSS Distributions /*! 711*8d741a5dSApple OSS Distributions * @function sockopt_valsize 712*8d741a5dSApple OSS Distributions * @discussion Retrieves the size of the socket option data. 713*8d741a5dSApple OSS Distributions * @param sopt The socket option. 714*8d741a5dSApple OSS Distributions * @result The length, in bytes, of the data. 715*8d741a5dSApple OSS Distributions */ 716*8d741a5dSApple OSS Distributions extern size_t sockopt_valsize(sockopt_t sopt) 717*8d741a5dSApple OSS Distributions __NKE_API_DEPRECATED; 718*8d741a5dSApple OSS Distributions 719*8d741a5dSApple OSS Distributions /*! 720*8d741a5dSApple OSS Distributions * @function sockopt_copyin 721*8d741a5dSApple OSS Distributions * @discussion Copies the data from the socket option to a buffer. 722*8d741a5dSApple OSS Distributions * @param sopt The socket option. 723*8d741a5dSApple OSS Distributions * @param data A pointer to the buffer to copy the data in to. 724*8d741a5dSApple OSS Distributions * @param length The number of bytes to copy. 725*8d741a5dSApple OSS Distributions * @result An errno error or zero upon success. 726*8d741a5dSApple OSS Distributions */ 727*8d741a5dSApple OSS Distributions extern errno_t sockopt_copyin(sockopt_t sopt, void *__sized_by(length) data, size_t length) 728*8d741a5dSApple OSS Distributions __NKE_API_DEPRECATED; 729*8d741a5dSApple OSS Distributions 730*8d741a5dSApple OSS Distributions /*! 731*8d741a5dSApple OSS Distributions * @function sockopt_copyout 732*8d741a5dSApple OSS Distributions * @discussion Copies the data from a buffer to a socket option. 733*8d741a5dSApple OSS Distributions * @param sopt The socket option. 734*8d741a5dSApple OSS Distributions * @param data A pointer to the buffer to copy the data out of. 735*8d741a5dSApple OSS Distributions * @param length The number of bytes to copy. 736*8d741a5dSApple OSS Distributions * @result An errno error or zero upon success. 737*8d741a5dSApple OSS Distributions */ 738*8d741a5dSApple OSS Distributions extern errno_t sockopt_copyout(sockopt_t sopt, void *__sized_by(length) data, size_t length) 739*8d741a5dSApple OSS Distributions __NKE_API_DEPRECATED; 740*8d741a5dSApple OSS Distributions 741*8d741a5dSApple OSS Distributions #undef __NKE_API_DEPRECATED 742*8d741a5dSApple OSS Distributions __END_DECLS 743*8d741a5dSApple OSS Distributions #endif /* __KPI_SOCKETFILTER__ */ 744