Static Routing and Diagnostic Tools in Katharà
In questa pagina 8
See also: Forwarding and Routing TablesForwarding moves each packet one hop at a time toward its destination; it is direct when the destination is on a local network (ARP gives its MAC) and indirect otherwise (the packet goes to the next hop or default gateway given by the routing table). With netmasks, delivery is direct on interface x if the destination AND the mask equals the interface address AND the mask; otherwise the table is scanned from the longest mask (longest prefix match) and the default route 0.0.0.0/0 always matches; route aggregation shortens tables.Forwarding and Routing Tables →, Subnetting, VLSM and SupernettingSubnetting splits an address block into smaller subnets by lengthening the mask (2^s subnets when s bits are borrowed), supernetting does the opposite by shortening the prefix to merge contiguous blocks; design rules for variable-size subnets are power-of-two sizes M with M at least hosts plus 2, prefix 32 minus log2 M, and start addresses divisible by M (largest subnet first), while 2^j blocks of prefix n aggregate into one block of prefix n minus j only if contiguous and aligned.Subnetting, VLSM and Supernetting →, ARP - Address Resolution ProtocolTo place an IP datagram in a frame a node needs the MAC address of the next hop, obtained with ARP: a broadcast request (destination MAC FF:FF:FF:FF:FF:FF, carrying the sender's IP and MAC and the wanted IP) and a unicast reply with the wanted MAC, stored in the ARP cache; ARP always resolves the next hop (destination host or router), never the final destination, and since broadcasts do not leave the subnet a router can answer with its own MAC (proxy ARP).ARP - Address Resolution Protocol →, ICMP, Ping and TracerouteICMP adds to IP the error reporting and queries it lacks; its messages travel inside IP datagrams with Protocol 1 and are either errors (destination unreachable type 3, time exceeded 11, redirect 5, parameter problem 12), always sent to the original source with the original IP header plus 8 bytes of data, or queries (echo request 8 and reply 0, timestamp 13 and 14); ICMP only reports and never corrects errors, and it supports ping (RTT), path MTU discovery with the D bit (maximum payload 1500-20-8=1472 bytes) and traceroute (TTL increasing, n+1 probes for n routers), but also attacks such as smurf and fake redirects.ICMP, Ping and Traceroute →.
Laboratory 2 shows how to build by hand the routing table of hosts and routers, using Katharà as in Emulating Networks with KatharàKatharà is a network emulator where every device is a Docker container joined to virtual collision domains; a lab is a folder with lab.conf (topology), one .startup file per machine and optional per-device folders; machines are configured with ifconfig, tested with ping and observed with tcpdump and Wireshark.Emulating Networks with Katharà →.
Topology
195.11.14.0/24 (domain A) 100.0.0.8/30 (domain B) 200.1.1.0/24 (domain C)
pc1 .5 --eth0-- .1 eth0 [r1] eth1 .9 ============ .10 eth1 [r2] eth0 .1 --eth0-- .7 pc2| device | interface | domain | address |
|---|---|---|---|
| pc1 | eth0 | A | 195.11.14.5/24 |
| r1 | eth0 | A | 195.11.14.1/24 |
| r1 | eth1 | B | 100.0.0.9/30 |
| r2 | eth1 | B | 100.0.0.10/30 |
| r2 | eth0 | C | 200.1.1.1/24 |
| pc2 | eth0 | C | 200.1.1.7/24 |
100.0.0.8/30 has only two usable addresses, and , with broadcast : the right network for a point-to-point link between routers. The /30 mask is 255.255.255.252; the last octet leaves host bits, addresses ( network, and hosts, broadcast).
Lab files.
r1[0]=A
r1[1]=B
r2[0]=C
r2[1]=B
pc1[0]=A
pc2[0]=C# pc1.startup
ifconfig eth0 195.11.14.5/24 up
# pc2.startup
ifconfig eth0 200.1.1.7/24 up
# r1.startup
ifconfig eth0 195.11.14.1/24 up
ifconfig eth1 100.0.0.9/30 up
# r2.startup
ifconfig eth0 200.1.1.1/24 up
ifconfig eth1 100.0.0.10/30 upThere is no route: this is what the next steps add. Start with kathara lstart.
Step 1: what pc1 reaches without routes
root@pc1:~$ ping 195.11.14.1 # r1 eth0: same network
64 bytes from 195.11.14.1: icmp_seq=1 ttl=64 time=3.17 ms
root@pc1:~$ ping 100.0.0.9 # r1 eth1: different network
connect: Network is unreachableThe second fails immediately and nothing is sent: no route for 100.0.0.9. For each table row the kernel computes destination AND Genmask and compares it to Destination: 195.11.14.1 AND 255.255.255.0 195.11.14.0 matches; 100.0.0.9 AND 255.255.255.0 100.0.0.0 does not match.
root@pc1:~$ route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
195.11.14.0 * 255.255.255.0 U 0 0 0 eth0| column | meaning |
|---|---|
Destination |
destination network (or host) |
Gateway |
next router; * or 0.0.0.0 with -n means directly connected |
Genmask |
mask of the destination |
Flags |
U = route up, G = via a gateway |
Metric, Ref, Use |
cost, references, uses (not needed here) |
Iface |
outgoing interface |
Property (directly connected networks). When an interface is activated, its network is inserted automatically in the routing table. All other networks must be added by hand (or by a routing protocol). This holds for any IP device, real routers included.
Step 2: the default route on the PCs
Definition (default route). Route with destination
0.0.0.0and mask0.0.0.0(, containing all addresses), used when no more specific route matches.
root@pc1:~$ route add default gw 195.11.14.1 # unknown networks go through r1 (eth0)
root@pc2:~$ route add default gw 200.1.1.1 # for pc2 the gateway is r2It inserts default 195.11.14.1 0.0.0.0 UG eth0. The gateway address must be directly reachable. Now ping 100.0.0.9 from pc1 works: the packet goes to the MAC of r1 (ARP on the gateway) and r1 answers.
Why pc1 does not reach r2. ping 100.0.0.10 gets loss. Sniffing on r2:
r2:~# tcpdump -tenni eth1
arp who-has 100.0.0.10 tell 100.0.0.9
arp reply 100.0.0.10 is-at fe:fd:64:00:00:0a
IP 195.11.14.5 > 100.0.0.10: icmp 64: echo request seq 1
IP 195.11.14.5 > 100.0.0.10: icmp 64: echo request seq 2The requests arrive (r1 forwards them), but no echo reply leaves: r2 has nothing for 195.11.14.0/24.
Property (two-way reachability). Two nodes communicate only with a path both ways: each router on the way needs a route to the destination and to the sender. A
pingwithout reply does not say the forward path is broken: sniff at both ends.
Step 3: static routes on the routers
r2:~# route add -net 195.11.14.0/24 gw 100.0.0.9 dev eth1Formula.
route add -net <network>/<prefix> gw <next hop> dev <interface>: "the network is reached through the next hop leaving from the interface". The next hop must be in a network directly connected to that interface.
It equals route add -net 195.11.14.0 netmask 255.255.255.0 gw 100.0.0.9 dev eth1. Table of r2:
Destination Gateway Genmask Flags Iface
100.0.0.8 * 255.255.255.252 U eth1
200.1.1.0 * 255.255.255.0 U eth0
195.11.14.0 100.0.0.9 255.255.255.0 UG eth1Symmetrically on r1: route add -net 200.1.1.0/24 gw 100.0.0.10 dev eth1. Now the PCs see each other:
root@pc1:~$ ping 200.1.1.7
64 bytes from 200.1.1.7: icmp_seq=1 ttl=62 time=1.05 msProperty (TTL and routers crossed). Every router forwarding a datagram subtracts from the TTL. A destination starting with replies so that the packet arrives with , routers crossed.
The reply of pc2 goes through r2 and r1: . traceroute 200.1.1.7 from pc1 shows 195.11.14.1 (r1), 100.0.0.10 (r2), 200.1.1.7: each router answers with an ICMP time exceeded when the TTL reaches .
Static routes versus default route
Property (longest prefix match). If several routes match, the kernel uses the one with the longest prefix. The default route () is the least specific.
Example. On r2 a packet for 195.11.14.5 matches 195.11.14.0/24 and the default (if present); the prefixes are and , so wins.
Default as a static route. route add default gw 195.11.14.1 dev eth0 equals route add -net 0.0.0.0/0 gw 195.11.14.1 dev eth0.
Routers with default routes only. Each router has one neighbour:
r1:~# route add default gw 100.0.0.10 dev eth1
r2:~# route add default gw 100.0.0.9 dev eth1It works for the two PCs, but a non-existing address such as 8.8.8.8 bounces between r1 and r2 until the TTL reaches : routers with several neighbours use specific routes.
Lab exercises
- Exercise 1. The only row of pc1 is
195.11.14.0 * 255.255.255.0 U eth0. - Exercise 2. After the default routes, pc1 reaches both interfaces of r1 and nothing else (r2 has no return route). r1 reaches pc1, its own interfaces and
100.0.0.10, but not200.1.1.1or200.1.1.7. - Exercise 3. On r1:
route add -net 200.1.1.0 netmask 255.255.255.0 gw 100.0.0.10 dev eth1. - Exercise 4. Static routes on pc1 instead of the default:
route del default
route add -net 100.0.0.8/30 gw 195.11.14.1 dev eth0 # link between the routers
route add -net 200.1.1.0/24 gw 195.11.14.1 dev eth0 # LAN of pc2Advanced exercise: four hosts on one domain with different masks
| host | address | network |
|---|---|---|
| pcA | 192.168.20.1/24 | 192.168.20.0/24 |
| pcB | 192.168.20.193/28 | 192.168.20.192/28 |
| pcC | 192.168.20.206/28 | 192.168.20.192/28 |
| pcD | 192.168.20.254/24 | 192.168.20.0/24 |
192.168.20.192/28: mask , broadcast , hosts to . Each host decides whether a destination is local using its own mask: pcA and pcD () think everything in - is local; pcB and pcC () think only - is local.
| from to | outcome |
|---|---|
| A D | works |
| B C | works |
| A or D B or C | the frame arrives, but B or C have no route back: fails |
| B or C A or D | Network is unreachable: fails |
Solution. A route without gateway on pcB and pcC; longest prefix keeps B C on the route:
route add -net 192.168.20.0/24 dev eth0Table of pcB: 192.168.20.192 mask 255.255.255.240 eth0, and 192.168.20.0 mask 255.255.255.0 eth0.
ARP along the path
A ping from pc2 (200.1.1.7) to pc1 crosses three domains (arp -n shows the cache, tcpdump -tenni the packets):
- Domain C: pc2 asks in broadcast
who-has 200.1.1.1 tell 200.1.1.7; r2 replies in unicast. - Domain B: r2 finds the next hop
100.0.0.9and runs ARP for it. - Domain A: r1 sees that
195.11.14.5is directly connected and runs ARP for the final address.
Property. In every LAN, ARP asks for the MAC of the next node (gateway or final destination). IP addresses of the packet stay the same all along the path, MAC addresses change at each domain.
Katharà MACs: 200.1.1.1 is FE:FD:C8:01:01:01, 100.0.0.10 is FE:FD:64:00:00:0A, 195.11.14.5 is FE:FD:C3:0B:0E:05 (, ). The ARP cache starts empty; whoever receives an ARP request learns the sender's MAC; for a destination outside the network the cache contains the MAC of the router; a ping to a non-existing address in the same network gives some unanswered ARP requests, then Destination Host Unreachable.
- Stale cache. If two hosts swap addresses after pc1 cached them, pc1 keeps sending frames to the old MACs until the entry expires.
- Proxy ARP. Two domains with the same IP network: pc1 cannot reach pc3 because ARP does not leave the domain. On r1:
route add 192.168.10.30/32 eth1andecho 1 > /proc/sys/net/ipv4/conf/eth0/proxy_arp(andeth1): r1 answers ARP with its own MAC. The route is mandatory: the kernel answers for others only for addresses it knows.
Common mistakes
- Missing return route: the
pingrequest arrives, the reply does not. - Gateway not in a directly connected network.
- Forgetting the interface or confusing
eth0witheth1. - Route without prefix:
route add -net 195.11.14.0 gw ...assumes the class mask. - Expecting a host with a narrower mask (
/28) to reach the whole/24.
Versione ripasso
- Topology (LAB2). pc1
195.11.14.5/24(A) - r1 (195.11.14.1/24on A,100.0.0.9/30on B) - r2 (100.0.0.10/30on B,200.1.1.1/24on C) - pc2200.1.1.7/24(C).lab.conf:r1[0]=A,r1[1]=B,r2[0]=C,r2[1]=B,pc1[0]=A,pc2[0]=C.100.0.0.8/30: hosts.9and.10, broadcast.11. - Connected networks.
ifconfig eth0 IP/prefix upadds only its own network. Without routesping 195.11.14.1works (ttl=64),ping 100.0.0.9givesNetwork is unreachableand nothing is sent. route -n.Destination,Gateway(*or0.0.0.0if connected),Genmask,Flags(Uup,Gvia gateway),Iface.- Default route on PCs.
route add default gw 195.11.14.1(pc2:200.1.1.1) ; gateway must be in a connected network. - pc1 does not reach r2. On r2
tcpdump -tenni eth1shows echo requests but no replies: r2 has no route to195.11.14.0/24.
Property (two-way reachability). A path both ways is needed: every router must have a route to the destination and to the sender. Sniff at both ends.
- Static routes. r2:
route add -net 195.11.14.0/24 gw 100.0.0.9 dev eth1; r1:route add -net 200.1.1.0/24 gw 100.0.0.10 dev eth1. "Network reached through the next hop, leaving fromdev". Next hop in a connected network. - Result.
ping 200.1.1.7from pc1:ttl=62(, each router subtracts 1).tracerouteshows195.11.14.1,100.0.0.10,200.1.1.7through ICMP time exceeded. - Longest prefix match. The longest matching prefix wins; default is last. On r2,
195.11.14.5matches/24over default. - Default as static route.
route add -net 0.0.0.0/0 gw 195.11.14.1 dev eth0. Defaults only on routers:r1: route add default gw 100.0.0.10 dev eth1,r2: route add default gw 100.0.0.9 dev eth1; works, but8.8.8.8bounces until TTL is .
route del default
route add -net 100.0.0.8/30 gw 195.11.14.1 dev eth0
route add -net 200.1.1.0/24 gw 195.11.14.1 dev eth0- Four hosts, different masks. pcA
192.168.20.1/24, pcD.254/24, pcB.193/28, pcC.206/28(192.168.20.192/28: hosts.193-.206, broadcast.207). Each host uses its own mask. AD and BC work; A or D to B or C fails on return; B or C to A or D isNetwork is unreachable. Fix on pcB and pcC:route add -net 192.168.20.0/24 dev eth0(no gateway). - ARP along the path. pc2 to pc1 crosses C (MAC of r2), B (MAC of
100.0.0.9), A (MAC of195.11.14.5). IPs unchanged, MACs change at each hop; the receiver of an ARP request learns the sender's MAC; outside the network the cache holds the router's MAC. - Stale cache and proxy ARP. Old cache sends frames to wrong MACs until expiry. Proxy ARP on r1:
echo 1 > /proc/sys/net/ipv4/conf/eth0/proxy_arp, plusroute add 192.168.10.30/32 eth1. - Mistakes: missing return route; gateway not connected; wrong interface; route without prefix;
/28host expected to reach the/24.