ethernet_interface: Get rid of address gateway usage

This parameter doesn't make any sense the way it is used currently. It
applies a "gateway" route to the routing table for the network of the IP
address that gets assigned to the interface. This ends up with two
routes being configured, one that designates all addresses on the
network be routed locally, and a second that designates those same
addresses be routed through the "gateway". Since the l2 route has
priority, we will never actually use this gateway route. The logic
didn't make sense conceptually anyway.

If you created an ip of 192.168.10.2/24 and gw 192.168.10.1 you would
end up with the routes:
192.168.10.0/24 via 192.168.10.1 dev eth0 proto static
192.168.10.0/24 dev eth0 proto kernel scope link src 192.168.10.100

But the kernel won't actually let you do this an will just ignore the
"gateway" route.

Change-Id: I4fb8b322e45164031599a94e573ff0bbd3cc8eb5
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/ethernet_interface.cpp b/ethernet_interface.cpp
index a1438de..a2cd1c8 100644
--- a/ethernet_interface.cpp
+++ b/ethernet_interface.cpp
@@ -6,7 +6,6 @@
 #include "ipaddress.hpp"
 #include "neighbor.hpp"
 #include "network_manager.hpp"
-#include "routing_table.hpp"
 #include "vlan_interface.hpp"
 
 #include <arpa/inet.h>
@@ -79,8 +78,6 @@
 
     auto addrs = getInterfaceAddrs()[interfaceName()];
 
-    route::Table routingTable;
-
     for (auto& addr : addrs)
     {
         IP::Protocol addressType = convertFamily(addr.addrType);
@@ -93,8 +90,8 @@
         {
             origin = IP::AddressOrigin::LinkLocal;
         }
-        std::string gateway =
-            routingTable.getGateway(addr.addrType, addr.ipaddress, addr.prefix);
+        // Obsolete parameter
+        std::string gateway = "";
 
         std::string ipAddressObjectPath = generateObjectPath(
             addressType, addr.ipaddress, addr.prefix, gateway);
@@ -165,18 +162,13 @@
                               Argument::ARGUMENT_VALUE(ipaddress.c_str()));
     }
 
-    if (!gateway.empty() && (!isValidIP(addressFamily, gateway)))
-    {
-        log<level::ERR>("Not a valid Gateway"),
-            entry("GATEWAY=%s", gateway.c_str());
-        elog<InvalidArgument>(Argument::ARGUMENT_NAME("gateway"),
-                              Argument::ARGUMENT_VALUE(gateway.c_str()));
-    }
+    // Gateway is an obsolete parameter
+    gateway = "";
 
     if (!isValidPrefix(addressFamily, prefixLength))
     {
         log<level::ERR>("PrefixLength is not correct "),
-            entry("PREFIXLENGTH=%d", gateway.c_str());
+            entry("PREFIXLENGTH=%" PRIu8, prefixLength);
         elog<InvalidArgument>(
             Argument::ARGUMENT_NAME("prefixLength"),
             Argument::ARGUMENT_VALUE(std::to_string(prefixLength).c_str()));
@@ -679,30 +671,6 @@
                 stream << "Gateway=" << gateway6 << "\n";
             }
         }
-
-        // write the route section
-        for (const auto& addr : addrs)
-        {
-            if (addr.second->origin() == AddressOrigin::Static)
-            {
-                int addressFamily = addr.second->type() == IP::Protocol::IPv4
-                                        ? AF_INET
-                                        : AF_INET6;
-
-                std::string destination =
-                    getNetworkID(addressFamily, addr.second->address(),
-                                 addr.second->prefixLength());
-
-                if (addr.second->gateway() != "0.0.0.0" &&
-                    addr.second->gateway() != "" && destination != "0.0.0.0" &&
-                    destination != "")
-                {
-                    stream << "[Route]\n";
-                    stream << "Gateway=" << addr.second->gateway() << "\n";
-                    stream << "Destination=" << destination << "\n";
-                }
-            }
-        }
     }
 
     // Write the neighbor sections
diff --git a/routing_table.cpp b/routing_table.cpp
index 4391668..ffbeecb 100644
--- a/routing_table.cpp
+++ b/routing_table.cpp
@@ -227,20 +227,6 @@
     return routeList;
 }
 
-std::string Table::getGateway(int addressFamily, const std::string& ipaddress,
-                              uint8_t prefix) const
-{
-    std::string gateway;
-    std::string network = getNetworkID(addressFamily, ipaddress, prefix);
-    auto it = routeList.find(network);
-    if (it != routeList.end())
-    {
-        gateway = it->second.gateway;
-    }
-
-    return gateway;
-}
-
 } // namespace route
 } // namespace network
 } // namespace phosphor
diff --git a/routing_table.hpp b/routing_table.hpp
index 4943a4a..6923592 100644
--- a/routing_table.hpp
+++ b/routing_table.hpp
@@ -79,16 +79,6 @@
         return defaultGateway6;
     };
 
