By the end of this chapter you'll be able to…

  • 1Name the IPv4 header fields and explain why the checksum covers only the header
  • 2Convert between prefix length, subnet mask and usable host count
  • 3Subnet a given block into a required number of equal subnets and give exact address ranges
  • 4Aggregate contiguous blocks and state the alignment condition that makes it valid
  • 5Apply longest prefix match to a forwarding table with overlapping entries
  • 6Fragment a datagram for a given MTU, computing offsets in 8-byte units
  • 7Explain why one lost fragment destroys the whole datagram and how path MTU discovery avoids it
  • 8State which IP and MAC addresses appear in each frame along a two-hop path
  • 9Describe the DHCP four-message exchange and the ICMP messages behind ping and traceroute
  • 10Explain what NAT rewrites and the two classes of thing it breaks
💡
Why this chapter matters in GATE
IP promises only best-effort delivery to an address anywhere, and every other mechanism here creates addresses, translates them, or repairs a gap that promise leaves. GATE tests this arithmetically through subnet range computation, CIDR aggregation, longest prefix match and fragment offsets, plus the conceptual ARP question about which addresses change at each hop.

Before you start — revise these

🔗
Binary representation of octets and bitwise AND
🔗
Powers of two up to 2 to the 32
🔗
Framing and MAC addressing from the data link layer

The Network Layer

The network layer makes one promise and deliberately makes no others.

The organising fact is that IP offers best-effort delivery of a datagram to an address anywhere, and every other mechanism in this chapter either creates addresses, translates between them, or repairs a gap that the promise leaves.

Best-effort means no guarantee of delivery, ordering, or timing, and no notification when a datagram is dropped. ICMP exists precisely because that silence is sometimes unacceptable.

ARP translates an IP address into a link-layer address. DHCP hands out addresses. NAT rewrites them. Fragmentation repairs the mismatch between what IP allows and what a link can carry.

The second organising fact is that an IP address names an interface, not a machine, and its structure is a prefix identifying a network plus a suffix identifying a host within it.

The third is that forwarding uses longest prefix match, which is what allows a general route and a specific exception to coexist in one table.

1. The IPv4 Datagram

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

FieldSizePurpose
Version4 bitsAlways 4 for IPv4
IHL4 bitsHeader length in 4-byte words, so 5 means 20 bytes
Type of service8 bitsDifferentiated services and congestion notification
Total length16 bitsHeader plus data, capping a datagram at 65,535 bytes
Identification16 bitsGroups fragments of one original datagram
Flags3 bitsDon't fragment and more fragments
Fragment offset13 bitsPosition in the original, in units of 8 bytes
Time to live8 bitsDecremented per hop; discarded at zero
Protocol8 bitsWhich transport protocol the payload belongs to
Header checksum16 bitsCovers the header only, recomputed at every hop
Source and destination32 bits eachThe endpoints

Two design decisions are worth pausing on. The checksum covers only the header, because the payload is the transport layer's responsibility and recomputing a full checksum at every hop would be prohibitive.

The time to live field is decremented by each router, and a datagram reaching zero is discarded with an ICMP notification, which is what prevents routing loops from consuming the network forever.

2. Addressing and CIDR

Classful addressing divided the space into fixed blocks: class A with an 8-bit prefix, class B with 16, and class C with 24. The rigidity wasted enormous numbers of addresses, since an organisation needing 300 hosts had to take a class B block of 65,534.

Classless inter-domain routing replaced it with an explicit prefix length. An address is written as a.b.c.d/n, where the first bits are the network prefix.

The block contains addresses, of which two are unusable: the all-zeros host part is the network address and the all-ones host part is the directed broadcast.

So a prefix of length supports hosts.

The subnet mask is ones followed by zeros, and a host computes its network address by taking the bitwise AND of its address with the mask.

Subnetting borrows bits from the host part to create several smaller networks inside one block. Supernetting or aggregation merges adjacent blocks into a shorter prefix, which is what keeps global routing tables from exploding.

Longest prefix match is the forwarding rule. When several entries match a destination, the one with the longest prefix wins, which lets a specific route override a general one without any explicit priority field.

3. Fragmentation

Every link has a maximum transmission unit, and a datagram larger than the next link's MTU must be fragmented or discarded.

