mirror of
https://github.com/OpenNebula/one.git
synced 2026-11-21 04:12:37 +09:00
bug #1520: Firewall not working with Open vSwitch
Switch from the iptables firewall scripts, which aren't compatible anymore, with scripts that create openvswitch flows. Port whitelisting functionality can't be implemented with this model.
This commit is contained in:
1 parent
69eb361d7a
commit
0e1729e84c
4 files changed
+119
-23
No files matched your search
@@ -37,7 +37,8 @@ COMMANDS = {
|
||||
:vconfig => "sudo /sbin/vconfig",
|
||||
:virsh => "virsh -c qemu:///system",
|
||||
:xm => "sudo /usr/sbin/xm",
|
||||
:ovs_vsctl=> "sudo /usr/local/bin/ovs-vsctl",
|
||||
:ovs_vsctl=> "sudo /usr/bin/ovs-vsctl",
|
||||
:ovs_ofctl=> "sudo /usr/bin/ovs-ofctl",
|
||||
:lsmod => "/sbin/lsmod"
|
||||
}
|
||||
|
||||
|
||||
@@ -17,7 +17,13 @@
|
||||
require 'OpenNebulaNetwork'
|
||||
|
||||
class OpenvSwitchVLAN < OpenNebulaNetwork
|
||||
XPATH_FILTER = "TEMPLATE/NIC[VLAN='YES']"
|
||||
FIREWALL_PARAMS = [:white_ports_tcp,
|
||||
:white_ports_udp,
|
||||
:black_ports_tcp,
|
||||
:black_ports_udp,
|
||||
:icmp]
|
||||
|
||||
XPATH_FILTER = "TEMPLATE/NIC"
|
||||
|
||||
def initialize(vm, deploy_id = nil, hypervisor = nil)
|
||||
super(vm,XPATH_FILTER,deploy_id,hypervisor)
|
||||
@@ -25,18 +31,113 @@ class OpenvSwitchVLAN < OpenNebulaNetwork
|
||||
|
||||
def activate
|
||||
process do |nic|
|
||||
if nic[:vlan_id]
|
||||
vlan = nic[:vlan_id]
|
||||
else
|
||||
vlan = CONF[:start_vlan] + nic[:network_id].to_i
|
||||
end
|
||||
@nic = nic
|
||||
|
||||
cmd = "#{COMMANDS[:ovs_vsctl]} set Port #{nic[:tap]} "
|
||||
cmd << "tag=#{vlan}"
|
||||
# Apply VLAN
|
||||
tag_vlan if @nic[:vlan] == "YES"
|
||||
|
||||
OpenNebula.exec_and_log(cmd)
|
||||
# Apply Firewall
|
||||
configure_fw if FIREWALL_PARAMS & @nic.keys != []
|
||||
end
|
||||
|
||||
return 0
|
||||
end
|
||||
|
||||
def deactivate
|
||||
process do |nic|
|
||||
@nic = nic
|
||||
|
||||
# Remove flows
|
||||
del_flows
|
||||
end
|
||||
end
|
||||
|
||||
def tag_vlan
|
||||
if @nic[:vlan_id]
|
||||
vlan = @nic[:vlan_id]
|
||||
else
|
||||
vlan = CONF[:start_vlan] + @nic[:network_id].to_i
|
||||
end
|
||||
|
||||
cmd = "#{COMMANDS[:ovs_vsctl]} set Port #{@nic[:tap]} "
|
||||
cmd << "tag=#{vlan}"
|
||||
|
||||
run cmd
|
||||
end
|
||||
|
||||
def configure_fw
|
||||
# Mac-spoofing
|
||||
add_flow("in_port=#{port},dl_src=#{@nic[:mac]}",:normal,40000)
|
||||
add_flow("in_port=#{port}",:drop,39000)
|
||||
|
||||
# TCP
|
||||
if range = @nic[:black_ports_tcp]
|
||||
if range? range
|
||||
range.split(",").each do |p|
|
||||
add_flow("tcp,dl_dst=#{@nic[:mac]},tp_dst=#{p}",:drop)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# UDP
|
||||
if range = @nic[:black_ports_udp]
|
||||
if range? range
|
||||
range.split(",").each do |p|
|
||||
add_flow("udp,dl_dst=#{@nic[:mac]},tp_dst=#{p}",:drop)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# ICMP
|
||||
if @nic[:icmp]
|
||||
if %w(no drop).include? @nic[:icmp].downcase
|
||||
add_flow("icmp,dl_dst=#{@nic[:mac]}",:drop)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def del_flows
|
||||
in_port = ""
|
||||
|
||||
dump_flows = "#{COMMANDS[:ovs_ofctl]} dump-flows #{@nic[:bridge]}"
|
||||
`#{dump_flows}`.lines do |flow|
|
||||
next unless flow.match("#{@nic[:mac]}")
|
||||
flow = flow.split.select{|e| e.match(@nic[:mac])}.first
|
||||
if in_port.empty? and (m = flow.match(/in_port=(\d+)/))
|
||||
in_port = m[1]
|
||||
end
|
||||
del_flow flow
|
||||
end
|
||||
|
||||
del_flow "in_port=#{in_port}" if !in_port.empty?
|
||||
end
|
||||
|
||||
def add_flow(filter,action,priority=nil)
|
||||
priority = (priority.to_s.empty? ? "" : "priority=#{priority},")
|
||||
|
||||
run "#{COMMANDS[:ovs_ofctl]} add-flow " <<
|
||||
"#{@nic[:bridge]} #{filter},#{priority}actions=#{action}"
|
||||
end
|
||||
|
||||
def del_flow(filter)
|
||||
filter.gsub!(/priority=(\d+)/,"")
|
||||
run "#{COMMANDS[:ovs_ofctl]} del-flows " <<
|
||||
"#{@nic[:bridge]} #{filter}"
|
||||
end
|
||||
|
||||
def run(cmd)
|
||||
OpenNebula.exec_and_log(cmd)
|
||||
end
|
||||
|
||||
def port
|
||||
return @nic[:port] if @nic[:port]
|
||||
|
||||
dump_ports = `#{COMMANDS[:ovs_ofctl]} \
|
||||
dump-ports #{@nic[:bridge]} #{@nic[:tap]}`
|
||||
|
||||
@nic[:port] = dump_ports.scan(/^\s*port\s*(\d+):/).flatten.first
|
||||
end
|
||||
|
||||
def range?(range)
|
||||
!range.match(/^\d+(,\d+)*$/).nil?
|
||||
end
|
||||
end
|
||||
@@ -19,9 +19,9 @@
|
||||
$: << File.dirname(__FILE__)
|
||||
$: << File.join(File.dirname(__FILE__), "..")
|
||||
|
||||
require 'OpenNebulaNetwork'
|
||||
require 'Firewall'
|
||||
require 'OpenvSwitch'
|
||||
|
||||
fw = OpenNebulaFirewall.from_base64(ARGV[0])
|
||||
template64 = ARGV[0]
|
||||
|
||||
fw.deactivate
|
||||
ovs = OpenvSwitchVLAN.from_base64(template64)
|
||||
ovs.deactivate
|
||||
@@ -20,15 +20,9 @@ $: << File.dirname(__FILE__)
|
||||
$: << File.join(File.dirname(__FILE__), "..")
|
||||
|
||||
require 'OpenvSwitch'
|
||||
require 'Firewall'
|
||||
|
||||
template64 = ARGV[0]
|
||||
deploy_id = ARGV[1]
|
||||
|
||||
onevlan = OpenvSwitchVLAN.from_base64(template64, deploy_id)
|
||||
|
||||
onevlan.activate
|
||||
|
||||
fw = OpenNebulaFirewall.from_base64(template64, deploy_id)
|
||||
|
||||
fw.activate
|
||||
ovs = OpenvSwitchVLAN.from_base64(template64, deploy_id)
|
||||
ovs.activate
|
||||
Reference in new issue
Block a user