RouterOS 设定NAT loopback (Hairpin NAT)回流

In the below network topology a web server behind a router is on private IP address space, and the router performs NAT to forward traffic to its public IP address to the web server behind it.

The NAT configuration would look like below:

/ip firewall natadd chain=dstnat dst-address=1.1.1.1 protocol=tcp dst-port=80 \ action=dst-nat to-address=192.168.1.2add chain=srcnat out-interface=WAN action=masquerade

When a client out on the Internet with IP address 2.2.2.2 establishes a connection to the web server, the router performs NAT as configured.

  1. the client sends a packet with a source IP address of 2.2.2.2 to a destination IP address of 1.1.1.1 on port tcp/80 to request some web resource.
  2. the router destination NATs the packet to 192.168.1.2 and replaces the destination IP address in the packet accordingly. The source IP address stays the same: 2.2.2.2.
  3. the server replies to the client‘s request and the reply packet has a source IP address of 192.168.1.2 and a destination IP address of 2.2.2.2.
  4. the router determines that the packet is part of a previous connection and undoes the destination NAT, and puts the original destination IP address into the source IP address field. The destination IP address is 2.2.2.2, and the source IP address is 1.1.1.1.

The client receives the reply packet it expects, and the connection is established.

When a client on the same internal network as the web server requests a connection to the web server‘s public IP address, the connection breaks.

  1. the client sends a packet with a source IP address of 192.168.1.10 to a destination IP address of 1.1.1.1 on port tcp/80 to request some web resource.
  2. the router destination NATs the packet to 192.168.1.2 and replaces the destination IP address in the packet accordingly. The source IP address stays the same: 192.168.1.10.
  3. the server replies to the client‘s request. However, the source IP address of the request is on the same subnet as the web server. The web server does not send the reply back to the router, but sends it back directly to 192.168.1.10 with a source IP address in the reply of 192.168.1.2.

The client receives the reply packet, but it discards it because it expects a packet back from 1.1.1.1, and not from 192.168.1.2. As far as the client is concerned the packet is invalid and not related to any connection the client previously attempted to establish.

To fix the issue, an additional NAT rule needs to be introduced on the router to enforce that all reply traffic flows through the router, despite the client and server being on the same subnet. The rule below is very specific to only apply to the traffic that the issue could occur with - if there are many servers the issue occurs with, the rule could be made broader to save having one such exception per forwarded service.

/ip firewall natadd chain=srcnat src-address=192.168.1.0/24 \ dst-address=192.168.1.2 protocol=tcp dst-port=80 \ out-interface=LAN action=masquerade

With that additional rule, the flow now changes:

  1. the client sends a packet with a source IP address of 192.168.1.10 to a destination IP address of 1.1.1.1 on port tcp/80 to request some web resource.
  2. the router destination NATs the packet to 192.168.1.2 and replaces the destination IP address in the packet accordingly. It also source NATs the packet and replaces the source IP address in the packet with the IP address on its LAN interface. The destination IP address is 192.168.1.2, and the source IP address is 192.168.1.1.
  3. the web server replies to the request and sends the reply with a source IP address of 192.168.1.2 back to the router‘s LAN interface IP address of 192.168.1.1.
  4. the router determines that the packet is part of a previous connection and undoes both the source and destination NAT, and puts the original destination IP address of 1.1.1.1 into the source IP address field, and the original source IP address of 192.168.1.10 into the destination IP address field.

The client receives the reply packet it expects, and the connection is established.

However, the web server only ever sees a source IP address of 192.168.1.1 for all requests from internal clients regardless of the internal client‘s real IP address. There is no way to avoid this without either using a router that can do application level DNS inspection and can rewrite A records accordingly, or a split DNS server that serves the internal clients the internal server IP address and external clients the external server IP address.

This is called - among other terms - hair pin NAT because the traffic flow has clients enter the router through the same interface it leaves through, which when drawn looks like a hair pin.

时间: 2024-08-18 09:46:24

RouterOS 设定NAT loopback (Hairpin NAT)回流的相关文章

【有图有真相】静态NAT、动态NAT、PAT、端口映射的详细配置过程

实验要求:使用宿主机利用NAT地址转换技术访问虚拟机的Web80端口.全是***实弹的操作步骤. 实验前提:开启server 2008虚拟机并且搭建一个简单web服务.打开GNS3连接拓扑图的线路,一边连接宿主机,一边连接虚拟机的网卡. 实验步骤:首先是在宿主机和虚拟机上配置IP地址.然后在路由器上配置IP地址,然后先测试宿主机此时是否可以正常访问web服务. 然后在路由器上配置静态NAT.动态NAT和PAT端口多路复用,以及端口映射的关系来改变我们正常访问的接口.下面是配置的详细过程. 首先给

什么叫NAT,设置NAT的两个方法

NAT是网络地址翻译就是把公网IP翻译成私有地址, 又叫端口映射或端口转发. 采用路由方式是指ADSL拥有一个动态或固定的公网IP,ADSL直接接在HUB或交换机上,所有的电脑共享上网.这时ADSL的外部地址只有一个,比如61.177.*.*. 而内部的IP是人为设置的,比如ADSL设为192.168.0.1,下面的电脑就依次设为192.168.0.*(*表示1,2,3,...).     如何实现NAT功能呢?一般路由器中有个虚拟服务器(有些英文设置界面的叫services项)的设置.针对邮件

静态NAT、动态NAT

静态NAT.动态NAT 实验拓扑: 实验目的:熟悉网络地址转换协议 掌握静态NAT 和动态NAT的配置 分析静态NAT 和动态NAT的区别 使用show命令来检查NAT的运行情况 实验要求:按拓扑图来配置静态NAT 和动态NAT 实验步骤: 步骤1 按如上拓扑做好底层配置,并检验相邻设备之间的连通性 步骤2在NAT 上配置一条到internet的缺省路由 NAT(config)#ip route 0.0.0.0 0.0.0.0 200.200.100.2 因为内部主机与internet通信时转换

大型企业网络构建之动态NAT、静态NAT和华为NAT

动态NAT.静态NAT和华为NAT一.NAT概述1.(network address translation )网络地址转换.2.NAT的工作过程:NAT设备收到内网的数据包以后,1.首先查看本地是否有去往数据包目地地址的路由:2.再次查看本地设备是否存在对应的nat转换条目:-- 如果有,则进行地质转换,然后发送出去:-- 如果无,则不进行地质转换,然后发送出去.3.静态NAT:私有-公有 1:1,不节省IP地址:4.配置静态NAT:1.给R1(即内部最末端的路由器)配置一个去往运营商的缺省路

静态NAT与动态NAT综合实验

实验目的:公司内部使用1个公用地址连接外网,并且把内网web服务器映射到公用地址上,提供对外访问! 实验拓扑: 配置: 路由器R1 R1(config)#interface Serial2/0 R1(config-if)#ip address 202.1.1.1 255.255.255.0 R1(config-if)#clock rate 64000 R1(config-if)#no shutdown R1(config-if)#ip nat outside R1(config-if)#inte

NAT原理与NAT穿越

最近在看东西的时候发现很多网络程序中都需要NAT穿越,特意在此总结一下. 先做一个约定: 内网A中有:A1(192.168.0.8).A2(192.168.0.9)两用户 网关X1(一个NAT设备)有公网IP 1.2.3.4 内网B中有:B1(192.168.1.8).B2(192.168.1.9)两用户, 网关Y1(一个NAT设备)有公网IP 1.2.3.5 公网服务器:C (6.7.8.9) D (6.7.8.10) NAT原理 网络地址转换(NAT,Network Address Tran

nat技术动态NAT申请公网IP地址pool

在R1通过动态NAT实现企业内网192.160.1.0/24转换为公网地址12.1.1.3-12.1.1.6,访问192.160.2.1网络拓扑如图:R1:fa 0/0 12.1.1.1/29, fa 1/0 192.160.1.254/24PC1:192.160.1.1/24,PC2:192.160.1.2/24R2:fa 0/0 12.1.1.2/29,fa 1/0 192.160.2.254/24PC3:192.160.2.1/24R1>enableconfigure terminalin

配置NAT回流导致外网解析到了内网IP

单位有3个域名,用量很大,2014年开始本人研究部署了Bind+DLZ +Mysql的三机智能多链路DNS,非常好用,优点是: 1.使用Mysql管理记录,配置.管理.查询方便. 2.自动判断运营商,返回指定IP,实现智能多链路.目前我们单位有电信.移动.教育网专线和固定IP,通过Bind的View,判断请求源IP自动返回对应运营商的服务IP.很好很强大也很实用. 3.根据配置自动为内网分配对应的运营商解析...我单位内网用户较多,有一个不少见的需求,内网部分用户要使用电信专线.部分用户要使用移

(转)NAT与NAT穿透 原理

原文:http://blog.csdn.net/ustcgy/article/details/5655050 5. NAT穿透5.1 转发     最可靠但又是最低效的点对点通信方法,莫过于将p2p网络通信看作一个C/S结构,通过服务器来转发信息.如下图,两个客户端A和B,均与服务器S初始化了一个TCP或UDP连接,服务器S具有公网固定IP地址,两个客户端分布在不同的私网中,这样,他们各自的NAT代理服务器将不允许他们进行直连.