CS Atlas GATE 2027
Computer Networks course
0 / 90 complete

Computer Networks/Phase 3 · Network Layer

Lesson 3.2.1 · Item 48 of 90

IPv4 header fields: HLEN, Total Length, TTL, Protocol & Checksum

Read at your own pace. Try the questions before opening the answers.

On this page

Recall warm-up

  1. What is the smallest CIDR block size that can accommodate 50 usable hosts?
    Stuck? Peek at 3.1.6 Variable-Length Subnet Masking (VLSM) & block allocation
    • Block size for H usable hosts: smallest 2ʰ with 2ʰ − 2 ≥ H; prefix = /(32 − h).
    • Largest-first rule: sort subnets by decreasing 2ʰ and allocate sequentially so every block is naturally aligned.
    • Non-overlap: sum of block sizes ∑ 2ʰᵢ ≤ 2³²⁻ⁿ_parent, and every block starts on a multiple of its own 2ʰᵢ.

    Read lesson 3.1.6

  2. How does a receiver verify a 16-bit 1’s-complement Internet checksum?
    Stuck? Peek at 2.2.3 The Internet checksum: compute and verify
    • 16-bit words; checksum field = 0 while summing.
    • Sum with 1's-complement addition (wrap carries); checksum = complement of the sum.
    • Receiver: sum of all words + checksum = 0xFFFF → accept.
    • Misses word reordering and compensating errors.

    Read lesson 2.2.3

  3. What is the network-layer PDU called, and what does encapsulation add to the transport PDU?
    Stuck? Peek at 1.1.3 Encapsulation, headers, PDU names, addresses and devices per layer
    • Encapsulation: payload + header (+ trailer at data link); decapsulation in reverse.
    • PDUs: message (app) → segment (transport) → packet/datagram (network) → frame (data link) → bits.
    • Addresses: link (per hop, changes) · network (end to end, fixed) · which-program id · names.
    • Devices: hub/repeater 1 · bridge/switch 2 · router 3 · gateway/host up to 7.
    • Efficiency = data / (data + all headers + trailer).

    Read lesson 1.1.3

The Hook

In Chapter 3.1 we stamped a 32-bit source IP and a 32-bit destination IP onto our network-layer envelope. Those two addresses take 4 + 4 = 8 bytes. Yet the minimum IPv4 header is 20 bytes!

What are the other 12 bytes doing? They protect the network from packets looping forever, tell the destination how long the header and packet are, help split and reassemble oversized datagrams, and identify which upper-layer protocol sits inside the payload.

Predict First

The IPv4 header length field (HLEN) is only 4 bits wide (maximum value 1111₂ = 15), yet the IPv4 header is at least 20 bytes and can be up to 60 bytes long. How can a 4-bit number represent 20 to 60 bytes?

,

By counting in 4-byte (32-bit) words instead of single bytes! Every value in HLEN is multiplied by 4: minimum 5 × 4 = 20 bytes (no options), maximum 15 × 4 = 60 bytes (20B base + up to 40B of options).

Core Concept

The 20-byte Fixed IPv4 Header (Five 32-bit Rows)

An IPv4 header consists of a mandatory 20-byte (5 × 32-bit) fixed part followed by 0 to 40 bytes of options (padded to a multiple of 4 bytes):

048162431
Version (4)4 bits
HLEN (×4B)4 bits
Type of Service (TOS)8 bits
Total Length (bytes)16 bits
Identification16 bits
Flags (0|DF|MF)3 bits
Fragment Offset (×8B)13 bits
Time to Live (TTL)8 bits
Protocol8 bits
Header Checksum16 bits
Source IPv4 Address (32 bits)32 bits
Destination IPv4 Address (32 bits)32 bits
Options + Padding (0 to 40 bytes, multiple of 4 bytes)32 bits
The IPv4 datagram header (32 bits = 4 bytes per row; rows 1–5 are the mandatory 20-byte base header).

The Scaling-Factor Trio: HLEN (×4), Total Length (×1), Offset (×8)

