Bài gốc: Understand Linux Routing System - Part 2 — tác giả BaconYao, lấy qua Notion public API.
Key Takeaway
Experiment 2 - Direct route from a Network Namespace to Host eth0
Experimental Environment Setup
commands - Create our environment
commands - Create our environment
Let’s begin the experiment!
1. Ping Host eth0 from ns1

First, let’s check the routing table of
ns1. At the moment, it contains only one entry:10.22.33.0/24 dev eth0 proto kernel scope link src 10.22.33.4Next, when we issue a ping command from ns1 to the Host, we can observe that it results in a “Network is unreachable” status.2. Update ns1’s routing table

Since that’s the case, let’s try adding the
Host’s eth0 to ns1’s routing table.-
Step 1: Add the route with the command
ip route add 172.16.0.2/32 dev eth0 -
Step 2: Issue the
pingcommand to the Host again
ping command becomes stuck (hangs)…Why is that? Let’s debug it step by step.3. Observe two questions

-
Step 1: Check the ARP table in
ns1. We can observe an interesting phenomenon:- Question 1: Why does
ns1’s ARP table show the IP address of theHost’s eth0interface, but the MAC address recorded is that of thebr0interface?
- Question 1: Why does
-
Step 2: On the Host side (bottom right), run
tcpdumpto listen for packets traversing thebr0interface. -
Step 3: Return to
ns1and issue thepingto the Host again.
br0 receives the ICMP Request packets but no ICMP Reply packets. Why is that?4. Solve the Question 2

Let’s answer Question 2 first.
-
Step 1: Check the Host’s routing table.
- We find only the
defaultroute and a172.16.0.0/24 …route. That means the Host does not know how to deliver packets tons1.
- We find only the
-
Step 2: Therefore, we manually add a route to
ns1:$ sudo ip route add 10.22.33.0/24 dev br0
5. Result of solving the Question 2

After adding the route, we issue the
ping command again — and this time, it succeeds. We can also observe that the br0 interface now carries both ICMP Request and ICMP Reply packets.Let’s return to Question 1 once again.
The core issue

Let’s confirm the packet path when pinging
Host eth0 from ns1.-
Step 1: On the right side of the left diagram, we listen on the
Host eth0interface (above) and on thebr0interface (below). -
Step 2: From
ns1, initiate apingcommand to theHost eth0interface.

Next, we observe an interesting phenomenon.The packet reaches the
br0 interface and is returned back to ns1; it is not forwarded to the Host eth0 interface.So, why does this happen? And who allows br0 to respond to ARP requests on behalf of Host eth0? Please continue reading to find out.Analysis of the cause behind Question 1
Because we added 172.16.0.2/32 dev eth0 to ns1’s routing table, this tells the Linux kernel that packets destined for 172.16.0.2 can be sent directly without going through a gateway, using the eth0 interface in ns1.
So, Linux considers 172.16.0.2 to be on the “local network” of ns1, and therefore it actively sends out an ARP request asking, “Who has 172.16.0.2?”
When the Host receives this ARP request, it notices:
-
Although
172.16.0.2is the IP address ofHost eth0, -
The packet came in through the
vethinterface (which is connected tobr0), -
If
ip_forwardis enabled and there are no other restrictions, - Then the Linux kernel will reply to the ARP request, saying: “172.16.0.2 is me, my MAC address is [MAC of br0]. Send the packets to me, and I will forward them.”
ns1 to show (Host eth0 IP address) at [br0 MAC address] on eth0.
A question left for you to ponder
After analyzing Question 1, we know that in this case,br0 can respond to ICMP packets from ns1 on behalf of Host eth0.
So, the question is: Did the packets actually get delivered to Host eth0, or were they returned as soon as they reached br0?
Feel free to try it out and find the answer yourself.
Experiment 3 - Try to communicate to Google DNS from a Network Namespace
Experimental Environment Setup
commands - Create our environment
commands - Create our environment
Let’s begin the experiment!
1. First check our environment

After setting up the environment, it will appear as shown in the left diagram.In addition to displaying the network configuration, it also shows the routing tables of both the
Host and ns1 individually.2. Ping Google DNS from ns1

We confirm that in the Host environment, we can successfully ping Google DNS (8.8.8.8).However, in the
ns1 environment, the result is “Network is unreachable.”- This is because
ns1’s routing table lacks a default route and has no specified route to the destination, so it does not know how to forward the packets.
3. Direct routing to Google DNS in ns1

-
Step 1: On the Host side, monitor all packets passing through
br0. -
Step 2: Manually add a direct route for Google DNS:
$ ip route add 8.8.8.8/32 dev eth0 -
Step 3: Attempt the
pingcommand again.-
At this point, the command hangs in
ns1. -
On the Host side (left), observe that the
br0interface receives ARP Request packets asking “Who has IP address 8.8.8.8?”
-
At this point, the command hangs in
8.8.8.8 belongs to an external network and is not part of any subnet on this machine, the Linux kernel responsible for the Host’s br0 interface cannot respond to these ARP requests, resulting in no ARP Reply.Relation to Experiment 2
Relation to Experiment 2
4. Turn br0 as Gateway and monitor it

How can we address the issue above?
-
Step 1: Remove the previous direct route and add a new route with the command:
$ ip route add 8.8.8.8/32 via 10.22.33.1 dev eth0-
This sets
br0 (10.22.33.1)as the gateway. -
You will see
8.8.8.8 via 10.22.33.1 dev eth0inns1’s routing table.
-
This sets
-
Step 2: Try pinging
8.8.8.8again.- You will observe that the Host now receives ICMP echo request packets.
5. Examine our ARP Table

So far, after checking
ns1’s current Routing Table and ARP Table (using the command $ arp -a), we can analyze the following:-
? (8.8.8.8) at <incomplete> on eth0-
<incomplete>means thatns1attempted to resolve the MAC address for8.8.8.8but failed. This is usually because8.8.8.8is not in the same network aseth0(which is in the10.22.33.0/24subnet). -
ARP protocol only works for IP address resolution within the same subnet. Since
8.8.8.8is an external network address, its MAC address cannot be obtained via ARP. -
This situation usually indicates that
ns1needs to access external networks via a gateway (for example,10.22.33.1may act as the gateway).
-
-
? (10.22.33.1) at 46:47:b7:ad:a6:90 [ether] on eth0-
Since
ns1’s eth0 andbr0are on the same network (10.22.33.0/24),ns1can communicate directly withbr0using ARP. -
This ARP entry shows that
ns1successfully obtained the MAC address ofbr0, meaning thatns1’s eth0 andbr0(IP10.22.33.1) are able to communicate at Layer 2 (data link layer).
-
Since
5. Monitor Host eth0 interface

Earlier, we monitored the
br0 interface. Now, let’s switch to monitoring ICMP packets on the Host’s eth0 interface.-
Step 1: Before starting, confirm whether IP forwarding is enabled. If it’s not set to 1, the host will not forward packets from
br0toHost eth0. -
Step 2: Monitor ICMP packets on the
Host eth0interface. -
Step 3: From
ns1, initiate a ping command to8.8.8.8.
Host eth0 interface towards the external network.However, we do not receive ICMP Echo Reply packets. This issue will be discussed in a future article.Conclusion
In this article, we have summarized the following conclusions:- In Experiment 2, we explored two interesting issues and understood that for packets to be correctly sent and received, every node along the path must have clear and proper routing.
- In Experiment 3, through communication with external services, we learned the concept of a gateway and gained a deeper understanding of routing.