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