Skip to content
Chethan Yadav_
Computer Networks

CN/Lesson 3 of 8/7 min read

Network Layer and IP Addressing

IPv4 header, IP address classes, private addresses, subnetting with worked examples, CIDR and supernetting, NAT, fragmentation, ICMP, DHCP, and IPv6.


On this page

Role of the network layer

The network layer moves packets from source host to destination host across multiple networks.

  • Logical addressing (IP addresses).
  • Routing: choosing the path (control plane, see routing).
  • Forwarding: moving a packet from an input to the right output port of a router (data plane).
  • Fragmentation when a link's MTU is smaller than the packet.

IP is connectionless and best effort: no guarantee of delivery, order or no duplication. Reliability is left to TCP.

IP Address

An IP address identifies a network interface at layer 3. IPv4 addresses are 32 bits, written in dotted decimal: 192.168.10.5. An address has a network part and a host part.

IPv4

IPv4 header (20 bytes minimum):

FieldPurpose
Version4
IHLHeader length in 32-bit words
Type of Service / DSCPPriority, QoS
Total LengthHeader + data, max 65535
Identification, Flags, Fragment OffsetFragmentation and reassembly
TTLHop limit
ProtocolPayload type: 1 ICMP, 6 TCP, 17 UDP
Header ChecksumHeader only, recomputed each hop
Source / Destination Address32 bits each
OptionsRare

TTL

Time To Live is decremented by 1 at every router; when it hits 0 the packet is dropped and an ICMP "time exceeded" is sent back. Prevents routing loops from circulating packets forever. traceroute exploits this by sending packets with TTL 1, 2, 3, ...

IP Address Classes

Classful addressing (historic, replaced by CIDR in 1993, but still asked):

ClassFirst bitsFirst octet rangeDefault maskNetworks / hosts
A01 to 126/8 (255.0.0.0)126 nets, 2^24 - 2 hosts each
B10128 to 191/16 (255.255.0.0)16384 nets, 2^16 - 2 hosts
C110192 to 223/24 (255.255.255.0)~2 million nets, 254 hosts
D1110224 to 239n/aMulticast
E1111240 to 255n/aReserved

127.0.0.0/8 is loopback (127.0.0.1 = localhost). 0.0.0.0 means "this host" / any address.

Private IP Address

Reserved ranges not routed on the public internet (RFC 1918), used inside homes and organizations behind NAT:

  • 10.0.0.0/8
  • 172.16.0.0/12 (172.16.0.0 to 172.31.255.255)
  • 192.168.0.0/16

Also 169.254.0.0/16 is link-local (APIPA, self-assigned when DHCP fails) and 100.64.0.0/10 is carrier-grade NAT.

Subnet Mask

A subnet mask marks which bits are the network part (1s) and which are the host part (0s). 255.255.255.0 = /24.

  • Network address = IP AND mask (all host bits 0).
  • Broadcast address = all host bits 1.
  • Usable hosts = 2^(host bits) - 2.

Subnetting

Subnetting borrows host bits to split one network into smaller subnets: less broadcast traffic, better security and address use.

Worked example 1

Split 192.168.1.0/24 into 4 subnets.

Need 2 extra bits (2^2 = 4), so /26 (mask 255.255.255.192), block size 256 - 192 = 64.

SubnetNetworkUsable hostsBroadcast
1192.168.1.0.1 to .62192.168.1.63
2192.168.1.64.65 to .126192.168.1.127
3192.168.1.128.129 to .190192.168.1.191
4192.168.1.192.193 to .254192.168.1.255

Each has 2^6 - 2 = 62 usable hosts.

Worked example 2

Which subnet is 172.16.45.130/20 in?

  • /20 mask = 255.255.240.0; third octet block size 256 - 240 = 16.
  • 45 falls in the block 32 to 47.
  • Network 172.16.32.0, broadcast 172.16.47.255, hosts 172.16.32.1 to 172.16.47.254, 2^12 - 2 = 4094 hosts.

Quick prefix table

PrefixMaskHosts
/24255.255.255.0254
/25255.255.255.128126
/26255.255.255.19262
/27255.255.255.22430
/28255.255.255.24014
/29255.255.255.2486
/30255.255.255.2522 (point-to-point links)

