Skip to content

The Network Layer

IPv4 Addressing

On a LAN, devices communicate using MAC addresses (Layer 2). But MAC addresses are flat - they aren’t hierarchical and can’t be used for routing across networks. IP addresses solve this.

IPv4 Address Format

An IPv4 address is a 32-bit number written as four octets in dotted decimal notation:

192.168.1.100
│ │ │ │
8 8 8 8 bits = 32 bits total
Each octet = 8 bits = 0 to 255
  • IP addresses belong to networks, not devices. A laptop gets a new IP when it joins a new network.
  • Dynamic IP (via DHCP) - automatically assigned, common for clients
  • Static IP - manually configured, used for servers and network equipment

IPv4 Datagram Structure

Every IP packet (called a datagram) has a header with these fields:

FieldSizePurpose
Version4 bitsIP version (4 for IPv4)
Header Length (IHL)4 bitsHeader size (usually 20 bytes / 5 words)
Type of Service (DSCP)8 bitsQuality of Service (QoS) priority marking
Total Length16 bitsEntire datagram size (max 65,535 bytes)
Identification16 bitsGroups fragments of the same original datagram
Flags3 bitsControls fragmentation (DF = Don’t Fragment, MF = More Fragments)
Fragment Offset13 bitsPosition of this fragment in the original datagram
TTL (Time to Live)8 bitsHop counter - decremented at each router, dropped at 0
Protocol8 bitsUpper-layer protocol (6=TCP, 17=UDP, 1=ICMP)
Header Checksum16 bitsError detection for the header only
Source IP32 bitsSender’s IP address
Destination IP32 bitsRecipient’s IP address
OptionsVariableOptional - used for testing/debugging (rarely used)
PaddingVariableZeros to ensure header aligns to 32-bit boundary

Fragmentation occurs when a datagram exceeds the MTU (Maximum Transmission Unit) of a link (typically 1500 bytes for Ethernet). The IP layer splits it into smaller fragments, each with the same Identification field. The receiving host reassembles them.

Terminal window
# Check the MTU of an interface
ip link show eth0 | grep mtu
# mtu 1500
# Test path MTU to a destination (avoid fragmentation issues)
ping -c 4 -s 1472 -M do 8.8.8.8
# -s 1472 + 28 bytes overhead = 1500 (exact MTU)
# If fragmentation needed, you'll get an ICMP error

Before CIDR, IP addresses were divided into classes based on the first few bits:

ClassFirst BitsNetwork / Host SplitRangeHosts per NetworkUse
A08 / 240.0.0.0 - 127.255.255.25516,777,214Large ISPs, governments
B1016 / 16128.0.0.0 - 191.255.255.25565,534Medium-large organizations
C11024 / 8192.0.0.0 - 223.255.255.255254Small networks
D1110N/A224.0.0.0 - 239.255.255.255N/AMulticast
E1111N/A240.0.0.0 - 255.255.255.255N/AReserved / experimental

ARP bridges Layer 2 and Layer 3. When a device knows the destination IP (Layer 3) but not the MAC address (Layer 2), it uses ARP to find it.

How ARP works:

  1. Device broadcasts an ARP request: “Who has IP 192.168.1.1? Tell 192.168.1.50
  2. The device with that IP responds with an ARP reply: “I’m 192.168.1.1, my MAC is a4:c3:f0:2b:7e:91
  3. The requesting device caches this in its ARP table (entries expire after a timeout to account for network changes)

ARP Request/Reply Flow

Terminal window
# View the ARP table
ip neigh show
# or the legacy command:
arp -a
# Manually add a static ARP entry (rarely needed)
sudo ip neigh add 192.168.1.1 lladdr aa:bb:cc:dd:ee:ff dev eth0
# Clear the ARP cache
sudo ip neigh flush all
# On Windows
arp -a

Before modern routing, IPs were handed out in rigid classes (A,B,C). It was massively wasteful; a company needing 300 addresses would get a Class B with 65,534 hosts, wasting over 65,000 addresses. Subnetting solves this by splitting a large network into smaller, manageable pieces, each with its own gateway router.

Internet
Core Router
┌────┼────┐
│ │ │
Subnet Subnet Subnet
/26 /26 /26
(62) (62) (62) hosts each

A subnet mask is a 32-bit number that tells you where the network/subnet portion ends and the host portion begins:

Subnet MaskBinaryCIDRNetwork BitsHost BitsUsable Hosts
255.0.0.011111111.00000000.00000000.00000000/882416,777,214
255.255.0.011111111.11111111.00000000.00000000/16161665,534
255.255.255.011111111.11111111.11111111.00000000/24248254
255.255.255.12811111111.11111111.11111111.10000000/25257126
255.255.255.19211111111.11111111.11111111.11000000/2626662
255.255.255.22411111111.11111111.11111111.11100000/2727530
255.255.255.24011111111.11111111.11111111.11110000/2828414
255.255.255.25211111111.11111111.11111111.11111100/303022

Subnetting

Given 192.168.1.0/26:

  • Network bits = 26, Host bits = 6
  • Subnet increment = 2^6 = 64
  • Subnets: 192.168.1.0/26, 192.168.1.64/26, 192.168.1.128/26, 192.168.1.192/26
  • First subnet usable range: 192.168.1.1 to 192.168.1.62 (broadcast: 192.168.1.63)