Three header fields control it. Identification is copied to every fragment so the destination can group them. The more-fragments flag is set on all but the last. The offset gives the fragment's position in the original payload.

The offset is measured in units of 8 bytes, because 13 bits must address a payload of up to 65,515 bytes, and .

Consequently every fragment except the last must carry a payload that is a multiple of 8 bytes.

Reassembly happens only at the destination, never at intermediate routers, because different fragments may take different paths.

A lost fragment costs the whole datagram, since reassembly cannot complete, which is one reason fragmentation is avoided in practice through path MTU discovery.

Setting the don't-fragment flag makes a router discard an oversized datagram and return an ICMP message naming the MTU, which is how path MTU discovery learns the limit.

4. ARP, DHCP, ICMP and NAT

ARP resolves an IP address to a link-layer address on the same subnet. The requester broadcasts a query and the owner replies by unicast, and both cache the result.

ARP only ever resolves addresses on the local subnet. To reach a remote destination, a host resolves the address of its default gateway, so the frame's destination MAC is the router's while the datagram's destination IP remains the final target.

That distinction is examined constantly: the IP addresses stay fixed end to end while the MAC addresses change at every hop.

DHCP assigns addresses dynamically through a four-message exchange: discover, offer, request and acknowledge. It runs over UDP on ports 67 and 68, and the first messages are broadcast because the client has no address yet.

ICMP reports errors and provides diagnostics, carried inside IP as protocol 1. Destination unreachable, time exceeded, echo request and echo reply are the messages to know.

Ping uses echo request and reply. Traceroute uses time exceeded, sending datagrams with time to live 1, then 2, and so on, so each router in turn reports itself.

ICMP never makes IP reliable. It reports failures; it does not retransmit.

An ICMP error carries the failed datagram's header and first eight payload bytes, which is exactly enough for the source to identify the transport connection responsible, since the first eight bytes of a TCP or UDP header contain both port numbers.

NAT rewrites addresses and ports at a boundary, letting many private hosts share one public address. The translation table maps an internal address and port to an external port.

NAT breaks the end-to-end principle, since an outside host cannot initiate a connection inward without explicit configuration, and protocols embedding addresses in their payload need special handling.

Special and Private Addresses

Three ranges are reserved for private use and are never routed on the public internet: 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16.

The loopback block is 127.0.0.0/8, of which 127.0.0.1 is the familiar address, and traffic to it never leaves the host.

169.254.0.0/16 is link-local, self-assigned when DHCP fails, which is why a machine with no network shows an address in that range.

255.255.255.255 is the limited broadcast, never forwarded by a router.

IPv6

IPv6 uses 128-bit addresses, ending exhaustion as a design concern, and simplifies the header rather than merely enlarging it.

The header is a fixed 40 bytes with no checksum and no fragmentation fields. Removing the checksum avoids recomputation at every hop, and fragmentation is pushed to the source through mandatory path MTU discovery.

Options move into extension headers chained after the fixed header, so a router that does not need them skips them entirely.

ARP is replaced by neighbour discovery, which runs over ICMPv6 rather than as a separate link-layer protocol, and broadcast is replaced by multicast throughout.

5. Worked Examples

Example 1. An organisation is given 200.10.16.0/20 and needs 8 equal subnets. Compute the new prefix length, the addresses per subnet, and the range of the third subnet.

Eight subnets need 3 additional bits, since .

The new prefix length is .

Each subnet holds addresses, of which are usable by hosts.

The block starts at 200.10.16.0, and each subnet spans 512 addresses, which is two full values of the third octet.

Subnet 0 covers 200.10.16.0 to 200.10.17.255.

Subnet 1 covers 200.10.18.0 to 200.10.19.255.

Subnet 2, the third one, covers 200.10.20.0 to 200.10.21.255.

Its network address is 200.10.20.0 and its broadcast is 200.10.21.255, so the usable host range is 200.10.20.1 to 200.10.21.254.

The mask for a /23 is 255.255.254.0, since 23 ones is eight, eight, then seven ones and one zero, and 11111110 is 254.

Example 2. A router has these entries. To which interface does a datagram for 192.168.5.130 go?

PrefixInterface
192.168.0.0/16A
192.168.4.0/22B
192.168.5.128/25C
0.0.0.0/0D

