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