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 January 20, 2025 29.Dt UDP 4 30.Os 31.Sh NAME 32.Nm udp 33.Nd Internet User Datagram Protocol 34.Sh SYNOPSIS 35.In sys/types.h 36.In sys/socket.h 37.In netinet/in.h 38.Ft int 39.Fn socket AF_INET SOCK_DGRAM 0 40.Sh DESCRIPTION 41.Tn UDP 42is a simple, unreliable datagram protocol which is used 43to support the 44.Dv SOCK_DGRAM 45abstraction for the Internet 46protocol family. 47.Tn UDP 48sockets are connectionless, and are 49normally used with the 50.Xr sendto 2 51and 52.Xr recvfrom 2 53calls, though the 54.Xr connect 2 55call may also be used to fix the destination for future 56packets (in which case the 57.Xr recv 2 58or 59.Xr read 2 60and 61.Xr send 2 62or 63.Xr write 2 64system calls may be used). 65.Pp 66.Tn UDP 67address formats are identical to those used by 68.Tn TCP . 69In particular 70.Tn UDP 71provides a port identifier in addition 72to the normal Internet address format. 73Note that the 74.Tn UDP 75port 76space is separate from the 77.Tn TCP 78port space (i.e., a 79.Tn UDP 80port 81may not be 82.Dq connected 83to a 84.Tn TCP 85port). 86In addition broadcast 87packets may be sent (assuming the underlying network supports 88this) by using a reserved 89.Dq broadcast address ; 90this address 91is network interface dependent. 92.Pp 93Options at the 94.Tn IP 95transport level may be used with 96.Tn UDP ; 97see 98.Xr ip 4 . 99.Tn UDP_ENCAP 100socket option may be used at the 101.Tn IPPROTO_UDP 102level to encapsulate 103.Tn ESP 104packets in 105.Tn UDP . 106Only one value is supported for this option: 107.Tn UDP_ENCAP_ESPINUDP 108from RFC 3948, defined in 109.In netinet/udp.h . 110.Sh FIB support 111UDP sockets are FIB-aware. 112They inherit the FIB of the process which created the socket. 113By default, a UDP socket bound to an address can receive datagrams originating 114from any FIB. 115If the 116.Va net.inet.udp.bind_all_fibs 117tunable is set to 0, all UDP sockets will receive only datagrams originating 118from the same FIB as the socket. 119In this mode, multiple sockets can be bound to the same address, so long as 120each socket belongs to a different FIB, similar to the behavior of the 121.Dv SO_REUSEPORT 122option. 123.Sh MIB (sysctl) Variables 124The 125.Nm 126protocol implements a number of variables in the 127.Va net.inet.udp 128branch of the 129.Xr sysctl 3 130MIB, which can be also read or modified with 131.Xr sysctl 8 : 132.Bl -tag -width ".Va log_in_vain" 133.It Va blackhole 134When a datagram is received on a port where there is no socket 135listening, do not return an ICMP port unreachable message. 136(Disabled by default. 137See 138.Xr blackhole 4 . ) 139.It Va checksum 140Enable UDP checksums (enabled by default). 141.It Va log_in_vain 142For all UDP datagrams, to ports on which there is no socket 143listening, log the connection attempt (disabled by default). 144.It Va maxdgram 145Maximum outgoing UDP datagram size 146.It Va recvspace 147Maximum space for incoming UDP datagrams 148.El 149.Sh ERRORS 150A socket operation may fail with one of the following errors returned: 151.Bl -tag -width Er 152.It Bq Er EISCONN 153when trying to establish a connection on a socket which 154already has one, or when trying to send a datagram with the destination 155address specified and the socket is already connected; 156.It Bq Er ENOTCONN 157when trying to send a datagram, but 158no destination address is specified, and the socket has not been 159connected; 160.It Bq Er ENOBUFS 161when the system runs out of memory for 162an internal data structure; 163.It Bq Er EADDRINUSE 164when an attempt 165is made to create a socket with a port which has already been 166allocated; 167.It Bq Er EADDRNOTAVAIL 168when an attempt is made to create a 169socket with a network address for which no network interface 170exists. 171.El 172.Sh SEE ALSO 173.Xr getsockopt 2 , 174.Xr recv 2 , 175.Xr send 2 , 176.Xr socket 2 , 177.Xr blackhole 4 , 178.Xr inet 4 , 179.Xr intro 4 , 180.Xr ip 4 , 181.Xr udplite 4 182.Sh HISTORY 183The 184.Nm 185protocol appeared in 186.Bx 4.2 . 187