📦 Tutorial 3: User Datagram Protocol (UDP) – Advanced

Comprehensive university‑level coverage – COMP347 (TrustOpen University)

Table of Contents

🎯 Learning Objectives

After completing this tutorial, you should be able to:

🔍 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:

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:

Figure 1: UDP Segment Format

 0                   1                   2                   3
 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|          Source Port          |       Destination Port        |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|            Length             |           Checksum            |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                             Data                              |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

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:

  1. Construct the pseudo‑header from the IP addresses, protocol (17), and UDP length.
  2. 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).
  3. 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).
  4. 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

4.2 Limitations

📘 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:

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:

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:

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.

  1. What is the size of the UDP header in bytes?
    Answer8 bytes.
  2. Which fields are present in the UDP header?
    AnswerSource Port, Destination Port, Length, Checksum.
  3. What is the purpose of the UDP pseudo‑header?
    AnswerTo include IP addresses, protocol number, and UDP length in the checksum calculation, protecting against misrouting.
  4. Is the UDP checksum mandatory in IPv4?
    AnswerNo, it is optional; a value of 0 indicates no checksum. In IPv6 it is mandatory.
  5. What does the UDP Length field indicate?
    AnswerThe total length of the UDP segment (header + data) in bytes. Minimum 8.
  6. Name three applications that typically use UDP.
    AnswerDNS, DHCP, VoIP (RTP).
  7. Does UDP provide flow control?
    AnswerNo.
  8. Does UDP provide congestion control?
    AnswerNo.
  9. What is the range of UDP port numbers?
    Answer0–65535.
  10. Why might a video streaming application prefer UDP over TCP?
    AnswerTo avoid retransmission delays and head‑of‑line blocking; low latency is more important than perfect reliability.
  11. What is a UDP amplification attack?
    AnswerAn attack where a small UDP request (with spoofed source IP) triggers a large response to the victim, amplifying traffic.
  12. How does the receiver verify the UDP checksum?
    AnswerIt 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.
  13. What is the role of the source port in UDP?
    AnswerIt identifies the sending process; the receiver can use it to reply. It may be 0 if no reply is expected.
  14. Can UDP be used for broadcast and multicast?
    AnswerYes, UDP is commonly used for broadcast (e.g., DHCP) and multicast (e.g., streaming).
  15. What is the minimum length of a UDP datagram (including header)?
    Answer8 bytes (header only).
  16. How does UDP handle packet reordering?
    AnswerIt does not; it delivers datagrams in the order received, if at all.
  17. What is UDP‑Lite?
    AnswerA variant of UDP where the checksum covers only part of the payload, allowing corruption in unprotected portions.
  18. Why is UDP checksum considered “optional” in IPv4 but mandatory in IPv6?
    AnswerIPv6 does not have a header checksum, so the transport layer must provide error detection to avoid corrupt data being delivered.
  19. What is the purpose of padding in UDP checksum calculation?
    AnswerIf the data length is odd, a zero byte is appended to make it even, allowing 16‑bit word summation.
  20. How does a TFTP (Trivial FTP) achieve reliability over UDP?
    AnswerIt uses a stop‑and‑wait protocol: sends data, waits for ACK, retransmits on timeout.
  21. What is the primary advantage of UDP over TCP for DNS?
    AnswerLow latency (no handshake) and small overhead, making queries fast.
  22. Can UDP be used for secure communication? Why or why not?
    AnswerUDP itself does not provide security, but security can be added at higher layers (e.g., DTLS, QUIC’s TLS).
  23. What is the difference between UDP and TCP in terms of data boundaries?
    AnswerUDP preserves message boundaries; TCP is a byte‑stream with no boundaries.
  24. Why might an application implement its own congestion control over UDP?
    AnswerTo have finer control over sending rates, or to experiment with new algorithms not available in TCP.
  25. What is the maximum size of a UDP datagram (theoretical)?
    AnswerThe 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.

  1. 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).

  2. Exercise 2: UDP Header Analysis
    A UDP datagram has length field = 100 bytes. How many bytes of data are present?
    Solution100 – 8 = 92 bytes.
  3. 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?
    SolutionSource port: 53, Destination port: 54321.
  4. 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).
    SolutionUDP: (8/1008)*100 ≈ 0.79%. TCP: (20/1020)*100 ≈ 1.96%.
  5. Exercise 5: Reliability over UDP
    Design a simple reliable protocol over UDP for a file transfer. Describe the sequence of messages, timeouts, and retransmissions.
    SolutionDivide 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.
  6. Exercise 6: UDP Broadcast
    Explain how a DHCP client uses UDP broadcast to discover a server. What port numbers are used?
    SolutionThe 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.
  7. Exercise 7: Checksum Offloading
    Why might a network interface card (NIC) offload UDP checksum computation? What is the benefit?
    SolutionTo reduce CPU load. The NIC computes the checksum in hardware, freeing the CPU for other tasks, improving throughput.
  8. 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?
    SolutionThe 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.
  9. 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?
    SolutionIf any fragment is lost, the entire datagram is lost, as UDP does not reassemble. This can reduce reliability. Additionally, fragmentation increases overhead.
  10. Exercise 10: UDP and QoS
    How can a network differentiate UDP traffic for QoS purposes?
    SolutionBy using Differentiated Services (DiffServ) marking in the IP header, or by classifying based on port numbers (e.g., prioritize RTP ports).
  11. Exercise 11: UDP‑Lite Use Case
    Give an example application where UDP‑Lite would be beneficial and explain why.
    SolutionVideo streaming with error‑resilient codecs: corrupted pixels in certain regions can be tolerated, so checksum can cover only the critical header portion, saving CPU.
  12. 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?
    SolutionAmplification factor = response size / request size = 4000 / 60 ≈ 66.7.
  13. 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?
    SolutionTCP 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.
  14. Exercise 14: UDP Checksum Failure
    If a UDP datagram arrives with an invalid checksum, what does the receiver do?
    SolutionIt discards the datagram silently (no error message to sender).
  15. Exercise 15: UDP over IPv6
    Why is the UDP checksum mandatory in IPv6? What happens if it is zero?
    SolutionIPv6 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.

  1. Problem 1: Pseudo‑header Necessity
    Explain why the pseudo‑header is needed for UDP checksum. What would happen if it were omitted?
    Sample AnswerThe 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.
  2. 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 AnswerNetwork 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.
  3. 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 AnswerWith 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.
  4. 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 AnswerHigh 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.
  5. Problem 5: UDP and Firewalls
    Why do many firewalls treat UDP differently than TCP? What are the implications for applications using UDP?
    Sample AnswerFirewalls 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.
  6. 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 AnswerUse 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.
  7. 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 AnswerRTP 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.
  8. 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 AnswerQUIC 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.
  9. Problem 9: UDP Checksum in Hardware
    Discuss the trade‑offs of offloading UDP checksum to the NIC. What are the potential downsides?
    Sample AnswerOffloading 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.
  10. 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 AnswerAn 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).
  11. Problem 11: UDP and Load Balancing
    How can a load balancer distribute UDP traffic across multiple servers? What challenges arise compared to TCP?
    Sample AnswerFor 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.
  12. Problem 12: UDP vs TCP for Gaming
    Why do many multiplayer games use UDP? Give at least three reasons.
    Sample Answer1. 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.
  13. Problem 13: UDP and VPNs
    Some VPNs use UDP (e.g., OpenVPN with UDP). Why would a VPN choose UDP over TCP?
    Sample AnswerUDP avoids TCP‑over‑TCP issues (cascading retransmissions, head‑of‑line blocking). It also reduces overhead and latency, which is important for tunneling.
  14. 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 AnswerLow latency, high throughput, and the ability to implement custom application‑layer reliability and congestion control that fits the microservices architecture.
  15. 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 AnswerYes, 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

In the next tutorial, we will explore the Principles of Reliable Data Transfer, the foundation for TCP’s reliability.

COMP347 – Computer Networks (Rev. 10) · TrustOpen University · Based on Kurose & Ross, Computer Networking: A Top‑Down Approach, 9th ed. (2025).