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