Skip to content

Lesson 4 of 9 · 25m

Routing and Policy Routing: The Decision Before the Packet Leaves

Understand connected routes, longest-prefix match, multiple tables, ip rule, reverse path filtering, and the subtle failures of multi-homed hosts.

Routing failures are rarely 'there is no route.' More often there are three routes, one of them wins in a way you did not expect, and return traffic takes a different interface than the request arrived on. You debug this by tracing the kernel's decision, not by staring at the default route.

Longest-prefix match wins

ip route show
ip route get 198.51.100.20

A /32 beats a /24; a /24 beats the default. Metrics only choose between otherwise identical prefixes. This is why a stray connected route or VPN-pushed prefix can hijack traffic even when the default route looks correct.

ip route get is the high-value command. It reports the selected source address, output interface, gateway, and sometimes the table decision. If you remember one command from this course, make it that one.

Multi-homed hosts need policy, not hope

A host with eth0 on 192.0.2.0/24 and eth1 on 198.51.100.0/24 may receive a packet on eth1 but reply out eth0 because the default route says so. Some networks tolerate that. Stateful firewalls, anti-spoofing filters, and strict reverse path validation often do not.

Linux supports multiple routing tables selected by rules:

ip rule add from 198.51.100.14 lookup isp2 priority 100
ip route add default via 198.51.100.1 dev eth1 table isp2
ip rule show
ip route show table isp2

The rule says traffic whose source is the second interface address uses the second ISP table. This is policy routing: source-based selection before ordinary destination lookup.

Reverse path filtering can discard valid-looking traffic

sysctl net.ipv4.conf.all.rp_filter net.ipv4.conf.eth0.rp_filter

Strict mode (1) accepts ingress traffic only if the reply would leave through the same interface. In asymmetric routing, that assumption is false. Loose mode (2) is often the practical compromise: accept if the source is reachable at all. Do not disable anti-spoofing casually; understand why the topology violates strictness first.

Routes are state with consequences

Adding a route is not harmless experimentation. A broad 10.0.0.0/8 override can swallow half the enterprise. Use exact prefixes while proving reachability:

ip route add 10.24.16.0/24 via 192.0.2.254 dev eth0
ip route get 10.24.16.20
ping -c 3 10.24.16.20
ip route del 10.24.16.0/24 via 192.0.2.254

Persist routes through the owning network manager after the temporary experiment proves correct.

Field notes

  • RTNETLINK answers: File exists usually means an equivalent route already exists; inspect before retrying with different syntax.
  • Nexthop has invalid gateway means the gateway is not reachable through a connected route.
  • Multiple default routes with metrics create failover only when the kernel can detect failure; a dead gateway may still look valid.
  • VPNs often install rules and tables, not just routes. Inspect ip rule before declaring routing sane.
  • Test both directions. One-way success is not connectivity, especially through NAT or stateful middleboxes.

Was this lesson helpful?

Sign in to rate