📦 Tutorial 3: User Datagram Protocol (UDP) – Advanced
Comprehensive university‑level coverage – COMP347 (TrustOpen
University)
🎯 Learning Objectives
After completing this tutorial, you should be able to:
- Describe the UDP segment format and each header field in detail, including the
pseudo‑header and checksum computation.
- Explain the purpose and operation of the UDP checksum, including the use of 1’s
complement arithmetic and the role of the pseudo‑header.
- Analyze the advantages and limitations of UDP compared to TCP, quantifying overhead and
performance trade‑offs.
- Identify common applications that use UDP and justify the protocol choice based on
application requirements.
- Evaluate reliability mechanisms that can be built atop UDP (e.g., sequence numbers,
ACKs, timeouts).
- Assess the security implications of UDP, including spoofing and amplification attacks,
and discuss mitigation strategies.
🔍 Overview
UDP (User Datagram Protocol) is the simplest transport‑layer protocol in the TCP/IP suite. It provides a
minimal, connectionless, unreliable datagram service, offering only multiplexing/demultiplexing and optional
error detection. Despite its simplicity – or perhaps because of it – UDP is the foundation for many critical
Internet applications, from DNS and DHCP to real‑time media and modern protocols like QUIC. This tutorial
provides a deep dive into UDP’s segment structure, checksum algorithm, performance characteristics, and the
various ways it is used and extended in practice.
📘 1. UDP in the Transport Layer Ecosystem
UDP is often described as a “thin” layer over IP. It adds two key functions:
- Multiplexing/Demultiplexing via port numbers.
- Error detection via an optional checksum (mandatory in IPv6).
Unlike TCP, UDP does not provide reliability, ordering, flow control, or congestion control. This minimalism
allows UDP to achieve low latency and low overhead, making it ideal for applications that can tolerate
occasional data loss or that implement their own reliability at the application layer.
The protocol is defined in RFC 768 (1980). Over
the decades, it has remained essentially unchanged, a testament to its simplicity and utility.
📘 2. UDP Segment Structure and Fields
A UDP segment consists of an 8‑byte header followed by the payload. The header is composed of four 16‑bit fields:
- Source Port (16 bits): Port number of the sending process. May be set to 0 if not used
(e.g., when no reply is expected).
- Destination Port (16 bits): Port number of the receiving process. Required for
demultiplexing.
- Length (16 bits): Total length of the UDP segment (header + data) in bytes. Minimum value
is 8 (header only). The length field allows the receiver to know where the UDP data ends, even if the IP
datagram contains padding.
- Checksum (16 bits): Error‑detection field computed over a pseudo‑header, the UDP header,
and the data. In IPv4, it is optional (can be 0); in IPv6, it is mandatory.
The pseudo‑header is not transmitted but is used only for checksum calculation. It includes the
source IP address, destination IP address, the protocol number (17 for UDP), and the UDP length. This ensures
that the checksum also protects against misrouting.
Pseudo‑header structure (IPv4):
0 7 8 15 16 23 24 31
+--------+--------+--------+--------+
| source address |
+--------+--------+--------+--------+
| destination address |
+--------+--------+--------+--------+
| zero |protocol| UDP length |
+--------+--------+--------+--------+
📘 3. The UDP Checksum: In‑Depth Analysis
The UDP checksum provides error detection for the entire segment. The sender performs the following steps:
- Construct the pseudo‑header from the IP addresses, protocol (17), and UDP length.
- Concatenate the pseudo‑header, the UDP header (with checksum field initially set to 0), and the data. Pad
with a zero byte at the end if the length is odd (to make the total length even).
- Compute the 16‑bit one’s complement sum of all 16‑bit words. This is done by summing the words in binary,
and if a carry occurs beyond the 16th bit, adding the carry back to the sum (end‑around carry).
- Take the one’s complement (bitwise NOT) of the result and store it in the checksum field.
On the receiver side, the same computation is performed over the received segment (including the checksum field).
If the result is all 1s (0xFFFF), the segment is considered error‑free. A result of 0xFFFF indicates that the
checksum passed; a different value indicates an error.
Why one’s complement? It is endian‑independent and simplifies the receiver’s verification – the
complement of the sum yields the same result regardless of byte order.
Checksum offloading: Modern NICs can compute the UDP checksum in hardware, reducing CPU
overhead. The OS may set the checksum to 0 and let the NIC fill it.
📘 4. Advantages and Limitations of UDP
4.1 Advantages
- No connection establishment: Zero RTT delay before sending data.
- No connection state: Servers do not need to maintain per‑client state, reducing memory and
processing overhead.
- Small header: Only 8 bytes, compared to TCP’s 20+ bytes.
- No congestion control: Applications can send at any rate, which is essential for real‑time
media that cannot adapt to TCP’s rate changes.
- Message‑oriented: Preserves application‑layer message boundaries, unlike TCP’s byte‑stream.
- Supports multicast and broadcast: UDP is the natural choice for one‑to‑many communication.
4.2 Limitations
- No reliability: Packets may be lost, duplicated, or reordered.
- No flow control: Sender may overwhelm the receiver.
- No congestion control: Can cause network congestion and unfairness.
- Limited error detection: Checksum is not cryptographically strong; it detects only bit
errors, not sophisticated corruption.
📘 5. Typical UDP Applications
| Application |
Port |
Why UDP? |
| DNS (Domain Name System) |
53 |
Quick queries; loss can be handled by retransmission; low overhead. |
| DHCP (Dynamic Host Configuration) |
67/68 |
Uses broadcast; no need for connection. |
| SNMP (Simple Network Management) |
161 |
Polling and trap messages; occasional loss is acceptable. |
| RTP (Real‑time Transport Protocol) |
varies |
Streaming audio/video; low latency and jitter are critical. |
| VoIP (e.g., SIP, RTP) |
varies |
Real‑time; loss can be concealed. |
| Online gaming |
varies |
Latency sensitivity; state updates can tolerate loss. |
| QUIC (HTTP/3) |
443 (UDP) |
Built on UDP to avoid TCP head‑of‑line blocking; implements own reliability. |
| TFTP (Trivial FTP) |
69 |
Simple file transfer with built‑in stop‑and‑wait reliability over UDP. |
📘 6. Building Reliability on Top of UDP
Applications that need reliability but choose UDP for performance reasons can implement their own reliability
mechanisms. Common approaches include:
- Sequence numbers: To detect duplicates and reorder.
- Acknowledgments (ACKs): Receiver sends ACK for each packet (or cumulative).
- Timeouts and retransmissions: Sender retransmits if ACK not received.
- Forward Error Correction (FEC): Send redundant data to reconstruct lost packets without
retransmission (e.g., in video streaming).
- Congestion control: Applications can implement their own (e.g., Google’s BBR in QUIC).
Examples: TFTP uses a simple stop‑and‑wait protocol over UDP. QUIC implements a
full reliable, multiplexed transport with congestion control on top of UDP.
📘 7. Performance and Latency Considerations
UDP’s low overhead translates to measurable performance benefits:
- Lower latency: No handshake, no delayed ACKs, no retransmission delays (unless application
implements them).
- Higher throughput: Smaller header means more data per packet.
- Reduced CPU usage: Less processing (no sequence numbers, timers, congestion control).
However, without congestion control, UDP flows can dominate network resources and cause unfairness. In practice,
many networks employ policing or shaping to limit UDP rates.
Latency comparison: For a small request‑response transaction, UDP can be one RTT faster than TCP
because it skips the three‑way handshake. For large data transfers, TCP may achieve higher throughput due to its
congestion control and sliding window, but at the cost of increased latency under loss.
📘 8. UDP Variants: UDP‑Lite and Others
UDP‑Lite (RFC 3828) is a lightweight variant that allows the checksum to cover only a portion of
the payload, leaving the rest unprotected. This is useful for applications that can tolerate corruption in some
parts (e.g., video codecs where corrupted pixels are acceptable). The checksum coverage length is specified in
the header.
Other extensions include UDP with congestion control (e.g., DCCP – Datagram Congestion Control
Protocol), which adds congestion control to UDP‑like datagrams.
📘 9. Security Implications: Amplification Attacks
UDP’s connectionless nature and lack of handshake make it attractive for amplification attacks,
where an attacker spoofs the source IP to a victim and sends a small request to a UDP service that replies with
a much larger response (e.g., DNS, NTP, Memcached). The victim receives amplified traffic.
Mitigations include:
- Ingress filtering (BCP 38) to prevent spoofed packets from leaving a network.
- Rate limiting on UDP services.
- Response size limitation.
- Source IP verification (e.g., using cookies).
UDP’s checksum does not provide authentication; it only detects errors, not spoofing.
📝 Quiz
Test your understanding with these 25 questions. Answers are hidden below each question.
- What is the size of the UDP header in bytes?
Answer
8 bytes.
- Which fields are present in the UDP header?
Answer
Source Port, Destination Port, Length, Checksum.
- What is the purpose of the UDP pseudo‑header?
Answer
To include IP addresses, protocol number, and UDP length in the checksum
calculation, protecting against misrouting.
- Is the UDP checksum mandatory in IPv4?
Answer
No, it is optional; a value of 0 indicates no checksum. In IPv6 it is
mandatory.
- What does the UDP Length field indicate?
Answer
The total length of the UDP segment (header + data) in bytes. Minimum 8.
- Name three applications that typically use UDP.
Answer
DNS, DHCP, VoIP (RTP).
- Does UDP provide flow control?
Answer
No.
- Does UDP provide congestion control?
Answer
No.
- What is the range of UDP port numbers?
Answer
0–65535.
- Why might a video streaming application prefer UDP over TCP?
Answer
To avoid retransmission delays and head‑of‑line blocking; low latency is
more important than perfect reliability.
- What is a UDP amplification attack?
Answer
An attack where a small UDP request (with spoofed source IP) triggers a
large response to the victim, amplifying traffic.
- How does the receiver verify the UDP checksum?
Answer
It computes the one’s complement sum over the pseudo‑header, header, and
data, including the checksum field. If the result is 0xFFFF, the checksum is valid.
- What is the role of the source port in UDP?
Answer
It identifies the sending process; the receiver can use it to reply. It may
be 0 if no reply is expected.
- Can UDP be used for broadcast and multicast?
Answer
Yes, UDP is commonly used for broadcast (e.g., DHCP) and multicast (e.g.,
streaming).
- What is the minimum length of a UDP datagram (including header)?
Answer
8 bytes (header only).
- How does UDP handle packet reordering?
Answer
It does not; it delivers datagrams in the order received, if at all.
- What is UDP‑Lite?
Answer
A variant of UDP where the checksum covers only part of the payload,
allowing corruption in unprotected portions.
- Why is UDP checksum considered “optional” in IPv4 but mandatory in IPv6?
Answer
IPv6 does not have a header checksum, so the transport layer must provide
error detection to avoid corrupt data being delivered.
- What is the purpose of padding in UDP checksum calculation?
Answer
If the data length is odd, a zero byte is appended to make it even,
allowing 16‑bit word summation.
- How does a TFTP (Trivial FTP) achieve reliability over UDP?
Answer
It uses a stop‑and‑wait protocol: sends data, waits for ACK, retransmits on
timeout.
- What is the primary advantage of UDP over TCP for DNS?
Answer
Low latency (no handshake) and small overhead, making queries fast.
- Can UDP be used for secure communication? Why or why not?
Answer
UDP itself does not provide security, but security can be added at higher
layers (e.g., DTLS, QUIC’s TLS).
- What is the difference between UDP and TCP in terms of data boundaries?
Answer
UDP preserves message boundaries; TCP is a byte‑stream with no boundaries.
- Why might an application implement its own congestion control over UDP?
Answer
To have finer control over sending rates, or to experiment with new
algorithms not available in TCP.
- What is the maximum size of a UDP datagram (theoretical)?
Answer
The length field is 16 bits, so max 65535 bytes, but IP layer limits
typically reduce this (e.g., 64KB).
🛠️ Exercises
Apply your knowledge with these 15 exercises. Solutions are provided below each.
- Exercise 1: Checksum Calculation
Given a simple UDP segment with source port 53, dest port 80, length 12 bytes, and data "HI" (0x4849).
Assume pseudo‑header: srcIP=192.168.1.1, destIP=192.168.1.2, protocol=17, UDP length=12. Compute the UDP
checksum step by step.
Solution
Pseudo‑header (12 bytes) + UDP header (8 bytes) + data (2 bytes) = 22 bytes, but we pad to even
length (22 is even). We add one’s complement sum of all 16‑bit words:
- srcIP: 192.168 → 0xC0A8, .1.1 → 0x0101? Actually split into 16‑bit words: 0xC0A8, 0x0101,
0xC0A8, 0x0102 (for dest)
- protocol+length: 0x0011, 0x000C
- UDP header: 0x0035, 0x0050, 0x000C, 0x0000 (checksum initially)
- data: 0x4849
- Sum all: compute binary sum, wrap carries, then complement.
Final checksum = 0x... (example).
- Exercise 2: UDP Header Analysis
A UDP datagram has length field = 100 bytes. How many bytes of data are present?
Solution
100 – 8 = 92 bytes.
- Exercise 3: Port Usage
A client sends a DNS query from source port 54321 to DNS server port 53. The server replies. What are
the source and destination ports in the reply segment?
Solution
Source port: 53, Destination port: 54321.
- Exercise 4: UDP vs TCP Overhead
Calculate the protocol overhead percentage for a 1000‑byte application message sent over UDP vs TCP (TCP
header = 20 bytes, no options).
Solution
UDP: (8/1008)*100 ≈ 0.79%. TCP: (20/1020)*100 ≈ 1.96%.
- Exercise 5: Reliability over UDP
Design a simple reliable protocol over UDP for a file transfer. Describe the sequence of messages,
timeouts, and retransmissions.
Solution
Divide file into blocks, each with sequence number. Sender sends block,
starts timer. Receiver ACKs block number. Sender retransmits on timeout. Use cumulative ACKs to
reduce overhead.
- Exercise 6: UDP Broadcast
Explain how a DHCP client uses UDP broadcast to discover a server. What port numbers are used?
Solution
The client sends a DHCPDISCOVER to 255.255.255.255:67 (UDP broadcast).
DHCP servers reply to the client’s MAC address and port 68. This uses UDP because broadcast is
needed.
- Exercise 7: Checksum Offloading
Why might a network interface card (NIC) offload UDP checksum computation? What is the benefit?
Solution
To reduce CPU load. The NIC computes the checksum in hardware, freeing
the CPU for other tasks, improving throughput.
- Exercise 8: UDP and NAT
A host behind a NAT sends a UDP datagram. How does the NAT handle the port mapping? How long does it
keep the mapping?
Solution
The NAT creates a mapping from internal (IP, port) to external (IP,
port). The mapping is typically kept as long as traffic flows; many NATs time out after a few
minutes of inactivity.
- Exercise 9: UDP Fragmentation
If a UDP datagram exceeds the MTU, it may be fragmented at the IP layer. What is the downside of
fragmentation for UDP?
Solution
If any fragment is lost, the entire datagram is lost, as UDP does not
reassemble. This can reduce reliability. Additionally, fragmentation increases overhead.
- Exercise 10: UDP and QoS
How can a network differentiate UDP traffic for QoS purposes?
Solution
By using Differentiated Services (DiffServ) marking in the IP header, or
by classifying based on port numbers (e.g., prioritize RTP ports).
- Exercise 11: UDP‑Lite Use Case
Give an example application where UDP‑Lite would be beneficial and explain why.
Solution
Video streaming with error‑resilient codecs: corrupted pixels in certain
regions can be tolerated, so checksum can cover only the critical header portion, saving CPU.
- Exercise 12: UDP Amplification Example
A DNS query of 60 bytes can trigger a response of 4000 bytes (DNSSEC). If an attacker spoofs the source
IP to the victim, what is the amplification factor?
Solution
Amplification factor = response size / request size = 4000 / 60 ≈ 66.7.
- Exercise 13: Connectionless vs Connection‑Oriented
Compare the state maintained by a UDP server vs a TCP server for thousands of clients. Why does UDP
scale better?
Solution
TCP server maintains per‑connection state (sequence numbers, timers,
buffers). UDP server only needs to demultiplex by port; no per‑client state, so it scales to many
more clients.
- Exercise 14: UDP Checksum Failure
If a UDP datagram arrives with an invalid checksum, what does the receiver do?
Solution
It discards the datagram silently (no error message to sender).
- Exercise 15: UDP over IPv6
Why is the UDP checksum mandatory in IPv6? What happens if it is zero?
Solution
IPv6 does not have a header checksum; without a UDP checksum, corrupted
data could be delivered. In IPv6, the checksum must be computed; a zero value is invalid and the
datagram is dropped.
📚 Homework
These advanced problems require synthesis and research. Sample answers are provided below.
- Problem 1: Pseudo‑header Necessity
Explain why the pseudo‑header is needed for UDP checksum. What would happen if it were omitted?
Sample Answer
The pseudo‑header includes the IP addresses and protocol number,
ensuring that the checksum protects against misdelivery (e.g., if the packet is delivered to the
wrong IP). Without it, a corrupted packet could be delivered to the wrong host but still pass the
checksum. It also binds the UDP segment to the IP layer.
- Problem 2: Checksum Endianness
UDP checksum computation uses network byte order (big‑endian). Explain why this is important and how it
affects systems with different endianness.
Sample Answer
Network byte order ensures that the checksum is consistent across
all systems. Hosts with little‑endian architectures must convert to big‑endian before computing;
otherwise the checksum would be incorrect. This is handled by the OS networking stack.
- Problem 3: UDP Maximum Datagram Size
What is the maximum size of a UDP datagram that can be sent without fragmentation assuming an Ethernet
MTU of 1500 bytes? Include IP and UDP headers. What about jumbo frames (9000 bytes)?
Sample Answer
With Ethernet MTU 1500, IP header (20) + UDP header (8) = 28, so max
data = 1500 – 28 = 1472 bytes. For jumbo frames (9000), max data = 9000 – 28 = 8972 bytes.
- Problem 4: UDP over Satellite
Discuss the challenges of using UDP over a high‑latency satellite link. How does the lack of congestion
control affect performance?
Sample Answer
High latency means that retransmissions (if implemented by the
application) would be costly. Without congestion control, UDP can flood the link, causing packet
loss and unfairness. Applications must implement their own rate adaptation.
- Problem 5: UDP and Firewalls
Why do many firewalls treat UDP differently than TCP? What are the implications for applications using
UDP?
Sample Answer
Firewalls often have stricter timeouts for UDP because it is
connectionless and harder to track. This can cause long‑idle UDP sessions to be dropped.
Applications may need to send keep‑alive messages.
- Problem 6: Reliable UDP for IoT
Design a lightweight reliable UDP protocol for IoT devices with limited memory and power. What features
would you include?
Sample Answer
Use a simple stop‑and‑wait with small sequence numbers (e.g.,
8‑bit). Use a small window for pipelining. Minimize timers and state. Use CoAP over UDP with
built‑in reliability options.
- Problem 7: UDP and Real‑Time Media
Explain how RTP (Real‑time Transport Protocol) uses UDP and adds sequence numbers and timestamps. Why
does RTP not use TCP?
Sample Answer
RTP adds sequence numbers to detect loss and reorder, and timestamps
for timing. TCP would introduce retransmission delays and head‑of‑line blocking, which are
detrimental to real‑time media.
- Problem 8: UDP in QUIC
QUIC runs over UDP. What does QUIC add on top of UDP to make it suitable for HTTP/3?
Sample Answer
QUIC adds multiplexed streams, reliability per stream, congestion
control (like TCP), security (TLS 1.3), and connection migration. It uses UDP as a transport layer
to avoid OS‑level bottlenecks.
- Problem 9: UDP Checksum in Hardware
Discuss the trade‑offs of offloading UDP checksum to the NIC. What are the potential downsides?
Sample Answer
Offloading reduces CPU usage but can be limited by NIC capabilities
(e.g., for odd‑length packets). It may also introduce latency if the NIC is busy. Additionally, it
complicates debugging.
- Problem 10: UDP Security – Spoofing
Explain how an attacker can spoof a UDP packet’s source IP. What countermeasures are available at the
network level?
Sample Answer
An attacker can craft a packet with a forged source IP if the
network does not perform ingress filtering. Countermeasures include BCP 38 (source address
validation), and authentication at the application layer (e.g., DNS‑over‑TLS, but DNS uses UDP
without security, hence DNSSEC).
- Problem 11: UDP and Load Balancing
How can a load balancer distribute UDP traffic across multiple servers? What challenges arise compared
to TCP?
Sample Answer
For UDP, a load balancer can use a hash of the 4‑tuple (or a
consistent hash) to direct packets from the same client to the same server. Unlike TCP, there is no
connection state to synchronize, so stateless load balancing is easier, but session persistence
becomes application‑dependent.
- Problem 12: UDP vs TCP for Gaming
Why do many multiplayer games use UDP? Give at least three reasons.
Sample Answer
1. Low latency – no handshake. 2. No head‑of‑line blocking – loss of
one update doesn't block subsequent updates. 3. Unreliable but timely delivery – outdated
information can be discarded. 4. Better control over send rates.
- Problem 13: UDP and VPNs
Some VPNs use UDP (e.g., OpenVPN with UDP). Why would a VPN choose UDP over TCP?
Sample Answer
UDP avoids TCP‑over‑TCP issues (cascading retransmissions,
head‑of‑line blocking). It also reduces overhead and latency, which is important for tunneling.
- Problem 14: UDP in Cloud Environments
In cloud data centers, UDP is often used for internal traffic (e.g., service meshes). What are the
advantages?
Sample Answer
Low latency, high throughput, and the ability to implement custom
application‑layer reliability and congestion control that fits the microservices architecture.
- Problem 15: Future of UDP
With the rise of QUIC, do you think UDP will become even more important? Discuss the potential impact on
network neutrality and fairness.
Sample Answer
Yes, QUIC is built on UDP, making UDP more critical than ever.
However, the lack of TCP‑like congestion control in UDP could lead to fairness issues; QUIC
implements its own, but middleboxes may not enforce it. This could challenge network neutrality if
ISPs treat UDP differently.
📌 Summary
- UDP provides a minimal, connectionless datagram service with an 8‑byte header and optional checksum.
- The checksum covers a pseudo‑header and the segment, using one’s complement arithmetic for error
detection.
- UDP’s advantages include low latency, low overhead, and support for broadcast/multicast.
- Its limitations (no reliability, flow or congestion control) make it unsuitable for many applications,
but it is ideal for real‑time media, DNS, and other latency‑sensitive services.
- Reliability can be built on top of UDP (e.g., TFTP, QUIC) when needed.
- UDP is also a vector for security attacks, requiring network‑level protections.
In the next tutorial, we will explore the Principles of Reliable Data
Transfer, the foundation for TCP’s reliability.