NAT technology explain

First, the origin of IPv4 and NAT

1, IPv4 protocol introduced

  February 3, 2011, IANA announced: IPv4 address space of the last five blocks of addresses have been assigned to the five regional committees subordinates. April 15, 2011, Asia-Pacific APNIC Commission announced that in addition to individual reserved addresses, IPv4 addresses all of the region almost exhausted. All of a sudden, IPv4 address as an endangered resource worth increased sharply.

  IPv4 ie internet protocol version 4 (Internet Protocol Version 4) is defined across a heterogeneous network interconnection super net assign globally unique IP address for each node of the Internet network. IPv4 uses 32bits integer expression of an address, the maximum address range is about 232 4300000000 . IP-founding period can be networked device point of view, such a large space has been hard to be a short run. However, the fact that far more than people imagine, the rapid growth of computer networks in the next few decades, the explosive growth in the number of network terminals.

  Worse, in order to facilitate routing and management, according to the address space of 4,300,000,000 different prefix length is divided into A, B, C, D, E class address and the network address retained.

  

  Address classification as follows (by default, the first eight bits to identify categories):

    A  00000000---01111111     0-127   

    B  10000000---10111111     128-191

    C  11000000---11011111     192-223

    D  11100000---11101111     224-239

    E  11110000---11111110     240-254

  IANA allocated to Class A network address large enterprises / organizations for some time. Class B network addresses assigned to medium-sized enterprises or educational institutions, a period of time. Such an IP address allocation strategy so that waste is very serious, many addresses are assigned out to be no real use, address consume quickly. It must be considered an alternative to IPv4, while taking measures to slow the depletion of IPv4 addresses. Therefore, network address translation --NAT debut.

2, NAT Introduction

  NAT is an amazing technology, it appears almost let IPv4 back to life. But also because NAT to the IP network model has had a profound impact on its shadow over the network every corner. According to a recent study, 70% of P2P users located within the NAT gateway. Because most major PC P2P running on the end user's PC, this figure means that the connection to the Internet through a NAT gateway. If coupled with 2G and 3G networking way of smart phones and other mobile terminals, users behind a NAT gateway, far more than the ratio.

  Seeking its source only to find a very strange fact: NAT this significant technology, there is not even recognized as the inventor. NAT first version of RFC, just summarizing the technology has been widely adopted.

Two, NAT working model and features

1, NAT conceptual model

  NAT (network address translation): Replace the address in the IP packet header . NAT is typically deployed in an organization's network egress position , by replacing the internal IP address of the network to provide connectivity to the public network reachability and the upper layer protocol is IP address of the exporter.

  RFC1918 specifies three paragraphs reserved Address: 10.0.0.0-10.255.255.255; 172.16.0.0-172.31.255.255; 192.168.0.0-192.168.255.255. In these three ranges are A, B, address segment class C, not more assigned to a specific user, the IANA as private address reserved

  These addresses can be in any organization or business use internal , and the difference is that other Internet addresses, only used internally , not as a global routing address. That is to say, the scope of management of these organizations addresses no longer made sense, either as a source address or destination address.

  For there is demand for Internet access and internal and private address network, it is necessary in the export position of the organization to deploy NAT gateways , when the message leaves the private network into the Internet, will replace the source IP address for the public network, usually the export of equipment interfaces address. 

  An external access request after reaching the target, the performance by the Organization initiated the export of equipment, and therefore requested a response from the server can be exported back to Internet gateway. Gateway then export destination IP address of the source host address private network, back inside. Completed such a request and response to the public network server in a case where no perceived by the ends of the communication private host. According to this model, a huge number of internal hosts no longer need a public IP address.

  

 

2, NAT processing the message key features

  We generally use private IP as an internal LAN host identification, the use of public IP as the Internet communication identity. Throughout the NAT, the most critical processes are the following:

  • Network is divided into two parts private network and public network, private network NAT gateway is provided to the public network routing exit position, two-way traffic must go through the NAT gateway;
  • Network access can only be initiated first by the private network side , the public can not take the initiative to access the private network host;
  • NAT gateway access is completed in both directions two address conversion or translation, the direction, to be replaced source information, destination information inbound do replacement;
  • NAT gateway exists on both sides of communication remains transparent;
  • NAT gateways in order to achieve two-way translation function, it is necessary to maintain an association table, the saved information session.

  These features are distinctive, but not absolute. The second feature which broke IP protocol architecture for all other nodes in the communication status, which is the biggest drawbacks of NAT, brought many problems to peer communication, of course, the appropriate means to overcome the emerged. In fact, the fourth point is NAT is committed to achieve, but in many cases, NAT did not do it, because in addition to the IP header, the upper layer communication protocols often carry IP address information internally. These we explain later.

