Skip to content
Tutorials Center
My learning

Topics

Lesson 22 of 79 ยท The Network Layer: IP

The IPv4 Packet and Fragmentation

Learn every field of the IPv4 header, how the header checksum and TTL work, why fragmentation exists, and how path MTU discovery avoids it, with Python demos.

You know how an IPv4 address names a host and how CIDR groups addresses into networks. Now look at the thing that actually carries those addresses across the Internet: the IPv4 packet. Every TCP segment, UDP datagram and ICMP message you will meet later travels as the payload of an IP packet, and every router on the path reads the same small header to decide what to do with it.

That header is only 20 bytes long (without options), yet nearly every field has a job that shows up in real troubleshooting: the TTL that traceroute abuses, the protocol number that tells the receiver which transport to hand the data to, and the fragmentation fields that appear whenever a packet is too big for a link. In this lesson you build a header from raw bytes, check its checksum, and fragment and reassemble a large packet yourself.

The header, field by field

An IPv4 header is a sequence of 32-bit words. Fields are always in network byte order (big-endian).

flowchart TB
    subgraph W1["Word 1"]
      A["Version (4 bits)"] --- B["IHL (4)"] --- C["DSCP + ECN (8)"] --- D["Total length (16)"]
    end
    subgraph W2["Word 2"]
      E["Identification (16)"] --- F["Flags (3)"] --- G["Fragment offset (13)"]
    end
    subgraph W3["Word 3"]
      H["TTL (8)"] --- I["Protocol (8)"] --- J["Header checksum (16)"]
    end
    subgraph W4["Words 4 and 5"]
      K["Source address (32)"] --- L["Destination address (32)"]
    end
    W1 --> W2 --> W3 --> W4

The five mandatory 32-bit words of an IPv4 header. Options, if any, follow the destination address.

