xref: /freebsd/lib/libsys/getsockopt.2 (revision a1da7dc1cdad8c000622a7b23ff5994ccfe9cac6)
1.\" Copyright (c) 1983, 1991, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\" 3. Neither the name of the University nor the names of its contributors
13.\"    may be used to endorse or promote products derived from this software
14.\"    without specific prior written permission.
15.\"
16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26.\" SUCH DAMAGE.
27.\"
28.Dd July 8, 2024
29.Dt GETSOCKOPT 2
30.Os
31.Sh NAME
32.Nm getsockopt ,
33.Nm setsockopt
34.Nd get and set options on sockets
35.Sh LIBRARY
36.Lb libc
37.Sh SYNOPSIS
38.In sys/types.h
39.In sys/socket.h
40.Ft int
41.Fn getsockopt "int s" "int level" "int optname" "void * restrict optval" "socklen_t * restrict optlen"
42.Ft int
43.Fn setsockopt "int s" "int level" "int optname" "const void *optval" "socklen_t optlen"
44.Sh DESCRIPTION
45The
46.Fn getsockopt
47and
48.Fn setsockopt
49system calls
50manipulate the
51.Em options
52associated with a socket.
53Options may exist at multiple
54protocol levels; they 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
65other level the protocol number of the appropriate protocol
66controlling the option is supplied.
67For example,
68to indicate that an option is to be interpreted by the
69.Tn TCP
70protocol,
71.Fa level
72should be set to the protocol number of
73.Tn TCP ;
74see
75.Xr getprotoent 3 .
76.Pp
77The
78.Fa optval
79and
80.Fa optlen
81arguments
82are used to access option values for
83.Fn setsockopt .
84For
85.Fn getsockopt
86they identify a buffer in which the value for the
87requested option(s) are to be returned.
88For
89.Fn getsockopt ,
90.Fa optlen
91is a value-result argument, initially containing the
92size of the buffer pointed to by
93.Fa optval ,
94and modified on return to indicate the actual size of
95the value returned.
96If no option value is
97to be supplied or returned,
98.Fa optval
99may be NULL.
100.Pp
101The
102.Fa optname
103argument
104and any specified options are passed uninterpreted to the appropriate
105protocol module for interpretation.
106The include file
107.In sys/socket.h
108contains definitions for
109socket level options, described below.
110Options at other protocol levels vary in format and
111name; consult the appropriate entries in
112section
1134 of the manual.
114.Pp
115Most socket-level options utilize an
116.Vt int
117argument for
118.Fa optval .
119For
120.Fn setsockopt ,
121the argument should be non-zero to enable a boolean option,
122or zero if the option is to be disabled.
123.Dv SO_LINGER
124uses a
125.Vt "struct linger"
126argument, defined in
127.In sys/socket.h ,
128which specifies the desired state of the option and the
129linger interval (see below).
130.Dv SO_SNDTIMEO
131and
132.Dv SO_RCVTIMEO
133use a
134.Vt "struct timeval"
135argument, defined in
136.In sys/time.h .
137.Pp
138The following options are recognized at the socket level.
139For protocol-specific options, see protocol manual pages,
140e.g.
141.Xr ip 4
142or
143.Xr tcp 4 .
144Except as noted, each may be examined with
145.Fn getsockopt
146and set with
147.Fn setsockopt .
148.Bl -column SO_ACCEPTFILTER -offset indent
149.It Dv SO_DEBUG Ta "enables recording of debugging information"
150.It Dv SO_REUSEADDR Ta "enables local address reuse"
151.It Dv SO_REUSEPORT Ta "enables duplicate address and port bindings"
152.It Dv SO_REUSEPORT_LB Ta "enables duplicate address and port bindings with load balancing"
153.It Dv SO_KEEPALIVE Ta "enables keep connections alive"
154.It Dv SO_DONTROUTE Ta "enables routing bypass for outgoing messages"
155.It Dv SO_LINGER  Ta "linger on close if data present"
156.It Dv SO_BROADCAST Ta "enables permission to transmit broadcast messages"
157.It Dv SO_OOBINLINE Ta "enables reception of out-of-band data in band"
158.It Dv SO_SNDBUF Ta "set buffer size for output"
159.It Dv SO_RCVBUF Ta "set buffer size for input"
160.It Dv SO_SNDLOWAT Ta "set minimum count for output"
161.It Dv SO_RCVLOWAT Ta "set minimum count for input"
162.It Dv SO_SNDTIMEO Ta "set timeout value for output"
163.It Dv SO_RCVTIMEO Ta "set timeout value for input"
164.It Dv SO_ACCEPTFILTER Ta "set accept filter on listening socket"
165.It Dv SO_NOSIGPIPE Ta
166controls generation of
167.Dv SIGPIPE
168for the socket
169.It Dv SO_TIMESTAMP Ta "enables reception of a timestamp with datagrams"
170.It Dv SO_BINTIME Ta "enables reception of a timestamp with datagrams"
171.It Dv SO_ACCEPTCONN Ta "get listening status of the socket (get only)"
172.It Dv SO_DOMAIN Ta "get the domain of the socket (get only)"
173.It Dv SO_TYPE Ta "get the type of the socket (get only)"
174.It Dv SO_PROTOCOL Ta "get the protocol number for the socket (get only)"
175.It Dv SO_PROTOTYPE Ta "SunOS alias for the Linux SO_PROTOCOL (get only)"
176.It Dv SO_ERROR Ta "get and clear error on the socket (get only)"
177.It Dv SO_RERROR Ta "enables receive error reporting"
178.It Dv SO_SETFIB Ta "set the associated FIB (routing table) for the socket (set only)"
179.El
180.Pp
181The following options are recognized in
182.Fx :
183.Bl -column SO_LISTENINCQLEN -offset indent
184.It Dv SO_LABEL Ta "get MAC label of the socket (get only)"
185.It Dv SO_PEERLABEL Ta "get socket's peer's MAC label (get only)"
186.It Dv SO_LISTENQLIMIT Ta "get backlog limit of the socket (get only)"
187.It Dv SO_LISTENQLEN Ta "get complete queue length of the socket (get only)"
188.It Dv SO_LISTENINCQLEN Ta "get incomplete queue length of the socket (get only)"
189.It Dv SO_USER_COOKIE Ta "set the 'so_user_cookie' value for the socket (uint32_t, set only)"
190.It Dv SO_TS_CLOCK Ta "set specific format of timestamp returned by SO_TIMESTAMP"
191.It Dv SO_MAX_PACING_RATE Ta "set the maximum transmit rate in bytes per second for the socket"
192.It Dv SO_NO_OFFLOAD Ta "disables protocol offloads"
193.It Dv SO_NO_DDP Ta "disables direct data placement offload"
194.It Dv SO_SPLICE Ta "splice two sockets together"
195.El
196.Pp
197.Dv SO_DEBUG
198enables debugging in the underlying protocol modules.
199.Pp
200.Dv SO_REUSEADDR
201indicates that the rules used in validating addresses supplied
202in a
203.Xr bind 2
204system call should allow reuse of local addresses.
205.Pp
206.Dv SO_REUSEPORT
207allows completely duplicate bindings by multiple processes
208if they all set
209.Dv SO_REUSEPORT
210before binding the port.
211This option permits multiple instances of a program to each
212receive UDP/IP multicast or broadcast datagrams destined for the bound port.
213.Pp
214.Dv SO_REUSEPORT_LB
215allows completely duplicate bindings by multiple sockets
216if they all set
217.Dv SO_REUSEPORT_LB
218before binding the port.
219Incoming TCP and UDP connections are distributed among the participating
220listening sockets based on a hash function of local port number, and foreign IP
221address and port number.
222A maximum of 256 sockets can be bound to the same load-balancing group.
223.Pp
224.Dv SO_KEEPALIVE
225enables the
226periodic transmission of messages on a connected socket.
227Should the
228connected party fail to respond to these messages, the connection is
229considered broken and processes using the socket are notified via a
230.Dv SIGPIPE
231signal when attempting to send data.
232.Pp
233.Dv SO_DONTROUTE
234indicates that outgoing messages should
235bypass the standard routing facilities.
236Instead, messages are directed
237to the appropriate network interface according to the network portion
238of the destination address.
239.Pp
240.Dv SO_LINGER
241controls the action taken when unsent messages
242are queued on socket and a
243.Xr close 2
244is performed.
245If the socket promises reliable delivery of data and
246.Dv SO_LINGER
247is set,
248the system will block the process on the
249.Xr close 2
250attempt until it is able to transmit the data or until it decides it
251is unable to deliver the information (a timeout period, termed the
252linger interval, is specified in seconds in the
253.Fn setsockopt
254system call when
255.Dv SO_LINGER
256is requested).
257If
258.Dv SO_LINGER
259is disabled and a
260.Xr close 2
261is issued, the system will process the close in a manner that allows
262the process to continue as quickly as possible.
263.Pp
264The option
265.Dv SO_BROADCAST
266requests permission to send broadcast datagrams
267on the socket.
268Broadcast was a privileged operation in earlier versions of the system.
269.Pp
270With protocols that support out-of-band data, the
271.Dv SO_OOBINLINE
272option
273requests that out-of-band data be placed in the normal data input queue
274as received; it will then be accessible with
275.Xr recv 2
276or
277.Xr read 2
278calls without the
279.Dv MSG_OOB
280flag.
281Some protocols always behave as if this option is set.
282.Pp
283.Dv SO_SNDBUF
284and
285.Dv SO_RCVBUF
286are options to adjust the normal
287buffer sizes allocated for output and input buffers, respectively.
288The buffer size may be increased for high-volume connections,
289or may be decreased to limit the possible backlog of incoming data.
290The system places an absolute maximum on these values, which is accessible
291through the
292.Xr sysctl 3
293MIB variable
294.Dq Li kern.ipc.maxsockbuf .
295.Pp
296.Dv SO_SNDLOWAT
297is an option to set the minimum count for output operations.
298Most output operations process all of the data supplied
299by the call, delivering data to the protocol for transmission
300and blocking as necessary for flow control.
301Nonblocking output operations will process as much data as permitted
302subject to flow control without blocking, but will process no data
303if flow control does not allow the smaller of the low water mark value
304or the entire request to be processed.
305A
306.Xr select 2
307operation testing the ability to write to a socket will return true
308only if the low water mark amount could be processed.
309The default value for
310.Dv SO_SNDLOWAT
311is set to a convenient size for network efficiency, often 1024.
312.Pp
313.Dv SO_RCVLOWAT
314is an option to set the minimum count for input operations.
315In general, receive calls will block until any (non-zero) amount of data
316is received, then return with the smaller of the amount available or the amount
317requested.
318The default value for
319.Dv SO_RCVLOWAT
320is 1.
321If
322.Dv SO_RCVLOWAT
323is set to a larger value, blocking receive calls normally
324wait until they have received the smaller of the low water mark value
325or the requested amount.
326Receive calls may still return less than the low water mark if an error
327occurs, a signal is caught, or the type of data next in the receive queue
328is different from that which was returned.
329.Pp
330.Dv SO_SNDTIMEO
331is an option to set a timeout value for output operations.
332It accepts a
333.Vt "struct timeval"
334argument with the number of seconds and microseconds
335used to limit waits for output operations to complete.
336If a send operation has blocked for this much time,
337it returns with a partial count
338or with the error
339.Er EWOULDBLOCK
340if no data were sent.
341In the current implementation, this timer is restarted each time additional
342data are delivered to the protocol,
343implying that the limit applies to output portions ranging in size
344from the low water mark to the high water mark for output.
345.Pp
346.Dv SO_RCVTIMEO
347is an option to set a timeout value for input operations.
348It accepts a
349.Vt "struct timeval"
350argument with the number of seconds and microseconds
351used to limit waits for input operations to complete.
352In the current implementation, this timer is restarted each time additional
353data are received by the protocol,
354and thus the limit is in effect an inactivity timer.
355If a receive operation has been blocked for this much time without
356receiving additional data, it returns with a short count
357or with the error
358.Er EWOULDBLOCK
359if no data were received.
360.Pp
361.Dv SO_SETFIB
362can be used to over-ride the default FIB (routing table) for the given socket.
363The value must be from 0 to one less than the number returned from
364the sysctl
365.Em net.fibs .
366.Pp
367.Dv SO_USER_COOKIE
368can be used to set the uint32_t so_user_cookie field in the socket.
369The value is an uint32_t, and can be used in the kernel code that
370manipulates traffic related to the socket.
371The default value for the field is 0.
372As an example, the value can be used as the skipto target or
373pipe number in
374.Nm ipfw/dummynet .
375.Pp
376.Dv SO_ACCEPTFILTER
377places an
378.Xr accept_filter 9
379on the socket,
380which will filter incoming connections
381on a listening stream socket before being presented for
382.Xr accept 2 .
383Once more,
384.Xr listen 2
385must be called on the socket before
386trying to install the filter on it,
387or else the
388.Fn setsockopt
389system call will fail.
390.Bd -literal
391struct  accept_filter_arg {
392        char    af_name[16];
393        char    af_arg[256-16];
394};
395.Ed
396.Pp
397The
398.Fa optval
399argument
400should point to a
401.Fa struct accept_filter_arg
402that will select and configure the
403.Xr accept_filter 9 .
404The
405.Fa af_name
406argument
407should be filled with the name of the accept filter
408that the application wishes to place on the listening socket.
409The optional argument
410.Fa af_arg
411can be passed to the accept
412filter specified by
413.Fa af_name
414to provide additional configuration options at attach time.
415Passing in an
416.Fa optval
417of NULL will remove the filter.
418.Pp
419The
420.Dv SO_NOSIGPIPE
421option controls generation of the
422.Dv SIGPIPE
423signal normally sent
424when writing to a connected socket where the other end has been
425closed returns with the error
426.Er EPIPE .
427.Pp
428If the
429.Dv SO_TIMESTAMP
430or
431.Dv SO_BINTIME
432option is enabled on a
433.Dv SOCK_DGRAM
434socket, the
435.Xr recvmsg 2
436call may return a timestamp corresponding to when the datagram was received.
437However, it may not, for example due to a resource shortage.
438The
439.Va msg_control
440field in the
441.Vt msghdr
442structure points to a buffer that contains a
443.Vt cmsghdr
444structure followed by a
445.Vt "struct timeval"
446for
447.Dv SO_TIMESTAMP
448and
449.Vt "struct bintime"
450for
451.Dv SO_BINTIME .
452The
453.Vt cmsghdr
454fields have the following values for TIMESTAMP by default:
455.Bd -literal
456     cmsg_len = CMSG_LEN(sizeof(struct timeval));
457     cmsg_level = SOL_SOCKET;
458     cmsg_type = SCM_TIMESTAMP;
459.Ed
460.Pp
461and for
462.Dv SO_BINTIME :
463.Bd -literal
464     cmsg_len = CMSG_LEN(sizeof(struct bintime));
465     cmsg_level = SOL_SOCKET;
466     cmsg_type = SCM_BINTIME;
467.Ed
468.Pp
469Additional timestamp types are available by following
470.Dv SO_TIMESTAMP
471with
472.Dv SO_TS_CLOCK ,
473which requests a specific timestamp format to be returned instead of
474.Dv SCM_TIMESTAMP when
475.Dv SO_TIMESTAMP is enabled.
476These
477.Dv SO_TS_CLOCK
478values are recognized in
479.Fx :
480.Bl -column SO_TS_CLOCK -offset indent
481.It Dv SO_TS_REALTIME_MICRO Ta "realtime (SCM_TIMESTAMP, struct timeval), default"
482.It Dv SO_TS_BINTIME Ta "realtime (SCM_BINTIME, struct bintime)"
483.It Dv SO_TS_REALTIME Ta "realtime (SCM_REALTIME, struct timespec)"
484.It Dv SO_TS_MONOTONIC Ta "monotonic time (SCM_MONOTONIC, struct timespec)"
485.El
486.Pp
487.Dv SO_ACCEPTCONN ,
488.Dv SO_TYPE ,
489.Dv SO_PROTOCOL
490(and its alias
491.Dv SO_PROTOTYPE )
492and
493.Dv SO_ERROR
494are options used only with
495.Fn getsockopt .
496.Dv SO_ACCEPTCONN
497returns whether the socket is currently accepting connections,
498that is, whether or not the
499.Xr listen 2
500system call was invoked on the socket.
501.Dv SO_TYPE
502returns the type of the socket, such as
503.Dv SOCK_STREAM ;
504it is useful for servers that inherit sockets on startup.
505.Dv SO_PROTOCOL
506returns the protocol number for the socket, for
507.Dv AF_INET
508and
509.Dv AF_INET6
510address families.
511.Dv SO_ERROR
512returns any pending error on the socket and clears
513the error status.
514It may be used to check for asynchronous errors on connected
515datagram sockets or for other asynchronous errors.
516.Dv SO_RERROR
517indicates that receive buffer overflows should be handled as errors.
518Historically receive buffer overflows have been ignored and programs
519could not tell if they missed messages or messages had been truncated
520because of overflows.
521Since programs historically do not expect to get receive overflow errors,
522this behavior is not the default.
523.Pp
524.Dv SO_LABEL
525returns the MAC label of the socket.
526.Dv SO_PEERLABEL
527returns the MAC label of the socket's peer.
528Note that your kernel must be compiled with MAC support.
529See
530.Xr mac 3
531for more information.
532.Pp
533.Dv SO_LISTENQLIMIT
534returns the maximal number of queued connections, as set by
535.Xr listen 2 .
536.Dv SO_LISTENQLEN
537returns the number of unaccepted complete connections.
538.Dv SO_LISTENINCQLEN
539returns the number of unaccepted incomplete connections.
540.Pp
541.Dv SO_MAX_PACING_RATE
542instruct the socket and underlying network adapter layers to limit the
543transfer rate to the given unsigned 32-bit value in bytes per second.
544.Pp
545.Dv SO_NO_OFFLOAD
546disables support for protocol offloads.
547At present, this prevents TCP sockets from using TCP offload engines.
548.Dv SO_NO_DDP
549disables support for a specific TCP offload known as direct data
550placement (DDP).
551DDP is an offload supported by Chelsio network adapters that permits
552reassembled TCP data streams to be received via zero-copy in
553user-supplied buffers using
554.Xr aio_read 2 .
555.Pp
556.Dv SO_SPLICE ,
557when passed to
558.Fn setsockopt ,
559splices two sockets together using the following
560.Fa optval :
561.Bd -literal
562struct so_splice {
563	int sp_fd;
564	off_t sp_max;
565	struct timeval sp_idle;
566};
567.Ed
568.Pp
569Data received on
570.Fa s
571will automatically be transmitted from the socket specified in
572.Fa sp_fd
573without any intervention by userspace.
574Splicing is a one-way operation; a given pair of sockets may be
575spliced in one or both directions.
576Currently only connected
577.Xr tcp 4
578sockets may be spliced together.
579If
580.Fa sp_max
581is greater than zero, the socket pair will automatically be unspliced
582once that number of bytes have been transmitted.
583If
584.Fa sp_idle
585is non-zero, the socket pair will automatically be unspliced once the
586specified amount of time has elapsed since the initial call to
587.Fn setsockopt .
588If
589.Fa sp_fd
590is -1, the socket will be unspliced immediately.
591.Pp
592When passed to
593.Fn getsockopt ,
594the
595.Dv SO_SPLICE
596option returns a 64-bit integer containing the number of bytes transmitted by
597the most recent splice.
598That is, while the socket is spliced, the value returned will be the number
599of bytes spliced so far.
600When unsplicing, this value is saved and is returned until the socket is closed
601or spliced again.
602For example, if a splice transmits 100 bytes and is then unspliced, a subsequent
603.Nm getsockopt
604call will return 100 until the socket is spliced again.
605.Sh RETURN VALUES
606.Rv -std
607.Sh ERRORS
608The
609.Fn getsockopt
610and
611.Fn setsockopt
612system calls succeed unless:
613.Bl -tag -width Er
614.It Bq Er EBADF
615The argument
616.Fa s
617is not a valid descriptor.
618.It Bq Er ENOTSOCK
619The argument
620.Fa s
621is a file, not a socket.
622.It Bq Er ENOPROTOOPT
623The option is unknown at the level indicated.
624.It Bq Er EFAULT
625The address pointed to by
626.Fa optval
627is not in a valid part of the process address space.
628For
629.Fn getsockopt ,
630this error may also be returned if
631.Fa optlen
632is not in a valid part of the process address space.
633.It Bq Er EINVAL
634Installing an
635.Xr accept_filter 9
636on a non-listening socket was attempted.
637.It Bq Er ENOMEM
638A memory allocation failed that was required to service the request.
639.El
640.Pp
641The
642.Fn setsockopt
643system call may also return the following error:
644.Bl -tag -width Er
645.It Bq Er ENOBUFS
646Insufficient resources were available in the system
647to perform the operation.
648.El
649.Sh SEE ALSO
650.Xr ioctl 2 ,
651.Xr listen 2 ,
652.Xr recvmsg 2 ,
653.Xr socket 2 ,
654.Xr getprotoent 3 ,
655.Xr mac 3 ,
656.Xr sysctl 3 ,
657.Xr ip 4 ,
658.Xr ip6 4 ,
659.Xr sctp 4 ,
660.Xr tcp 4 ,
661.Xr protocols 5 ,
662.Xr sysctl 8 ,
663.Xr accept_filter 9 ,
664.Xr bintime 9
665.Sh HISTORY
666The
667.Fn getsockopt
668and
669.Fn setsockopt
670system calls appeared in
671.Bx 4.2 .
672The
673.Dv SO_SPLICE
674option originated in
675.Ox 4.9
676and first appeared in
677.Fx 15.0 .
678The
679.Fx
680implementation aims to be source-compatible.
681.Sh BUGS
682Several of the socket options should be handled at lower levels of the system.
683