Lab: on-link vs off-link delivery - ARP, the gateway and ICMP in a capture
Build a small routed network in unprivileged Linux network namespaces, watch a host ARP for a neighbour or for its gateway, read an unanswered ARP and an ICMP error, and break a subnet mask to see the asymmetric path it causes.
Contentsand lesson path
On this page
- Goal and setup
- Step 0: Check the tools and enter an isolated router shell
- Step 1: Build the routed lab and ask the kernel for its route choices
- Step 2: Predict, then capture an on-link and an off-link ping
- Step 3: Ping an on-link address that nobody owns
- Step 4: Break the mask: give host-a a /28 and watch the detour
- Step 5: Clean up and confirm the host is untouched
- Check your understanding
- Apply it
- Review prompt
Lessons of IPv4 Addressing & Subnetting
- Addresses as 32-bit numbers: binary, masks and prefixes Concept · 20 min
- Worked example: subnetting any prefix without a cheat sheet Worked example · 25 min
- VLSM planning: sizing, ordering and leaving room to summarize Worked example · 25 min
- Special-purpose ranges: private, CGNAT, loopback, link-local and documentation Concept · 15 min
- Lab: on-link vs off-link delivery - ARP, the gateway and ICMP in a capture Lab · 25 min
- Check: timed subnetting and addressing-plan exercise Knowledge check · 8 questions
What you will learn
- Explain how ARP and the default gateway let a host deliver packets on-link and off-link, and verify it with ICMP tools and a capture.
Prerequisites: Networking Basics
01. Goal and setup
Earlier lessons worked out on paper which addresses share a subnet. This lab shows what a host does with that answer: it delivers straight to a neighbour, hands a packet to its gateway, fails to reach an address nobody owns, and misbehaves visibly when its mask is too narrow.
The principle under test: a host decides whether a destination is on-link (on its own subnet) or off-link from its own address and prefix length, and that decision chooses which IPv4 address it asks about with ARP and which MAC address goes into the Ethernet frame. ARP (RFC 826) is a broadcast "who has this IPv4 address?" answered by a unicast "it is at this MAC address".
Everything runs in Linux network namespaces, separate copies of the network stack. unshare --map-current-user --net --keep-caps bash starts a shell in new user and network namespaces where you may change interfaces and settings, but only for that private stack, so no sudo is needed. That shell is the router; three more namespaces, each kept alive by a sleep process, are the hosts. A Linux bridge (a software switch) joins host-a and host-c, and host-b sits behind the router. All addresses come from the RFC 5737 documentation ranges.
host-a a-eth0 192.0.2.10/24 02:00:00:00:02:10 --+
host-c c-eth0 192.0.2.20/24 02:00:00:00:02:20 --+-- br0, a Linux bridge
router br0 192.0.2.1/24 02:00:00:00:02:01 --+ (the "switch")
router r-b 198.51.100.1/24 02:00:00:00:64:01 ------ cable to host-b
host-b b-eth0 198.51.100.10/24 02:00:00:00:64:10
default gateway: 192.0.2.1 for host-a and host-c, 198.51.100.1 for host-bYou need Linux, on a machine or a VM, that allows unprivileged user namespaces, with util-linux, iproute2, tcpdump, procps and iputils ping. Your own interfaces, routes and ARP settings are never touched. The outputs were recorded on Ubuntu 22.04.5 (kernel 6.8.0-149) as an ordinary user. Illustrative blocks contain values that change per run (round-trip times, ICMP ids, ARP retry counts); MAC addresses, routes and frame order do not. Job lines such as [1] 4242 are left out.
02. Step 0: Check the tools and enter an isolated router shell
unshare --version
ip -V
tcpdump --version 2>&1 | head -1
ping -Vunshare from util-linux 2.37.2
ip utility, iproute2-5.15.0, libbpf 0.5.0
tcpdump version 4.99.1
ping from iputils 20211215mkdir -p ~/ipv4-lab05 && cd ~/ipv4-lab05
unshare --map-current-user --net --keep-caps baship -br link
id -ulo DOWN 00:00:00:00:00:00 <LOOPBACK>
1000The shell sees only a loopback interface that is down, none of your real ones: it is a separate network stack. id -u prints your own user id because --map-current-user maps you to yourself, and --keep-caps gives you network capabilities inside. Everything up to the exit in Step 5 runs in this shell.
03. Step 1: Build the routed lab and ask the kernel for its route choices
First turn the shell into a router: ip_forward=1 lets it pass packets between interfaces, and send_redirects=1 lets it send the ICMP redirects you will meet in Step 4. Then create the bridge with the router's LAN address, start three sleep processes in new namespaces (their PIDs, kept in A, C and B, are how you reach them), and connect everything with veth pairs: virtual cables with an interface at each end.
ip link set lo up
sysctl -w net.ipv4.ip_forward=1 net.ipv4.conf.all.send_redirects=1
ip link add br0 address 02:00:00:00:02:01 type bridge
ip addr add 192.0.2.1/24 dev br0 && ip link set br0 up
unshare --net sleep infinity & A=$!
unshare --net sleep infinity & C=$!
unshare --net sleep infinity & B=$!
sleep 1
ip link add a-eth0 address 02:00:00:00:02:10 type veth peer name a-br
ip link add c-eth0 address 02:00:00:00:02:20 type veth peer name c-br
ip link add b-eth0 address 02:00:00:00:64:10 type veth peer name r-b \
address 02:00:00:00:64:01
ip link set a-eth0 netns $A && ip link set c-eth0 netns $C
ip link set b-eth0 netns $B
ip link set a-br master br0 && ip link set a-br up
ip link set c-br master br0 && ip link set c-br up
ip addr add 198.51.100.1/24 dev r-b && ip link set r-b upnet.ipv4.ip_forward = 1
net.ipv4.conf.all.send_redirects = 1cfg configures one host through nsenter, which runs a command in the namespace of a PID: interfaces up, address and prefix, default route, then forwarding off and redirects ignored. Those settings are explicit because a new namespace copies its IPv4 settings from your machine (devconf_inherit_init_net in the kernel sysctl documentation). ip route get then asks host-a's kernel which route it would use; it sends nothing.
cfg() { # cfg PID ADDRESS/PREFIX INTERFACE GATEWAY
nsenter -t "$1" -n sh -c "ip link set lo up && ip addr add $2 dev $3 &&
ip link set $3 up && ip route add default via $4 &&
sysctl -qw net.ipv4.ip_forward=0 net.ipv4.conf.all.accept_redirects=0 &&
sysctl -qw net.ipv4.conf.$3.accept_redirects=0"
}
cfg $A 192.0.2.10/24 a-eth0 192.0.2.1
cfg $C 192.0.2.20/24 c-eth0 192.0.2.1
cfg $B 198.51.100.10/24 b-eth0 198.51.100.1
sleep 2
nsenter -t $A -n ip route get 192.0.2.20
nsenter -t $A -n ip route get 198.51.100.10192.0.2.20 dev a-eth0 src 192.0.2.10 uid 1000
cache
198.51.100.10 via 192.0.2.1 dev a-eth0 src 192.0.2.10 uid 1000
cache192.0.2.20 has no via: host-a's connected route 192.0.2.0/24 says it is on-link, so host-a will deliver to it directly. 198.51.100.10 comes back via 192.0.2.1: only the default route matches, so the gateway gets the packet. Neither answer proves that anything exists at those addresses.
04. Step 2: Predict, then capture an on-link and an off-link ping
Predict before you run. host-a pings host-c (192.0.2.20), then host-b (198.51.100.10). For each ping, which IPv4 address will be in host-a's ARP request, and to which MAC address will the echo request go? Write both down.
cap and stop start and stop a capture of ARP and ICMP on host-a's interface (--immediate-mode writes each packet at once). Reading it back with -e shows the MAC addresses, and sed splits each frame over two lines.
cap() { # start capturing ARP and ICMP on host-a: cap FILE
nsenter -t "$A" -n tcpdump --immediate-mode -i a-eth0 -w "$1" 'arp or icmp' \
2>/dev/null &
TP=$!; sleep 1
}
stop() { sleep 1; kill "$TP"; wait "$TP"; }
cap step2.pcap
nsenter -t $A -n ping -c 1 192.0.2.20
nsenter -t $A -n ping -c 1 198.51.100.10
stop
tcpdump -r step2.pcap -nn -e -q -t 2>/dev/null | sed 's/: /:\n /'PING 192.0.2.20 (192.0.2.20) 56(84) bytes of data.
64 bytes from 192.0.2.20: icmp_seq=1 ttl=64 time=0.075 ms
--- 192.0.2.20 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.075/0.075/0.075/0.000 ms
PING 198.51.100.10 (198.51.100.10) 56(84) bytes of data.
64 bytes from 198.51.100.10: icmp_seq=1 ttl=63 time=0.071 ms
--- 198.51.100.10 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.071/0.071/0.071/0.000 ms
02:00:00:00:02:10 > ff:ff:ff:ff:ff:ff, ARP, length 42:
Request who-has 192.0.2.20 tell 192.0.2.10, length 28
02:00:00:00:02:20 > 02:00:00:00:02:10, ARP, length 42:
Reply 192.0.2.20 is-at 02:00:00:00:02:20, length 28
02:00:00:00:02:10 > 02:00:00:00:02:20, IPv4, length 98:
192.0.2.10 > 192.0.2.20: ICMP echo request, id 19124, seq 1, length 64
02:00:00:00:02:20 > 02:00:00:00:02:10, IPv4, length 98:
192.0.2.20 > 192.0.2.10: ICMP echo reply, id 19124, seq 1, length 64
02:00:00:00:02:10 > ff:ff:ff:ff:ff:ff, ARP, length 42:
Request who-has 192.0.2.1 tell 192.0.2.10, length 28
02:00:00:00:02:01 > 02:00:00:00:02:10, ARP, length 42:
Reply 192.0.2.1 is-at 02:00:00:00:02:01, length 28
02:00:00:00:02:10 > 02:00:00:00:02:01, IPv4, length 98:
192.0.2.10 > 198.51.100.10: ICMP echo request, id 6953, seq 1, length 64
02:00:00:00:02:01 > 02:00:00:00:02:10, IPv4, length 98:
198.51.100.10 > 192.0.2.10: ICMP echo reply, id 6953, seq 1, length 64For host-c, host-a broadcast who-has 192.0.2.20, host-c answered with its MAC, and the echo request went to 02:00:00:00:02:20. For host-b, host-a never asked about 198.51.100.10: it asked for its gateway 192.0.2.1 and sent the frame to 02:00:00:00:02:01, while the IP header still said 192.0.2.10 > 198.51.100.10. IP addresses name the two ends; MAC addresses name only the next hop on this link.
The ttl values agree. Linux starts packets with a TTL of 64 (ip_default_ttl in the IP sysctl documentation), and each router that forwards a packet lowers it by one: ttl=64 from host-c means no router, ttl=63 from host-b means one.
05. Step 3: Ping an on-link address that nobody owns
Predict before you run. 192.0.2.99 is inside host-a's /24, but no namespace has that address. Will any echo request leave host-a? Which address will report the failure: the router's or host-a's own?
cap step3.pcap
nsenter -t $A -n ping -c 4 192.0.2.99
stop
nsenter -t $A -n ip neigh show 192.0.2.99
tcpdump -r step3.pcap -nn -t host 192.0.2.99 2>/dev/null | sort | uniq -cPING 192.0.2.99 (192.0.2.99) 56(84) bytes of data.
From 192.0.2.10 icmp_seq=1 Destination Host Unreachable
From 192.0.2.10 icmp_seq=2 Destination Host Unreachable
From 192.0.2.10 icmp_seq=3 Destination Host Unreachable
From 192.0.2.10 icmp_seq=4 Destination Host Unreachable
--- 192.0.2.99 ping statistics ---
4 packets transmitted, 0 received, +4 errors, 100% packet loss, time 3101ms
pipe 4
192.0.2.99 dev a-eth0 FAILED
6 ARP, Request who-has 192.0.2.99 tell 192.0.2.10, length 28The capture holds only ARP requests: a frame needs a destination MAC, so host-a queued the packets and kept asking who has 192.0.2.99. After the configured number of unanswered requests (mcast_solicit, three by default) the neighbour entry became FAILED, the packets were dropped, and the kernel reported Destination Host Unreachable from 192.0.2.10, host-a itself; no router takes part in on-link delivery. A powered-off, unplugged, misaddressed or wrong-VLAN target looks the same: the capture shows that nothing at layer 2 claimed the address, not why.
06. Step 4: Break the mask: give host-a a /28 and watch the detour
Predict before you run. You will replace host-a's /24 with a /28, which covers only 192.0.2.0 to 192.0.2.15; host-c (192.0.2.20) keeps its correct /24 on the same bridge. Predict what ip route shows after the change, whether host-a can still reach host-c, and which MAC address the echo request will carry.
nsenter -t $A -n sh -c 'ip addr flush dev a-eth0 &&
ip addr add 192.0.2.10/28 dev a-eth0'
nsenter -t $A -n ip route
nsenter -t $A -n ip route add default via 192.0.2.1
nsenter -t $A -n ip route get 192.0.2.20192.0.2.0/28 dev a-eth0 proto kernel scope link src 192.0.2.10
192.0.2.20 via 192.0.2.1 dev a-eth0 src 192.0.2.10 uid 1000
cacheThe default route vanished with the old address: when an interface loses its last IPv4 address, the kernel removes the routes that used it, and the new address brings back only its connected route. The third command restores it. ip route get now says via 192.0.2.1: by host-a's own mask, a neighbour on the same switch is off-link.
cap step4.pcap
nsenter -t $A -n ping -c 2 192.0.2.20
stop
tcpdump -r step4.pcap -nn -e -q -t 2>/dev/null | sed 's/: /:\n /'PING 192.0.2.20 (192.0.2.20) 56(84) bytes of data.
From 192.0.2.1: icmp_seq=1 Redirect Host(New nexthop: 192.0.2.20)
64 bytes from 192.0.2.20: icmp_seq=1 ttl=64 time=0.116 ms
From 192.0.2.1: icmp_seq=2 Redirect Host(New nexthop: 192.0.2.20)
64 bytes from 192.0.2.20: icmp_seq=2 ttl=64 time=0.104 ms
--- 192.0.2.20 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1021ms
rtt min/avg/max/mdev = 0.104/0.110/0.116/0.006 ms
02:00:00:00:02:10 > ff:ff:ff:ff:ff:ff, ARP, length 42:
Request who-has 192.0.2.1 tell 192.0.2.10, length 28
02:00:00:00:02:01 > 02:00:00:00:02:10, ARP, length 42:
Reply 192.0.2.1 is-at 02:00:00:00:02:01, length 28
02:00:00:00:02:10 > 02:00:00:00:02:01, IPv4, length 98:
192.0.2.10 > 192.0.2.20: ICMP echo request, id 41384, seq 1, length 64
02:00:00:00:02:01 > 02:00:00:00:02:10, IPv4, length 126:
192.0.2.1 > 192.0.2.10: ICMP redirect 192.0.2.20 to host 192.0.2.20, length 92
02:00:00:00:02:01 > ff:ff:ff:ff:ff:ff, ARP, length 42:
Request who-has 192.0.2.20 tell 192.0.2.1, length 28
02:00:00:00:02:20 > 02:00:00:00:02:10, IPv4, length 98:
192.0.2.20 > 192.0.2.10: ICMP echo reply, id 41384, seq 1, length 64
02:00:00:00:02:10 > 02:00:00:00:02:01, IPv4, length 98:
192.0.2.10 > 192.0.2.20: ICMP echo request, id 41384, seq 2, length 64
02:00:00:00:02:01 > 02:00:00:00:02:10, IPv4, length 126:
192.0.2.1 > 192.0.2.10: ICMP redirect 192.0.2.20 to host 192.0.2.20, length 92
02:00:00:00:02:20 > 02:00:00:00:02:10, IPv4, length 98:
192.0.2.20 > 192.0.2.10: ICMP echo reply, id 41384, seq 2, length 64The ping works, which is why this fault survives in real networks. The capture shows the cost:
- host-a's ARP cache went with its old address, so it asked for the gateway again and sent the echo request for
192.0.2.20to the gateway's MAC. - The router sent the packet back out of the interface it arrived on, plus an ICMP redirect: the message RFC 792 defines for a gateway to tell a host about a better next hop on the same network, here host-c itself. host-a ignores redirects in this lab, so the second request took the same detour.
- After asking who has
192.0.2.20, the router passed the request on. host-c, whose/24is correct, sees192.0.2.10as on-link and replies straight to host-a's MAC, hence02:00:00:00:02:20andttl=64.
Requests now go through the router and replies go around it. The router carries traffic it should never see, and anything on it that tracks both directions of a connection, such as a stateful firewall, sees only half of each flow. A mask that is too wide fails the other way: the host ARPs for an off-link destination, nothing local answers, and you get the Step 3 picture for a host that exists. Apply lets you try it.
07. Step 5: Clean up and confirm the host is untouched
kill $A $B $C
exitcd ~ && rm -rf ~/ipv4-lab05
ip link show a-brDevice "a-br" does not exist.Killing the sleep processes removes the host namespaces; leaving the shell removes the router namespace, the bridge and r-b. Your normal shell never had any of them.
08. Check your understanding
09. Apply it
Rebuild the lab (Steps 0 and 1) and predict each result before running it:
- A dropped digit: give host-a
192.0.2.10/2instead of/24, restore its default route and ping host-b. What doesip route get 198.51.100.10print, and who reports the failure? - Redirects: in Step 4, first run
nsenter -t $A -n sysctl -w net.ipv4.conf.a-eth0.accept_redirects=1, then ping host-c three times and compareip route get 192.0.2.20before and after. What did host-a learn?
Next, Ethernet Switching & VLANs explains what the bridge was doing, and IP Routing Fundamentals builds on these route lookups.
10. Review prompt
Without looking back, explain from your captures how host-a chose between ARPing for the destination and for its gateway, why the frame to host-b carried the gateway's MAC, how an unanswered ARP differs from a router's ICMP error, and why a too-narrow mask can let pings succeed while still being wrong.
References
Your progress
Reading state
Sign in to save that you finished reading this lesson.
Prove this skill
8 questions · about 6 minutes · scored automatically. Marking a lesson as read never demonstrates a skill.