天下网吧 >> 网吧天地 >> 网吧技术 >> 网吧网络 >> 正文

对NAT中术语和转换关系的理解和研究

2008-3-12ChinaITLab收集整理佚名

  在NAT中有4个术语:内部本地,内部全局,外部本地,外部全局.这四个术语如果不细致理解,确实让人感到非常的乱,但理解后其实并不难.

内部本地 内部全局 外部本地 外部全局


(相同颜色处于同一层次平面)
上面四个术语描述的IP地址,可以这样理解:
内部本地和外部全局,是通信中正式的真正源/目的地址
内部全局和外部本地是在NAT过程中的一个中间量.

  内部全局是内部本地在全局平面(外部网络)的表现,也就是说 内部全局在外部网络(全局平面)中代表了内部本地
  外部本地是外部全局在本地平面(内部网络)的表现,也就是说 外部本地在内部网络(本地平面)中代表了外部全局
如图:


  看下面的图进一步理解这样的关系,这个图是我自己理解关系时候想象出来的,通过这个图可以比较直观的理清关系,在这个图中我引入了2个名词本地平面/全局平面.

 

只有处在同一平面的才能进行直接的数据传输

  那么内部本地(SA)要想和外部全局(DA)通信,该如何进行?
首先:要想能正常传输,必须要让数据处于同一个平面,现在2者不在同一平面,由于数据方向是内部本地-->外部全局,要统一到全局平面,所以需要把内部本地转换成内部全局,用内部全局代表了内部本地,内部全局与外部全局就处于同一平面,就可以正常通信.

  同理,外部全局(SA)要和内部本地(DA)通信,由于数据方向是外部全局--->内部本地,要统一到本地平面,外部全局就需要被转换到外部本地,用然后外部本地与内部本地通信.


事实上,我们可以这样理解路由器的行为:

  从内部本地发向外部全局的数据,数据包的源地址是内部本地,目的地址是外部本地,在经过路由器的inside接口后,源地址被替换为内部全局,而目的地址被替换为外部全局,也就是说实现了从本地平面向全局平面的迁移,在这里,如果转换前后的目标地址相同(外部本地和外部全局),就可以认为是普通的由内到外的NAT,如果转换前后的目标地址不同(外部本地和外部全局),就可以将这种方式用来处理路由器两边网络存在地址重叠的情况.

  从外部全局发向内部本地的数据,数据保的源地址是外部全局,目的地址是内部全局,在经过路由器的outside接口后,源地址被替换为外部本地,而目的地址被替换为内部本地,也就是说实现了从全局平面向本地平面的迁移,在这里,如果转换前后的目标地址相同(内部全局和内部本地)相同,就可以认为是普通的由外向内的NAT,如果转换前后的目标地址不同,就可以将这种方式用来处理路由器两边网络存在地址重叠的情况.

============

NAT 具体命令理解

1,由内向外的转换,在路由器的inside口处发生了NAT转换行为

r1-2514(config)#ip nat inside ?
  destination  Destination address translation
  source     Source address translation

从上面可以看出,在inside边可以对数据包中的源地址或者目标地址进行转换.

r1-2514(config)#ip nat inside source ?
  list     Specify access list describing local addresses
  route-map  Specify route-map
  static   Specify static local->global mapping

从上面可以看出,针对源地址进行转换可以使用acl 或者 route-map来表述一个本地地址,意思是数据包中源地址符合这些的都要被转换. 也可以使用static进行静态映射,指定一个静态的从本地到全局的映射.

r1-2514(config)#ip nat inside source list 1 ?
  interface  Specify interface for global address
  pool     Name pool of global addresses

从上面输出可以看出接下来要给一个全局地址,数据包中的源地址将被这个全局地址替代.

对于静态映射,还可以指定协议 端口号:
When translating addresses to an interface's address, outside-initiated connections to services on the inside network (like mail) will require additional configuration to send the connection to the correct inside host. This command allows the user to map certain services to certain inside hosts.

ip nat inside source static { tcp | udp } <localaddr> <localport> <globaladdr> <globalport>
Example:
ip nat inside source static tcp 192.168.10.1 25 171.69.232.209 25
In this example, outside-initiated connections to the SMTP port (25) will be sent to the inside host 192.168.10.1.
在inside边对目标进行转换:
r1-2514(config)#ip nat inside destination ?
  list  Specify access list describing global addresses
从上面输出可以看出,路由器要求输入一个表示全局地址的ACL
r1-2514(config)#ip nat inside destination list 1 ?
  pool  Name pool of local addresses
  pool  Name pool of local addresses