Test each entry by masking the destination.

Against /16, the first 16 bits of the destination are 192.168, which matches. Entry A matches.

Against /22, the mask covers the first 22 bits, so the third octet is masked with 11111100, which is 252. The destination's third octet is 5, and , matching the prefix's 4. Entry B matches.

Against /25, the mask covers the first 25 bits, so the fourth octet is masked with 128. The destination's fourth octet is 130, and , matching. Entry C matches.

Entry D, the default route, matches everything.

Four entries match, and longest prefix match selects the longest, which is the /25.

The datagram goes to interface C.

This is exactly the mechanism that lets a specific exception override a general rule without any priority or ordering field in the table. Adding a more specific route is how traffic is redirected, and removing it restores the previous behaviour automatically.

Example 3. A 4000-byte datagram, including a 20-byte header, must cross a link with an MTU of 1500 bytes. Compute the fragments.

The payload is bytes.

Each fragment carries a 20-byte header, so at most bytes of payload.

The payload per fragment must be a multiple of 8, and 1480 is , so 1480 is usable directly.

Fragment 1 carries bytes 0 to 1479, offset 0, more-fragments set.

Fragment 2 carries bytes 1480 to 2959. Its offset is , more-fragments set.

Fragment 3 carries the remaining bytes, that is bytes 2960 to 3979. Its offset is , more-fragments clear.

Total bytes on the wire are , against 4000 originally, so fragmentation added 40 bytes of header overhead.

All three fragments carry the same identification value, which is how the destination groups them.

If fragment 2 is lost, fragments 1 and 3 are useless. The destination holds them until a reassembly timer expires and then discards everything, and IP sends no notification to the source about the individual fragment.

That is why path MTU discovery is preferred: setting don't-fragment and learning the limit avoids the fragility entirely.

Example 4. Host A at 10.0.1.5 sends to host B at 10.0.2.9 through router R, which has interfaces 10.0.1.1 and 10.0.2.1. State the source and destination IP and MAC addresses in each frame.

Host A first determines that B is not on its subnet, by masking both addresses and finding different network numbers.

So A must send the datagram to its default gateway, which is R at 10.0.1.1.

A uses ARP to learn R's MAC address on the 10.0.1.0 subnet, broadcasting a request for 10.0.1.1 and receiving R's reply.

In the first frame, from A to R: source IP is 10.0.1.5, destination IP is 10.0.2.9, source MAC is A's, destination MAC is R's 10.0.1.1 interface.

Note carefully that the destination IP is B's, not R's. The datagram is addressed to its final destination throughout; only the frame is addressed to the next hop.

R receives the frame, strips it, decrements the time to live, and consults its routing table.

R then uses ARP on the 10.0.2.0 subnet to learn B's MAC address.

In the second frame, from R to B: source IP is still 10.0.1.5, destination IP is still 10.0.2.9, source MAC is R's 10.0.2.1 interface, destination MAC is B's.

The rule to carry away: IP addresses are end to end and never change; MAC addresses are hop by hop and change at every router.

The only exception is NAT, which deliberately rewrites the IP addresses and is therefore not a router in the pure sense.

Example 5. Four blocks are allocated to one organisation: 200.10.8.0/24, 200.10.9.0/24, 200.10.10.0/24 and 200.10.11.0/24. Can they be aggregated, and what is the resulting prefix?

Write the third octets in binary. 8 is 00001000, 9 is 00001001, 10 is 00001010, 11 is 00001011.

The four values share their first six bits, 000010, and differ only in the last two.

Since the first two octets are identical, the common prefix is bits.

The aggregate is 200.10.8.0/22, covering 200.10.8.0 to 200.10.11.255, which is exactly the four blocks and nothing more.

Two conditions had to hold, and both are essential.

The blocks must be contiguous, which 8 through 11 are.

The count must be a power of two and the first block must be aligned to that boundary, which 8 is, since and the block count is 4.

Had the blocks been 9 through 12, aggregation would fail. Their common prefix would be only /20, covering 200.10.0.0 to 200.10.15.255, which includes eleven blocks the organisation does not own, and advertising it would attract traffic for addresses belonging to others.

