Timetombs

泛义的工具是文明的基础,而确指的工具却是愚人的器物

66h / 116a
,更新于 2024-04-06T22:06:23Z+08:00 by   35f4f88

[计算机网络] NAT

版权声明 - CC BY-NC-SA 4.0

1 NAT

NAT(Network Address Translation)1 : 网络地址转换。是一种重写 IP PacketSource IPDestination IP 的技术,用来解决私有地址无法和外部通信的问题,也大大的缓解了IPv4的枯竭问题,同时促进了互联网的蓬勃发展。

假设原始的 IP Packet 如下 :

Source IPDestination IP
192.168.2.29.9.9.9

1.1 SNAT

SNAT(Source Network Address Translation) : 源地址转换,重写的是 Source IP

重写后如下 :

Source IPDestination IP
1.1.1.19.9.9.9

1.2 DNAT

DNAT(Destination Network Address Translation) : 目标地址转换,重写的是 Destination IP

重写后如下 :

Source IPDestination IP
192.168.2.22.2.2.2

2 PAT

PAT(Port Address Translation) : 端口地址转换(或者称为Port Forwarding),是一种重写传输层的 Source Port 或者 Destination Port 的技术(隶属于 NAT 体系)。

假设原始的数据包如下 :

Source IPSource PortDestination IPDestination Port
192.168.2.266669.9.9.980

Destination Port重写后如下 :

Source IPSource PortDestination IPDestination Port
192.168.2.266669.9.9.98080

3 NAPT

通常情况下NAT和PAT会被同时搭配组合使用, 这时通常称为 NAPT(Network Address Port Translation)。

比如最常见的例子是重写私有地址端口192.168.2.2:6666为公网的IP和端口1.1.1.1:7777,以此可以实现私有地址访问公网的目的。重写后如下 :

Source IPSource PortDestination IPDestination Port
1.1.1.177779.9.9.980

或者重写目标地址的IP和端口号。比如把发往 9.9.9.9:80 的数据重写为发往 2.2.2.2:8080,以此可以可以实现数据包转发的目的(如果可以动态的轮询转发给多个IP, 则也可以实现负载均衡的目的)。重写后如下 :

Source IPSource PortDestination IPDestination Port
192.168.2.266662.2.2.28080

根据NAPT设备对转换的策略,可以得到一下4种类型的NAPT。示例如下 :

TypeSource > DestinationNAPTNAPT Allowed IPNAPT Allowed Port
ConeFull192.168.2.2:6666 > 9.9.9.9:801.1.1.1:7777anyany
192.168.2.2:6666 > 2.2.2.2:33anyany
Address Restricted192.168.2.2:6666 > 9.9.9.9:801.1.1.1:77779.9.9.9any
192.168.2.2:6666 > 2.2.2.2:332.2.2.2any
Address Port Restricted192.168.2.2:6666 > 9.9.9.9:801.1.1.1:77779.9.9.980
192.168.2.2:6666 > 2.2.2.2:332.2.2.233
Symmetric192.168.2.2:6666 > 9.9.9.9:801.1.1.1:77779.9.9.980
192.168.2.2:6666 > 2.2.2.2:331.1.1.1:78782.2.2.233

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

Full Cone NAT

3.1.2 Address Restricted Cone NAT

映射建立后,允许外部的 Destination IP : Any Port 访问映射的 NAT IP:NAT Port

Address Restricted Cone NAT

3.1.3 Address Port Restricted Cone NAT

映射建立后,允许外部的 Destination IP : Destination Port 访问映射的 NAT IP:NAT Port

Port Restricted Cone NAT

3.2 Symmetric NAT

Source方发送数据包触发,根据 Source IP:Source PortDestination IP:Destination Port 为唯一标识,在NAPT中建立映射关系。

映射建立后,允许外部的 Destination IP : Destination Port 访问映射的 NAT IP:NAT Port

Symmetric NAT

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

https://www.youtube.com/watch?v=QBqPzHEDzvo

https://www.youtube.com/watch?v=wg8Hosr20yw

上一篇 : [计算机网络] TCP
下一篇 : [计算机网络] IO 模型