Field Size Meaning
Version 4 bits Always 4 for IPv4.
IHL 4 bits Header length in 32-bit words. The minimum is 5 (20 bytes), the maximum 15 (60 bytes).
DSCP / ECN 6 + 2 bits Traffic class for QoS and congestion signalling. This byte was once called "type of service".
Total length 16 bits Header plus payload in bytes. The largest possible packet is 65,535 bytes.
Identification 16 bits A number the sender picks so that the fragments of one packet can be matched up.
Flags 3 bits One reserved bit (must be 0), DF (Don't Fragment) and MF (More Fragments).
Fragment offset 13 bits Where this fragment's data starts in the original payload, counted in 8-byte units.
TTL 8 bits Time to live: a hop budget. Each router subtracts 1.
Protocol 8 bits Which protocol is in the payload.
Header checksum 16 bits Protects the header only, not the payload.
Source / destination 32 bits each The IPv4 addresses.

Because the IHL field is four bits and counts words, the header can never exceed 60 bytes, so there are at most 40 bytes of options. Options such as record route or timestamp are rare today: routers often handle them slowly or drop them.

TTL and the protocol field

The TTL exists to stop packets circling forever when routing is broken. The sender starts it at a value such as 64 or 128. Every router decreases it by one, and a router that would bring it to 0 discards the packet and sends back an ICMP time exceeded message. Despite the name, it counts hops and not seconds. traceroute sends probes with TTL 1, 2, 3 and so on, and uses the error messages to learn each router on the path.

The protocol field is the multiplexing key of the network layer, in the same way that ports are for the transport layer. A few values you should know:

Value Protocol
1 ICMP
6 TCP
17 UDP
41 IPv6 inside IPv4 (tunnel)
47 GRE
50 IPsec ESP

The header checksum

The checksum is the 16-bit ones' complement sum of the header, taken as 16-bit words, with the checksum field counted as zero while computing. The procedure:

  1. Split the header into 16-bit words and add them in ordinary arithmetic.
  2. If the sum overflows 16 bits, fold the carry back in by adding it to the low 16 bits. Repeat until nothing is left above bit 15.
  3. Flip every bit of the result. That is the checksum.

A receiver adds all words, including the checksum, folds the carries, and the result must be 0xFFFF. Equivalently, the inverted result is 0. Since the TTL changes at every hop, every router must recompute the checksum. (Routers do this with an incremental update instead of summing everything again, but the effect is the same.)

The checksum covers only the header: the payload is protected by the transport protocol's own checksum and, on the link, by the CRC in the frame. Run the example below. It builds a real 20-byte header, parses it, verifies it, damages one bit, and then simulates a router forwarding the packet.

Python Build, parse and verify an IPv4 header

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice DF = 1 in the output. The sender set the Don't Fragment flag, which matters in the next section.

MTU and why fragmentation exists

Every link technology has a limit on the biggest payload one frame can carry, called the Maximum Transmission Unit (MTU). Ethernet allows 1,500 bytes of IP packet per frame. Some links allow less: many PPPoE (DSL) connections give 1,492, and tunnels such as VPNs subtract room for their own headers. Others allow more, like "jumbo frames" of around 9,000 bytes in data centers.

An IP packet is only as large as the application and transport layers made it, so a router can receive a packet that does not fit the next link. IPv4 has two answers:

  • If DF is 0, the router may split the packet into fragments, each small enough for the outgoing link.
  • If DF is 1, the router must not fragment. It drops the packet and sends back an ICMP Destination Unreachable, Fragmentation Needed message that carries the MTU of the link that was too small.

How fragments are made

Fragmentation works on the payload of the packet. The rules:

  1. Each fragment gets its own IP header, copied from the original, with the same identification value.
  2. Every fragment except the last has MF = 1. The last has MF = 0.
  3. The fragment offset says where the fragment's data begins in the original payload, in units of 8 bytes. So all fragments except the last must carry a multiple of 8 bytes of data.
  4. Total length and checksum are rewritten for each fragment.

For a 4,000-byte payload crossing a link with MTU 1,500, each fragment can hold 1,500 - 20 = 1,480 bytes of data, which is a multiple of 8 (1,480 / 8 = 185). So the fragments carry 1,480, 1,480 and 1,040 bytes, with offsets 0, 185 and 370.

flowchart LR
    P["Original packet<br/>payload 4000 B, id 7"] --> R{{"Router<br/>next MTU 1500"}}
    R --> F1["Fragment 1<br/>1480 B, offset 0, MF 1"]
    R --> F2["Fragment 2<br/>1480 B, offset 185, MF 1"]
    R --> F3["Fragment 3<br/>1040 B, offset 370, MF 0"]

One router splits a large packet; the destination, not the next router, puts it back together.

Reassembly happens only at the destination. Intermediate routers do not glue fragments together, because the fragments might take different paths and a later link may need to split them again. The receiver collects all fragments with the same source, destination, protocol and identification, places each at its offset, and waits until it has a fragment with MF = 0 and no gaps. If a timer runs out first, it discards everything. A fragment can itself be fragmented again, and the offsets stay relative to the original payload.

Python Fragment a packet and reassemble it out of order

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Why fragmentation is avoided

Fragmentation looks harmless, but it has real costs:

  • One lost fragment loses the whole packet. The receiver cannot use a partial datagram, and the sender (for UDP) or TCP has to resend all of it. With a loss rate p per fragment, the chance that a packet with n fragments survives falls quickly as n grows.
  • Routers and firewalls struggle with fragments. Only the first fragment has the transport header, so a firewall that filters on ports cannot judge the others. Fragment tricks have been used to dodge filters, so many devices drop or limit them.
  • Extra work and memory at the receiver, which has to buffer partial packets (an old denial-of-service target).
  • Small identification space. 16 bits wraps quickly on fast links, so old fragments can be mixed with new ones.

Modern stacks therefore try to send packets that fit from the start.

Path MTU discovery

Path MTU Discovery (PMTUD) finds the smallest MTU on the whole path, so the sender can choose a packet size that never needs fragmentation. The method is simple trial and error:

  1. The sender sets DF = 1 on every packet and starts with its own link's MTU.
  2. A router whose next link is too small drops the packet and replies with ICMP type 3, code 4, "fragmentation needed and DF set", including that link's MTU.
  3. The sender lowers its packet size to the reported value and sends again.
  4. This repeats until a packet makes it. The size that worked is the path MTU.

TCP does this automatically and sizes its segments with the MSS (maximum segment size, the MTU minus the 20-byte IP header and 20-byte TCP header, so 1,460 bytes on Ethernet). See TCP segments.

Python Path MTU discovery simulation

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The black hole problem

PMTUD depends on those ICMP messages getting back. Some networks block all ICMP "for security". Then the sender never learns why its large packets vanish, while small packets (such as the TCP handshake and a short request) work fine. The connection appears to hang as soon as a big response is sent. This PMTU black hole is a classic troubleshooting case, and the reason why you should allow at least ICMP types 3 and 11 through a firewall. See troubleshooting networks for how to diagnose it. Some systems add packetization-layer PMTUD, which uses the transport protocol's own probes and does not depend on ICMP.

IPv6 removes in-network fragmentation altogether: routers never fragment, only the sending host may, and PMTUD is mandatory.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. A packet has IHL = 6. How long is its header?

    IHL counts 32-bit words, so 6 words are 24 bytes: the 20-byte base header plus 4 bytes of options.
  2. 2. A router receives a packet with TTL = 1 that is not addressed to it. What does it do?

    Decrementing would make the TTL 0, so the router drops the packet and reports it with ICMP time exceeded. This is exactly what traceroute relies on.
  3. 3. A 3,000-byte payload crosses a link with MTU 1,500. How many bytes of data does the first fragment carry, and what is the second fragment's offset field?

    Each fragment holds up to 1,500 - 20 = 1,480 data bytes, a multiple of 8. The offset is counted in 8-byte units, so 1,480 / 8 = 185.
  4. 4. Where are IPv4 fragments normally reassembled?

    Fragments may take different paths and may be split again later, so only the destination host reassembles them.
  5. 5. Large transfers hang while pings and small requests work, and a firewall blocks all ICMP. What is the likely cause?

    With DF set, oversized packets are dropped and the ICMP error that would tell the sender to shrink them is blocked, so only small packets get through.

Summary

  • An IPv4 header is 20 bytes plus up to 40 bytes of options. IHL counts 32-bit words; total length is in bytes.
  • TTL is a hop limit that each router decrements, and it stops routing loops. The protocol field names the payload (1 ICMP, 6 TCP, 17 UDP).
  • The header checksum is a ones' complement sum over the header only. It is recomputed at every hop because the TTL changes.
  • Fragmentation splits a packet to fit a smaller MTU. Fragments share an identification, use 8-byte offset units, and set MF on all but the last. Only the destination reassembles them.
  • Fragmentation is costly, so senders set DF and use Path MTU Discovery, which depends on ICMP "fragmentation needed" messages. Blocking them causes black holes.

Next, see how the much larger IPv6 address space and simpler header fix many of IPv4's limits.