network_manager: Move interface validation
This will be required so the rtnetlink_server can add new interfaces.
Change-Id: I72f8687515ff92bdc339689da0c07788ef664504
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/src/network_manager.cpp b/src/network_manager.cpp
index 79a4e41..62ca491 100644
--- a/src/network_manager.cpp
+++ b/src/network_manager.cpp
@@ -6,6 +6,9 @@
#include "ipaddress.hpp"
#include "system_queries.hpp"
#include "types.hpp"
+#include "util.hpp"
+
+#include <net/if.h>
#include <filesystem>
#include <fstream>
@@ -141,6 +144,22 @@
void Manager::addInterface(const InterfaceInfo& info)
{
+ if (info.flags & IFF_LOOPBACK)
+ {
+ return;
+ }
+ if (!info.name)
+ {
+ throw std::invalid_argument("Interface missing name");
+ }
+ const auto& ignored = internal::getIgnoredInterfaces();
+ if (ignored.find(*info.name) != ignored.end())
+ {
+ auto msg = fmt::format("Ignoring interface {}\n", *info.name);
+ log<level::INFO>(msg.c_str());
+ return;
+ }
+
auto it = systemdNetworkdEnabled.find(info.idx);
if (it != systemdNetworkdEnabled.end())
{
diff --git a/src/system_queries.cpp b/src/system_queries.cpp
index 5006095..19c37ba 100644
--- a/src/system_queries.cpp
+++ b/src/system_queries.cpp
@@ -2,7 +2,6 @@
#include "netlink.hpp"
#include "rtnetlink.hpp"
-#include "util.hpp"
#include <fmt/format.h>
#include <linux/ethtool.h>
@@ -207,24 +206,6 @@
return ret;
}
-bool detail::validateNewInterface(const InterfaceInfo& info)
-{
- if (info.flags & IFF_LOOPBACK)
- {
- return false;
- }
- if (!info.name)
- {
- throw std::invalid_argument("Interface Dump missing name");
- }
- const auto& ignored = internal::getIgnoredInterfaces();
- if (ignored.find(*info.name) != ignored.end())
- {
- return false;
- }
- return true;
-}
-
bool detail::validateNewAddr(const AddressInfo& info,
const AddressFilter& filter) noexcept
{
@@ -251,11 +232,7 @@
auto cb = [&](const nlmsghdr& hdr, std::string_view msg) {
try
{
- auto info = detail::parseInterface(hdr, msg);
- if (detail::validateNewInterface(info))
- {
- ret.emplace_back(std::move(info));
- }
+ ret.emplace_back(detail::parseInterface(hdr, msg));
}
catch (const std::exception& e)
{
diff --git a/src/system_queries.hpp b/src/system_queries.hpp
index 9217a1f..9ec478d 100644
--- a/src/system_queries.hpp
+++ b/src/system_queries.hpp
@@ -43,7 +43,6 @@
namespace detail
{
InterfaceInfo parseInterface(const nlmsghdr& hdr, std::string_view msg);
-bool validateNewInterface(const InterfaceInfo& info);
bool validateNewAddr(const AddressInfo& info,
const AddressFilter& filter) noexcept;
bool validateNewNeigh(const NeighborInfo& info,
diff --git a/test/test_system_queries.cpp b/test/test_system_queries.cpp
index a79bc77..5042cc6 100644
--- a/test/test_system_queries.cpp
+++ b/test/test_system_queries.cpp
@@ -82,35 +82,6 @@
EXPECT_EQ(info, expected);
}
-TEST(ValidateNewInterface, Loopback)
-{
- InterfaceInfo info;
- info.flags = IFF_LOOPBACK | IFF_RUNNING;
- EXPECT_FALSE(validateNewInterface(info));
-}
-
-TEST(ValidateNewInterface, NoName)
-{
- EXPECT_THROW(validateNewInterface(InterfaceInfo{}), std::invalid_argument);
-}
-
-TEST(ValidateNewInterface, IgnoredInterface)
-{
- InterfaceInfo info;
- setenv("IGNORED_INTERFACES", "ign", true);
- info.name = "ign";
- info.flags = IFF_RUNNING;
- EXPECT_FALSE(validateNewInterface(info));
-}
-
-TEST(ValidateNewInterface, Valid)
-{
- InterfaceInfo info;
- info.name = "eth0";
- info.flags = 0;
- EXPECT_TRUE(validateNewInterface(info));
-}
-
TEST(ValidateNewAddr, Filtering)
{
AddressInfo info = {};