Notes › Computer Networks
Routing and the Network Layer
The network layer (layer 3) moves packets from the source host to the destination host across many networks. Its address is the IP address; its devices are routers. (Addresses and subnet maths: IP Addressing and Subnetting.)
Forwarding vs routing
- Forwarding (data plane): a router looks at a packet's destination IP and sends it out of the right interface — nanoseconds, in hardware.
- Routing (control plane): routers talk to each other to build the routing table — runs in software, every few seconds/minutes.
The routing table and longest prefix match
Destination Next hop Interface
10.0.0.0/8 192.168.1.2 eth1
10.1.0.0/16 192.168.1.3 eth2
10.1.5.0/24 192.168.1.4 eth3
0.0.0.0/0 203.0.113.1 eth0 ← default route (internet)
For destination 10.1.5.20, all of the first three match; the router picks the most specific (longest prefix): /24 → eth3. If nothing matches, the default route is used.
IPv4 packet — key header fields
| Field | Use |
|---|---|
| Version, header length | 4, usually 20 bytes |
| TTL | decremented at each router; at 0 the packet is dropped (stops loops) |
| Protocol | 6 = TCP, 17 = UDP, 1 = ICMP |
| Source / destination IP | 32-bit addresses |
| Identification, flags, fragment offset | fragmentation when a packet is bigger than the link MTU (1500 bytes on Ethernet) |
| Header checksum | errors in the header |
Routing algorithms
| Distance vector | Link state | |
|---|---|---|
| Knowledge | each router knows only its neighbours' distance tables | every router knows the whole map |
| Shares | its whole table, with neighbours only | its link info, flooded to all routers |
| Algorithm | Bellman–Ford | Dijkstra |
| Convergence | slow; count-to-infinity problem | fast |
| Protocol | RIP | OSPF, IS-IS |
Count-to-infinity fixes: split horizon, poison reverse, maximum hop count (RIP's 16 = unreachable). Shortest-path algorithms themselves: Shortest Paths and Minimum Spanning Tree.
Routing protocols
| Protocol | Type | Used | Metric |
|---|---|---|---|
| RIP | distance vector, interior | small networks | hop count (max 15) |
| OSPF | link state, interior | inside a company / ISP | cost (based on bandwidth) |
| BGP | path vector, exterior | between ISPs — the internet's routing | policies, AS path |
- The internet is made of Autonomous Systems (AS) — networks run by one organisation (ISP, Google, a university), each with an AS number.
- IGP (RIP, OSPF) routes inside an AS; BGP routes between ASes.
Static vs dynamic routing
Static: an admin types routes — simple, predictable, doesn't adapt. Dynamic: protocols update routes automatically when links fail.
Helper protocols
ARP — IP → MAC (inside a LAN)
To send to 192.168.1.7 on the same network, a host needs its MAC address. It broadcasts "Who has 192.168.1.7?"; the owner replies with its MAC; the answer is cached in the ARP table (arp -a). For an IP outside the subnet, the host sends the frame to the default gateway's MAC.
DHCP — get an IP automatically (UDP 67/68)
DORA: Discover (client broadcast) → Offer (server proposes an IP) → Request (client accepts) → Acknowledge. The client gets an IP, subnet mask, default gateway and DNS server for a lease time.
ICMP — errors and diagnostics
pinguses Echo Request / Echo Reply.traceroutesends packets with TTL = 1, 2, 3… and reads the Time Exceeded messages to list each router on the path.- "Destination unreachable" messages report failures.
NAT — many devices, one public IP
Home devices have private IPs (10.x, 172.16–31.x, 192.168.x). The router rewrites the source to its one public IP and a port, remembering the mapping:
192.168.1.5:51000 ⇄ 49.36.10.2:62001 ⇄ server 142.250.0.1:443
- ✅ Saves IPv4 addresses, hides the inside network.
- ❌ Outsiders can't start connections to inside devices (needs port forwarding; peer-to-peer needs tricks like STUN/TURN).
IPv4 vs IPv6
| IPv4 | IPv6 | |
|---|---|---|
| Size | 32 bits (~4.3 billion) | 128 bits (3.4 × 10³⁸) |
| Written as | 192.168.1.1 |
2001:db8::1 |
| Header | variable, has checksum | fixed 40 bytes, no checksum |
| Address setup | DHCP or manual | SLAAC (auto) or DHCPv6 |
| NAT | common | not needed |
| Fragmentation | routers and hosts | only the sender |
| Broadcast | yes | no (multicast instead) |
Interview one-liners
- Router = layer 3, uses IP; switch = layer 2, uses MAC.
- Routing tables use longest prefix match.
- RIP = distance vector (Bellman–Ford); OSPF = link state (Dijkstra); BGP = between ASes.
- TTL prevents packets from looping forever.
- ARP: IP → MAC; DHCP: gives an IP (DORA); NAT: private ↔ public.
- Related: OSI and TCP/IP Models, Network Devices and Common Ports.