Luckylau's Blog

Neutron的安全组原理

​ Security group通过Linux IPtables来实现,为此,在Compute节点上引入了qbr*这样的Linux传统bridge(iptables规则目前无法加载到直接挂在到ovs的tap设备上)。

安全组的INPUT、OUTPUT、FORWARD

​ 其中id的前10位数字被用作虚机对外连接的qbr(同时也是tap口)的id。i或o加上前9位数字被用作安全组chain的id。所有的规则默认都在Compute节点上的filter表(默认表)中实现,分别来查看filter表的INPUT、OUTPUT、FORWARD三条链上的规则。

INPUT
iptables –line-numbers -vnL INPUT

可以看到,跟安全组相关的规则被重定向到neutron-openvswi-INPUT。 查看其规则,只有一条。

iptables –line-numbers -vnL neutron-openvswi-INPUT

iptables –line-numbers -vnL neutron-openvswi-o46364368-5

iptables –line-numbers -vnL neutron-openvswi-s46364368-5

这条chain主要检查从vm发出来的网包,是否是openstack所分配的IP和MAC,如果不匹配,则禁止通过。这将防止利用vm上进行一些伪装地址的攻击。

OUTPUT

iptables –line-numbers -vnL OUTPUT

分别跳转到neutron-filter-top和neutron-openvswi-OUTPUT

iptables –line-numbers -vnL neutron-filter-top

该chain目前无规则。

iptables –line-numbers -vnL neutron-openvswi-OUTPUT

该chain目前无规则。

FORWARD

iptables –line-numbers -vnL FORWARD

同样跳转到neutron-filter-top,无规则。跳转到neutron-openvswi-FORWARD。

iptables –line-numbers -vnL neutron-openvswi-FORWARD

iptables –line-numbers -vnL neutron-openvswi-sg-chain

如果是网桥从tap-XXX端口发出到VM的流量,则跳转到neutron-openvswi-i9LETTERID,例如i46364368-5;如果是从tap-XXX端口进入到网桥的(即vm发出来的)流量,则跳转到neutron-openvswi-o9LETTERID,例如o46364368-5。

neutron-openvswi-i9LETTERID允许安全组中配置的策略(允许ssh、ping等)和dhcp reply通过。默认的neutron-openvswi-sg-fallback将drop所有流量。

iptables –line-numbers -vnL neutron-openvswi-i46364368-5

iptables –line-numbers -vnL neutron-openvswi-o46364368-5

neutron-openvswi-o9LETTERID将跳转到 neutron-openvswi-s46364368-5,允许DHCP Request和匹配VM的源IP和源MAC的流量通过,同时允许安全组中配置的策略(允许ssh、ping等)通过。

iptables –line-numbers -vnL neutron-openvswi-s46364368-5

整体逻辑

快速查找安全组规则

从前面分析可以看出,某个vm的安全组相关规则的chain的名字,跟vm的id的前9个字符有关。

因此,要快速查找qbr-XXX上相关的iptables规则,可以用iptables -S列出(默认是filter表)所有链上的规则,其中含有id的链即为虚拟机相关的安全组规则。其中–physdev-in表示即将进入某个网桥的端口,–physdev-out表示即将从某个网桥端口发出。

iptables -S | grep tap4ca9818f-53

可以看出,进出tap-XXX口的FORWARD链上的流量都被扔到了neutron-openvswi-sg-chain这个链,neutron-openvswi-sg-chain上是security group具体的实现(两条规则,访问虚拟机的流量扔给neutron-openvswi-i583c7038-d;从虚拟机出来的扔给neutron-openvswi-o583c7038-d)。

Luckylau wechat
如果对您有价值,看官可以打赏的!