Computer Networks chapters (5)
  1. 01Reference Models & Network Layers
  2. 02Data Link Layer & MAC Protocols
  3. 03Network Layer & IP Addressing
  4. 04Transport Layer (TCP & UDP)
  5. 05Application Layer Protocols & Security

Chapter 03 · CN

Network Layer & IP Addressing

4 topics · 1 sections · beginner-friendly study notes · Updated

Share with friends:WhatsAppTelegram

Embed on your site

Free for teachers, schools and blogs. Please keep the attribution link.

Preview

The network layer delivers packets across multiple networks using logical IP addresses.

This chapter covers IPv4 classes and private ranges, subnetting and VLSM, CIDR and longest-prefix match, the IPv4 header and fragmentation, routing basics, ARP, ICMP and an introduction to IPv6.

Subnetting is the most reliably tested numerical area in networks for GATE and CCNA.

Designing office and cloud networks, configuring routers and VPCs, firewall rules, and diagnosing connectivity with ping and traceroute.

1

IPv4 addressing, classes and special addresses

An IPv4 address is a 32-bit logical address written in dotted-decimal (four octets). In classful addressing the leading bits define the class: A (0xxx, 1-126), B (10xx, 128-191), C (110x, 192-223), D multicast (1110, 224-239), E reserved (240-255).

In short: An IP address is like a postal address with two parts: which network, and which host in it.

Class blueprint → objects

Think about it

Every host and interface needs a unique address and mask to route traffic.

Default masks: A /8 (255.0.

0.0), B /16, C /24.

Hosts per network = 2^h - 2 (network and broadcast addresses are reserved).

Special addresses: 0.0.

0.0 (this host/default route), 127.0.

0.0/8 (loopback), 255.255.255.255 (limited broadcast), network address (host bits all 0), directed broadcast (host bits all 1).

Private ranges (RFC 1918): 10.0.

0.0/8, 172.16.0.

0/12 (172.16-172.31) and 192.168.0.

0/16, not routable on the public Internet; 169.254.0.

0/16 is link-local (APIPA).

Classful addressing wasted addresses, which led to subnetting and CIDR.

Key points

  • Network ID: Address with all host bits 0.
  • Directed broadcast: Address with all host bits 1, sent to all hosts of that network.
  • Loopback: 127.x.x.x refers to the local machine.
  • Class A: 2^7 - 2 networks, 2^24 - 2 hosts; Class B: 2^14 networks, 2^16 - 2 hosts; Class C: 2^21 networks, 254 hosts.
2

IPv4 Subnetting & CIDR

Subnetting borrows host bits to create smaller networks, described by a subnet mask. CIDR (classless inter-domain routing) writes an address with a prefix length, e.g. 192.168.10.0/26, independent of classes. VLSM allows subnets of different sizes inside one block.

In short: Cut a big plot of land into smaller plots; the mask tells how many leading bits identify the plot.

Think about it

Conserves addresses and shrinks routing tables.

Method: (1) find the prefix length; (2) host bits h = 32 - prefix; (3) hosts per subnet = 2^h - 2; (4) block size = 256 - mask octet value in the interesting octet; (5) network ID = address AND mask; (6) broadcast = network ID with host bits set to 1; (7) usable range = network+1 to broadcast-1.

Borrowing s bits from a block gives 2^s subnets.

For VLSM, allocate the largest subnet first, then the next largest, to avoid overlap.

CIDR route aggregation (supernetting): contiguous blocks that share a common leading prefix and total a power of two can be summarised, e.

g.

192.168.0.

0/24 to 192.168.3.

0/24 become 192.168.0.

0/22.

Routers use longest-prefix match: when several table entries match the destination, the one with the longest prefix length wins.

Common masks: /24 = 255.255.255.0, /25 = .

128, /26 = .

192, /27 = .

224, /28 = .

240, /29 = .

248, /30 = .

252 (2 usable hosts, used for point-to-point links).

Key points

  • Subnet mask: 32-bit value with 1s for network bits and 0s for host bits.
  • VLSM: Variable-length subnet masks: subnets of different sizes.
  • Supernetting: Combining contiguous networks into one shorter prefix.
  • Longest prefix match: Router picks the most specific matching route.
  • Usable hosts = 2^(32 - prefix) - 2. A /31 (RFC 3021) allows 2 hosts on point-to-point links; /32 is a single host.
  • Block size in the last octet: /25 = 128, /26 = 64, /27 = 32, /28 = 16, /29 = 8, /30 = 4.