接着要求输入一个本地地址池
所以这是一个针对从outside向inside方向数据的NAT,凡是在这个方向数据包中目标地址符合ACL描述的全部被转换成POOL中的本地地址.这可以被用来进行TCP的负载均衡,即外部都请求同一个全局地址,而在路由器的inside边,这些请求的目标地址全部被转换成地址池中的地址,而且是循环使用地址池中的地址,从而达到负载均衡,但是这种方法只适合TCP流,同时不适宜用在WEB服务的负载均担上.详细解释看这里:

 

Destination Address Rotary Translation

A dynamic form of destination translation can be configured for some outside-to-inside traffic. Once a mapping is set up, a destination address matching one of those on an access list will be replaced with an address from a rotary pool. Allocation is done in a round-robin basis, performed only when a new connection is opened from the outside to the inside. All non-TCP traffic is passed untranslated (unless other translations are in effect).
This feature was designed to provide protocol translation load distribution. It is not designed nor intended to be used as a substitute technology for Cisco's LocalDirector product. Destination address rotary translation should not be used to provide web service load balancing because, like vanilla DNS, it knows nothing about service availability. As a result, if a web server were to become offline, the destination address rotary translation feature would continue to send requests to the downed server.
http://www.cisco.com/warp/public/732/Tech/ipservices/natalgs.pdf
2.由外向内,在OUTSIDE边发生的行为:
r1-2514(config)#ip nat outside ?        
  source  Source address translation
从上面可以看出在OUTSIDE边,只能对数据包中的源地址转换
r1-2514(config)#ip nat outside source ?
  list     Specify access list describing global addresses
  route-map  Specify route-map
  static   Specify static global->local mapping
从上面可以看出接下来路由器要求给定一个全局地址的描述,可以是ACL route-map 或者 静态的.
r1-2514(config)#ip nat outside source list 1 ?
  pool  Name pool of local addresses
从上面可以看出,路由器接着又要求给定一个本地地址,这说明 这个命令是对从外到内的数据包,进行源地址字段的替换,它将外部全局地址转换成内部地址(内部本地或者内部全局,内部本地和内部全局可以相同也可以不同)
ip nat outside source { list <acl> pool <name> | static <global-ip> <local-ip> }
The first form (list..pool..) enables dynamic translation. Packets from addresses that match those on the simple access list are translated using local addresses allocated from the named pool.
The second form (static) of the command sets up a single static translation.


 

一个例子:
CONFIGURATION EXAMPLES
The following sample configuration translates between inside hosts addressed from either the 192.168.1.0 or 192.168.2.0 nets to the globally-unique 171.69.233.208/28 network.
ip nat pool net-20 171.69.233.208 171.69.233.223 netmask <netmask> 255.255.255.240
ip nat inside source list 1 pool net-20
!
interface Ethernet0
ip address 171.69.232.182 255.255.255.240
ip nat outside
!
interface Ethernet1
ip address 192.168.1.94 255.255.255.0
ip nat inside
!
access-list 1 permit 192.168.1.0 0.0.0.255
access-list 1 permit 192.168.2.0 0.0.0.255
The next sample configuration translates between inside hosts addressed from the 9.114.11.0 net to the globally unique 171.69.233.208/28 network. Packets from outside hosts addressed from 9.114.11.0 net (the "true" 9.114.11.0 net) are translated to appear to be from net 10.0.1.0/24.
ip nat pool net-20 171.69.233.208 171.69.233.223 netmask <netmask> 255.255.255.240
定义一个名称为 net-20的内部全局地址池
ip nat pool net-10 10.0.1.0 10.0.1.255 netmask <netmask> 255.255.255.0
定义一个名称为net-10的外部本地地址池
ip nat inside source list 1 pool net-20
ip nat outside source list 1 pool net-10
注意inside /outside全部调用了list 1 这说明 内外两边的源地址是重叠地址,通过将内部的源地址转换成net-20中地址和外部的9.114.11.0网络通信。将外部的源地址转换成net-10中的地址来与内部这边的9.114.11.0网络通信
!
interface Ethernet0
ip address 171.69.232.182 255.255.255.240
ip nat outside
!
interface Ethernet1
ip address 9.114.11.39 255.255.255.0
ip nat inside
!
access-list 1 permit 9.114.11.0 0.0.0.255

