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.
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 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 .
Offset (byte position of its data in the original data) .
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 data bytes and fragment payload : number of fragments , and the extra bytes sent are (number of fragments ).
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 says the first bits are the network prefix and the last bits name a host. The mask has ones then zeros, e.g. .
For a block :
network address = address AND mask (host bits all 0);
directed broadcast = host bits all 1;
usable hosts .
The old classful scheme fixed the prefix by the first bits: class A – (), B – (), C – (), D – multicast, E – reserved. CIDR (classless) lets be any length, which wastes far fewer addresses.
Special addresses: private blocks , ( to ) and , which are not routed on the public Internet; loopback; limited broadcast; "this host".
A fast trick: in the octet where the prefix ends, the block size is . For 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 bits from a gives subnets of size . 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. blocks of size can be summarised as one only if they are contiguous and the first one starts on a multiple of blocks. For example to (four blocks, 64 is a multiple of 4) become .
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 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
–
A
–
B
–
C
everything
D
matches all four; the longest is , 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:
replaces the source IP with its public IP and the source port with a free port of its own;
records (private IP, private port) ↔ (public IP, new port) in its translation table;
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
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
Data in a datagram
Largest fragment payload = IP header bytes, usually 20
Fragment offset
Number of fragments = data bytes of the original datagram
Hosts in a /n block
Subnets from borrowing s bits
Common mistakes
Writing the fragment offset in bytes instead of 8-byte units.
Using 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 to cannot become one .
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.
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 . Find the network address, broadcast address, the range of usable host addresses and their number.
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 , B , C , D default), find the next hop for , and .
Step 1: Check $172.16.70.1$ against each range.
Try it on paper first.
Example 4: Following a packet through NAT
conceptual
Host opens a TCP connection from port 5000 to a web server at :80 through a NAT router with public address . What do the outgoing and returning packets look like on the public side?
Step 1: Rewrite the outgoing packet.
Try it on paper first.
Previous year questions
22 official GATE CSE questions on this topic, newest first.
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.