Sub topic 5
Layer 3: Network, IP addressing and routing
This chapter assumes you know what an IP address, a router, and a MAC address are, and that you can run simple terminal commands. Layer 2 moves frames between devices on the same link. Layer 3 moves packets between networks, so that two hosts with no direct link can still talk.
What Layer 3 does
The Network layer has three jobs:
- Logical addressing. Every host gets an IP address that identifies it and the network it belongs to.
- Routing. Routers choose the next hop for each packet based on its destination address.
- Forwarding and fragmentation. Routers pass packets along and, for IPv4, may split them if a link's maximum transmission unit (MTU) is too small.
Layer 3 is best-effort. IP does not guarantee delivery, order, or absence of duplicates. Reliability is added by Layer 4 (TCP) or by the application.
IP addresses and subnets
An IPv4 address has 32 bits, written as four decimal numbers, such as 192.168.1.25. A subnet mask or prefix length splits the address into a network part and a host part. In 192.168.1.25/24, the first 24 bits name the network (192.168.1.0) and the last 8 bits name the host.
Python's standard library can do this arithmetic:
import ipaddress
iface = ipaddress.ip_interface("192.168.1.25/24")
net = iface.network
print(net) # 192.168.1.0/24
print(net.netmask) # 255.255.255.0
print(net.broadcast_address) # 192.168.1.255
print(net.num_addresses - 2) # 254 usable hosts
print(ipaddress.ip_address("192.168.1.200") in net) # True
print(ipaddress.ip_address("192.168.2.7") in net) # FalseA host compares the destination address with its own network. If the destination is inside the same network, the host sends the packet directly, using ARP to find the MAC address. If not, it sends the packet to its default gateway, which is a router.
IPv6 uses 128-bit addresses, written in hexadecimal such as 2001:db8::1. It removes broadcast, drops router fragmentation, and uses Neighbor Discovery instead of ARP. The routing logic is the same.
Private and special addresses
| Range | Purpose |
|---|---|
| `10.0.0.0/8`, `172.16.0.0/12`, `192.168.0.0/16` | Private networks, not routed on the public internet |
| `127.0.0.0/8` | Loopback, traffic stays on the host |
| `169.254.0.0/16` | Link-local, self-assigned when DHCP fails |
Hosts with private addresses reach the internet through network address translation (NAT), usually done by the home or office router.
How routing works
Each host and router keeps a routing table. For each packet, the device finds the entry whose prefix matches the destination. If several match, it uses the longest prefix match, meaning the most specific route wins. The entry 0.0.0.0/0 is the default route and matches everything.
import ipaddress
routes = {
"10.0.0.0/8": "router-A",
"10.1.0.0/16": "router-B",
"0.0.0.0/0": "default-gateway",
}
def next_hop(dest):
addr = ipaddress.ip_address(dest)
matches = [ipaddress.ip_network(p) for p in routes if addr in ipaddress.ip_network(p)]
best = max(matches, key=lambda n: n.prefixlen)
return routes[str(best)]
print(next_hop("10.1.5.9")) # router-B
print(next_hop("10.9.9.9")) # router-A
print(next_hop("8.8.8.8")) # default-gatewayRoutes come from three sources: directly connected networks, static routes set by an administrator, and dynamic routing protocols such as OSPF (inside an organization) and BGP (between organizations).
At each hop, the router removes the incoming Layer 2 frame, reads the IP header, picks the next hop, and builds a new frame. The MAC addresses change at every hop. The source and destination IP addresses stay the same, except where NAT rewrites them.
The IP header fields that matter
- Source and destination address: who sent it and where it goes.
- TTL (IPv4) or Hop Limit (IPv6): a counter that each router decrements. At zero the packet is dropped and the router sends back an ICMP "Time Exceeded" message. This stops routing loops from lasting forever.
- Protocol: identifies the Layer 4 payload, such as 6 for TCP, 17 for UDP, 1 for ICMP.
ICMP, the Internet Control Message Protocol, reports errors and powers ping and traceroute. It travels inside IP, so it is a Layer 3 helper protocol.
Inspecting Layer 3 on Linux
ip addr show # your IP address and prefix
ip route show # the routing table, including the default route
ip route get 8.8.8.8 # which route and next hop would be used
traceroute example.com # each router on the path, found using TTL expiryCommon errors
- Wrong prefix length. A host with
/24that should have/16thinks distant peers are off-network and sends them to the gateway. - Missing or wrong default gateway. The host reaches local machines but nothing beyond.
- Duplicate IP addresses. Two hosts claim one address, giving intermittent failures.
- Missing return route. Packets arrive, but replies have no path back.
- ICMP blocked by a firewall.
pingfails even though the service works, so a failed ping does not always mean a Layer 3 fault.
Exercise
A host has the address 10.1.4.20/16 and a routing table with 10.0.0.0/8 via router-A, 10.1.0.0/16 via router-B, and a default route via router-C. Which next hop is used for a packet to 10.1.200.3, and which for 10.2.0.9?
Show answer
For 10.1.200.3 the most specific matching route is 10.1.0.0/16, so the packet goes via router-B. For 10.2.0.9 only 10.0.0.0/8 and the default route match. The /8 is longer than /0, so it goes via router-A.
Exercise
A packet travels through three routers from Host A to Host B. Which of these change at each hop: source IP, destination IP, source MAC, destination MAC (ignoring NAT)?
Show answer
The source and destination MAC addresses change at each hop, because each link has its own Layer 2 frame. The source and destination IP addresses stay the same.