VLSM (variable length subnet masking) gives each subnet a different prefix length to fit its size, wasting fewer addresses.

CIDR

Classless Inter-Domain Routing drops classes; any prefix length is allowed: 203.0.113.0/22. This slowed IPv4 exhaustion and shrank routing tables.

Supernetting

Supernetting (route aggregation) combines contiguous networks into one shorter prefix. 192.168.0.0/24 through 192.168.3.0/24 aggregate to 192.168.0.0/22.

Routers forward using longest prefix match: if a packet matches both 10.0.0.0/8 and 10.1.0.0/16, the more specific /16 wins.

NAT

Network Address Translation lets many devices with private addresses share one public address.

  • PAT / NAT overload (most common, home routers): the router rewrites the source IP and source port of outgoing packets and keeps a translation table to map replies back.
Inside 192.168.1.10:5000  ->  router  ->  Outside 203.0.113.7:62001  ->  server
  • Static NAT: one private address permanently mapped to one public address (for servers).
  • Dynamic NAT: private addresses mapped to a pool of public ones.

Pros: saves IPv4 addresses, hides internal structure. Cons: breaks end-to-end connectivity (inbound connections need port forwarding), complicates peer-to-peer and some protocols (needs STUN/TURN for WebRTC).

Fragmentation

MTU

The Maximum Transmission Unit is the largest packet a link can carry (1500 bytes for Ethernet).

If a packet is larger than the next link's MTU, an IPv4 router fragments it. Fragments share the Identification field; the fragment offset (in 8-byte units) and the More Fragments (MF) flag let the destination reassemble. If the Don't Fragment (DF) flag is set, the router drops the packet and sends ICMP "fragmentation needed": this is how Path MTU Discovery works.

Example: a 4000-byte packet (20 header + 3980 data) over MTU 1500. Each fragment carries at most 1480 data bytes: 1480 + 1480 + 1020, offsets 0, 185, 370.

IPv6 routers never fragment; only the source does.

ICMP

ICMP (Internet Control Message Protocol) carries error and diagnostic messages for IP (protocol number 1).

TypeMessage
0 / 8Echo reply / echo request (ping)
3Destination unreachable (network, host, port, fragmentation needed)
5Redirect
11Time exceeded (TTL expired, used by traceroute)

DHCP

DHCP (Dynamic Host Configuration Protocol) automatically gives a host its IP address, subnet mask, default gateway and DNS servers. Uses UDP ports 67 (server) and 68 (client).

The DORA process:

  1. Discover: client broadcasts looking for a server (source 0.0.0.0).
  2. Offer: server offers an address.
  3. Request: client broadcasts that it accepts that offer.
  4. Acknowledge: server confirms and starts the lease.

Clients renew at 50% of the lease time. A DHCP relay agent forwards broadcasts to a server on another subnet. Rogue DHCP servers are a classic LAN attack (DHCP snooping on switches prevents it).

IPv6

IPv6 uses 128-bit addresses (2^128 addresses), written as 8 groups of hex: 2001:0db8:0000:0000:0000:ff00:0042:8329, shortened to 2001:db8::ff00:42:8329 (drop leading zeros, replace one run of zero groups with ::).

Differences from IPv4:

IPv4IPv6
32-bit128-bit
Variable header with options and checksumFixed 40-byte header, extension headers, no checksum
Routers fragmentOnly the source fragments
ARPNeighbor Discovery Protocol (ICMPv6)
BroadcastNo broadcast, multicast and anycast instead
DHCP or manualSLAAC (stateless autoconfiguration) or DHCPv6
NAT commonDesigned for end-to-end, no NAT needed

Address types: global unicast (2000::/3), link-local (fe80::/10), unique local (fc00::/7, like private), multicast (ff00::/8), loopback ::1.

Transition: dual stack (run both), tunneling (IPv6 inside IPv4), translation (NAT64).

Quick revision

  • Network = IP AND mask; hosts = 2^h - 2.
  • Private ranges: 10/8, 172.16/12, 192.168/16.
  • Longest prefix match decides forwarding.
  • NAT maps private IP:port to public IP:port.
  • DHCP = DORA over UDP 67/68.
  • IPv6: 128-bit, no broadcast, no router fragmentation, NDP replaces ARP.