This alignment requirement is why address allocation is done in aligned power-of-two blocks, and it is the entire reason the global routing table is merely large rather than unmanageable.

Example 6. A NAT router has one public address 203.0.113.7. Two internal hosts, 192.168.1.10 and 192.168.1.11, each open a connection to 93.184.216.34 port 80 from their own source port 5000. Describe the translation and one thing it breaks.

Both internal connections have the same source port, 5000, which would collide if the router simply substituted its own address.

So NAT rewrites the port as well, allocating a distinct external port for each connection.

The table holds two entries: internal 192.168.1.10 port 5000 mapped to external 203.0.113.7 port 40001, and internal 192.168.1.11 port 5000 mapped to external port 40002.

Outgoing datagrams have their source address and port rewritten, and the checksums must be recomputed, since both the IP header and the transport header change.

Incoming datagrams are matched by destination port against the table and rewritten back.

What this breaks is inbound connection establishment. An outside host cannot initiate a connection to 192.168.1.10, because no table entry exists until the internal host sends something first, and the router has nothing to match on.

A second breakage is protocol-specific. Any protocol that carries an IP address inside its payload, as classic FTP does in its port command, sends a private address the outside world cannot route, so NAT must inspect and rewrite the payload too.

The general principle is that NAT violates the end-to-end argument by putting connection state in the middle of the network, which is exactly what IP was designed to avoid, and it is tolerated because it postponed IPv4 exhaustion by decades.

Summary

IP promises best-effort delivery to an address anywhere, and everything else here creates addresses, translates them, or repairs a gap the promise leaves.

The header is 20 to 60 bytes. The checksum covers the header only, because the payload belongs to the transport layer. Time to live prevents loops from consuming the network.

CIDR writes an address as a prefix length. A /n block holds addresses and usable hosts, since the all-zeros and all-ones host parts are reserved. Subnetting borrows host bits; aggregation merges aligned, contiguous, power-of-two blocks into a shorter prefix.

Longest prefix match lets a specific route override a general one with no priority field.

Fragment offset is in 8-byte units, so every fragment but the last carries a multiple of 8 bytes. Reassembly happens only at the destination, and one lost fragment destroys the whole datagram. Don't-fragment plus ICMP gives path MTU discovery.

ARP resolves only local addresses, so a host off-subnet resolves its gateway instead. IP addresses are end to end and unchanged; MAC addresses change at every hop.

DHCP uses discover, offer, request and acknowledge over UDP ports 67 and 68. ICMP reports errors without repairing them, and traceroute exploits time exceeded.

NAT rewrites addresses and ports, requires checksum recomputation, prevents unsolicited inbound connections, and breaks protocols that embed addresses in payloads.

The private ranges are 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16; 127.0.0.0/8 is loopback and 169.254.0.0/16 is the self-assigned link-local block that appears when DHCP fails.

IPv6 has 128-bit addresses and a fixed 40-byte header with no checksum and no fragmentation fields, moving options into chained extension headers and replacing ARP with neighbour discovery over ICMPv6.

Key formulas & results

Everything to memorise for the exam hall, in one card. Screenshot this for revision.

The organising principle
IP offers best-effort delivery to an address anywhere; everything else creates, translates or repairs
Best effort means no guarantee of delivery, ordering or timing, and no notification on loss, which is why ICMP exists.
Block size and host count
a /n prefix holds 2 to the (32 minus n) addresses and 2 to the (32 minus n) minus 2 usable hosts
The all-zeros host part is the network address and the all-ones host part is the directed broadcast.
Subnetting bits
creating k subnets needs ceiling(log base 2 of k) borrowed bits, giving prefix length n plus that
Each resulting subnet holds 2 to the (32 minus new n) addresses.
Aggregation condition
blocks must be contiguous, a power-of-two count, and the first must be aligned to that boundary
Blocks 8 to 11 aggregate to a /22; blocks 9 to 12 do not aggregate at all without covering addresses you do not own.
Longest prefix match
among all matching entries, forward on the one with the longest prefix
This is what lets a specific exception override a general route with no priority field in the table.
Fragment offset unit
offset is in units of 8 bytes, so 13 bits address 65,536 bytes
Every fragment except the last must therefore carry a payload that is a multiple of 8 bytes.
Fragments needed
payload per fragment = largest multiple of 8 not exceeding (MTU minus header)
Each fragment gets its own 20-byte header, so fragmentation adds 20 bytes per extra fragment.
Address change rule
IP addresses are end to end and unchanged; MAC addresses change at every hop
The only exception is NAT, which deliberately rewrites IP addresses and is therefore not a pure router.
ICMP error payload
an ICMP error carries the failed IP header plus the first 8 payload bytes
Eight bytes is exactly enough to include both port numbers of a TCP or UDP header, so the source can identify the connection.
Private and special ranges
10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16 private; 127.0.0.0/8 loopback; 169.254.0.0/16 link-local
A link-local address appearing on a machine means DHCP failed and the host self-assigned.
⚠️

