]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man7/socket.7
add_key.2, chown.2, epoll_ctl.2, epoll_wait.2, execve.2, fcntl.2, get_mempolicy.2...
[thirdparty/man-pages.git] / man7 / socket.7
1 '\" t
2 .\" This man page is Copyright (C) 1999 Andi Kleen <ak@muc.de>.
3 .\" and copyright (c) 1999 Matthew Wilcox.
4 .\"
5 .\" %%%LICENSE_START(VERBATIM_ONE_PARA)
6 .\" Permission is granted to distribute possibly modified copies
7 .\" of this page provided the header is included verbatim,
8 .\" and in case of nontrivial modification author and date
9 .\" of the modification is added to the header.
10 .\" %%%LICENSE_END
11 .\"
12 .\" 2002-10-30, Michael Kerrisk, <mtk.manpages@gmail.com>
13 .\" Added description of SO_ACCEPTCONN
14 .\" 2004-05-20, aeb, added SO_RCVTIMEO/SO_SNDTIMEO text.
15 .\" Modified, 27 May 2004, Michael Kerrisk <mtk.manpages@gmail.com>
16 .\" Added notes on capability requirements
17 .\" A few small grammar fixes
18 .\" 2010-06-13 Jan Engelhardt <jengelh@medozas.de>
19 .\" Documented SO_DOMAIN and SO_PROTOCOL.
20 .\" FIXME
21 .\" The following are not yet documented:
22 .\" SO_PEERNAME (2.4?)
23 .\" get only
24 .\" Seems to do something similar to getpeername(), but then
25 .\" why is it necessary / how does it differ?
26 .\" SO_TIMESTAMPNS (2.6.22)
27 .\" Documentation/networking/timestamping.txt
28 .\" commit 92f37fd2ee805aa77925c1e64fd56088b46094fc
29 .\" Author: Eric Dumazet <dada1@cosmosbay.com>
30 .\" SO_TIMESTAMPING (2.6.30)
31 .\" Documentation/networking/timestamping.txt
32 .\" commit cb9eff097831007afb30d64373f29d99825d0068
33 .\" Author: Patrick Ohly <patrick.ohly@intel.com>
34 .\" SO_WIFI_STATUS (3.3)
35 .\" commit 6e3e939f3b1bf8534b32ad09ff199d88800835a0
36 .\" Author: Johannes Berg <johannes.berg@intel.com>
37 .\" Also: SCM_WIFI_STATUS
38 .\" SO_NOFCS (3.4)
39 .\" commit 3bdc0eba0b8b47797f4a76e377dd8360f317450f
40 .\" Author: Ben Greear <greearb@candelatech.com>
41 .\" SO_GET_FILTER (3.8)
42 .\" commit a8fc92778080c845eaadc369a0ecf5699a03bef0
43 .\" Author: Pavel Emelyanov <xemul@parallels.com>
44 .\" SO_LOCK_FILTER (3.9)
45 .\" commit d59577b6ffd313d0ab3be39cb1ab47e29bdc9182
46 .\" Author: Vincent Bernat <bernat@luffy.cx>
47 .\" SO_SELECT_ERR_QUEUE (3.10)
48 .\" commit 7d4c04fc170087119727119074e72445f2bb192b
49 .\" Author: Keller, Jacob E <jacob.e.keller@intel.com>
50 .\" SO_MAX_PACING_RATE (3.13)
51 .\" commit 62748f32d501f5d3712a7c372bbb92abc7c62bc7
52 .\" SO_BPF_EXTENSIONS (3.14)
53 .\" commit ea02f9411d9faa3553ed09ce0ec9f00ceae9885e
54 .\"
55 .TH SOCKET 7 2015-05-07 Linux "Linux Programmer's Manual"
56 .SH NAME
57 socket \- Linux socket interface
58 .SH SYNOPSIS
59 .B #include <sys/socket.h>
60 .sp
61 .IB sockfd " = socket(int " socket_family ", int " socket_type ", int " protocol );
62 .SH DESCRIPTION
63 This manual page describes the Linux networking socket layer user
64 interface.
65 The BSD compatible sockets
66 are the uniform interface
67 between the user process and the network protocol stacks in the kernel.
68 The protocol modules are grouped into
69 .I protocol families
70 such as
71 .BR AF_INET ", " AF_IPX ", and " AF_PACKET ,
72 and
73 .I socket types
74 such as
75 .B SOCK_STREAM
76 or
77 .BR SOCK_DGRAM .
78 See
79 .BR socket (2)
80 for more information on families and types.
81 .SS Socket-layer functions
82 These functions are used by the user process to send or receive packets
83 and to do other socket operations.
84 For more information see their respective manual pages.
85
86 .BR socket (2)
87 creates a socket,
88 .BR connect (2)
89 connects a socket to a remote socket address,
90 the
91 .BR bind (2)
92 function binds a socket to a local socket address,
93 .BR listen (2)
94 tells the socket that new connections shall be accepted, and
95 .BR accept (2)
96 is used to get a new socket with a new incoming connection.
97 .BR socketpair (2)
98 returns two connected anonymous sockets (implemented only for a few
99 local families like
100 .BR AF_UNIX )
101 .PP
102 .BR send (2),
103 .BR sendto (2),
104 and
105 .BR sendmsg (2)
106 send data over a socket, and
107 .BR recv (2),
108 .BR recvfrom (2),
109 .BR recvmsg (2)
110 receive data from a socket.
111 .BR poll (2)
112 and
113 .BR select (2)
114 wait for arriving data or a readiness to send data.
115 In addition, the standard I/O operations like
116 .BR write (2),
117 .BR writev (2),
118 .BR sendfile (2),
119 .BR read (2),
120 and
121 .BR readv (2)
122 can be used to read and write data.
123 .PP
124 .BR getsockname (2)
125 returns the local socket address and
126 .BR getpeername (2)
127 returns the remote socket address.
128 .BR getsockopt (2)
129 and
130 .BR setsockopt (2)
131 are used to set or get socket layer or protocol options.
132 .BR ioctl (2)
133 can be used to set or read some other options.
134 .PP
135 .BR close (2)
136 is used to close a socket.
137 .BR shutdown (2)
138 closes parts of a full-duplex socket connection.
139 .PP
140 Seeking, or calling
141 .BR pread (2)
142 or
143 .BR pwrite (2)
144 with a nonzero position is not supported on sockets.
145 .PP
146 It is possible to do nonblocking I/O on sockets by setting the
147 .B O_NONBLOCK
148 flag on a socket file descriptor using
149 .BR fcntl (2).
150 Then all operations that would block will (usually)
151 return with
152 .B EAGAIN
153 (operation should be retried later);
154 .BR connect (2)
155 will return
156 .B EINPROGRESS
157 error.
158 The user can then wait for various events via
159 .BR poll (2)
160 or
161 .BR select (2).
162 .TS
163 tab(:) allbox;
164 c s s
165 l l l.
166 I/O events
167 Event:Poll flag:Occurrence
168 Read:POLLIN:T{
169 New data arrived.
170 T}
171 Read:POLLIN:T{
172 A connection setup has been completed
173 (for connection-oriented sockets)
174 T}
175 Read:POLLHUP:T{
176 A disconnection request has been initiated by the other end.
177 T}
178 Read:POLLHUP:T{
179 A connection is broken (only for connection-oriented protocols).
180 When the socket is written
181 .B SIGPIPE
182 is also sent.
183 T}
184 Write:POLLOUT:T{
185 Socket has enough send buffer space for writing new data.
186 T}
187 Read/Write:T{
188 POLLIN|
189 .br
190 POLLOUT
191 T}:T{
192 An outgoing
193 .BR connect (2)
194 finished.
195 T}
196 Read/Write:POLLERR:An asynchronous error occurred.
197 Read/Write:POLLHUP:The other end has shut down one direction.
198 Exception:POLLPRI:T{
199 Urgent data arrived.
200 .B SIGURG
201 is sent then.
202 T}
203 .\" FIXME . The following is not true currently:
204 .\" It is no I/O event when the connection
205 .\" is broken from the local end using
206 .\" .BR shutdown (2)
207 .\" or
208 .\" .BR close (2).
209 .TE
210 .PP
211 An alternative to
212 .BR poll (2)
213 and
214 .BR select (2)
215 is to let the kernel inform the application about events
216 via a
217 .B SIGIO
218 signal.
219 For that the
220 .B O_ASYNC
221 flag must be set on a socket file descriptor via
222 .BR fcntl (2)
223 and a valid signal handler for
224 .B SIGIO
225 must be installed via
226 .BR sigaction (2).
227 See the
228 .I Signals
229 discussion below.
230 .SS Socket address structures
231 Each socket domain has its own format for socket addresses,
232 with a domain-specific address structure.
233 Each of these structures begins with an
234 integer "family" field (typed as
235 .IR sa_family_t )
236 that indicates the type of the address structure.
237 This allows
238 the various system calls (e.g.,
239 .BR connect (2),
240 .BR bind (2),
241 .BR accept (2),
242 .BR getsockname (2),
243 .BR getpeername (2)),
244 which are generic to all socket domains,
245 to determine the domain of a particular socket address.
246
247 To allow any type of socket address to be passed to
248 interfaces in the sockets API,
249 the type
250 .IR "struct sockaddr"
251 is defined.
252 The purpose of this type is purely to allow casting of
253 domain-specific socket address types to a "generic" type,
254 so as to avoid compiler warnings about type mismatches in
255 calls to the sockets API.
256
257 In addition, the sockets API provides the data type
258 .IR "struct sockaddr_storage".
259 This type
260 is suitable to accommodate all supported domain-specific socket
261 address structures; it is large enough and is aligned properly.
262 (In particular, it is large enough to hold
263 IPv6 socket addresses.)
264 The structure includes the following field, which can be used to identify
265 the type of socket address actually stored in the structure:
266
267 .in +4n
268 .nf
269 sa_family_t ss_family;
270 .fi
271 .in
272
273 The
274 .I sockaddr_storage
275 structure is useful in programs that must handle socket addresses
276 in a generic way
277 (e.g., programs that must deal with both IPv4 and IPv6 socket addresses).
278 .SS Socket options
279 The socket options listed below can be set by using
280 .BR setsockopt (2)
281 and read with
282 .BR getsockopt (2)
283 with the socket level set to
284 .B SOL_SOCKET
285 for all sockets.
286 Unless otherwise noted,
287 .I optval
288 is a pointer to an
289 .IR int .
290 .\" FIXME .
291 .\" In the list below, the text used to describe argument types
292 .\" for each socket option should be more consistent
293 .\"
294 .\" SO_ACCEPTCONN is in POSIX.1-2001, and its origin is explained in
295 .\" W R Stevens, UNPv1
296 .TP
297 .B SO_ACCEPTCONN
298 Returns a value indicating whether or not this socket has been marked
299 to accept connections with
300 .BR listen (2).
301 The value 0 indicates that this is not a listening socket,
302 the value 1 indicates that this is a listening socket.
303 This socket option is read-only.
304 .TP
305 .B SO_BINDTODEVICE
306 Bind this socket to a particular device like \(lqeth0\(rq,
307 as specified in the passed interface name.
308 If the
309 name is an empty string or the option length is zero, the socket device
310 binding is removed.
311 The passed option is a variable-length null-terminated
312 interface name string with the maximum size of
313 .BR IFNAMSIZ .
314 If a socket is bound to an interface,
315 only packets received from that particular interface are processed by the
316 socket.
317 Note that this works only for some socket types, particularly
318 .B AF_INET
319 sockets.
320 It is not supported for packet sockets (use normal
321 .BR bind (2)
322 there).
323
324 Before Linux 3.8,
325 this socket option could be set, but could not retrieved with
326 .BR getsockopt (2).
327 Since Linux 3.8, it is readable.
328 The
329 .I optlen
330 argument should contain the buffer size available
331 to receive the device name and is recommended to be
332 .BR IFNAMSZ
333 bytes.
334 The real device name length is reported back in the
335 .I optlen
336 argument.
337 .TP
338 .B SO_BROADCAST
339 Set or get the broadcast flag.
340 When enabled, datagram sockets are allowed to send
341 packets to a broadcast address.
342 This option has no effect on stream-oriented sockets.
343 .TP
344 .B SO_BSDCOMPAT
345 Enable BSD bug-to-bug compatibility.
346 This is used by the UDP protocol module in Linux 2.0 and 2.2.
347 If enabled, ICMP errors received for a UDP socket will not be passed
348 to the user program.
349 In later kernel versions, support for this option has been phased out:
350 Linux 2.4 silently ignores it, and Linux 2.6 generates a kernel warning
351 (printk()) if a program uses this option.
352 Linux 2.0 also enabled BSD bug-to-bug compatibility
353 options (random header changing, skipping of the broadcast flag) for raw
354 sockets with this option, but that was removed in Linux 2.2.
355 .TP
356 .B SO_DEBUG
357 Enable socket debugging.
358 Only allowed for processes with the
359 .B CAP_NET_ADMIN
360 capability or an effective user ID of 0.
361 .TP
362 .BR SO_DOMAIN " (since Linux 2.6.32)"
363 Retrieves the socket domain as an integer, returning a value such as
364 .BR AF_INET6 .
365 See
366 .BR socket (2)
367 for details.
368 This socket option is read-only.
369 .TP
370 .B SO_ERROR
371 Get and clear the pending socket error.
372 This socket option is read-only.
373 Expects an integer.
374 .TP
375 .B SO_DONTROUTE
376 Don't send via a gateway, send only to directly connected hosts.
377 The same effect can be achieved by setting the
378 .B MSG_DONTROUTE
379 flag on a socket
380 .BR send (2)
381 operation.
382 Expects an integer boolean flag.
383 .TP
384 .B SO_KEEPALIVE
385 Enable sending of keep-alive messages on connection-oriented sockets.
386 Expects an integer boolean flag.
387 .TP
388 .B SO_LINGER
389 Sets or gets the
390 .B SO_LINGER
391 option.
392 The argument is a
393 .I linger
394 structure.
395 .sp
396 .in +4n
397 .nf
398 struct linger {
399 int l_onoff; /* linger active */
400 int l_linger; /* how many seconds to linger for */
401 };
402 .fi
403 .in
404 .IP
405 When enabled, a
406 .BR close (2)
407 or
408 .BR shutdown (2)
409 will not return until all queued messages for the socket have been
410 successfully sent or the linger timeout has been reached.
411 Otherwise,
412 the call returns immediately and the closing is done in the background.
413 When the socket is closed as part of
414 .BR exit (2),
415 it always lingers in the background.
416 .TP
417 .BR SO_MARK " (since Linux 2.6.25)"
418 .\" commit 4a19ec5800fc3bb64e2d87c4d9fdd9e636086fe0
419 .\" and 914a9ab386a288d0f22252fc268ecbc048cdcbd5
420 Set the mark for each packet sent through this socket
421 (similar to the netfilter MARK target but socket-based).
422 Changing the mark can be used for mark-based
423 routing without netfilter or for packet filtering.
424 Setting this option requires the
425 .B CAP_NET_ADMIN
426 capability.
427 .TP
428 .B SO_OOBINLINE
429 If this option is enabled,
430 out-of-band data is directly placed into the receive data stream.
431 Otherwise, out-of-band data is passed only when the
432 .B MSG_OOB
433 flag is set during receiving.
434 .\" don't document it because it can do too much harm.
435 .\".B SO_NO_CHECK
436 .\" The kernel has support for the SO_NO_CHECK socket
437 .\" option (boolean: 0 == default, calculate checksum on xmit,
438 .\" 1 == do not calculate checksum on xmit).
439 .\" Additional note from Andi Kleen on SO_NO_CHECK (2010-08-30)
440 .\" On Linux UDP checksums are essentially free and there's no reason
441 .\" to turn them off and it would disable another safety line.
442 .\" That is why I didn't document the option.
443 .TP
444 .B SO_PASSCRED
445 Enable or disable the receiving of the
446 .B SCM_CREDENTIALS
447 control message.
448 For more information see
449 .BR unix (7).
450 .\" FIXME Document SO_PASSSEC, added in 2.6.18; there is some info
451 .\" in the 2.6.18 ChangeLog
452 .TP
453 .BR SO_PEEK_OFF " (since Linux 3.4)"
454 .\" commit ef64a54f6e558155b4f149bb10666b9e914b6c54
455 This option, which is currently supported only for
456 .BR unix (7)
457 sockets, sets the value of the "peek offset" for the
458 .BR recv (2)
459 system call when used with
460 .BR MSG_PEEK
461 flag.
462
463 When this option is set to a negative value
464 (it is set to \-1 for all new sockets),
465 traditional behavior is provided:
466 .BR recv (2)
467 with the
468 .BR MSG_PEEK
469 flag will peek data from the front of the queue.
470
471 When the option is set to a value greater than or equal to zero,
472 then the next peek at data queued in the socket will occur at
473 the byte offset specified by the option value.
474 At the same time, the "peek offset" will be
475 incremented by the number of bytes that were peeked from the queue,
476 so that a subsequent peek will return the next data in the queue.
477
478 If data is removed from the front of the queue via a call to
479 .BR recv (2)
480 (or similar) without the
481 .BR MSG_PEEK
482 flag, the "peek offset" will be decreased by the number of bytes removed.
483 In other words, receiving data without the
484 .B MSG_PEEK
485 flag will cause the "peek offset" to be adjusted to maintain
486 the correct relative position in the queued data,
487 so that a subsequent peek will retrieve the data that would have been
488 retrieved had the data not been removed.
489
490 For datagram sockets, if the "peek offset" points to the middle of a packet,
491 the data returned will be marked with the
492 .BR MSG_TRUNC
493 flag.
494
495 The following example serves to illustrate the use of
496 .BR SO_PEEK_OFF .
497 Suppose a stream socket has the following queued input data:
498
499 aabbccddeeff
500
501 .IP
502 The following sequence of
503 .BR recv (2)
504 calls would have the effect noted in the comments:
505
506 .in +4n
507 .nf
508 int ov = 4; // Set peek offset to 4
509 setsockopt(fd, SOL_SOCKET, SO_PEEK_OFF, &ov, sizeof(ov));
510
511 recv(fd, buf, 2, MSG_PEEK); // Peeks "cc"; offset set to 6
512 recv(fd, buf, 2, MSG_PEEK); // Peeks "dd"; offset set to 8
513 recv(fd, buf, 2, 0); // Reads "aa"; offset set to 6
514 recv(fd, buf, 2, MSG_PEEK); // Peeks "ee"; offset set to 8
515 .fi
516 .in
517 .TP
518 .B SO_PEERCRED
519 Return the credentials of the foreign process connected to this socket.
520 This is possible only for connected
521 .B AF_UNIX
522 stream sockets and
523 .B AF_UNIX
524 stream and datagram socket pairs created using
525 .BR socketpair (2);
526 see
527 .BR unix (7).
528 The returned credentials are those that were in effect at the time
529 of the call to
530 .BR connect (2)
531 or
532 .BR socketpair (2).
533 The argument is a
534 .I ucred
535 structure; define the
536 .B _GNU_SOURCE
537 feature test macro to obtain the definition of that structure from
538 .IR <sys/socket.h> .
539 This socket option is read-only.
540 .TP
541 .B SO_PRIORITY
542 Set the protocol-defined priority for all packets to be sent on
543 this socket.
544 Linux uses this value to order the networking queues:
545 packets with a higher priority may be processed first depending
546 on the selected device queueing discipline.
547 .\" For
548 .\" .BR ip (7),
549 .\" this also sets the IP type-of-service (TOS) field for outgoing packets.
550 Setting a priority outside the range 0 to 6 requires the
551 .B CAP_NET_ADMIN
552 capability.
553 .TP
554 .BR SO_PROTOCOL " (since Linux 2.6.32)"
555 Retrieves the socket protocol as an integer, returning a value such as
556 .BR IPPROTO_SCTP .
557 See
558 .BR socket (2)
559 for details.
560 This socket option is read-only.
561 .TP
562 .B SO_RCVBUF
563 Sets or gets the maximum socket receive buffer in bytes.
564 The kernel doubles this value (to allow space for bookkeeping overhead)
565 when it is set using
566 .\" Most (all?) other implementations do not do this -- MTK, Dec 05
567 .BR setsockopt (2),
568 and this doubled value is returned by
569 .BR getsockopt (2).
570 .\" The following thread on LMKL is quite informative:
571 .\" getsockopt/setsockopt with SO_RCVBUF and SO_SNDBUF "non-standard" behavior
572 .\" 17 July 2012
573 .\" http://thread.gmane.org/gmane.linux.kernel/1328935
574 The default value is set by the
575 .I /proc/sys/net/core/rmem_default
576 file, and the maximum allowed value is set by the
577 .I /proc/sys/net/core/rmem_max
578 file.
579 The minimum (doubled) value for this option is 256.
580 .TP
581 .BR SO_RCVBUFFORCE " (since Linux 2.6.14)"
582 Using this socket option, a privileged
583 .RB ( CAP_NET_ADMIN )
584 process can perform the same task as
585 .BR SO_RCVBUF ,
586 but the
587 .I rmem_max
588 limit can be overridden.
589 .TP
590 .BR SO_RCVLOWAT " and " SO_SNDLOWAT
591 Specify the minimum number of bytes in the buffer until the socket layer
592 will pass the data to the protocol
593 .RB ( SO_SNDLOWAT )
594 or the user on receiving
595 .RB ( SO_RCVLOWAT ).
596 These two values are initialized to 1.
597 .B SO_SNDLOWAT
598 is not changeable on Linux
599 .RB ( setsockopt (2)
600 fails with the error
601 .BR ENOPROTOOPT ).
602 .B SO_RCVLOWAT
603 is changeable
604 only since Linux 2.4.
605 The
606 .BR select (2)
607 and
608 .BR poll (2)
609 system calls currently do not respect the
610 .B SO_RCVLOWAT
611 setting on Linux,
612 and mark a socket readable when even a single byte of data is available.
613 A subsequent read from the socket will block until
614 .B SO_RCVLOWAT
615 bytes are available.
616 .\" See http://marc.theaimsgroup.com/?l=linux-kernel&m=111049368106984&w=2
617 .\" Tested on kernel 2.6.14 -- mtk, 30 Nov 05
618 .TP
619 .BR SO_RCVTIMEO " and " SO_SNDTIMEO
620 .\" Not implemented in 2.0.
621 .\" Implemented in 2.1.11 for getsockopt: always return a zero struct.
622 .\" Implemented in 2.3.41 for setsockopt, and actually used.
623 Specify the receiving or sending timeouts until reporting an error.
624 The argument is a
625 .IR "struct timeval" .
626 If an input or output function blocks for this period of time, and
627 data has been sent or received, the return value of that function
628 will be the amount of data transferred; if no data has been transferred
629 and the timeout has been reached, then \-1 is returned with
630 .I errno
631 set to
632 .BR EAGAIN
633 or
634 .BR EWOULDBLOCK ,
635 .\" in fact to EAGAIN
636 or
637 .B EINPROGRESS
638 (for
639 .BR connect (2))
640 just as if the socket was specified to be nonblocking.
641 If the timeout is set to zero (the default),
642 then the operation will never timeout.
643 Timeouts only have effect for system calls that perform socket I/O (e.g.,
644 .BR read (2),
645 .BR recvmsg (2),
646 .BR send (2),
647 .BR sendmsg (2));
648 timeouts have no effect for
649 .BR select (2),
650 .BR poll (2),
651 .BR epoll_wait (2),
652 and so on.
653 .TP
654 .B SO_REUSEADDR
655 .\" commit c617f398edd4db2b8567a28e899a88f8f574798d
656 .\" https://lwn.net/Articles/542629/
657 Indicates that the rules used in validating addresses supplied in a
658 .BR bind (2)
659 call should allow reuse of local addresses.
660 For
661 .B AF_INET
662 sockets this
663 means that a socket may bind, except when there
664 is an active listening socket bound to the address.
665 When the listening socket is bound to
666 .B INADDR_ANY
667 with a specific port then it is not possible
668 to bind to this port for any local address.
669 Argument is an integer boolean flag.
670 .TP
671 .BR SO_REUSEPORT " (since Linux 3.9)"
672 Permits multiple
673 .B AF_INET
674 or
675 .B AF_INET6
676 sockets to be bound to an identical socket address.
677 This option must be set on each socket (including the first socket)
678 prior to calling
679 .BR bind (2)
680 on the socket.
681 To prevent port hijacking,
682 all of the processes binding to the same address must have the same
683 effective UID.
684 This option can be employed with both TCP and UDP sockets.
685
686 For TCP sockets, this option allows
687 .BR accept (2)
688 load distribution in a multi-threaded server to be improved by
689 using a distinct listener socket for each thread.
690 This provides improved load distribution as compared
691 to traditional techniques such using a single
692 .BR accept (2)ing
693 thread that distributes connections,
694 or having multiple threads that compete to
695 .BR accept (2)
696 from the same socket.
697
698 For UDP sockets,
699 the use of this option can provide better distribution
700 of incoming datagrams to multiple processes (or threads) as compared
701 to the traditional technique of having multiple processes
702 compete to receive datagrams on the same socket.
703 .TP
704 .BR SO_RXQ_OVFL " (since Linux 2.6.33)"
705 .\" commit 3b885787ea4112eaa80945999ea0901bf742707f
706 Indicates that an unsigned 32-bit value ancillary message (cmsg)
707 should be attached to received skbs indicating
708 the number of packets dropped by the socket between
709 the last received packet and this received packet.
710 .TP
711 .B SO_SNDBUF
712 Sets or gets the maximum socket send buffer in bytes.
713 The kernel doubles this value (to allow space for bookkeeping overhead)
714 when it is set using
715 .\" Most (all?) other implementations do not do this -- MTK, Dec 05
716 .\" See also the comment to SO_RCVBUF (17 Jul 2012 LKML mail)
717 .BR setsockopt (2),
718 and this doubled value is returned by
719 .BR getsockopt (2).
720 The default value is set by the
721 .I /proc/sys/net/core/wmem_default
722 file and the maximum allowed value is set by the
723 .I /proc/sys/net/core/wmem_max
724 file.
725 The minimum (doubled) value for this option is 2048.
726 .TP
727 .BR SO_SNDBUFFORCE " (since Linux 2.6.14)"
728 Using this socket option, a privileged
729 .RB ( CAP_NET_ADMIN )
730 process can perform the same task as
731 .BR SO_SNDBUF ,
732 but the
733 .I wmem_max
734 limit can be overridden.
735 .TP
736 .B SO_TIMESTAMP
737 Enable or disable the receiving of the
738 .B SO_TIMESTAMP
739 control message.
740 The timestamp control message is sent with level
741 .B SOL_SOCKET
742 and the
743 .I cmsg_data
744 field is a
745 .I "struct timeval"
746 indicating the
747 reception time of the last packet passed to the user in this call.
748 See
749 .BR cmsg (3)
750 for details on control messages.
751 .TP
752 .B SO_TYPE
753 Gets the socket type as an integer (e.g.,
754 .BR SOCK_STREAM ).
755 This socket option is read-only.
756 .TP
757 .BR SO_BUSY_POLL " (since Linux 3.11)"
758 Sets the approximate time in microseconds to busy poll on a blocking receive
759 when there is no data.
760 Increasing this value requires
761 .BR CAP_NET_ADMIN .
762 The default for this option is controlled by the
763 .I /proc/sys/net/core/busy_read
764 file.
765
766 The value in the
767 .I /proc/sys/net/core/busy_poll
768 file determines how long
769 .BR select (2)
770 and
771 .BR poll (2)
772 will busy poll when they operate on sockets with
773 .BR SO_BUSY_POLL
774 set and no events to report are found.
775
776 In both cases,
777 busy polling will only be done when the socket last received data
778 from a network device that supports this option.
779
780 While busy polling may improve latency of some applications,
781 care must be taken when using it since this will increase
782 both CPU utilization and power usage.
783 .SS Signals
784 When writing onto a connection-oriented socket that has been shut down
785 (by the local or the remote end)
786 .B SIGPIPE
787 is sent to the writing process and
788 .B EPIPE
789 is returned.
790 The signal is not sent when the write call
791 specified the
792 .B MSG_NOSIGNAL
793 flag.
794 .PP
795 When requested with the
796 .B FIOSETOWN
797 .BR fcntl (2)
798 or
799 .B SIOCSPGRP
800 .BR ioctl (2),
801 .B SIGIO
802 is sent when an I/O event occurs.
803 It is possible to use
804 .BR poll (2)
805 or
806 .BR select (2)
807 in the signal handler to find out which socket the event occurred on.
808 An alternative (in Linux 2.2) is to set a real-time signal using the
809 .B F_SETSIG
810 .BR fcntl (2);
811 the handler of the real time signal will be called with
812 the file descriptor in the
813 .I si_fd
814 field of its
815 .IR siginfo_t .
816 See
817 .BR fcntl (2)
818 for more information.
819 .PP
820 Under some circumstances (e.g., multiple processes accessing a
821 single socket), the condition that caused the
822 .B SIGIO
823 may have already disappeared when the process reacts to the signal.
824 If this happens, the process should wait again because Linux
825 will resend the signal later.
826 .\" .SS Ancillary messages
827 .SS /proc interfaces
828 The core socket networking parameters can be accessed
829 via files in the directory
830 .IR /proc/sys/net/core/ .
831 .TP
832 .I rmem_default
833 contains the default setting in bytes of the socket receive buffer.
834 .TP
835 .I rmem_max
836 contains the maximum socket receive buffer size in bytes which a user may
837 set by using the
838 .B SO_RCVBUF
839 socket option.
840 .TP
841 .I wmem_default
842 contains the default setting in bytes of the socket send buffer.
843 .TP
844 .I wmem_max
845 contains the maximum socket send buffer size in bytes which a user may
846 set by using the
847 .B SO_SNDBUF
848 socket option.
849 .TP
850 .IR message_cost " and " message_burst
851 configure the token bucket filter used to load limit warning messages
852 caused by external network events.
853 .TP
854 .I netdev_max_backlog
855 Maximum number of packets in the global input queue.
856 .TP
857 .I optmem_max
858 Maximum length of ancillary data and user control data like the iovecs
859 per socket.
860 .\" netdev_fastroute is not documented because it is experimental
861 .SS Ioctls
862 These operations can be accessed using
863 .BR ioctl (2):
864
865 .in +4n
866 .nf
867 .IB error " = ioctl(" ip_socket ", " ioctl_type ", " &value_result ");"
868 .fi
869 .in
870 .TP
871 .B SIOCGSTAMP
872 Return a
873 .I struct timeval
874 with the receive timestamp of the last packet passed to the user.
875 This is useful for accurate round trip time measurements.
876 See
877 .BR setitimer (2)
878 for a description of
879 .IR "struct timeval" .
880 .\"
881 This ioctl should be used only if the socket option
882 .B SO_TIMESTAMP
883 is not set on the socket.
884 Otherwise, it returns the timestamp of the
885 last packet that was received while
886 .B SO_TIMESTAMP
887 was not set, or it fails if no such packet has been received,
888 (i.e.,
889 .BR ioctl (2)
890 returns \-1 with
891 .I errno
892 set to
893 .BR ENOENT ).
894 .TP
895 .B SIOCSPGRP
896 Set the process or process group to send
897 .B SIGIO
898 or
899 .B SIGURG
900 signals
901 to when an
902 asynchronous I/O operation has finished or urgent data is available.
903 The argument is a pointer to a
904 .IR pid_t .
905 If the argument is positive, send the signals to that process.
906 If the
907 argument is negative, send the signals to the process group with the ID
908 of the absolute value of the argument.
909 The process may only choose itself or its own process group to receive
910 signals unless it has the
911 .B CAP_KILL
912 capability or an effective UID of 0.
913 .TP
914 .B FIOASYNC
915 Change the
916 .B O_ASYNC
917 flag to enable or disable asynchronous I/O mode of the socket.
918 Asynchronous I/O mode means that the
919 .B SIGIO
920 signal or the signal set with
921 .B F_SETSIG
922 is raised when a new I/O event occurs.
923 .IP
924 Argument is an integer boolean flag.
925 (This operation is synonymous with the use of
926 .BR fcntl (2)
927 to set the
928 .B O_ASYNC
929 flag.)
930 .\"
931 .TP
932 .B SIOCGPGRP
933 Get the current process or process group that receives
934 .B SIGIO
935 or
936 .B SIGURG
937 signals,
938 or 0
939 when none is set.
940 .PP
941 Valid
942 .BR fcntl (2)
943 operations:
944 .TP
945 .B FIOGETOWN
946 The same as the
947 .B SIOCGPGRP
948 .BR ioctl (2).
949 .TP
950 .B FIOSETOWN
951 The same as the
952 .B SIOCSPGRP
953 .BR ioctl (2).
954 .SH VERSIONS
955 .B SO_BINDTODEVICE
956 was introduced in Linux 2.0.30.
957 .B SO_PASSCRED
958 is new in Linux 2.2.
959 The
960 .I /proc
961 interfaces were introduced in Linux 2.2.
962 .B SO_RCVTIMEO
963 and
964 .B SO_SNDTIMEO
965 are supported since Linux 2.3.41.
966 Earlier, timeouts were fixed to
967 a protocol-specific setting, and could not be read or written.
968 .SH NOTES
969 Linux assumes that half of the send/receive buffer is used for internal
970 kernel structures; thus the values in the corresponding
971 .I /proc
972 files are twice what can be observed on the wire.
973
974 Linux will allow port reuse only with the
975 .B SO_REUSEADDR
976 option
977 when this option was set both in the previous program that performed a
978 .BR bind (2)
979 to the port and in the program that wants to reuse the port.
980 This differs from some implementations (e.g., FreeBSD)
981 where only the later program needs to set the
982 .B SO_REUSEADDR
983 option.
984 Typically this difference is invisible, since, for example, a server
985 program is designed to always set this option.
986 .SH BUGS
987 The
988 .B CONFIG_FILTER
989 socket options
990 .B SO_ATTACH_FILTER
991 and
992 .B SO_DETACH_FILTER
993 .\" FIXME Document SO_ATTACH_FILTER and SO_DETACH_FILTER
994 are not documented.
995 The suggested interface to use them is via the libpcap
996 library.
997 .\" .SH AUTHORS
998 .\" This man page was written by Andi Kleen.
999 .SH SEE ALSO
1000 .BR connect (2),
1001 .BR getsockopt (2),
1002 .BR setsockopt (2),
1003 .BR socket (2),
1004 .BR capabilities (7),
1005 .BR ddp (7),
1006 .BR ip (7),
1007 .BR packet (7),
1008 .BR tcp (7),
1009 .BR udp (7),
1010 .BR unix (7)