Layer 3: IP, routing, ICMP and NAT
Routing decisions
Every host and router has a routing table. For each packet, it chooses the most specific matching route (longest prefix match):
$ ip route
default via 10.0.0.1 dev eth0 # 0.0.0.0/0 → gateway
10.0.0.0/24 dev eth0 proto kernel src 10.0.0.15 # local subnet: deliver directly
172.17.0.0/16 dev docker0 # Docker bridge
A packet to 10.0.0.50 matches /24 → sent directly on the LAN. A packet to 8.8.8.8 matches only default → sent to the default gateway (10.0.0.1), the router that handles anything not local.
ip route get 8.8.8.8 shows exactly which route and source address would be used.
Routers forward packets between networks based on IP routing tables; they decrement the IP TTL (time to live, a hop count) and drop packets that reach 0 — preventing loops.
ICMP
The control-message protocol of IP (layer 3, no ports):
- Echo request/reply —
ping. - Time exceeded — sent when TTL hits 0; traceroute exploits it by sending packets with TTL 1, 2, 3... and recording who replies at each hop.
- Destination unreachable — including "port unreachable" (for UDP) and "fragmentation needed" (PMTUD).
Many networks block ICMP — including AWS security groups by default — so "ping fails" doesn't prove a host is down. Test the actual port (nc -zv host 443) or use TCP traceroute (traceroute -T -p 443).
NAT
Network Address Translation rewrites addresses in packet headers:
- SNAT (source NAT) — rewrites the source. Many private hosts share one public IP for outbound traffic (with port translation: PAT/masquerading). Home routers, AWS NAT gateways, Docker's bridge, Kubernetes nodes for pod egress.
- DNAT (destination NAT) — rewrites the destination. Port forwarding, load balancers, Docker
-p 8080:80, Kubernetes Services (kube-proxy).
NAT keeps state in a connection tracking table: return traffic is translated back. Consequences:
- The internet can't initiate connections to private hosts behind SNAT — only outbound-initiated flows work.
- NAT devices have finite ports per destination; very high connection rates to the same destination exhaust them (AWS NAT gateway: up to 55,000 simultaneous connections per unique destination IP:port per NAT IP — you can add secondary IPs). Symptoms: intermittent connection timeouts under load,
ErrorPortAllocationin NAT gateway metrics. - NAT hides client IPs — proxies add
X-Forwarded-Forto preserve them at L7.
DHCP
DHCP automatically gives hosts their IP address, subnet mask, default gateway, DNS servers and lease time (DORA: Discover, Offer, Request, Acknowledge — over UDP 67/68). In AWS, the VPC's DHCP options set controls the DNS servers and domain name handed to instances.