Debian Codegraph @

upstream/debian-trixie/man7/socket.7

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