#Why this matters
Earlier lessons showed how switches forward Ethernet frames by MAC address within one LAN and how CIDR splits IP space into subnets. Real networks connect many subnets through routers, translate private RFC 1918 addresses onto public links, and slice shared switches into separate security zones.
Misconfigured routes send traffic out the wrong gateway, careless port forwarding exposes internal services, and flat switches let one compromised workstation scan or ARP-spoof critical servers.
#Routing tables and longest prefix match
Every IP host and router keeps a routing table mapping destination prefixes to an outgoing interface and next-hop IP. At each Layer 3 hop, a router strips the incoming Ethernet header, inspects the destination IP, selects a route, and builds a new frame addressed to the next hop's MAC address while preserving the IP addresses (unless NAT is active).
When multiple entries match a destination IP, the kernel uses longest prefix match (LPM): the route with the largest prefix length (/24 beats /16, which beats /0) always wins regardless of row order. The 0.0.0.0/0 entry (::/0 in IPv6) is the default gateway, used only when no more specific prefix matches.
$ ip route
default via 10.10.10.1 dev eth0 proto static
10.10.10.0/24 dev eth0 proto kernel scope link src 10.10.10.25
10.10.20.0/24 via 10.10.10.254 dev eth0
$ traceroute -n 198.51.100.10
traceroute to 198.51.100.10 (198.51.100.10), 30 hops max, 60 byte packets
1 10.10.10.1 0.421 ms 0.388 ms 0.351 ms
2 192.0.2.1 1.914 ms 1.872 ms 1.840 ms
3 198.51.100.10 4.210 ms 4.185 ms 4.150 msTraffic to 10.10.10.50 goes directly on eth0 via ARP (scope link), traffic to 10.10.20.15 goes via 10.10.10.254, and 198.51.100.10 falls back to default. Passing -n to traceroute disables reverse DNS so you see raw hop IPs immediately.
#Gateways and Network Address Translation (NAT)
Private RFC 1918 IPv4 ranges (10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16) are not routable on the internet. A gateway running Network Address Translation (NAT) rewrites IP addresses and ports in packet headers and tracks sessions in a state table so replies return to the original host.
| NAT type | Direction | Rewrite | Use |
|---|---|---|---|
| SNAT / Masquerade | Outbound | Private src IP/port to public IP/port | Sharing one public IP |
| DNAT / Port forward | Inbound | Public dst IP/port to internal IP/port | Publishing a service |
NAT is an addressing mechanism, not a firewall. SNAT hides internal hosts behind one public IP, yet compromised hosts can still open outbound connections, and upstream logs show only the gateway's IP unless NAT translation logs are kept. Every DNAT rule also creates a direct inbound path that requires strict host hardening.
#VLAN segmentation and 802.1Q tagging
On an unsegmented switch, all ports share one broadcast domain: any host can ARP-spoof neighbours, and perimeter firewalls never inspect East-West traffic. VLANs (Virtual Local Area Networks) divide a switch into isolated Layer 2 broadcast domains using IEEE 802.1Q tags, which insert a 4-byte header carrying a 12-bit VLAN ID (1 to 4094).
- Access ports attach end hosts to one VLAN using untagged frames.
- Trunk ports link switches or connect a switch to a router, preserving 802.1Q tags across multiple allowed VLANs.
Traffic between VLAN 10 (10.10.10.0/24) and VLAN 20 (10.10.20.0/24) cannot cross at Layer 2; it must pass through a router or Layer 3 switch, creating a natural choke point for firewall inspection.
#Defending and common mistakes
- Treating NAT as a security boundary: Combine NAT with stateful egress rules and retain translation logs (
src_ip:port,nat_ip:port, timestamp) for incident response. - Asymmetric routing: Outbound traffic leaving via router A while replies return via router B causes stateful firewalls to drop unknown flows.
- Leaving default VLAN 1 active on trunks and unused ports: Disable unused ports or assign them to a dead VLAN, and restrict trunks to explicit VLAN IDs to prevent VLAN hopping.
- Misreading
* * *intraceroute -n: Stars at hop 2 followed by replies at hop 3 mean hop 2 filters ICMP Time Exceeded replies, not that packets are dropped.
#Summary
Routing tables select paths via longest prefix match and fall back to the default gateway only when no specific CIDR prefix matches. Gateways use stateful SNAT and DNAT to translate private RFC 1918 addresses, which requires translation logging and hardening of exposed ports. IEEE 802.1Q VLANs isolate Layer 2 broadcast domains on shared switches and force inter-VLAN traffic through a Layer 3 gateway where security controls apply.