Opscraft
Networking fundamentals · Lesson 3 of 9 · 5 min read

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.

Enlarge diagramPacket to 10.0.0.50Packet to 8.8.8.810.0.0.0/24 dev eth0local subnet: deliver directly172.17.0.0/16 dev docker0the Docker bridgedefault via 10.0.0.10.0.0.0/0: send to the gateway routerrouting tablemost specific matchonly the default matches
Figure 1. How a host picks a route: the most specific matching entry wins (longest prefix match). The default route only catches what nothing else matches.
Swipe sideways to see all of it, or tap to open it full screen.

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).

Enlarge diagramYouRouter 1Router 2DestinationTTL = 1time exceeded: hop 1TTL = 2time exceeded: hop 2TTL = 3reply: destination reached
Figure 2. How traceroute works: send packets with TTL 1, 2, 3… Each router that drops one to zero sends back "time exceeded", revealing itself.
Swipe sideways to see all of it, or tap to open it full screen.

NAT

Network Address Translation rewrites addresses in packet headers:

Enlarge diagramSNAT · OUTBOUND: NAT GATEWAY, HOME ROUTER, POD EGRESSDNAT · INBOUND: LOAD BALANCER, DOCKER -P, KUBERNETES SERVICEapp serverfrom 10.0.1.5 : 51000NAT gatewayrewrites the sourceapi.example.comsees 52.1.2.3 : 62000clientto 203.0.113.10 : 443Load balancerrewrites thedestinationpodreceives at 10.0.2.7 :8080
Figure 3. The two kinds of NAT. SNAT changes who a packet seems to come from; DNAT changes where it goes.
Swipe sideways to see all of it, or tap to open it full screen.
  • 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, ErrorPortAllocation in NAT gateway metrics.
  • NAT hides client IPs — proxies add X-Forwarded-For to 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.

Enlarge diagramNew machineno IP yetDHCP server1. Discover: any DHCP server? (broadcast)2. Offer: you can have 10.0.1.253. Request: I will take 10.0.1.254. Acknowledge: lease, gateway, DNS servers
Figure 4. DHCP in four messages (Discover, Offer, Request, Acknowledge): how a new machine gets its IP address, gateway and DNS servers.
Swipe sideways to see all of it, or tap to open it full screen.