Traps GATE sets — and how to dodge them

These are the exact option-traps and misreads that cost marks under negative marking.

WATCH OUT
Forgetting to subtract two when counting usable hosts
Subtract the network address and the directed broadcast. A /24 has 256 addresses but 254 hosts.
Why it happens: The block size is the number the arithmetic produces, and the reserved addresses are easy to overlook.
WATCH OUT
Giving the fragment offset in bytes
Divide the byte position by 8. A fragment starting at byte 1480 has offset 185.
Why it happens: Every other length field in the header is in bytes, so the unit change is unexpected.
WATCH OUT
Letting a non-final fragment carry a payload that is not a multiple of 8
Round the payload down to the nearest multiple of 8. Only the last fragment may have any length.
Why it happens: The MTU minus header figure is used directly without checking divisibility.
WATCH OUT
Putting the router's IP address as the destination in the first frame
The destination IP is the final host throughout. Only the destination MAC is the router's, which is exactly the distinction the question is testing.
Why it happens: The frame is going to the router, so its address seems like the destination.
WATCH OUT
Aggregating blocks that are contiguous but misaligned
Check that the first block's number is divisible by the count. Blocks 9 to 12 give a /20 covering eleven blocks you do not own.
Why it happens: Contiguity looks like the only requirement, and the resulting prefix appears to cover them.
WATCH OUT
Choosing the first matching route instead of the longest
IP forwarding always selects the longest matching prefix regardless of table order, which is why a default route never wins when anything else matches.
Why it happens: Table order suggests precedence, as in a firewall rule list.
WATCH OUT
Assuming a router reassembles fragments before forwarding
Fragments may take different paths, so only the destination can reassemble. Intermediate routers may fragment further but never reassemble.
Why it happens: It has all the pieces passing through it, so reassembly there seems natural.
WATCH OUT
Believing ICMP retransmits lost datagrams
ICMP reports and nothing more. Recovery is entirely the transport layer's or the application's problem.
Why it happens: It reports the loss, so it appears to be doing something about it.

Exam-pattern practice

PYQ-style questions with full solutions. Work through them as a readiness check — mark yourself honestly and get your gap report at the end.

Readiness check

Are you exam-ready for Network Layer: IPv4, CIDR, Fragmentation, ARP, DHCP, ICMP & NAT?

10 problems from this chapter. Try each one, reveal the worked solution, mark yourself honestly — get your gap report at the end.

10 questions~7 min

5-minute revision

The whole chapter, distilled. Read this the night before the exam.

  • IP promises best effort only; ICMP reports failures but never repairs them
  • Header is 20 to 60 bytes; the checksum covers the header only and is recomputed per hop
  • Time to live prevents routing loops from consuming the network
  • A /n block holds 2 to the (32 minus n) addresses and two fewer usable hosts
  • Subnetting borrows host bits; k subnets need ceiling of log base 2 of k bits
  • Aggregation requires contiguity, a power-of-two count and alignment
  • Longest prefix match, not table order, decides forwarding
  • Fragment offset is in 8-byte units, so all but the last fragment carry a multiple of 8
  • Only the destination reassembles; routers may fragment further but never reassemble
  • One lost fragment destroys the datagram; path MTU discovery avoids fragmentation entirely
  • ARP resolves local addresses only; off-subnet traffic resolves the default gateway
  • IP addresses are end to end; MAC addresses change at every hop
  • DHCP: discover, offer, request, acknowledge, over UDP ports 67 and 68
  • Ping uses echo request and reply; traceroute uses time exceeded with increasing TTL
  • An ICMP error carries the failed header plus 8 payload bytes, enough for both port numbers
  • NAT rewrites address and port, recomputes checksums, blocks inbound initiation, and breaks payload-embedded addresses
  • Private: 10/8, 172.16/12, 192.168/16. Loopback 127/8. Link-local 169.254/16
  • IPv6: 128-bit addresses, fixed 40-byte header, no checksum, no fragmentation fields, extension headers, neighbour discovery

