udp(7)

๐Ÿ’Œ udp โ€” the manual page, but make it bestie

okay so. hi. you're here because you met udp(7), which is the manual page for the User Datagram Protocol for IPv4 โ€” literally the chillest protocol in the family. no handshakes, no drama, no "did you get my message ๐Ÿฅบ". it just sends. let's get into it ๐Ÿ’–


โœจ NAME โœจ

udp โ€” User Datagram Protocol for IPv4. that's it, that's the whole vibe ๐Ÿฆ‹


๐ŸŽ€ SYNOPSIS ๐ŸŽ€

like, first you gotta invite the right headers to the party:

#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/udp.h>

udp_socket = socket(AF_INET, SOCK_DGRAM, 0);

AF_INET + SOCK_DGRAM = your lil udp socket, born ready โœจ


๐ŸŒธ DESCRIPTION ๐ŸŒธ

so this is an implementation of the User Datagram Protocol described in RFC 768. it implements a connectionless, unreliable datagram packet service. and yes bestie, it says unreliable in the official docs, that's not me being shady ๐Ÿ’€ packets may be reordered or duplicated before they arrive. it's giving "sent from my iPhone" energy. that said, UDP does generate and check checksums to catch transmission errors, so it's not a total chaos gremlin ๐Ÿซถ

when a UDP socket is created, its local and remote addresses are unspecified. you can send datagrams immediately using sendto(2) or sendmsg(2) with a valid destination address as an argument. then when you call connect(2) on the socket, the default destination address is set, and now you can send with send(2) or write(2) without specifying a destination address. it is still possible to send to other destinations by passing an address to sendto(2) or sendmsg(2) โ€” no gatekeeping ๐Ÿ’…

to receive packets, the socket can be bound to a local address first by using bind(2). otherwise, the socket layer will automatically assign a free local port out of the range defined by /proc/sys/net/ipv4/ip_local_port_range and bind the socket to INADDR_ANY. it just handles it. main character energy honestly ๐Ÿ‘‘

all receive operations return only one packet. one. singular. when the packet is smaller than the passed buffer, only that much data is returned; when it is bigger, the packet is truncated and the MSG_TRUNC flag is set. and MSG_WAITALL is not supported. rip. it said "no ๐Ÿ’€"

IP options may be sent or received using the socket options described in ip(7). they are processed by the kernel only when the appropriate /proc parameter is enabled โ€” but they're still passed to the user even when it is turned off. see ip(7).

when the MSG_DONTROUTE flag is set on sending, the destination address must refer to a local interface address and the packet is sent only to that interface. stay local, babe.

by default, Linux UDP does path MTU (Maximum Transmission Unit) discovery. this means the kernel will keep track of the MTU to a specific target IP address and return EMSGSIZE when a UDP packet write exceeds it. when this happens, the application should decrease the packet size ๐Ÿ“

path MTU discovery can also be turned off using the IP_MTU_DISCOVER socket option or the /proc/sys/net/ipv4/ip_no_pmtu_disc file; see ip(7) for details. when turned off, UDP will fragment outgoing UDP packets that exceed the interface MTU. however, disabling it is not recommended for performance and reliability reasons. so like... don't ๐Ÿ™ˆ

๐Ÿฆ‹ Address format ๐Ÿฆ‹

UDP uses the IPv4 sockaddr_in address format described in ip(7). it's the classic. iconic. never goes out of style โœจ

๐Ÿ˜ญ Error handling ๐Ÿ˜ญ

all fatal errors will be passed to the user as an error return even when the socket is not connected. this includes asynchronous errors received from the network. you may get an error for an earlier packet that was sent on the same socket. yes, the error from three messages ago, showing up now, uninvited. this behavior differs from many other BSD socket implementations which don't pass any errors unless the socket is connected. Linux's behavior is mandated by RFC 1122, so it's not a bug, it's the law ๐Ÿ“œ

for compatibility with legacy code, in Linux 2.0 and 2.2 it was possible to set the SO_BSDCOMPAT SOL_SOCKET option to receive remote errors only when the socket has been connected (except for EPROTO and EMSGSIZE). locally generated errors are always passed. support for this socket option was removed in later kernels; see socket(7) for further information. it's gone, babe, it's in the past ๐Ÿชฆ

when the IP_RECVERR option is enabled, all errors are stored in the socket error queue, and can be received by recvmsg(2) with the MSG_ERRQUEUE flag set. errors but make it a queue ๐Ÿงš

๐Ÿ“ /proc interfaces ๐Ÿ“

