rtnetlink: Return neighbor even if addr is missing
There are valid configurations that return these type of messages from
the kernel and we don't want to fail for them
Change-Id: I74a0fd519e553c2e4bc3e114e7563fe93d8facf6
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/src/ethernet_interface.cpp b/src/ethernet_interface.cpp
index b15f598..4f71a46 100644
--- a/src/ethernet_interface.cpp
+++ b/src/ethernet_interface.cpp
@@ -219,17 +219,16 @@
{
return;
}
- if (!info.mac)
+ if (!info.mac || !info.addr)
{
- auto msg = fmt::format("Missing neighbor mac for `{}` on {}\n",
- info.addr, interfaceName());
+ auto msg = fmt::format("Missing neighbor mac on {}\n", interfaceName());
log<level::ERR>(msg.c_str());
return;
}
staticNeighbors.emplace(
- info.addr, std::make_unique<Neighbor>(bus, std::string_view(objPath),
- *this, info.addr, *info.mac,
- Neighbor::State::Permanent));
+ *info.addr, std::make_unique<Neighbor>(bus, std::string_view(objPath),
+ *this, *info.addr, *info.mac,
+ Neighbor::State::Permanent));
}
void EthernetInterface::createStaticNeighborObjects()
diff --git a/src/rtnetlink.cpp b/src/rtnetlink.cpp
index 6b456b7..febc305 100644
--- a/src/rtnetlink.cpp
+++ b/src/rtnetlink.cpp
@@ -88,7 +88,6 @@
NeighborInfo ret;
ret.ifidx = ndm.ndm_ifindex;
ret.state = ndm.ndm_state;
- bool set_addr = false;
while (!msg.empty())
{
auto [hdr, data] = netlink::extractRtAttr(msg);
@@ -99,13 +98,8 @@
else if (hdr.rta_type == NDA_DST)
{
ret.addr = addrFromBuf(ndm.ndm_family, data);
- set_addr = true;
}
}
- if (!set_addr)
- {
- throw std::runtime_error("Missing address");
- }
return ret;
}
diff --git a/src/rtnetlink_server.cpp b/src/rtnetlink_server.cpp
index 4645367..5afe637 100644
--- a/src/rtnetlink_server.cpp
+++ b/src/rtnetlink_server.cpp
@@ -123,9 +123,9 @@
{
it->second->addStaticNeigh(info);
}
- else
+ else if (info.addr)
{
- it->second->staticNeighbors.erase(info.addr);
+ it->second->staticNeighbors.erase(*info.addr);
}
}
diff --git a/src/types.hpp b/src/types.hpp
index 9262623..709874c 100644
--- a/src/types.hpp
+++ b/src/types.hpp
@@ -138,7 +138,7 @@
{
unsigned ifidx;
uint16_t state;
- InAddrAny addr;
+ std::optional<InAddrAny> addr;
std::optional<ether_addr> mac;
constexpr bool operator==(const NeighborInfo& rhs) const noexcept
diff --git a/test/test_rtnetlink.cpp b/test/test_rtnetlink.cpp
index 8bf2a1e..7b60072 100644
--- a/test/test_rtnetlink.cpp
+++ b/test/test_rtnetlink.cpp
@@ -69,8 +69,8 @@
{
alignas(NLMSG_ALIGNTO) ndmsg ndm = {};
} msg;
- EXPECT_THROW(neighFromRtm(stdplus::raw::asView<char>(msg)),
- std::runtime_error);
+
+ EXPECT_EQ((NeighborInfo{}), neighFromRtm(stdplus::raw::asView<char>(msg)));
}
TEST(NeighFromRtm, NoMac)