OSDN Git Service

(split) LDP: Update original to LDP v3.51.
[linuxjm/LDP_man-pages.git] / original / man7 / udp.7
1 .\" This man page is Copyright (C) 1999 Andi Kleen <ak@muc.de>.
2 .\"
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.
8 .\" %%%LICENSE_END
9 .\"
10 .\" $Id: udp.7,v 1.7 2000/01/22 01:55:05 freitag Exp $
11 .\"
12 .TH UDP  7 2010-06-13 "Linux" "Linux Programmer's Manual"
13 .SH NAME
14 udp \- User Datagram Protocol for IPv4
15 .SH SYNOPSIS
16 .B #include <sys/socket.h>
17 .br
18 .B #include <netinet/in.h>
19 .sp
20 .B udp_socket = socket(AF_INET, SOCK_DGRAM, 0);
21 .SH DESCRIPTION
22 This is an implementation of the User Datagram Protocol
23 described in RFC\ 768.
24 It implements a connectionless, unreliable datagram packet service.
25 Packets may be reordered or duplicated before they arrive.
26 UDP generates and checks checksums to catch transmission errors.
27
28 When a UDP socket is created,
29 its local and remote addresses are unspecified.
30 Datagrams can be sent immediately using
31 .BR sendto (2)
32 or
33 .BR sendmsg (2)
34 with a valid destination address as an argument.
35 When
36 .BR connect (2)
37 is called on the socket, the default destination address is set and
38 datagrams can now be sent using
39 .BR send (2)
40 or
41 .BR write (2)
42 without specifying a destination address.
43 It is still possible to send to other destinations by passing an
44 address to
45 .BR sendto (2)
46 or
47 .BR sendmsg (2).
48 In order to receive packets, the socket can be bound to a local
49 address first by using
50 .BR bind (2).
51 Otherwise the socket layer will automatically assign
52 a free local port out of the range defined by
53 .I /proc/sys/net/ipv4/ip_local_port_range
54 and bind the socket to
55 .BR INADDR_ANY .
56
57 All receive operations return only one packet.
58 When the packet is smaller than the passed buffer, only that much
59 data is returned; when it is bigger, the packet is truncated and the
60 .B MSG_TRUNC
61 flag is set.
62 .B MSG_WAITALL
63 is not supported.
64
65 IP options may be sent or received using the socket options described in
66 .BR ip (7).
67 They are processed by the kernel only when the appropriate
68 .I /proc
69 parameter
70 is enabled (but still passed to the user even when it is turned off).
71 See
72 .BR ip (7).
73
74 When the
75 .B MSG_DONTROUTE
76 flag is set on sending, the destination address must refer to a local
77 interface address and the packet is sent only to that interface.
78
79 By default, Linux UDP does path MTU (Maximum Transmission Unit) discovery.
80 This means the kernel
81 will keep track of the MTU to a specific target IP address and return
82 .B EMSGSIZE
83 when a UDP packet write exceeds it.
84 When this happens, the application should decrease the packet size.
85 Path MTU discovery can be also turned off using the
86 .B IP_MTU_DISCOVER
87 socket option or the
88 .I /proc/sys/net/ipv4/ip_no_pmtu_disc
89 file; see
90 .BR ip (7)
91 for details.
92 When turned off, UDP will fragment outgoing UDP packets
93 that exceed the interface MTU.
94 However, disabling it is not recommended
95 for performance and reliability reasons.
96 .SS Address format
97 UDP uses the IPv4
98 .I sockaddr_in
99 address format described in
100 .BR ip (7).
101 .SS Error handling
102 All fatal errors will be passed to the user as an error return even
103 when the socket is not connected.
104 This includes asynchronous errors
105 received from the network.
106 You may get an error for an earlier packet
107 that was sent on the same socket.
108 This behavior differs from many other BSD socket implementations
109 which don't pass any errors unless the socket is connected.
110 Linux's behavior is mandated by
111 .BR RFC\ 1122 .
112
113 For compatibility with legacy code, in Linux 2.0 and 2.2
114 it was possible to set the
115 .B SO_BSDCOMPAT
116 .B SOL_SOCKET
117 option to receive remote errors only when the socket has been
118 connected (except for
119 .B EPROTO
120 and
121 .BR EMSGSIZE ).
122 Locally generated errors are always passed.
123 Support for this socket option was removed in later kernels; see
124 .BR socket (7)
125 for further information.
126
127 When the
128 .B IP_RECVERR
129 option is enabled, all errors are stored in the socket error queue,
130 and can be received by
131 .BR recvmsg (2)
132 with the
133 .B MSG_ERRQUEUE
134 flag set.
135 .SS /proc interfaces
136 System-wide UDP parameter settings can be accessed by files in the directory
137 .IR /proc/sys/net/ipv4/ .
138 .TP
139 .IR udp_mem " (since Linux 2.6.25)"
140 This is a vector of three integers governing the number
141 of pages allowed for queueing by all UDP sockets.
142 .RS
143 .TP 10
144 .I min
145 Below this number of pages, UDP is not bothered about its
146 memory appetite.
147 When the amount of memory allocated by UDP exceeds
148 this number, UDP starts to moderate memory usage.
149 .TP
150 .I pressure
151 This value was introduced to follow the format of
152 .IR tcp_mem
153 (see
154 .BR tcp (7)).
155 .TP
156 .I max
157 Number of pages allowed for queueing by all UDP sockets.
158 .RE
159 .IP
160 Defaults values for these three items are
161 calculated at boot time from the amount of available memory.
162 .TP
163 .IR udp_rmem_min " (integer; default value: PAGE_SIZE; since Linux 2.6.25)"
164 Minimal size, in bytes, of receive buffers used by UDP sockets in moderation.
165 Each UDP socket is able to use the size for receiving data,
166 even if total pages of UDP sockets exceed
167 .I udp_mem
168 pressure.
169 .TP
170 .IR udp_wmem_min " (integer; default value: PAGE_SIZE; since Linux 2.6.25)"
171 Minimal size, in bytes, of send buffer used by UDP sockets in moderation.
172 Each UDP socket is able to use the size for sending data,
173 even if total pages of UDP sockets exceed
174 .I udp_mem
175 pressure.
176 .SS Socket options
177 To set or get a UDP socket option, call
178 .BR getsockopt (2)
179 to read or
180 .BR setsockopt (2)
181 to write the option with the option level argument set to
182 .BR IPPROTO_UDP .
183 .TP
184 .BR UDP_CORK " (since Linux 2.5.44)"
185 If this option is enabled, then all data output on this socket
186 is accumulated into a single datagram that is transmitted when
187 the option is disabled.
188 This option should not be used in code intended to be
189 portable.
190 .\" FIXME document UDP_ENCAP (new in kernel 2.5.67)
191 .\" From include/linux/udp.h:
192 .\" /* UDP encapsulation types */
193 .\" #define UDP_ENCAP_ESPINUDP_NON_IKE      1 /* draft-ietf-ipsec-nat-t-ike-00/01 */
194 .\" #define UDP_ENCAP_ESPINUDP      2 /* draft-ietf-ipsec-udp-encaps-06 */
195 .\" #define UDP_ENCAP_L2TPINUDP     3 /* rfc2661 */
196 .SS Ioctls
197 These ioctls can be accessed using
198 .BR ioctl (2).
199 The correct syntax is:
200 .PP
201 .RS
202 .nf
203 .BI int " value";
204 .IB error " = ioctl(" udp_socket ", " ioctl_type ", &" value ");"
205 .fi
206 .RE
207 .TP
208 .BR FIONREAD " (" SIOCINQ )
209 Gets a pointer to an integer as argument.
210 Returns the size of the next pending datagram in the integer in bytes,
211 or 0 when no datagram is pending.
212 .B Warning:
213 Using
214 .BR FIONREAD ,
215 it is impossible to distinguish the case where no datagram is pending
216 from the case where the next pending datagram contains zero bytes of data.
217 It is safer to use
218 .BR select (2),
219 .BR poll (2),
220 or
221 .BR epoll (7)
222 to distinguish these cases.
223 .\" See http://www.securiteam.com/unixfocus/5KP0I15IKO.html
224 .\" "GNUnet DoS (UDP Socket Unreachable)", 14 May 2006
225 .TP
226 .BR TIOCOUTQ " (" SIOCOUTQ )
227 Returns the number of data bytes in the local send queue.
228 Only supported with Linux 2.4 and above.
229 .PP
230 In addition all ioctls documented in
231 .BR ip (7)
232 and
233 .BR socket (7)
234 are supported.
235 .SH ERRORS
236 All errors documented for
237 .BR socket (7)
238 or
239 .BR ip (7)
240 may be returned by a send or receive on a UDP socket.
241 .TP
242 .B ECONNREFUSED
243 No receiver was associated with the destination address.
244 This might be caused by a previous packet sent over the socket.
245 .SH VERSIONS
246 .B IP_RECVERR
247 is a new feature in Linux 2.2.
248 .\" .SH CREDITS
249 .\" This man page was written by Andi Kleen.
250 .SH SEE ALSO
251 .BR ip (7),
252 .BR raw (7),
253 .BR socket (7),
254 .BR udplite (7)
255
256 RFC\ 768 for the User Datagram Protocol.
257 .br
258 RFC\ 1122 for the host requirements.
259 .br
260 RFC\ 1191 for a description of path MTU discovery.