Nat Loopback

Hairpin NAT

Hairpin network address translation (NAT Loopback) is where the device on the LAN is able to access another machine on the LAN via the public IP address of the gateway router. 

In the above example the gateway router has the following dst-nat configuration rule:

/ip firewall nat add chain=dstnat action=dst-nat dst-address=172.16.16.1 dst-port=443 to-addresses=10.0.0.3 to-ports=443 protocol=tcp

When a user from the PC at home establishes a connection to the webserver, the router performs DST NAT as configured:

  1. the client sends a packet with a source IP address of 192.168.88.1 to a destination IP address of 172.16.16.1 on port 443 to request some web resources;
  2. the router destination NAT`s the packet to 10.0.0.3 and replaces the destination IP address in the packet accordingly. The source IP address stays the same: 192.168.88.1;
  3. the server replies to the client’s request and the reply packet have a source IP address of 10.0.0.3 and a destination IP address of 192.168.88.1.
  4. the router determines that the packet is part of a previous connection and undoes the destination NAT, and puts the original destination IP address into the source IP address field. The destination IP address is 192.168.88.1, and the source IP address is 172.16.16.1;
  5. The client receives the reply packet it expects, and the connection is established;

But, there will be a problem, when a client on the same network as the webserver requests a connection to the web server’s public IP address: 

  1. the client sends a packet with a source IP address of 10.0.0.2 to a destination IP address of 172.16.16.1 on port 443 to request some web resources;
  2. the router destination NATs the packet to 10.0.0.3 and replaces the destination IP address in the packet accordingly. The source IP address stays the same: 10.0.0.2;
  3. the server replies to the client’s request. However, the source IP address of the request is on the same subnet as the webserver. The web server does not send the reply back to the router but sends it back directly to 10.0.0.2 with a source IP address in the reply of 10.0.0.3;
  4. The client receives the reply packet, but it discards it because it expects a packet back from 172.16.16.1, and not from 10.0.0.3;

To resolve this issue, we will configure a new src-nat rule (the hairpin NAT rule) as follows:

/ip firewall natadd action=masquerade chain=srcnat dst-address=10.0.0.3 out-interface=LAN protocol=tcp src-address=10.0.0.0/24

After configuring the rule above:

  1. the client sends a packet with a source IP address of 10.0.0.2 to a destination IP address of 172.16.16.1 on port 443 to request some web resources;
  2. the router destination NATs the packet to 10.0.0.3 and replaces the destination IP address in the packet accordingly. It also source NATs the packet and replaces the source IP address in the packet with the IP address on its LAN interface. The destination IP address is 10.0.0.3, and the source IP address is 10.0.0.1;
  3. the web server replies to the request and sends the reply with a source IP address of 10.0.0.3 back to the router’s LAN interface IP address of 10.0.0.1;
  4. the router determines that the packet is part of a previous connection and undoes both the source and destination NAT, and puts the original destination IP address of 10.0.0.3 into the source IP address field, and the original source IP address of 172.16.16.1 into the destination IP address field

 

 

Hairpin NAT

Hairpin hálózati címfordítás (NAT Loopback) azt jelenti, hogy a LAN-on lévő eszköz az átjáró útválasztójának nyilvános IP-címén keresztül hozzá tud férni egy másik géphez a LAN-on.

A fenti példában az átjáró útválasztója a következő dst-nat konfigurációs szabállyal rendelkezik:

/ip firewall nat add chain=dstnat action=dst-nat dst-address=172.16.16.1 dst-port=443 to-addresses=10.0.0.3 to-ports=443 protocol=tcp

Amikor egy felhasználó az otthoni számítógépről kapcsolatot létesít a webszerverrel, az útválasztó a DST NAT-ot hajtja végre a konfigurált módon:

  1. az ügyfél 192.168.88.1 forrás IP-című csomagot küld a 443-as porton lévő 172.16.16.1 cél IP-címre, hogy kérjen néhány webes erőforrást;
  2. az útválasztó célállomása a NAT a 10.0.0.3 csomagot, és ennek megfelelően lecseréli a cél IP-címét a csomagban. A forrás IP-címe változatlan marad: 192.168.88.1;
  3. a szerver válaszol az ügyfél kérésére, és a válaszcsomag forrás IP-címe 10.0.0.3 és cél IP-címe 192.168.88.1.
  4. az útválasztó megállapítja, hogy a csomag egy korábbi kapcsolat része, és visszavonja a cél NAT-ot, és az eredeti cél IP-címet a forrás IP-cím mezőbe helyezi. A cél IP-címe 192.168.88.1, a forrás IP-címe pedig 172.16.16.1;
  5. Az ügyfél megkapja a várt válaszcsomagot, és a kapcsolat létrejön;

Probléma lesz azonban, ha a webszerverrel azonos hálózaton lévő kliens kapcsolatot kér a webszerver nyilvános IP-címéhez:

  1. a kliens 10.0.0.2 forrás IP-című csomagot küld a 443-as porton lévő 172.16.16.1 cél IP-címre, hogy kérjen néhány webes erőforrást;
  2. az útválasztó célállomása a csomagot 10.0.0.3-ra NATolja, és ennek megfelelően lecseréli a cél IP-címét a csomagban. A forrás IP-címe változatlan marad: 10.0.0.2;
  3. a szerver válaszol a kliens kérésére. A kérelem forrás IP-címe azonban ugyanazon az alhálózaton található, mint a webszerver. A webszerver nem küldi vissza a választ az útválasztónak, hanem közvetlenül a 10.0.0.2-re küldi vissza a 10.0.0.3 válaszban szereplő forrás IP-címmel;
  4. A kliens megkapja a válaszcsomagot, de elveti, mert a 172.16.16.1-től vár vissza csomagot, és nem a 10.0.0.3-tól;

A probléma megoldása érdekében az alábbiak szerint konfigurálunk egy új src-nat szabályt (a hajtű NAT szabályt):

/ip firewall natadd action=masquerade chain=srcnat dst-address=10.0.0.3 out-interface=LAN protocol=tcp src-address=10.0.0.0/24

A fenti szabály konfigurálása után:

  1. a kliens 10.0.0.2 forrás IP-című csomagot küld a 443-as porton lévő 172.16.16.1 cél IP-címre, hogy kérjen néhány webes erőforrást;
  2. az útválasztó célállomása a csomagot 10.0.0.3-ra NATolja, és ennek megfelelően lecseréli a cél IP-címét a csomagban. Ezenkívül a csomagot NAT-ként adja, és a csomagban lévő forrás IP-címet a LAN-interfészén lévő IP-címre cseréli. A cél IP-címe 10.0.0.3, a forrás IP-címe pedig 10.0.0.1;
  3. a webszerver válaszol a kérésre, és a választ 10.0.0.3-as forrás IP-címmel visszaküldi az útválasztó 10.0.0.1-es LAN-interfészének IP-címére;
  4. az útválasztó megállapítja, hogy a csomag egy korábbi kapcsolat része, és visszavonja mind a forrás, mind a cél NAT-ot, és az eredeti 10.0.0.3 cél IP-címet a forrás IP-cím mezőbe, az eredeti 172.16.16.1-es forrás IP-címet pedig a cél IP-cím mezőjét

Hozzászólások lezárva.