Router
A router works at OSI Layer 3 (Network) and connects different networks together. A switch answers "which device inside this building?"; a router answers "which building?"
01What does a router do?
- Routing — picks the best path to the destination
- Network separation — connects different LANs, stops broadcasts
- NAT — translates private IPs to public ones
- DHCP — hands out IPs (on home routers)
- Firewall — basic traffic filtering (on home routers)
A home router is really five devices in one box: router + switch + access point + DHCP server + firewall.
02Routing
The routing table
code
Destination Interface Next hop
192.168.1.0/24 eth0 Directly connected
10.0.0.0/8 eth1 192.168.1.254
0.0.0.0/0 eth2 ISP Gateway
The last line is the default route: any packet matching no other rule leaves "toward the internet" through this door.
The routing process
- A packet arrives → destination IP is read
- The routing table is consulted
- The most specific match wins (longest prefix match)
- The packet departs through that interface
03Home vs enterprise router
| Feature | Home router | Enterprise router |
|---|---|---|
| Ports | 4–8 | 24+ |
| Speed | 1 Gbps | 10–100 Gbps |
| Features | NAT, DHCP, Wi-Fi all-in-one | Routing only |
| Management | Web UI | CLI / SSH |
04Static vs dynamic routing
Static routing
Written by hand, never changes. For small, predictable networks:
code
ip route 10.0.0.0/8 192.168.1.254
Dynamic routing
Routers learn routes from each other and find alternatives when a link dies:
- OSPF — medium/large enterprise networks
- BGP — the internet's backbone; route exchange between ISPs
- RIP — first generation, now mostly in textbooks
05What changes as a packet crosses a router?
code
[Ethernet Header][IP Header][Data]
↓ ↓
Changes Unchanged
Source MAC: becomes the router's MAC
Dest MAC: becomes the next hop's MAC
Source IP: stays original
Dest IP: stays original
The rule you learned earlier is at work here: MAC addresses are renewed hop by hop, IPs stay fixed end to end.
06Switch vs router
| Feature | Switch | Router |
|---|---|---|
| Layer | Layer 2 | Layer 3 |
| Looks at | MAC | IP |
| Broadcasts | Forwards | Stops |
| Used for | Inside a LAN | Between LANs |
07Real-world example
code
Internet
|
[Router] ← NAT, firewall
|
[Switch]
/ \
PC1 PC2
- PC1 wants to reach
google.com - The packet travels through the switch to the router
- The router applies NAT (private IP → public IP) and sends it out
- When the reply returns, the NAT table points back to PC1
08Summary
- Router = connects different networks, works at Layer 3
- Picks paths with a routing table + longest prefix match
- Stops broadcasts (every port is its own broadcast domain)
- A home router is five devices sharing one box