Example

Code
Network ID calculation:
Address: 172.16.45.200/20
Mask     : 255.255.240.0
3rd octet: 45  = 00101101
mask octet 240 = 11110000
AND        = 00100000 = 32
Network  : 172.16.32.0
Broadcast: 172.16.47.255
Usable   : 172.16.32.1 - 172.16.47.254  (2^12 - 2 = 4094 hosts)
3

IPv4 Header Structure

An IPv4 header is 20 bytes (without options) with fields: version, IHL, type of service, total length, identification, flags (DF, MF), fragment offset, TTL, protocol, header checksum, source and destination addresses. Fragmentation splits a datagram larger than the link MTU into fragments that are reassembled only at the destination.

In short: If a parcel is too large for a road, cut it into pieces with numbered labels and reassemble at the end.

Parts that work together

Think about it

Different links have different MTUs, so IP must adapt packet size.

Each fragment carries its own IP header; the data size of every fragment except the last must be a multiple of 8 bytes because the fragment offset is measured in 8-byte units.

MF = 1 on all fragments except the last; the DF bit set means “do not fragment” and a router drops the packet and sends an ICMP error if it does not fit (used for path-MTU discovery).

TTL is decremented at every router and the packet is discarded at 0 (prevents loops), with an ICMP Time Exceeded message sent back.

The header checksum is recomputed at every hop because TTL changes.

Total length is 16 bits, so the maximum datagram size is 65535 bytes.

The protocol field identifies the upper-layer protocol: 6 = TCP, 17 = UDP, 1 = ICMP.

Key points

  • MF (More Fragments): Set on every fragment except the last.
  • Fragment offset: Position of fragment data in the original datagram in 8-byte units.
  • TTL: Hop limit decremented by each router.
4

Routing Algorithms (Dijkstra & OSPF)

Routing finds paths between networks; forwarding moves each packet to the next hop using the routing table. Distance-vector protocols (RIP) share their whole table with neighbours; link-state protocols (OSPF) flood link information and each router runs Dijkstra’s algorithm.

In short: Distance vector: ask neighbours how far things are. Link state: everyone gets the full map and computes their own routes.

Dijkstra: grow shortest distances from source

Think about it

Routers need up-to-date tables to forward packets across large networks.

A routing table entry has destination prefix, next hop, outgoing interface and metric.

The default route 0.0.

0.0/0 handles all unmatched destinations.

Distance vector uses the Bellman-Ford equation: d_x(y) = min over neighbours v of {c(x,v) + d_v(y)}; it converges slowly and suffers count-to-infinity, mitigated by split horizon, poison reverse and maximum hop limit (RIP: 15).

Link state converges faster and uses more memory and bandwidth.

Hierarchy: interior gateway protocols (RIP, OSPF) work inside an autonomous system; BGP is the path-vector exterior protocol between autonomous systems.

Static routes are manually configured.

DHCP gives hosts an address, mask, default gateway and DNS server automatically.

Key points

  • Autonomous System: Network under one administrative control with a common routing policy.
  • Next hop: Adjacent router to which a packet is forwarded.
  • Count to infinity: Slow convergence problem in distance-vector routing after a link failure.
  • RIP: distance vector, hop-count metric, max 15 hops (16 = infinity), updates every 30 s, UDP port 520. OSPF: link state, cost metric (inversely related to bandwidth), runs Dijkstra on its link-state database, areas (backbone area 0), multicast hellos, IP protocol 89, converges quickly. BGP: path vector between autonomous systems over TCP port 179.
  • OSPF operation: discover neighbours with Hello packets → exchange LSAs by flooding → every router builds the same link-state database (LSDB) → each router runs Dijkstra with itself as root → shortest-path tree gives the routing table. Designated Router (DR) reduces adjacencies on broadcast networks.
  • Dijkstra example: edges A–B 2, A–C 5, B–C 1, B–D 4, C–D 2, D–E 3, C–E 6. From A: B = 2, C = min(5, 2+1) = 3, D = min(2+4, 3+2) = 5, E = min(5+3, 3+6) = 8. Permanent order A, B, C, D, E; shortest path to E is A→B→C→D→E with cost 8.
  • Dijkstra needs non-negative weights; Bellman-Ford (distance vector) handles negative edges. Link-state: O(V²) per router and floods O(E) LSAs; distance vector: smaller messages but count-to-infinity.

