NAT Type 及STUN

  今天整理下NAT的類型,我想要更好的理解NAT的不同類型的不同表現,可以藉助學習STUN協議的測試流程,通過STUN的判斷標準,進一步瞭解不同NAT的表現,希望帶着下面一個問題來進行學習, 爲什麼stun只能不能解決對稱的nat類型呢.?

 

 

一, NAT 的具體流程,

 

  請看下圖,   圖示爲LAN端一PC和WAN端兩Server, 中間爲NAT的Soho Router,

 1, Client A 使用10.0.0.1:1234 需要和Server 1的18.181.0.31:1235 建立session, 假設建立UDP Session,則UDP pckets經過Router時, NAT會將該UDP包中的Sourec IP和Source Port進行修改, Source IP = Router's WAN IP, Source Port=WAN PORT = 62000(一般爲者隨機端口), 並重新計算crc將包發給S1;

 2, 在NAT將包重組,併發送出去的同時, 創建並維護了一張NAT Address Port Table表. 

Client Source IP:Client Sourece Port -- Wan IP:Wan port -- DesIP:DesPort(Server IP:ServerPort)

 3, Server 1收到該UDP Packets後,並進行回覆. 回覆的UDP包的SIP:SPORT=18.181.0.31:1235, DIP:DPORT=Router's WAN IP: WAN port=155.99.25.11:62000;

 4, 當NAT Router收到該UDP數據包後, 會根據step 2中創建的NAT Table中的表項進行查找, 根據該包中的Desport:DesPort 和Source IP:Sport找到對應的Client IP:Port,  並修改該Packets的DIP:Dport爲ClientIP:Port,併發送該Client A; 最終完成整個NAT UDP的流程.

 

Cone nat

 

 

 

 

 

二, NAT 的類型;

 

1, NAT Type

NAT 從實現分爲Cone NAT和Symmtric NAT兩類。 最主要區別在於內網的某個Client使用相同的(Source IP+Source Port)組合訪問不同的外網Server時,在NAT在出口方向上的打洞方式(Cone和Symmtric 映射表現的方式)以及入口方向上的源IP及源Port的條件限制檢測(Cone內部3中不同的NAT方式對入口報文有不同的來源限制)。

 

 若上圖所示的Client A使用同樣的Source Port要和Server 2建立UDP session, 則不同的NAT type的行爲將會不一樣.

1.1, Cone NAT:  若相同的Client Source IP: Source Port在NAT Router上創建一個相同對應關係的WANIP:WAN Port. 爲Cone NAT. 如上圖所示, Session 2使用的Wan Source Port 仍爲62000;

 

1.2, Symmtric NAT: 若相同的Client Source IP: Source Port在NAT Router上創建不同對應關係的WANIP:WAN Port. 爲Symmtric NAT; 若上圖中, Session 2使用的Wan Source Port爲62001;  相比Cone NAT, Symmtric的限制更嚴格, 更安全;

 

2, Detail Information of Cone NAT

Cone  NAT,即錐行NAT,  從抽象上來理解爲, NAT 使用一個洞,同WAN端很多臺Server進行通信, 如下圖所示,(前提條件爲Client IP:Source Port 相同).但是根據NAT Router對WAN端入包的判斷嚴格程度不同, Cone NAT又可分爲以下3中types:

根據本人理解:  

Full Cone NAT: 在NAT Router Wan端收到數據包,並根據NAT Table找到相應的Client後,直接修改併發送; 不關心Source IP:Port.

Restricted Cone NAT: 在Full Cone的基礎上,增加了對Source IP的判斷, 若該Source IP並不是之前記錄的DIP(Server IP), 則Drop掉;

Port Restricted NAT: 在Restricted Cone的基層上又增加了對Source Port的檢測, 若該SourceIP:Source Port != 之前記錄的DIP:DPort(ServerIP:Server:Port),則Drop掉. 

 

 

 

2.1 Full cone nat

  Once an internal address (iAddr:port1) is mapped to an external address (eAddr:port2), any packets from iAddr:port1 will be sent through eAddr:port2. Any external host can send packets to iAddr:port1 by sending packets to eAddr:port2.

 

2.2 Restricted nat

 §Once an internal address (iAddr:port1) is mapped to an external address (eAddr:port2), any packets from iAddr:port1 will be sent through eAddr:port2. An external host (hostAddr:any) can send packets to iAddr:port1 by sending packets to eAddr:port2 only if iAddr:port1 had previously sent a packet to hostAddr:any. "any" means the port number doesn't matter.

2.3 Port Restricted nat 

§Like a restricted cone NAT, but the restriction includes port numbers.
§Once an internal address (iAddr:port1) is mapped to an external address (eAddr:port2), any packets from iAddr:port1 will be sent through eAddr:port2. An external host (hostAddr:port3) can send packets to iAddr:port1 by sending packets to eAddr:port2 only if iAddr:port1 had previously sent a packet to hostAddr:port3.
 

3, Detail Information Of Symmetric NAT  

§Each request from the same internal IP address and port to a specific destination IP address and port is mapped to a unique external source IP address and port.
If the same internal host sends a packet even with the same source address and port but to a different destination, a different mapping is used.
§Only an external host that receives a packet from an internal host can send a packet back.

 

 

 三, STUN 及測試流程, 

STUN即簡單nat 穿越協議. 可以檢測設備當前NAT Router的類型,以及該NAT Map的ip和port以供應用程序使用. 下面就來說說stun是如何測試的.  簡單來說,就是修改stun server的ip和port給wan口進行發包,並根據lan client收到包的個數和其中的map address的內容來判斷nat的類型.  

首先,請看下圖

  Test1,  c1給s1發送一個包, 同時這個s1給c1回覆,並帶有map address(i1) 的ip 和port(p1);

  Test2,   s2給c1發包,其目的地址爲i1:p1, 若c1能收到,則爲full cone;收不到則爲restriected ;

  Test3,   c1給s2發包, 並收到s2給c1的回覆, 類似於test1, 帶有map address(i2)的ip和port (p2), 若P2!=P1,則表示c1給s2發送時有重新打了個洞, 則爲Symmetric NAT,

  Test4.  s1更換源端口給c1(i1:p1)發包,若c1不能接收到的話則爲port restricted;

最後附上一個流程圖.

 

四, 參考資料,
RFC1631.The IP Network Address Translator (NAT)
RFC2663.IP Network Address Translator (NAT) Terminology and Considerations
RFC3235.Network Address Translator (NAT)-Friendly Application
RFC4787.Network Address Translation (NAT) Behavioral Requirem

若各位有什麼問題,可以留言.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章