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