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