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