3, Static NAT (one model)

  If an internal host occupies a unique public IP, this way is known as one model . In this embodiment, the upper layer protocol conversion is unnecessary as a public IP network can be uniquely corresponds to an internal host. Obviously, this approach does not make much sense to save public network IP, mainly in order to achieve some special networking requirements . For example the user wishes to hide the true internal IP host, or two overlapping IP addresses for communication networks.

  

 

4, dynamic NAT

  Dynamic NAT refers to the internal network private IP address is converted into a public IP address when, IP address is uncertain, random, all is authorized to access the Internet private IP address can be converted to any randomly assigned legal IP address. In other words, as long as the internal address specified which can be converted, as well as external address, which can be dynamically converted with legal address.

  Dynamic NAT router is arranged on a external IP address pool , when the internal computer and the external communication needs, taken on a dynamic external IP address from the pool, and bind their relations to the NAT table, after the communication, the external network IP was released, available for other internal IP address translation, the DHCP lease IP similarities.

  

  

5, NAT overload (often used)

  PAT (port address Translation, Port Address Translation, many model, also known as port address multiplexing) . NAT This is the most typical application scenarios: a network of organizations to deploy NAT gateways in the export position, all access to the public network performance as a host. This is called a many-to-model .

  In this manner, the outlet device occupies only one assigned by the Internet service provider public network IP address. Face internal private network and a huge number of hosts, if only a simple NAT replaces the IP address, the question arises: When there are multiple internal hosts to access the same server, the information from the return is not sufficient to distinguish the response should be forwarded to which internal hosts.

  In this case, it is necessary to distinguish the NAT device in accordance with the transport layer information or the upper layer protocols different sessions, and may want to identify the upper layer protocol conversion, such as TCP or UDP port numbers.

  这样NAT网关就可以将不同的内部连接访问映射到同一公网IP的不同传输层端口,通过这种方式实现公网IP的复用和解复用。这种方式也被称为端口转换PAT、NAPT或IP伪装,但更多时候直接被称为NAT,因为它是最典型的一种应用模式。

  

  如上例所示:客户端172.18.250.6和百度服务器202.108.22.5通信,172.18.250.6发送数据时,先转换为219.155.6.240:1723(任意>1024的随机端口),然后再利用这个身份发送数据给百度服务器,然后百度服务器回应数据并发送给219.155.6.240:1723,NAT网关检查自己的关联表,意识到这是自己地私网中172.18.250.6的数据包,然后把这个数据发送给客户端。

  总结:利用端口号的唯一性实现了公网ip转换为私网ip的这一步。PAT(NAT重载)能够使用传输层端口号来标识主机,因此,从理论上说,最多可让大约65000台主机共用一个公有IP地址。

6、NAT端口映射方式

  在一对多模型中,按照端口转换的工作方式不同,又可以进行更进一步的划分。为描述方便,以下将IP和端口标记为(nAddr:nPort),其中n代表主机或NAT网关的不同角色。

(1)全锥形NAT

  其特点为:一旦内部主机端口对(iAddr:iPort)被NAT网关映射到(eAddr:ePort),所有后续的(iAddr:iPort)报文都会被转换为(eAddr:ePort);任何一个外部主机发送到(eAddr:ePort)的报文将会被转换后发到(iAddr:iPort)。

(2)限制锥形NAT

  其特点为:一旦内部主机端口对(iAddr:iPort)被映射到(eAddr:ePort),所有后续的(iAddr:iPort)报文都会被转换为(eAddr:ePort);只有 (iAddr:iPort)向特定的外部主机hAddr发送过数据,主机hAddr从任意端口发送到(eAddr:ePort)的报文将会被转发到(iAddr:iPort)。

(3)端口限制锥形NAT

  其特点为:一旦内部主机端口对(iAddr:iPort)被映射到(eAddr:ePort),所有后续的(iAddr:iPort)报文都会被转换为(eAddr:ePort);只有(iAddr:iPort)向特定的外部主机端口对(hAddr:hPort)发送过数据,由 (hAddr:hPort)发送到(eAddr:ePort)的报文将会被转发到(iAddr:iPort)。

