1 .\" This man page is Copyright (C) 1999 Andi Kleen <ak@muc.de>.
3 .\" %%%LICENSE_START(VERBATIM_ONE_PARA)
4 .\" Permission is granted to distribute possibly modified copies
5 .\" of this page provided the header is included verbatim,
6 .\" and in case of nontrivial modification author and date
7 .\" of the modification is added to the header.
10 .\" $Id: udp.7,v 1.7 2000/01/22 01:55:05 freitag Exp $
12 .TH UDP 7 2021-03-22 "Linux" "Linux Programmer's Manual"
14 udp \- User Datagram Protocol for IPv4
17 .B #include <sys/socket.h>
18 .B #include <netinet/in.h>
19 .B #include <netinet/udp.h>
21 .B udp_socket = socket(AF_INET, SOCK_DGRAM, 0);
24 This is an implementation of the User Datagram Protocol
25 described in RFC\ 768.
26 It implements a connectionless, unreliable datagram packet service.
27 Packets may be reordered or duplicated before they arrive.
28 UDP generates and checks checksums to catch transmission errors.
30 When a UDP socket is created,
31 its local and remote addresses are unspecified.
32 Datagrams can be sent immediately using
36 with a valid destination address as an argument.
39 is called on the socket, the default destination address is set and
40 datagrams can now be sent using
44 without specifying a destination address.
45 It is still possible to send to other destinations by passing an
50 In order to receive packets, the socket can be bound to a local
51 address first by using
53 Otherwise, the socket layer will automatically assign
54 a free local port out of the range defined by
55 .I /proc/sys/net/ipv4/ip_local_port_range
56 and bind the socket to
59 All receive operations return only one packet.
60 When the packet is smaller than the passed buffer, only that much
61 data is returned; when it is bigger, the packet is truncated and the
67 IP options may be sent or received using the socket options described in
69 They are processed by the kernel only when the appropriate
72 is enabled (but still passed to the user even when it is turned off).
78 flag is set on sending, the destination address must refer to a local
79 interface address and the packet is sent only to that interface.
81 By default, Linux UDP does path MTU (Maximum Transmission Unit) discovery.
83 will keep track of the MTU to a specific target IP address and return
85 when a UDP packet write exceeds it.
86 When this happens, the application should decrease the packet size.
87 Path MTU discovery can be also turned off using the
90 .I /proc/sys/net/ipv4/ip_no_pmtu_disc
94 When turned off, UDP will fragment outgoing UDP packets
95 that exceed the interface MTU.
96 However, disabling it is not recommended
97 for performance and reliability reasons.
101 address format described in
104 All fatal errors will be passed to the user as an error return even
105 when the socket is not connected.
106 This includes asynchronous errors
107 received from the network.
108 You may get an error for an earlier packet
109 that was sent on the same socket.
110 This behavior differs from many other BSD socket implementations
111 which don't pass any errors unless the socket is connected.
112 Linux's behavior is mandated by
115 For compatibility with legacy code, in Linux 2.0 and 2.2
116 it was possible to set the
119 option to receive remote errors only when the socket has been
120 connected (except for
124 Locally generated errors are always passed.
125 Support for this socket option was removed in later kernels; see
127 for further information.
131 option is enabled, all errors are stored in the socket error queue,
132 and can be received by
138 System-wide UDP parameter settings can be accessed by files in the directory
139 .IR /proc/sys/net/ipv4/ .
141 .IR udp_mem " (since Linux 2.6.25)"
142 This is a vector of three integers governing the number
143 of pages allowed for queueing by all UDP sockets.
147 Below this number of pages, UDP is not bothered about its
149 When the amount of memory allocated by UDP exceeds
150 this number, UDP starts to moderate memory usage.
153 This value was introduced to follow the format of
159 Number of pages allowed for queueing by all UDP sockets.
162 Defaults values for these three items are
163 calculated at boot time from the amount of available memory.
165 .IR udp_rmem_min " (integer; default value: PAGE_SIZE; since Linux 2.6.25)"
166 Minimal size, in bytes, of receive buffers used by UDP sockets in moderation.
167 Each UDP socket is able to use the size for receiving data,
168 even if total pages of UDP sockets exceed
172 .IR udp_wmem_min " (integer; default value: PAGE_SIZE; since Linux 2.6.25)"
173 Minimal size, in bytes, of send buffer used by UDP sockets in moderation.
174 Each UDP socket is able to use the size for sending data,
175 even if total pages of UDP sockets exceed
179 To set or get a UDP socket option, call
183 to write the option with the option level argument set to
185 Unless otherwise noted,
190 Following is a list of UDP-specific socket options.
191 For details of some other socket options that are also applicable
195 .BR UDP_CORK " (since Linux 2.5.44)"
196 If this option is enabled, then all data output on this socket
197 is accumulated into a single datagram that is transmitted when
198 the option is disabled.
199 This option should not be used in code intended to be
201 .\" FIXME document UDP_ENCAP (new in kernel 2.5.67)
202 .\" From include/linux/udp.h:
203 .\" UDP_ENCAP_ESPINUDP_NON_IKE draft-ietf-ipsec-nat-t-ike-00/01
204 .\" UDP_ENCAP_ESPINUDP draft-ietf-ipsec-udp-encaps-06
205 .\" UDP_ENCAP_L2TPINUDP rfc2661
206 .\" FIXME Document UDP_NO_CHECK6_TX and UDP_NO_CHECK6_RX, added in Linux 3.16
208 These ioctls can be accessed using
210 The correct syntax is:
215 .IB error " = ioctl(" udp_socket ", " ioctl_type ", &" value ");"
219 .BR FIONREAD " (" SIOCINQ )
220 Gets a pointer to an integer as argument.
221 Returns the size of the next pending datagram in the integer in bytes,
222 or 0 when no datagram is pending.
226 it is impossible to distinguish the case where no datagram is pending
227 from the case where the next pending datagram contains zero bytes of data.
233 to distinguish these cases.
234 .\" See http://www.securiteam.com/unixfocus/5KP0I15IKO.html
235 .\" "GNUnet DoS (UDP Socket Unreachable)", 14 May 2006
237 .BR TIOCOUTQ " (" SIOCOUTQ )
238 Returns the number of data bytes in the local send queue.
239 Supported only with Linux 2.4 and above.
241 In addition, all ioctls documented in
247 All errors documented for
251 may be returned by a send or receive on a UDP socket.
254 No receiver was associated with the destination address.
255 This might be caused by a previous packet sent over the socket.
258 is a new feature in Linux 2.2.
260 .\" This man page was written by Andi Kleen.
267 The kernel source file
268 .IR Documentation/networking/ip\-sysctl.txt .
270 RFC\ 768 for the User Datagram Protocol.
272 RFC\ 1122 for the host requirements.
274 RFC\ 1191 for a description of path MTU discovery.