xref: /netbsd-src/share/man/man4/udp.4 (revision 9ddb6ab554e70fb9bbd90c3d96b812bc57755a14)
1.\"	$NetBSD: udp.4,v 1.12 2011/09/24 18:49:13 christos Exp $
2.\"
3.\" Copyright (c) 1983, 1991, 1993
4.\"	The Regents of the University of California.  All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\"    notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\"    notice, this list of conditions and the following disclaimer in the
13.\"    documentation and/or other materials provided with the distribution.
14.\" 3. Neither the name of the University nor the names of its contributors
15.\"    may be used to endorse or promote products derived from this software
16.\"    without specific prior written permission.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28.\" SUCH DAMAGE.
29.\"
30.\"     @(#)udp.4	8.1 (Berkeley) 6/5/93
31.\"
32.Dd September 24, 2011
33.Dt UDP 4
34.Os
35.Sh NAME
36.Nm udp
37.Nd Internet User Datagram Protocol
38.Sh SYNOPSIS
39.In sys/socket.h
40.In netinet/in.h
41.Ft int
42.Fn socket AF_INET SOCK_DGRAM 0
43.Ft int
44.Fn socket AF_INET6 SOCK_DGRAM 0
45.Sh DESCRIPTION
46.Tn UDP
47is a simple, unreliable datagram protocol which is used
48to support the
49.Dv SOCK_DGRAM
50abstraction for the Internet
51protocol family.
52.Tn UDP
53sockets are connectionless, and are
54normally used with the
55.Xr sendto 2
56and
57.Xr recvfrom 2
58calls, though the
59.Xr connect 2
60call may also be used to fix the destination for future
61packets (in which case the
62.Xr recv 2
63or
64.Xr read 2
65and
66.Xr send 2
67or
68.Xr write 2
69system calls may be used).
70.Pp
71.Tn UDP
72address formats are identical to those used by
73.Tn TCP .
74In particular
75.Tn UDP
76provides a port identifier in addition
77to the normal Internet address format.
78Note that the
79.Tn UDP
80port
81space is separate from the
82.Tn TCP
83port space (i.e. a
84.Tn UDP
85port
86may not be
87.Dq connected
88to a
89.Tn TCP
90port).
91In addition broadcast packets may be sent (assuming the underlying
92network supports this) by using a reserved
93.Dq broadcast address ;
94this address
95is network interface dependent.
96.Pp
97There are two
98.Tn UDP-level
99.Xr setsockopt 2 Ns / Ns Xr getsockopt 2
100options.
101.Dv UDP_OPTIONS
102may be used to change the default behavior of the socket.
103For example:
104.Bd -literal
105setsockopt(s, IPPROTO_UDP, UDP_OPTIONS, NULL, 0);
106.Ed
107.Pp
108The
109.Dv UDP_ENCAP
110option can be used to encapsulate
111.Tn ESP
112packets in
113.Tn UDP .
114There are two valid enapsulation options:
115.Tn UDP_ENCAP_ESPINUDP_NON_IKE
116from draft-ietf-ipsec-nat-t-ike-00/01 and
117.Tn UDP_ENCAP_ESPINUDP
118from draft-ietf-ipsec-udp-encaps-06
119defined in
120.In netinet/udp.h .
121.Pp
122The
123.Dv UDP_RFC6056ALGO
124can be used to randomize the port selection.
125Valid algorithms are described in
126.Xr rfc6056 7
127and their respective constants are in
128.In netinet/rfc6056.h .
129For example,
130.Bd -literal
131int algo = RFC6056_ALGO_RANDOM_PICK;       /* see \*[Lt]netinet/rfc6056.h\*[Gt] */
132setsockopt(s, IPPROTO_UDP, UDP_RFC6056ALGO, \*[Am]algo, sizeof(algo));
133.Ed
134.Pp
135The port selection can be also controlled at a global level for all
136.Tn UDP
137or
138.Tn UDP6
139sockets using the following
140.Xr sysctl 7
141variables:
142.Dv net.inet.udp.rfc6056.selected
143and
144.Dv net.inet6.udp6.rfc6056.selected
145respectively.
146.Pp
147Options at the
148.Tn IP
149transport level may be used with
150.Tn UDP ;
151see
152.Xr ip 4
153or
154.Xr ip6 4 .
155.Sh DIAGNOSTICS
156A socket operation may fail with one of the following errors returned:
157.Bl -tag -width [EADDRNOTAVAIL]
158.It Bq Er EISCONN
159when trying to establish a connection on a socket which
160already has one, or when trying to send a datagram with the destination
161address specified and the socket is already connected;
162.It Bq Er ENOTCONN
163when trying to send a datagram, but
164no destination address is specified, and the socket hasn't been
165connected;
166.It Bq Er ENOBUFS
167when the system runs out of memory for
168an internal data structure;
169.It Bq Er EADDRINUSE
170when an attempt
171is made to create a socket with a port which has already been
172allocated;
173.It Bq Er EADDRNOTAVAIL
174when an attempt is made to create a socket with a network address
175for which no network interface exists.
176.El
177.Sh SEE ALSO
178.Xr getsockopt 2 ,
179.Xr recv 2 ,
180.Xr send 2 ,
181.Xr socket 2 ,
182.Xr inet 4 ,
183.Xr inet6 4 ,
184.Xr intro 4 ,
185.Xr ip 4 ,
186.Xr ip6 4 ,
187.Xr rfc6056 7 ,
188.Xr sysctl 7
189.Rs
190.%R RFC
191.%N 768
192.%D August 28, 1980
193.%T "User Datagram Protocol"
194.Re
195.Rs
196.%R RFC
197.%N 1122
198.%D October 1989
199.%T "Requirements for Internet Hosts -- Communication Layers"
200.Re
201.Sh HISTORY
202The
203.Nm
204protocol appeared in
205.Bx 4.2 .
206