4 · Routing
In one sentence: a route says “for these addresses, send out this interface, through this machine”; the kernel picks the most specific route that matches, a router passes packets from one of its links to another, and the reply needs a route back.
The problem it solves
Lesson 3 ended with two machines on different networks and ping: connect: Network is unreachable. Real networks
are always like that: your laptop is on 192.168.1.x, the server you want is on some other network far away, and
nothing connects them directly. Something in the middle has to pass packets along, and every machine has to know
where to hand them.
The idea: deliver it yourself, or hand it to the post office
A letter for your own street you carry over yourself. Anything else goes in the post box, and the post office works out the next step. A machine does the same:
- the address is on one of my links (a connected route,
proto kernel, from lesson 3): send it straight there; - otherwise, find a route that names a gateway, a machine on my link that will pass it on, and hand it to that.
The lab for this lesson has three namespaces. router has a leg in each network:
red router blue
veth-red 10.0.1.2/24 --- r-red 10.0.1.1/24
r-blue 10.0.2.1/24 --- veth-blue 10.0.2.2/24
network 10.0.1.0/24 network 10.0.2.0/24
Reading a routing table
With only the addresses set, red knows one network, its own:
10.0.1.0/24 dev veth-red proto kernel scope link src 10.0.1.2
So red can ping 10.0.1.1 (on its link) but not 10.0.2.2. ip route get <address> asks the kernel which route it
would use, without sending anything. For 10.0.2.2 it has none:
RTNETLINK answers: Network is unreachable
The router itself needs nothing extra: both networks are on its own links.
10.0.1.0/24 dev r-red proto kernel scope link src 10.0.1.1
10.0.2.0/24 dev r-blue proto kernel scope link src 10.0.2.1
Give red a default route, the “anything else” line, through the router:
ip -n red route add default via 10.0.1.1
ip -n red route get 10.0.2.2
10.0.2.2 via 10.0.1.1 dev veth-red src 10.0.1.2 uid 0
Read it as: to reach 10.0.2.2, send out veth-red to the gateway 10.0.1.1, with 10.0.1.2 as the sender
address. Blue gets the mirror image, default via 10.0.2.1.
A router has to agree to forward
A machine that receives a packet for someone else’s address normally throws it away. Passing it on is IP
forwarding, a setting called net.ipv4.ip_forward. Turn it off in the router and ping again:
ip netns exec router sysctl -w net.ipv4.ip_forward=0
ip netns exec red ping -c3 -W1 10.0.2.2
3 packets transmitted, 0 received, 100% packet loss, time 2046ms
tcpdump on the router’s red side shows the three requests arriving; on its blue side it captures nothing. The
router dropped them and sent no error back, so red only sees silence. Turn it on (=1) and the replies come:
64 bytes from 10.0.2.2: icmp_seq=1 ttl=63 time=0.055 ms
Look at ttl=63. Every packet carries a TTL (time to live), a counter each router lowers by one, so a packet
caught in a loop dies instead of circling forever. Blue sent its reply with 64; one router on the way made it 63.
A ping straight to the router comes back with ttl=64. Send a ping with its TTL set to 1 (-t1): it dies at the first
router, and that router tells you:
From 10.0.1.1 icmp_seq=1 Time to live exceeded
The way back
A connection is two one-way trips. Delete blue’s default route and ping from red again: 100% packet loss. Red’s
route is fine and the router forwards; tcpdump in blue shows all three echo requests arriving. But blue has no
route to 10.0.1.2, so it can’t send the replies. Ask blue’s kernel:
ip -n blue route get 10.0.1.2
RTNETLINK answers: Network is unreachable
When packets go out and nothing comes back, check the route back too, from the other machine.
Which route wins
When several routes match, the longest prefix wins: the most specific one. default is 0.0.0.0/0, prefix
length 0, so it matches everything and loses to anything else. Add a (wrong) route for half of blue’s network:
ip -n red route add 10.0.2.128/25 dev veth-red
Now 10.0.2.200 is in the /25, so the kernel sends it straight out of veth-red, no gateway, while 10.0.2.2
still goes through the router:
10.0.2.200 dev veth-red src 10.0.1.2 uid 0
10.0.2.2 via 10.0.1.1 dev veth-red src 10.0.1.2 uid 0
That’s how one wrong route breaks a few addresses and leaves the rest working. ip route get shows which route
wins for any address. One more rule: the gateway must be on a link you have. ip -n red route add 10.0.9.0/24 via 10.0.2.1 fails with Error: Nexthop has invalid gateway.
| Symptom from red | First table to read | Where |
|---|---|---|
Network is unreachable at once | routes: no route matches | red: ip route get |
| silence, nothing comes back | forwarding, then the route back | router: sysctl net.ipv4.ip_forward; blue: ip route |
Time to live exceeded from a router | the packet’s TTL ran out there | (a loop, or -t set low) |
Try it on your computer
Practical 1 · A router between two networks: build red, router and blue, route between them, switch forwarding off and on, then delete a route back and find it.
Go deeper: how the kernel picks a route
For each packet the kernel looks up the destination in the routing table and takes the matching route with the
longest prefix; among routes of the same prefix, the lowest metric. The answer is the outgoing interface, the
gateway (if any) and the source address (src). ip route get runs the same lookup and prints it.
A route with via sends the packet to the gateway’s MAC address (found with ARP, lesson 5) while the IP
header still says 10.0.2.2: the IP address is where it’s going, the MAC is the next hop. That’s why the gateway
must be on one of your links. More in ip-route(8).
Every namespace has its own table. The lab container’s, with Docker’s gateway:
default via 172.17.0.1 dev eth0
172.17.0.0/16 dev eth0 proto kernel scope link src 172.17.0.4
Go deeper: ip_forward belongs to the namespace
net.ipv4.ip_forward is one of the per-namespace net.* settings (lesson 2). In the lab it is already 1 in the
container, and red and router were 1 too before anything was changed. Setting it to 0 in router leaves the
container’s at 1.
Where the 1 comes from: a new namespace copies its IPv4 settings from your computer’s own, and Docker Engine
turned forwarding on there. So in the lab, forwarding is on unless you switch it off; Practical 1 switches it off
first to see the difference, and sets it back with sysctl -w.
A machine with forwarding on and more than one network is a router, whether you meant it or not. On a server,
sysctl net.ipv4.ip_forward says which it is. The kernel’s own description of the setting is in
ip-sysctl.
Common misunderstandings
- “The router needs a default route.” Not to join two networks it sits on: both are connected routes. It needs one only to reach networks beyond.
- “No reply means my side is wrong.” Requests can arrive perfectly and the reply be lost: no route back, or
forwarding off on the way.
tcpdumpon the far side tells you which half failed. - “A gateway is an address far away.” A gateway is always on one of your links: the next machine, not the destination.
Check yourself
1. Red's ping to 10.0.2.2 gets 100% packet loss and no error. tcpdump on router's r-red sees the requests, on r-blue nothing. What do you check?
The router isn't passing them on: ip netns exec router sysctl net.ipv4.ip_forward. It says 0;
set it to 1.
2. The replies from a machine say ttl=62. How many routers are between you?
Two, if it sent them with 64 as the lab's machines do: each router lowers it by one.
3. Red has default via 10.0.1.1 and 10.0.2.0/24 via 10.0.1.5. Which gateway does 10.0.2.2 go to?
10.0.1.5: the /24 is longer than default's /0, so it wins.
ip route get 10.0.2.2 confirms it.
Next: Practical 1 · A router between two networks, then lesson 5 · ARP and bridges.