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