(4)对称型NAT

  其特点为:NAT网关会把内部主机“地址端口对”和外部主机“地址端口对”完全相同的报文看作一个连接,在网关上创建一个公网“地址端口对”映射进行转换,只有收到报文的外部主机从对应的端口对发送回应的报文,才能被转换。即使内部主机使用之前用过的地址端口对去连接不同外部主机(或端口)时,NAT网关也会建立新的映射关系。

  事实上,这些术语的引入是很多混淆的起源。现实中的很多NAT设备是将这些转换方式混合在一起工作的,而不单单使用一种,所以这些术语只适合描述一种工作方式,而不是一个设备。比如,很多NAT设备对内部发出的连接使用对称型NAT方式,而同时支持静态的端口映射,后者可以被看作是全锥型NAT方式。而有些情况下,NAT设备的一个公网地址和端口可以同时映射到内部几个服务器上以实现负载分担,比如一个对外提供WEB服务器的站点可能是有成百上千个服务器在提供HTTP服务,但是对外却表现为一个或少数几个IP地址。

三、NAT技术特点

1、NAT的优点

  • 节省公共IP地址资源:理论上所有网络端口有多少,一个公网IP就能支持多少台机器联网,解决了IP地址不够用的问题;
  • 安全防护:实际机器隐藏自己的真实IP,仅通过端口来区分是内网中哪台机器,保证了自身安全。

2、NAT的缺点

(1)无法进行端到端的IP跟踪

  NAT在实现上将多个内部主机发出的连接复用到一个IP上,这就使依赖IP进行主机跟踪的机制都失效了。如网络管理中需要的基于网络流量分析的应用无法跟踪到终端用户与流量的具体行为的关系。基于用户行为的日志分析也变得困难,因为一个IP被很多用户共享,如果存在恶意的用户行为,很难定位到发起连接的那个主机。即便有一些机制提供了在NAT网关上进行连接跟踪的方法,但是把这种变换关系接续起来也困难重重。基于IP的用户授权不再可靠,因为拥有一个IP的不等于一个用户或主机。一个服务器也不能简单把同一IP的访问视作同一主机发起的,不能进行关联。有些服务器设置有连接限制,同一时刻只接纳来自一个IP的有限访问(有时是仅一个访问),这会造成不同用户之间的服务抢占和排队。有时服务器端这样做是出于DOS攻击防护的考虑,因为一个用户正常情况下不应该建立大量的连接请求,过度使用服务资源被理解为攻击行为。但是这在NAT存在时不能简单按照连接数判断。总之,因为NAT隐蔽了通信的一端,把简单的事情复杂化了。  

(2)使IP会话的保持时效变短

  因为一个会话建立后会在NAT设备上建立一个关联表,在会话静默的这段时间,NAT网关会进行老化操作。这是任何一个NAT网关必须做的事情,因为IP和端口资源有限,通信的需求无限,所以必须在会话结束后回收资源。通常TCP会话通过协商的方式主动关闭连接,NAT网关可以跟踪这些报文,但总是存在例外的情况,要依赖自己的定时器去回收资源。而基于UDP的通信协议很难确定何时通信结束,所以NAT网关主要依赖超时机制回收外部端口。通过定时器老化回收会带来一个问题,如果应用需要维持连接的时间大于NAT网关的设置,通信就会意外中断。因为网关回收相关转换表资源以后,新的数据到达时就找不到相关的转换信息,必须建立新的连接。当这个新数据是由公网侧向私网侧发送时,就会发生无法触发新连接建立,也不能通知到私网侧的主机去重建连接的情况。这时候通信就会中断,不能自动恢复。即使新数据是从私网侧发向公网侧,因为重建的会话表往往使用不同于之前的公网IP和端口地址,公网侧主机也无法对应到之前的通信上,导致用户可感知的连接中断。NAT网关要把回收空闲连接的时间设置到不发生持续的资源流失,又维持大部分连接不被意外中断,是一件比较有难度的事情。在NAT已经普及化的时代,很多应用协议的设计者已经考虑到了这种情况,所以一般会设置一个连接保活的机制,即在一段时间没有数据需要发送时,主动发送一个NAT能感知到而又没有实际数据的保活消息,这么做的主要目的就是重置NAT的会话定时器