GATE loves testing the three length/offset fields and their scaling factors:

  • Version (4 bits): Always 4 (0100₂) for IPv4.
  • HLEN / IHL (4 bits, scaling factor ×4 bytes): Header length in 32-bit words. Range 5 to 15 → 20 to 60 bytes. Values 0–4 are invalid!
  • Total Length (16 bits, scaling factor ×1 byte): Total datagram length (header + payload) in bytes. Max = 2¹⁶ − 1 = 65,535 bytes; payload length = Total Length − 4 × HLEN.
  • Identification (16 bits), Flags (3 bits: reserved 0, DF = Don't Fragment, MF = More Fragments), Fragment Offset (13 bits, scaling factor ×8 bytes): Used when a datagram is sliced into smaller fragments (3.2.2).
HLEN (4 bits) × 4
Counts 4-byte words. Range 5..15 → 20..60 B.
Total Length (16 bits) × 1
Counts 1-byte units (header + data). Max 65,535 B.
Fragment Offset (13 bits) × 8
Counts 8-byte blocks of original payload.
Memorise the three multipliers: ×4 (HLEN), ×1 (Total Length), ×8 (Fragment Offset).

TTL, Protocol, and Header Checksum (What Changes at Every Router?)

Row 3 controls hop-by-hop survival, demultiplexing to the upper layer, and header integrity:

  • Time to Live — TTL (8 bits): Despite the name, TTL is a hop counter, not seconds! Every router along the path decrements TTL by 1. If TTL reaches 0, the router discards the datagram (preventing infinite routing loops) and sends an error notification back to the source.
  • Protocol (8 bits): Tells the final destination's network layer which upper-layer protocol should receive the payload (e.g., 1 = ICMP, 6 = TCP, 17 = UDP, 89 = OSPF — here just protocol ID numbers).
  • Header Checksum (16 bits): 1's-complement checksum computed ONLY over the IPv4 header (NOT the data payload!). Because TTL decreases at every router, every router must recompute the Header Checksum on every hop!
FieldBitsChanged by a normal router?Why?
Version, HLEN, TOS4, 4, 8No (unless TOS remarking)Fixed at source
Total Length, ID, Flags, Offset16, 16, 3, 13Only if fragmentation occursUpdated only when splitting datagram
TTL8YES — every router!Decremented by 1 on each hop to stop loops
Protocol, Src IP, Dst IP8, 32, 32NoEnd-to-end identifiers
Header Checksum16YES — every router!Must be recomputed because TTL changed!
On a normal hop without fragmentation, a router modifies ONLY TTL and Header Checksum.

Putting it together

  • The IPv4 header is 20 to 60 bytes long; the 4-bit HLEN field counts 4-byte words (5 to 15).
  • Total Length (16 bits) gives the entire datagram size in bytes, so Payload = Total Length − 4×HLEN.
  • Every router decrements TTL by 1 to prevent infinite loops and recomputes the 16-bit Header Checksum (which protects only the IP header).

Visual

Router receives datagram
verifies Header Checksum
Decrements TTL = TTL − 1
if TTL == 0 → drop!
Recomputes Header Checksum
because TTL changed
Looks up Dst IP via LPM
forwards to next hop
What every IPv4 router does to the header on a normal hop.

Worked Examples

Easy

An IPv4 datagram arrives with HLEN = 5 and Total Length = 400. How many bytes of header, options, and upper-layer payload does it carry?

Show the full solution

Header length = 5 × 4 = 20 bytes. Since minimum header is 20 bytes, options = 0 bytes.
Payload = Total Length − Header = 400 − 20 = 380 bytes.

Medium

An IPv4 header carries 22 bytes of option data. What must be the value of HLEN, and how many bytes of padding are added?

Show the full solution

Total header length must be a multiple of 4 bytes. Base header (20B) + options (22B) = 42B → round up to next multiple of 4 = 44 bytes.
Padding added = 44 − 42 = 2 bytes.
HLEN = 44 / 4 = 11 (1011₂).

GATE

The first 8 bits of an arriving IPv4 datagram are 01000100₂. Why does the router immediately discard the packet?

Show the full solution

First 4 bits = Version = 0100₂ = 4 (valid IPv4).
Next 4 bits = HLEN = 0100₂ = 4 → Header length = 4 × 4 = 16 bytes. Since the mandatory IPv4 base header is 20 bytes (minimum HLEN = 5), HLEN = 4 is corrupt/invalid and the packet is dropped!

GATE Traps

  • HLEN minimum is 5 (0101₂), NOT 0 or 20! Any HLEN < 5 is invalid; max HLEN = 15 means at most 40 bytes of options (60 − 20 = 40).
  • IPv4 Header Checksum covers ONLY the IP header! Corruption in the data payload is NOT detected by the IPv4 header checksum.
  • TTL is a hop count, not seconds! Even if a packet waits 5 seconds in a router queue, that router decrements TTL by 1.

Memory Hook

HLEN × 4, Total × 1, Offset × 8; TTL drops by 1 and Checksum is redone at every gate!

Connect the Dots

Look at Row 2 of the IPv4 header: Identification, Flags (DF, MF), and Fragment Offset (×8). Next question (3.2.2): What happens when our datagram is 4000 bytes long, but the next Ethernet link has an MTU of only 1500 bytes?

Self-Test

  1. NAT

    An IPv4 datagram has HLEN = 9 (in decimal) and Total Length = 1000 bytes. How many bytes of data payload does the datagram carry?

    Write your answer before opening the key.

  2. NAT

    What is the maximum number of option bytes that can be placed in an IPv4 header?

    Write your answer before opening the key.

  3. MSQ

    Which fields of an IPv4 header are ALWAYS modified by a standard router when forwarding a datagram (assuming no fragmentation)?

    1. Time to Live (TTL)
    2. Header Checksum
    3. Source IPv4 Address
    4. Total Length
  4. MCQ

    Why does the IPv4 Header Checksum need to be recomputed at every router along a path?

    1. Because the source and destination MAC addresses change at every hop.
    2. Because the TTL field in the IPv4 header is decremented at every hop.
    3. Because the data payload is re-encrypted at every hop.
    4. Because the Protocol field changes at every hop.
  5. MCQ

    Which of the following values of the 4-bit HLEN field in an IPv4 datagram is INVALID?

    1. 5
    2. 6
    3. 15
    4. 3

Rule Card

  • Base header = 20 B (`HLEN = 5`); Max header = 60 B (`HLEN = 15`, max 40 B options).
  • Multipliers: `Header = 4 × HLEN`, `Total Length = Header + Payload` (max 65,535 B), `Offset × 8 = start byte`.
  • Per-hop changes: Every router decrements TTL by 1 (drops if 0) and recomputes Header Checksum (which covers IP header only).

Concept Ledger

  • IPv4 header
  • version (4)
  • HLEN (×4)
  • type of service
  • total length
  • identification
  • flags (DF, MF)
  • fragment offset (×8)
  • TTL (hop limit)
  • protocol field (1/6/17)
  • header checksum
  • options (20–60 bytes)

Answer Vault

Try the questions first. Open when you are ready to check.

Warm-up answers

  1. 64 addresses (/26, since 2⁶ − 2 = 62 ≥ 50).
  2. It adds all 16-bit words including the checksum using 1’s-complement wrap-around addition; if the result is all 1s (complement = 0), no error is detected.
  3. A packet (or datagram); encapsulation prepends a network-layer header containing logical source/destination addresses and control fields.

Self-test answers

  1. 964 — Header length = 9 × 4 = 36 bytes. Data payload = 1000 − 36 = 964 bytes.
  2. 40 — 4-bit HLEN max is 15 → max total header = 15 × 4 = 60 bytes. Subtracting the 20-byte mandatory base header leaves 40 bytes for options.
  3. AB — Every router decrements TTL by 1 and must therefore recompute the Header Checksum. Source IP and Total Length (changed only on fragmentation) remain unchanged.
    • C: Source IP is unchanged by normal routing (only MAC source changes at L2).
    • D: Total Length changes only if the router fragments the datagram.
  4. B — The IPv4 header checksum covers the IPv4 header bytes, including TTL. Since TTL changes at every router, the checksum changes too.
    • A: MAC addresses are in the Layer-2 Ethernet frame header, not the IPv4 header.
    • C: IPv4 does not re-encrypt payloads.
    • D: Protocol field is fixed end-to-end.
  5. D — Since the minimum IPv4 header is 20 bytes = 5 × 4 bytes, any HLEN < 5 (such as 3 → 12 bytes) is invalid.
    • A: HLEN=5 → 20B (standard header with no options).
    • B: HLEN=6 → 24B (valid, 4B options).
    • C: HLEN=15 → 60B (valid, max 40B options).

Flashcards

  1. What are the minimum and maximum values of the 4-bit HLEN field, and what byte sizes do they represent?
    Reveal answerMin HLEN = 5 (20 bytes, no options); Max HLEN = 15 (60 bytes, 40 bytes of options).
  2. Which two IPv4 header fields change at every router hop during normal forwarding?
    Reveal answerTTL (decremented by 1) and Header Checksum (recomputed).
  3. Does the IPv4 Header Checksum cover the data payload?
    Reveal answerNo — it covers ONLY the IPv4 header (20 to 60 bytes).
  4. What are the scaling factors for HLEN, Total Length, and Fragment Offset?
    Reveal answerHLEN: ×4 bytes; Total Length: ×1 byte; Fragment Offset: ×8 bytes.

Exam shortcuts

  • 4–1–8 Multiplier Rule: HLEN × 4 = Header Bytes; Total Length × 1 = Total Bytes; Fragment Offset × 8 = Starting Payload Byte. — every IPv4 header numerical.
  • Max IPv4 Payload: 65,535 − 20 = 65,515 bytes (when HLEN = 5). — questions asking for the maximum data bytes in one IPv4 datagram.

Ready for the next idea?

Mark this item complete when you’ve finished reading and working through it.