CISCO UNICAST NAT工作原理

NAT技術的出現源於對私有網絡安全性的要求以及IPv4地址不夠用的現狀。Cisco IOS所支持的常用NAT技術有以下幾種。今天發現公司同事對Cisco NAT的實現理解的不好,導致配置出問題。這裏就簡單講解一下Cisco NAT的實現原理。
 
1. static NAT,內部ip地址和外部ip地址的一一對應。
 
2. dynamic NAT,內部ip地址和外部ip地址的一對一關係,和static NAT不同的是,內部ip地址是對應的外部ip地址池,每次轉換的時候會使用外部ip 地址池中的未用地址做翻譯。
 
3. overloading NAT,也就是常用的端口地址翻譯,PAT。多個內部ip地址可以通過這種技術映射到一個外部ip地址,因爲一個連接是由源地址,源端口,目的地址,目的端口和協議這五元組構成的。
 
當然,還有其他一些NAT的實現技術,不過上面三種是最常用的。這些都是基本的NAT規則,具體大家可以參考下面這篇文檔。
 
 
這裏重點想要講解的是NAT和路由表,NAT和ACL以及IPSec報文穿越情況下,報文的執行順序和動作。下面這張表在理解NAT以及配置NAT的時候是要牢記的。
 
通常情況下,NAT路由器可以對源地址,目的地址,做翻譯。上圖左側是ip nat inside,右側是ip nat outside。當然NAT也可以工作在IP層之上的傳輸層甚至應用層,這些以後有機會在講述。
 
ip nat inside source 和 ip nat outside source 是常用的配置命令,下面就通過一個例子來分析NAT的工作原理。
 
以下的工作原則也是必須要牢記的。
 
當流量的方向是從 outside到inside的時候,首先會查找並匹配NAT規則並作地址轉換,然後再根據目標地址查詢路由轉發表。當流量方向是從inside到 outside的時候,首先會根據目標地址查詢路由轉發表(如果沒有匹配路由,會丟棄),然後再查找並匹配NAT規則並作地址轉換。
 
ip nat outside source static
 •Translates the source of the IP packets that travel outside to inside.
•Translates the destination of the IP packets that travel inside to outside.
 
ip nat inside source static
 •Translates the source of IP packets that travel inside to outside.
•Translates the destination of the IP packets that travel outside to inside.
 
 
 
例子拓撲:
 
r1
 
interface Loopback0
 ip address 172.1.1.1 255.255.255.0
!        
interface Ethernet1/0
 ip address 10.1.1.1 255.255.255.0
 duplex half
!
!
ip classless
ip route 0.0.0.0 0.0.0.0 10.1.1.2
!
r2
 
interface Ethernet1/0
 ip address 10.1.1.2 255.255.255.0
 ip nat inside
 no ip route-cache cef
 no ip route-cache
 duplex half
!
interface Ethernet1/1
 ip address 10.2.2.2 255.255.255.0
 ip nat outside
 no ip route-cache cef
 no ip route-cache
 duplex half
!
ip nat inside source static 172.1.1.1 100.1.1.1
ip nat outside source static 172.3.3.3 100.3.3.3

ip classless
ip route 100.3.3.3 255.255.255.255 10.2.2.3
ip route 172.1.1.1 255.255.255.255 10.1.1.1
 
r3
!
interface Loopback0
 ip address 172.3.3.3 255.255.255.0
!
interface Ethernet1/1
 ip address 10.2.2.3 255.255.255.0
 duplex half
!
ip classless
ip route 0.0.0.0 0.0.0.0 10.2.2.2
 
 
我們這裏重點關注的就是NAT router r2的NAT配置和路由配置,根據以上所寫的綠色規則,如果從r1的環回口ping r3的環回口(ping 100.3.3.3 from 172.1.1.1),
1. ping request就是從inside到outside的流量方向,
2. 首先會在r2上查找路由轉發表,是否有到目的地100.3.3.3的路由,這也是爲什麼r2上有一條到100.3.3.3的路由的原因,如果沒有這一條路由,路由器會丟棄報文並會destination unreachable差錯報文。
3. 當有路由匹配的時候,接着會查詢是否有匹配的NAT規則,如果有就做NAT地址轉換。這裏有兩條NAT規則匹配,根據上面的轉換原則,源地址會被轉換成100.1.1.1,目的地址會被轉換成172.3.3.3。
4. 轉發報文出e1/1口(ping 172.3.3.3 from 100.1.1.1)
 