(3)许多应用层协议无法识别

  NAT工作机制依赖于修改IP包头的信息,这会妨碍一些安全协议的工作。因为NAT篡改了IP地址、传输层端口号和校验和,这会导致认证协议彻底不能工作,因为认证目的就是要保证这些信息在传输过程中没有变化。对于一些隧道协议,NAT的存在也导致了额外的问题,因为隧道协议通常用外层地址标识隧道实体,穿过NAT的隧道会有IP复用关系,在另一端需要小心处理。ICMP是一种网络控制协议,它的工作原理也是在两个主机之间传递差错和控制消息,因为IP的对应关系被重新映射,ICMP也要进行复用和解复用处理,很多情况下因为ICMP报文载荷无法提供足够的信息,解复用会失败。IP分片机制是在信息源端或网络路径上,需要发送的IP报文尺寸大于路径实际能承载最大尺寸时,IP协议层会将一个报文分成多个片断发送,然后在接收端重组这些片断恢复原始报文。IP这样的分片机制会导致传输层的信息只包括在第一个分片中,NAT难以识别后续分片与关联表的对应关系,因此需要特殊处理。

3、NAT的局限性

(1)NAT违反了IP地址结构模型的设计原则。IP地址结构模型的基础是每个IP地址均标识了一个网络的连接。Internet的软件设计就是建立在这个前提之上,而NAT使得有很多主机可能在使用相同的地址,如10.0.0.1。

(2)NAT使得IP协议从面向无连接变成立面向连接。NAT必须维护专用IP地址与公用IP地址以及端口号的映射关系。在TCP/IP协议体系中,如果一个路由器出现故障,不会影响到TCP协议的执行。因为只要几秒收不到应答,发送进程就会进入超时重传处理。而当存在NAT时,最初设计的TCP/IP协议过程将发生变化,Internet可能变得非常脆弱。

(3)NAT违反了基本的网络分层结构模型的设计原则。因为在传统的网络分层结构模型中,第N层是不能修改第N+1层的报头内容的。NAT破坏了这种各层独立的原则。

(4)有些应用是将IP地址插入到正文的内容中,例如标准的FTP协议与IP Phone协议H.323。如果NAT与这一类协议一起工作,那么NAT协议一定要做适当地修正。同时,网络的传输层也可能使用TCP与UDP协议之外的其他协议,那么NAT协议必须知道并且做相应的修改。由于NAT的存在,使得P2P应用实现出现困难,因为P2P的文件共享与语音共享都是建立在IP协议的基础上的。

(5)NAT同时存在对高层协议和安全性的影响问题。RFC对NAT存在的问题进行了讨论。NAT的反对者认为这种临时性的缓解IP地址短缺的方案推迟了Ipv6迁移的进程,而并没有解决深层次的问题,他们认为是不可取的。

四、NAT弊端解决方案——NAT穿越技术

  针对NAT的弊端,为了解决IP端到端应用在NAT环境下遇到的问题,网络协议的设计者们创造了各种武器来进行应对。但遗憾的是,这里每一种方法都不完美,还需要在内部主机、应用程序或者NAT网关上增加额外的处理。

1、应用层网关(ALG)

  应用层网关(ALG)是解决NAT对应用层协议无感知的一个最常用方法,已经被NAT设备厂商广泛采用,成为NAT设备的一个必需功能。

(1)作用

  因为NAT不感知应用协议,所以有必要额外为每个应用协议定制协议分析功能,这样NAT网关就能理解并支持特定的协议。ALG与NAT形成互动关系,在一个NAT网关检测到新的连接请求时,需要判断是否为已知的应用类型,这通常是基于连接的传输层端口信息来识别的。在识别为已知应用时,再调用相应功能对报文的深层内容进行检查,当发现任何形式表达的IP地址和端口时,将会把这些信息同步转换,并且为这个新连接创建一个附加的转换表项。这样,当报文到达公网侧的目的主机时,应用层协议中携带的信息就是NAT网关提供的地址和端口。一旦公网侧主机开始发送数据或建立连接到此端口,NAT网关就可以根据关联表信息进行转换,再把数据转发到私网侧的主机。

  很多应用层协议实现不限于一个初始连接(通常为信令或控制通道)加一个数据连接,可能是一个初始连接对应很多后续的新连接。比较特别的协议,在一次协商中会产生一组相关连接,比如RTP/RTCP协议规定,一个RTP通道建立后占用连续的两个端口,一个服务于数据,另一个服务于控制消息。此时,就需要ALG分配连续的端口为应用服务。ALG能成功解决大部分协议的NAT穿越需求,但是这个方法也有很大的限制。

