The IP Protocol, Fragmentation, ARP, DHCP, ICMP and NAT

The IPv4 header field by field, fragmentation and offsets, TTL, and the protocols that support IP (ARP, DHCP, ICMP, NAT), with a look at IPv6.

IP delivers datagrams from a source host to a destination host across many networks. It is connectionless and best effort: datagrams may be lost, duplicated or delivered out of order, and IP does nothing about it. Reliability is left to TCP.

The IPv4 header

The header is 20 bytes without options, and at most 60 bytes.

FieldBitsMeaning
Version44 for IPv4
HLEN (header length)4Header length in 4-byte words: 5 (20 bytes) to 15 (60 bytes)
Type of service / DSCP8Priority and congestion marking
Total length16Header + data, in bytes: at most 65,535
Identification16The same for all fragments of one datagram
Flags3DF (don't fragment), MF (more fragments follow)
Fragment offset13Position of this fragment's data, in units of 8 bytes
TTL (time to live)8Decremented by every router; the datagram is discarded at 0
Protocol8What is inside: 1 = ICMP, 6 = TCP, 17 = UDP
Header checksum16Covers the header only; recomputed at every router, because the TTL changes
Source address, destination address32 + 32
Options0–320Rarely used

Two quick consequences:

  • Data length = total length − 4 × HLEN. With HLEN = 8 and total length 1000, the header is 32 bytes and the data 968 bytes.
  • TTL stops a datagram from looping forever when routing tables are wrong. traceroute sends datagrams with TTL = 1, 2, 3, ... and collects the ICMP "time exceeded" messages each router returns.

Fragmentation

Every link has a maximum transmission unit (MTU), the largest IP datagram it can carry (1500 bytes on Ethernet). A router that must forward a datagram larger than the next link's MTU splits it into fragments, each a datagram with its own header. Fragments are reassembled only at the destination, never by routers.

Rules for each fragment:

  1. The data in every fragment except the last must be a multiple of 8 bytes (because the offset counts 8-byte units).
  2. Offset = (position of the fragment's first data byte in the original data) / 8.
  3. MF = 1 on every fragment except the last; the identification is copied from the original.
  4. If DF = 1 and fragmentation is needed, the router drops the datagram and sends an ICMP error.

Example: a 4000-byte datagram (20-byte header, 3980 bytes of data) reaches a link with MTU 1500. Each fragment can carry at most ⌊(1500 − 20) / 8⌋ × 8 = 1480 bytes of data.

def fragment(total_length, mtu, header=20):
    data = total_length - header
    per_fragment = (mtu - header) // 8 * 8      # multiple of 8 bytes
    frags, start = [], 0
    while start < data:
        size = min(per_fragment, data - start)
        more = start + size < data
        frags.append((size + header, start // 8, int(more)))
        start += size
    return frags

print("total length  offset  MF")
for length, offset, mf in fragment(4000, 1500):
    print(f"{length:12}  {offset:6}  {mf:2}")
Output
total length  offset  MF
        1500       0   1
        1500     185   1
        1040     370   0

The offsets 0, 185 and 370 mean the data starts at bytes 0, 185 × 8 = 1480 and 370 × 8 = 2960. Fragmentation adds overhead: three headers (60 bytes) instead of one.

The receiver knows it has every fragment when the pieces cover the data from offset 0 to the fragment with MF = 0 without gaps. If any fragment is lost, the whole datagram is discarded. Because of this, modern hosts prefer path MTU discovery (send with DF = 1 and shrink the size whenever an ICMP "fragmentation needed" error comes back) instead of relying on fragmentation.

ARP: from IP address to MAC address

To send a datagram to the next hop on an Ethernet, the sender needs the next hop's MAC address. The Address Resolution Protocol (ARP) finds it:

  1. The sender broadcasts an ARP request: "Who has 192.168.1.1? Tell 192.168.1.10."
  2. The owner of that address unicasts an ARP reply with its MAC address.
  3. Both sides store the mapping in their ARP cache for a few minutes.

If the destination is on another network, the sender resolves the MAC address of its default gateway (router), not of the final destination. The IP addresses in the datagram stay the same along the path; the MAC addresses change at every hop.

DHCP: getting an address

A new host has no IP address. The Dynamic Host Configuration Protocol gives it one, together with the subnet mask, default gateway and DNS server, as a time-limited lease. It runs over UDP (server port 67, client port 68) in four steps, known as DORA:

Discoverclient broadcastsOfferserver proposes an addressRequestclient broadcasts its choiceAcknowledgeserver confirms the lease
Getting an address with DHCP

The client uses 0.0.0.0 as its source address and 255.255.255.255 as the destination until it has an address.

ICMP: errors and diagnostics

The Internet Control Message Protocol carries error reports and queries for IP. ICMP messages travel inside IP datagrams (protocol 1).

MessageSent when
Echo request / echo replyping tests whether a host is reachable
Destination unreachableNo route, host down, port closed, or fragmentation needed with DF = 1
Time exceededTTL reached 0 (used by traceroute), or reassembly timed out
RedirectA better first-hop router exists

No ICMP error is ever sent about an ICMP error message, a fragment other than the first, or a broadcast datagram, so errors cannot multiply.

NAT: many hosts behind one address

Network Address Translation lets a whole private network (addresses such as 192.168.x.x) share one public IP address. The NAT router rewrites each outgoing datagram's source (private address, port) to (public address, new port), records the mapping in a translation table, and reverses the rewrite for replies.

Private sidePublic side
192.168.1.10 : 51000203.0.113.5 : 40001
192.168.1.11 : 51000203.0.113.5 : 40002

NAT saves IPv4 addresses, but it breaks the end-to-end model: a host outside cannot start a connection to a host inside unless a mapping (port forwarding) is set up. Because it rewrites addresses and ports, NAT must also recompute the IP and TCP/UDP checksums.

IPv6 in brief

IPv4IPv6
Address size32 bits128 bits, written as 8 groups of hex: 2001:db8::1
Header20–60 bytes, with optionsFixed 40 bytes; options moved to extension headers
Header checksumYesNo (left to link and transport layers)
FragmentationBy routers and hostsOnly by the source host
Address configurationDHCP or manualAlso stateless autoconfiguration
BroadcastYesNo (multicast instead)

Practice questions

GATE-styleNAT · numerical answer

A datagram with total length 2400 bytes (20-byte header) is sent over a link with MTU 820 bytes. What is the fragment offset field of the last fragment?

Show answer and explanationAnswer: 200

Each fragment carries at most ⌊800 / 8⌋ × 8 = 800 data bytes. The 2380 data bytes split into 800 + 800 + 780, starting at bytes 0, 800 and 1600. The last offset is 1600 / 8 = 200.

GATE-styleNAT · numerical answer

For the same datagram (2400 bytes total, 20-byte header, MTU 820), how many bytes in total (headers included) are sent over the link after fragmentation?

Show answer and explanationAnswer: 2440

There are 3 fragments, each with its own 20-byte header: 2380 + 3 × 20 = 2440 bytes, 40 more than the original datagram.

GATE-styleNAT · numerical answer

An IPv4 header has HLEN = 7 and the total length field is 500. How many bytes of data does the datagram carry?

Show answer and explanationAnswer: 472

HLEN counts 4-byte words, so the header is 7 × 4 = 28 bytes and the data 500 − 28 = 472 bytes.

GATE-styleMSQ · one or more correct answers

Which statements are true? (Select all that apply.)

Show answer and explanationAnswer: A, B, D

Routers forward fragments independently and never reassemble them. The checksum must be recomputed because the TTL changes at each hop. ARP requests are broadcast, but replies are unicast. IPv6 dropped the header checksum.

GATE-styleMCQ · one correct answer

Host H on network N1 sends an IP datagram to host K on another network, through router R. In the Ethernet frame that leaves H, what are the destination MAC and destination IP addresses?

Show answer and explanationAnswer: B

The frame only goes to the next hop, the router, so it carries R's MAC address (found with ARP). The datagram inside keeps the final destination's IP address, K's, all the way.