1*e7776783SApple OSS Distributions /* 2*e7776783SApple OSS Distributions * Copyright (c) 2008-2017 Apple Inc. All rights reserved. 3*e7776783SApple OSS Distributions * 4*e7776783SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ 5*e7776783SApple OSS Distributions * 6*e7776783SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code 7*e7776783SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License 8*e7776783SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in 9*e7776783SApple OSS Distributions * compliance with the License. The rights granted to you under the License 10*e7776783SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of, 11*e7776783SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to 12*e7776783SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any 13*e7776783SApple OSS Distributions * terms of an Apple operating system software license agreement. 14*e7776783SApple OSS Distributions * 15*e7776783SApple OSS Distributions * Please obtain a copy of the License at 16*e7776783SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file. 17*e7776783SApple OSS Distributions * 18*e7776783SApple OSS Distributions * The Original Code and all software distributed under the License are 19*e7776783SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 20*e7776783SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 21*e7776783SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 22*e7776783SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 23*e7776783SApple OSS Distributions * Please see the License for the specific language governing rights and 24*e7776783SApple OSS Distributions * limitations under the License. 25*e7776783SApple OSS Distributions * 26*e7776783SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ 27*e7776783SApple OSS Distributions */ 28*e7776783SApple OSS Distributions /*! 29*e7776783SApple OSS Distributions * @header kpi_socket.h 30*e7776783SApple OSS Distributions * This header defines an API for creating and interacting with sockets 31*e7776783SApple OSS Distributions * in the kernel. It is possible to create sockets in the kernel 32*e7776783SApple OSS Distributions * without an associated file descriptor. In some cases, a reference to 33*e7776783SApple OSS Distributions * the socket may be known while the file descriptor is not. These 34*e7776783SApple OSS Distributions * functions can be used for interacting with sockets in the kernel. 35*e7776783SApple OSS Distributions * The API is similar to the user space socket API. 36*e7776783SApple OSS Distributions */ 37*e7776783SApple OSS Distributions #ifndef __KPI_SOCKET__ 38*e7776783SApple OSS Distributions #define __KPI_SOCKET__ 39*e7776783SApple OSS Distributions 40*e7776783SApple OSS Distributions #include <sys/types.h> 41*e7776783SApple OSS Distributions #include <sys/kernel_types.h> 42*e7776783SApple OSS Distributions #include <sys/socket.h> 43*e7776783SApple OSS Distributions 44*e7776783SApple OSS Distributions #ifndef PRIVATE 45*e7776783SApple OSS Distributions #include <Availability.h> 46*e7776783SApple OSS Distributions #define __NKE_API_DEPRECATED __API_DEPRECATED("Network Kernel Extension KPI is deprecated", macos(10.4, 10.15)) 47*e7776783SApple OSS Distributions #else 48*e7776783SApple OSS Distributions #define __NKE_API_DEPRECATED 49*e7776783SApple OSS Distributions #endif /* PRIVATE */ 50*e7776783SApple OSS Distributions 51*e7776783SApple OSS Distributions __BEGIN_DECLS 52*e7776783SApple OSS Distributions 53*e7776783SApple OSS Distributions struct timeval; 54*e7776783SApple OSS Distributions 55*e7776783SApple OSS Distributions /*! 56*e7776783SApple OSS Distributions * @typedef sock_upcall 57*e7776783SApple OSS Distributions * 58*e7776783SApple OSS Distributions * @discussion sock_upcall is used by a socket to notify an in kernel 59*e7776783SApple OSS Distributions * client that data is waiting. Instead of making blocking calls in 60*e7776783SApple OSS Distributions * the kernel, a client can specify an upcall which will be called 61*e7776783SApple OSS Distributions * when data is available or the socket is ready for sending. 62*e7776783SApple OSS Distributions * 63*e7776783SApple OSS Distributions * Calls to your upcall function are not serialized and may be 64*e7776783SApple OSS Distributions * called concurrently from multiple threads in the kernel. 65*e7776783SApple OSS Distributions * 66*e7776783SApple OSS Distributions * Your upcall function will be called: 67*e7776783SApple OSS Distributions * when there is data more than the low water mark for reading, 68*e7776783SApple OSS Distributions * or when there is space for a write, 69*e7776783SApple OSS Distributions * or when there is a connection to accept, 70*e7776783SApple OSS Distributions * or when a socket is connected, 71*e7776783SApple OSS Distributions * or when a socket is closed or disconnected 72*e7776783SApple OSS Distributions * 73*e7776783SApple OSS Distributions * @param so A reference to the socket that's ready. 74*e7776783SApple OSS Distributions * @param cookie The cookie passed in when the socket was created. 75*e7776783SApple OSS Distributions * @param waitf Indicates whether or not it's safe to block. 76*e7776783SApple OSS Distributions */ 77*e7776783SApple OSS Distributions typedef void (*sock_upcall)(socket_t so, void *cookie, int waitf); 78*e7776783SApple OSS Distributions 79*e7776783SApple OSS Distributions #ifdef KERNEL_PRIVATE 80*e7776783SApple OSS Distributions /*! 81*e7776783SApple OSS Distributions * @typedef sock_evupcall 82*e7776783SApple OSS Distributions * 83*e7776783SApple OSS Distributions * @discussion sock_evupcall is used by a socket to notify an in kernel 84*e7776783SApple OSS Distributions * client when an event occurs. Instead of making blocking calls in 85*e7776783SApple OSS Distributions * the kernel, a client can specify an upcall which will be called 86*e7776783SApple OSS Distributions * when an event status is available. 87*e7776783SApple OSS Distributions * @param so A reference to the socket that's ready. 88*e7776783SApple OSS Distributions * @param cookie The cookie passed in when the socket was created. 89*e7776783SApple OSS Distributions * @param event Indicates the event as defined by SO_FILT_HINT_* 90*e7776783SApple OSS Distributions */ 91*e7776783SApple OSS Distributions typedef void (*sock_evupcall)(socket_t so, void *cookie, long event); 92*e7776783SApple OSS Distributions #endif /* KERNEL_PRIVATE */ 93*e7776783SApple OSS Distributions 94*e7776783SApple OSS Distributions /*! 95*e7776783SApple OSS Distributions * @function sock_accept 96*e7776783SApple OSS Distributions * @discussion Accepts an incoming connection on a socket. See 'man 2 97*e7776783SApple OSS Distributions * accept' for more information. Allocating a socket in this manner 98*e7776783SApple OSS Distributions * creates a socket with no associated file descriptor. 99*e7776783SApple OSS Distributions * @param so The listening socket you'd like to accept a connection on. 100*e7776783SApple OSS Distributions * @param from A pointer to a socket address that will be filled in 101*e7776783SApple OSS Distributions * with the address the connection is from. 102*e7776783SApple OSS Distributions * @param fromlen Maximum length of from. 103*e7776783SApple OSS Distributions * @param flags Supports MSG_DONTWAIT and MSG_USEUPCALL. If 104*e7776783SApple OSS Distributions * MSG_DONTWAIT is set, accept will return EWOULDBLOCK if there are 105*e7776783SApple OSS Distributions * no connections ready to be accepted. If MSG_USEUPCALL is set, 106*e7776783SApple OSS Distributions * the created socket will use the same upcall function attached to 107*e7776783SApple OSS Distributions * the original socket. 108*e7776783SApple OSS Distributions * @param callback A notifier function to be called when an event 109*e7776783SApple OSS Distributions * occurs on the socket. This may be NULL. 110*e7776783SApple OSS Distributions * @param cookie A cookie passed directly to the callback. 111*e7776783SApple OSS Distributions * @param new_so Upon success, *new_so will be a reference to a new 112*e7776783SApple OSS Distributions * socket for tracking the connection. 113*e7776783SApple OSS Distributions * @result 0 on success otherwise the errno error. 114*e7776783SApple OSS Distributions */ 115*e7776783SApple OSS Distributions #ifdef KERNEL_PRIVATE 116*e7776783SApple OSS Distributions extern errno_t sock_accept_internal(socket_t so, struct sockaddr *from, int fromlen, 117*e7776783SApple OSS Distributions int flags, sock_upcall callback, void *cookie, socket_t *new_so); 118*e7776783SApple OSS Distributions 119*e7776783SApple OSS Distributions #define sock_accept(so, from, fromlen, flags, callback, cookie, new_so) \ 120*e7776783SApple OSS Distributions sock_accept_internal((so), (from), (fromlen), (flags), (callback), \ 121*e7776783SApple OSS Distributions (cookie), (new_so)) 122*e7776783SApple OSS Distributions #else 123*e7776783SApple OSS Distributions extern errno_t sock_accept(socket_t so, struct sockaddr *from, int fromlen, 124*e7776783SApple OSS Distributions int flags, sock_upcall callback, void *cookie, socket_t *new_so) 125*e7776783SApple OSS Distributions __NKE_API_DEPRECATED; 126*e7776783SApple OSS Distributions #endif /* KERNEL_PRIVATE */ 127*e7776783SApple OSS Distributions 128*e7776783SApple OSS Distributions /*! 129*e7776783SApple OSS Distributions * @function sock_bind 130*e7776783SApple OSS Distributions * @discussion Binds a socket to a specific address. See 'man 2 bind' 131*e7776783SApple OSS Distributions * for more information. 132*e7776783SApple OSS Distributions * @param so The socket to be bound. 133*e7776783SApple OSS Distributions * @param to The local address the socket should be bound to. 134*e7776783SApple OSS Distributions * @result 0 on success otherwise the errno error. 135*e7776783SApple OSS Distributions */ 136*e7776783SApple OSS Distributions extern errno_t sock_bind(socket_t so, const struct sockaddr *to) 137*e7776783SApple OSS Distributions __NKE_API_DEPRECATED; 138*e7776783SApple OSS Distributions 139*e7776783SApple OSS Distributions /*! 140*e7776783SApple OSS Distributions * @function sock_connect 141*e7776783SApple OSS Distributions * @discussion Initiates a connection on the socket. See 'man 2 142*e7776783SApple OSS Distributions * connect' for more information. 143*e7776783SApple OSS Distributions * @param so The socket to be connect. 144*e7776783SApple OSS Distributions * @param to The remote address the socket should connect to. 145*e7776783SApple OSS Distributions * @param flags Flags for connecting. The only flag supported so far is 146*e7776783SApple OSS Distributions * MSG_DONTWAIT. MSG_DONTWAIT will perform a non-blocking connect. 147*e7776783SApple OSS Distributions * sock_connect will return immediately with EINPROGRESS. The 148*e7776783SApple OSS Distributions * upcall, if supplied, will be called when the connection is 149*e7776783SApple OSS Distributions * completed. 150*e7776783SApple OSS Distributions * @result 0 on success, EINPROGRESS for a non-blocking connect that 151*e7776783SApple OSS Distributions * has not completed, otherwise the errno error. 152*e7776783SApple OSS Distributions */ 153*e7776783SApple OSS Distributions extern errno_t sock_connect(socket_t so, const struct sockaddr *to, int flags) 154*e7776783SApple OSS Distributions __NKE_API_DEPRECATED; 155*e7776783SApple OSS Distributions 156*e7776783SApple OSS Distributions #ifdef KERNEL_PRIVATE 157*e7776783SApple OSS Distributions /* 158*e7776783SApple OSS Distributions * This function was added to support NFS. NFS does something funny, 159*e7776783SApple OSS Distributions * setting a short timeout and checking to see if it should abort the 160*e7776783SApple OSS Distributions * connect every two seconds. Ideally, NFS would use the upcall to be 161*e7776783SApple OSS Distributions * notified when the connect is complete. 162*e7776783SApple OSS Distributions * 163*e7776783SApple OSS Distributions * If you feel you need to use this function, please contact us to 164*e7776783SApple OSS Distributions * explain why. 165*e7776783SApple OSS Distributions * 166*e7776783SApple OSS Distributions * @function sock_connectwait 167*e7776783SApple OSS Distributions * @discussion Allows a caller to wait on a socket connect. 168*e7776783SApple OSS Distributions * @param so The socket being connected. 169*e7776783SApple OSS Distributions * @param tv The amount of time to wait. 170*e7776783SApple OSS Distributions * @result 0 on success otherwise the errno error. EINPROGRESS will be 171*e7776783SApple OSS Distributions * returned if the connection did not complete in the timeout 172*e7776783SApple OSS Distributions * specified. 173*e7776783SApple OSS Distributions */ 174*e7776783SApple OSS Distributions extern errno_t sock_connectwait(socket_t so, const struct timeval *tv); 175*e7776783SApple OSS Distributions #endif /* KERNEL_PRIVATE */ 176*e7776783SApple OSS Distributions 177*e7776783SApple OSS Distributions /*! 178*e7776783SApple OSS Distributions * @function sock_getpeername 179*e7776783SApple OSS Distributions * @discussion Retrieves the remote address of a connected socket. See 180*e7776783SApple OSS Distributions * 'man 2 getpeername'. 181*e7776783SApple OSS Distributions * @param so The socket. 182*e7776783SApple OSS Distributions * @param peername Storage for the peer name. 183*e7776783SApple OSS Distributions * @param peernamelen Length of storage for the peer name. 184*e7776783SApple OSS Distributions * @result 0 on success otherwise the errno error. 185*e7776783SApple OSS Distributions */ 186*e7776783SApple OSS Distributions extern errno_t sock_getpeername(socket_t so, struct sockaddr *peername, 187*e7776783SApple OSS Distributions int peernamelen) 188*e7776783SApple OSS Distributions __NKE_API_DEPRECATED; 189*e7776783SApple OSS Distributions 190*e7776783SApple OSS Distributions /*! 191*e7776783SApple OSS Distributions * @function sock_getsockname 192*e7776783SApple OSS Distributions * @discussion Retrieves the local address of a socket. See 'man 2 193*e7776783SApple OSS Distributions * getsockname'. 194*e7776783SApple OSS Distributions * @param so The socket. 195*e7776783SApple OSS Distributions * @param sockname Storage for the local name. 196*e7776783SApple OSS Distributions * @param socknamelen Length of storage for the socket name. 197*e7776783SApple OSS Distributions * @result 0 on success otherwise the errno error. 198*e7776783SApple OSS Distributions */ 199*e7776783SApple OSS Distributions extern errno_t sock_getsockname(socket_t so, struct sockaddr *sockname, 200*e7776783SApple OSS Distributions int socknamelen) 201*e7776783SApple OSS Distributions __NKE_API_DEPRECATED; 202*e7776783SApple OSS Distributions 203*e7776783SApple OSS Distributions /*! 204*e7776783SApple OSS Distributions * @function sock_getsockopt 205*e7776783SApple OSS Distributions * @discussion Retrieves a socket option. See 'man 2 getsockopt'. 206*e7776783SApple OSS Distributions * @param so The socket. 207*e7776783SApple OSS Distributions * @param level Level of the socket option. 208*e7776783SApple OSS Distributions * @param optname The option name. 209*e7776783SApple OSS Distributions * @param optval The option value. 210*e7776783SApple OSS Distributions * @param optlen The length of optval, returns the actual length. 211*e7776783SApple OSS Distributions * @result 0 on success otherwise the errno error. 212*e7776783SApple OSS Distributions */ 213*e7776783SApple OSS Distributions extern errno_t sock_getsockopt(socket_t so, int level, int optname, 214*e7776783SApple OSS Distributions void *optval, int *optlen) 215*e7776783SApple OSS Distributions __NKE_API_DEPRECATED; 216*e7776783SApple OSS Distributions 217*e7776783SApple OSS Distributions /*! 218*e7776783SApple OSS Distributions * @function sock_ioctl 219*e7776783SApple OSS Distributions * @discussion Performs an ioctl operation on a socket. See 'man 2 ioctl'. 220*e7776783SApple OSS Distributions * @param so The socket. 221*e7776783SApple OSS Distributions * @param request The ioctl name. 222*e7776783SApple OSS Distributions * @param argp The argument. 223*e7776783SApple OSS Distributions * @result 0 on success otherwise the errno error. 224*e7776783SApple OSS Distributions */ 225*e7776783SApple OSS Distributions extern errno_t sock_ioctl(socket_t so, unsigned long request, void *argp) 226*e7776783SApple OSS Distributions __NKE_API_DEPRECATED; 227*e7776783SApple OSS Distributions 228*e7776783SApple OSS Distributions /*! 229*e7776783SApple OSS Distributions * @function sock_setsockopt 230*e7776783SApple OSS Distributions * @discussion Sets a socket option. See 'man 2 setsockopt'. 231*e7776783SApple OSS Distributions * @param so The socket. 232*e7776783SApple OSS Distributions * @param level Level of the socket option. 233*e7776783SApple OSS Distributions * @param optname The option name. 234*e7776783SApple OSS Distributions * @param optval The option value. 235*e7776783SApple OSS Distributions * @param optlen The length of optval. 236*e7776783SApple OSS Distributions * @result 0 on success otherwise the errno error. 237*e7776783SApple OSS Distributions */ 238*e7776783SApple OSS Distributions extern errno_t sock_setsockopt(socket_t so, int level, int optname, 239*e7776783SApple OSS Distributions const void *optval, int optlen) 240*e7776783SApple OSS Distributions __NKE_API_DEPRECATED; 241*e7776783SApple OSS Distributions 242*e7776783SApple OSS Distributions #ifdef KERNEL_PRIVATE 243*e7776783SApple OSS Distributions /* 244*e7776783SApple OSS Distributions * This function was added to support AFP setting the traffic class 245*e7776783SApple OSS Distributions * for a backup stream within a wireless LAN or over link-local address. 246*e7776783SApple OSS Distributions * 247*e7776783SApple OSS Distributions * If you feel you need to use this function, please contact us to 248*e7776783SApple OSS Distributions * explain why. 249*e7776783SApple OSS Distributions * 250*e7776783SApple OSS Distributions * @function sock_settclassopt 251*e7776783SApple OSS Distributions * @discussion Allows a caller to set the traffic class. 252*e7776783SApple OSS Distributions * @param so The socket. 253*e7776783SApple OSS Distributions * @param optval The option value. 254*e7776783SApple OSS Distributions * @param optlen The length of optval. 255*e7776783SApple OSS Distributions * @result 0 on success otherwise the errno error. 256*e7776783SApple OSS Distributions */ 257*e7776783SApple OSS Distributions extern errno_t sock_settclassopt(socket_t so, const void* optval, size_t optlen); 258*e7776783SApple OSS Distributions 259*e7776783SApple OSS Distributions /* 260*e7776783SApple OSS Distributions * This function was added to support AFP getting the traffic class 261*e7776783SApple OSS Distributions * set on a stream. 262*e7776783SApple OSS Distributions * 263*e7776783SApple OSS Distributions * This is also a private API, please contact us if you need to use it. 264*e7776783SApple OSS Distributions * 265*e7776783SApple OSS Distributions * @function sockgettclassopt 266*e7776783SApple OSS Distributions * @discussion Allows a caller to get the traffic class. 267*e7776783SApple OSS Distributions * @param so The socket. 268*e7776783SApple OSS Distributions * @param optval The option value. 269*e7776783SApple OSS Distributions * @param optlen The length of optval, returns the actual length. 270*e7776783SApple OSS Distributions * @result 0 on success otherwise the errno error. 271*e7776783SApple OSS Distributions */ 272*e7776783SApple OSS Distributions extern errno_t sock_gettclassopt(socket_t so, void* optval, size_t* optlen); 273*e7776783SApple OSS Distributions 274*e7776783SApple OSS Distributions #ifdef XNU_KERNEL_PRIVATE 275*e7776783SApple OSS Distributions extern void socket_set_traffic_mgt_flags_locked(socket_t so, u_int8_t flags); 276*e7776783SApple OSS Distributions extern void socket_clear_traffic_mgt_flags_locked(socket_t so, u_int8_t flags); 277*e7776783SApple OSS Distributions #endif /* XNU_KERNEL_PRIVATE */ 278*e7776783SApple OSS Distributions #ifdef BSD_KERNEL_PRIVATE 279*e7776783SApple OSS Distributions extern void socket_set_traffic_mgt_flags(socket_t so, u_int8_t flags); 280*e7776783SApple OSS Distributions extern void socket_clear_traffic_mgt_flags(socket_t so, u_int8_t flags); 281*e7776783SApple OSS Distributions extern errno_t socket_defunct(struct proc *, socket_t so, int); 282*e7776783SApple OSS Distributions extern errno_t sock_receive_internal(socket_t, struct msghdr *, mbuf_t *, 283*e7776783SApple OSS Distributions int, size_t *); 284*e7776783SApple OSS Distributions #endif /* BSD_KERNEL_PRIVATE */ 285*e7776783SApple OSS Distributions #endif /* KERNEL_PRIVATE */ 286*e7776783SApple OSS Distributions 287*e7776783SApple OSS Distributions /*! 288*e7776783SApple OSS Distributions * @function sock_listen 289*e7776783SApple OSS Distributions * @discussion Indicate that the socket should start accepting incoming 290*e7776783SApple OSS Distributions * connections. See 'man 2 listen'. 291*e7776783SApple OSS Distributions * @param so The socket. 292*e7776783SApple OSS Distributions * @param backlog The maximum length of the queue of pending connections. 293*e7776783SApple OSS Distributions * @result 0 on success otherwise the errno error. 294*e7776783SApple OSS Distributions */ 295*e7776783SApple OSS Distributions extern errno_t sock_listen(socket_t so, int backlog) 296*e7776783SApple OSS Distributions __NKE_API_DEPRECATED; 297*e7776783SApple OSS Distributions 298*e7776783SApple OSS Distributions /*! 299*e7776783SApple OSS Distributions * @function sock_receive 300*e7776783SApple OSS Distributions * @discussion Receive data from a socket. Similar to recvmsg. See 'man 301*e7776783SApple OSS Distributions * 2 recvmsg' for more information about receiving data. 302*e7776783SApple OSS Distributions * @param so The socket. 303*e7776783SApple OSS Distributions * @param msg The msg describing how the data should be received. 304*e7776783SApple OSS Distributions * @param flags See 'man 2 recvmsg'. 305*e7776783SApple OSS Distributions * @param recvdlen Number of bytes received, same as return value of 306*e7776783SApple OSS Distributions * userland recvmsg. 307*e7776783SApple OSS Distributions * @result 0 on success, EWOULDBLOCK if non-blocking and operation 308*e7776783SApple OSS Distributions * would cause the thread to block, otherwise the errno error. 309*e7776783SApple OSS Distributions */ 310*e7776783SApple OSS Distributions extern errno_t sock_receive(socket_t so, struct msghdr *msg, int flags, 311*e7776783SApple OSS Distributions size_t *recvdlen) 312*e7776783SApple OSS Distributions __NKE_API_DEPRECATED; 313*e7776783SApple OSS Distributions 314*e7776783SApple OSS Distributions /*! 315*e7776783SApple OSS Distributions * @function sock_receivembuf 316*e7776783SApple OSS Distributions * @discussion Receive data from a socket. Similar to sock_receive 317*e7776783SApple OSS Distributions * though data is returned as a chain of mbufs. See 'man 2 recvmsg' 318*e7776783SApple OSS Distributions * for more information about receiving data. 319*e7776783SApple OSS Distributions * @param so The socket. 320*e7776783SApple OSS Distributions * @param msg The msg describing how the data should be received. May 321*e7776783SApple OSS Distributions * be NULL. The msg_iov is ignored. 322*e7776783SApple OSS Distributions * @param data Upon return *data will be a reference to an mbuf chain 323*e7776783SApple OSS Distributions * containing the data received. This eliminates copying the data 324*e7776783SApple OSS Distributions * out of the mbufs. Caller is responsible for freeing the mbufs. 325*e7776783SApple OSS Distributions * @param flags See 'man 2 recvmsg'. 326*e7776783SApple OSS Distributions * @param recvlen Maximum number of bytes to receive in the mbuf chain. 327*e7776783SApple OSS Distributions * Upon return, this value will be set to the number of bytes 328*e7776783SApple OSS Distributions * received, same as return value of userland recvmsg. 329*e7776783SApple OSS Distributions * @result 0 on success, EWOULDBLOCK if non-blocking and operation 330*e7776783SApple OSS Distributions * would cause the thread to block, otherwise the errno error. 331*e7776783SApple OSS Distributions */ 332*e7776783SApple OSS Distributions extern errno_t sock_receivembuf(socket_t so, struct msghdr *msg, mbuf_t *data, 333*e7776783SApple OSS Distributions int flags, size_t *recvlen) 334*e7776783SApple OSS Distributions __NKE_API_DEPRECATED; 335*e7776783SApple OSS Distributions 336*e7776783SApple OSS Distributions /*! 337*e7776783SApple OSS Distributions * @function sock_send 338*e7776783SApple OSS Distributions * @discussion Send data on a socket. Similar to sendmsg. See 'man 2 339*e7776783SApple OSS Distributions * sendmsg' for more information about sending data. 340*e7776783SApple OSS Distributions * @param so The socket. 341*e7776783SApple OSS Distributions * @param msg The msg describing how the data should be sent. Any 342*e7776783SApple OSS Distributions * pointers must point to data in the kernel. 343*e7776783SApple OSS Distributions * @param flags See 'man 2 sendmsg'. 344*e7776783SApple OSS Distributions * @param sentlen The number of bytes sent. 345*e7776783SApple OSS Distributions * @result 0 on success, EWOULDBLOCK if non-blocking and operation 346*e7776783SApple OSS Distributions * would cause the thread to block, otherwise the errno error. 347*e7776783SApple OSS Distributions */ 348*e7776783SApple OSS Distributions extern errno_t sock_send(socket_t so, const struct msghdr *msg, int flags, 349*e7776783SApple OSS Distributions size_t *sentlen) 350*e7776783SApple OSS Distributions __NKE_API_DEPRECATED; 351*e7776783SApple OSS Distributions 352*e7776783SApple OSS Distributions /*! 353*e7776783SApple OSS Distributions * @function sock_sendmbuf 354*e7776783SApple OSS Distributions * @discussion Send data in an mbuf on a socket. Similar to sock_send 355*e7776783SApple OSS Distributions * only the data to be sent is taken from the mbuf chain. 356*e7776783SApple OSS Distributions * @param so The socket. 357*e7776783SApple OSS Distributions * @param msg The msg describing how the data should be sent. The 358*e7776783SApple OSS Distributions * msg_iov is ignored. msg may be NULL. 359*e7776783SApple OSS Distributions * @param data The mbuf chain of data to send. 360*e7776783SApple OSS Distributions * @param flags See 'man 2 sendmsg'. 361*e7776783SApple OSS Distributions * @param sentlen The number of bytes sent. 362*e7776783SApple OSS Distributions * @result 0 on success, EWOULDBLOCK if non-blocking and operation 363*e7776783SApple OSS Distributions * would cause the thread to block, otherwise the errno error. 364*e7776783SApple OSS Distributions * Regardless of return value, the mbuf chain 'data' will be freed. 365*e7776783SApple OSS Distributions */ 366*e7776783SApple OSS Distributions extern errno_t sock_sendmbuf(socket_t so, const struct msghdr *msg, mbuf_t data, 367*e7776783SApple OSS Distributions int flags, size_t *sentlen) 368*e7776783SApple OSS Distributions __NKE_API_DEPRECATED; 369*e7776783SApple OSS Distributions 370*e7776783SApple OSS Distributions /*! 371*e7776783SApple OSS Distributions * @function sock_shutdown 372*e7776783SApple OSS Distributions * @discussion Shutdown one or both directions of a connection. See 373*e7776783SApple OSS Distributions * 'man 2 shutdown' for more information. 374*e7776783SApple OSS Distributions * @param so The socket. 375*e7776783SApple OSS Distributions * @param how SHUT_RD - shutdown receive. 376*e7776783SApple OSS Distributions * SHUT_WR - shutdown send. 377*e7776783SApple OSS Distributions * SHUT_RDWR - shutdown both. 378*e7776783SApple OSS Distributions * @result 0 on success otherwise the errno error. 379*e7776783SApple OSS Distributions */ 380*e7776783SApple OSS Distributions extern errno_t sock_shutdown(socket_t so, int how) 381*e7776783SApple OSS Distributions __NKE_API_DEPRECATED; 382*e7776783SApple OSS Distributions 383*e7776783SApple OSS Distributions /*! 384*e7776783SApple OSS Distributions * @function sock_socket 385*e7776783SApple OSS Distributions * @discussion Allocate a socket. Allocating a socket in this manner 386*e7776783SApple OSS Distributions * creates a socket with no associated file descriptor. For more 387*e7776783SApple OSS Distributions * information, see 'man 2 socket'. 388*e7776783SApple OSS Distributions * @param domain The socket domain (PF_INET, etc...). 389*e7776783SApple OSS Distributions * @param type The socket type (SOCK_STREAM, SOCK_DGRAM, etc...). 390*e7776783SApple OSS Distributions * @param protocol The socket protocol. 391*e7776783SApple OSS Distributions * @param callback A notifier function to be called when an event 392*e7776783SApple OSS Distributions * occurs on the socket. This may be NULL. 393*e7776783SApple OSS Distributions * @param cookie A cookie passed directly to the callback. 394*e7776783SApple OSS Distributions * @param new_so Upon success, a reference to the new socket. 395*e7776783SApple OSS Distributions * @result 0 on success otherwise the errno error. 396*e7776783SApple OSS Distributions */ 397*e7776783SApple OSS Distributions #ifdef KERNEL_PRIVATE 398*e7776783SApple OSS Distributions extern errno_t sock_socket_internal(int domain, int type, int protocol, 399*e7776783SApple OSS Distributions sock_upcall callback, void *cookie, socket_t *new_so); 400*e7776783SApple OSS Distributions 401*e7776783SApple OSS Distributions #define sock_socket(domain, type, protocol, callback, cookie, new_so) \ 402*e7776783SApple OSS Distributions sock_socket_internal((domain), (type), (protocol), \ 403*e7776783SApple OSS Distributions (callback), (cookie), (new_so)) 404*e7776783SApple OSS Distributions #else 405*e7776783SApple OSS Distributions extern errno_t sock_socket(int domain, int type, int protocol, 406*e7776783SApple OSS Distributions sock_upcall callback, void *cookie, socket_t *new_so) 407*e7776783SApple OSS Distributions __NKE_API_DEPRECATED; 408*e7776783SApple OSS Distributions #endif /* KERNEL_PRIVATE */ 409*e7776783SApple OSS Distributions 410*e7776783SApple OSS Distributions /*! 411*e7776783SApple OSS Distributions * @function sock_close 412*e7776783SApple OSS Distributions * @discussion Close the socket. 413*e7776783SApple OSS Distributions * @param so The socket to close. This should only ever be a socket 414*e7776783SApple OSS Distributions * created with sock_socket. Closing a socket created in user space 415*e7776783SApple OSS Distributions * using sock_close may leave a file descriptor pointing to the 416*e7776783SApple OSS Distributions * closed socket, resulting in undefined behavior. 417*e7776783SApple OSS Distributions */ 418*e7776783SApple OSS Distributions extern void sock_close(socket_t so) 419*e7776783SApple OSS Distributions __NKE_API_DEPRECATED; 420*e7776783SApple OSS Distributions 421*e7776783SApple OSS Distributions /* 422*e7776783SApple OSS Distributions * @function sock_retain 423*e7776783SApple OSS Distributions * @discussion Prevents the socket from closing 424*e7776783SApple OSS Distributions * @param so The socket to close. Increment a retain count on the 425*e7776783SApple OSS Distributions * socket, preventing it from being closed when sock_close is 426*e7776783SApple OSS Distributions * called. This is used when a File Descriptor is passed (and 427*e7776783SApple OSS Distributions * closed) from userland and the kext wants to keep ownership of 428*e7776783SApple OSS Distributions * that socket. It is used in conjunction with 429*e7776783SApple OSS Distributions * sock_release(socket_t so). 430*e7776783SApple OSS Distributions */ 431*e7776783SApple OSS Distributions extern void sock_retain(socket_t so) 432*e7776783SApple OSS Distributions __NKE_API_DEPRECATED; 433*e7776783SApple OSS Distributions 434*e7776783SApple OSS Distributions /* 435*e7776783SApple OSS Distributions * @function sock_release 436*e7776783SApple OSS Distributions * @discussion Decrement the retain count and close the socket if the 437*e7776783SApple OSS Distributions * retain count reaches zero. 438*e7776783SApple OSS Distributions * @param so The socket to release. This is used to release ownership 439*e7776783SApple OSS Distributions * on a socket acquired with sock_retain. When the last retain 440*e7776783SApple OSS Distributions * count is reached, this will call sock_close to close the socket. 441*e7776783SApple OSS Distributions */ 442*e7776783SApple OSS Distributions extern void sock_release(socket_t so) 443*e7776783SApple OSS Distributions __NKE_API_DEPRECATED; 444*e7776783SApple OSS Distributions 445*e7776783SApple OSS Distributions /*! 446*e7776783SApple OSS Distributions * @function sock_setpriv 447*e7776783SApple OSS Distributions * @discussion Set the privileged bit in the socket. Allows for 448*e7776783SApple OSS Distributions * operations that require root privileges. 449*e7776783SApple OSS Distributions * @param so The socket on which to modify the SS_PRIV flag. 450*e7776783SApple OSS Distributions * @param on Indicate whether or not the SS_PRIV flag should be set. 451*e7776783SApple OSS Distributions * @result 0 on success otherwise the errno error. 452*e7776783SApple OSS Distributions */ 453*e7776783SApple OSS Distributions extern errno_t sock_setpriv(socket_t so, int on) 454*e7776783SApple OSS Distributions __NKE_API_DEPRECATED; 455*e7776783SApple OSS Distributions 456*e7776783SApple OSS Distributions /*! 457*e7776783SApple OSS Distributions * @function sock_isconnected 458*e7776783SApple OSS Distributions * @discussion Returns whether or not the socket is connected. 459*e7776783SApple OSS Distributions * @param so The socket to check. 460*e7776783SApple OSS Distributions * @result 0 - socket is not connected. 1 - socket is connected. 461*e7776783SApple OSS Distributions */ 462*e7776783SApple OSS Distributions extern int sock_isconnected(socket_t so) 463*e7776783SApple OSS Distributions __NKE_API_DEPRECATED; 464*e7776783SApple OSS Distributions 465*e7776783SApple OSS Distributions /*! 466*e7776783SApple OSS Distributions * @function sock_isnonblocking 467*e7776783SApple OSS Distributions * @discussion Returns whether or not the socket is non-blocking. In 468*e7776783SApple OSS Distributions * the context of this KPI, non-blocking means that functions to 469*e7776783SApple OSS Distributions * perform operations on a socket will not wait for completion. 470*e7776783SApple OSS Distributions * 471*e7776783SApple OSS Distributions * To enable or disable blocking, use the FIONBIO ioctl. The 472*e7776783SApple OSS Distributions * parameter is an int. If the int is zero, the socket will block. 473*e7776783SApple OSS Distributions * If the parameter is non-zero, the socket will not block. 474*e7776783SApple OSS Distributions * @result 0 - socket will block. 1 - socket will not block. 475*e7776783SApple OSS Distributions */ 476*e7776783SApple OSS Distributions extern int sock_isnonblocking(socket_t so) 477*e7776783SApple OSS Distributions __NKE_API_DEPRECATED; 478*e7776783SApple OSS Distributions 479*e7776783SApple OSS Distributions /*! 480*e7776783SApple OSS Distributions * @function sock_gettype 481*e7776783SApple OSS Distributions * @discussion Retrieves information about the socket. This is the same 482*e7776783SApple OSS Distributions * information that was used to create the socket. If any of the 483*e7776783SApple OSS Distributions * parameters following so are NULL, that information is not 484*e7776783SApple OSS Distributions * retrieved. 485*e7776783SApple OSS Distributions * @param so The socket to check. 486*e7776783SApple OSS Distributions * @param domain The domain of the socket (PF_INET, ...). May be NULL. 487*e7776783SApple OSS Distributions * @param type The socket type (SOCK_STREAM, SOCK_DGRAM, ...). May be NULL. 488*e7776783SApple OSS Distributions * @param protocol The socket protocol. May be NULL. 489*e7776783SApple OSS Distributions * @result 0 on success otherwise the errno error. 490*e7776783SApple OSS Distributions */ 491*e7776783SApple OSS Distributions extern errno_t sock_gettype(socket_t so, int *domain, int *type, int *protocol) 492*e7776783SApple OSS Distributions __NKE_API_DEPRECATED; 493*e7776783SApple OSS Distributions 494*e7776783SApple OSS Distributions #ifdef KERNEL_PRIVATE 495*e7776783SApple OSS Distributions /* 496*e7776783SApple OSS Distributions * @function sock_nointerrupt 497*e7776783SApple OSS Distributions * @discussion Disables interrupt on socket buffers (sets SB_NOINTR on 498*e7776783SApple OSS Distributions * send and receive socket buffers). 499*e7776783SApple OSS Distributions * @param so The socket to modify. 500*e7776783SApple OSS Distributions * @param on Indicate whether or not the SB_NOINTR flag should be set. 501*e7776783SApple OSS Distributions * @result 0 on success otherwise the errno error. 502*e7776783SApple OSS Distributions */ 503*e7776783SApple OSS Distributions extern errno_t sock_nointerrupt(socket_t so, int on); 504*e7776783SApple OSS Distributions 505*e7776783SApple OSS Distributions /* 506*e7776783SApple OSS Distributions * @function sock_getlistener 507*e7776783SApple OSS Distributions * @discussion Retrieves the listening socket of a pre-accepted socket, 508*e7776783SApple OSS Distributions * i.e. a socket which is still in the incomplete/completed list. 509*e7776783SApple OSS Distributions * Once a socket has been accepted, the information pertaining 510*e7776783SApple OSS Distributions * to its listener is no longer available. Therefore, modules 511*e7776783SApple OSS Distributions * interested in finding out the listening socket should install 512*e7776783SApple OSS Distributions * the appropriate socket filter callback (sf_attach) which gets 513*e7776783SApple OSS Distributions * invoked prior to the socket being fully accepted, and call 514*e7776783SApple OSS Distributions * this routine at such a time to obtain the listener. Callers 515*e7776783SApple OSS Distributions * are guaranteed that the listener socket will not go away 516*e7776783SApple OSS Distributions * during the sf_attach callback, and therefore the value is 517*e7776783SApple OSS Distributions * safe to be used only in that callback context. Callers should 518*e7776783SApple OSS Distributions * therefore take note that the listening socket's lock will be 519*e7776783SApple OSS Distributions * held throughout the duration of the callback. 520*e7776783SApple OSS Distributions * @param so The pre-accepted socket. 521*e7776783SApple OSS Distributions * @result Non-NULL value which indicates the listening socket; otherwise, 522*e7776783SApple OSS Distributions * NULL if the socket is not in the incomplete/completed list 523*e7776783SApple OSS Distributions * of a listener. 524*e7776783SApple OSS Distributions */ 525*e7776783SApple OSS Distributions extern socket_t sock_getlistener(socket_t so); 526*e7776783SApple OSS Distributions 527*e7776783SApple OSS Distributions /* 528*e7776783SApple OSS Distributions * @function sock_getaddr 529*e7776783SApple OSS Distributions * @discussion Retrieves the local or remote address of a socket. 530*e7776783SApple OSS Distributions * This is a composite of sock_getpeername and sock_getsockname, 531*e7776783SApple OSS Distributions * except that the allocated socket address is returned to the 532*e7776783SApple OSS Distributions * caller, and that the caller is reponsible for calling 533*e7776783SApple OSS Distributions * sock_freeaddr once finished with it. 534*e7776783SApple OSS Distributions * @param so The socket. 535*e7776783SApple OSS Distributions * @param psockname Pointer to the storage for the socket name. 536*e7776783SApple OSS Distributions * @param peername 0 for local address, and non-zero for peer address. 537*e7776783SApple OSS Distributions * @result 0 on success otherwise the errno error. 538*e7776783SApple OSS Distributions */ 539*e7776783SApple OSS Distributions extern errno_t sock_getaddr(socket_t so, struct sockaddr **psockname, 540*e7776783SApple OSS Distributions int peername); 541*e7776783SApple OSS Distributions 542*e7776783SApple OSS Distributions /* 543*e7776783SApple OSS Distributions * @function sock_freeaddr 544*e7776783SApple OSS Distributions * @discussion Frees the socket address allocated by sock_getaddr. 545*e7776783SApple OSS Distributions * @param sockname The socket name to be freed. 546*e7776783SApple OSS Distributions */ 547*e7776783SApple OSS Distributions extern void sock_freeaddr(struct sockaddr *sockname); 548*e7776783SApple OSS Distributions 549*e7776783SApple OSS Distributions /* 550*e7776783SApple OSS Distributions * @function sock_setupcall 551*e7776783SApple OSS Distributions * @discussion Set the notifier function to be called when an event 552*e7776783SApple OSS Distributions * occurs on the socket. This may be set to NULL to disable 553*e7776783SApple OSS Distributions * further notifications. Setting the function does not 554*e7776783SApple OSS Distributions * affect currently notifications about to be sent or being sent. 555*e7776783SApple OSS Distributions * Note: When this function is used on a socket passed from 556*e7776783SApple OSS Distributions * userspace it is crucial to call sock_retain() on the socket 557*e7776783SApple OSS Distributions * otherwise a callback could be dispatched on a closed socket 558*e7776783SApple OSS Distributions * and cause a crash. 559*e7776783SApple OSS Distributions * @param sock The socket. 560*e7776783SApple OSS Distributions * @param callback The notifier function 561*e7776783SApple OSS Distributions * @param context A cookie passed directly to the callback 562*e7776783SApple OSS Distributions */ 563*e7776783SApple OSS Distributions extern errno_t sock_setupcall(socket_t sock, sock_upcall callback, 564*e7776783SApple OSS Distributions void *context); 565*e7776783SApple OSS Distributions 566*e7776783SApple OSS Distributions /* 567*e7776783SApple OSS Distributions * @function sock_setupcalls 568*e7776783SApple OSS Distributions * @discussion Set the notifier function to be called when an event 569*e7776783SApple OSS Distributions * occurs on the socket. This may be set to NULL to disable 570*e7776783SApple OSS Distributions * further notifications. Setting the function does not 571*e7776783SApple OSS Distributions * affect currently notifications about to be sent or being sent. 572*e7776783SApple OSS Distributions * Note: When this function is used on a socket passed from 573*e7776783SApple OSS Distributions * userspace it is crucial to call sock_retain() on the socket 574*e7776783SApple OSS Distributions * otherwise a callback could be dispatched on a closed socket 575*e7776783SApple OSS Distributions * and cause a crash. 576*e7776783SApple OSS Distributions * @param sock The socket. 577*e7776783SApple OSS Distributions * @param read_callback The read notifier function 578*e7776783SApple OSS Distributions * @param read_context A cookie passed directly to the read callback 579*e7776783SApple OSS Distributions * @param write_callback The write notifier function 580*e7776783SApple OSS Distributions * @param write_context A cookie passed directly to the write callback 581*e7776783SApple OSS Distributions */ 582*e7776783SApple OSS Distributions extern errno_t sock_setupcalls(socket_t sock, sock_upcall read_callback, 583*e7776783SApple OSS Distributions void *read_context, sock_upcall write_callback, void *write_context); 584*e7776783SApple OSS Distributions 585*e7776783SApple OSS Distributions /* 586*e7776783SApple OSS Distributions * @function sock_setupcalls_locked 587*e7776783SApple OSS Distributions * @discussion The locked version of sock_setupcalls 588*e7776783SApple OSS Distributions * @param locked: When sets, indicates that the callbacks expect to be 589*e7776783SApple OSS Distributions * on a locked socket. Thus, no unlock is done prior to 590*e7776783SApple OSS Distributions * calling the callback. 591*e7776783SApple OSS Distributions */ 592*e7776783SApple OSS Distributions extern void sock_setupcalls_locked(socket_t sock, 593*e7776783SApple OSS Distributions sock_upcall rcallback, void *rcontext, 594*e7776783SApple OSS Distributions sock_upcall wcallback, void *wcontext, int locked); 595*e7776783SApple OSS Distributions 596*e7776783SApple OSS Distributions /* 597*e7776783SApple OSS Distributions * @function sock_catchevents 598*e7776783SApple OSS Distributions * @discussion Set the notifier function to be called when an event 599*e7776783SApple OSS Distributions * occurs on the socket. This may be set to NULL to disable 600*e7776783SApple OSS Distributions * further notifications. Setting the function does not 601*e7776783SApple OSS Distributions * affect currently notifications about to be sent or being sent. 602*e7776783SApple OSS Distributions * @param sock The socket. 603*e7776783SApple OSS Distributions * @param event_callback The event notifier function 604*e7776783SApple OSS Distributions * @param event_context A cookie passed directly to the event callback 605*e7776783SApple OSS Distributions * @param event_mask One or more SO_FILT_HINT_* values OR'ed together, 606*e7776783SApple OSS Distributions * indicating the registered event(s). 607*e7776783SApple OSS Distributions */ 608*e7776783SApple OSS Distributions extern errno_t sock_catchevents(socket_t sock, sock_evupcall event_callback, 609*e7776783SApple OSS Distributions void *event_context, long event_mask); 610*e7776783SApple OSS Distributions 611*e7776783SApple OSS Distributions extern void sock_catchevents_locked(socket_t sock, sock_evupcall ecallback, 612*e7776783SApple OSS Distributions void *econtext, long emask); 613*e7776783SApple OSS Distributions 614*e7776783SApple OSS Distributions 615*e7776783SApple OSS Distributions /* 616*e7776783SApple OSS Distributions * @function sock_iskernel 617*e7776783SApple OSS Distributions * @discussion Returns true if the socket was created by the kernel or 618*e7776783SApple OSS Distributions * is owned by the kernel. 619*e7776783SApple OSS Distributions * @param sock The socket. 620*e7776783SApple OSS Distributions * @result True if the kernel owns the socket. 621*e7776783SApple OSS Distributions */ 622*e7776783SApple OSS Distributions extern int sock_iskernel(socket_t); 623*e7776783SApple OSS Distributions #endif /* KERNEL_PRIVATE */ 624*e7776783SApple OSS Distributions 625*e7776783SApple OSS Distributions __END_DECLS 626*e7776783SApple OSS Distributions #undef __NKE_API_DEPRECATED 627*e7776783SApple OSS Distributions #endif /* __KPI_SOCKET__ */ 628