]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man7/socket.7
socket.7: SEE ALSO: add pcap(3)
[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_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>
53 .\"
54 .TH SOCKET 7 2015-05-07 Linux "Linux Programmer's Manual"
55 .SH NAME
56 socket \- Linux socket interface
57 .SH SYNOPSIS
58 .B #include <sys/socket.h>
59 .sp
60 .IB sockfd " = socket(int " socket_family ", int " socket_type ", int " protocol );
61 .SH DESCRIPTION
62 This manual page describes the Linux networking socket layer user
63 interface.
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
68 .I protocol families
69 such as
70 .BR AF_INET ", " AF_IPX ", and " AF_PACKET ,
71 and
72 .I socket types
73 such as
74 .B SOCK_STREAM
75 or
76 .BR SOCK_DGRAM .
77 See
78 .BR socket (2)
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.
84
85 .BR socket (2)
86 creates a socket,
87 .BR connect (2)
88 connects a socket to a remote socket address,
89 the
90 .BR bind (2)
91 function binds a socket to a local socket address,
92 .BR listen (2)
93 tells the socket that new connections shall be accepted, and
94 .BR accept (2)
95 is used to get a new socket with a new incoming connection.
96 .BR socketpair (2)
97 returns two connected anonymous sockets (implemented only for a few
98 local families like
99 .BR AF_UNIX )
100 .PP
101 .BR send (2),
102 .BR sendto (2),
103 and
104 .BR sendmsg (2)
105 send data over a socket, and
106 .BR recv (2),
107 .BR recvfrom (2),
108 .BR recvmsg (2)
109 receive data from a socket.
110 .BR poll (2)
111 and
112 .BR select (2)
113 wait for arriving data or a readiness to send data.
114 In addition, the standard I/O operations like
115 .BR write (2),
116 .BR writev (2),
117 .BR sendfile (2),
118 .BR read (2),
119 and
120 .BR readv (2)
121 can be used to read and write data.
122 .PP
123 .BR getsockname (2)
124 returns the local socket address and
125 .BR getpeername (2)
126 returns the remote socket address.
127 .BR getsockopt (2)
128 and
129 .BR setsockopt (2)
130 are used to set or get socket layer or protocol options.
131 .BR ioctl (2)
132 can be used to set or read some other options.
133 .PP
134 .BR close (2)
135 is used to close a socket.
136 .BR shutdown (2)
137 closes parts of a full-duplex socket connection.
138 .PP
139 Seeking, or calling
140 .BR pread (2)
141 or
142 .BR pwrite (2)
143 with a nonzero position is not supported on sockets.
144 .PP
145 It is possible to do nonblocking I/O on sockets by setting the
146 .B O_NONBLOCK
147 flag on a socket file descriptor using
148 .BR fcntl (2).
149 Then all operations that would block will (usually)
150 return with
151 .B EAGAIN
152 (operation should be retried later);
153 .BR connect (2)
154 will return
155 .B EINPROGRESS
156 error.
157 The user can then wait for various events via
158 .BR poll (2)
159 or
160 .BR select (2).
161 .TS
162 tab(:) allbox;
163 c s s
164 l l l.
165 I/O events
166 Event:Poll flag:Occurrence
167 Read:POLLIN:T{
168 New data arrived.
169 T}
170 Read:POLLIN:T{
171 A connection setup has been completed
172 (for connection-oriented sockets)
173 T}
174 Read:POLLHUP:T{
175 A disconnection request has been initiated by the other end.
176 T}
177 Read:POLLHUP:T{
178 A connection is broken (only for connection-oriented protocols).
179 When the socket is written
180 .B SIGPIPE
181 is also sent.
182 T}
183 Write:POLLOUT:T{
184 Socket has enough send buffer space for writing new data.
185 T}
186 Read/Write:T{
187 POLLIN|
188 .br
189 POLLOUT
190 T}:T{
191 An outgoing
192 .BR connect (2)
193 finished.
194 T}
195 Read/Write:POLLERR:An asynchronous error occurred.
196 Read/Write:POLLHUP:The other end has shut down one direction.
197 Exception:POLLPRI:T{
198 Urgent data arrived.
199 .B SIGURG
200 is sent then.
201 T}
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
205 .\" .BR shutdown (2)
206 .\" or
207 .\" .BR close (2).
208 .TE
209 .PP
210 An alternative to
211 .BR poll (2)
212 and
213 .BR select (2)
214 is to let the kernel inform the application about events
215 via a
216 .B SIGIO
217 signal.
218 For that the
219 .B O_ASYNC
220 flag must be set on a socket file descriptor via
221 .BR fcntl (2)
222 and a valid signal handler for
223 .B SIGIO
224 must be installed via
225 .BR sigaction (2).
226 See the
227 .I Signals
228 discussion below.
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
234 .IR sa_family_t )
235 that indicates the type of the address structure.
236 This allows
237 the various system calls (e.g.,
238 .BR connect (2),
239 .BR bind (2),
240 .BR accept (2),
241 .BR getsockname (2),
242 .BR getpeername (2)),
243 which are generic to all socket domains,
244 to determine the domain of a particular socket address.
245
246 To allow any type of socket address to be passed to
247 interfaces in the sockets API,
248 the type
249 .IR "struct sockaddr"
250 is defined.
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.
255
256 In addition, the sockets API provides the data type
257 .IR "struct sockaddr_storage".
258 This type
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:
265
266 .in +4n
267 .nf
268 sa_family_t ss_family;
269 .fi
270 .in
271
272 The
273 .I sockaddr_storage
274 structure is useful in programs that must handle socket addresses
275 in a generic way
276 (e.g., programs that must deal with both IPv4 and IPv6 socket addresses).
277 .SS Socket options
278 The socket options listed below can be set by using
279 .BR setsockopt (2)
280 and read with
281 .BR getsockopt (2)
282 with the socket level set to
283 .B SOL_SOCKET
284 for all sockets.
285 Unless otherwise noted,
286 .I optval
287 is a pointer to an
288 .IR int .
289 .\" FIXME .
290 .\" In the list below, the text used to describe argument types
291 .\" for each socket option should be more consistent
292 .\"
293 .\" SO_ACCEPTCONN is in POSIX.1-2001, and its origin is explained in
294 .\" W R Stevens, UNPv1
295 .TP
296 .B SO_ACCEPTCONN
297 Returns a value indicating whether or not this socket has been marked
298 to accept connections with
299 .BR listen (2).
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.
303 .TP
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.
312
313 The argument for
314 .BR SO_ATTACH_FILTER
315 is a
316 .I sock_fprog
317 structure in
318 .B <linux/filter.h>.
319 .sp
320 .in +4n
321 .nf
322 struct sock_fprog {
323 unsigned short len;
324 struct sock_filter *filter;
325 };
326 .fi
327 .in
328 .IP
329 The argument for
330 .BR SO_ATTACH_BPF
331 is a file descriptor returned by the
332 .BR bpf (2)
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
339 filter defined.
340
341 .BR SO_ATTACH_FILTER
342 is available since Linux 2.2.
343 .BR SO_ATTACH_BPF
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
347 .TP
348 .BR SO_ATTACH_REUSEPORT_CBPF ", " SO_ATTACH_REUSEPORT_EBPF " (since Linux 4.5)"
349 For use with the
350 .BR SO_REUSEPORT
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
354 .BR SO_REUSEPORT
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
360 .BR SO_REUSEPORT
361 mechanism.
362
363 Sockets are numbered in the order in which they are added to the group
364 (that is, the order of
365 .BR bind (2)
366 calls for UDP sockets or the order of
367 .BR listen (2)
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
370 group (via
371 .BR close (2))
372 the last socket in the group will be moved into the closed socket's
373 position.
374
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
377 the group.
378 .BR SO_ATTACH_REUSEPORT_CBPF
379 takes the same socket argument type as
380 .BR SO_ATTACH_FILTER
381 and
382 .BR SO_ATTACH_REUSEPORT_EBPF
383 takes the same socket argument type as
384 .BR SO_ATTACH_BPF.
385 UDP support for this feature is available since Linux 4.5.
386 TCP support for this feature is available since Linux 4.6.
387 .TP
388 .B SO_BINDTODEVICE
389 Bind this socket to a particular device like \(lqeth0\(rq,
390 as specified in the passed interface name.
391 If the
392 name is an empty string or the option length is zero, the socket device
393 binding is removed.
394 The passed option is a variable-length null-terminated
395 interface name string with the maximum size of
396 .BR IFNAMSIZ .
397 If a socket is bound to an interface,
398 only packets received from that particular interface are processed by the
399 socket.
400 Note that this works only for some socket types, particularly
401 .B AF_INET
402 sockets.
403 It is not supported for packet sockets (use normal
404 .BR bind (2)
405 there).
406
407 Before Linux 3.8,
408 this socket option could be set, but could not retrieved with
409 .BR getsockopt (2).
410 Since Linux 3.8, it is readable.
411 The
412 .I optlen
413 argument should contain the buffer size available
414 to receive the device name and is recommended to be
415 .BR IFNAMSZ
416 bytes.
417 The real device name length is reported back in the
418 .I optlen
419 argument.
420 .TP
421 .B SO_BROADCAST
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.
426 .TP
427 .B SO_BSDCOMPAT
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
431 to the user program.
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.
438 .TP
439 .B SO_DEBUG
440 Enable socket debugging.
441 Only allowed for processes with the
442 .B CAP_NET_ADMIN
443 capability or an effective user ID of 0.
444 .TP
445 .BR SO_DETACH_FILTER " and " SO_DETACH_BPF
446 These options may be used to remove the BPF program attached to the
447 socket with either
448 .BR SO_ATTACH_FILTER
449 or
450 .BR SO_ATTACH_BPF.
451 The option value is ignored.
452 .BR SO_DETACH_FILTER
453 is available since Linux 2.2.
454 .BR SO_DETACH_BPF
455 is available since Linux 3.19.
456 .TP
457 .BR SO_DOMAIN " (since Linux 2.6.32)"
458 Retrieves the socket domain as an integer, returning a value such as
459 .BR AF_INET6 .
460 See
461 .BR socket (2)
462 for details.
463 This socket option is read-only.
464 .TP
465 .B SO_ERROR
466 Get and clear the pending socket error.
467 This socket option is read-only.
468 Expects an integer.
469 .TP
470 .B SO_DONTROUTE
471 Don't send via a gateway, send only to directly connected hosts.
472 The same effect can be achieved by setting the
473 .B MSG_DONTROUTE
474 flag on a socket
475 .BR send (2)
476 operation.
477 Expects an integer boolean flag.
478 .TP
479 .B SO_KEEPALIVE
480 Enable sending of keep-alive messages on connection-oriented sockets.
481 Expects an integer boolean flag.
482 .TP
483 .B SO_LINGER
484 Sets or gets the
485 .B SO_LINGER
486 option.
487 The argument is a
488 .I linger
489 structure.
490 .sp
491 .in +4n
492 .nf
493 struct linger {
494 int l_onoff; /* linger active */
495 int l_linger; /* how many seconds to linger for */
496 };
497 .fi
498 .in
499 .IP
500 When enabled, a
501 .BR close (2)
502 or
503 .BR shutdown (2)
504 will not return until all queued messages for the socket have been
505 successfully sent or the linger timeout has been reached.
506 Otherwise,
507 the call returns immediately and the closing is done in the background.
508 When the socket is closed as part of
509 .BR exit (2),
510 it always lingers in the background.
511 .TP
512 .B SO_LOCK_FILTER
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,
517 .BR SO_ATTACH_BPF,
518 .BR SO_ATTACH_REUSEPORT_CBPF
519 or
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
523 .BR SO_LOCK_FILTER
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
527 .BR SO_LOCK_FILTER
528 is set will result in an error with value
529 .BR EPERM.
530 .TP
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
539 .B CAP_NET_ADMIN
540 capability.
541 .TP
542 .B SO_OOBINLINE
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
546 .B MSG_OOB
547 flag is set during receiving.
548 .\" don't document it because it can do too much harm.
549 .\".B SO_NO_CHECK
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.
557 .TP
558 .B SO_PASSCRED
559 Enable or disable the receiving of the
560 .B SCM_CREDENTIALS
561 control message.
562 For more information see
563 .BR unix (7).
564 .\" FIXME Document SO_PASSSEC, added in 2.6.18; there is some info
565 .\" in the 2.6.18 ChangeLog
566 .TP
567 .BR SO_PEEK_OFF " (since Linux 3.4)"
568 .\" commit ef64a54f6e558155b4f149bb10666b9e914b6c54
569 This option, which is currently supported only for
570 .BR unix (7)
571 sockets, sets the value of the "peek offset" for the
572 .BR recv (2)
573 system call when used with
574 .BR MSG_PEEK
575 flag.
576
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:
580 .BR recv (2)
581 with the
582 .BR MSG_PEEK
583 flag will peek data from the front of the queue.
584
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.
591
592 If data is removed from the front of the queue via a call to
593 .BR recv (2)
594 (or similar) without the
595 .BR MSG_PEEK
596 flag, the "peek offset" will be decreased by the number of bytes removed.
597 In other words, receiving data without the
598 .B MSG_PEEK
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.
603
604 For datagram sockets, if the "peek offset" points to the middle of a packet,
605 the data returned will be marked with the
606 .BR MSG_TRUNC
607 flag.
608
609 The following example serves to illustrate the use of
610 .BR SO_PEEK_OFF .
611 Suppose a stream socket has the following queued input data:
612
613 aabbccddeeff
614 .IP
615 The following sequence of
616 .BR recv (2)
617 calls would have the effect noted in the comments:
618
619 .in +4n
620 .nf
621 int ov = 4; // Set peek offset to 4
622 setsockopt(fd, SOL_SOCKET, SO_PEEK_OFF, &ov, sizeof(ov));
623
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
628 .fi
629 .in
630 .TP
631 .B SO_PEERCRED
632 Return the credentials of the foreign process connected to this socket.
633 This is possible only for connected
634 .B AF_UNIX
635 stream sockets and
636 .B AF_UNIX
637 stream and datagram socket pairs created using
638 .BR socketpair (2);
639 see
640 .BR unix (7).
641 The returned credentials are those that were in effect at the time
642 of the call to
643 .BR connect (2)
644 or
645 .BR socketpair (2).
646 The argument is a
647 .I ucred
648 structure; define the
649 .B _GNU_SOURCE
650 feature test macro to obtain the definition of that structure from
651 .IR <sys/socket.h> .
652 This socket option is read-only.
653 .TP
654 .B SO_PRIORITY
655 Set the protocol-defined priority for all packets to be sent on
656 this socket.
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.
660 .\" For
661 .\" .BR ip (7),
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
664 .B CAP_NET_ADMIN
665 capability.
666 .TP
667 .BR SO_PROTOCOL " (since Linux 2.6.32)"
668 Retrieves the socket protocol as an integer, returning a value such as
669 .BR IPPROTO_SCTP .
670 See
671 .BR socket (2)
672 for details.
673 This socket option is read-only.
674 .TP
675 .B SO_RCVBUF
676 Sets or gets the maximum socket receive buffer in bytes.
677 The kernel doubles this value (to allow space for bookkeeping overhead)
678 when it is set using
679 .\" Most (all?) other implementations do not do this -- MTK, Dec 05
680 .BR setsockopt (2),
681 and this doubled value is returned by
682 .BR getsockopt (2).
683 .\" The following thread on LMKL is quite informative:
684 .\" getsockopt/setsockopt with SO_RCVBUF and SO_SNDBUF "non-standard" behavior
685 .\" 17 July 2012
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
691 file.
692 The minimum (doubled) value for this option is 256.
693 .TP
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
698 .BR SO_RCVBUF ,
699 but the
700 .I rmem_max
701 limit can be overridden.
702 .TP
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
706 .RB ( SO_SNDLOWAT )
707 or the user on receiving
708 .RB ( SO_RCVLOWAT ).
709 These two values are initialized to 1.
710 .B SO_SNDLOWAT
711 is not changeable on Linux
712 .RB ( setsockopt (2)
713 fails with the error
714 .BR ENOPROTOOPT ).
715 .B SO_RCVLOWAT
716 is changeable
717 only since Linux 2.4.
718 The
719 .BR select (2)
720 and
721 .BR poll (2)
722 system calls currently do not respect the
723 .B SO_RCVLOWAT
724 setting on Linux,
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
727 .B SO_RCVLOWAT
728 bytes are available.
729 .\" See http://marc.theaimsgroup.com/?l=linux-kernel&m=111049368106984&w=2
730 .\" Tested on kernel 2.6.14 -- mtk, 30 Nov 05
731 .TP
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.
737 The argument is a
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
743 .I errno
744 set to
745 .BR EAGAIN
746 or
747 .BR EWOULDBLOCK ,
748 .\" in fact to EAGAIN
749 or
750 .B EINPROGRESS
751 (for
752 .BR connect (2))
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.,
757 .BR read (2),
758 .BR recvmsg (2),
759 .BR send (2),
760 .BR sendmsg (2));
761 timeouts have no effect for
762 .BR select (2),
763 .BR poll (2),
764 .BR epoll_wait (2),
765 and so on.
766 .TP
767 .B SO_REUSEADDR
768 .\" commit c617f398edd4db2b8567a28e899a88f8f574798d
769 .\" https://lwn.net/Articles/542629/
770 Indicates that the rules used in validating addresses supplied in a
771 .BR bind (2)
772 call should allow reuse of local addresses.
773 For
774 .B AF_INET
775 sockets this
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
779 .B INADDR_ANY
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.
783 .TP
784 .BR SO_REUSEPORT " (since Linux 3.9)"
785 Permits multiple
786 .B AF_INET
787 or
788 .B AF_INET6
789 sockets to be bound to an identical socket address.
790 This option must be set on each socket (including the first socket)
791 prior to calling
792 .BR bind (2)
793 on the socket.
794 To prevent port hijacking,
795 all of the processes binding to the same address must have the same
796 effective UID.
797 This option can be employed with both TCP and UDP sockets.
798
799 For TCP sockets, this option allows
800 .BR accept (2)
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
805 .BR accept (2)ing
806 thread that distributes connections,
807 or having multiple threads that compete to
808 .BR accept (2)
809 from the same socket.
810
811 For UDP sockets,
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.
816 .TP
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.
823 .TP
824 .B SO_SNDBUF
825 Sets or gets the maximum socket send buffer in bytes.
826 The kernel doubles this value (to allow space for bookkeeping overhead)
827 when it is set using
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)
830 .BR setsockopt (2),
831 and this doubled value is returned by
832 .BR getsockopt (2).
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
837 file.
838 The minimum (doubled) value for this option is 2048.
839 .TP
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
844 .BR SO_SNDBUF ,
845 but the
846 .I wmem_max
847 limit can be overridden.
848 .TP
849 .B SO_TIMESTAMP
850 Enable or disable the receiving of the
851 .B SO_TIMESTAMP
852 control message.
853 The timestamp control message is sent with level
854 .B SOL_SOCKET
855 and the
856 .I cmsg_data
857 field is a
858 .I "struct timeval"
859 indicating the
860 reception time of the last packet passed to the user in this call.
861 See
862 .BR cmsg (3)
863 for details on control messages.
864 .TP
865 .B SO_TYPE
866 Gets the socket type as an integer (e.g.,
867 .BR SOCK_STREAM ).
868 This socket option is read-only.
869 .TP
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
874 .BR CAP_NET_ADMIN .
875 The default for this option is controlled by the
876 .I /proc/sys/net/core/busy_read
877 file.
878
879 The value in the
880 .I /proc/sys/net/core/busy_poll
881 file determines how long
882 .BR select (2)
883 and
884 .BR poll (2)
885 will busy poll when they operate on sockets with
886 .BR SO_BUSY_POLL
887 set and no events to report are found.
888
889 In both cases,
890 busy polling will only be done when the socket last received data
891 from a network device that supports this option.
892
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.
896 .SS Signals
897 When writing onto a connection-oriented socket that has been shut down
898 (by the local or the remote end)
899 .B SIGPIPE
900 is sent to the writing process and
901 .B EPIPE
902 is returned.
903 The signal is not sent when the write call
904 specified the
905 .B MSG_NOSIGNAL
906 flag.
907 .PP
908 When requested with the
909 .B FIOSETOWN
910 .BR fcntl (2)
911 or
912 .B SIOCSPGRP
913 .BR ioctl (2),
914 .B SIGIO
915 is sent when an I/O event occurs.
916 It is possible to use
917 .BR poll (2)
918 or
919 .BR select (2)
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
922 .B F_SETSIG
923 .BR fcntl (2);
924 the handler of the real time signal will be called with
925 the file descriptor in the
926 .I si_fd
927 field of its
928 .IR siginfo_t .
929 See
930 .BR fcntl (2)
931 for more information.
932 .PP
933 Under some circumstances (e.g., multiple processes accessing a
934 single socket), the condition that caused the
935 .B SIGIO
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
940 .SS /proc interfaces
941 The core socket networking parameters can be accessed
942 via files in the directory
943 .IR /proc/sys/net/core/ .
944 .TP
945 .I rmem_default
946 contains the default setting in bytes of the socket receive buffer.
947 .TP
948 .I rmem_max
949 contains the maximum socket receive buffer size in bytes which a user may
950 set by using the
951 .B SO_RCVBUF
952 socket option.
953 .TP
954 .I wmem_default
955 contains the default setting in bytes of the socket send buffer.
956 .TP
957 .I wmem_max
958 contains the maximum socket send buffer size in bytes which a user may
959 set by using the
960 .B SO_SNDBUF
961 socket option.
962 .TP
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.
966 .TP
967 .I netdev_max_backlog
968 Maximum number of packets in the global input queue.
969 .TP
970 .I optmem_max
971 Maximum length of ancillary data and user control data like the iovecs
972 per socket.
973 .\" netdev_fastroute is not documented because it is experimental
974 .SS Ioctls
975 These operations can be accessed using
976 .BR ioctl (2):
977
978 .in +4n
979 .nf
980 .IB error " = ioctl(" ip_socket ", " ioctl_type ", " &value_result ");"
981 .fi
982 .in
983 .TP
984 .B SIOCGSTAMP
985 Return a
986 .I struct timeval
987 with the receive timestamp of the last packet passed to the user.
988 This is useful for accurate round trip time measurements.
989 See
990 .BR setitimer (2)
991 for a description of
992 .IR "struct timeval" .
993 .\"
994 This ioctl should be used only if the socket option
995 .B SO_TIMESTAMP
996 is not set on the socket.
997 Otherwise, it returns the timestamp of the
998 last packet that was received while
999 .B SO_TIMESTAMP
1000 was not set, or it fails if no such packet has been received,
1001 (i.e.,
1002 .BR ioctl (2)
1003 returns \-1 with
1004 .I errno
1005 set to
1006 .BR ENOENT ).
1007 .TP
1008 .B SIOCSPGRP
1009 Set the process or process group to send
1010 .B SIGIO
1011 or
1012 .B SIGURG
1013 signals
1014 to when an
1015 asynchronous I/O operation has finished or urgent data is available.
1016 The argument is a pointer to a
1017 .IR pid_t .
1018 If the argument is positive, send the signals to that process.
1019 If the
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
1024 .B CAP_KILL
1025 capability or an effective UID of 0.
1026 .TP
1027 .B FIOASYNC
1028 Change the
1029 .B O_ASYNC
1030 flag to enable or disable asynchronous I/O mode of the socket.
1031 Asynchronous I/O mode means that the
1032 .B SIGIO
1033 signal or the signal set with
1034 .B F_SETSIG
1035 is raised when a new I/O event occurs.
1036 .IP
1037 Argument is an integer boolean flag.
1038 (This operation is synonymous with the use of
1039 .BR fcntl (2)
1040 to set the
1041 .B O_ASYNC
1042 flag.)
1043 .\"
1044 .TP
1045 .B SIOCGPGRP
1046 Get the current process or process group that receives
1047 .B SIGIO
1048 or
1049 .B SIGURG
1050 signals,
1051 or 0
1052 when none is set.
1053 .PP
1054 Valid
1055 .BR fcntl (2)
1056 operations:
1057 .TP
1058 .B FIOGETOWN
1059 The same as the
1060 .B SIOCGPGRP
1061 .BR ioctl (2).
1062 .TP
1063 .B FIOSETOWN
1064 The same as the
1065 .B SIOCSPGRP
1066 .BR ioctl (2).
1067 .SH VERSIONS
1068 .B SO_BINDTODEVICE
1069 was introduced in Linux 2.0.30.
1070 .B SO_PASSCRED
1071 is new in Linux 2.2.
1072 The
1073 .I /proc
1074 interfaces were introduced in Linux 2.2.
1075 .B SO_RCVTIMEO
1076 and
1077 .B SO_SNDTIMEO
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.
1081 .SH NOTES
1082 Linux assumes that half of the send/receive buffer is used for internal
1083 kernel structures; thus the values in the corresponding
1084 .I /proc
1085 files are twice what can be observed on the wire.
1086
1087 Linux will allow port reuse only with the
1088 .B SO_REUSEADDR
1089 option
1090 when this option was set both in the previous program that performed a
1091 .BR bind (2)
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
1095 .B SO_REUSEADDR
1096 option.
1097 Typically this difference is invisible, since, for example, a server
1098 program is designed to always set this option.
1099 .\" .SH AUTHORS
1100 .\" This man page was written by Andi Kleen.
1101 .SH SEE ALSO
1102 .BR bpf (2),
1103 .BR connect (2),
1104 .BR getsockopt (2),
1105 .BR setsockopt (2),
1106 .BR socket (2),
1107 .BR pcap (3),
1108 .BR capabilities (7),
1109 .BR ddp (7),
1110 .BR ip (7),
1111 .BR packet (7),
1112 .BR tcp (7),
1113 .BR udp (7),
1114 .BR unix (7)