问题中的networking基本上是这样的:
/----Inet1 / H1---[111.0/24]---GW1---[99.0/24] \----GW2-----Inet2
设备说明
简而言之:H有超过一个可能的路线到互联网。
H应该只在GW2上访问互联网,所以GW1有一些专门针对H1的策略路由:
ip rule add from 192.168.111.47 table 991 ip route add default via 192.168.99.1 table 991
尽pipe只要GW2与互联网有直接的联系,这个问题就会起作用,但当链路断开时就会出现问题。 接下来发生的事情是,GW2将数据包转发回GW1,再次转发回GW2,造成无限循环的TCP乒乓。 最好的结果是数据包刚被丢弃。
GW1上的iptables可以做些什么来防止这种情况? 基本上,一个iptables友好版本的“如果数据包来自GW2,但起源于H1,放弃它”
TLDR; H1只能通过GW2接入互联网,但仍然需要能够与GW1和GW2通话。
编辑: GW1的接口是“99”networkingbr0.105,“111”networking是br0.111。 溶剂可能会或可能不会令人讨厌的简单,但我还没有能够自己产生正确的iptables语法,所以帮助将不胜感激。
PS:这是这个问题的后续问题
如果可能的话,你真的应该修改GW2
的路由。 如果不是,您可以设置一个解决方法。
让:
br0.105 = interface at GW1 in the 99.0/24 network facing GW2 <mac-gw2> = the MAC address of GW2's interface in the 99.0/24 network facing GW1 <ip-h1> = IP address of H1 from the 111.0/24 network
然后添加规则
iptables -A FORWARD -i br0.105 -s <ip-h1> -m mac --mac-source <mac-gw2> -j REJECT
在来自<ip-h1>
的数据包曾经从GW2
返回到GW1
的情况下,将导致发送到<ip-h1>
的ICMP目的地不可达消息。 这里的想法是过滤MAC地址,因为你似乎有几条路由通过这个接口到不同的路由器,你需要区分起始路由器。
您可以通过查看其接口configuration来获取GW2
的MAC地址,或者如果您无法访问GW2
,则通过查看GW1
的ARPcaching表 – 只需运行arp -n
即可查看caching中的当前条目。
请注意,如果GW1
正在为通过<int2>
通信执行NAT,那么<ip-h1>
将需要是<int2>
的IP地址,而不是H1
的IP地址。
当你问一个通配MAC地址的方法 – 没有,iptables的mac模块需要一个完全匹配。 但是,你显然可以编写脚本(快速和肮脏,在投入生产之前添加满足你偏执狂水平的检查):
#!/bin/bash # variables # the IP address of GW2 IP_GW2=192.168.99.1 # the IP address of H1 IP_H1=192.168.111.47 # the configuration file containing GW2's MAC address CONFIG_MAC_GW2="/etc/default/iptables-mac-gw2" # the "arping" utility (used to resolve an IP address to a MAC address) ARPING=/usr/sbin/arping # include $CONFIG_MAC_GW2, if exists test -f $CONFIG_MAC_GW2 && source $CONFIG_MAC_GW2 # test if $MAC_GW2 is empty or undefined if [ ! -n "$MAC_GW2" ]; then MAC_GW2=`$ARPING $IP_GW2 -c 1 -r` if [ $? == 1 ]; then echo \$MAC_GW2 has not been configured and could not be detected using "$ARPING $IP_GW2 -c 1 -r" exit 1 fi # write the discovered MAC address into $CONFIG-MAC-GW2 so we don't have # to resolve it on succeeding runs echo MAC_GW2=${MAC_GW2} > $CONFIG_MAC_GW2 fi # [...] iptables -A FORWARD -i br0.105 -s $IP_H1 -m mac --mac-source $MAC_GW2 -j REJECT
由于G1是一个linux系统,为什么不用一个pingtesting来testingG2的外部接口,并在没有响应的时候改变默认路由呢? 而且,如果你做对了的话,你可以在平台重新开始工作的时候自动回复。