Practical 2 · A switch and a service
Goal: three machines on one switch (a bridge), a TCP service on blue that red and green can reach, its handshake caught with
tcpdump, and two breaks you tell apart by their errors: a service on the wrong address, and a machine unplugged from the switch. All rebuilt byp2.sh.Time: ~45 minutes. Where: files in
~/linux-networking-zero/on your computer; network commands inside the lab container (./on). Read lesson 5 and lesson 6 first.
Skipped a practical, or your lab is in a state you don’t trust? ./lab reset p2 (on your computer) deletes every
namespace in netlab and rebuilds what Practicals 0 and 1 built.
As before, commands in a bash block run inside the lab (./on opens a shell there); the ones starting with
./on or ./lab run on your computer.
Task 1 · A switch with three machines
Write ~/linux-networking-zero/p2.sh yourself:
- starts clean like
p1.sh: deletesred,blue,green,routerandswitchif they exist, stops at the first failure - a namespace
switchwith a bridgebr0in it, up, with no IP address - three machines,
red,blue,green, each with a cable fromveth-<name>(in the machine) tosw-<name>(inswitch), plugged intobr0and up - addresses
10.0.0.1/24(red),10.0.0.2/24(blue),10.0.0.3/24(green); every interface up,loincluded
red 10.0.0.1 blue 10.0.0.2 green 10.0.0.3
veth-red veth-blue veth-green
| | |
switch: sw-red ------------- sw-blue -------------- sw-green
\___________________ br0 ___________________/
Hint
The three machines differ only in name and number, so a loop saves typing:
for c in red blue green; do ...; done, with a counter for the last number of the address. master br0 plugs a
port into the bridge (lesson 5).
Solution
#!/bin/bash
# p2.sh: a switch (bridge br0) with red, blue and green on it (Practical 2)
set -e
for ns in red blue green router switch; do ip netns del $ns 2>/dev/null || true; done
ip netns add switch
ip -n switch link add br0 type bridge
ip -n switch link set br0 up
n=1
for c in red blue green; do
ip netns add $c
ip link add veth-$c type veth peer name sw-$c
ip link set veth-$c netns $c
ip link set sw-$c netns switch
ip -n switch link set sw-$c master br0
ip -n switch link set sw-$c up
ip -n $c addr add 10.0.0.$n/24 dev veth-$c
ip -n $c link set lo up
ip -n $c link set veth-$c up
n=$((n + 1))
done
./on bash p2.sh
./on bridge -n switch link
Three ports, each master br0 state forwarding.
Predict first: red pings blue for the first time while
tcpdumplistens in green. What does green see?
ip netns exec green timeout 6 tcpdump -n -e -c 5 -i veth-green arp or icmp & sleep 1; ip netns exec red ping -c3 10.0.0.2; wait
bridge -n switch fdb show br br0 | grep -v permanent
Why: arp or icmp leaves out the IPv6 messages new interfaces send by themselves; -e shows MAC addresses.
What you see
One frame: red’s ARP request, to ff:ff:ff:ff:ff:ff. The reply and the pings went only to blue’s port. The
forwarding database shows which port each MAC was learned on.
Task 2 · A service on blue
Add two lines to p2.sh:
- near the top, before the deletes: stop any
ncalready running in the lab (pkill -x nc), without failing when there is none. A program running in a namespace keeps it alive afterip netns del, so an old listener would linger. - at the end: start
nc -lk 8080in blue, in the background, so it keeps running after./onreturns.
Hint
pkill -x nc || true. For the service, setsid -f <command> </dev/null >/dev/null 2>&1 starts it detached from your
terminal: a plain & dies with the ./on session.
Solution
pkill -x nc || true
(after set -e), and as the last line:
setsid -f ip netns exec blue nc -lk 8080 </dev/null >/dev/null 2>&1
./on bash p2.sh
./on ip netns exec blue ss -tlnp
./on ip netns exec red nc -zv -w2 10.0.0.2 8080
./on ip netns exec green nc -zv -w2 10.0.0.2 8080
ss shows LISTEN on 0.0.0.0:8080 with users:(("nc",...)), and both say
Connection to 10.0.0.2 8080 port [tcp/http-alt] succeeded!.
Predict first: red sends
helloto the service. How many packets cross before the first one with data?
ip netns exec blue timeout 6 tcpdump -n -c 8 -i veth-blue tcp port 8080 & sleep 1; echo hello | ip netns exec red nc -N -w2 10.0.0.2 8080; wait
What you see
Three: [S], [S.], [.], the handshake. Then [P.] with length 6 (hello and a newline), blue’s [.],
and the close: [F.] from each side and a last [.]. Red’s port is a different ephemeral number each run.
Task 3 · Break it: the wrong address
Restart the service the way a hasty config change would:
pkill -x nc; setsid -f ip netns exec blue nc -lk 127.0.0.1 8080 </dev/null >/dev/null 2>&1
Red can’t connect any more. Find out why using only these, in this order:
- is blue there?
ip netns exec red ping -c1 10.0.0.2 - what kind of failure?
ip netns exec red nc -zv -w2 10.0.0.2 8080: at once, or after a wait? - what crosses the wire?
ip netns exec blue timeout 6 tcpdump -n -c 2 -i veth-blue tcp port 8080 & sleep 1; ip netns exec red nc -z -w2 10.0.0.2 8080; wait - what listens?
ip netns exec blue ss -tlnp
What each says
1 works: the network is fine. 2 fails at once: Connection refused, an answer. 3 shows the answer, [S] then
[R.]. 4 shows the cause: LISTEN on 127.0.0.1:8080, which takes connections from blue itself only.
Fix it by running your script, which starts the service the right way: ./on bash p2.sh.
Task 4 · Break it: unplugged
Now take blue’s port out of the switch, and make red forget blue’s MAC (as if it had been a while):
ip -n switch link set sw-blue nomaster
ip -n red neigh flush all
Predict first: will red’s
ncto blue say refused, timed out, or something else, and how fast?
time ip netns exec red nc -zv -w5 10.0.0.2 8080
Find where it breaks, in this order:
ip netns exec red ping -c3 10.0.0.2ip -n red neighip -n blue -br linkbridge -n switch link
What each says
nc gives No route to host after about 3 seconds. 1 says Destination Host Unreachable, from red itself. 2 shows
10.0.0.2 ... FAILED: nobody answered ARP. 3 is the trap: blue’s veth-blue is still UP with LOWER_UP. 4
lists only sw-red and sw-green: blue’s port is out of the bridge.
Put it back with ./on bash p2.sh, which rebuilds everything, the service included.
| Break | Error | How fast | Found with |
|---|---|---|---|
service on 127.0.0.1 | Connection refused | at once | ss -tlnp on blue |
| blue unplugged | No route to host | about 3 s | ip neigh, then bridge link |
Check your work
./lab check p2
All 37 checks should pass. They read the switch, its ports and the three machines, ping blue and green from red,
check that something listens on port 8080 in blue on every address, connect to it from red and green, and read
p2.sh.
Go deeper: one connection, seen from the switch
A port of the bridge is an interface like any other, so tcpdump works on it too, from inside switch. Watch
red’s side of a connection to blue on sw-red, with the MACs:
ip netns exec switch timeout 6 tcpdump -n -e -c 4 -i sw-red tcp port 8080 & sleep 1; ip netns exec red nc -z -w2 10.0.0.2 8080; wait
The frames carry red’s and blue’s MACs (ip -n red -br link, ip -n blue -br link): the bridge passes them along
without changing them. A router (Practical 1, Go deeper) rewrites them; a switch doesn’t.
Next: lesson 7 · Firewalls and NAT.