1 NAT
NAT(Network Address Translation)1 : 网络地址转换。是一种重写 IP Packet 的 Source IP 或 Destination IP 的技术,用来解决私有地址无法和外部通信的问题,也大大的缓解了IPv4的枯竭问题,同时促进了互联网的蓬勃发展。
假设原始的 IP Packet 如下 :
| Source IP | Destination IP |
|---|---|
| 192.168.2.2 | 9.9.9.9 |
1.1 SNAT
SNAT(Source Network Address Translation) : 源地址转换,重写的是 Source IP。
重写后如下 :
| Source IP | Destination IP |
|---|---|
| 1.1.1.1 | 9.9.9.9 |
1.2 DNAT
DNAT(Destination Network Address Translation) : 目标地址转换,重写的是 Destination IP。
重写后如下 :
| Source IP | Destination IP |
|---|---|
| 192.168.2.2 | 2.2.2.2 |
2 PAT
PAT(Port Address Translation) : 端口地址转换(或者称为Port Forwarding),是一种重写传输层的 Source Port 或者 Destination Port 的技术(隶属于 NAT 体系)。
假设原始的数据包如下 :
| Source IP | Source Port | Destination IP | Destination Port |
|---|---|---|---|
| 192.168.2.2 | 6666 | 9.9.9.9 | 80 |
Destination Port重写后如下 :
| Source IP | Source Port | Destination IP | Destination Port |
|---|---|---|---|
| 192.168.2.2 | 6666 | 9.9.9.9 | 8080 |
3 NAPT
通常情况下NAT和PAT会被同时搭配组合使用, 这时通常称为 NAPT(Network Address Port Translation)。
比如最常见的例子是重写私有地址端口192.168.2.2:6666为公网的IP和端口1.1.1.1:7777,以此可以实现私有地址访问公网的目的。重写后如下 :
| Source IP | Source Port | Destination IP | Destination Port |
|---|---|---|---|
| 1.1.1.1 | 7777 | 9.9.9.9 | 80 |
或者重写目标地址的IP和端口号。比如把发往 9.9.9.9:80 的数据重写为发往 2.2.2.2:8080,以此可以可以实现数据包转发的目的(如果可以动态的轮询转发给多个IP, 则也可以实现负载均衡的目的)。重写后如下 :
| Source IP | Source Port | Destination IP | Destination Port |
|---|---|---|---|
| 192.168.2.2 | 6666 | 2.2.2.2 | 8080 |
根据NAPT设备对转换的策略,可以得到一下4种类型的NAPT。示例如下 :
| Type | Source > Destination | NAPT | NAPT Allowed IP | NAPT Allowed Port | |
|---|---|---|---|---|---|
| Cone | Full | 192.168.2.2:6666 > 9.9.9.9:80 | 1.1.1.1:7777 | any | any |
| 192.168.2.2:6666 > 2.2.2.2:33 | any | any | |||
| Address Restricted | 192.168.2.2:6666 > 9.9.9.9:80 | 1.1.1.1:7777 | 9.9.9.9 | any | |
| 192.168.2.2:6666 > 2.2.2.2:33 | 2.2.2.2 | any | |||
| Address Port Restricted | 192.168.2.2:6666 > 9.9.9.9:80 | 1.1.1.1:7777 | 9.9.9.9 | 80 | |
| 192.168.2.2:6666 > 2.2.2.2:33 | 2.2.2.2 | 33 | |||
| Symmetric | 192.168.2.2:6666 > 9.9.9.9:80 | 1.1.1.1:7777 | 9.9.9.9 | 80 | |
| 192.168.2.2:6666 > 2.2.2.2:33 | 1.1.1.1:7878 | 2.2.2.2 | 33 | ||
3.1 Cone NAT
由 Source 方发送数据包触发,根据 Source IP:Source Port 为唯一标识,在NAPT中建立映射关系(即使 Destination 不同也只会建立一个映射关系)。
3.1.1 Full Cone NAT
映射建立后,允许外部的 Any IP : Any Port 访问映射的 NAT IP:NAT Port。
3.1.2 Address Restricted Cone NAT
映射建立后,允许外部的 Destination IP : Any Port 访问映射的 NAT IP:NAT Port。
3.1.3 Address Port Restricted Cone NAT
映射建立后,允许外部的 Destination IP : Destination Port 访问映射的 NAT IP:NAT Port。
3.2 Symmetric NAT
由Source方发送数据包触发,根据 Source IP:Source Port 和 Destination IP:Destination Port 为唯一标识,在NAPT中建立映射关系。
映射建立后,允许外部的 Destination IP : Destination Port 访问映射的 NAT IP:NAT Port。
4 Soluction
4.1 DMZ(Demilitarized Zone)
https://en.wikipedia.org/wiki/DMZ_(computing)
4.2 UPnP(Universal Plug and Play)
https://en.wikipedia.org/wiki/Universal_Plug_and_Play
5 Reference
https://tools.ietf.org/html/rfc2663