Practical 1 · A router between two networks
Goal: three machines (
red,router,blue) on two networks, routes that send red’s and blue’s traffic through the router, forwarding switched off and on, and a missing route back that you find withip route getandtcpdump, all rebuilt by one script,p1.sh.Time: ~40 minutes. Where: files in
~/linux-networking-zero/on your computer; network commands inside the lab container (./on). Read lesson 3 and lesson 4 first.
Skipped Practical 0, or your lab is in a state you don’t trust? ./lab reset p1 (on your computer) deletes every
namespace in netlab and rebuilds Practical 0’s red and blue. It needs the lab container from Practical 0, task 2.
As in Practical 0, commands in a bash block run inside the lab: open a shell there with ./on. The ones
that start with ./on or ./lab run on your computer.
Task 1 · Two networks and a router
Write ~/linux-networking-zero/p1.sh yourself:
- deletes
red,blueandrouterif they exist (Practical 0’s red and blue too), without errors, and stops at the first command that fails, likep0.sh - three namespaces:
red,router,blue - a cable from
veth-red(in red) tor-red(in router), and one fromveth-blue(in blue) tor-blue(in router) - addresses: red
10.0.1.2/24, router10.0.1.1/24onr-redand10.0.2.1/24onr-blue, blue10.0.2.2/24 - every interface up,
loincluded; no routes yet
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
Hint
Start from p0.sh: one more ip netns del and ip netns add, two ip link add ... type veth peer name ..., and
each end moved with ip link set <end> netns <namespace>. The router’s two ends both go into router.
Solution
#!/bin/bash
# p1.sh: red and blue on two networks, joined by router (Practical 1)
set -e
ip netns del red 2>/dev/null || true
ip netns del blue 2>/dev/null || true
ip netns del router 2>/dev/null || true
ip netns add red
ip netns add router
ip netns add blue
ip link add veth-red type veth peer name r-red
ip link set veth-red netns red
ip link set r-red netns router
ip link add veth-blue type veth peer name r-blue
ip link set veth-blue netns blue
ip link set r-blue netns router
ip -n red addr add 10.0.1.2/24 dev veth-red
ip -n router addr add 10.0.1.1/24 dev r-red
ip -n router addr add 10.0.2.1/24 dev r-blue
ip -n blue addr add 10.0.2.2/24 dev veth-blue
ip -n red link set lo up
ip -n red link set veth-red up
ip -n router link set lo up
ip -n router link set r-red up
ip -n router link set r-blue up
ip -n blue link set lo up
ip -n blue link set veth-blue up
./on bash p1.sh
Predict first: which of these works from red: a ping to the router (
10.0.1.1), a ping to blue (10.0.2.2)?
ip netns exec red ping -c1 -W1 10.0.1.1
ip netns exec red ping -c1 -W1 10.0.2.2
ip -n red route get 10.0.2.2
What you see
The router answers: it’s on red’s own link. Blue doesn’t, and no packet even leaves:
ping: connect: Network is unreachable
RTNETLINK answers: Network is unreachable
ip -n red route has one line, 10.0.1.0/24 dev veth-red ...: red knows its own network and nothing else.
Task 2 · Routes both ways
Give red a default route through the router, and blue one through the router’s other address. Then ask red’s kernel which way it would send to blue.
Hint
ip -n <ns> route add default via <the router's address on your network>.
Solution
ip -n red route add default via 10.0.1.1
ip -n blue route add default via 10.0.2.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
Add the two lines to the end of p1.sh, so the script matches what you built.
Task 3 · Forwarding off, then on
In the lab, new namespaces start with forwarding on (they copy it from your computer, where Docker Engine turned it on), so switch it off in the router first and see what that does.
Predict first: with forwarding off, does red get an error, or silence? On which of the router’s two sides will
tcpdumpsee red’s pings?
ip netns exec router sysctl -w net.ipv4.ip_forward=0
ip netns exec router timeout 6 tcpdump -n -c 3 -i r-red icmp & sleep 1; ip netns exec red ping -c3 -W1 10.0.2.2; wait
ip netns exec router timeout 6 tcpdump -n -c 3 -i r-blue icmp & sleep 1; ip netns exec red ping -c3 -W1 10.0.2.2; wait
Why: & starts tcpdump in the background, sleep 1 gives it time to start, wait waits for it to finish.
-c 3 stops after three packets, timeout 6 after six seconds when fewer come.
What you see
Silence: 3 packets transmitted, 0 received, 100% packet loss, and no error line. On r-red tcpdump catches three
ICMP echo requests; on r-blue, 0 packets captured. The router takes red’s packets in and drops them.
Now switch it on, and look at the replies:
ip netns exec router sysctl -w net.ipv4.ip_forward=1
ip netns exec red ping -c3 10.0.2.2
ip netns exec red ping -c1 -W1 -t1 10.0.2.2
The replies say ttl=63: one router lowered blue’s 64 by one. With -t1 (TTL 1) the ping dies at the first router, and the
router says so: From 10.0.1.1 icmp_seq=1 Time to live exceeded. Add the sysctl -w ... =1 line to p1.sh
(as ip netns exec router sysctl -w net.ipv4.ip_forward=1), then run it again from your computer: ./on bash p1.sh
twice, and red’s ping to blue still works.
Task 4 · Break it: the way back
Run this, without reading it closely: it’s what a colleague did.
ip -n blue route del default
Red’s ping to blue now fails. Find the cause using only these, in this order, and write down what each says:
- red’s own route:
ip -n red route get 10.0.2.2 - the router:
ip netns exec router sysctl net.ipv4.ip_forward - does it arrive?
ip netns exec blue timeout 6 tcpdump -n -c 6 -i veth-blue icmp & sleep 1; ip netns exec red ping -c3 -W1 10.0.2.2; wait - blue’s way back:
ip -n blue route get 10.0.1.2
What each says
1 and 2 are fine: via 10.0.1.1, ip_forward = 1. In 3 tcpdump sees three echo requests arrive and no
echo reply leave. 4 says why: RTNETLINK answers: Network is unreachable. Blue gets the requests and has no
route to send the replies.
Fix it with the same command as task 2 (ip -n blue route add default via 10.0.2.1). p1.sh still has that line,
so it already matches; run ./on bash p1.sh to be sure the script and the lab agree.
Check your work
./lab check p1
All 31 checks should pass. They read your namespaces, routes and forwarding setting, ping blue from red (and look for
ttl=63), and read p1.sh.
Go deeper: watch the router rewrite the packet
The router changes two things in every packet it forwards: the TTL (one lower) and the Ethernet addresses (the
frame leaving r-blue is from the router’s MAC to blue’s). The IP addresses stay 10.0.1.2 > 10.0.2.2. -e
shows the Ethernet header, -v the TTL:
ip netns exec router timeout 6 tcpdump -n -e -v -c 2 -i r-blue icmp & sleep 1; ip netns exec red ping -c1 10.0.2.2; wait
Compare the MACs with ip -n router link show r-blue and ip -n blue link show veth-blue. Lesson 5 shows how the
router found blue’s MAC.
Next: lesson 5 · ARP and bridges.