1.\" $OpenBSD: getsockopt.2,v 1.58 2021/10/08 14:12:58 jmc Exp $ 2.\" $NetBSD: getsockopt.2,v 1.7 1995/02/27 12:33:29 cgd Exp $ 3.\" 4.\" Copyright (c) 1983, 1991, 1993 5.\" The Regents of the University of California. All rights reserved. 6.\" 7.\" Redistribution and use in source and binary forms, with or without 8.\" modification, are permitted provided that the following conditions 9.\" are met: 10.\" 1. Redistributions of source code must retain the above copyright 11.\" notice, this list of conditions and the following disclaimer. 12.\" 2. Redistributions in binary form must reproduce the above copyright 13.\" notice, this list of conditions and the following disclaimer in the 14.\" documentation and/or other materials provided with the distribution. 15.\" 3. Neither the name of the University nor the names of its contributors 16.\" may be used to endorse or promote products derived from this software 17.\" without specific prior written permission. 18.\" 19.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29.\" SUCH DAMAGE. 30.\" 31.\" @(#)getsockopt.2 8.3 (Berkeley) 4/19/94 32.\" 33.Dd $Mdocdate: October 8 2021 $ 34.Dt GETSOCKOPT 2 35.Os 36.Sh NAME 37.Nm getsockopt , 38.Nm setsockopt 39.Nd get and set options on sockets 40.Sh SYNOPSIS 41.In sys/socket.h 42.Ft int 43.Fn getsockopt "int s" "int level" "int optname" "void *optval" "socklen_t *optlen" 44.Ft int 45.Fn setsockopt "int s" "int level" "int optname" "const void *optval" "socklen_t optlen" 46.Sh DESCRIPTION 47.Fn getsockopt 48and 49.Fn setsockopt 50manipulate the 51.Em options 52associated with a socket. 53Options may exist at multiple protocol levels; 54they are always present at the uppermost 55.Dq socket 56level. 57.Pp 58When manipulating socket options the level at which the 59option resides and the name of the option must be specified. 60To manipulate options at the socket level, 61.Fa level 62is specified as 63.Dv SOL_SOCKET . 64To manipulate options at any other level the protocol number of the 65appropriate protocol controlling the option is supplied. 66For example, to indicate that an option is to be interpreted by the 67TCP protocol, 68.Fa level 69should be set to the protocol number of TCP; see 70.Xr getprotoent 3 . 71.Pp 72The parameters 73.Fa optval 74and 75.Fa optlen 76are used to access option values for 77.Fn setsockopt . 78For 79.Fn getsockopt 80they identify a buffer in which the value for the 81requested option(s) are to be returned. 82For 83.Fn getsockopt , 84.Fa optlen 85is a value-result parameter, initially containing the 86size of the buffer pointed to by 87.Fa optval , 88and modified on return to indicate the actual size of the value returned. 89If no option value is to be supplied or returned, 90.Fa optval 91may be 92.Dv NULL . 93.Pp 94.Fa optname 95and any specified options are passed uninterpreted to the appropriate 96protocol module for interpretation. 97The include file 98.In sys/socket.h 99contains definitions for socket level options, described below. 100Options at other protocol levels vary in format and name; 101consult the appropriate entries in section 4 of the manual. 102.Pp 103Most socket-level options utilize an 104.Li int 105parameter for 106.Fa optval . 107For 108.Fn setsockopt , 109the parameter should be non-zero to enable a boolean option, 110or zero if the option is to be disabled. 111.Dv SO_LINGER 112uses a 113.Li struct linger 114parameter, defined in 115.In sys/socket.h , 116which specifies the desired state of the option and the 117linger interval (see below). 118.Dv SO_SNDTIMEO 119and 120.Dv SO_RCVTIMEO 121use a 122.Li struct timeval 123parameter, defined in 124.In sys/time.h . 125.Pp 126The following options are recognized at the socket level. 127Except as noted, each may be examined with 128.Fn getsockopt 129and set with 130.Fn setsockopt . 131.Pp 132.Bl -tag -width SO_OOBINLINE -offset indent -compact 133.It Dv SO_DEBUG 134enables recording of debugging information 135.It Dv SO_REUSEADDR 136enables local address reuse 137.It Dv SO_REUSEPORT 138enables duplicate address and port bindings 139.It Dv SO_KEEPALIVE 140enables keep connections alive 141.It Dv SO_DONTROUTE 142enables routing bypass; not supported 143.It Dv SO_LINGER 144linger on close if data present 145.It Dv SO_BROADCAST 146enables permission to transmit broadcast messages 147.It Dv SO_OOBINLINE 148enables reception of out-of-band data in band 149.It Dv SO_BINDANY 150enables binding to any address 151.It Dv SO_SNDBUF 152set buffer size for output 153.It Dv SO_RCVBUF 154set buffer size for input 155.It Dv SO_SNDLOWAT 156set minimum count for output 157.It Dv SO_RCVLOWAT 158set minimum count for input 159.It Dv SO_SNDTIMEO 160set timeout value for output 161.It Dv SO_RCVTIMEO 162set timeout value for input 163.It Dv SO_TIMESTAMP 164enables reception of a timestamp with datagrams 165.It Dv SO_RTABLE 166set the routing table used for route lookups 167.It Dv SO_SPLICE 168splice two sockets together or get data length 169.It Dv SO_ZEROIZE 170clear all memory containing user supplied data 171.It Dv SO_TYPE 172get the type of the socket (get only) 173.It Dv SO_ERROR 174get and clear error on the socket (get only) 175.It Dv SO_DOMAIN 176get the domain of the socket (get only) 177.It Dv SO_PROTOCOL 178get the protocol of the socket (get only) 179.It Dv SO_PEERCRED 180get the credentials from other side of connection (get only) 181.El 182.Pp 183.Dv SO_DEBUG 184enables debugging in the underlying protocol modules. 185Transliterate the protocol trace with 186.Xr trpt 8 . 187.Dv SO_REUSEADDR 188indicates that the rules used in validating addresses supplied in a 189.Xr bind 2 190call should allow reuse of local addresses 191by callers with the same user ID (or the superuser). 192.Dv SO_REUSEPORT 193allows completely duplicate bindings by multiple processes if they all set 194.Dv SO_REUSEPORT 195before binding the port. 196This option permits multiple instances of a program to each 197receive UDP/IP multicast or broadcast datagrams destined for the bound port. 198.Dv SO_KEEPALIVE 199enables the periodic transmission of messages on a connected socket. 200Should the connected party fail to respond to these messages, the connection 201is considered broken and processes using the socket are notified via a 202.Dv SIGPIPE 203signal when attempting to send data. 204.Pp 205.Dv SO_LINGER 206controls the action taken when unsent messages 207are queued on socket and a 208.Xr close 2 209is performed. 210If the socket promises reliable delivery of data and 211.Dv SO_LINGER 212is set, the system will block the process on the 213.Xr close 2 214attempt until it is able to transmit the data or until it decides it 215is unable to deliver the information (a timeout period measured in seconds, 216termed the linger interval, is specified in the 217.Fn setsockopt 218call when 219.Dv SO_LINGER 220is requested). 221If 222.Dv SO_LINGER 223is disabled and a 224.Xr close 2 225is issued, the system will process the close in a manner that allows 226the process to continue as quickly as possible. 227.Pp 228The option 229.Dv SO_BROADCAST 230requests permission to send broadcast datagrams 231on the socket. 232Broadcast was a privileged operation in earlier versions of the system. 233With protocols that support out-of-band data, the 234.Dv SO_OOBINLINE 235option requests that out-of-band data be placed in the normal data input 236queue as received; it will then be accessible with 237.Xr recv 2 238or 239.Xr read 2 240calls without the 241.Dv MSG_OOB 242flag. 243Some protocols always behave as if this option is set. 244.Pp 245.Dv SO_BINDANY 246allows the socket to be bound to addresses 247which are not local to the machine, so it 248can be used to make a transparent proxy. 249Note that this option is limited to the superuser. 250In order to receive packets for these addresses, 251.Dv SO_BINDANY 252needs to be combined with matching outgoing 253.Xr pf 4 254rules with the 255.Ar divert-reply 256parameter. 257For example, with the following rule the socket receives packets 258for 192.168.0.10 even if it is not a local address: 259.Pp 260.Dl pass out inet from 192.168.0.10 divert-reply 261.Pp 262.Dv SO_SNDBUF 263and 264.Dv SO_RCVBUF 265are options to adjust the normal 266buffer sizes allocated for output and input buffers, respectively. 267The buffer size may be increased for high-volume connections, 268or may be decreased to limit the possible backlog of incoming data. 269The system places an absolute limit on these values. 270.Pp 271.Dv SO_SNDLOWAT 272is an option to set the minimum count for output operations. 273Most output operations process all of the data supplied 274by the call, delivering data to the protocol for transmission 275and blocking as necessary for flow control. 276Nonblocking output operations will process as much data as permitted 277subject to flow control without blocking, but will process no data 278if flow control does not allow the smaller of the low water mark value 279or the entire request to be processed. 280A 281.Xr select 2 282or 283.Xr poll 2 284operation testing the ability to write to a socket will return true 285only if the low water mark amount could be processed. 286The default value for 287.Dv SO_SNDLOWAT 288is set to a convenient size for network efficiency, often 1024. 289.Dv SO_RCVLOWAT 290is an option to set the minimum count for input operations. 291In general, receive calls will block until any (non-zero) amount of data 292is received, then return with the smaller of the amount available or the amount 293requested. 294The default value for 295.Dv SO_RCVLOWAT 296is 1. 297If 298.Dv SO_RCVLOWAT 299is set to a larger value, blocking receive calls normally 300wait until they have received the smaller of the low water mark value 301or the requested amount. 302Receive calls may still return less than the low water mark if an error 303occurs, a signal is caught, or the type of data next in the receive queue 304is different than that returned. 305.Pp 306.Dv SO_SNDTIMEO 307is an option to set a timeout value for output operations. 308It accepts a 309.Li struct timeval 310parameter with the number of seconds and microseconds 311used to limit waits for output operations to complete. 312If a send operation has blocked for this much time, 313it returns with a partial count or with the error 314.Er EWOULDBLOCK 315if no data was sent. 316In the current implementation, this timer is restarted each time additional 317data are delivered to the protocol, 318implying that the limit applies to output portions ranging in size 319from the low water mark to the high water mark for output. 320.Dv SO_RCVTIMEO 321is an option to set a timeout value for input operations. 322It accepts a 323.Li struct timeval 324parameter with the number of seconds and microseconds 325used to limit waits for input operations to complete. 326In the current implementation, this timer is restarted each time additional 327data are received by the protocol, 328and thus the limit is in effect an inactivity timer. 329If a receive operation has been blocked for this much time without 330receiving additional data, it returns with a short count 331or with the error 332.Er EWOULDBLOCK 333if no data were received. 334.Pp 335If the 336.Dv SO_TIMESTAMP 337option is enabled on a 338.Dv SOCK_DGRAM 339socket, the 340.Xr recvmsg 2 341call will return a timestamp corresponding to when the datagram was 342received. 343The msg_control field in the msghdr structure points to a buffer 344that contains a cmsghdr structure followed by a struct timeval. 345The cmsghdr fields have the following values: 346.Bd -literal -offset indent 347cmsg_len = CMSG_LEN(sizeof(struct timeval)) 348cmsg_level = SOL_SOCKET 349cmsg_type = SCM_TIMESTAMP 350.Ed 351.Pp 352The 353.Dv SO_RTABLE 354option gets or sets the routing table which will be used by the socket 355for address lookups. 356If a protocol family of the socket doesn't support switching routing tables, 357the 358.Er ENOPROTOOPT 359error is returned. 360Only the superuser is allowed to change the routing table if it is already 361set to a non-zero value. 362A socket's chosen routing table is initialized from the process's configuration, 363previously selected using 364.Xr setrtable 2 . 365.Pp 366.Dv SO_SPLICE 367can splice together two TCP or UDP sockets for unidirectional 368zero-copy data transfers. 369Splice also the other way around to get bidirectional data flow. 370Both sockets must be of the same type. 371In the first form, 372.Fn setsockopt 373is called with the source socket 374.Fa s 375and the drain socket's 376.Vt int 377file descriptor as 378.Fa optval . 379In the second form, 380.Fa optval 381is a 382.Vt struct splice 383with the drain socket in 384.Va sp_fd , 385a positive maximum number of bytes or 0 in 386.Va sp_max 387and an idle timeout 388.Va sp_idle 389in the form of a 390.Vt struct timeval . 391If \-1 is given as drain socket, the source socket 392.Fa s 393gets unspliced. 394Otherwise the spliced data transfer continues within the kernel 395until the optional maximum is reached, one of the connections 396terminates, idle timeout expires or an error occurs. 397A successful 398.Xr select 2 , 399.Xr poll 2 , 400or 401.Xr kqueue 2 402operation testing the ability to read from the source socket indicates 403that the splicing has terminated. 404When one of the sockets gets closed, splicing ends. 405The error status can be examined with 406.Dv SO_ERROR 407at the source socket. 408The 409.Er ELOOP 410error is set if userland created a loop by splicing sockets connected 411to localhost. 412The 413.Er ETIMEDOUT 414error is set if there was no data transferred between two sockets 415during the 416.Va sp_idle 417period of time. 418The 419.Er EFBIG 420error is set after exactly 421.Va sp_max 422bytes have been transferred. 423Note that if a maximum is given, it is only guaranteed that no more 424bytes are transferred. 425A short splice can happen, but then a second call to splice will 426transfer the remaining data immediately. 427The 428.Dv SO_SPLICE 429option with 430.Fn getsockopt 431and an 432.Vt off_t 433value as 434.Fa optval 435can be used to retrieve the number of bytes transferred so far from the 436source socket 437.Fa s . 438A successful new splice resets this number. 439.Pp 440Userland may write sensitive data into a socket. 441If 442.Dv SO_ZEROIZE 443is set, overwrite kernel memory after sending data. 444.Pp 445Finally, 446.Dv SO_TYPE , 447.Dv SO_DOMAIN , 448.Dv SO_PROTOCOL , 449.Dv SO_ERROR , 450and 451.Dv SO_PEERCRED 452are options used only with 453.Fn getsockopt . 454.Dv SO_TYPE 455returns the type of the socket, such as 456.Dv SOCK_STREAM ; 457it is useful for servers that inherit sockets on startup. 458.Dv SO_DOMAIN 459returns the domain of the socket, such as 460.Dv AF_INET . 461.Dv SO_PROTOCOL 462returns the protocol of the socket such as 463.Dv IPPROTO_TCP . 464.Dv SO_ERROR 465returns any pending error on the socket and clears the error status. 466It may be used to check for asynchronous errors on connected 467datagram sockets or for other asynchronous errors. 468.Dv SO_PEERCRED 469fetches the 470.Va struct sockpeercred 471credentials from the other side of the connection 472(currently only possible on 473.Dv AF_UNIX 474sockets). 475These credentials are from the time that 476.Xr bind 2 , 477.Xr connect 2 478or 479.Xr socketpair 2 480were called. 481.Sh RETURN VALUES 482.Rv -std 483.Sh ERRORS 484The call succeeds unless: 485.Bl -tag -width Er 486.It Bq Er EBADF 487The argument 488.Fa s 489is not a valid descriptor. 490.It Bq Er ENOTSOCK 491The argument 492.Fa s 493is a file, not a socket. 494.It Bq Er ENOPROTOOPT 495The option is unknown at the level indicated. 496.It Bq Er EOPNOTSUPP 497The option is unsupported. 498.It Bq Er EFAULT 499The address pointed to by 500.Fa optval 501is not in a valid part of the process address space. 502For 503.Fn getsockopt , 504this error may also be returned if 505.Fa optlen 506is not in a valid part of the process address space. 507.El 508.Sh SEE ALSO 509.Xr connect 2 , 510.Xr getrtable 2 , 511.Xr ioctl 2 , 512.Xr poll 2 , 513.Xr select 2 , 514.Xr socket 2 , 515.Xr getprotoent 3 , 516.Xr divert 4 , 517.Xr pf.conf 5 , 518.Xr protocols 5 , 519.Xr sosplice 9 520.Sh STANDARDS 521The 522.Fn getsockopt 523and 524.Fn setsockopt 525functions conform to 526.St -p1003.1-2008 . 527.Sh HISTORY 528The 529.Fn getsockopt 530system call appeared in 531.Bx 4.1c . 532.Sh BUGS 533Several of the socket options should be handled at lower levels of the system. 534