我必须连接两个局域网:LAN1:10.10.0.0/16和LAN2:192.168.0.0/16。 我不能做简单的路由,因为在LAN1中禁止192.168.0.0/16networking,所以我正在考虑使用Full cone nat(1:1)将192.168.xy / 16转换为10.11.xy / 16。 每个翻译都是由这个规则完成的:
iptables -t nat -A PREROUTING -d 10.11.0.0/16 -j DNAT --to-destination 192.168.0.0/16 iptables -t nat -A POSTROUTING -s 192.168.0.0/16 -j SNAT --to-source 10.11.0.0/16
但是我必须input254 * 254 * 2的规则,我认为会导致巨大的性能下降。 那么,有没有办法用最less的规则编写这种一对一的翻译呢?
我不确定它是否存在于所有内核中,但是您可能正在寻找的是NETMAP目标。
从iptables手册页
NETMAP This target allows you to statically map a whole network of addresses onto another network of addresses. It can only be used from rules in the nat table. --to address[/mask] Network address to map to. The resulting address will be constructed in the following way: All 'one' bits in the mask are filled in from the new 'address'. All bits that are zero in the mask are filled in from the original address.
就像第一个回答说的那样,使用-j NETMAP:
# iptables -t nat -A PREROUTING -d 10.11.0.0/16 -j NETMAP --to 192.168.0.0/16 # iptables -t nat -A POSTROUTING -s 192.168.0.0/16 -j NETMAP --to 10.11.0.0/16
在POSTROUTING行中添加-d 10.10.0.0/16也是一个好主意。
你可以用一个小的shell脚本来做到这一点
#!/bin/bash for (( i=0 ; $i<256 ; i=$i+1 )) ; do for (( j=0 ; $j<256 ; j=$j+1 )) ; do iptables -t nat -A PREROUTING -d 10.11.$i.$j/16 -j DNAT --to-destination 192.168.$i.$j/16 iptables -t nat -A POSTROUTING -s 192.168.$i.$j/16 -j SNAT --to-source 10.11.$i.$j/16 done done
但我认为这是一个错误。 我认为应该是/ 32而不是/ 16。