Skip to content
GATEiit

GATE CSE · Computer Networks

IPv4: fragmentation, CIDR notation and network address translation (NAT)

The IPv4 header, fragmentation with offsets in 8-byte units, addresses, masks and CIDR blocks, subnetting, route aggregation and longest prefix match, and how NAT maps private addresses to a public one.

Before you start

  • Binary numbers →

    Masks, prefixes and block boundaries are read bit by bit; you convert octets between decimal and binary all the time.

  • MTU of a link →

    Fragmentation happens when a datagram is larger than the next link's MTU (1500 bytes for Ethernet).

  • Forwarding tables →

    CIDR prefixes are the entries of a forwarding table, matched by longest prefix.

Concept notes

1. The IPv4 header

An IPv4 datagram is a header of 20 to 60 bytes followed by data.

  • HLEN (4 bits): header length in 4-byte words, from 5 (20 bytes) to 15 (60 bytes).
  • Total length (16 bits): header plus data in bytes, at most 65,535. Data length = total length −4× HLEN.
  • Identification (16 bits): copied into every fragment of one datagram so the receiver can group them.
  • Flags (3 bits): reserved, DF (don't fragment) and MF (more fragments).
  • Fragment offset (13 bits): where this fragment's data starts in the original data, in units of 8 bytes.
  • TTL (8 bits): decremented by each router; the datagram is dropped at 0 and an ICMP time-exceeded message goes back.
  • Protocol (8 bits): which upper layer gets the data: 1 = ICMP, 6 = TCP, 17 = UDP.
  • Header checksum: covers the header only, so every router recomputes it after changing TTL.
  • Source and destination addresses, 32 bits each; then options.

IP is best effort and connectionless: no ACKs, no ordering, no retransmission.

IPv4 header08162431VersionHLENType of serviceTotal lengthIdentificationFlagsFragment offsetTTLProtocolHeader checksumSource IP addressDestination IP addressOptions and padding (0 to 40 bytes)20 Bfixed
IPv4 header, 32 bits per row. The first five rows are the fixed 20 bytes; options can add up to 40 more.

2. Fragmentation

Each link has a maximum transmission unit (MTU), the largest IP datagram (header included) its frames can carry. If a datagram is larger than the next link's MTU and DF = 0, the router splits it into fragments. If DF = 1, it drops the datagram and sends an ICMP "fragmentation needed" message.

Rules for each fragment:

  • It gets its own copy of the IP header (20 bytes if there are no options), with the same identification.
  • Its data part must be a multiple of 8 bytes, except in the last fragment. So the largest data per fragment is ⌊(MTU−20)/8⌋×8.
  • Offset = (byte position of its data in the original data) /8.
  • MF = 1 in every fragment except the last; the last has MF = 0 (and offset > 0).
  • Total length is set to its own header plus data.

Only the destination host reassembles; routers never do. A fragment can be fragmented again on a smaller link; offsets stay relative to the original datagram. If any fragment is lost, the destination discards the whole datagram after a timer.

With D data bytes and fragment payload P: number of fragments =⌈D/P⌉, and the extra bytes sent are 20× (number of fragments −1).

How to tell a fragment: MF = 1 or offset > 0. A datagram with MF = 0 and offset = 0 was not fragmented.

3. IPv4 addresses and CIDR notation

An IPv4 address is 32 bits written as four decimal octets. CIDR notation a.b.c.d/n says the first n bits are the network prefix and the last 32−n bits name a host. The mask has n ones then zeros, e.g. /26=255.255.255.192.

For a block /n:

  • network address = address AND mask (host bits all 0);
  • directed broadcast = host bits all 1;
  • usable hosts =232−n−2.

The old classful scheme fixed the prefix by the first bits: class A 0–127 (/8), B 128–191 (/16), C 192–223 (/24), D 224–239 multicast, E 240–255 reserved. CIDR (classless) lets n be any length, which wastes far fewer addresses.

Special addresses: private blocks 10.0.0.0/8, 172.16.0.0/12 (172.16 to 172.31) and 192.168.0.0/16, which are not routed on the public Internet; 127.0.0.0/8 loopback; 255.255.255.255 limited broadcast; 0.0.0.0 "this host".

A fast trick: in the octet where the prefix ends, the block size is 2(host bits in that octet). For /26 the last octet has 6 host bits, so blocks start at multiples of 64.

4. Subnetting, aggregation and longest prefix match

Subnetting borrows host bits to split a block: borrowing s bits from a /n gives 2s subnets of size /(n+s). Variable-length subnet masks (VLSM) give different sizes to different subnets: allocate the largest first, each on a boundary that is a multiple of its own size.

Aggregation (supernetting) goes the other way. 2k blocks of size /n can be summarised as one /(n−k) only if they are contiguous and the first one starts on a multiple of 2k blocks. For example 150.20.64.0/24 to 150.20.67.0/24 (four blocks, 64 is a multiple of 4) become 150.20.64.0/22.

Longest prefix match. A forwarding table may hold overlapping prefixes. For a destination, the router finds every entry whose prefix matches and uses the longest one (most specific). A default route 0.0.0.0/0 matches everything and is used only when nothing else does.

To check a match: convert only the octet where the prefix ends to binary, or check whether the address lies in the range [network, broadcast] of the entry.

Entry Range Next hop
172.16.0.0/16 172.16.0.0 – 172.16.255.255 A
172.16.64.0/18 172.16.64.0 – 172.16.127.255 B
172.16.96.0/19 172.16.96.0 – 172.16.127.255 C
0.0.0.0/0 everything D

172.16.100.5 matches all four; the longest is /19, so it goes to C.

5. Network address translation (NAT)

NAT lets many hosts with private addresses share one (or a few) public addresses. The NAT router sits between the private network and the Internet.

Most NAT is NAPT (port translation). For an outgoing packet the router:

  1. replaces the source IP with its public IP and the source port with a free port of its own;
  2. records (private IP, private port) ↔ (public IP, new port) in its translation table;
  3. recomputes the IP header checksum and the TCP/UDP checksum (which covers the IP addresses through a pseudo-header).

For an incoming reply, it looks up the destination port in the table and rewrites the destination back to the private IP and port.

Inside Public side
192.168.1.10:5000 203.0.113.7:40001
192.168.1.11:5000 203.0.113.7:40002

Consequences:

  • Port numbers are 16 bits, so one public address supports at most about 65,000 simultaneous mappings per transport protocol.
  • Outside hosts cannot start a connection to an inside host unless a mapping is set up by hand (port forwarding).
  • NAT breaks the end-to-end principle and layering: a network-layer box rewrites transport-layer ports. Protocols that carry IP addresses inside their data (such as FTP) need extra help.
  • It saves public IPv4 addresses and hides the inside network's structure.

Formulas to remember

Header lengthHLEN from 5 to 15
header bytes=4×HLEN
Data in a datagram
data=total length−4×HLEN
Largest fragment payloadH = IP header bytes, usually 20
P=⌊MTU−H8⌋×8
Fragment offset
offset=byte position of the fragment’s data8
Number of fragmentsD = data bytes of the original datagram
⌈DP⌉
Hosts in a /n block
232−n−2
Subnets from borrowing s bits
2s subnets of size /(n+s)

Common mistakes

  • Writing the fragment offset in bytes instead of 8-byte units.
  • Using MTU−20 as the payload without rounding down to a multiple of 8.
  • Forgetting that every fragment carries its own 20-byte header, so total bytes sent go up.
  • Reading HLEN as bytes; it counts 4-byte words, while total length counts bytes.
  • Choosing the first matching prefix instead of the longest one.
  • Aggregating blocks whose start is not aligned, for example 65.0/24 to 68.0/24 cannot become one /22.

Worked examples

Try each step yourself, then reveal the working.

Example 1: Fragmenting a datagram

numerical

A 2400-byte datagram (20-byte header, no options, DF = 0) must cross a link with MTU 1000 bytes. Give each fragment's data size, total length, offset and MF bit.

  1. Step 1: Find the data size and the largest fragment payload.

    Try it on paper first.

Example 2: Reading a CIDR address

numerical

A host has address 200.45.67.150/26. Find the network address, broadcast address, the range of usable host addresses and their number.

  1. Step 1: Where does the prefix end, and what is the block size?

    Try it on paper first.

Example 3: Longest prefix match

numerical

Using the table in the concept above (A 172.16.0.0/16, B 172.16.64.0/18, C 172.16.96.0/19, D default), find the next hop for 172.16.70.1, 172.16.200.1 and 10.0.0.1.

  1. Step 1: Check $172.16.70.1$ against each range.

    Try it on paper first.

Example 4: Following a packet through NAT

conceptual

Host 192.168.1.10 opens a TCP connection from port 5000 to a web server at 198.51.100.20:80 through a NAT router with public address 203.0.113.7. What do the outgoing and returning packets look like on the public side?

  1. Step 1: Rewrite the outgoing packet.

    Try it on paper first.

Previous year questions

22 official GATE CSE questions on this topic, newest first.

Practise these 22 PYQs →

Topic test

10 questions: up to 4 official PYQs and original practice questions, with GATE marking (MCQ wrong answers lose marks). Score 60% or more to pass. A new set is drawn each time.

Mark as mastered

Loading your progress…

My revision notes

Notes written by the GATEiit team against the official GATE CSE syllabus; being checked by a reviewer. Updated 2026-11-21. Spotted a mistake? Use Report an issue on any question, or contact us.