-    /**
-     * @brief get the gateway for the network.
-     * @param[in] addressFamily - ip address family(AF_INET/AF_INET6)
-     * @param[in] ipaddress - ip address.
-     * @param[in] prefix - prefix length.
-     * @returns the gatway for the given network.
-     */
-    std::string getGateway(int addressFamily, const std::string& ipaddress,
-                           uint8_t prefix) const;
-
   private:
     /**
      * @brief read the routing data from the socket and fill the buffer.
diff --git a/test/test_util.cpp b/test/test_util.cpp
index 37193b8..33b5c48 100644
--- a/test/test_util.cpp
+++ b/test/test_util.cpp
@@ -200,26 +200,6 @@
     EXPECT_EQ(mask, "255.255.255.224");
 }
 
-TEST_F(TestUtil, getNetworkAddress)
-{
-    std::string address = getNetworkID(AF_INET, "9.3.23.251", 24);
-    EXPECT_EQ("9.3.23.0", address);
-
-    address = getNetworkID(AF_INET, "9.3.23.251", 25);
-    EXPECT_EQ("9.3.23.128", address);
-
-    address = getNetworkID(AF_INET6, "2001:db8:abcd:dd12::0", 64);
-    EXPECT_EQ("2001:db8:abcd:dd12::", address);
-
-    EXPECT_THROW(getNetworkID(AF_INET, "a.b.c.d", 25), InternalFailure);
-
-    EXPECT_THROW(getNetworkID(AF_INET6, "2001:db8:gghh:dd12::0", 64),
-                 InternalFailure);
-
-    address = getNetworkID(AF_INET6, "fe80::201:6cff:fe80:228", 64);
-    EXPECT_EQ("fe80::", address);
-}
-
 TEST_F(TestUtil, CopyFromTooSmall)
 {
     constexpr auto expected = "abcde"sv;
diff --git a/util.cpp b/util.cpp
index a17bc5d..b01d644 100644
--- a/util.cpp
+++ b/util.cpp
@@ -146,65 +146,6 @@
     return inet_ntoa(netmask);
 }
 
-std::string getNetworkID(int addressFamily, const std::string& ipaddress,
-                         uint8_t prefix)
-{
-    unsigned char* pntMask = nullptr;
-    unsigned char* pntNetwork = nullptr;
-    int bit{};
-    int offset{};
-    struct in6_addr netmask
-    {
-    };
-    const u_char maskbit[] = {0x00, 0x80, 0xc0, 0xe0, 0xf0,
-                              0xf8, 0xfc, 0xfe, 0xff};
-
-    pntMask = reinterpret_cast<unsigned char*>(&netmask);
-
-    offset = prefix / 8;
-    bit = prefix % 8;
-
-    while (offset--)
-    {
-        *pntMask++ = 0xff;
-    }
-
-    if (bit)
-    {
-        *pntMask = maskbit[bit];
-    }
-
-    // convert ipaddres string into network address
-    struct in6_addr ipaddressNetwork;
-    if (inet_pton(addressFamily, ipaddress.c_str(), &ipaddressNetwork) <= 0)
-    {
-        log<level::ERR>("inet_pton failure",
-                        entry("IPADDRESS=%s", ipaddress.c_str()));
-        elog<InternalFailure>();
-
-        return "";
-    }
-
-    // Now bit wise and gets you the network address
-    pntMask = reinterpret_cast<unsigned char*>(&netmask);
-    pntNetwork = reinterpret_cast<unsigned char*>(&ipaddressNetwork);
-
-    for (int i = 0; i < 16; i++)
-    {
-        pntNetwork[i] = pntNetwork[i] & pntMask[i];
-    }
-
-    // convert the network address into string fomat.
-    char networkString[INET6_ADDRSTRLEN] = {0};
-    if (inet_ntop(addressFamily, &ipaddressNetwork, networkString,
-                  INET6_ADDRSTRLEN) == NULL)
-    {
-        log<level::ERR>("inet_ntop failure");
-        elog<InternalFailure>();
-    }
-    return networkString;
-}
-
 InAddrAny addrFromBuf(int addressFamily, std::string_view buf)
 {
     if (addressFamily == AF_INET)
diff --git a/util.hpp b/util.hpp
index 79593c5..1f86815 100644
--- a/util.hpp
+++ b/util.hpp
@@ -124,15 +124,6 @@
  */
 bool isValidPrefix(int addressFamily, uint8_t prefixLength);
 
-/* @brief gets the network section of the ip address.
- * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
- * @param[in] ipaddress - IP address.
- * @param[in] prefix - prefix length.
- * @returns network section of the ipaddress.
- */
-std::string getNetworkID(int addressFamily, const std::string& ipaddress,
-                         uint8_t prefix);
-
 /** @brief Gets the map of interface and the associated
  *         address.
  *  @returns map of interface and the address.