2 .\" This man page is Copyright (C) 1999 Andi Kleen <ak@muc.de>.
3 .\" and copyright (c) 1999 Matthew Wilcox.
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.
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.
21 .\" The following are not yet documented:
22 .\" SO_PEERNAME (2.4?)
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
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_SELECT_ERR_QUEUE (3.10)
45 .\" commit 7d4c04fc170087119727119074e72445f2bb192b
46 .\" Author: Keller, Jacob E <jacob.e.keller@intel.com>
47 .\" SO_MAX_PACING_RATE (3.13)
48 .\" commit 62748f32d501f5d3712a7c372bbb92abc7c62bc7
49 .\" Author: Eric Dumazet <edumazet@google.com>
50 .\" SO_BPF_EXTENSIONS (3.14)
51 .\" commit ea02f9411d9faa3553ed09ce0ec9f00ceae9885e
52 .\" Author: Michal Sekletar <msekleta@redhat.com>
54 .TH SOCKET 7 2015-05-07 Linux "Linux Programmer's Manual"
56 socket \- Linux socket interface
58 .B #include <sys/socket.h>
60 .IB sockfd " = socket(int " socket_family ", int " socket_type ", int " protocol );
62 This manual page describes the Linux networking socket layer user
64 The BSD compatible sockets
65 are the uniform interface
66 between the user process and the network protocol stacks in the kernel.
67 The protocol modules are grouped into
70 .BR AF_INET ", " AF_IPX ", and " AF_PACKET ,
79 for more information on families and types.
80 .SS Socket-layer functions
81 These functions are used by the user process to send or receive packets
82 and to do other socket operations.
83 For more information see their respective manual pages.
88 connects a socket to a remote socket address,
91 function binds a socket to a local socket address,
93 tells the socket that new connections shall be accepted, and
95 is used to get a new socket with a new incoming connection.
97 returns two connected anonymous sockets (implemented only for a few
105 send data over a socket, and
109 receive data from a socket.
113 wait for arriving data or a readiness to send data.
114 In addition, the standard I/O operations like
121 can be used to read and write data.
124 returns the local socket address and
126 returns the remote socket address.
130 are used to set or get socket layer or protocol options.
132 can be used to set or read some other options.
135 is used to close a socket.
137 closes parts of a full-duplex socket connection.
143 with a nonzero position is not supported on sockets.
145 It is possible to do nonblocking I/O on sockets by setting the
147 flag on a socket file descriptor using
149 Then all operations that would block will (usually)
152 (operation should be retried later);
157 The user can then wait for various events via
166 Event:Poll flag:Occurrence
171 A connection setup has been completed
172 (for connection-oriented sockets)
175 A disconnection request has been initiated by the other end.
178 A connection is broken (only for connection-oriented protocols).
179 When the socket is written
184 Socket has enough send buffer space for writing new data.
195 Read/Write:POLLERR:An asynchronous error occurred.
196 Read/Write:POLLHUP:The other end has shut down one direction.
202 .\" FIXME . The following is not true currently:
203 .\" It is no I/O event when the connection
204 .\" is broken from the local end using
214 is to let the kernel inform the application about events
220 flag must be set on a socket file descriptor via
222 and a valid signal handler for
224 must be installed via
229 .SS Socket address structures
230 Each socket domain has its own format for socket addresses,
231 with a domain-specific address structure.
232 Each of these structures begins with an
233 integer "family" field (typed as
235 that indicates the type of the address structure.
237 the various system calls (e.g.,
242 .BR getpeername (2)),
243 which are generic to all socket domains,
244 to determine the domain of a particular socket address.
246 To allow any type of socket address to be passed to
247 interfaces in the sockets API,
249 .IR "struct sockaddr"
251 The purpose of this type is purely to allow casting of
252 domain-specific socket address types to a "generic" type,
253 so as to avoid compiler warnings about type mismatches in
254 calls to the sockets API.
256 In addition, the sockets API provides the data type
257 .IR "struct sockaddr_storage".
259 is suitable to accommodate all supported domain-specific socket
260 address structures; it is large enough and is aligned properly.
261 (In particular, it is large enough to hold
262 IPv6 socket addresses.)
263 The structure includes the following field, which can be used to identify
264 the type of socket address actually stored in the structure:
268 sa_family_t ss_family;
274 structure is useful in programs that must handle socket addresses
276 (e.g., programs that must deal with both IPv4 and IPv6 socket addresses).
278 The socket options listed below can be set by using
282 with the socket level set to
285 Unless otherwise noted,
290 .\" In the list below, the text used to describe argument types
291 .\" for each socket option should be more consistent
293 .\" SO_ACCEPTCONN is in POSIX.1-2001, and its origin is explained in
294 .\" W R Stevens, UNPv1
297 Returns a value indicating whether or not this socket has been marked
298 to accept connections with
300 The value 0 indicates that this is not a listening socket,
301 the value 1 indicates that this is a listening socket.
302 This socket option is read-only.
304 .BR SO_ATTACH_FILTER " and " SO_ATTACH_BPF
305 Attach a classic or extended BPF program (respectively) to the socket
306 for use as a filter of incoming packets. A packet will be dropped if
307 the filter program returns zero. If the filter program returns a
308 non-zero value which is less than the packet's data length, the packet
309 will be truncated to the length returned. If the value returned by
310 the filter is greater than or equal to the packet's data length, the
311 packet is allowed to proceed unmodified.
324 struct sock_filter *filter;
331 is a file descriptor returned by the
333 system call and must refer to a program of type
334 .BR BPF_PROG_TYPE_SOCKET_FILTER.
335 These options may be set multiple times for a given socket, each time
336 replacing the previous filter program. The classic and extended
337 versions may be called on the same socket, but the previous filter
338 will always be replaced such that a socket never has more than one
342 is available since Linux 2.2.
344 is available since Linux 3.19. Both classic and extended BPF are
345 explained in the kernel source file
346 .I Documentation/networking/filter.txt
348 .BR SO_ATTACH_REUSEPORT_CBPF ", " SO_ATTACH_REUSEPORT_EBPF " (since Linux 4.5)"
351 option, these options allow the user to set a classic or extended
352 BPF program (respectively) which defines how packets are assigned to
353 the sockets in the reuseport group (that is, all sockets which have
355 set and are using the same local address to receive packets). The BPF
356 program must return an index between 0 and N-1 representing the socket
357 which should receive the packet (where N is the number of sockets in
358 the group). If the BPF program returns an invalid index, socket
359 selection will fall back to the plain
363 Sockets are numbered in the order in which they are added to the group
364 (that is, the order of
366 calls for UDP sockets or the order of
368 calls for TCP sockets). New sockets added to a reuseport group will
369 inherit the BPF program. When a socket is removed from a reuseport
372 the last socket in the group will be moved into the closed socket's
375 These options may be set repeatedly at any time on any single socket
376 in the group to replace the current BPF program used by all sockets in
378 .BR SO_ATTACH_REUSEPORT_CBPF
379 takes the same socket argument type as
382 .BR SO_ATTACH_REUSEPORT_EBPF
383 takes the same socket argument type as
385 UDP support for this feature is available since Linux 4.5.
386 TCP support for this feature is available since Linux 4.6.
389 Bind this socket to a particular device like \(lqeth0\(rq,
390 as specified in the passed interface name.
392 name is an empty string or the option length is zero, the socket device
394 The passed option is a variable-length null-terminated
395 interface name string with the maximum size of
397 If a socket is bound to an interface,
398 only packets received from that particular interface are processed by the
400 Note that this works only for some socket types, particularly
403 It is not supported for packet sockets (use normal
408 this socket option could be set, but could not retrieved with
410 Since Linux 3.8, it is readable.
413 argument should contain the buffer size available
414 to receive the device name and is recommended to be
417 The real device name length is reported back in the
422 Set or get the broadcast flag.
423 When enabled, datagram sockets are allowed to send
424 packets to a broadcast address.
425 This option has no effect on stream-oriented sockets.
428 Enable BSD bug-to-bug compatibility.
429 This is used by the UDP protocol module in Linux 2.0 and 2.2.
430 If enabled, ICMP errors received for a UDP socket will not be passed
432 In later kernel versions, support for this option has been phased out:
433 Linux 2.4 silently ignores it, and Linux 2.6 generates a kernel warning
434 (printk()) if a program uses this option.
435 Linux 2.0 also enabled BSD bug-to-bug compatibility
436 options (random header changing, skipping of the broadcast flag) for raw
437 sockets with this option, but that was removed in Linux 2.2.
440 Enable socket debugging.
441 Only allowed for processes with the
443 capability or an effective user ID of 0.
445 .BR SO_DETACH_FILTER " and " SO_DETACH_BPF
446 These options may be used to remove the BPF program attached to the
451 The option value is ignored.
453 is available since Linux 2.2.
455 is available since Linux 3.19.
457 .BR SO_DOMAIN " (since Linux 2.6.32)"
458 Retrieves the socket domain as an integer, returning a value such as
463 This socket option is read-only.
466 Get and clear the pending socket error.
467 This socket option is read-only.
471 Don't send via a gateway, send only to directly connected hosts.
472 The same effect can be achieved by setting the
477 Expects an integer boolean flag.
480 Enable sending of keep-alive messages on connection-oriented sockets.
481 Expects an integer boolean flag.
494 int l_onoff; /* linger active */
495 int l_linger; /* how many seconds to linger for */
504 will not return until all queued messages for the socket have been
505 successfully sent or the linger timeout has been reached.
507 the call returns immediately and the closing is done in the background.
508 When the socket is closed as part of
510 it always lingers in the background.
513 When set, this option will prevent an unprivileged process from
514 changing the filters associated with the socket. These filters
515 include any set using the socket options
516 .BR SO_ATTACH_FILTER,
518 .BR SO_ATTACH_REUSEPORT_CBPF
520 .BR SO_ATTACH_REUSEPORT_EPBF.
521 The typical use case is for a privileged process to setup a socket with
522 restrictive filters, set
524 and then either drop its privileges or pass the socket file descriptor
525 to an unprivileged process. Attempts to change a filter by an
526 unprivileged process while
528 is set will result in an error with value
531 .BR SO_MARK " (since Linux 2.6.25)"
532 .\" commit 4a19ec5800fc3bb64e2d87c4d9fdd9e636086fe0
533 .\" and 914a9ab386a288d0f22252fc268ecbc048cdcbd5
534 Set the mark for each packet sent through this socket
535 (similar to the netfilter MARK target but socket-based).
536 Changing the mark can be used for mark-based
537 routing without netfilter or for packet filtering.
538 Setting this option requires the
543 If this option is enabled,
544 out-of-band data is directly placed into the receive data stream.
545 Otherwise, out-of-band data is passed only when the
547 flag is set during receiving.
548 .\" don't document it because it can do too much harm.
550 .\" The kernel has support for the SO_NO_CHECK socket
551 .\" option (boolean: 0 == default, calculate checksum on xmit,
552 .\" 1 == do not calculate checksum on xmit).
553 .\" Additional note from Andi Kleen on SO_NO_CHECK (2010-08-30)
554 .\" On Linux UDP checksums are essentially free and there's no reason
555 .\" to turn them off and it would disable another safety line.
556 .\" That is why I didn't document the option.
559 Enable or disable the receiving of the
562 For more information see
564 .\" FIXME Document SO_PASSSEC, added in 2.6.18; there is some info
565 .\" in the 2.6.18 ChangeLog
567 .BR SO_PEEK_OFF " (since Linux 3.4)"
568 .\" commit ef64a54f6e558155b4f149bb10666b9e914b6c54
569 This option, which is currently supported only for
571 sockets, sets the value of the "peek offset" for the
573 system call when used with
577 When this option is set to a negative value
578 (it is set to \-1 for all new sockets),
579 traditional behavior is provided:
583 flag will peek data from the front of the queue.
585 When the option is set to a value greater than or equal to zero,
586 then the next peek at data queued in the socket will occur at
587 the byte offset specified by the option value.
588 At the same time, the "peek offset" will be
589 incremented by the number of bytes that were peeked from the queue,
590 so that a subsequent peek will return the next data in the queue.
592 If data is removed from the front of the queue via a call to
594 (or similar) without the
596 flag, the "peek offset" will be decreased by the number of bytes removed.
597 In other words, receiving data without the
599 flag will cause the "peek offset" to be adjusted to maintain
600 the correct relative position in the queued data,
601 so that a subsequent peek will retrieve the data that would have been
602 retrieved had the data not been removed.
604 For datagram sockets, if the "peek offset" points to the middle of a packet,
605 the data returned will be marked with the
609 The following example serves to illustrate the use of
611 Suppose a stream socket has the following queued input data:
615 The following sequence of
617 calls would have the effect noted in the comments:
621 int ov = 4; // Set peek offset to 4
622 setsockopt(fd, SOL_SOCKET, SO_PEEK_OFF, &ov, sizeof(ov));
624 recv(fd, buf, 2, MSG_PEEK); // Peeks "cc"; offset set to 6
625 recv(fd, buf, 2, MSG_PEEK); // Peeks "dd"; offset set to 8
626 recv(fd, buf, 2, 0); // Reads "aa"; offset set to 6
627 recv(fd, buf, 2, MSG_PEEK); // Peeks "ee"; offset set to 8
632 Return the credentials of the foreign process connected to this socket.
633 This is possible only for connected
637 stream and datagram socket pairs created using
641 The returned credentials are those that were in effect at the time
648 structure; define the
650 feature test macro to obtain the definition of that structure from
652 This socket option is read-only.
655 Set the protocol-defined priority for all packets to be sent on
657 Linux uses this value to order the networking queues:
658 packets with a higher priority may be processed first depending
659 on the selected device queueing discipline.
662 .\" this also sets the IP type-of-service (TOS) field for outgoing packets.
663 Setting a priority outside the range 0 to 6 requires the
667 .BR SO_PROTOCOL " (since Linux 2.6.32)"
668 Retrieves the socket protocol as an integer, returning a value such as
673 This socket option is read-only.
676 Sets or gets the maximum socket receive buffer in bytes.
677 The kernel doubles this value (to allow space for bookkeeping overhead)
679 .\" Most (all?) other implementations do not do this -- MTK, Dec 05
681 and this doubled value is returned by
683 .\" The following thread on LMKL is quite informative:
684 .\" getsockopt/setsockopt with SO_RCVBUF and SO_SNDBUF "non-standard" behavior
686 .\" http://thread.gmane.org/gmane.linux.kernel/1328935
687 The default value is set by the
688 .I /proc/sys/net/core/rmem_default
689 file, and the maximum allowed value is set by the
690 .I /proc/sys/net/core/rmem_max
692 The minimum (doubled) value for this option is 256.
694 .BR SO_RCVBUFFORCE " (since Linux 2.6.14)"
695 Using this socket option, a privileged
696 .RB ( CAP_NET_ADMIN )
697 process can perform the same task as
701 limit can be overridden.
703 .BR SO_RCVLOWAT " and " SO_SNDLOWAT
704 Specify the minimum number of bytes in the buffer until the socket layer
705 will pass the data to the protocol
707 or the user on receiving
709 These two values are initialized to 1.
711 is not changeable on Linux
717 only since Linux 2.4.
722 system calls currently do not respect the
725 and mark a socket readable when even a single byte of data is available.
726 A subsequent read from the socket will block until
729 .\" See http://marc.theaimsgroup.com/?l=linux-kernel&m=111049368106984&w=2
730 .\" Tested on kernel 2.6.14 -- mtk, 30 Nov 05
732 .BR SO_RCVTIMEO " and " SO_SNDTIMEO
733 .\" Not implemented in 2.0.
734 .\" Implemented in 2.1.11 for getsockopt: always return a zero struct.
735 .\" Implemented in 2.3.41 for setsockopt, and actually used.
736 Specify the receiving or sending timeouts until reporting an error.
738 .IR "struct timeval" .
739 If an input or output function blocks for this period of time, and
740 data has been sent or received, the return value of that function
741 will be the amount of data transferred; if no data has been transferred
742 and the timeout has been reached, then \-1 is returned with
748 .\" in fact to EAGAIN
753 just as if the socket was specified to be nonblocking.
754 If the timeout is set to zero (the default),
755 then the operation will never timeout.
756 Timeouts only have effect for system calls that perform socket I/O (e.g.,
761 timeouts have no effect for
768 .\" commit c617f398edd4db2b8567a28e899a88f8f574798d
769 .\" https://lwn.net/Articles/542629/
770 Indicates that the rules used in validating addresses supplied in a
772 call should allow reuse of local addresses.
776 means that a socket may bind, except when there
777 is an active listening socket bound to the address.
778 When the listening socket is bound to
780 with a specific port then it is not possible
781 to bind to this port for any local address.
782 Argument is an integer boolean flag.
784 .BR SO_REUSEPORT " (since Linux 3.9)"
789 sockets to be bound to an identical socket address.
790 This option must be set on each socket (including the first socket)
794 To prevent port hijacking,
795 all of the processes binding to the same address must have the same
797 This option can be employed with both TCP and UDP sockets.
799 For TCP sockets, this option allows
801 load distribution in a multi-threaded server to be improved by
802 using a distinct listener socket for each thread.
803 This provides improved load distribution as compared
804 to traditional techniques such using a single
806 thread that distributes connections,
807 or having multiple threads that compete to
809 from the same socket.
812 the use of this option can provide better distribution
813 of incoming datagrams to multiple processes (or threads) as compared
814 to the traditional technique of having multiple processes
815 compete to receive datagrams on the same socket.
817 .BR SO_RXQ_OVFL " (since Linux 2.6.33)"
818 .\" commit 3b885787ea4112eaa80945999ea0901bf742707f
819 Indicates that an unsigned 32-bit value ancillary message (cmsg)
820 should be attached to received skbs indicating
821 the number of packets dropped by the socket between
822 the last received packet and this received packet.
825 Sets or gets the maximum socket send buffer in bytes.
826 The kernel doubles this value (to allow space for bookkeeping overhead)
828 .\" Most (all?) other implementations do not do this -- MTK, Dec 05
829 .\" See also the comment to SO_RCVBUF (17 Jul 2012 LKML mail)
831 and this doubled value is returned by
833 The default value is set by the
834 .I /proc/sys/net/core/wmem_default
835 file and the maximum allowed value is set by the
836 .I /proc/sys/net/core/wmem_max
838 The minimum (doubled) value for this option is 2048.
840 .BR SO_SNDBUFFORCE " (since Linux 2.6.14)"
841 Using this socket option, a privileged
842 .RB ( CAP_NET_ADMIN )
843 process can perform the same task as
847 limit can be overridden.
850 Enable or disable the receiving of the
853 The timestamp control message is sent with level
860 reception time of the last packet passed to the user in this call.
863 for details on control messages.
866 Gets the socket type as an integer (e.g.,
868 This socket option is read-only.
870 .BR SO_BUSY_POLL " (since Linux 3.11)"
871 Sets the approximate time in microseconds to busy poll on a blocking receive
872 when there is no data.
873 Increasing this value requires
875 The default for this option is controlled by the
876 .I /proc/sys/net/core/busy_read
880 .I /proc/sys/net/core/busy_poll
881 file determines how long
885 will busy poll when they operate on sockets with
887 set and no events to report are found.
890 busy polling will only be done when the socket last received data
891 from a network device that supports this option.
893 While busy polling may improve latency of some applications,
894 care must be taken when using it since this will increase
895 both CPU utilization and power usage.
897 When writing onto a connection-oriented socket that has been shut down
898 (by the local or the remote end)
900 is sent to the writing process and
903 The signal is not sent when the write call
908 When requested with the
915 is sent when an I/O event occurs.
916 It is possible to use
920 in the signal handler to find out which socket the event occurred on.
921 An alternative (in Linux 2.2) is to set a real-time signal using the
924 the handler of the real time signal will be called with
925 the file descriptor in the
931 for more information.
933 Under some circumstances (e.g., multiple processes accessing a
934 single socket), the condition that caused the
936 may have already disappeared when the process reacts to the signal.
937 If this happens, the process should wait again because Linux
938 will resend the signal later.
939 .\" .SS Ancillary messages
941 The core socket networking parameters can be accessed
942 via files in the directory
943 .IR /proc/sys/net/core/ .
946 contains the default setting in bytes of the socket receive buffer.
949 contains the maximum socket receive buffer size in bytes which a user may
955 contains the default setting in bytes of the socket send buffer.
958 contains the maximum socket send buffer size in bytes which a user may
963 .IR message_cost " and " message_burst
964 configure the token bucket filter used to load limit warning messages
965 caused by external network events.
967 .I netdev_max_backlog
968 Maximum number of packets in the global input queue.
971 Maximum length of ancillary data and user control data like the iovecs
973 .\" netdev_fastroute is not documented because it is experimental
975 These operations can be accessed using
980 .IB error " = ioctl(" ip_socket ", " ioctl_type ", " &value_result ");"
987 with the receive timestamp of the last packet passed to the user.
988 This is useful for accurate round trip time measurements.
992 .IR "struct timeval" .
994 This ioctl should be used only if the socket option
996 is not set on the socket.
997 Otherwise, it returns the timestamp of the
998 last packet that was received while
1000 was not set, or it fails if no such packet has been received,
1009 Set the process or process group to send
1015 asynchronous I/O operation has finished or urgent data is available.
1016 The argument is a pointer to a
1018 If the argument is positive, send the signals to that process.
1020 argument is negative, send the signals to the process group with the ID
1021 of the absolute value of the argument.
1022 The process may only choose itself or its own process group to receive
1023 signals unless it has the
1025 capability or an effective UID of 0.
1030 flag to enable or disable asynchronous I/O mode of the socket.
1031 Asynchronous I/O mode means that the
1033 signal or the signal set with
1035 is raised when a new I/O event occurs.
1037 Argument is an integer boolean flag.
1038 (This operation is synonymous with the use of
1046 Get the current process or process group that receives
1069 was introduced in Linux 2.0.30.
1071 is new in Linux 2.2.
1074 interfaces were introduced in Linux 2.2.
1078 are supported since Linux 2.3.41.
1079 Earlier, timeouts were fixed to
1080 a protocol-specific setting, and could not be read or written.
1082 Linux assumes that half of the send/receive buffer is used for internal
1083 kernel structures; thus the values in the corresponding
1085 files are twice what can be observed on the wire.
1087 Linux will allow port reuse only with the
1090 when this option was set both in the previous program that performed a
1092 to the port and in the program that wants to reuse the port.
1093 This differs from some implementations (e.g., FreeBSD)
1094 where only the later program needs to set the
1097 Typically this difference is invisible, since, for example, a server
1098 program is designed to always set this option.
1100 .\" This man page was written by Andi Kleen.
1108 .BR capabilities (7),