应用背景
- 为连接在router上的subnet添加防火墙,没有防火墙时连接在同一个router上的subnet是相互联通的。安装防火后如果需要打通subnet之间的网络需要为其设置防火墙规则。
实现原理
- 在router network namespace中利用iptables进行流量控制,实现neutron firewall。
Before set firewal
show table mangle
ip netns exec qrouter-bf522caa-1fda-4626-8a9c-846bc1818a50 iptables -nvL -t mangle
Chain PREROUTING (policy ACCEPT 68 packets, 6468 bytes)
pkts bytes target prot opt in out source destination
70 6672 neutron-l3-agent-PREROUTING all -- * * 0.0.0.0/0 0.0.0.0/0
Chain INPUT (policy ACCEPT 68 packets, 6468 bytes)
pkts bytes target prot opt in out source destination
70 6672 neutron-l3-agent-INPUT all -- * * 0.0.0.0/0 0.0.0.0/0
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 neutron-l3-agent-FORWARD all -- * * 0.0.0.0/0 0.0.0.0/0
Chain OUTPUT (policy ACCEPT 116 packets, 9912 bytes)
pkts bytes target prot opt in out source destination
120 10264 neutron-l3-agent-OUTPUT all -- * * 0.0.0.0/0 0.0.0.0/0
Chain POSTROUTING (policy ACCEPT 116 packets, 9912 bytes)
pkts bytes target prot opt in out source destination
120 10264 neutron-l3-agent-POSTROUTING all -- * * 0.0.0.0/0 0.0.0.0/0
Chain neutron-l3-agent-FORWARD (1 references)
pkts bytes target prot opt in out source destination
Chain neutron-l3-agent-INPUT (1 references)
pkts bytes target prot opt in out source destination
Chain neutron-l3-agent-OUTPUT (1 references)
pkts bytes target prot opt in out source destination
Chain neutron-l3-agent-POSTROUTING (1 references)
pkts bytes target prot opt in out source destination
50 3592 CONNMARK all -- * qg-aed694eb-3d 0.0.0.0/0 0.0.0.0/0 connmark match 0x0/0xffff0000 CONNMARK save mask 0xffff0000
Chain neutron-l3-agent-PREROUTING (1 references)
pkts bytes target prot opt in out source destination
70 6672 neutron-l3-agent-mark all -- * * 0.0.0.0/0 0.0.0.0/0
70 6672 neutron-l3-agent-scope all -- * * 0.0.0.0/0 0.0.0.0/0
0 0 CONNMARK all -- * * 0.0.0.0/0 0.0.0.0/0 connmark match ! 0x0/0xffff0000 CONNMARK restore mask 0xffff0000
70 6672 neutron-l3-agent-floatingip all -- * * 0.0.0.0/0 0.0.0.0/0
0 0 MARK tcp -- qr-+ * 0.0.0.0/0 169.254.169.254 tcp dpt:80 MARK xset 0x1/0xffff
Chain neutron-l3-agent-float-snat (0 references)
pkts bytes target prot opt in out source destination
0 0 CONNMARK all -- * * 0.0.0.0/0 0.0.0.0/0 connmark match 0x0/0xffff0000 CONNMARK save mask 0xffff0000
Chain neutron-l3-agent-floatingip (1 references)
pkts bytes target prot opt in out source destination
Chain neutron-l3-agent-mark (1 references)
pkts bytes target prot opt in out source destination
0 0 MARK all -- qg-aed694eb-3d * 0.0.0.0/0 0.0.0.0/0 MARK xset 0x2/0xffff
Chain neutron-l3-agent-scope (1 references)
pkts bytes target prot opt in out source destination
0 0 MARK all -- qr-3ca3a71b-2c * 0.0.0.0/0 0.0.0.0/0 MARK xset 0x4000000/0xffff0000
0 0 MARK all -- qg-aed694eb-3d * 0.0.0.0/0 0.0.0.0/0 MARK xset 0x4000000/0xffff0000
0 0 MARK all -- qr-c5abe08a-ad * 0.0.0.0/0 0.0.0.0/0 MARK xset 0x4000000/0xffff0000
show chain FORWARD
ip netns exec qrouter-bf522caa-1fda-4626-8a9c-846bc1818a50 iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
4 408 neutron-l3-agent-INPUT all -- * * 0.0.0.0/0 0.0.0.0/0
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 neutron-filter-top all -- * * 0.0.0.0/0 0.0.0.0/0
0 0 neutron-l3-agent-FORWARD all -- * * 0.0.0.0/0 0.0.0.0/0
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
8 704 neutron-filter-top all -- * * 0.0.0.0/0 0.0.0.0/0
8 704 neutron-l3-agent-OUTPUT all -- * * 0.0.0.0/0 0.0.0.0/0
Chain neutron-filter-top (2 references)
pkts bytes target prot opt in out source destination
8 704 neutron-l3-agent-local all -- * * 0.0.0.0/0 0.0.0.0/0
Chain neutron-l3-agent-FORWARD (1 references)
pkts bytes target prot opt in out source destination
0 0 neutron-l3-agent-scope all -- * * 0.0.0.0/0 0.0.0.0/0
Chain neutron-l3-agent-INPUT (1 references)
pkts bytes target prot opt in out source destination
0 0 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 mark match 0x1/0xffff
0 0 DROP tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:9697
Chain neutron-l3-agent-OUTPUT (1 references)
pkts bytes target prot opt in out source destination
Chain neutron-l3-agent-local (1 references)
pkts bytes target prot opt in out source destination
Chain neutron-l3-agent-scope (1 references)
pkts bytes target prot opt in out source destination
0 0 DROP all -- * qr-3ca3a71b-2c 0.0.0.0/0 0.0.0.0/0 mark match ! 0x4000000/0xffff0000
0 0 DROP all -- * qr-c5abe08a-ad 0.0.0.0/0 0.0.0.0/0 mark match ! 0x4000000/0xffff0000
SET firewall with rule
Show table mangle
ip netns exec qrouter-bf522caa-1fda-4626-8a9c-846bc1818a50 iptables -nvL -t mangle
Chain PREROUTING (policy ACCEPT 68 packets, 6468 bytes)
pkts bytes target prot opt in out source destination
70 6672 neutron-l3-agent-PREROUTING all -- * * 0.0.0.0/0 0.0.0.0/0
Chain INPUT (policy ACCEPT 68 packets, 6468 bytes)
pkts bytes target prot opt in out source destination
70 6672 neutron-l3-agent-INPUT all -- * * 0.0.0.0/0 0.0.0.0/0
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 neutron-l3-agent-FORWARD all -- * * 0.0.0.0/0 0.0.0.0/0
Chain OUTPUT (policy ACCEPT 116 packets, 9912 bytes)
pkts bytes target prot opt in out source destination
120 10264 neutron-l3-agent-OUTPUT all -- * * 0.0.0.0/0 0.0.0.0/0
Chain POSTROUTING (policy ACCEPT 116 packets, 9912 bytes)
pkts bytes target prot opt in out source destination
120 10264 neutron-l3-agent-POSTROUTING all -- * * 0.0.0.0/0 0.0.0.0/0
Chain neutron-l3-agent-FORWARD (1 references)
pkts bytes target prot opt in out source destination
Chain neutron-l3-agent-INPUT (1 references)
pkts bytes target prot opt in out source destination
Chain neutron-l3-agent-OUTPUT (1 references)
pkts bytes target prot opt in out source destination
Chain neutron-l3-agent-POSTROUTING (1 references)
pkts bytes target prot opt in out source destination
50 3592 CONNMARK all -- * qg-aed694eb-3d 0.0.0.0/0 0.0.0.0/0 connmark match 0x0/0xffff0000 CONNMARK save mask 0xffff0000
Chain neutron-l3-agent-PREROUTING (1 references)
pkts bytes target prot opt in out source destination
70 6672 neutron-l3-agent-mark all -- * * 0.0.0.0/0 0.0.0.0/0
70 6672 neutron-l3-agent-scope all -- * * 0.0.0.0/0 0.0.0.0/0
0 0 CONNMARK all -- * * 0.0.0.0/0 0.0.0.0/0 connmark match ! 0x0/0xffff0000 CONNMARK restore mask 0xffff0000
70 6672 neutron-l3-agent-floatingip all -- * * 0.0.0.0/0 0.0.0.0/0
0 0 MARK tcp -- qr-+ * 0.0.0.0/0 169.254.169.254 tcp dpt:80 MARK xset 0x1/0xffff
Chain neutron-l3-agent-float-snat (0 references)
pkts bytes target prot opt in out source destination
0 0 CONNMARK all -- * * 0.0.0.0/0 0.0.0.0/0 connmark match 0x0/0xffff0000 CONNMARK save mask 0xffff0000
Chain neutron-l3-agent-floatingip (1 references)
pkts bytes target prot opt in out source destination
Chain neutron-l3-agent-mark (1 references)
pkts bytes target prot opt in out source destination
0 0 MARK all -- qg-aed694eb-3d * 0.0.0.0/0 0.0.0.0/0 MARK xset 0x2/0xffff
Chain neutron-l3-agent-scope (1 references)
pkts bytes target prot opt in out source destination
0 0 MARK all -- qr-3ca3a71b-2c * 0.0.0.0/0 0.0.0.0/0 MARK xset 0x4000000/0xffff0000
0 0 MARK all -- qg-aed694eb-3d * 0.0.0.0/0 0.0.0.0/0 MARK xset 0x4000000/0xffff0000
0 0 MARK all -- qr-c5abe08a-ad * 0.0.0.0/0 0.0.0.0/0 MARK xset 0x4000000/0xffff0000
show chain Forward
ip netns exec qrouter-bf522caa-1fda-4626-8a9c-846bc1818a50 iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
4 408 neutron-l3-agent-INPUT all -- * * 0.0.0.0/0 0.0.0.0/0
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 neutron-filter-top all -- * * 0.0.0.0/0 0.0.0.0/0
0 0 neutron-l3-agent-FORWARD all -- * * 0.0.0.0/0 0.0.0.0/0
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
8 704 neutron-filter-top all -- * * 0.0.0.0/0 0.0.0.0/0
8 704 neutron-l3-agent-OUTPUT all -- * * 0.0.0.0/0 0.0.0.0/0
Chain neutron-filter-top (2 references)
pkts bytes target prot opt in out source destination
8 704 neutron-l3-agent-local all -- * * 0.0.0.0/0 0.0.0.0/0
Chain neutron-l3-agent-FORWARD (1 references)
pkts bytes target prot opt in out source destination
0 0 neutron-l3-agent-scope all -- * * 0.0.0.0/0 0.0.0.0/0
0 0 neutron-l3-agent-iv43b649c11 all -- * qr-+ 0.0.0.0/0 0.0.0.0/0
0 0 neutron-l3-agent-ov43b649c11 all -- qr-+ * 0.0.0.0/0 0.0.0.0/0
0 0 neutron-l3-agent-fwaas-defau all -- * qr-+ 0.0.0.0/0 0.0.0.0/0
0 0 neutron-l3-agent-fwaas-defau all -- qr-+ * 0.0.0.0/0 0.0.0.0/0
Chain neutron-l3-agent-INPUT (1 references)
pkts bytes target prot opt in out source destination
0 0 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 mark match 0x1/0xffff
0 0 DROP tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:9697
Chain neutron-l3-agent-OUTPUT (1 references)
pkts bytes target prot opt in out source destination
Chain neutron-l3-agent-fwaas-defau (2 references)
pkts bytes target prot opt in out source destination
0 0 DROP all -- * * 0.0.0.0/0 0.0.0.0/0
Chain neutron-l3-agent-iv43b649c11 (1 references)
pkts bytes target prot opt in out source destination
0 0 DROP all -- * * 0.0.0.0/0 0.0.0.0/0 state INVALID
0 0 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
0 0 ACCEPT all -- * * 192.168.1.0/24 192.168.4.0/24
Chain neutron-l3-agent-local (1 references)
pkts bytes target prot opt in out source destination
Chain neutron-l3-agent-ov43b649c11 (1 references)
pkts bytes target prot opt in out source destination
0 0 DROP all -- * * 0.0.0.0/0 0.0.0.0/0 state INVALID
0 0 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
0 0 ACCEPT all -- * * 192.168.1.0/24 192.168.4.0/24
Chain neutron-l3-agent-scope (1 references)
pkts bytes target prot opt in out source destination
0 0 DROP all -- * qr-3ca3a71b-2c 0.0.0.0/0 0.0.0.0/0 mark match ! 0x4000000/0xffff0000
0 0 DROP all -- * qr-c5abe08a-ad 0.0.0.0/0 0.0.0.0/0 mark match ! 0x4000000/0xffff0000