NAT的一些扩展特性:
1。更灵活的地址池分配方法
More flexible pool configuration:
The pool configuration syntax has been extended to allow discontiguous ranges of addresses. The following syntax is now allowed:
ip nat pool <name> { netmask <mask> | prefix-length <length> } [ type { rotary } ]
This command will put the user into IP NAT Pool configuration mode, where a sequence of address ranges can be configured. There is only one command in this mode:
address <start> <end>
Example:
Router(config)#ip nat pool fred prefix-length 24
Router(config-ipnat-pool)#address 171.69.233.225 171.69.233.226
Router(config-ipnat-pool)#address 171.69.233.228 171.69.233.238
This configuration creates a pool containing addresses 171.69.233.225-226 and 171.69.233.228-238 (171.69.233.227 has been omitted).
2。使用接口作地址,满足那些没有固定IP情况的需要
Translating to interface's address:
As a convenience for users wishing to translate all inside addresses to the address assigned to an interface on the router, the NAT code allows one to simply name the interface when configuring the dynamic translation rule:
ip nat inside source list <number> interface <interface> overload
If there is no address on the interface, or it the interface is not up, no translation will occur.
Example:
ip nat inside source list 1 interface Serial0 overload
3。利用端口的静态转换
Static translations with ports:
When translating addresses to an interface's address, outside-initiated connections to services on the inside network (like mail) will require additional configuration to send the connection to the correct inside host. This command allows the user to map certain services to certain inside hosts.
ip nat inside source static { tcp | udp } <localaddr> <localport> <globaladdr> <globalport>
Example:
ip nat inside source static tcp 192.168.10.1 25 171.69.232.209 25
In this example, outside-initiated connections to the SMTP port (25) will be sent to the inside host 192.168.10.1.
4。利用route-map实现多ISP策略
Support for route maps:
The dynamic translation command can now specify a route-map to be processed instead of an access-list. A route-map allows the user to match any combination of access-list, next-hop IP address, and output interface to determine which pool to use:
ip nat inside source route-map <name> pool <name>
Example:
ip nat pool provider1-space 171.69.232.1 171.69.232.254 prefix-length 24
ip nat pool provider2-space 131.108.43.1 131.108.43.254 prefix-length 24
ip nat inside source route-map provider1-map pool provider1-space
ip nat inside source route-map provider2-map pool provider2-space
!
interface Serial0/0
 ip nat outside
!
interface Serial0/1
 ip nat outside
!
interface Fddi1/0
 ip nat inside
!
route-map provider1-map permit 10
 match ip address 1
 match interface Serial0/0
!
route-map provider2-map permit 10
 match ip address 1
 match interface Serial0/1
关于在NAT中route-map 与ACL逻辑先后关系的研究请看这里:
http://www.mycisco.cn/post/70.html
5。利用add-route参数自动增加一个路由,详细例子请看这里
PAT相关:
Basic Concepts of PAT


Figure 5

Unique Source Port per Translation Entry

Several internal addresses can be NATed to only one or a few external addresses by using a feature called Port Address Translation (PAT) which is also referred to as "overload", a subset of NAT functionality.
PAT uses unique source port numbers on the Inside Global IP address to distinguish between translations. Because the port number is encoded in 16 bits, the total number could theoretically be as high as 65,536 per IP address. PAT will attempt to preserve the original source port, if this source port is already allocated PAT will attempt to find the first available port number starting from the beginning of the appropriate port group 0-5111, 512-1023 or 1024-65535. If there is still no port available from the appropriate group and more than one IP address is configured, PAT will move to the next IP address and try to allocate the original source port again. This continues until it runs out of available ports and IP addresses.
本文参考了CISCO文档
http://www.cisco.com/en/US/tech/tk648/tk361/technologies_white_paper09186a0080091cb9.shtml
http://www.cisco.com/en/US/tech/tk648/tk361/technologies_tech_note09186a0080094837.shtml

欢迎访问最专业的网吧论坛,无盘论坛,网吧经营,网咖管理,网吧专业论坛https://bbs.txwb.com

关注天下网吧微信,了解网吧网咖经营管理,安装维护:


本文来源:ChinaITLab收集整理 作者:佚名

声明
本文来源地址:0
声明:本站所发表的文章、评论及图片仅代表作者本人观点,与本站立场无关。若文章侵犯了您的相关权益,请及时与我们联系,我们会及时处理,感谢您对本站的支持!联系Email:support@txwb.com.,本站所有有注明来源为天下网吧或天下网吧论坛的原创作品,各位转载时请注明来源链接!
天下网吧·网吧天下
  • 本周热门
  • 本月热门
  • 阅读排行