28cbd28ac66c2a55f62669ddcafec8ecc8360d76
[apple/xnu.git] / bsd / sys / kpi_socket.h
1 /*
2 * Copyright (c) 2008 Apple Computer, 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 __BEGIN_DECLS
45
46 struct timeval;
47
48 /*!
49 @typedef sock_upcall
50
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.
55
56 Calls to your upcall function are not serialized and may be
57 called concurrently from multiple threads in the kernel.
58
59 Your upcall function will be called when:
60
61 @param so A reference to the socket that's ready.
62 @param cookie The cookie passed in when the socket was created.
63 @param waitf Indicates whether or not it's safe to block.
64 */
65 typedef void (*sock_upcall)(socket_t so, void *cookie, int waitf);
66
67 /*!
68 @function sock_accept
69 @discussion Accepts an incoming connection on a socket. See 'man 2
70 accept' for more information. Allocating a socket in this manner
71 creates a socket with no associated file descriptor.
72 @param so The listening socket you'd like to accept a connection on.
73 @param from A pointer to a socket address that will be filled in
74 with the address the connection is from.
75 @param fromlen Maximum length of from.
76 @param flags Supports MSG_DONTWAIT and MSG_USEUPCALL. If
77 MSG_DONTWAIT is set, accept will return EWOULDBLOCK if there are
78 no connections ready to be accepted. If MSG_USEUPCALL is set,
79 the created socket will use the same upcall function attached to
80 the original socket.
81 @param callback A notifier function to be called when an event
82 occurs on the socket. This may be NULL.
83 @param cookie A cookie passed directly to the callback.
84 @param new_so Upon success, *new_so will be a reference to a new
85 socket for tracking the connection.
86 @result 0 on success otherwise the errno error.
87 */
88 extern errno_t sock_accept(socket_t so, struct sockaddr *from, int fromlen,
89 int flags, sock_upcall callback, void *cookie, socket_t *new_so);
90
91 /*!
92 @function sock_bind
93 @discussion Binds a socket to a specific address. See 'man 2 bind'
94 for more information.
95 @param so The socket to be bound.
96 @param to The local address the socket should be bound to.
97 @result 0 on success otherwise the errno error.
98 */
99 extern errno_t sock_bind(socket_t so, const struct sockaddr *to);
100
101 /*!
102 @function sock_connect
103 @discussion Initiates a connection on the socket. See 'man 2
104 connect' for more information.
105 @param so The socket to be connect.
106 @param to The remote address the socket should connect to.
107 @param flags Flags for connecting. The only flag supported so far is
108 MSG_DONTWAIT. MSG_DONTWAIT will perform a non-blocking connect.
109 sock_connect will return immediately with EINPROGRESS. The
110 upcall, if supplied, will be called when the connection is
111 completed.
112 @result 0 on success, EINPROGRESS for a non-blocking connect that
113 has not completed, otherwise the errno error.
114 */
115 extern errno_t sock_connect(socket_t so, const struct sockaddr *to, int flags);
116
117 #ifdef KERNEL_PRIVATE
118 /*
119 This function was added to support NFS. NFS does something funny,
120 setting a short timeout and checking to see if it should abort the
121 connect every two seconds. Ideally, NFS would use the upcall to be
122 notified when the connect is complete.
123
124 If you feel you need to use this function, please contact us to
125 explain why.
126
127 @function sock_connectwait
128 @discussion Allows a caller to wait on a socket connect.
129 @param so The socket being connected.
130 @param tv The amount of time to wait.
131 @result 0 on success otherwise the errno error. EINPROGRESS will be
132 returned if the connection did not complete in the timeout
133 specified.
134 */
135 extern errno_t sock_connectwait(socket_t so, const struct timeval *tv);
136 #endif /* KERNEL_PRIVATE */
137
138 /*!
139 @function sock_getpeername
140 @discussion Retrieves the remote address of a connected socket. See
141 'man 2 getpeername'.
142 @param so The socket.
143 @param peername Storage for the peer name.
144 @param peernamelen Length of storage for the peer name.
145 @result 0 on success otherwise the errno error.
146 */
147 extern errno_t sock_getpeername(socket_t so, struct sockaddr *peername,
148 int peernamelen);
149
150 /*!
151 @function sock_getsockname
152 @discussion Retrieves the local address of a socket. See 'man 2
153 getsockname'.
154 @param so The socket.
155 @param sockname Storage for the local name.
156 @param socknamelen Length of storage for the socket name.
157 @result 0 on success otherwise the errno error.
158 */
159 extern errno_t sock_getsockname(socket_t so, struct sockaddr *sockname,
160 int socknamelen);
161
162 /*!
163 @function sock_getsockopt
164 @discussion Retrieves a socket option. See 'man 2 getsockopt'.
165 @param so The socket.
166 @param level Level of the socket option.
167 @param optname The option name.
168 @param optval The option value.
169 @param optlen The length of optval, returns the actual length.
170 @result 0 on success otherwise the errno error.
171 */
172 extern errno_t sock_getsockopt(socket_t so, int level, int optname,
173 void *optval, int *optlen);
174
175 /*!
176 @function sock_ioctl
177 @discussion Performs an ioctl operation on a socket. See 'man 2 ioctl'.
178 @param so The socket.
179 @param request The ioctl name.
180 @param argp The argument.
181 @result 0 on success otherwise the errno error.
182 */
183 extern errno_t sock_ioctl(socket_t so, unsigned long request, void *argp);
184
185 /*!
186 @function sock_setsockopt
187 @discussion Sets a socket option. See 'man 2 setsockopt'.
188 @param so The socket.
189 @param level Level of the socket option.
190 @param optname The option name.
191 @param optval The option value.
192 @param optlen The length of optval.
193 @result 0 on success otherwise the errno error.
194 */
195 extern errno_t sock_setsockopt(socket_t so, int level, int optname,
196 const void *optval, int optlen);
197
198 #ifdef KERNEL_PRIVATE
199 /*
200 This function was added to support AFP setting the traffic class
201 for a backup stream within a wireless LAN or over link-local address.
202
203 If you feel you need to use this function, please contact us to
204 explain why.
205
206 @function sock_settclassopt
207 @discussion Allows a caller to set the traffic class.
208 @param so The socket.
209 @param optval The option value.
210 @param optlen The length of optval.
211 @result 0 on success otherwise the errno error.
212 */
213 extern errno_t sock_settclassopt(socket_t so, const void* optval, size_t optlen);
214
215 /*
216 This function was added to support AFP getting the traffic class
217 set on a stream.
218
219 This is also a private API, please contact us if you need to use it.
220
221 @function sockgettclassopt
222 @discussion Allows a caller to get the traffic class.
223 @param so The socket.
224 @param optval The option value.
225 @param optlen The length of optval, returns the actual length.
226 @result 0 on success otherwise the errno error.
227 */
228 extern errno_t sock_gettclassopt(socket_t so, void* optval, size_t* optlen);
229 #endif
230
231 /*!
232 @function sock_listen
233 @discussion Indicate that the socket should start accepting incoming
234 connections. See 'man 2 listen'.
235 @param so The socket.
236 @param backlog The maximum length of the queue of pending connections.
237 @result 0 on success otherwise the errno error.
238 */
239 extern errno_t sock_listen(socket_t so, int backlog);
240
241 /*!
242 @function sock_receive
243 @discussion Receive data from a socket. Similar to recvmsg. See 'man
244 2 recvmsg' for more information about receiving data.
245 @param so The socket.
246 @param msg The msg describing how the data should be received.
247 @param flags See 'man 2 recvmsg'.
248 @param recvdlen Number of bytes received, same as return value of
249 userland recvmsg.
250 @result 0 on success, EWOULDBLOCK if non-blocking and operation
251 would cause the thread to block, otherwise the errno error.
252 */
253 extern errno_t sock_receive(socket_t so, struct msghdr *msg, int flags,
254 size_t *recvdlen);
255
256 /*!
257 @function sock_receivembuf
258 @discussion Receive data from a socket. Similar to sock_receive
259 though data is returned as a chain of mbufs. See 'man 2 recvmsg'
260 for more information about receiving data.
261 @param so The socket.
262 @param msg The msg describing how the data should be received. May
263 be NULL. The msg_iov is ignored.
264 @param data Upon return *data will be a reference to an mbuf chain
265 containing the data received. This eliminates copying the data
266 out of the mbufs. Caller is responsible for freeing the mbufs.
267 @param flags See 'man 2 recvmsg'.
268 @param recvlen Maximum number of bytes to receive in the mbuf chain.
269 Upon return, this value will be set to the number of bytes
270 received, same as return value of userland recvmsg.
271 @result 0 on success, EWOULDBLOCK if non-blocking and operation
272 would cause the thread to block, otherwise the errno error.
273 */
274 extern errno_t sock_receivembuf(socket_t so, struct msghdr *msg, mbuf_t *data,
275 int flags, size_t *recvlen);
276
277 /*!
278 @function sock_send
279 @discussion Send data on a socket. Similar to sendmsg. See 'man 2
280 sendmsg' for more information about sending data.
281 @param so The socket.
282 @param msg The msg describing how the data should be sent. Any
283 pointers must point to data in the kernel.
284 @param flags See 'man 2 sendmsg'.
285 @param sentlen The number of bytes sent.
286 @result 0 on success, EWOULDBLOCK if non-blocking and operation
287 would cause the thread to block, otherwise the errno error.
288 */
289 extern errno_t sock_send(socket_t so, const struct msghdr *msg, int flags,
290 size_t *sentlen);
291
292 /*!
293 @function sock_sendmbuf
294 @discussion Send data in an mbuf on a socket. Similar to sock_send
295 only the data to be sent is taken from the mbuf chain.
296 @param so The socket.
297 @param msg The msg describing how the data should be sent. The
298 msg_iov is ignored. msg may be NULL.
299 @param data The mbuf chain of data to send.
300 @param flags See 'man 2 sendmsg'.
301 @param sentlen The number of bytes sent.
302 @result 0 on success, EWOULDBLOCK if non-blocking and operation
303 would cause the thread to block, otherwise the errno error.
304 Regardless of return value, the mbuf chain 'data' will be freed.
305 */
306 extern errno_t sock_sendmbuf(socket_t so, const struct msghdr *msg, mbuf_t data,
307 int flags, size_t *sentlen);
308
309 /*!
310 @function sock_shutdown
311 @discussion Shutdown one or both directions of a connection. See
312 'man 2 shutdown' for more information.
313 @param so The socket.
314 @param how SHUT_RD - shutdown receive.
315 SHUT_WR - shutdown send.
316 SHUT_RDWR - shutdown both.
317 @result 0 on success otherwise the errno error.
318 */
319 extern errno_t sock_shutdown(socket_t so, int how);
320
321 /*!
322 @function sock_socket
323 @discussion Allocate a socket. Allocating a socket in this manner
324 creates a socket with no associated file descriptor. For more
325 information, see 'man 2 socket'.
326 @param domain The socket domain (PF_INET, etc...).
327 @param type The socket type (SOCK_STREAM, SOCK_DGRAM, etc...).
328 @param protocol The socket protocol.
329 @param callback A notifier function to be called when an event
330 occurs on the socket. This may be NULL.
331 @param cookie A cookie passed directly to the callback.
332 @param new_so Upon success, a reference to the new socket.
333 @result 0 on success otherwise the errno error.
334 */
335 extern errno_t sock_socket(int domain, int type, int protocol,
336 sock_upcall callback, void *cookie, socket_t *new_so);
337
338 /*!
339 @function sock_close
340 @discussion Close the socket.
341 @param so The socket to close. This should only ever be a socket
342 created with sock_socket. Closing a socket created in user space
343 using sock_close may leave a file descriptor pointing to the
344 closed socket, resulting in undefined behavior.
345 */
346 extern void sock_close(socket_t so);
347
348 #ifdef KERNEL_PRIVATE
349 /*
350 @function sock_retain
351 @discussion Prevents the socket from closing
352 @param so The socket to close. Increment a retain count on the
353 socket, preventing it from being closed when sock_close is
354 called. This is used when a File Descriptor is passed (and
355 closed) from userland and the kext wants to keep ownership of
356 that socket. It is used in conjunction with
357 sock_release(socket_t so).
358 */
359 extern void sock_retain(socket_t so);
360
361 /*
362 @function sock_release
363 @discussion Decrement the retain count and close the socket if the
364 retain count reaches zero.
365 @param so The socket to release. This is used to release ownership
366 on a socket acquired with sock_retain. When the last retain
367 count is reached, this will call sock_close to close the socket.
368 */
369 extern void sock_release(socket_t so);
370 #endif /* KERNEL_PRIVATE */
371
372 /*!
373 @function sock_setpriv
374 @discussion Set the privileged bit in the socket. Allows for
375 operations that require root privileges.
376 @param so The socket on which to modify the SS_PRIV flag.
377 @param on Indicate whether or not the SS_PRIV flag should be set.
378 @result 0 on success otherwise the errno error.
379 */
380 extern errno_t sock_setpriv(socket_t so, int on);
381
382 /*!
383 @function sock_isconnected
384 @discussion Returns whether or not the socket is connected.
385 @param so The socket to check.
386 @result 0 - socket is not connected. 1 - socket is connected.
387 */
388 extern int sock_isconnected(socket_t so);
389
390 /*!
391 @function sock_isnonblocking
392 @discussion Returns whether or not the socket is non-blocking. In
393 the context of this KPI, non-blocking means that functions to
394 perform operations on a socket will not wait for completion.
395
396 To enable or disable blocking, use the FIONBIO ioctl. The
397 parameter is an int. If the int is zero, the socket will block.
398 If the parameter is non-zero, the socket will not block.
399 @result 0 - socket will block. 1 - socket will not block.
400 */
401 extern int sock_isnonblocking(socket_t so);
402
403 /*!
404 @function sock_gettype
405 @discussion Retrieves information about the socket. This is the same
406 information that was used to create the socket. If any of the
407 parameters following so are NULL, that information is not
408 retrieved.
409 @param so The socket to check.
410 @param domain The domain of the socket (PF_INET, ...). May be NULL.
411 @param type The socket type (SOCK_STREAM, SOCK_DGRAM, ...). May be NULL.
412 @param protocol The socket protocol. May be NULL.
413 @result 0 on success otherwise the errno error.
414 */
415 extern errno_t sock_gettype(socket_t so, int *domain, int *type, int *protocol);
416
417 #ifdef KERNEL_PRIVATE
418 /*
419 @function sock_nointerrupt
420 @discussion Disables interrupt on socket buffers (sets SB_NOINTR on
421 send and receive socket buffers).
422 @param so The socket to modify.
423 @param on Indicate whether or not the SB_NOINTR flag should be set.
424 @result 0 on success otherwise the errno error.
425 */
426 extern errno_t sock_nointerrupt(socket_t so, int on);
427
428 /*
429 @function sock_getlistener
430 @discussion Retrieves the listening socket of a pre-accepted socket,
431 i.e. a socket which is still in the incomplete/completed list.
432 Once a socket has been accepted, the information pertaining
433 to its listener is no longer available. Therefore, modules
434 interested in finding out the listening socket should install
435 the appropriate socket filter callback (sf_attach) which gets
436 invoked prior to the socket being fully accepted, and call
437 this routine at such a time to obtain the listener. Callers
438 are guaranteed that the listener socket will not go away
439 during the sf_attach callback, and therefore the value is
440 safe to be used only in that callback context. Callers should
441 therefore take note that the listening socket's lock will be
442 held throughout the duration of the callback.
443 @param so The pre-accepted socket.
444 @result Non-NULL value which indicates the listening socket; otherwise,
445 NULL if the socket is not in the incomplete/completed list
446 of a listener.
447 */
448 extern socket_t sock_getlistener(socket_t so);
449
450 /*
451 @function sock_getaddr
452 @discussion Retrieves the local or remote address of a socket.
453 This is a composite of sock_getpeername and sock_getsockname,
454 except that the allocated socket address is returned to the
455 caller, and that the caller is reponsible for calling
456 sock_freeaddr once finished with it.
457 @param so The socket.
458 @param psockname Pointer to the storage for the socket name.
459 @param peername 0 for local address, and non-zero for peer address.
460 @result 0 on success otherwise the errno error.
461 */
462 extern errno_t sock_getaddr(socket_t so, struct sockaddr **psockname,
463 int peername);
464
465 /*
466 @function sock_freeaddr
467 @discussion Frees the socket address allocated by sock_getaddr.
468 @param sockname The socket name to be freed.
469 */
470 extern void sock_freeaddr(struct sockaddr *sockname);
471 #endif /* KERNEL_PRIVATE */
472
473 __END_DECLS
474 #endif /* __KPI_SOCKET__ */