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_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
55 .TH SOCKET 7 2015-05-07 Linux "Linux Programmer's Manual"
57 socket \- Linux socket interface
59 .B #include <sys/socket.h>
61 .IB sockfd " = socket(int " socket_family ", int " socket_type ", int " protocol );
63 This manual page describes the Linux networking socket layer user
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
71 .BR AF_INET ", " AF_IPX ", and " AF_PACKET ,
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.
89 connects a socket to a remote socket address,
92 function binds a socket to a local socket address,
94 tells the socket that new connections shall be accepted, and
96 is used to get a new socket with a new incoming connection.
98 returns two connected anonymous sockets (implemented only for a few
106 send data over a socket, and
110 receive data from a socket.
114 wait for arriving data or a readiness to send data.
115 In addition, the standard I/O operations like
122 can be used to read and write data.
125 returns the local socket address and
127 returns the remote socket address.
131 are used to set or get socket layer or protocol options.
133 can be used to set or read some other options.
136 is used to close a socket.
138 closes parts of a full-duplex socket connection.
144 with a nonzero position is not supported on sockets.
146 It is possible to do nonblocking I/O on sockets by setting the
148 flag on a socket file descriptor using
150 Then all operations that would block will (usually)
153 (operation should be retried later);
158 The user can then wait for various events via
167 Event:Poll flag:Occurrence
172 A connection setup has been completed
173 (for connection-oriented sockets)
176 A disconnection request has been initiated by the other end.
179 A connection is broken (only for connection-oriented protocols).
180 When the socket is written
185 Socket has enough send buffer space for writing new data.
196 Read/Write:POLLERR:An asynchronous error occurred.
197 Read/Write:POLLHUP:The other end has shut down one direction.
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
215 is to let the kernel inform the application about events
221 flag must be set on a socket file descriptor via
223 and a valid signal handler for
225 must be installed via
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
236 that indicates the type of the address structure.
238 the various system calls (e.g.,
243 .BR getpeername (2)),
244 which are generic to all socket domains,
245 to determine the domain of a particular socket address.
247 To allow any type of socket address to be passed to
248 interfaces in the sockets API,
250 .IR "struct sockaddr"
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.
257 In addition, the sockets API provides the data type
258 .IR "struct sockaddr_storage".
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:
269 sa_family_t ss_family;
275 structure is useful in programs that must handle socket addresses
277 (e.g., programs that must deal with both IPv4 and IPv6 socket addresses).
279 The socket options listed below can be set by using
283 with the socket level set to
286 Unless otherwise noted,
291 .\" In the list below, the text used to describe argument types
292 .\" for each socket option should be more consistent
294 .\" SO_ACCEPTCONN is in POSIX.1-2001, and its origin is explained in
295 .\" W R Stevens, UNPv1
298 Returns a value indicating whether or not this socket has been marked
299 to accept connections with
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.
306 Bind this socket to a particular device like \(lqeth0\(rq,
307 as specified in the passed interface name.
309 name is an empty string or the option length is zero, the socket device
311 The passed option is a variable-length null-terminated
312 interface name string with the maximum size of
314 If a socket is bound to an interface,
315 only packets received from that particular interface are processed by the
317 Note that this works only for some socket types, particularly
320 It is not supported for packet sockets (use normal
325 this socket option could be set, but could not retrieved with
327 Since Linux 3.8, it is readable.
330 argument should contain the buffer size available
331 to receive the device name and is recommended to be
334 The real device name length is reported back in the
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.
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
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.
357 Enable socket debugging.
358 Only allowed for processes with the
360 capability or an effective user ID of 0.
362 .BR SO_DOMAIN " (since Linux 2.6.32)"
363 Retrieves the socket domain as an integer, returning a value such as
368 This socket option is read-only.
371 Get and clear the pending socket error.
372 This socket option is read-only.
376 Don't send via a gateway, send only to directly connected hosts.
377 The same effect can be achieved by setting the
382 Expects an integer boolean flag.
385 Enable sending of keep-alive messages on connection-oriented sockets.
386 Expects an integer boolean flag.
399 int l_onoff; /* linger active */
400 int l_linger; /* how many seconds to linger for */
409 will not return until all queued messages for the socket have been
410 successfully sent or the linger timeout has been reached.
412 the call returns immediately and the closing is done in the background.
413 When the socket is closed as part of
415 it always lingers in the background.
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
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
433 flag is set during receiving.
434 .\" don't document it because it can do too much harm.
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.
445 Enable or disable the receiving of the
448 For more information see
450 .\" FIXME Document SO_PASSSEC, added in 2.6.18; there is some info
451 .\" in the 2.6.18 ChangeLog
453 .BR SO_PEEK_OFF " (since Linux 3.4)"
454 .\" commit ef64a54f6e558155b4f149bb10666b9e914b6c54
455 This option, which is currently supported only for
457 sockets, sets the value of the "peek offset" for the
459 system call when used with
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:
469 flag will peek data from the front of the queue.
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.
478 If data is removed from the front of the queue via a call to
480 (or similar) without the
482 flag, the "peek offset" will be decreased by the number of bytes removed.
483 In other words, receiving data without the
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.
490 For datagram sockets, if the "peek offset" points to the middle of a packet,
491 the data returned will be marked with the
495 The following example serves to illustrate the use of
497 Suppose a stream socket has the following queued input data:
502 The following sequence of
504 calls would have the effect noted in the comments:
508 int ov = 4; // Set peek offset to 4
509 setsockopt(fd, SOL_SOCKET, SO_PEEK_OFF, &ov, sizeof(ov));
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
519 Return the credentials of the foreign process connected to this socket.
520 This is possible only for connected
524 stream and datagram socket pairs created using
528 The returned credentials are those that were in effect at the time
535 structure; define the
537 feature test macro to obtain the definition of that structure from
539 This socket option is read-only.
542 Set the protocol-defined priority for all packets to be sent on
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.
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
554 .BR SO_PROTOCOL " (since Linux 2.6.32)"
555 Retrieves the socket protocol as an integer, returning a value such as
560 This socket option is read-only.
563 Sets or gets the maximum socket receive buffer in bytes.
564 The kernel doubles this value (to allow space for bookkeeping overhead)
566 .\" Most (all?) other implementations do not do this -- MTK, Dec 05
568 and this doubled value is returned by
570 .\" The following thread on LMKL is quite informative:
571 .\" getsockopt/setsockopt with SO_RCVBUF and SO_SNDBUF "non-standard" behavior
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
579 The minimum (doubled) value for this option is 256.
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
588 limit can be overridden.
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
594 or the user on receiving
596 These two values are initialized to 1.
598 is not changeable on Linux
604 only since Linux 2.4.
609 system calls currently do not respect the
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
616 .\" See http://marc.theaimsgroup.com/?l=linux-kernel&m=111049368106984&w=2
617 .\" Tested on kernel 2.6.14 -- mtk, 30 Nov 05
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.
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
635 .\" in fact to EAGAIN
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.,
648 timeouts have no effect for
655 .\" commit c617f398edd4db2b8567a28e899a88f8f574798d
656 .\" https://lwn.net/Articles/542629/
657 Indicates that the rules used in validating addresses supplied in a
659 call should allow reuse of local addresses.
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
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.
671 .BR SO_REUSEPORT " (since Linux 3.9)"
676 sockets to be bound to an identical socket address.
677 This option must be set on each socket (including the first socket)
681 To prevent port hijacking,
682 all of the processes binding to the same address must have the same
684 This option can be employed with both TCP and UDP sockets.
686 For TCP sockets, this option allows
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
693 thread that distributes connections,
694 or having multiple threads that compete to
696 from the same socket.
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.
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.
712 Sets or gets the maximum socket send buffer in bytes.
713 The kernel doubles this value (to allow space for bookkeeping overhead)
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)
718 and this doubled value is returned by
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
725 The minimum (doubled) value for this option is 2048.
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
734 limit can be overridden.
737 Enable or disable the receiving of the
740 The timestamp control message is sent with level
747 reception time of the last packet passed to the user in this call.
750 for details on control messages.
753 Gets the socket type as an integer (e.g.,
755 This socket option is read-only.
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
762 The default for this option is controlled by the
763 .I /proc/sys/net/core/busy_read
767 .I /proc/sys/net/core/busy_poll
768 file determines how long
772 will busy poll when they operate on sockets with
774 set and no events to report are found.
777 busy polling will only be done when the socket last received data
778 from a network device that supports this option.
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.
784 When writing onto a connection-oriented socket that has been shut down
785 (by the local or the remote end)
787 is sent to the writing process and
790 The signal is not sent when the write call
795 When requested with the
802 is sent when an I/O event occurs.
803 It is possible to use
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
811 the handler of the real time signal will be called with
812 the file descriptor in the
818 for more information.
820 Under some circumstances (e.g., multiple processes accessing a
821 single socket), the condition that caused the
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
828 The core socket networking parameters can be accessed
829 via files in the directory
830 .IR /proc/sys/net/core/ .
833 contains the default setting in bytes of the socket receive buffer.
836 contains the maximum socket receive buffer size in bytes which a user may
842 contains the default setting in bytes of the socket send buffer.
845 contains the maximum socket send buffer size in bytes which a user may
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.
854 .I netdev_max_backlog
855 Maximum number of packets in the global input queue.
858 Maximum length of ancillary data and user control data like the iovecs
860 .\" netdev_fastroute is not documented because it is experimental
862 These operations can be accessed using
867 .IB error " = ioctl(" ip_socket ", " ioctl_type ", " &value_result ");"
874 with the receive timestamp of the last packet passed to the user.
875 This is useful for accurate round trip time measurements.
879 .IR "struct timeval" .
881 This ioctl should be used only if the socket option
883 is not set on the socket.
884 Otherwise, it returns the timestamp of the
885 last packet that was received while
887 was not set, or it fails if no such packet has been received,
896 Set the process or process group to send
902 asynchronous I/O operation has finished or urgent data is available.
903 The argument is a pointer to a
905 If the argument is positive, send the signals to that process.
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
912 capability or an effective UID of 0.
917 flag to enable or disable asynchronous I/O mode of the socket.
918 Asynchronous I/O mode means that the
920 signal or the signal set with
922 is raised when a new I/O event occurs.
924 Argument is an integer boolean flag.
925 (This operation is synonymous with the use of
933 Get the current process or process group that receives
956 was introduced in Linux 2.0.30.
961 interfaces were introduced in Linux 2.2.
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.
969 Linux assumes that half of the send/receive buffer is used for internal
970 kernel structures; thus the values in the corresponding
972 files are twice what can be observed on the wire.
974 Linux will allow port reuse only with the
977 when this option was set both in the previous program that performed a
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
984 Typically this difference is invisible, since, for example, a server
985 program is designed to always set this option.
993 .\" FIXME Document SO_ATTACH_FILTER and SO_DETACH_FILTER
995 The suggested interface to use them is via the libpcap
998 .\" This man page was written by Andi Kleen.
1004 .BR capabilities (7),