DEVOPS, SRE AND CLOUD · PART 2 · NETWORKING FOR DEVOPS · ROUTING · 31 OF 187

Routing, ARP and Default Gateways Explained

How a packet finds its way, built for real: two networks joined by a Linux router made from network namespaces. Read a routing table and the longest-prefix rule, see "Network is unreachable" without a default route, 100% loss until ip_forward=1, watch ARP ask "who-has 10.0.1.1", follow TTL dropping from 64 to 63 per hop, and trace the path with traceroute.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
1 / 11

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • Longest prefix wins
  • default gateway
  • ip_forward
  • ARP who-has
  • TTL 64 → 63
  • traceroute
  • return routes

Notes

Our lab

Here is our lab. A network namespace gives a set of processes its own interfaces and routing table, the same isolation containers use. We created three: host A on ten dot zero dot one, host B on ten dot zero dot two, and a router with a leg in each. Virtual ethernet pairs, veth, act as cables between them. Everything that follows is real output from this lab.

Built with ip netns
ip netns add a; ... r; ... b
ip link add va type veth
  peer name ra
A  10.0.1.10/24
R  10.0.1.1/24 + 10.0.2.1/24
B  10.0.2.20/24

a namespace = its own
  interfaces and routes

Routing table

Every machine has a routing table. Host A has two routes. Ten dot zero dot one slash twenty four is directly connected, so those addresses are delivered on the local link. The default route says everything else goes via ten dot zero dot one dot one, the gateway. When several routes match, the most specific, the longest prefix, wins, and default, slash zero, is the least specific. ip route get shows the decision for any address.

ip route on host A
default via 10.0.1.1 dev va
10.0.1.0/24 dev va
  scope link src 10.0.1.10

rule: the most specific
  matching route wins
10.0.1.x → deliver directly
anything else → 10.0.1.1
$ ip route get <ip> asks

Break 1: no route

First break. Before we added the default route, host A pinged host B and got Network is unreachable, instantly. The packet never left the machine: no route matched ten dot zero dot two, so the kernel refused. This exact message in a container or VM usually means a missing default route or a missing interface. We added default via ten dot zero dot one dot one.

Before adding the default
$ ping 10.0.2.20
ping: connect: Network is
  unreachable

the kernel had no route
  at all, so it failed
  instantly, on the host
fix: ip route add default
  via 10.0.1.1

Break 2: no forwarding

Second break. With routes on both hosts, ping still lost one hundred percent of packets. The router namespace had ip forward set to zero, the Linux default: a normal machine does not pass packets between interfaces. After setting it to one, three of three packets got through, in under a tenth of a millisecond. Docker and Kubernetes nodes turn this on, and a node where it was switched off is a classic broken-pod mystery.

With routes, forwarding off
router: net.ipv4.ip_forward
  = 0
$ ping -c2 10.0.2.20
2 transmitted, 0 received,
  100% packet loss
$ sysctl -w
    net.ipv4.ip_forward=1
3 transmitted, 3 received,
  0% packet loss, 0.038 ms

ARP

The routing table names the next hop by IP address, but Ethernet delivers frames by MAC address. ARP bridges the gap. We cleared host A's cache and captured it: a broadcast asking who has ten dot zero dot one dot one, tell ten dot zero dot one dot ten, and the router replied with its MAC address. Host A caches the answer in its neighbour table, shown by ip neigh as reachable. Note: the MAC of the final host never matters, only the next hop's.

tcpdump arp on the router
ARP, Request who-has
  10.0.1.1 tell 10.0.1.10
ARP, Reply 10.0.1.1 is-at
  fa:b2:79:39:8d:ad

$ ip neigh
10.0.1.1 lladdr
  fa:b2:79:39:8d:ad REACHABLE

TTL and traceroute

Every IP packet carries a time to live, and every router lowers it by one, so looping packets eventually die. Replies from host B had a TTL of sixty four when pinged from the router, and sixty three from host A, one hop further. traceroute uses this: it sends packets with TTL one, two, three, and each router that drops one reports back. Our path: hop one, the router, hop two, host B. On the internet you see a dozen hops, and where the replies stop.

Our lab
ping from router: ttl=64
ping from host A: ttl=63
  (one router on the way)
$ traceroute -n 10.0.2.20
 1  10.0.1.1   0.019 ms
 2  10.0.2.20  0.006 ms

TTL 0 → router drops it and
  sends "time exceeded"

In the cloud

In the cloud you rarely run routers, but the rules are identical. Each subnet has a route table: the VPC range is local, and zero slash zero goes to an internet gateway or a NAT gateway. Peering, VPNs and transit gateways just add more specific routes. The cloud fabric answers ARP for you. And Kubernetes networking, in part ten, installs routes so every node knows how to reach every pod range.

Cloud routing
  • each subnet has a route table 10.0.0.0/16 → local 0.0.0.0/0 → igw or nat
  • peering, VPN, transit gateway = more specific routes
  • no ARP to debug; the cloud fabric answers it
  • Kubernetes: routes per node

Common mistakes

Mistakes. A container or VM with no default route. ip forward off on a machine meant to route. Asymmetric routing, where the reply returns by a different path and a firewall drops it. A route you did not expect winning because it is more specific. And the most common one: adding a route one way and forgetting the return route, so requests arrive and replies never do.

Avoid these
  • missing default route in a container or VM
  • ip_forward off on a router or Kubernetes node
  • asymmetric paths: reply takes another route
  • a broad route hiding a specific one you expected
  • forgetting the return route

Say it like this

In the interview: the host looks up the destination in its routing table; nothing more specific matches, so it uses the default gateway. ARP resolves the gateway's MAC, the frame is sent there, and the router lowers the TTL and repeats the same lookup. To debug, I use ip route get, ip neigh and traceroute, and check forwarding is enabled and that a return route exists.

In the interview
  • "The host looks up the destination: no specific route, so the default gateway. ARP gets
  • the gateway's MAC, the frame goes
  • there, the router lowers TTL and repeats the lookup. I check ip
  • route get, ip neigh, forwarding
  • and the return route."

Interview questions

Q1What is a default gateway?

The router a host sends traffic to when no more specific route matches the destination; the 0.0.0.0/0 route in the table.

Q2What does longest prefix match mean?

When several routes match a destination, the one with the longest prefix (most specific, e.g. /24 over /16 over /0) is used.

Q3What does ARP do?

Maps an IPv4 address on the local link to a MAC address with a broadcast "who-has" request; results are cached in the neighbour table (ip neigh).

Q4What is TTL and how does traceroute use it?

Each router decrements the packet's time to live; at 0 it drops it and returns ICMP time exceeded. traceroute sends TTL 1, 2, 3... to reveal each hop.

Q5Why might a Linux host not forward packets between interfaces?

net.ipv4.ip_forward is 0 by default; routers, Docker hosts and Kubernetes nodes must set it to 1.

Q6"Network is unreachable" versus a timeout?

Unreachable is immediate: the host has no route. A timeout means packets left but no reply came back, often a firewall or missing return route.

Q7What is asymmetric routing and why is it a problem?

Replies take a different path than requests; stateful firewalls on one path see only half the connection and drop it.