Day 2: Where does the packet go?
Goal: You ask the kernel which path a packet takes instead of evaluating routing tables in your head, and you find missing default routes, overly specific routes, wrong source addresses and asymmetric paths that the kernel silently drops · Duration: approx. 75 min · Prerequisite: Day 1, checkpoint green
Briefing
- After the link comes the route. Every host decides for every packet itself which interface and which gateway it goes through. The router makes the same decision again, and the reply makes its own decision on the way back – not necessarily the same path.
- Reading routing tables is error-prone: the longest matching route
wins, rules (
ip rule) can select a different table, and the source address depends on the chosen route. That is why today is mostly about one command:ip route get. It asks the kernel what it would do, without sending a packet. - Four failure patterns: no default route (looks like DNS), an overly specific route (only one target broken), a detour through another machine (asymmetry) and the kernel throwing away the reply (reverse path filter).
Drill 1: Ask the Kernel
# on netz
# The routing table of client
# -n client in the namespace client
# route object routing table (without subcommand: show)
sudo ip -n client route
# Which path would a packet to web take?
# route get 10.2.0.10 look up the route for this target, send no packet
sudo ip -n client route get 10.2.0.10
# And to the internet?
sudo ip -n client route get 1.1.1.1
# For comparison, the VM itself (root namespace, without sudo)
ip route get 1.1.1.1Expected:
default via 10.1.0.1 dev eth0
10.1.0.0/24 dev eth0 proto kernel scope link src 10.1.0.10
10.2.0.10 via 10.1.0.1 dev eth0 src 10.1.0.10 uid 0
cache
1.1.1.1 via 10.1.0.1 dev eth0 src 10.1.0.10 uid 0
cache
1.1.1.1 via 172.16.0.1 dev eth0 src 172.16.0.123 uid 1000
cache
For the VM you see its own gateway, on your system probably a
different address. This is how you read a line of
route get:
| Part | Meaning |
|---|---|
10.2.0.10 |
the requested target |
via 10.1.0.1 |
the gateway (next router). If via is missing, the
target is in the local network and is addressed directly |
dev eth0 |
the packet leaves through this interface |
src 10.1.0.10 |
the kernel sets this source address if the program does not choose its own |
uid 0 |
the user the query was made for (rules can choose different paths per user) |
cache |
the result comes from the routing cache, not an error |
In the table itself, proto kernel scope link means: the
kernel created this route itself when the address was set, and the
network is attached directly to the interface.
Save your progress? Sign up for free
Drill 2: No Default Route – Looks Like DNS
# on netz
# Inject the fault: delete the default route in client
# route del default remove the route for “everything else”
sudo ip -n client route del default
# Symptom, as a developer sees it
# -sS silent, but still show error messages
# -m3 at most 3 seconds
# -o /dev/null throw away the response body
sudo ip netns exec client curl -sS -m3 -o /dev/null https://example.comExpected:
curl: (6) Could not resolve host: example.com
The message sends everyone to DNS first. The cause lies one level
deeper: the name server 1.1.1.1 from
/etc/netns/client/resolv.conf is unreachable without a
default route. The measurement:
# on netz
# Hypothesis: no route to the internet
sudo ip -n client route get 1.1.1.1
# Cross-check: the local network still works
# -c1 -W1 -q one packet, wait 1 second, summary only (see Day 1)
sudo ip netns exec client ping -c1 -W1 -q 10.1.0.1 | tail -2Expected:
RTNETLINK answers: Network is unreachable
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.225/0.225/0.225/0.000 ms
Network is unreachable comes
immediately and from your own kernel: it has no route
and sends nothing at all. A timeout, on the other hand, would mean: the
packet is out, but nothing comes back. You will see this difference in
every tool from Day 4 on.
Fix and proof:
# on netz
# Set the default route again
# route add default via 10.1.0.1 everything without a more specific route goes to the router
sudo ip -n client route add default via 10.1.0.1
sudo ip -n client route get 1.1.1.1
# -w '%{http_code}\n' print only the HTTP status
sudo ip netns exec client curl -s -m5 -o /dev/null -w '%{http_code}\n' https://example.comExpected:
1.1.1.1 via 10.1.0.1 dev eth0 src 10.1.0.10 uid 0
cache
200
In practice the default route disappears when DHCP does not renew the lease, when netplan applies only part of the configuration after a typo, or when a VPN client “cleans up” when it disconnects.
Save your progress? Sign up for free
Drill 3: The Longest Matching Route Wins
A colleague wanted to test the web server through a new router and forgot a route:
# on netz
# Inject the fault: a different gateway only for 10.2.0.10
# 10.2.0.10/32 exactly this one address (32-bit prefix)
# via 10.1.0.99 a gateway that does not exist
sudo ip -n client route add 10.2.0.10/32 via 10.1.0.99
# Symptom
sudo ip netns exec client curl -sS -m3 http://10.2.0.10/Expected:
curl: (28) Connection timed out after 3003 milliseconds
If you run the same command again right away, a different message
appears after about one second:
curl: (7) Failed to connect to 10.2.0.10 port 80 after 995 ms: Couldn't connect to server.
The first time, the kernel is still waiting for an ARP reply from the
gateway; the second time it already knows that none will come. Two
different error messages for the same cause – one more reason not to
search for the wording, but to measure.
The db at the same site is still reachable, only web is not. That sounds like a problem on web. The measurement shows something else:
# on netz
# Two targets in the same network, two different paths
sudo ip -n client route get 10.2.0.10
sudo ip -n client route get 10.2.0.20
# The table: the /32 route is more specific than the default route
sudo ip -n client route
# Is the gateway found?
# neigh show 10.1.0.99 entry in the neighbor table (ARP) for this address
sudo ip -n client neigh show 10.1.0.99Expected:
10.2.0.10 via 10.1.0.99 dev eth0 src 10.1.0.10 uid 0
cache
10.2.0.20 via 10.1.0.1 dev eth0 src 10.1.0.10 uid 0
cache
default via 10.1.0.1 dev eth0
10.1.0.0/24 dev eth0 proto kernel scope link src 10.1.0.10
10.2.0.10 via 10.1.0.99 dev eth0
10.1.0.99 dev eth0 FAILED
The kernel always chooses the route with the longest matching prefix:
/32 beats /24, and /24 beats the
default route (/0). The gateway 10.1.0.99 does
not answer ARP, so the entry is FAILED. More about
neighbors on Day 3.
# on netz
# Fix: remove the wrong route
sudo ip -n client route del 10.2.0.10/32
# Proof
sudo ip -n client route get 10.2.0.10
sudo ip netns exec client curl -s -m3 http://10.2.0.10/Expected:
10.2.0.10 via 10.1.0.1 dev eth0 src 10.1.0.10 uid 0
cache
hallo von web
Save your progress? Sign up for free
Drill 4: Which Source Address?
A host with two interfaces has two addresses. Which one appears as the sender in the packet is decided by the route, not the program (as long as it does not choose its own).
# on netz
# web talks to the db over the storage network …
sudo ip -n web route get 10.10.0.20
# … and to the client through the router
sudo ip -n web route get 10.1.0.10Expected:
10.10.0.20 dev stor0 src 10.10.0.10 uid 0
cache
10.1.0.10 via 10.2.0.1 dev eth0 src 10.2.0.10 uid 0
cache
This matters as soon as a firewall or a database filters by sender: a
PostgreSQL rule in pg_hba.conf that only allows
10.10.0.0/24 lets web in only over the storage network –
and ip route get tells you in advance which address web
will use. If you want to force it from the outside, ask with
from:
# on netz
# Which path would a packet with sender 10.10.0.10 to the client take?
# from 10.10.0.10 set the source address for the query
sudo ip -n web route get 10.1.0.10 from 10.10.0.10Expected:
10.1.0.10 from 10.10.0.10 via 10.2.0.1 dev eth0 uid 0
cache
Sender from the storage network, but exit through eth0:
the routing table only knows targets, not senders. This is the basis for
the failure pattern in Drill 5.
Save your progress? Sign up for free
Drill 5: There via A, Back via B
The storage network is supposed to be reachable from site A for a migration. Instead of a clean solution, someone turns the db into a router and adds a path to it on the router:
# on netz
# db forwards
# sysctl -w net.ipv4.ip_forward=1 set a kernel parameter: forward IPv4 packets
sudo ip netns exec db sysctl -w net.ipv4.ip_forward=1
# Router: storage network via the db
# 10.10.0.0/24 via 10.2.0.20 this network with the db as gateway
sudo ip -n router route add 10.10.0.0/24 via 10.2.0.20
# Outbound path from the client
# traceroute -n show intermediate hops without resolving names
# -q1 one probe per hop instead of three
# -w1 wait at most 1 second for each reply
sudo ip netns exec client traceroute -n -q1 -w1 10.10.0.10Expected (times differ):
net.ipv4.ip_forward = 1
traceroute to 10.10.0.10 (10.10.0.10), 30 hops max, 60 byte packets
1 10.1.0.1 0.230 ms
2 10.2.0.20 0.160 ms
3 10.10.0.10 0.117 ms
Outbound path: client → router → db → stor0 of web. And
the return path? The reply has the target 10.1.0.10, and
for that web only has the default route via eth0 (Drill 4).
So the reply goes directly to the router, bypassing the db. This is
asymmetric routing. It works – for now:
# on netz
sudo ip netns exec client ping -c3 -W1 -q 10.10.0.10 | tail -2Expected:
3 packets transmitted, 3 received, 0% packet loss, time 2040ms
rtt min/avg/max/mdev = 0.195/0.210/0.221/0.011 ms
Asymmetry is not wrong, but it is dangerous: any stateful firewall on only one of the two paths sees half the connection and drops it (Day 9), and the kernel itself can be configured not to accept such packets. That is the exercise.
Save your progress? Sign up for free
Exercise
A security team hardens web according to the CIS Benchmark and sets the reverse path filter to strict:
# on netz
# Inject the fault
# conf.all.rp_filter=1 strict for all interfaces …
# conf.stor0.rp_filter=1 … and for stor0 (the higher value of all and interface takes effect)
sudo ip netns exec web sysctl -w net.ipv4.conf.all.rp_filter=1
sudo ip netns exec web sysctl -w net.ipv4.conf.stor0.rp_filter=1The ping from the client to 10.10.0.10 no longer works.
The link counters (Day 1) show nothing, the route of web is unchanged.
Find the proof that web drops the packets itself, and explain why.
Solution
# on netz
# Symptom
sudo ip netns exec client ping -c3 -W1 -q 10.10.0.10 | tail -2
# Interface counters: no dropped
# sed -n 3,4p only lines 3 and 4 (RX header and values)
sudo ip -n web -s link show stor0 | sed -n 3,4p
# Kernel counter for the reverse path filter
# nstat read the kernel's network counters
# -a absolute values instead of changes since the last call
# -z also show counters with value 0
# TcpExtIPReversePathFilter only this counter
sudo ip netns exec web nstat -az TcpExtIPReversePathFilterExpected:
3 packets transmitted, 0 received, 100% packet loss, time 2049ms
RX: bytes packets errors dropped missed mcast
7450 97 0 0 0 0
#kernel
TcpExtIPReversePathFilter 3 0.0
Three pings, three hits in the counter (yours may be higher if you
tried more often). The name is misleading: despite Tcp, it
counts all IPv4 packets. Why does web drop them? The reverse path filter
asks for every incoming packet: “Would I send the reply to this sender
through the same interface?” The packets from the client arrive via
stor0, the reply to 10.1.0.10 would go via
eth0 (Drill 5). With rp_filter=1 (strict) that
is a reason to drop; with 2 (loose, Ubuntu default) it is
enough that a route to the sender exists at all; with 0
there is no check.
The quick fix would be to set the filter back to 2. That
undoes the hardening – the Challenge shows how to do without it.
Save your progress? Sign up for free
Challenge
Leave rp_filter=1 on web. Make sure that everything web
sends with the sender 10.10.0.10 goes back via
stor0 and the db. The ping from the client to
10.10.0.10 must work again, and the web server on
10.2.0.10 must keep answering via eth0 as
before. Prove both with ip route get.
Solution
The main table chooses by target only. For a decision by sender you need a second table and a rule that selects it: policy routing.
# on netz
# Separate table 100 with a default route via the db in the storage network
# route add default via 10.10.0.20 dev stor0 gateway db, exit stor0
# table 100 in table 100 instead of main
sudo ip -n web route add default via 10.10.0.20 dev stor0 table 100
# Rule: packets with sender 10.10.0.10 look into table 100
# rule add from 10.10.0.10 condition: this source address
# lookup 100 action: use table 100
# priority 1000 order (lower = earlier), before main (32766)
sudo ip -n web rule add from 10.10.0.10 lookup 100 priority 1000
# The rules in order
sudo ip -n web ruleExpected:
0: from all lookup local
1000: from 10.10.0.10 lookup 100
32766: from all lookup main
32767: from all lookup default
The kernel walks through the rules from top to bottom:
local (own addresses), then your rule, then
main (the table that ip route shows), and
finally default (empty).
# on netz
# Proof 1: sender 10.10.0.10 goes via stor0 …
sudo ip -n web route get 10.1.0.10 from 10.10.0.10
# … without a sender (the web server on 10.2.0.10) still via eth0
sudo ip -n web route get 10.1.0.10
# Proof 2: ping works again, the counter no longer rises
sudo ip netns exec client ping -c3 -W1 -q 10.10.0.10 | tail -2
sudo ip netns exec web nstat -az TcpExtIPReversePathFilter
sudo ip netns exec client curl -s -m3 http://10.2.0.10/Expected:
10.1.0.10 from 10.10.0.10 via 10.10.0.20 dev stor0 table 100 uid 0
cache
10.1.0.10 via 10.2.0.1 dev eth0 src 10.2.0.10 uid 0
cache
3 packets transmitted, 3 received, 0% packet loss, time 2052ms
rtt min/avg/max/mdev = 0.240/0.262/0.288/0.019 ms
#kernel
TcpExtIPReversePathFilter 3 0.0
hallo von web
The reverse path filter is now satisfied too: it looks up the return
path with swapped addresses, that is, with sender
10.10.0.10 – and finds stor0 through the rule.
You need exactly this pattern (one table per uplink, one rule per source
address) for servers with two networks, for Kubernetes nodes with their
own storage or management network, and for load balancers with Direct
Server Return.
Save your progress? Sign up for free
Checkpoint
# on netz
# Rule and table 100 on web
sudo ip -n web rule | grep -c 'lookup 100'
sudo ip -n web route show table 100
# Path and source address for both senders
sudo ip -n web route get 10.1.0.10 from 10.10.0.10 | head -1
sudo ip -n web route get 10.1.0.10 | head -1
# client: default route present, no special route for web
sudo ip -n client route | grep -c '^default via 10.1.0.1'
sudo ip -n client route | grep -c '10.2.0.10'head -1 shows only the first line, grep -c
counts matches.
Expected:
1
default via 10.10.0.20 dev stor0
10.1.0.10 from 10.10.0.10 via 10.10.0.20 dev stor0 table 100 uid 0
10.1.0.10 via 10.2.0.1 dev eth0 src 10.2.0.10 uid 0
1
0
Clean up
Day 3 starts again with the training ground from Day 0. Undo the detour and the hardening, in reverse order:
# on netz
# Remove the rule and table 100 on web
sudo ip -n web rule del from 10.10.0.10 lookup 100 priority 1000
# route flush table 100 delete all routes of this table
sudo ip -n web route flush table 100
# Reverse path filter back to the Ubuntu default (loose)
sudo ip netns exec web sysctl -w net.ipv4.conf.all.rp_filter=2
sudo ip netns exec web sysctl -w net.ipv4.conf.stor0.rp_filter=2
# Remove the detour via the db
sudo ip -n router route del 10.10.0.0/24 via 10.2.0.20
sudo ip netns exec db sysctl -w net.ipv4.ip_forward=0
# Proof: storage network unreachable from the client again, as on Day 0
sudo ip netns exec client ping -c1 -W1 -q 10.10.0.10 >/dev/null || echo "10.10.0.10 von client aus nicht erreichbar"Expected:
net.ipv4.conf.all.rp_filter = 2
net.ipv4.conf.stor0.rp_filter = 2
net.ipv4.ip_forward = 0
10.10.0.10 von client aus nicht erreichbar
Interview note
“A server has two network cards; it is not reachable via the second
one, but it is via the first. What is the cause?” The expected answer:
the replies leave via the default route of the first card (asymmetric),
and either a firewall on the path drops the half connection or the
server’s reverse path filter does. Measure with
ip route get <client> from <address of the second interface>
and nstat -az TcpExtIPReversePathFilter, fix with policy
routing (separate table + ip rule from) instead of
rp_filter=0.
Enterprise note
Policy routing set by hand disappears at the next reboot. On servers
it belongs in the network configuration: netplan has
routing-policy and routes with
table for this, NetworkManager has
ipv4.routing-rules. And monitor
TcpExtIPReversePathFilter (node_exporter:
node_netstat_TcpExt_IPReversePathFilter, if enabled in the
netstat collector) – a rising value after hardening or a network change
is almost always an asymmetric path that nobody noticed before.