为什么我的iptables条目不阻止ping Xen虚拟机?

时间:2009-03-17 11:20:19

标签: linux iptables xen

我正在编写一个bash脚本来为Xen添加简单的防火墙。

以下是实际的防火墙配置:

Chain INPUT (policy ACCEPT)
target     prot opt source               destination
RH-Firewall-1-INPUT  all  --  anywhere             anywhere

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination
RH-Firewall-1-INPUT  all  --  anywhere             anywhere

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

Chain RH-Firewall-1-INPUT (2 references)
target     prot opt source               destination
ACCEPT     all  --  anywhere             anywhere
ACCEPT     icmp --  anywhere             anywhere            icmp any
ACCEPT     esp  --  anywhere             anywhere
ACCEPT     ah   --  anywhere             anywhere
ACCEPT     udp  --  anywhere             224.0.0.251         udp dpt:mdns
ACCEPT     udp  --  anywhere             anywhere            udp dpt:ipp
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:ipp
ACCEPT     all  --  anywhere             anywhere            state RELATED,ESTABLISHED
ACCEPT     udp  --  anywhere             anywhere            state NEW udp dpt:ha-cluster
ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:ssh
ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:http
ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:https
REJECT     all  --  anywhere             anywhere            reject-with icmp-host-prohibited

我想为每个虚拟机添加一个新链(每个虚拟机都有一个名为vif1.0,vif2.0等的虚拟接口)。输出接口(网桥)是xenbr0。

这就是我的所作所为(例如阻止ping'in'到domU1,vif1.0):

iptables -N domUFirewall
iptables -I FORWARD -j domUFirewall
iptables -I INPUT -j domUFirewall
iptables -A domUFirewall -i vif1.0 -p icmp -j DROP

但是..它不起作用,我仍然可以ping入/退出domU。

一定是真的'哑巴',但我找不到什么是错的。

任何线索?

THX

1 个答案:

答案 0 :(得分:2)

由于您正在使用具有桥接网络的XEN,因此在普通iptables命令可以影响它们之前,数据包将被拦截。因此,您可能需要使用ebtables命令以您希望的方式影响数据包路由。

下面留下的原始答案将适用于其他配置,但不适用于具有桥接网络的XEN。

为了示例,我假装vif1.0的IP地址是192.168.1.100。

我会重做逻辑,不检查输入设备,而是通过IP地址检查。在输入链中,数据包来自(例如)设备eth0,而不是来自vif1.0。因此,这条规则:

iptables -I INPUT -i vif1.0 -j domUFirewall
我之前提出的

永远不会匹配任何数据包。但是,如果您执行以下操作,它应该执行您想要的操作:

iptables -I INPUT -d 192.168.1.100 -j domUFirewall

在这种情况下,链domUFirewall由以下人员设置:

iptables -N domUFirewall
iptables -F domUFirewall
iptables -A domUFirewall -p icmp -j DROP

如果给定的链是针对单个设备的,那么您希望在跳转到链中之前检查上的“-j chainName”操作。然后,在链本身中,您永远不必检查设备或IP地址。

其次,我总是在你的脚本中刷新(清空)链,以防你重新运行脚本。请注意,重新运行脚本时,您可能会收到有关-N行的投诉。没关系。

还有其他方法可以做到这一点,但举一个不同的例子,我需要具体了解你的VM是如何设置的 - 桥接网络? NAT?等等。但我在这里给出的例子应该适用于任何这些模式。

以下是未来的一些有用链接: