proc.5: Remove duplicated /proc/[pid]/gid_map entry
[man-pages.git] / man7 / socket.7
blob67736214af9126059afe9d04f1d5a0e7dbf3dc47
1 .\" This man page is Copyright (C) 1999 Andi Kleen <ak@muc.de>.
2 .\" and copyright (c) 1999 Matthew Wilcox.
3 .\"
4 .\" %%%LICENSE_START(VERBATIM_ONE_PARA)
5 .\" Permission is granted to distribute possibly modified copies
6 .\" of this page provided the header is included verbatim,
7 .\" and in case of nontrivial modification author and date
8 .\" of the modification is added to the header.
9 .\" %%%LICENSE_END
10 .\"
11 .\" 2002-10-30, Michael Kerrisk, <mtk.manpages@gmail.com>
12 .\"     Added description of SO_ACCEPTCONN
13 .\" 2004-05-20, aeb, added SO_RCVTIMEO/SO_SNDTIMEO text.
14 .\" Modified, 27 May 2004, Michael Kerrisk <mtk.manpages@gmail.com>
15 .\"     Added notes on capability requirements
16 .\"     A few small grammar fixes
17 .\" 2010-06-13 Jan Engelhardt <jengelh@medozas.de>
18 .\"     Documented SO_DOMAIN and SO_PROTOCOL.
19 .\"
20 .\" FIXME
21 .\" The following are not yet documented:
22 .\"
23 .\" SO_PEERNAME (2.4?)
24 .\"     get only
25 .\"     Seems to do something similar to getpeername(), but then
26 .\"     why is it necessary / how does it differ?
27 .\"
28 .\" SO_TIMESTAMPING (2.6.30)
29 .\"     Documentation/networking/timestamping.txt
30 .\"     commit cb9eff097831007afb30d64373f29d99825d0068
31 .\"     Author: Patrick Ohly <patrick.ohly@intel.com>
32 .\"
33 .\"  SO_WIFI_STATUS (3.3)
34 .\"     commit 6e3e939f3b1bf8534b32ad09ff199d88800835a0
35 .\"     Author: Johannes Berg <johannes.berg@intel.com>
36 .\"     Also: SCM_WIFI_STATUS
37 .\"
38 .\" SO_NOFCS (3.4)
39 .\"     commit 3bdc0eba0b8b47797f4a76e377dd8360f317450f
40 .\"     Author: Ben Greear <greearb@candelatech.com>
41 .\"
42 .\"  SO_GET_FILTER (3.8)
43 .\"     commit a8fc92778080c845eaadc369a0ecf5699a03bef0
44 .\"     Author: Pavel Emelyanov <xemul@parallels.com>
45 .\"
46 .\" SO_MAX_PACING_RATE (3.13)
47 .\"     commit 62748f32d501f5d3712a7c372bbb92abc7c62bc7
48 .\"     Author: Eric Dumazet <edumazet@google.com>
49 .\"
50 .\" SO_BPF_EXTENSIONS (3.14)
51 .\"     commit ea02f9411d9faa3553ed09ce0ec9f00ceae9885e
52 .\"     Author: Michal Sekletar <msekleta@redhat.com>
53 .\"
54 .TH SOCKET 7 2021-03-22 Linux "Linux Programmer's Manual"
55 .SH NAME
56 socket \- Linux socket interface
57 .SH SYNOPSIS
58 .nf
59 .B #include <sys/socket.h>
60 .PP
61 .IB sockfd " = socket(int " socket_family ", int " socket_type ", int " protocol );
62 .fi
63 .SH DESCRIPTION
64 This manual page describes the Linux networking socket layer user
65 interface.
66 The BSD compatible sockets
67 are the uniform interface
68 between the user process and the network protocol stacks in the kernel.
69 The protocol modules are grouped into
70 .I protocol families
71 such as
72 .BR AF_INET ", " AF_IPX ", and " AF_PACKET ,
73 and
74 .I socket types
75 such as
76 .B SOCK_STREAM
78 .BR SOCK_DGRAM .
79 See
80 .BR socket (2)
81 for more information on families and types.
82 .SS Socket-layer functions
83 These functions are used by the user process to send or receive packets
84 and to do other socket operations.
85 For more information see their respective manual pages.
86 .PP
87 .BR socket (2)
88 creates a socket,
89 .BR connect (2)
90 connects a socket to a remote socket address,
91 the
92 .BR bind (2)
93 function binds a socket to a local socket address,
94 .BR listen (2)
95 tells the socket that new connections shall be accepted, and
96 .BR accept (2)
97 is used to get a new socket with a new incoming connection.
98 .BR socketpair (2)
99 returns two connected anonymous sockets (implemented only for a few
100 local families like
101 .BR AF_UNIX )
103 .BR send (2),
104 .BR sendto (2),
106 .BR sendmsg (2)
107 send data over a socket, and
108 .BR recv (2),
109 .BR recvfrom (2),
110 .BR recvmsg (2)
111 receive data from a socket.
112 .BR poll (2)
114 .BR select (2)
115 wait for arriving data or a readiness to send data.
116 In addition, the standard I/O operations like
117 .BR write (2),
118 .BR writev (2),
119 .BR sendfile (2),
120 .BR read (2),
122 .BR readv (2)
123 can be used to read and write data.
125 .BR getsockname (2)
126 returns the local socket address and
127 .BR getpeername (2)
128 returns the remote socket address.
129 .BR getsockopt (2)
131 .BR setsockopt (2)
132 are used to set or get socket layer or protocol options.
133 .BR ioctl (2)
134 can be used to set or read some other options.
136 .BR close (2)
137 is used to close a socket.
138 .BR shutdown (2)
139 closes parts of a full-duplex socket connection.
141 Seeking, or calling
142 .BR pread (2)
144 .BR pwrite (2)
145 with a nonzero position is not supported on sockets.
147 It is possible to do nonblocking I/O on sockets by setting the
148 .B O_NONBLOCK
149 flag on a socket file descriptor using
150 .BR fcntl (2).
151 Then all operations that would block will (usually)
152 return with
153 .B EAGAIN
154 (operation should be retried later);
155 .BR connect (2)
156 will return
157 .B EINPROGRESS
158 error.
159 The user can then wait for various events via
160 .BR poll (2)
162 .BR select (2).
164 tab(:) allbox;
165 c s s
166 l l lx.
167 I/O events
168 Event:Poll flag:Occurrence
169 Read:POLLIN:T{
170 New data arrived.
172 Read:POLLIN:T{
173 A connection setup has been completed
174 (for connection-oriented sockets)
176 Read:POLLHUP:T{
177 A disconnection request has been initiated by the other end.
179 Read:POLLHUP:T{
180 A connection is broken (only for connection-oriented protocols).
181 When the socket is written
182 .B SIGPIPE
183 is also sent.
185 Write:POLLOUT:T{
186 Socket has enough send buffer space for writing new data.
188 Read/Write:T{
189 POLLIN |
191 POLLOUT
192 T}:T{
193 An outgoing
194 .BR connect (2)
195 finished.
197 Read/Write:POLLERR:T{
198 An asynchronous error occurred.
200 Read/Write:POLLHUP:T{
201 The other end has shut down one direction.
203 Exception:POLLPRI:T{
204 Urgent data arrived.
205 .B SIGURG
206 is sent then.
208 .\" FIXME . The following is not true currently:
209 .\" It is no I/O event when the connection
210 .\" is broken from the local end using
211 .\" .BR shutdown (2)
212 .\" or
213 .\" .BR close (2).
216 An alternative to
217 .BR poll (2)
219 .BR select (2)
220 is to let the kernel inform the application about events
221 via a
222 .B SIGIO
223 signal.
224 For that the
225 .B O_ASYNC
226 flag must be set on a socket file descriptor via
227 .BR fcntl (2)
228 and a valid signal handler for
229 .B SIGIO
230 must be installed via
231 .BR sigaction (2).
232 See the
233 .I Signals
234 discussion below.
235 .SS Socket address structures
236 Each socket domain has its own format for socket addresses,
237 with a domain-specific address structure.
238 Each of these structures begins with an
239 integer "family" field (typed as
240 .IR sa_family_t )
241 that indicates the type of the address structure.
242 This allows
243 the various system calls (e.g.,
244 .BR connect (2),
245 .BR bind (2),
246 .BR accept (2),
247 .BR getsockname (2),
248 .BR getpeername (2)),
249 which are generic to all socket domains,
250 to determine the domain of a particular socket address.
252 To allow any type of socket address to be passed to
253 interfaces in the sockets API,
254 the type
255 .IR "struct sockaddr"
256 is defined.
257 The purpose of this type is purely to allow casting of
258 domain-specific socket address types to a "generic" type,
259 so as to avoid compiler warnings about type mismatches in
260 calls to the sockets API.
262 In addition, the sockets API provides the data type
263 .IR "struct sockaddr_storage".
264 This type
265 is suitable to accommodate all supported domain-specific socket
266 address structures; it is large enough and is aligned properly.
267 (In particular, it is large enough to hold
268 IPv6 socket addresses.)
269 The structure includes the following field, which can be used to identify
270 the type of socket address actually stored in the structure:
272 .in +4n
274     sa_family_t ss_family;
279 .I sockaddr_storage
280 structure is useful in programs that must handle socket addresses
281 in a generic way
282 (e.g., programs that must deal with both IPv4 and IPv6 socket addresses).
283 .SS Socket options
284 The socket options listed below can be set by using
285 .BR setsockopt (2)
286 and read with
287 .BR getsockopt (2)
288 with the socket level set to
289 .B SOL_SOCKET
290 for all sockets.
291 Unless otherwise noted,
292 .I optval
293 is a pointer to an
294 .IR int .
295 .\" FIXME .
296 .\" In the list below, the text used to describe argument types
297 .\" for each socket option should be more consistent
299 .\" SO_ACCEPTCONN is in POSIX.1-2001, and its origin is explained in
300 .\" W R Stevens, UNPv1
302 .B SO_ACCEPTCONN
303 Returns a value indicating whether or not this socket has been marked
304 to accept connections with
305 .BR listen (2).
306 The value 0 indicates that this is not a listening socket,
307 the value 1 indicates that this is a listening socket.
308 This socket option is read-only.
310 .BR SO_ATTACH_FILTER " (since Linux 2.2), " SO_ATTACH_BPF " (since Linux 3.19)"
311 Attach a classic BPF
312 .RB ( SO_ATTACH_FILTER )
313 or an extended BPF
314 .RB ( SO_ATTACH_BPF )
315 program to the socket for use as a filter of incoming packets.
316 A packet will be dropped if the filter program returns zero.
317 If the filter program returns a
318 nonzero value which is less than the packet's data length,
319 the packet will be truncated to the length returned.
320 If the value returned by the filter is greater than or equal to the
321 packet's data length, the packet is allowed to proceed unmodified.
323 The argument for
324 .BR SO_ATTACH_FILTER
325 is a
326 .I sock_fprog
327 structure, defined in
328 .IR <linux/filter.h> :
330 .in +4n
332 struct sock_fprog {
333     unsigned short      len;
334     struct sock_filter *filter;
339 The argument for
340 .BR SO_ATTACH_BPF
341 is a file descriptor returned by the
342 .BR bpf (2)
343 system call and must refer to a program of type
344 .BR BPF_PROG_TYPE_SOCKET_FILTER .
346 These options may be set multiple times for a given socket,
347 each time replacing the previous filter program.
348 The classic and extended versions may be called on the same socket,
349 but the previous filter will always be replaced such that a socket
350 never has more than one filter defined.
352 Both classic and extended BPF are explained in the kernel source file
353 .I Documentation/networking/filter.txt
355 .BR SO_ATTACH_REUSEPORT_CBPF ", " SO_ATTACH_REUSEPORT_EBPF
356 For use with the
357 .BR SO_REUSEPORT
358 option, these options allow the user to set a classic BPF
359 .RB ( SO_ATTACH_REUSEPORT_CBPF )
360 or an extended BPF
361 .RB ( SO_ATTACH_REUSEPORT_EBPF )
362 program which defines how packets are assigned to
363 the sockets in the reuseport group (that is, all sockets which have
364 .BR SO_REUSEPORT
365 set and are using the same local address to receive packets).
367 The BPF program must return an index between 0 and N\-1 representing
368 the socket which should receive the packet
369 (where N is the number of sockets in the group).
370 If the BPF program returns an invalid index,
371 socket selection will fall back to the plain
372 .BR SO_REUSEPORT
373 mechanism.
375 Sockets are numbered in the order in which they are added to the group
376 (that is, the order of
377 .BR bind (2)
378 calls for UDP sockets or the order of
379 .BR listen (2)
380 calls for TCP sockets).
381 New sockets added to a reuseport group will inherit the BPF program.
382 When a socket is removed from a reuseport group (via
383 .BR close (2)),
384 the last socket in the group will be moved into the closed socket's
385 position.
387 These options may be set repeatedly at any time on any socket in the group
388 to replace the current BPF program used by all sockets in the group.
390 .BR SO_ATTACH_REUSEPORT_CBPF
391 takes the same argument type as
392 .BR SO_ATTACH_FILTER
394 .BR SO_ATTACH_REUSEPORT_EBPF
395 takes the same argument type as
396 .BR SO_ATTACH_BPF .
398 UDP support for this feature is available since Linux 4.5;
399 TCP support is available since Linux 4.6.
401 .B SO_BINDTODEVICE
402 Bind this socket to a particular device like \(lqeth0\(rq,
403 as specified in the passed interface name.
404 If the
405 name is an empty string or the option length is zero, the socket device
406 binding is removed.
407 The passed option is a variable-length null-terminated
408 interface name string with the maximum size of
409 .BR IFNAMSIZ .
410 If a socket is bound to an interface,
411 only packets received from that particular interface are processed by the
412 socket.
413 Note that this works only for some socket types, particularly
414 .B AF_INET
415 sockets.
416 It is not supported for packet sockets (use normal
417 .BR bind (2)
418 there).
420 Before Linux 3.8,
421 this socket option could be set, but could not retrieved with
422 .BR getsockopt (2).
423 Since Linux 3.8, it is readable.
425 .I optlen
426 argument should contain the buffer size available
427 to receive the device name and is recommended to be
428 .BR IFNAMSIZ
429 bytes.
430 The real device name length is reported back in the
431 .I optlen
432 argument.
434 .B SO_BROADCAST
435 Set or get the broadcast flag.
436 When enabled, datagram sockets are allowed to send
437 packets to a broadcast address.
438 This option has no effect on stream-oriented sockets.
440 .B SO_BSDCOMPAT
441 Enable BSD bug-to-bug compatibility.
442 This is used by the UDP protocol module in Linux 2.0 and 2.2.
443 If enabled, ICMP errors received for a UDP socket will not be passed
444 to the user program.
445 In later kernel versions, support for this option has been phased out:
446 Linux 2.4 silently ignores it, and Linux 2.6 generates a kernel warning
447 (printk()) if a program uses this option.
448 Linux 2.0 also enabled BSD bug-to-bug compatibility
449 options (random header changing, skipping of the broadcast flag) for raw
450 sockets with this option, but that was removed in Linux 2.2.
452 .B SO_DEBUG
453 Enable socket debugging.
454 Allowed only for processes with the
455 .B CAP_NET_ADMIN
456 capability or an effective user ID of 0.
458 .BR SO_DETACH_FILTER " (since Linux 2.2), " SO_DETACH_BPF " (since Linux 3.19)"
459 These two options, which are synonyms,
460 may be used to remove the classic or extended BPF
461 program attached to a socket with either
462 .BR SO_ATTACH_FILTER
464 .BR SO_ATTACH_BPF .
465 The option value is ignored.
467 .BR SO_DOMAIN " (since Linux 2.6.32)"
468 Retrieves the socket domain as an integer, returning a value such as
469 .BR AF_INET6 .
471 .BR socket (2)
472 for details.
473 This socket option is read-only.
475 .B SO_ERROR
476 Get and clear the pending socket error.
477 This socket option is read-only.
478 Expects an integer.
480 .B SO_DONTROUTE
481 Don't send via a gateway, send only to directly connected hosts.
482 The same effect can be achieved by setting the
483 .B MSG_DONTROUTE
484 flag on a socket
485 .BR send (2)
486 operation.
487 Expects an integer boolean flag.
489 .BR SO_INCOMING_CPU " (gettable since Linux 3.19, settable since Linux 4.4)"
490 .\" getsockopt 2c8c56e15df3d4c2af3d656e44feb18789f75837
491 .\" setsockopt 70da268b569d32a9fddeea85dc18043de9d89f89
492 Sets or gets the CPU affinity of a socket.
493 Expects an integer flag.
495 .in +4n
497 int cpu = 1;
498 setsockopt(fd, SOL_SOCKET, SO_INCOMING_CPU, &cpu,
499            sizeof(cpu));
503 Because all of the packets for a single stream
504 (i.e., all packets for the same 4-tuple)
505 arrive on the single RX queue that is associated with a particular CPU,
506 the typical use case is to employ one listening process per RX queue,
507 with the incoming flow being handled by a listener
508 on the same CPU that is handling the RX queue.
509 This provides optimal NUMA behavior and keeps CPU caches hot.
511 .\" From an email conversation with Eric Dumazet:
512 .\" >> Note that setting the option is not supported if SO_REUSEPORT is used.
513 .\" >
514 .\" > Please define "not supported". Does this yield an API diagnostic?
515 .\" > If so, what is it?
516 .\" >
517 .\" >> Socket will be selected from an array, either by a hash or BPF program
518 .\" >> that has no access to this information.
519 .\" >
520 .\" > Sorry -- I'm lost here. How does this comment relate to the proposed
521 .\" > man page text above?
523 .\" Simply that :
525 .\" If an application uses both SO_INCOMING_CPU and SO_REUSEPORT, then
526 .\" SO_REUSEPORT logic, selecting the socket to receive the packet, ignores
527 .\" SO_INCOMING_CPU setting.
529 .BR SO_INCOMING_NAPI_ID " (gettable since Linux 4.12)"
530 .\" getsockopt 6d4339028b350efbf87c61e6d9e113e5373545c9
531 Returns a system-level unique ID called NAPI ID that is associated
532 with a RX queue on which the last packet associated with that
533 socket is received.
535 This can be used by an application to split the incoming flows among worker
536 threads based on the RX queue on which the packets associated with the
537 flows are received.
538 It allows each worker thread to be associated with
539 a NIC HW receive queue and service all the connection
540 requests received on that RX queue.
541 This mapping between a app thread and
542 a HW NIC queue streamlines the
543 flow of data from the NIC to the application.
545 .B SO_KEEPALIVE
546 Enable sending of keep-alive messages on connection-oriented sockets.
547 Expects an integer boolean flag.
549 .B SO_LINGER
550 Sets or gets the
551 .B SO_LINGER
552 option.
553 The argument is a
554 .I linger
555 structure.
557 .in +4n
559 struct linger {
560     int l_onoff;    /* linger active */
561     int l_linger;   /* how many seconds to linger for */
566 When enabled, a
567 .BR close (2)
569 .BR shutdown (2)
570 will not return until all queued messages for the socket have been
571 successfully sent or the linger timeout has been reached.
572 Otherwise,
573 the call returns immediately and the closing is done in the background.
574 When the socket is closed as part of
575 .BR exit (2),
576 it always lingers in the background.
578 .B SO_LOCK_FILTER
579 .\" commit d59577b6ffd313d0ab3be39cb1ab47e29bdc9182
580 When set, this option will prevent
581 changing the filters associated with the socket.
582 These filters include any set using the socket options
583 .BR SO_ATTACH_FILTER ,
584 .BR SO_ATTACH_BPF ,
585 .BR SO_ATTACH_REUSEPORT_CBPF ,
587 .BR SO_ATTACH_REUSEPORT_EBPF .
589 The typical use case is for a privileged process to set up a raw socket
590 (an operation that requires the
591 .BR CAP_NET_RAW
592 capability), apply a restrictive filter, set the
593 .BR SO_LOCK_FILTER
594 option,
595 and then either drop its privileges or pass the socket file descriptor
596 to an unprivileged process via a UNIX domain socket.
598 Once the
599 .BR SO_LOCK_FILTER
600 option has been enabled, attempts to change or remove the filter
601 attached to a socket, or to disable the
602 .BR SO_LOCK_FILTER
603 option will fail with the error
604 .BR EPERM .
606 .BR SO_MARK " (since Linux 2.6.25)"
607 .\" commit 4a19ec5800fc3bb64e2d87c4d9fdd9e636086fe0
608 .\" and    914a9ab386a288d0f22252fc268ecbc048cdcbd5
609 Set the mark for each packet sent through this socket
610 (similar to the netfilter MARK target but socket-based).
611 Changing the mark can be used for mark-based
612 routing without netfilter or for packet filtering.
613 Setting this option requires the
614 .B CAP_NET_ADMIN
615 capability.
617 .B SO_OOBINLINE
618 If this option is enabled,
619 out-of-band data is directly placed into the receive data stream.
620 Otherwise, out-of-band data is passed only when the
621 .B MSG_OOB
622 flag is set during receiving.
623 .\" don't document it because it can do too much harm.
624 .\".B SO_NO_CHECK
625 .\"     The kernel has support for the SO_NO_CHECK socket
626 .\"     option (boolean: 0 == default, calculate checksum on xmit,
627 .\"     1 == do not calculate checksum on xmit).
628 .\" Additional note from Andi Kleen on SO_NO_CHECK (2010-08-30)
629 .\"     On Linux UDP checksums are essentially free and there's no reason
630 .\"     to turn them off and it would disable another safety line.
631 .\"     That is why I didn't document the option.
633 .B SO_PASSCRED
634 Enable or disable the receiving of the
635 .B SCM_CREDENTIALS
636 control message.
637 For more information see
638 .BR unix (7).
640 .B SO_PASSSEC
641 Enable or disable the receiving of the
642 .B SCM_SECURITY
643 control message.
644 For more information see
645 .BR unix (7).
647 .BR SO_PEEK_OFF " (since Linux 3.4)"
648 .\" commit ef64a54f6e558155b4f149bb10666b9e914b6c54
649 This option, which is currently supported only for
650 .BR unix (7)
651 sockets, sets the value of the "peek offset" for the
652 .BR recv (2)
653 system call when used with
654 .BR MSG_PEEK
655 flag.
657 When this option is set to a negative value
658 (it is set to \-1 for all new sockets),
659 traditional behavior is provided:
660 .BR recv (2)
661 with the
662 .BR MSG_PEEK
663 flag will peek data from the front of the queue.
665 When the option is set to a value greater than or equal to zero,
666 then the next peek at data queued in the socket will occur at
667 the byte offset specified by the option value.
668 At the same time, the "peek offset" will be
669 incremented by the number of bytes that were peeked from the queue,
670 so that a subsequent peek will return the next data in the queue.
672 If data is removed from the front of the queue via a call to
673 .BR recv (2)
674 (or similar) without the
675 .BR MSG_PEEK
676 flag, the "peek offset" will be decreased by the number of bytes removed.
677 In other words, receiving data without the
678 .B MSG_PEEK
679 flag will cause the "peek offset" to be adjusted to maintain
680 the correct relative position in the queued data,
681 so that a subsequent peek will retrieve the data that would have been
682 retrieved had the data not been removed.
684 For datagram sockets, if the "peek offset" points to the middle of a packet,
685 the data returned will be marked with the
686 .BR MSG_TRUNC
687 flag.
689 The following example serves to illustrate the use of
690 .BR SO_PEEK_OFF .
691 Suppose a stream socket has the following queued input data:
693     aabbccddeeff
695 The following sequence of
696 .BR recv (2)
697 calls would have the effect noted in the comments:
699 .in +4n
701 int ov = 4;                  // Set peek offset to 4
702 setsockopt(fd, SOL_SOCKET, SO_PEEK_OFF, &ov, sizeof(ov));
704 recv(fd, buf, 2, MSG_PEEK);  // Peeks "cc"; offset set to 6
705 recv(fd, buf, 2, MSG_PEEK);  // Peeks "dd"; offset set to 8
706 recv(fd, buf, 2, 0);         // Reads "aa"; offset set to 6
707 recv(fd, buf, 2, MSG_PEEK);  // Peeks "ee"; offset set to 8
711 .B SO_PEERCRED
712 Return the credentials of the peer process connected to this socket.
713 For further details, see
714 .BR unix (7).
716 .BR SO_PEERSEC " (since Linux 2.6.2)"
717 Return the security context of the peer socket connected to this socket.
718 For further details, see
719 .BR unix (7)
721 .BR ip (7).
723 .B SO_PRIORITY
724 Set the protocol-defined priority for all packets to be sent on
725 this socket.
726 Linux uses this value to order the networking queues:
727 packets with a higher priority may be processed first depending
728 on the selected device queueing discipline.
729 .\" For
730 .\" .BR ip (7),
731 .\" this also sets the IP type-of-service (TOS) field for outgoing packets.
732 Setting a priority outside the range 0 to 6 requires the
733 .B CAP_NET_ADMIN
734 capability.
736 .BR SO_PROTOCOL " (since Linux 2.6.32)"
737 Retrieves the socket protocol as an integer, returning a value such as
738 .BR IPPROTO_SCTP .
740 .BR socket (2)
741 for details.
742 This socket option is read-only.
744 .B SO_RCVBUF
745 Sets or gets the maximum socket receive buffer in bytes.
746 The kernel doubles this value (to allow space for bookkeeping overhead)
747 when it is set using
748 .\" Most (all?) other implementations do not do this -- MTK, Dec 05
749 .BR setsockopt (2),
750 and this doubled value is returned by
751 .BR getsockopt (2).
752 .\" The following thread on LMKL is quite informative:
753 .\" getsockopt/setsockopt with SO_RCVBUF and SO_SNDBUF "non-standard" behavior
754 .\" 17 July 2012
755 .\" http://thread.gmane.org/gmane.linux.kernel/1328935
756 The default value is set by the
757 .I /proc/sys/net/core/rmem_default
758 file, and the maximum allowed value is set by the
759 .I /proc/sys/net/core/rmem_max
760 file.
761 The minimum (doubled) value for this option is 256.
763 .BR SO_RCVBUFFORCE " (since Linux 2.6.14)"
764 Using this socket option, a privileged
765 .RB ( CAP_NET_ADMIN )
766 process can perform the same task as
767 .BR SO_RCVBUF ,
768 but the
769 .I rmem_max
770 limit can be overridden.
772 .BR SO_RCVLOWAT " and " SO_SNDLOWAT
773 Specify the minimum number of bytes in the buffer until the socket layer
774 will pass the data to the protocol
775 .RB ( SO_SNDLOWAT )
776 or the user on receiving
777 .RB ( SO_RCVLOWAT ).
778 These two values are initialized to 1.
779 .B SO_SNDLOWAT
780 is not changeable on Linux
781 .RB ( setsockopt (2)
782 fails with the error
783 .BR ENOPROTOOPT ).
784 .B SO_RCVLOWAT
785 is changeable
786 only since Linux 2.4.
788 Before Linux 2.6.28
789 .\" Tested on kernel 2.6.14 -- mtk, 30 Nov 05
790 .BR select (2),
791 .BR poll (2),
793 .BR epoll (7)
794 did not respect the
795 .B SO_RCVLOWAT
796 setting on Linux,
797 and indicated a socket as readable when even a single byte of data
798 was available.
799 A subsequent read from the socket would then block until
800 .B SO_RCVLOWAT
801 bytes are available.
802 Since Linux 2.6.28,
803 .\" commit c7004482e8dcb7c3c72666395cfa98a216a4fb70
804 .BR select (2),
805 .BR poll (2),
807 .BR epoll (7)
808 indicate a socket as readable only if at least
809 .B SO_RCVLOWAT
810 bytes are available.
812 .BR SO_RCVTIMEO " and " SO_SNDTIMEO
813 .\" Not implemented in 2.0.
814 .\" Implemented in 2.1.11 for getsockopt: always return a zero struct.
815 .\" Implemented in 2.3.41 for setsockopt, and actually used.
816 Specify the receiving or sending timeouts until reporting an error.
817 The argument is a
818 .IR "struct timeval" .
819 If an input or output function blocks for this period of time, and
820 data has been sent or received, the return value of that function
821 will be the amount of data transferred; if no data has been transferred
822 and the timeout has been reached, then \-1 is returned with
823 .I errno
824 set to
825 .BR EAGAIN
827 .BR EWOULDBLOCK ,
828 .\" in fact to EAGAIN
830 .B EINPROGRESS
831 (for
832 .BR connect (2))
833 just as if the socket was specified to be nonblocking.
834 If the timeout is set to zero (the default),
835 then the operation will never timeout.
836 Timeouts only have effect for system calls that perform socket I/O (e.g.,
837 .BR read (2),
838 .BR recvmsg (2),
839 .BR send (2),
840 .BR sendmsg (2));
841 timeouts have no effect for
842 .BR select (2),
843 .BR poll (2),
844 .BR epoll_wait (2),
845 and so on.
847 .B SO_REUSEADDR
848 .\"     commit c617f398edd4db2b8567a28e899a88f8f574798d
849 .\"     https://lwn.net/Articles/542629/
850 Indicates that the rules used in validating addresses supplied in a
851 .BR bind (2)
852 call should allow reuse of local addresses.
854 .B AF_INET
855 sockets this
856 means that a socket may bind, except when there
857 is an active listening socket bound to the address.
858 When the listening socket is bound to
859 .B INADDR_ANY
860 with a specific port then it is not possible
861 to bind to this port for any local address.
862 Argument is an integer boolean flag.
864 .BR SO_REUSEPORT " (since Linux 3.9)"
865 Permits multiple
866 .B AF_INET
868 .B AF_INET6
869 sockets to be bound to an identical socket address.
870 This option must be set on each socket (including the first socket)
871 prior to calling
872 .BR bind (2)
873 on the socket.
874 To prevent port hijacking,
875 all of the processes binding to the same address must have the same
876 effective UID.
877 This option can be employed with both TCP and UDP sockets.
879 For TCP sockets, this option allows
880 .BR accept (2)
881 load distribution in a multi-threaded server to be improved by
882 using a distinct listener socket for each thread.
883 This provides improved load distribution as compared
884 to traditional techniques such using a single
885 .BR accept (2)ing
886 thread that distributes connections,
887 or having multiple threads that compete to
888 .BR accept (2)
889 from the same socket.
891 For UDP sockets,
892 the use of this option can provide better distribution
893 of incoming datagrams to multiple processes (or threads) as compared
894 to the traditional technique of having multiple processes
895 compete to receive datagrams on the same socket.
897 .BR SO_RXQ_OVFL " (since Linux 2.6.33)"
898 .\" commit 3b885787ea4112eaa80945999ea0901bf742707f
899 Indicates that an unsigned 32-bit value ancillary message (cmsg)
900 should be attached to received skbs indicating
901 the number of packets dropped by the socket since its creation.
903 .BR SO_SELECT_ERR_QUEUE " (since Linux 3.10)"
904 .\"     commit 7d4c04fc170087119727119074e72445f2bb192b
905 .\"     Author: Keller, Jacob E <jacob.e.keller@intel.com>
906 When this option is set on a socket,
907 an error condition on a socket causes notification not only via the
908 .I exceptfds
909 set of
910 .BR select (2).
911 Similarly,
912 .BR poll (2)
913 also returns a
914 .B POLLPRI
915 whenever an
916 .B POLLERR
917 event is returned.
918 .\" It does not affect wake up.
920 Background: this option was added when waking up on an error condition
921 occurred only via the
922 .IR readfds
924 .IR writefds
925 sets of
926 .BR select (2).
927 The option was added to allow monitoring for error conditions via the
928 .I exceptfds
929 argument without simultaneously having to receive notifications (via
930 .IR readfds )
931 for regular data that can be read from the socket.
932 After changes in Linux 4.16,
933 .\" commit 6e5d58fdc9bedd0255a8
934 .\" ("skbuff: Fix not waking applications when errors are enqueued")
935 the use of this flag to achieve the desired notifications
936 is no longer necessary.
937 This option is nevertheless retained for backwards compatibility.
939 .B SO_SNDBUF
940 Sets or gets the maximum socket send buffer in bytes.
941 The kernel doubles this value (to allow space for bookkeeping overhead)
942 when it is set using
943 .\" Most (all?) other implementations do not do this -- MTK, Dec 05
944 .\" See also the comment to SO_RCVBUF (17 Jul 2012 LKML mail)
945 .BR setsockopt (2),
946 and this doubled value is returned by
947 .BR getsockopt (2).
948 The default value is set by the
949 .I /proc/sys/net/core/wmem_default
950 file and the maximum allowed value is set by the
951 .I /proc/sys/net/core/wmem_max
952 file.
953 The minimum (doubled) value for this option is 2048.
955 .BR SO_SNDBUFFORCE " (since Linux 2.6.14)"
956 Using this socket option, a privileged
957 .RB ( CAP_NET_ADMIN )
958 process can perform the same task as
959 .BR SO_SNDBUF ,
960 but the
961 .I wmem_max
962 limit can be overridden.
964 .B SO_TIMESTAMP
965 Enable or disable the receiving of the
966 .B SO_TIMESTAMP
967 control message.
968 The timestamp control message is sent with level
969 .B SOL_SOCKET
970 and a
971 .I cmsg_type
973 .BR SCM_TIMESTAMP .
975 .I cmsg_data
976 field is a
977 .I "struct timeval"
978 indicating the
979 reception time of the last packet passed to the user in this call.
981 .BR cmsg (3)
982 for details on control messages.
984 .BR SO_TIMESTAMPNS " (since Linux 2.6.22)"
985 .\" commit 92f37fd2ee805aa77925c1e64fd56088b46094fc
986 Enable or disable the receiving of the
987 .B SO_TIMESTAMPNS
988 control message.
989 The timestamp control message is sent with level
990 .B SOL_SOCKET
991 and a
992 .I cmsg_type
994 .BR SCM_TIMESTAMPNS .
996 .I cmsg_data
997 field is a
998 .I "struct timespec"
999 indicating the
1000 reception time of the last packet passed to the user in this call.
1001 The clock used for the timestamp is
1002 .BR CLOCK_REALTIME .
1004 .BR cmsg (3)
1005 for details on control messages.
1007 A socket cannot mix
1008 .B SO_TIMESTAMP
1010 .BR SO_TIMESTAMPNS :
1011 the two modes are mutually exclusive.
1013 .B SO_TYPE
1014 Gets the socket type as an integer (e.g.,
1015 .BR SOCK_STREAM ).
1016 This socket option is read-only.
1018 .BR SO_BUSY_POLL " (since Linux 3.11)"
1019 Sets the approximate time in microseconds to busy poll on a blocking receive
1020 when there is no data.
1021 Increasing this value requires
1022 .BR CAP_NET_ADMIN .
1023 The default for this option is controlled by the
1024 .I /proc/sys/net/core/busy_read
1025 file.
1027 The value in the
1028 .I /proc/sys/net/core/busy_poll
1029 file determines how long
1030 .BR select (2)
1032 .BR poll (2)
1033 will busy poll when they operate on sockets with
1034 .BR SO_BUSY_POLL
1035 set and no events to report are found.
1037 In both cases,
1038 busy polling will only be done when the socket last received data
1039 from a network device that supports this option.
1041 While busy polling may improve latency of some applications,
1042 care must be taken when using it since this will increase
1043 both CPU utilization and power usage.
1044 .SS Signals
1045 When writing onto a connection-oriented socket that has been shut down
1046 (by the local or the remote end)
1047 .B SIGPIPE
1048 is sent to the writing process and
1049 .B EPIPE
1050 is returned.
1051 The signal is not sent when the write call
1052 specified the
1053 .B MSG_NOSIGNAL
1054 flag.
1056 When requested with the
1057 .B FIOSETOWN
1058 .BR fcntl (2)
1060 .B SIOCSPGRP
1061 .BR ioctl (2),
1062 .B SIGIO
1063 is sent when an I/O event occurs.
1064 It is possible to use
1065 .BR poll (2)
1067 .BR select (2)
1068 in the signal handler to find out which socket the event occurred on.
1069 An alternative (in Linux 2.2) is to set a real-time signal using the
1070 .B F_SETSIG
1071 .BR fcntl (2);
1072 the handler of the real time signal will be called with
1073 the file descriptor in the
1074 .I si_fd
1075 field of its
1076 .IR siginfo_t .
1078 .BR fcntl (2)
1079 for more information.
1081 Under some circumstances (e.g., multiple processes accessing a
1082 single socket), the condition that caused the
1083 .B SIGIO
1084 may have already disappeared when the process reacts to the signal.
1085 If this happens, the process should wait again because Linux
1086 will resend the signal later.
1087 .\" .SS Ancillary messages
1088 .SS /proc interfaces
1089 The core socket networking parameters can be accessed
1090 via files in the directory
1091 .IR /proc/sys/net/core/ .
1093 .I rmem_default
1094 contains the default setting in bytes of the socket receive buffer.
1096 .I rmem_max
1097 contains the maximum socket receive buffer size in bytes which a user may
1098 set by using the
1099 .B SO_RCVBUF
1100 socket option.
1102 .I wmem_default
1103 contains the default setting in bytes of the socket send buffer.
1105 .I wmem_max
1106 contains the maximum socket send buffer size in bytes which a user may
1107 set by using the
1108 .B SO_SNDBUF
1109 socket option.
1111 .IR message_cost " and " message_burst
1112 configure the token bucket filter used to load limit warning messages
1113 caused by external network events.
1115 .I netdev_max_backlog
1116 Maximum number of packets in the global input queue.
1118 .I optmem_max
1119 Maximum length of ancillary data and user control data like the iovecs
1120 per socket.
1121 .\" netdev_fastroute is not documented because it is experimental
1122 .SS Ioctls
1123 These operations can be accessed using
1124 .BR ioctl (2):
1126 .in +4n
1128 .IB error " = ioctl(" ip_socket ", " ioctl_type ", " &value_result ");"
1132 .B SIOCGSTAMP
1133 Return a
1134 .I struct timeval
1135 with the receive timestamp of the last packet passed to the user.
1136 This is useful for accurate round trip time measurements.
1138 .BR setitimer (2)
1139 for a description of
1140 .IR "struct timeval" .
1142 This ioctl should be used only if the socket options
1143 .B SO_TIMESTAMP
1145 .B SO_TIMESTAMPNS
1146 are not set on the socket.
1147 Otherwise, it returns the timestamp of the
1148 last packet that was received while
1149 .B SO_TIMESTAMP
1151 .B SO_TIMESTAMPNS
1152 were not set, or it fails if no such packet has been received,
1153 (i.e.,
1154 .BR ioctl (2)
1155 returns \-1 with
1156 .I errno
1157 set to
1158 .BR ENOENT ).
1160 .B SIOCSPGRP
1161 Set the process or process group that is to receive
1162 .B SIGIO
1164 .B SIGURG
1165 signals when I/O becomes possible or urgent data is available.
1166 The argument is a pointer to a
1167 .IR pid_t .
1168 For further details, see the description of
1169 .BR F_SETOWN
1171 .BR fcntl (2).
1173 .B FIOASYNC
1174 Change the
1175 .B O_ASYNC
1176 flag to enable or disable asynchronous I/O mode of the socket.
1177 Asynchronous I/O mode means that the
1178 .B SIGIO
1179 signal or the signal set with
1180 .B F_SETSIG
1181 is raised when a new I/O event occurs.
1183 Argument is an integer boolean flag.
1184 (This operation is synonymous with the use of
1185 .BR fcntl (2)
1186 to set the
1187 .B O_ASYNC
1188 flag.)
1191 .B SIOCGPGRP
1192 Get the current process or process group that receives
1193 .B SIGIO
1195 .B SIGURG
1196 signals,
1197 or 0
1198 when none is set.
1200 Valid
1201 .BR fcntl (2)
1202 operations:
1204 .B FIOGETOWN
1205 The same as the
1206 .B SIOCGPGRP
1207 .BR ioctl (2).
1209 .B FIOSETOWN
1210 The same as the
1211 .B SIOCSPGRP
1212 .BR ioctl (2).
1213 .SH VERSIONS
1214 .B SO_BINDTODEVICE
1215 was introduced in Linux 2.0.30.
1216 .B SO_PASSCRED
1217 is new in Linux 2.2.
1219 .I /proc
1220 interfaces were introduced in Linux 2.2.
1221 .B SO_RCVTIMEO
1223 .B SO_SNDTIMEO
1224 are supported since Linux 2.3.41.
1225 Earlier, timeouts were fixed to
1226 a protocol-specific setting, and could not be read or written.
1227 .SH NOTES
1228 Linux assumes that half of the send/receive buffer is used for internal
1229 kernel structures; thus the values in the corresponding
1230 .I /proc
1231 files are twice what can be observed on the wire.
1233 Linux will allow port reuse only with the
1234 .B SO_REUSEADDR
1235 option
1236 when this option was set both in the previous program that performed a
1237 .BR bind (2)
1238 to the port and in the program that wants to reuse the port.
1239 This differs from some implementations (e.g., FreeBSD)
1240 where only the later program needs to set the
1241 .B SO_REUSEADDR
1242 option.
1243 Typically this difference is invisible, since, for example, a server
1244 program is designed to always set this option.
1245 .\" .SH AUTHORS
1246 .\" This man page was written by Andi Kleen.
1247 .SH SEE ALSO
1248 .BR wireshark (1),
1249 .BR bpf (2),
1250 .BR connect (2),
1251 .BR getsockopt (2),
1252 .BR setsockopt (2),
1253 .BR socket (2),
1254 .BR pcap (3),
1255 .BR address_families (7),
1256 .BR capabilities (7),
1257 .BR ddp (7),
1258 .BR ip (7),
1259 .BR ipv6 (7),
1260 .BR packet (7),
1261 .BR tcp (7),
1262 .BR udp (7),
1263 .BR unix (7),
1264 .BR tcpdump (8)