(2)限制

  因为应用协议的数量非常多而且在不断发展变化之中,添加到设备中的ALG功能都是为特定协议的特定规范版本而开发的,协议的创新和演进要求NAT设备制造商必须跟踪这些协议的最近标准,同时兼容旧标准。尽管有如Linux这种开放平台允许动态加载新的ALG特性,但是管理成本仍然很高,网络维护人员也不能随时了解用户都需要什么应用。因此为每个应用协议开发ALG代码并跟踪最新标准是不可行的,ALG只能解决用户最常用的需求。此外,出于安全性需要,有些应用类型报文从源端发出就已经加密,这种报文在网络中间无法进行分析,所以ALG无能为力。

2、探针技术STUN和TURN

  探针技术,是通过在所有参与通信的实体上安装探测插件,以检测网络中是否存在NAT网关,并对不同NAT模型实施不同穿越方法的一种技术。

  STUN服务器被部署在公网上,用于接收来自通信实体的探测请求,服务器会记录收到请求的报文地址和端口,并填写到回送的响应报文中。客户端根据接收到的响应消息中记录的地址和端口与本地选择的地址和端口进行比较,就能识别出是否存在NAT网关。如果存在NAT网关,客户端会使用之前的地址和端口向服务器的另外一个IP发起请求,重复前面的探测。然后再比较两次响应返回的结果判断出NAT工作的模式。由前述的一对多转换模型得知,除对称型NAT以外的模型,NAT网关对内部主机地址端口的映射都是相对固定的,所以比较容易实现NAT穿越。

  而对称型NAT为每个连接提供一个映射,使得转换后的公网地址和端口对不可预测。此时TURN可以与STUN绑定提供穿越NAT的服务,即在公网服务器上提供一个“地址端口对”,所有此“地址端口对”接收到的数据会经由探测建立的连接转发到内网主机上。TURN分配的这个映射“地址端口对”会通过STUN响应发给内部主机,后者将此信息放入建立连接的信令中通知通信的对端。这种探针技术是一种通用方法,不用在NAT设备上为每种应用协议开发功能,相对于ALG方式有一定普遍性。但是TURN中继服务会成为通信瓶颈。而且在客户端中增加探针功能要求每个应用都要增加代码才能支持。

3、中间件技术

  这也是一种通过开发通用方法解决NAT穿越问题的努力。

  与前者不同之处是,NAT网关是这一解决方案的参与者。与ALG的不同在于,客户端会参与网关公网映射信息的维护,此时NAT网关只要理解客户端的请求并按照要求去分配转换表,不需要自己去分析客户端的应用层数据。

  其中UPnP就是这样一种方法。UPnP中文全称为通用即插即用,是一个通用的网络终端与网关的通信协议,具备信息发布和管理控制的能力。其中,网关映射请求可以为客户动态添加映射表项。此时,NAT不再需要理解应用层携带的信息,只转换IP地址和端口信息。而客户端通过控制消息或信令发到公网侧的信息中,直接携带公网映射的IP地址和端口,接收端可以按照此信息建立数据连接。NAT网关在收到数据或连接请求时,按照UPnP建立的表项只转换地址和端口信息,不关心内容,再将数据转发到内网。这种方案需要网关、内部主机和应用程序都支持UPnP技术,且组网允许内部主机和NAT网关之间可以直接交换UPnP信令才能实施。

4、中继代理技术

  准确说它不是NAT穿越技术,而是NAT旁路技术

  简单说,就是在NAT网关所在的位置旁边放置一个应用服务器,这个服务器在内部网络和外部公网分别有自己的网络连接。客户端特定的应用产生网络请求时,将定向发送到应用代理服务器。应用代理服务器根据代理协议解析客户端的请求,再从服务器的公网侧发起一个新的请求,把客户端请求的内容中继到外部网络上,返回的相应反方向中继。这项技术和ALG有很大的相似性,它要求为每个应用类型部署中继代理业务,中间服务器要理解这些请求。

5、特定协议自穿越技术

  在所有方法中最复杂也最可靠的就是自己解决自己的问题。

  比如IKE和IPsec技术,在设计时就考虑了到如何穿越NAT的问题。因为这个协议是一个自加密的协议并且具有报文防修改的鉴别能力,其他通用方法爱莫能助。因为实际应用的NAT网关基本都是NAPT方式,所有通过传输层协议承载的报文可以顺利通过NAT。IKE和IPsec采用的方案就是用UDP在报文外面再加一层封装,而内部的报文就不再受到影响。IKE中还专门增加了NAT网关是否存在的检查能力以及绕开NAT网关检测IKE协议的方法。

Guess you like

Origin www.cnblogs.com/xiugeng/p/12023620.html
NAT