Homework — Lesson 03
~30 min · Three tasks · Uses ip netns + iptables mangle
Task 1 — Source-based routing across 3 namespaces
Build the following topology on a single host:
client-a (10.10.1.5) ──veth──┐
├── shared-host (running ip rule + 2 tables)
client-b (10.10.2.5) ──veth──┘ │
├── gw-a (10.99.0.1)
└── gw-b (10.99.1.1)
- Create 5 namespaces:
client-a,client-b,shared-host,gw-a,gw-b. - Connect them with veth pairs. Assign IPs as shown.
- On
shared-host, enable IPv4 forwarding. - Add
ip rule from 10.10.1.0/24 lookup 100andip rule from 10.10.2.0/24 lookup 200. - Populate table 100 with a default via
gw-a; table 200 viagw-b. - Ping a "remote" IP from each client through the shared host.
Self-check:
ip route get 8.8.8.8 from 10.10.1.5resolves viagw-a.ip route get 8.8.8.8 from 10.10.2.5resolves viagw-b.tcpdumpon each gateway sees only its expected client.
Task 2 — fwmark-based routing
On a single namespace (or your host):
- Add an iptables MANGLE rule in OUTPUT: mark all outbound SSH (tcp/22) traffic with
0x42. - Add an
ip rule fwmark 0x42 lookup 300. - Populate table 300 with a default via a different gateway than your normal one.
- Verify with
ip route get 8.8.8.8 mark 0x42.
Self-check:
iptables -t mangle -L OUTPUT -v -nshows your MARK rule.ip rule showshows your fwmark rule.ip route get 8.8.8.8 mark 0x42resolves through table 300.
Task 3 — CONNMARK persistence verification
Building on Task 2's setup, add the three-rule CONNMARK chain:
- Add
-j MARK --set-mark 0x42on NEW packets (conntrack state NEW). - Add
-j CONNMARK --save-mark. - Add
-j CONNMARK --restore-markBEFORE the MARK rule so subsequent packets get the mark before routing. - Open a long-lived TCP connection (e.g.
nc -lk 9999on one namespace andnc <ip> 9999on another). - Run
conntrack -L | grep markwhile the connection is open.
Self-check:
conntrack -Lshows the connection entry withmark=0x42.- After typing some data into
ncon the client side, the response on the server side travels through the policy-routing table — verified bytcpdumpon the alternate gateway namespace. - Restarting
nccreates a new conntrack entry which also gets the mark.