Key Takeaway

<aside> 💡

In this chapter, we begin by discussing and exploring why, in the experiments from previous chapters, we were unable to successfully ping Google DNS from a self-created network namespace. This issue leads us to the main topic of this chapter: Network Address Translation (NAT), along with an explanation of its various types and Port Address Translation (PAT).

In the next chapter, we will conduct hands-on experiments using iptables to learn about SNAT, DNAT, and Conntrack, among other key concepts. Therefore, understanding NAT and PAT will serve as essential prerequisite knowledge for the upcoming chapter.

</aside>

The Previous Issue – Why couldn’t we successfully ping Google DNS from a self-created network namespace?

In a previous article (05. Understand Linux Routing System - Part 2 ), we left an open question. We attempted to send a ping from our self-created network namespace ns1 to an external network service, Google DNS. However, we observed that while the ICMP Echo Request packets were successfully sent, the ICMP Echo Reply packets never arrived.

routing table - 3.drawio.svg

To better understand this issue, let’s break down the ping flow step by step.

Ping Flow

NAT.drawio - 0.svg

We will use the diagram below to illustrate the packet flow of a ping command:

ICMP Echo Request 1 - ns1 to br0

NAT.drawio - 1.svg

When we initiate a ping from ns1 to Google DNS, the ICMP Request packet travels through the virtual eth0 interface and enters the br0 bridge. At this point, the contents of the ICMP packet are as shown in the diagram.

ICMP Echo Request 2 - br0 to wan (172.12.0.9)

NAT.drawio - 2.svg

Next, as the packet travels from br0 to the host’s eth0, this segment is handled within the same kernel network namespace, so it can be considered internal kernel processing.

For the segment from the host’s eth0 to a machine in the WAN, assuming no additional mechanisms or external assistance, the ICMP packet’s Source and Destination IP addresses remain unchanged. This ensures that the packet knows where to send the reply. What does change, however, is the MAC address.

ICMP Echo Request 3 - wan (8.8.8.123) to Google DNS

NAT.drawio - 3.svg

Next, the ICMP Request packet may traverse multiple routers within the WAN and eventually reach Google DNS, hypothetically arriving from the machine with IP address 8.8.8.123. At this point, the contents of the ICMP packet are as shown in the diagram.

ICMP Echo Reply

NAT.drawio - 4.svg

When the Google DNS host receives the ICMP Request packet, it sends back an ICMP Reply packet. However, we notice that the Destination IP of the reply is 10.22.33.4—a private IP within our internal network.

Since no one on the public network knows where this IP resides, the packet cannot be delivered to the correct destination and is ultimately dropped during routing.

Restating Our Issue

Based on the ping flow above, we can now describe our issue more precisely.

<aside> 💡

Can’t route back to my host since nobody knows how to route according to the Destination IP (10.22.33.4)

</aside>