Terminal window
# Quick subnet calculation with ipcalc
ipcalc 192.168.1.0/26
# Network: 192.168.1.0/26
# Netmask: 255.255.255.192 = 26
# Wildcard: 0.0.0.63
# HostMin: 192.168.1.1
# HostMax: 192.168.1.62
# Broadcast: 192.168.1.63
# Hosts/Net: 62
# View your subnet information
ip addr show eth0
# inet 192.168.1.50/24 brd 192.168.1.255 scope global eth0

Classful addressing was wasteful. A company needing 300 IPs couldn’t use a /24 (254 hosts) and had to request a /16 (65,534 hosts) - wasting 65,000+ addresses. CIDR removed the fixed class boundaries.

CIDR

CIDR uses Variable Length Subnet Masking (VLSM) - the network/host boundary can be placed anywhere in the 32 bits, not just at the 8/16/24-bit boundaries.

Classful:
Class A = /8
Class B = /16 Only 3 options
Class C = /24
CIDR:
/1, /2, /3 ... /30, /31, /32 Any prefix length

Key benefits:

  • No address waste - allocate exactly the number of IPs needed (e.g., /27 for 30 hosts)
  • Route aggregation (supernetting) - combine multiple networks into one route entry (e.g., two /24s become one /23), shrinking routing tables
  • Flexible network design - networks can be any size, not just A/B/C
CIDRSubnet MaskTotal IPsUsable Hosts
/8255.0.0.016,777,21616,777,214
/16255.255.0.065,53665,534
/20255.255.240.04,0964,094
/24255.255.255.0256254
/25255.255.255.128128126
/26255.255.255.1926462
/27255.255.255.2243230
/28255.255.255.2401614
/30255.255.255.25242
/32255.255.255.25511

Routing

A router sits between networks and decides where to forward each packet based on its destination IP:

  1. Packet arrives on one interface
  2. Router reads the destination IP from the IP header
  3. Router looks up the destination in its routing table
  4. Router forwards the packet out the interface closest to the destination

Every router maintains a routing table with these core columns:

ColumnWhat it contains
Destination NetworkNetwork ID + subnet mask (CIDR notation)
Next HopIP of the next router to forward to (or “directly connected”)
MetricCost/distance of this route (lower = preferred)
InterfaceWhich of the router’s interfaces to send the packet out of
Terminal window
# View the routing table on Linux
ip route show
# default via 192.168.1.1 dev eth0 proto dhcp metric 100
# 192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.50
# Add a static route
sudo ip route add 10.0.0.0/8 via 192.168.1.254
# Windows
route print

Routers don’t know about every network by default - they learn routes dynamically by talking to neighboring routers using routing protocols.

Interior Gateway Protocols (IGP) - Within an Autonomous System

Section titled “Interior Gateway Protocols (IGP) - Within an Autonomous System”

Routing Protocols

ProtocolTypeHow it worksScale
RIP (Routing Information Protocol)Distance vectorCounts hops (max 15). Sends full routing table to neighbors every 30s.Small networks (fewer than 100 routers)
EIGRP (Enhanced Interior Gateway)Advanced distance vectorUses bandwidth + delay as metrics. Cisco-proprietary (now open).Medium networks
OSPF (Open Shortest Path First)Link stateEvery router has a complete map of the network. Uses Dijkstra’s algorithm.Large enterprise networks
IS-IS (Intermediate System)Link stateSimilar to OSPF. Used by ISPs and very large networks.ISP-scale networks

Distance vector protocols are simpler but slower to converge. Routers only know what their neighbors tell them (“I can reach 10.0.0.0 in 3 hops”).

Link state protocols are more complex but converge faster. Every router has the full topology map and independently calculates the best path.

Exterior Gateway Protocol (EGP) - Between Autonomous Systems

Section titled “Exterior Gateway Protocol (EGP) - Between Autonomous Systems”

BGP

BGP (Border Gateway Protocol) is the only EGP in use today. It’s the protocol that makes the internet work - every ISP, cloud provider, and large organization uses BGP to exchange routing information.

  • An Autonomous System (AS) is a collection of networks under one administrative entity
  • Each AS gets an ASN (Autonomous System Number) assigned by IANA/regional registries
  • BGP routers exchange path attributes to determine the best route across the internet

RFC 1918 reserves three IP ranges for private/internal use. These IPs are never routed on the public internet - IGPs route them internally, but EGPs (BGP) reject them.

RangeCIDRTotal IPsTypical Use
10.0.0.0 - 10.255.255.25510.0.0.0/816.7 millionLarge enterprise networks, cloud VPCs
172.16.0.0 - 172.31.255.255172.16.0.0/12~1 millionMedium organizations, Docker default
192.168.0.0 - 192.168.255.255192.168.0.0/1665,536Home networks, small offices

Devices on private IPs reach the internet through NAT (Network Address Translation), which rewrites the private source IP to the router’s public IP on outgoing packets.

Routable (Public) IPsNon-Routable (Private) IPs
Forwarded between networks by routersNot forwarded beyond the local network
Globally unique, assigned by RIRsCan be reused in any private network
Directly accessible from the internetRequires NAT for internet access
Needs firewall protectionInherently isolated from the internet

Layer 3 is an elegant choreography of local discovery, private isolation, and global routing.

Network Layer