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