]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - share/man/man9/socket.9
MFV: file 5.33
[FreeBSD/FreeBSD.git] / share / man / man9 / socket.9
1 .\"-
2 .\" Copyright (c) 2006 Robert N. M. Watson
3 .\" Copyright (c) 2014 Benjamin J. Kaduk
4 .\" All rights reserved.
5 .\"
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
8 .\" are met:
9 .\" 1. Redistributions of source code must retain the above copyright
10 .\"    notice, this list of conditions and the following disclaimer.
11 .\" 2. Redistributions in binary form must reproduce the above copyright
12 .\"    notice, this list of conditions and the following disclaimer in the
13 .\"    documentation and/or other materials provided with the distribution.
14 .\"
15 .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 .\" SUCH DAMAGE.
26 .\"
27 .\" $FreeBSD$
28 .\"
29 .Dd May 26, 2014
30 .Dt SOCKET 9
31 .Os
32 .Sh NAME
33 .Nm socket
34 .Nd "kernel socket interface"
35 .Sh SYNOPSIS
36 .In sys/socket.h
37 .In sys/socketvar.h
38 .Ft void
39 .Fn soabort "struct socket *so"
40 .Ft int
41 .Fn soaccept "struct socket *so" "struct sockaddr **nam"
42 .Ft int
43 .Fn socheckuid "struct socket *so" "uid_t uid"
44 .Ft int
45 .Fn sobind "struct socket *so" "struct sockaddr *nam" "struct thread *td"
46 .Ft void
47 .Fn soclose "struct socket *so"
48 .Ft int
49 .Fn soconnect "struct socket *so" "struct sockaddr *nam" "struct thread *td"
50 .Ft int
51 .Fo socreate
52 .Fa "int dom" "struct socket **aso" "int type" "int proto"
53 .Fa "struct ucred *cred" "struct thread *td"
54 .Fc
55 .Ft int
56 .Fn sodisconnect "struct socket *so"
57 .Ft struct  sockaddr *
58 .Fn sodupsockaddr "const struct sockaddr *sa" "int mflags"
59 .Ft void
60 .Fn sofree "struct socket *so"
61 .Ft void
62 .Fn sohasoutofband "struct socket *so"
63 .Ft int
64 .Fn solisten "struct socket *so" "int backlog" "struct thread *td"
65 .Ft void
66 .Fn solisten_proto "struct socket *so" "int backlog"
67 .Ft int
68 .Fn solisten_proto_check "struct socket *so"
69 .Ft struct socket *
70 .Fn sonewconn "struct socket *head" "int connstatus"
71 .Ft int
72 .Fo sopoll
73 .Fa "struct socket *so" "int events" "struct ucred *active_cred"
74 .Fa "struct thread *td"
75 .Fc
76 .Ft int
77 .Fo sopoll_generic
78 .Fa "struct socket *so" "int events" "struct ucred *active_cred"
79 .Fa "struct thread *td"
80 .Fc
81 .Ft int
82 .Fo soreceive
83 .Fa "struct socket *so" "struct sockaddr **psa" "struct uio *uio"
84 .Fa "struct mbuf **mp0" "struct mbuf **controlp" "int *flagsp"
85 .Fc
86 .Ft int
87 .Fo soreceive_stream
88 .Fa "struct socket *so" "struct sockaddr **paddr"
89 .Fa "struct uio *uio" "struct mbuf **mp0" "struct mbuf **controlp"
90 .Fa "int *flagsp"
91 .Fc
92 .Ft int
93 .Fo soreceive_dgram
94 .Fa "struct socket *so" "struct sockaddr **paddr"
95 .Fa "struct uio *uio" "struct mbuf **mp0" "struct mbuf **controlp"
96 .Fa "int *flagsp"
97 .Fc
98 .Ft int
99 .Fo soreceive_generic
100 .Fa "struct socket *so" "struct sockaddr **paddr"
101 .Fa "struct uio *uio" "struct mbuf **mp0" "struct mbuf **controlp"
102 .Fa "int *flagsp"
103 .Fc
104 .Ft int
105 .Fn soreserve "struct socket *so" "u_long sndcc" "u_long rcvcc"
106 .Ft void
107 .Fn sorflush "struct socket *so"
108 .Ft int
109 .Fo sosend
110 .Fa "struct socket *so" "struct sockaddr *addr" "struct uio *uio"
111 .Fa "struct mbuf *top" "struct mbuf *control" "int flags" "struct thread *td"
112 .Fc
113 .Ft int
114 .Fo sosend_dgram
115 .Fa "struct socket *so" "struct sockaddr *addr"
116 .Fa "struct uio *uio" "struct mbuf *top" "struct mbuf *control"
117 .Fa "int flags" "struct thread *td"
118 .Fc
119 .Ft int
120 .Fo sosend_generic
121 .Fa "struct socket *so" "struct sockaddr *addr"
122 .Fa "struct uio *uio" "struct mbuf *top" "struct mbuf *control"
123 .Fa "int flags" "struct thread *td"
124 .Fc
125 .Ft int
126 .Fn soshutdown "struct socket *so" "int how"
127 .Ft void
128 .Fn sotoxsocket "struct socket *so" "struct xsocket *xso"
129 .Ft void
130 .Fn soupcall_clear "struct socket *so" "int which"
131 .Ft void
132 .Fo soupcall_set
133 .Fa "struct socket *so" "int which"
134 .Fa "int (*func)(struct socket *, void *, int)" "void *arg"
135 .Fc
136 .Ft void
137 .Fn sowakeup "struct socket *so" "struct sockbuf *sb"
138 .In sys/sockopt.h
139 .Ft int
140 .Fn sosetopt "struct socket *so" "struct sockopt *sopt"
141 .Ft int
142 .Fn sogetopt "struct socket *so" "struct sockopt *sopt"
143 .Ft int
144 .Fn sooptcopyin "struct sockopt *sopt" "void *buf" "size_t len" "size_t minlen"
145 .Ft int
146 .Fn sooptcopyout "struct sockopt *sopt" "const void *buf" "size_t len"
147 .Sh DESCRIPTION
148 The kernel
149 .Nm
150 programming interface permits in-kernel consumers to interact with
151 local and network socket objects in a manner similar to that permitted using
152 the
153 .Xr socket 2
154 user API.
155 These interfaces are appropriate for use by distributed file systems and
156 other network-aware kernel services.
157 While the user API operates on file descriptors, the kernel interfaces
158 operate directly on
159 .Vt "struct socket"
160 pointers.
161 Some portions of the kernel API exist only to implement the user API,
162 and are not expected to be used by kernel code.
163 The portions of the socket API used by socket consumers and
164 implementations of network protocols will differ; some routines
165 are only useful for protocol implementors.
166 .Pp
167 Except where otherwise indicated,
168 .Nm
169 functions may sleep, and are not appropriate for use in an
170 .Xr ithread 9
171 context or while holding non-sleepable kernel locks.
172 .Ss Creating and Destroying Sockets
173 A new socket may be created using
174 .Fn socreate .
175 As with
176 .Xr socket 2 ,
177 arguments specify the requested domain, type, and protocol via
178 .Fa dom , type ,
179 and
180 .Fa proto .
181 The socket is returned via
182 .Fa aso
183 on success.
184 In addition, the credential used to authorize operations associated with the
185 socket will be passed via
186 .Fa cred
187 (and will be cached for the lifetime of the socket), and the thread
188 performing the operation via
189 .Fa td .
190 .Em Warning :
191 authorization of the socket creation operation will be performed
192 using the thread credential for some protocols (such as raw sockets).
193 .Pp
194 Sockets may be closed and freed using
195 .Fn soclose ,
196 which has similar semantics to
197 .Xr close 2 .
198 .Pp
199 In certain circumstances, it is appropriate to destroy a socket without
200 waiting for it to disconnect, for which
201 .Fn soabort
202 is used.
203 This is only appropriate for incoming connections which are in a
204 partially connected state.
205 It must be called on an unreferenced socket, by the thread which
206 removed the socket from its listen queue, to prevent races.
207 It will call into protocol code, so no socket locks may be held
208 over the call.
209 The caller of
210 .Fn soabort
211 is responsible for setting the VNET context.
212 The normal path to freeing a socket is
213 .Fn sofree ,
214 which handles reference counting on the socket.
215 It should be called whenever a reference is released, and also whenever
216 reference flags are cleared in socket or protocol code.
217 Calls to
218 .Fn sofree
219 should not be made from outside the socket layer; outside callers
220 should use
221 .Fn soclose
222 instead.
223 .Ss Connections and Addresses
224 The
225 .Fn sobind
226 function is equivalent to the
227 .Xr bind 2
228 system call, and binds the socket
229 .Fa so
230 to the address
231 .Fa nam .
232 The operation would be authorized using the credential on thread
233 .Fa td .
234 .Pp
235 The
236 .Fn soconnect
237 function is equivalent to the
238 .Xr connect 2
239 system call, and initiates a connection on the socket
240 .Fa so
241 to the address
242 .Fa nam .
243 The operation will be authorized using the credential on thread
244 .Fa td .
245 Unlike the user system call,
246 .Fn soconnect
247 returns immediately; the caller may
248 .Xr msleep 9
249 on
250 .Fa so->so_timeo
251 while holding the socket mutex and waiting for the
252 .Dv SS_ISCONNECTING
253 flag to clear or
254 .Fa so->so_error
255 to become non-zero.
256 If
257 .Fn soconnect
258 fails, the caller must manually clear the
259 .Dv SS_ISCONNECTING
260 flag.
261 .Pp
262 A call to
263 .Fn sodisconnect
264 disconnects the socket without closing it.
265 .Pp
266 The
267 .Fn soshutdown
268 function is equivalent to the
269 .Xr shutdown 2
270 system call, and causes part or all of a connection on a socket to be closed
271 down.
272 .Pp
273 Sockets are transitioned from non-listening status to listening with
274 .Fn solisten .
275 .Ss Socket Options
276 The
277 .Fn sogetopt
278 function is equivalent to the
279 .Xr getsockopt 2
280 system call, and retrieves a socket option on socket
281 .Fa so .
282 The
283 .Fn sosetopt
284 function is equivalent to the
285 .Xr setsockopt 2
286 system call, and sets a socket option on socket
287 .Fa so .
288 .Pp
289 The second argument in both
290 .Fn sogetopt
291 and
292 .Fn sosetopt
293 is the
294 .Fa sopt
295 pointer to a
296 .Vt "struct sopt"
297 describing the socket option operation.
298 The caller-allocated structure must be zeroed, and then have its fields
299 initialized to specify socket option operation arguments:
300 .Bl -tag -width ".Va sopt_valsize"
301 .It Va sopt_dir
302 Set to
303 .Dv SOPT_SET
304 or
305 .Dv SOPT_GET
306 depending on whether this is a get or set operation.
307 .It Va sopt_level
308 Specify the level in the network stack the operation is targeted at; for
309 example,
310 .Dv SOL_SOCKET .
311 .It Va sopt_name
312 Specify the name of the socket option to set.
313 .It Va sopt_val
314 Kernel space pointer to the argument value for the socket option.
315 .It Va sopt_valsize
316 Size of the argument value in bytes.
317 .El
318 .Ss Socket Upcalls
319 In order for the owner of a socket to be notified when the socket
320 is ready to send or receive data, an upcall may be registered on
321 the socket.
322 The upcall is a function that will be called by the socket framework
323 when a socket buffer associated with the given socket is ready for
324 reading or writing.
325 .Fn soupcall_set
326 is used to register a socket upcall.
327 The function
328 .Va func
329 is registered, and the pointer
330 .Va arg
331 will be passed as its second argument when it is called by the framework.
332 The possible values for
333 .Va which
334 are
335 .Dv SO_RCV
336 and
337 .Dv SO_SND ,
338 which register upcalls for receive and send events, respectively.
339 The upcall function
340 .Fn func
341 must return either
342 .Dv SU_OK
343 or
344 .Dv SU_ISCONNECTED ,
345 depending on whether or not a call to
346 .Xr soisconnected
347 should be made by the socket framework after the upcall returns.
348 The upcall
349 .Va func
350 cannot call
351 .Xr soisconnected
352 itself due to lock ordering with the socket buffer lock.
353 Only
354 .Dv SO_RCV
355 upcalls should return
356 .Dv SU_ISCONNECTED .
357 When a
358 .Dv SO_RCV
359 upcall returns
360 .Dv SU_ISCONNECTED ,
361 the upcall will be removed from the socket.
362 .Pp
363 Upcalls are removed from their socket by
364 .Fn soupcall_clear .
365 The
366 .Va which
367 argument again specifies whether the sending or receiving upcall is to
368 be cleared, with
369 .Dv SO_RCV
370 or
371 .Dv SO_SND .
372 .Ss Socket I/O
373 The
374 .Fn soreceive
375 function is equivalent to the
376 .Xr recvmsg 2
377 system call, and attempts to receive bytes of data from the socket
378 .Fa so ,
379 optionally blocking awaiting for data if none is ready to read.
380 Data may be retrieved directly to kernel or user memory via the
381 .Fa uio
382 argument, or as an mbuf chain returned to the caller via
383 .Fa mp0 ,
384 avoiding a data copy.
385 The
386 .Fa uio
387 must always be
388 .Pf non- Dv NULL .
389 If
390 .Fa mp0
391 is
392 .Pf non- Dv NULL ,
393 only the
394 .Fa uio_resid
395 of
396 .Fa uio
397 is used.
398 The caller may optionally retrieve a socket address on a protocol with the
399 .Dv PR_ADDR
400 capability by providing storage via
401 .Pf non- Dv NULL
402 .Fa psa
403 argument.
404 The caller may optionally retrieve control data mbufs via a
405 .Pf non- Dv NULL
406 .Fa controlp
407 argument.
408 Optional flags may be passed to
409 .Fn soreceive
410 via a
411 .Pf non- Dv NULL
412 .Fa flagsp
413 argument, and use the same flag name space as the
414 .Xr recvmsg 2
415 system call.
416 .Pp
417 The
418 .Fn sosend
419 function is equivalent to the
420 .Xr sendmsg 2
421 system call, and attempts to send bytes of data via the socket
422 .Fa so ,
423 optionally blocking if data cannot be immediately sent.
424 Data may be sent directly from kernel or user memory via the
425 .Fa uio
426 argument, or as an mbuf chain via
427 .Fa top ,
428 avoiding a data copy.
429 Only one of the
430 .Fa uio
431 or
432 .Fa top
433 pointers may be
434 .Pf non- Dv NULL .
435 An optional destination address may be specified via a
436 .Pf non- Dv NULL
437 .Fa addr
438 argument, which may result in an implicit connect if supported by the
439 protocol.
440 The caller may optionally send control data mbufs via a
441 .Pf non- Dv NULL
442 .Fa control
443 argument.
444 Flags may be passed to
445 .Fn sosend
446 using the
447 .Fa flags
448 argument, and use the same flag name space as the
449 .Xr sendmsg 2
450 system call.
451 .Pp
452 Kernel callers running in
453 .Xr ithread 9
454 context, or with a mutex held, will wish to use non-blocking sockets and pass
455 the
456 .Dv MSG_DONTWAIT
457 flag in order to prevent these functions from sleeping.
458 .Pp
459 A socket can be queried for readability, writability, out-of-band data,
460 or end-of-file using
461 .Fn sopoll .
462 The possible values for
463 .Va events
464 are as for
465 .Xr poll 2 ,
466 with symbolic values
467 .Dv POLLIN ,
468 .Dv POLLPRI ,
469 .Dv POLLOUT ,
470 .Dv POLLRDNORM ,
471 .Dv POLLWRNORM ,
472 .Dv POLLRDBAND ,
473 and
474 .Dv POLLINGEOF
475 taken from
476 .In sys/poll.h .
477 .Pp
478 Calls to
479 .Fn soaccept
480 pass through to the protocol's accept routine to accept an incoming connection.
481 .Ss Socket Utility Functions
482 The uid of a socket's credential may be compared against a
483 .Va uid
484 with
485 .Fn socheckuid .
486 .Pp
487 A copy of an existing
488 .Vt struct sockaddr
489 may be made using
490 .Fn sodupsockaddr .
491 .Pp
492 Protocol implementations notify the socket layer of the arrival of
493 out-of-band data using
494 .Fn sohasoutofband ,
495 so that the socket layer can notify socket consumers of the available data.
496 .Pp
497 An
498 .Dq external-format
499 version of a
500 .Vt struct socket
501 can be created using
502 .Fn sotoxsocket ,
503 suitable for isolating user code from changes in the kernel structure.
504 .Ss Protocol Implementations
505 Protocols must supply an implementation for
506 .Fn solisten ;
507 such protocol implementations can call back into the socket layer using
508 .Fn solisten_proto_check
509 and
510 .Fn solisten_proto
511 to check and set the socket-layer listen state.
512 These callbacks are provided so that the protocol implementation
513 can order the socket layer and protocol locks as necessary.
514 Protocols must supply an implementation of
515 .Fn soreceive ;
516 the functions
517 .Fn soreceive_stream ,
518 .Fn soreceive_dgram ,
519 and
520 .Fn soreceive_generic
521 are supplied for use by such implementations.
522 .Pp
523 Protocol implementations can use
524 .Fn sonewconn
525 to create a socket and attach protocol state to that socket.
526 This can be used to create new sockets available for
527 .Fn soaccept
528 on a listen socket.
529 The returned socket has a reference count of zero.
530 .Pp
531 Protocols must supply an implementation for
532 .Fn sopoll ;
533 .Fn sopoll_generic
534 is provided for the use by protocol implementations.
535 .Pp
536 The functions
537 .Fn sosend_dgram
538 and
539 .Fn sosend_generic
540 are supplied to assist in protocol implementations of
541 .Fn sosend .
542 .Pp
543 When a protocol creates a new socket structure, it is necessary to
544 reserve socket buffer space for that socket, by calling
545 .Fn soreserve .
546 The rough inverse of this reservation is performed by
547 .Fn sorflush ,
548 which is called automatically by the socket framework.
549 .Pp
550 When a protocol needs to wake up threads waiting for the socket to
551 become ready to read or write, variants of
552 .Fn sowakeup
553 are used.
554 The
555 .Fn sowakeup
556 function should not be called directly by protocol code, instead use the
557 wrappers
558 .Fn sorwakeup ,
559 .Fn sorwakeup_locked ,
560 .Fn sowwakeup ,
561 and
562 .Fn sowwakeup_locked
563 for readers and writers, with the corresponding socket buffer lock
564 not already locked, or already held, respectively.
565 .Pp
566 The functions
567 .Fn sooptcopyin
568 and
569 .Fn sooptcopyout
570 are useful for transferring
571 .Vt struct sockopt
572 data between user and kernel code.
573 .Sh SEE ALSO
574 .Xr bind 2 ,
575 .Xr close 2 ,
576 .Xr connect 2 ,
577 .Xr getsockopt 2 ,
578 .Xr recv 2 ,
579 .Xr send 2 ,
580 .Xr setsockopt 2 ,
581 .Xr shutdown 2 ,
582 .Xr socket 2 ,
583 .Xr ng_ksocket 4 ,
584 .Xr ithread 9 ,
585 .Xr msleep 9 ,
586 .Xr ucred 9
587 .Sh HISTORY
588 The
589 .Xr socket 2
590 system call appeared in
591 .Bx 4.2 .
592 This manual page was introduced in
593 .Fx 7.0 .
594 .Sh AUTHORS
595 This manual page was written by
596 .An Robert Watson
597 and
598 .An Benjamin Kaduk .
599 .Sh BUGS
600 The use of explicitly passed credentials, credentials hung from explicitly
601 passed threads, the credential on
602 .Dv curthread ,
603 and the cached credential from
604 socket creation time is inconsistent, and may lead to unexpected behaviour.
605 It is possible that several of the
606 .Fa td
607 arguments should be
608 .Fa cred
609 arguments, or simply not be present at all.
610 .Pp
611 The caller may need to manually clear
612 .Dv SS_ISCONNECTING
613 if
614 .Fn soconnect
615 returns an error.
616 .Pp
617 The
618 .Dv MSG_DONTWAIT
619 flag is not implemented for
620 .Fn sosend ,
621 and may not always work with
622 .Fn soreceive
623 when zero copy sockets are enabled.
624 .Pp
625 This manual page does not describe how to register socket upcalls or monitor
626 a socket for readability/writability without using blocking I/O.
627 .Pp
628 The
629 .Fn soref
630 and
631 .Fn sorele
632 functions are not described, and in most cases should not be used, due to
633 confusing and potentially incorrect interactions when
634 .Fn sorele
635 is last called after
636 .Fn soclose .