Check prefix length in Set LAN Config Param 6 & 56

In phosphor-networkd, the allowed prefix length is 1 to 32 for IPv4 and
1 to 128 for IPv6. Calling DBus API with an invalid prefix length will
result in uncaught exceptions. This patch checks prefix length before
setting it and returns appropriate error code on invalid value.

Tested:
* Setting IPv4 SubnetMask to 0.0.0.0 (Param 6) returns 0xcc.
* Setting IPv6 static address with PrefixLength==129 (Param 56) returns
  0xc9.

Change-Id: I03af233905e415c96c896c85baf98846d0880e95
Signed-off-by: Jiaqing Zhao <jiaqing.zhao@intel.com>
diff --git a/transporthandler.cpp b/transporthandler.cpp
index 056c3f5..0f9f8a3 100644
--- a/transporthandler.cpp
+++ b/transporthandler.cpp
@@ -992,8 +992,12 @@
                 return responseReqDataLenInvalid();
             }
             copyInto(netmask, bytes);
-            channelCall<reconfigureIfAddr4>(channel, std::nullopt,
-                                            netmaskToPrefix(netmask));
+            uint8_t prefix = netmaskToPrefix(netmask);
+            if (prefix < MIN_IPV4_PREFIX_LENGTH)
+            {
+                return responseInvalidFieldRequest();
+            }
+            channelCall<reconfigureIfAddr4>(channel, std::nullopt, prefix);
             return responseSuccess();
         }
         case LanParam::Gateway1:
@@ -1109,6 +1113,11 @@
             copyInto(ip, ipbytes);
             if (enabled)
             {
+                if (prefix < MIN_IPV6_PREFIX_LENGTH ||
+                    prefix > MAX_IPV6_PREFIX_LENGTH)
+                {
+                    return responseParmOutOfRange();
+                }
                 try
                 {
                     channelCall<reconfigureIfAddr6>(channel, set, ip, prefix);
diff --git a/transporthandler.hpp b/transporthandler.hpp
index 6c58dc5..c2940b4 100644
--- a/transporthandler.hpp
+++ b/transporthandler.hpp
@@ -158,6 +158,12 @@
 constexpr uint8_t MAX_IPV6_STATIC_ADDRESSES = 15;
 constexpr uint8_t MAX_IPV6_DYNAMIC_ADDRESSES = 15;
 
+// Prefix length limits of phosphor-networkd
+constexpr uint8_t MIN_IPV4_PREFIX_LENGTH = 1;
+constexpr uint8_t MAX_IPV4_PREFIX_LENGTH = 32;
+constexpr uint8_t MIN_IPV6_PREFIX_LENGTH = 1;
+constexpr uint8_t MAX_IPV6_PREFIX_LENGTH = 128;
+
 /** @brief The dbus parameters for the interface corresponding to a channel
  *         This helps reduce the number of mapper lookups we need for each
  *         query and simplifies finding the VLAN interface if needed.