同樣的情況,當r3的環回口收到ping request(from 100.1.1.1 to 172.3.3.3)的時候,ping reply(reply from 172.3.3.3 to 100.1.1.1)會發到r2的e1/1口。
1. 根據規則,流量方向是從outside到inside方向,會首先查找匹配NAT規則,並作NAT轉換,這裏兩條NAT規則匹配,轉換後的報文是(reply from 100.3.3.3 to 172.1.1.1),
2. 然後,會根據轉換後的目的地址查詢r2的路由轉發表,如果沒有到172.1.1.1的路由條目,r2會丟棄報文並回destination unreachable差錯報文,所以這裏要配一條到172.1.1.1的路由,
3. 轉發報文出e1/0口(reply 172.1.1.1 from 100.3.3.3)
 
 
r2上debug的情況如下,
r2#debug ip pac
r2#debug ip packet
IP packet debugging is on
r2#
r2#
r2#deb
r2#debug ip na
r2#debug ip nat
IP NAT debugging is on
r2#
r2#
從inside到outside方向的流量:
*Sep 30 10:24:12.019: IP: tableid=0, s=172.1.1.1 (Ethernet1/0), d=100.3.3.3 (Ethernet1/1), routed via FIB
*Sep 30 10:24:12.019: NAT: s=172.1.1.1->100.1.1.1, d=100.3.3.3 [10]
*Sep 30 10:24:12.019: NAT: s=100.1.1.1, d=100.3.3.3->172.3.3.3 [10]

*Sep 30 10:24:12.019: IP: s=100.1.1.1 (Ethernet1/0), d=172.3.3.3 (Ethernet1/1), g=10.2.2.3, len 100, forward
 
從outside到inside方向的流量:
*Sep 30 10:24:12.127: NAT*: s=172.3.3.3->100.3.3.3, d=100.1.1.1 [10]
*Sep 30 10:24:12.127: NAT*: s=100.3.3.3, d=100.1.1.1->172.1.1.1 [10]

*Sep 30 10:24:12.127: IP: tableid=0, s=100.3.3.3 (Ethernet1/1), d=172.1.1.1 (Ethernet1/0), routed via FIB
*Sep 30 10:24:12.127: IP: s=100.3.3.3 (Ethernet1/1), d=172.1.1.1 (Ethernet1/0), g=10.1.1.1, len 100, forward