system-wide UDP parameter settings can be accessed by files in the directory /proc/sys/net/ipv4/.

  • udp_mem (since Linux 2.6.25) โ€” this is a vector of three integers governing the number of pages allowed for queueing by all UDP sockets.
    • min โ€” below this number of pages, UDP is not bothered about its memory appetite ๐Ÿ™ˆ when the amount of memory allocated by UDP exceeds this number, UDP starts to moderate memory usage.
    • pressure โ€” this value was introduced to follow the format of tcp_mem (see tcp(7)).
    • max โ€” number of pages allowed for queueing by all UDP sockets.
    • default values for these three items are calculated at boot time from the amount of available memory.
  • udp_rmem_min (integer; default value: PAGE_SIZE; since Linux 2.6.25) โ€” minimal size, in bytes, of receive buffers used by UDP sockets in moderation. each UDP socket is able to use the size for receiving data, even if total pages of UDP sockets exceed udp_mem pressure. little guaranteed snack ๐Ÿช
  • udp_wmem_min (integer; default value: PAGE_SIZE; since Linux 2.6.25) โ€” minimal size, in bytes, of send buffer used by UDP sockets in moderation. each UDP socket is able to use the size for sending data, even if total pages of UDP sockets exceed udp_mem pressure.

๐Ÿ’… Socket options ๐Ÿ’…

to set or get a UDP socket option, call getsockopt(2) to read or setsockopt(2) to write the option with the option level argument set to IPPROTO_UDP. unless otherwise noted, optval is a pointer to an int.

following is a list of UDP-specific socket options. for details of some other socket options that are also applicable for UDP sockets, see socket(7).

  • UDP_CORK (since Linux 2.5.44) โ€” if this option is enabled, then all data output on this socket is accumulated into a single datagram that is transmitted when the option is disabled. ๐Ÿซ™ this option should not be used in code intended to be portable.
  • UDP_SEGMENT (since Linux 4.18) โ€” enables UDP segmentation offload. segmentation offload reduces send(2) cost by transferring multiple datagrams worth of data as a single large packet through the kernel transmit path, even when that exceeds MTU. as late as possible, the large packet is split by segment size into a series of datagrams. this segmentation offload step is deferred to hardware if supported, else performed in software โœจ this option takes a value in the range [0, USHRT_MAX] that sets the segment size: the size of datagram payload, excluding the UDP header. the segment size must be chosen such that at most 64 datagrams are sent in a single call and that the datagrams after segmentation meet the same MTU rules that apply to datagrams sent without this option. segmentation offload depends on checksum offload, as datagram checksums are computed after segmentation. the option may also be set for individual sendmsg(2) calls by passing it as a cmsg(3). a value of zero disables the feature. this option should not be used in code intended to be portable.
  • UDP_GRO (since Linux 5.0) โ€” enables UDP receive offload. if enabled, the socket may receive multiple datagrams worth of data as a single large buffer, together with a cmsg(3) that holds the segment size. this option is the inverse of segmentation offload. it reduces receive cost by handling multiple datagrams worth of data as a single large packet in the kernel receive path, even when that exceeds MTU. this option should not be used in code intended to be portable. ๐Ÿ”ฅ

๐ŸŒˆ Ioctls ๐ŸŒˆ

these ioctls can be accessed using ioctl(2). the correct syntax is:

int value;
error = ioctl(udp_socket, ioctl_type, &value);
  • FIONREAD (SIOCINQ) โ€” gets a pointer to an integer as argument. returns the size of the next pending datagram in the integer in bytes, or 0 when no datagram is pending. โš ๏ธ warning: using FIONREAD, it is impossible to distinguish the case where no datagram is pending from the case where the next pending datagram contains zero bytes of data. it is safer to use select(2), poll(2), or epoll(7) to distinguish these cases. like, please, use the good ones ๐Ÿ™
  • TIOCOUTQ (SIOCOUTQ) โ€” returns the number of data bytes in the local send queue. supported only with Linux 2.4 and above.

in addition, all ioctls documented in ip(7) and socket(7) are supported ๐ŸŽ€


๐Ÿ”ฅ ERRORS ๐Ÿ”ฅ

all errors documented for socket(7) or ip(7) may be returned by a send or receive on a UDP socket.

  • ECONNREFUSED โ€” no receiver was associated with the destination address. this might be caused by a previous packet sent over the socket. ๐Ÿ‘ป the ghost of packets past, fr fr

โญ VERSIONS โญ

IP_RECVERR is a new feature in Linux 2.2. vintage but still slaying ๐Ÿ’œ


๐Ÿ‘‘ SEE ALSO ๐Ÿ‘‘


okay that's the whole page bestie, you ate it ๐Ÿ’– this lil masterpiece came from Linux man-pages 6.20, dated 2026-02-08 โ€” go forth and send datagrams responsibly, mwah ๐ŸŽ€โœจ๐Ÿฆ‹

๐Ÿ“ฆ where this page comes from
  • arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • ๐Ÿ“œ the page's license: Linux-man-pages-1-para
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms