使用iptables桥接DNATedstream量

过去几天我遇到了一个问题。 我的设置如下所述。

KVMpipe理程序[Openstack中的计算节点)。 虚拟机pipe理程序有两个接口被标记:

eth0.123 <- for private connectivity. This has an IP address 172.16.134.2. eth1.456 <- for public connectivity. This is attached to a bridge device br0. No IP associated with eth1.456 or br0. [This is created by openstack's neutron service] 

pipe理程序的路由表:

 # route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 172.16.134.1 0.0.0.0 UG 0 0 0 eth0.123 169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0 169.254.0.0 0.0.0.0 255.255.0.0 U 1004 0 0 eth1 169.254.0.0 0.0.0.0 255.255.0.0 U 1006 0 0 eth0.123 172.16.134.0 0.0.0.0 255.255.252.0 U 0 0 0 eth0.123 

我们有一个VM。 其分接头设备[tap123]与桥br0分开。

 # brctl show bridge name bridge id STP enabled interfaces br0 8000.0cc47ae0e386 no eth1.456 tap123 

正常的出境交通stream量正常,stream量为:

 tap123 -> br0 -> eth1.456 -> outside world 

现在,我试图将所有出站stream量转发到端口25,从虚拟机到1.2.3.4:3030 [1.2.3.4对所有邮件进行垃圾邮件检查]。 为了达到这个目的,在pipe理程序级别,我在iptables规则下面join:

 # iptables -t nat -I PREROUTING -i br0 -p tcp -m physdev --physdev-in tap123 -m tcp --dport 25 -j DNAT --to-destination 1.2.3.4:3030 

过滤表中的FORWARD链有默认规则ACCEPT,所以没有在FORWARD链中添加任何规则。 也没有在nat.POSTROUTING链中添加任何规则。

在添加DNAT规则之后,使用以下path开始从VM到端口25的所有出站连接:

 tap123 -> br0 -> eth0.123 -> outside world. 

我花了一些时间搞清楚为什么总是使用eth0.123 DNATedstream量。 按照http://ebtables.netfilter.org/misc/brnf-faq.html

 What happens with IP DNAT on a to be bridged packet? If IP DNAT happened then the bridge-nf code asks the routing table where the packet should be sent. If it has to be sent over another device (not the bridge device) then the packet is routed (an implicit redirect). If the routing table sends the packet to the bridge device, then the packet is bridged but the MAC destination is correctly changed. To do IP DNAT, you therefore need a correct routing table. 

所以,根据我的机器路由表,select的接口是eth0.123。 由于没有任何IP与eth1.456或br0 assosicated,我也不能添加一个路由规则。

无论如何要确保DNATedstream量是桥接和发送。 总之,即使DNATedstream量应该使用下面的path:

 tap123 -> br0 -> eth1.456 -> outside world 

注:所需的sysctl值已设置

 net.ipv4.conf.all.forwarding = 1 net.ipv4.conf.all.rp_filter = 0 net.bridge.bridge-nf-call-iptables = 1 

提前致谢。