Fix behavior when writing MAC address is disabled
Current code only doesn't write MAC address to systemd-network config
file, the behavior of writing MAC address on DBus remains the same.
Writing the MAC address on DBus can be successfully performed and the
value is changed. Though writing MAC address will reload DBus values
from systemd-network config, applications will read wrong MAC address
from DBus during the time before it reloads. And when compiled with
uboot-env=true, it still updates the MAC address in uboot env.
This patch resolves such issue by simply gives a NotAllowed error when
writing MAC address is disabled.
Tested:
Build with persist-mac=false
* Writing MAC address on DBus gives NotAllowed error.
* Updating MAC address from Redfish and IPMI returns error.
Change-Id: I32a81fe5ddac0c02209a785c6d595fdaf2456e3e
Signed-off-by: Jiaqing Zhao <jiaqing.zhao@intel.com>
diff --git a/src/ethernet_interface.cpp b/src/ethernet_interface.cpp
index 7a8af9d..312bd9d 100644
--- a/src/ethernet_interface.cpp
+++ b/src/ethernet_interface.cpp
@@ -1041,8 +1041,8 @@
// Write the link section
stream << "[Link]\n";
- auto mac = MacAddressIntf::macAddress();
#ifdef PERSIST_MAC
+ auto mac = MacAddressIntf::macAddress();
if (!mac.empty())
{
stream << "MACAddress=" << mac << "\n";
@@ -1170,8 +1170,9 @@
}
}
-std::string EthernetInterface::macAddress(std::string value)
+std::string EthernetInterface::macAddress([[maybe_unused]] std::string value)
{
+#ifdef PERSIST_MAC
ether_addr newMAC;
try
{
@@ -1227,6 +1228,10 @@
#endif // HAVE_UBOOT_ENV
return value;
+#else
+ elog<NotAllowed>(
+ NotAllowedArgument::REASON("Writing MAC address is not allowed"));
+#endif // PERSIST_MAC
}
void EthernetInterface::deleteAll()