Simple to understand NAT

Simple to understand NAT

Writing this article is for your own reference = W =

1 Introduction

  • NAT (NetworkAddressTranslation), i.e., network address translation. Using a gateway, the network can not access the external IP address into the IP address of the external access, enabling the internal network to access external networks.
  • classification
    • Static NAT (Static NAT)
    • Dynamic NAT (Pooled NAT)
    • Network Address Port Translation NAPT (Port-Level NAT) -> The most common
  • The main function
    • Camouflage packets, load balancing, port forwarding, transparent proxy
  • example
    • Broadband operators as we usually install (under normal circumstances, we have comes from within the network IP, unified converted by a NAT gateway has a public IP to ensure that we access the Internet)
    • NAT virtual machine transition

2. Static NAT (Static NAT)

  • feature
    • A network to an external IP network IP, will not change
    • Extranet direct access to the network
  • schematic diagram
    Static NAT schematic
  • Interpretation (cited chestnuts)
    • A server makes a request to the server X (src = 192.168.0.101, dst = 14.215.177.39), due to the internal network, a request is sent to the first gateway (192.168.0.1)
    • NAT gateway receives the request, in accordance with the set IP mapping table, the conversion request (src = 14.215.177.21, dst = 14.215.177.39), sends the request to the server via the network X
    • X server receiving the request, responds (src = 14.215.177.39, dst = 14.215.177.21), the request will first come NAT gateway
    • NAT gateway receives the request, in accordance with the set IP mapping table, the conversion request (src = 14.215.177.39, dst = 192.168.0.101)
    • A successful response from the server receives the server's x

3. Dynamic NAT (Pooled NAT)

  • feature
    • A network for a random external IP network IP, because the presence of the external network IP pool
    • Direct access to external network within the network is difficult
  • schematic diagram
    Dynamic NAT schematic
  • Explanation
    • With fewer external IP network, each server within the network can not be assigned to all external network IP. This time we need to share IP, IP allocation from the pool when used without the releases.
    • 传输方式和静态相似,需要注意一下几点:
      • 在NAT网关处分配IP后,NAT网关会做IP映射记录,用于将外部服务器的响应发送到正确的内网IP服务器
      • 在NAT网关处分配IP后,一定时间内不使用,将会抹去映射记录,该IP进入空闲状态,可以被分配给其他内网服务器
      • 由于内网访问外网的IP是动态的,所以外网向内网发送请求时难以成功

4. 网络地址端口转换NAPT(Port-Level NAT)

  • 特征
    • 多个内网IP对一个外网IP的不同端口
    • 外网难以直接访问内网
  • 示意图
    Network Address Port Translation NAPT schematic
  • 解释
    • NAT网关只需一个外网IP即可完成外网访问转换,极大地降低了外网IP的使用量
    • NAPT传输方式依旧和前面的相似,只有一个外网IP的情况下,将映射关系改为了“内网IP -> 随机端口”,用端口来区分内网IP,保证正确的解析外网服务器的响应
    • NAPT内部还有几个分类:全锥NAT、限制性锥NAT、端口限制性锥NAT、对称NAT等

5. 一些问题

  • 平时运营商给个人用户设置网络,采用的是什么方式?
    • 采用NAPT,这样节约公网IP
  • 如果使用的是网络地址端口转换NAPT,外网真的没法向内网发起请求吗?
    • 可以,通常有2种方式:
      • 服务器中转:
        • 在公网准备一台服务器S,内网服务器A与服务器S保持心跳连接。
        • 外网服务器B若想要访问内网服务器A,需向服务器S发起请求,服务器S再向内网服务器A获取资源,返回给外网服务器B。
        • 所有的数据传输都需要经过服务器S,性能较低。内网穿透方式不行时,可以这样干。
      • 内网穿透:
        • 在公网准备一台服务器S,服务器A与服务器S保持心跳连接,S获得服务器A的IP、Port。
        • B服务器向服务器S发送消息,表明它想访问服务器A。S获得服务器B的IP、Port,同时向A发送B的IP、Port,向B发送A的IP、Port。
        • 服务器A通过从服务器S处获得的B的IP、Port向服务器B发送消息。显然因为服务器B的NAT网关没有相关映射记录,服务器B收不到该消息。但是因为服务器A发送了这样一条消息,服务器A的NAT网关会记录下 A -> B的映射。
        • 同时服务器B也会向A发送消息,此时服务器B的NAT网关会记录下 B -> A的映射
        • 至此,服务器A与服务器B的网络通了
        • 成功率:
          • 静态端口映射的NAT成功率较高
          • 动态端口映射的NAT成功率较低
  • 描述一下NAT的功能?
    • 数据伪装:内网IP地址通过NAT被转换为外网IP,外网服务器只知道数据是NAT转换后的外网IP地址传过来的,这样内网服务器不用暴露在外网。
    • 平衡负载:NAT网关将内网多台服务器IP映射到一个外网IP,当外网服务器直接请求外网IP时,NAT网关会对内网的服务器进行轮询,再响应外网服务器,这样就实现了负载均衡。但是NAT无法感知内网服务器故障,即便内网服务器出了问题,依然会转发请求。
    • 端口转发:为内网某个服务器配置端口映射,例如 9000端口 -> 内网服务器A(192.168.0.100),即可将访问NAT网关该9000端口的请求转向内网服务器A。
    • 透明代理:内网服务器只需要设置默认网关为该NAT网关即可,NAT网关再去代理与外网连接,内网完全感受不到代理的存在。

6. 其他

  • Linux监控HTTP/TCP连接(三次握手/四次挥手)
    • 安装:yum install tcpdump
    • 开启监控:tcpdump -nn -i eth0 arp or port 80 (即监控网卡eth0的arp协议或80端口)
    • 发送请求:
      • curl www.baidu.com 80
      • 或 arp -d 192.168.0.1 && nc www.baidu.com 80
  • Windows监控HTTP/TCP连接(三次握手/四次挥手)
    • windows平台需要安装windump
    • Open monitoring
      • This machine is listed in the card: WinDump.exe -D
      • Card on a monitor connected to the www.baidu.com: WinDump.exe -nn -i "\ Device \ NPF_ {34D20894-77D0-40ED-ACE2-B658D462584B}" host "www.baidu.com" and "tcp"
    • send request
      • curl www.baidu.com
Published 128 original articles · won praise 45 · Views 150,000 +

Guess you like

Origin blog.csdn.net/alionsss/article/details/103543758