*Sep 30 10:24:12.203: IP: tableid=0, s=172.1.1.1 (Ethernet1/0), d=100.3.3.3 (Ethernet1/1), routed via FIB
*Sep 30 10:24:12.203: NAT: s=172.1.1.1->100.1.1.1, d=100.3.3.3 [11]
*Sep 30 10:24:12.203: NAT: s=100.1.1.1, d=100.3.3.3->172.3.3.3 [11]
*Sep 30 10:24:12.203: IP: s=100.1.1.1 (E
r2#thernet1/0), d=172.3.3.3 (Ethernet1/1), g=10.2.2.3, len 100, forward
*Sep 30 10:24:12.251: NAT*: s=172.3.3.3->100.3.3.3, d=100.1.1.1 [11]
*Sep 30 10:24:12.251: NAT*: s=100.3.3.3, d=100.1.1.1->172.1.1.1 [11]
*Sep 30 10:24:12.251: IP: tableid=0, s=100.3.3.3 (Ethernet1/1), d=172.1.1.1 (Ethernet1/0), routed via FIB
*Sep 30 10:24:12.251: IP: s=100.3.3.3 (Ethernet1/1), d=172.1.1.1 (Ethernet1/0), g=10.1.1.1, len 100, forward
*Sep 30 10:24:12.299: IP: tableid=0, s=172.1.1.1 (Ethernet1/0), d=100.3.3.3 (Ethernet1/1), routed via FIB
*Sep 30 10:24:12.299: NAT: s=172.1.1.1->100.1.1.1, d=100.3.3.3 [12]
*Sep 30 10:24:12.299: NAT: s=100.1.1.1, d=100.3.3.3->172.3.3.3 [12]
*Sep 30 10:24:12.299: IP: s=100.1.1.1 (Ethernet1/0), d=172.3.3.3 (Ethernet1/1), g=10.2.2.3, len 100, forward
*Sep 30 10:24:12.347: NAT*: s=172.3.3.3->100.3.3.3, d=100.1.1.1 [12]
*Sep 30 10:24:12.347: NAT*: s=100.3.3.3, d=100.1.1.1->172.1.1.1 [12]
*Sep 30 10:24:12.347: IP: tableid=0, s=100.3.3.3 (Ethernet1/1), d=172.1.1.1 (
r2#Ethernet1/0), routed via FIB
*Sep 30 10:24:12.347: IP: s=100.3.3.3 (Ethernet1/1), d=172.1.1.1 (Ethernet1/0), g=10.1.1.1, len 100, forward
*Sep 30 10:24:12.407: IP: tableid=0, s=172.1.1.1 (Ethernet1/0), d=100.3.3.3 (Ethernet1/1), routed via FIB
*Sep 30 10:24:12.407: NAT: s=172.1.1.1->100.1.1.1, d=100.3.3.3 [13]
*Sep 30 10:24:12.407: NAT: s=100.1.1.1, d=100.3.3.3->172.3.3.3 [13]
*Sep 30 10:24:12.407: IP: s=100.1.1.1 (Ethernet1/0), d=172.3.3.3 (Ethernet1/1), g=10.2.2.3, len 100, forward
*Sep 30 10:24:12.547: NAT*: s=172.3.3.3->100.3.3.3, d=100.1.1.1 [13]
*Sep 30 10:24:12.547: NAT*: s=100.3.3.3, d=100.1.1.1->172.1.1.1 [13]
*Sep 30 10:24:12.547: IP: tableid=0, s=100.3.3.3 (Ethernet1/1), d=172.1.1.1 (Ethernet1/0), routed via FIB
*Sep 30 10:24:12.547: IP: s=100.3.3.3 (Ethernet1/1), d=172.1.1.1 (Ethernet1/0), g=10.1.1.1, len 100, forward
*Sep 30 10:24:12.595: IP: tableid=0, s=172.1.1.1 (Ethernet1/0), d=100.3.3.3 (Ethernet1/1), routed via FIB
*Sep 30 10:24:12.595: NAT: s=172.1.1.1->
r2#100.1.1.1, d=100.3.3.3 [14]
*Sep 30 10:24:12.595: NAT: s=100.1.1.1, d=100.3.3.3->172.3.3.3 [14]
*Sep 30 10:24:12.595: IP: s=100.1.1.1 (Ethernet1/0), d=172.3.3.3 (Ethernet1/1), g=10.2.2.3, len 100, forward
*Sep 30 10:24:12.611: NAT*: s=172.3.3.3->100.3.3.3, d=100.1.1.1 [14]
*Sep 30 10:24:12.611: NAT*: s=100.3.3.3, d=100.1.1.1->172.1.1.1 [14]
*Sep 30 10:24:12.611: IP: tableid=0, s=100.3.3.3 (Ethernet1/1), d=172.1.1.1 (Ethernet1/0), routed via FIB
*Sep 30 10:24:12.611: IP: s=100.3.3.3 (Ethernet1/1), d=172.1.1.1 (Ethernet1/0), g=10.1.1.1, len 100, forward
 
 
 
 
!所以,再設計NAT的時候,不但要考慮地址轉換,還要考慮路由!
 
 
下面,我們再看一種更復雜的情況,就是在有配置訪問控制列表,並有IPSec報文穿越NAT的情況下,IPSec報文在ACL/NAT/Routing的處理順序。
請參考博客 Cisco IOS Unicast NAT 工作原理 [二]

本文出自 “jasonccie” 博客,請務必保留此出處http://jasonccie.blog.51cto.com/2143955/398815

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章