Don't send blank network override to host

if bmc is not configured with host network data
then don't send the blank override,send the ipmi
error.

Resolves openbmc/openbmc#2033

Change-Id: Idbbb9a81df568be376f2111cd2bafd4c04e13097
Signed-off-by: Ratan Gupta <ratagupt@in.ibm.com>
diff --git a/chassishandler.cpp b/chassishandler.cpp
index 1a69cdb..f81c2bf 100644
--- a/chassishandler.cpp
+++ b/chassishandler.cpp
@@ -46,12 +46,14 @@
 constexpr size_t MAX_PREFIX_VALUE = 32;
 constexpr size_t SIZE_COOKIE = 4;
 constexpr size_t SIZE_VERSION = 2;
-constexpr auto   MAC_ADDRESS_FORMAT = "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx";
-constexpr auto   IP_ADDRESS_FORMAT = "%d.%d.%d.%d";
+constexpr auto MAC_ADDRESS_FORMAT = "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx";
+constexpr auto IP_ADDRESS_FORMAT = "%u.%u.%u.%u";
 constexpr auto   PREFIX_FORMAT = "%hhd";
 constexpr auto   ADDR_TYPE_FORMAT = "%hhx";
 constexpr auto   IPV4_ADDRESS_SIZE_BYTE = 4;
 constexpr auto   IPV6_ADDRESS_SIZE_BYTE = 16;
+constexpr auto   DEFAULT_MAC_ADDRESS = "00:00:00:00:00:00";
+constexpr auto   DEFAULT_ADDRESS = "0.0.0.0";
 
 //PetiBoot-Specific
 static constexpr uint8_t net_conf_initial_bytes[] = {0x80, 0x21, 0x70, 0x62,
@@ -503,6 +505,42 @@
             getDbusProperty(macObjectInfo.second, macObjectInfo.first,
                             MAC_INTERFACE, "MACAddress");
 
+        auto ipAddress = properties["Address"].get<std::string>();
+
+        auto gateway = properties["Gateway"].get<std::string>();
+
+        auto prefix = properties["PrefixLength"].get<uint8_t>();
+
+        uint8_t isStatic = (properties["Origin"].get<std::string>() ==
+            "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
+                ? 1 : 0;
+
+        // it is expected here that we should get the valid data
+        // but we may also get the default values.
+        // Validation of the data is done by settings.
+        //
+        // if mac address is default mac address then
+        // don't send blank override.
+        if ((MACAddress == DEFAULT_MAC_ADDRESS))
+        {
+            memset(respptr->data, 0, SIZE_BOOT_OPTION);
+            rc = -1;
+            return rc;
+        }
+        // if addr is static then ipaddress,gateway,prefix
+        // should not be default one,don't send blank override.
+        if (isStatic)
+        {
+            if((ipAddress == DEFAULT_ADDRESS) ||
+               (gateway == DEFAULT_ADDRESS) ||
+               (!prefix))
+            {
+                memset(respptr->data, 0, SIZE_BOOT_OPTION);
+                rc = -1;
+                return rc;
+            }
+        }
+
         sscanf(MACAddress.c_str(), MAC_ADDRESS_FORMAT,
                (respptr->data + MAC_OFFSET),
                (respptr->data + MAC_OFFSET + 1),
@@ -511,14 +549,10 @@
                (respptr->data + MAC_OFFSET + 4),
                (respptr->data + MAC_OFFSET + 5));
 
-
         respptr->data[MAC_OFFSET + 6] = 0x00;
 
-        uint8_t addrOrigin = (properties["Origin"].get<std::string>() ==
-            "xyz.openbmc_project.Network.IP.AddressOrigin.Static") ? 1 : 0;
-
-        memcpy(respptr->data + ADDRTYPE_OFFSET, &addrOrigin,
-               sizeof(addrOrigin));
+        memcpy(respptr->data + ADDRTYPE_OFFSET, &isStatic,
+               sizeof(isStatic));
 
         uint8_t addressFamily = (properties["Type"].get<std::string>() ==
             "xyz.openbmc_project.Network.IP.Protocol.IPv4") ?
@@ -528,12 +562,8 @@
                                                 IPV6_ADDRESS_SIZE_BYTE;
 
         // ipaddress and gateway would be in IPv4 format
-
-        inet_pton(addressFamily,
-                  properties["Address"].get<std::string>().c_str(),
-                  (respptr->data + IPADDR_OFFSET));
-
-        uint8_t prefix = properties["PrefixLength"].get<uint8_t>();
+        inet_pton(addressFamily, ipAddress.c_str(),
+                 (respptr->data + IPADDR_OFFSET));
 
         uint8_t prefixOffset = IPADDR_OFFSET + addrSize;
 
@@ -541,9 +571,8 @@
 
         uint8_t gatewayOffset = prefixOffset + sizeof(decltype(prefix));
 
-        inet_pton(addressFamily,
-                  properties["Gateway"].get<std::string>().c_str(),
-                  (respptr->data + gatewayOffset));
+        inet_pton(addressFamily, gateway.c_str(),
+                 (respptr->data + gatewayOffset));
 
     }
     catch (InternalFailure& e)
@@ -621,7 +650,7 @@
 {
     using namespace std::string_literals;
     std::string host_network_config;
-    char mac[SIZE_MAC] = {0};
+    char mac[] {"00:00:00:00:00:00"};
     std::string ipAddress, gateway;
     char addrOrigin {0};
     uint8_t addrSize {0};
@@ -690,7 +719,8 @@
 
             uint8_t prefixOffset = IPADDR_OFFSET + addrSize;
 
-            memcpy(&prefix, &(reqptr->data[prefixOffset]), sizeof(decltype(prefix)));
+            memcpy(&prefix, &(reqptr->data[prefixOffset]),
+                   sizeof(decltype(prefix)));
 
             uint8_t gatewayOffset = prefixOffset + sizeof(decltype(prefix));
 
@@ -701,6 +731,7 @@
             }
 
             ipAddress = getAddrStr(family, reqptr->data, IPADDR_OFFSET, addrSize);
+
             gateway = getAddrStr(family, reqptr->data, gatewayOffset, addrSize);
 
         } while(0);
@@ -710,8 +741,6 @@
             ",prefix="s + std::to_string(prefix) + ",gateway="s + gateway +
             ",mac="s + mac + ",addressOrigin="s + addressOrigin;
 
-        log<level::DEBUG>("Network configuration changed",
-                entry("NETWORKCONFIG=%s", host_network_config.c_str()));
 
         auto ipObjectInfo = getDbusObject(IP_INTERFACE, SETTINGS_ROOT,
                                           SETTINGS_MATCH);
@@ -732,6 +761,9 @@
         setDbusProperty(macObjectInfo.second, macObjectInfo.first,
                 MAC_INTERFACE,"MACAddress", std::string(mac));
 
+        log<level::DEBUG>("Network configuration changed",
+                entry("NETWORKCONFIG=%s", host_network_config.c_str()));
+
     }
     catch (InternalFailure& e)
     {