Example

Code
Dijkstra algorithm:
/* Dijkstra shortest path from source s (used by OSPF / link-state) */
dist[s] = 0;  dist[v] = INF for all other v;  S = {};
while (S != all vertices) {
    u = vertex not in S with minimum dist[u];
    S = S + {u};
    for each neighbour v of u:
        if (dist[u] + w(u,v) < dist[v]) { dist[v] = dist[u] + w(u,v); prev[v] = u; }
}
/* Complexity: O(V^2) with array, O((V+E) log V) with a binary heap */
5

NAT & ARP Address Resolution

ARP (Address Resolution Protocol) resolves a known IPv4 address into the MAC address of a host on the same local network by broadcasting a request and receiving a unicast reply. NAT (Network Address Translation) rewrites IP addresses (and, in PAT, port numbers) in packets crossing a router so that many private hosts can share one public IPv4 address.

In short: ARP asks "who has this IP?" on the LAN; NAT is the border router that swaps private addresses for its own public one.

Think about it

ARP is how IP traffic actually reaches the right MAC on a LAN, and NAT stretches IPv4; both feature in GATE, CCNA and network-design interviews.

ARP procedure: host A (IP A, MAC a) wants to send to IP B on the same subnet.

It checks its ARP cache; on a miss it broadcasts an ARP request (destination MAC FF:FF:FF:FF:FF:FF, "who has B?

tell A") inside an Ethernet frame (type 0x0806).

Host B replies with a unicast ARP reply carrying its MAC; A caches it (typically minutes) and sends the IP packet.

If B is on another subnet, A applies its subnet mask, finds B is remote, and ARPs for the default gateway instead; the router re-ARPs on the next network.

Proxy ARP lets a router answer for remote hosts; Gratuitous ARP announces a host's own mapping (duplicate-IP detection, failover); ARP spoofing poisons caches (man-in-the-middle).

RARP (obsolete) mapped MAC to IP; DHCP replaced it.

NAT: a router with inside local addresses (e.

g.

192.168.1.

x) and an inside global address (203.0.

113.5).

Static NAT: one-to-one fixed mapping.

Dynamic NAT: pool of public addresses, first come first served.

PAT / NAT overload: many private hosts share one public address; the router keeps a translation table of (private IP:port ↔ public IP:port) and distinguishes flows by the rewritten source port, so up to about 65,535 simultaneous flows per public IP per destination.

The router also fixes the IP header checksum and the TCP/UDP checksum (which covers the pseudo-header).

Drawbacks: breaks end-to-end addressing and inbound connections (need port forwarding), problems with protocols that embed IP addresses (active FTP, SIP), and extra state in the router.

IPv6 removes the need for NAT.

Key points

  • ARP cache: Table of recent IP-to-MAC mappings with timeouts.
  • Gratuitous ARP: Unsolicited ARP announcing the sender's own IP-to-MAC mapping.
  • Inside local / inside global: Private address of an internal host / public address seen on the Internet.
  • PAT: Port address translation (NAT overload): one public IP shared by many hosts using different ports.
  • ARP request is a broadcast, ARP reply is a unicast; ARP works only inside one broadcast domain.
  • Sending from 192.168.1.10/24 to 8.8.8.8: destination is off-subnet → ARP for the default gateway 192.168.1.1; frame dest MAC = gateway MAC, packet dest IP = 8.8.8.8 (MAC changes per hop, IP does not except at NAT).

Example 1

Code
a /24 LAN of 200 hosts behind one public IP via PAT: the router needs one public address and up to ~64,000 table entries; with only a pool of 10 dynamic-NAT public addresses at most 10 hosts can go out at once.

Example 2

Code
ARP cache and PAT translation table:
# ARP on the local network
$ arp -a
192.168.1.1   at 00:1a:2b:3c:4d:5e  on eth0

# Linux NAT (PAT) on a gateway: masquerade private LAN behind the public interface
iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth0 -j MASQUERADE

# Router NAT table (conceptual)
Inside local        Inside global         Outside global
192.168.1.10:5000   203.0.113.5:40001     198.51.100.7:80
192.168.1.11:5000   203.0.113.5:40002     198.51.100.7:80
6

ARP, ICMP and IPv6 introduction

ARP maps an IPv4 address to a MAC address on the local network. ICMP carries error and diagnostic messages for IP (ping, traceroute). IPv6 uses 128-bit addresses written as eight groups of four hex digits.

In short: ARP asks “who has this IP?”; ICMP reports “host unreachable” or answers ping; IPv6 is the roomy successor to IPv4.

Think about it

IPv4 address exhaustion drives IPv6 adoption, and ARP/ICMP are everyday troubleshooting tools.

ARP: the sender broadcasts a request (destination FF:FF:FF:FF:FF:FF) and the owner replies with a unicast reply; results are cached.

To reach a host outside its subnet the sender ARPs for the default gateway’s MAC, not the remote host.

ICMP types: echo request (8) and reply (0), destination unreachable (3), time exceeded (11), redirect (5).

Ping uses echo messages; traceroute sends packets with increasing TTL to reveal each router.

IPv6: 40-byte fixed header, no fragmentation by routers (only the source fragments), no header checksum, no broadcast (uses multicast and anycast), built-in IPsec support, stateless autoconfiguration (SLAAC).

Address shortening: leading zeros in a group may be dropped and one run of all-zero groups replaced by :: once.

Transition mechanisms: dual stack, tunnelling, translation (NAT64).

Key points

  • ARP cache: Table of recently resolved IP-to-MAC mappings.
  • Dual stack: Hosts run IPv4 and IPv6 simultaneously.
  • SLAAC: Stateless address autoconfiguration in IPv6.
  • IPv6 address 2001:0db8:0000:0000:0000:0000:0000:0001 shortens to 2001:db8::1.

Solved examples

Example 01 · Intermediate

The block 192.168.10.0/24 must be divided into 4 equal subnets. Give the new mask, hosts per subnet, and each subnet’s network ID, usable range and broadcast address.

Given Data:
  • Block = 192.168.10.0/24
  • Required subnets = 4
Required To Find:

Mask, hosts per subnet, ranges

Formula Applied:—
Step-by-Step Solution:
Step 1: Need 4 subnets → borrow 2 bits (2^2 = 4). New prefix = 24 + 2 = /26 = 255.255.255.192.
Step 2: Host bits = 32 - 26 = 6 → hosts per subnet = 2^6 - 2 = 62. Block size = 256 - 192 = 64.
Step 3: Subnet 1: network 192.168.10.0, usable .1-.62, broadcast 192.168.10.63.
Step 4: Subnet 2: network 192.168.10.64, usable .65-.126, broadcast 192.168.10.127.
Step 5: Subnet 3: network 192.168.10.128, usable .129-.190, broadcast 192.168.10.191.
Step 6: Subnet 4: network 192.168.10.192, usable .193-.254, broadcast 192.168.10.255.
Final Answer: Mask 255.255.255.192 (/26); 62 usable hosts per subnet; networks .0, .64, .128, .192.

Approach note: Network IDs are multiples of the block size (64); broadcast = next network - 1.

Example 02 · Intermediate

Find the network address, broadcast address, valid host range and number of hosts for 172.16.45.200/20.

Given Data:
  • IP = 172.16.45.200
  • Prefix = /20 → mask 255.255.240.0
Required To Find:

Network, broadcast, range, hosts

Formula Applied:—
Step-by-Step Solution:
Step 1: Mask in 3rd octet: 240 (11110000), block size = 256 - 240 = 16.
Step 2: Third octet 45 lies in the block starting at 32 (32, 48 are multiples of 16; 45 is between 32 and 47).
Step 3: Network address = 172.16.32.0; next network is 172.16.48.0 so broadcast = 172.16.47.255.
Step 4: Valid hosts: 172.16.32.1 to 172.16.47.254.
Step 5: Host bits = 32 - 20 = 12 → hosts = 2^12 - 2 = 4094.
Final Answer: Network 172.16.32.0, broadcast 172.16.47.255, range .32.1 – .47.254, 4094 hosts.

Approach note: Use “block size” in the octet where the mask changes: multiples of 16 here.

Example 03 · Advanced

A datagram with total length 4000 bytes (20-byte header, 3980 bytes data) must cross a link with MTU 1500. Find the fragments (total length, offset, MF).

Given Data:
  • Header = 20 bytes
  • Data = 3980 bytes
  • MTU = 1500
Required To Find:

Fragment sizes, offsets and flags

Formula Applied:—
Step-by-Step Solution:
Step 1: MTU - header = 1500 - 20 = 1480 bytes; 1480 / 8 = 185 (an integer), so each full fragment carries 1480 data bytes.
Step 2: Fragment 1: data 1480, total length 1500, offset 0, MF = 1.
Step 3: Fragment 2: data 1480, total length 1500, offset 1480/8 = 185, MF = 1.
Step 4: Remaining data = 3980 - 2960 = 1020 bytes.
Step 5: Fragment 3: data 1020, total length 1040, offset 2960/8 = 370, MF = 0.
Final Answer: 3 fragments: (1500, offset 0, MF 1), (1500, offset 185, MF 1), (1040, offset 370, MF 0).

Approach note: Offsets are in 8-byte units; only the last fragment may carry a non-multiple of 8.

Example 04 · Advanced

A router has routes: 10.0.0.0/8 → R1, 10.1.0.0/16 → R2, 10.1.2.0/24 → R3, 0.0.0.0/0 → R4. Where is a packet to 10.1.2.77 forwarded? And to 10.1.9.1? And 11.1.1.1?

Given Data:
    Required To Find:

    Next hop for each destination

    Formula Applied:—
    Step-by-Step Solution:
    Step 1: 10.1.2.77 matches /8, /16 and /24 (and default). The longest prefix is /24 → R3.
    Step 2: 10.1.9.1 matches /8 and /16 (not /24, since third octet is 9). The longest is /16 → R2.
    Step 3: 11.1.1.1 matches only the default route → R4.
    Final Answer: 10.1.2.77 → R3; 10.1.9.1 → R2; 11.1.1.1 → R4.

    Approach note: List every matching entry, then pick the one with the largest prefix length.

    Real-world applications

    Networks

    Using Network Layer & IP Addressing in real projects

    Designing office and cloud networks, configuring routers and VPCs, firewall rules, and diagnosing connectivity with ping and traceroute.

    Mechanism: Write small programs, read errors carefully, then grow complexity only after the basics are solid.

    Common mistakes

    ❌ Myth: Every subnet has 2^h usable host addresses.

    Why this is wrong: The network address and the broadcast address cannot be assigned to hosts.

    ✅ Verified Truth: Usable hosts = 2^h - 2 (except special /31 and /32 cases).

    ⚠️ Exam Tip: Subtract 2 unless the question says otherwise.

    ❌ Myth: ARP is used to find the MAC of a remote host on another network.

    Why this is wrong: ARP works only on the local network; remote traffic goes via the default gateway.

    ✅ Verified Truth: The sender ARPs for the gateway’s MAC and the router repeats the process on the next network.

    ⚠️ Exam Tip: Draw the two MAC rewrites for a two-LAN path.

    ❌ Myth: IPv6 simply gives bigger addresses; everything else is unchanged.

    Why this is wrong: IPv6 removes the header checksum, router fragmentation and broadcast, and simplifies the header.

    ✅ Verified Truth: IPv6 has a fixed 40-byte header, SLAAC, multicast instead of broadcast and mandatory support for IPsec in implementations.

    ⚠️ Exam Tip: List 3 differences from IPv4 for a 3-mark answer.

    Revision notes

    The network layer delivers packets across multiple networks using logical IP addresses. This chapter covers IPv4 classes and private ranges, subnetting and VLSM, CIDR and longest-prefix match, the IPv4 header and fragmentation, routing basics, ARP, ICMP and an introduction to IPv6.

    High-Yield Quick Revision Points

    1. Classes: A 1–126, B 128–191, C 192–223, D 224–239
    2. Private: 10/8, 172.16/12, 192.168/16
    3. Hosts = 2^h - 2; block size = 256 - mask octet
    4. Network = IP AND mask; broadcast = host bits all 1
    5. Fragment offset in 8-byte units; MF=0 on last fragment
    6. Longest prefix match decides forwarding; ARP is local only
    7. IPv6: 128-bit, 40-byte header, no broadcast, :: once

    Practice questions

    Question 1 · MCQ (GATE / CCNA)

    The number of usable host addresses in a /27 subnet is:

    Question 2 · MCQ (GATE / CCNA)

    Which of the following is a private IPv4 address?

    Question 3 · MCQ (GATE)

    The broadcast address of the subnet containing 200.10.20.77/26 is:

    Question 4 · MCQ (GATE)

    Routers choose among multiple matching routes by:

    Question 5 · MCQ (GATE)

    In IPv4 fragmentation, the fragment offset field is measured in units of:

    Question 6 · MCQ (CCNA)

    Which protocol would a router use to tell a sender that the TTL expired?