GATE question blueprint

How this topic is asked, tier by tier — so you can prep to the pattern.

Typical weightage: 7

Question styleMarks eachTypical countWhat it tests
Addressing and CIDR21
Fragmentation21
Forwarding11
ARP and support protocols11
IPv4 header11

Exam-hall strategy

Battle-tested tips from mentors and toppers for this topic under the sectional clock.

  1. Convert prefix lengths to block sizes immediately, since the arithmetic then becomes octet counting rather than binary work. For subnetting questions, compute addresses per subnet first and step through the ranges rather than trying to reason about masks directly. In longest prefix match questions, mask the destination against each entry and record every match before choosing, because the wrong options are always shorter matches that also succeed. For fragmentation, compute the payload per fragment as a multiple of 8 before anything else, and give offsets in 8-byte units. When a question shows a two-hop path, write four rows for the two frames and fill IP and MAC separately, since the whole mark is in keeping them distinct.

Beyond the exam

Where this skill shows up in the job you're competing for — and in life.

Every cloud provider's virtual private cloud is a CIDR bl…

Every cloud provider's virtual private cloud is a CIDR block that the operator subnets, and choosing a prefix too small is a mistake that is painful to undo later

Path MTU discovery failures caused by firewalls that drop…

Path MTU discovery failures caused by firewalls that drop ICMP are a classic production problem, presenting as connections that establish and then hang on the first large transfer

Carrier-grade NAT places a second translation layer betwe…

Carrier-grade NAT places a second translation layer between subscribers and the internet, compounding every problem in this chapter for peer-to-peer applications

Kubernetes assigns each pod an IP from a cluster CIDR and…

Kubernetes assigns each pod an IP from a cluster CIDR and relies on longest prefix match plus NAT at the node boundary, so the mechanisms here decide whether pods can reach each other

BGP route aggregation is what keeps the global routing ta…

BGP route aggregation is what keeps the global routing table around a million entries rather than tens of millions, and misaligned advertisements are a recurring source of routing incidents

Where else this topic is tested

Prepare once, score in every exam that asks it.

GATE CS
GATE DA
UGC NET Computer Science
ISRO Scientist SC
BARC Computer Science

Questions aspirants ask

Pulled from the Q&A community and mentor sessions.

Because the time to live changes at every hop, so the checksum would otherwise fail immediately. This per-hop recomputation is pure overhead in the forwarding path, which is exactly why IPv6 removed the checksum entirely and relies on the link layer below and the transport layer above.

Because it solved the address exhaustion problem a decade before IPv6 deployment became practical, and it did so without changing a single host. The costs, mainly the loss of inbound reachability, turned out to be acceptable for the client-server traffic that dominates, and some operators even count the resulting inbound blocking as a security feature.

Because its failure mode is disproportionate. A single lost fragment wastes the bandwidth already spent on the others, the destination must hold partial datagrams and time them out, and firewalls often cannot inspect non-first fragments since they lack the transport header. Path MTU discovery avoids all of this, which is why IPv6 removed router fragmentation entirely.

It masks both its own address and the destination with its subnet mask and compares the results. Equal means local, so ARP resolves the destination directly; unequal means remote, so the datagram goes to the default gateway. This single comparison is what determines whether ARP asks for the destination or for the router.

Because IP provides no way to ask a router to identify itself. Sending a datagram with time to live 1 forces the first router to discard it and report a time exceeded message, which reveals its address. Incrementing the value walks the path one hop at a time, turning an error mechanism into a diagnostic tool.

It would with a naive search, which is why routers use tries, compressed prefix trees or ternary content-addressable memory that compares against every entry in one cycle. Aggregation matters here too: keeping the global table at hundreds of thousands rather than millions of entries is what keeps the hardware feasible.
Header Logo