Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not advertise temporary/deprecated IPv6 addresses by minimal mDNS on Linux #22009

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 62 additions & 1 deletion src/inet/InetInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,19 @@
#include <fcntl.h>
#include <sys/socket.h>
#include <unistd.h>

#ifdef HAVE_SYS_SOCKIO_H
#include <sys/sockio.h>
#endif /* HAVE_SYS_SOCKIO_H */

#include <ifaddrs.h>
#include <net/if.h>
#include <sys/ioctl.h>

#if __linux__
#include <linux/if_addr.h>
#endif

#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS && CHIP_SYSTEM_CONFIG_USE_BSD_IFADDRS

#if CHIP_SYSTEM_CONFIG_USE_ZEPHYR_NET_IF
Expand Down Expand Up @@ -154,6 +161,11 @@ uint8_t InterfaceAddressIterator::GetPrefixLength()
return 64;
}

BitFlags<InterfaceAddressIterator::Flags> InterfaceAddressIterator::GetFlags()
{
return BitFlags<InterfaceAddressIterator::Flags>();
}

#endif

#if CHIP_SYSTEM_CONFIG_USE_LWIP && !CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT
Expand Down Expand Up @@ -405,7 +417,12 @@ CHIP_ERROR InterfaceId::GetLinkLocalAddr(IPAddress * llAddr) const
return CHIP_NO_ERROR;
}

#endif // CHIP_SYSTEM_CONFIG_USE_LWIP
BitFlags<InterfaceAddressIterator::Flags> InterfaceAddressIterator::GetFlags()
{
return BitFlags<InterfaceAddressIterator::Flags>();
}

#endif // CHIP_SYSTEM_CONFIG_USE_LWIP && !CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT

#if CHIP_SYSTEM_CONFIG_USE_SOCKETS && CHIP_SYSTEM_CONFIG_USE_BSD_IFADDRS

Expand Down Expand Up @@ -897,6 +914,45 @@ CHIP_ERROR InterfaceId::GetLinkLocalAddr(IPAddress * llAddr) const
return (found) ? CHIP_NO_ERROR : INET_ERROR_ADDRESS_NOT_FOUND;
}

BitFlags<InterfaceAddressIterator::Flags> InterfaceAddressIterator::GetFlags()
{
BitFlags<InterfaceAddressIterator::Flags> flags;

#if __linux__
if (!HasCurrent())
{
return flags;
}

if (mCurAddr->ifa_flags & IFA_F_OPTIMISTIC)
{
flags.Set(InterfaceAddressIterator::Flags::kNotFinal);
}

if (mCurAddr->ifa_flags & IFA_F_DADFAILED)
{
flags.Set(InterfaceAddressIterator::Flags::kNotFinal);
}

if (mCurAddr->ifa_flags & IFA_F_TENTATIVE)
{
flags.Set(InterfaceAddressIterator::Flags::kNotFinal);
}
Comment on lines +927 to +940
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest to ifdef those flasg separately in case some platforms like Android don't have all of them

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would expect CI to detect this. I believe for android we care about IFA_F_OPTIMISTIC and DADFAILED, however the flags kNotFinal and such can remain the same as if a platform does not have that concept, the flags are not set. This is what currently happen for all non-linux


if (mCurAddr->ifa_flags & IFA_F_TEMPORARY)
{
flags.Set(InterfaceAddressIterator::Flags::kTemporary);
}

if (mCurAddr->ifa_flags & IFA_F_DEPRECATED)
{
flags.Set(InterfaceAddressIterator::Flags::kDeprecated);
}
#endif

return flags;
}

#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS && CHIP_SYSTEM_CONFIG_USE_BSD_IFADDRS

#if CHIP_SYSTEM_CONFIG_USE_ZEPHYR_NET_IF
Expand Down Expand Up @@ -1121,6 +1177,11 @@ CHIP_ERROR InterfaceId::GetLinkLocalAddr(IPAddress * llAddr) const
return CHIP_NO_ERROR;
}

BitFlags<InterfaceAddressIterator::Flags> InterfaceAddressIterator::GetFlags()
{
return BitFlags<InterfaceAddressIterator::Flags>();
}

#endif // CHIP_SYSTEM_CONFIG_USE_ZEPHYR_NET_IF

// static
Expand Down
14 changes: 14 additions & 0 deletions src/inet/InetInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,13 @@ class InterfaceIterator
class DLL_EXPORT InterfaceAddressIterator
{
public:
enum class Flags : uint8_t
{
kNotFinal = (1 << 0), // Not yet valid: Optimistic/DAD failed/tentative
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like it's exposing Linux flags in an API that is mean to work across a range of platforms that may not have these granular flags.

Perhaps start with just one (since all of the logic below seems to be treating all of these the same), and expand in the future as needed?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The flags here correspond to RFC 8981 concepts, so I think they're universally meaningful (even if not exposed on all platforms).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to make it somewhat conceptual by saying not final instead of providing linux flags. Beyond that, RFC concepts should be sufficient and we do not require all platforms to support them.

I was unsure if we can simplify more. We could in theory just have a kDoNotUse flag and let each platform pick what the rules are, but that seems a bit rough. We can go that route as a followup.

kTemporary = (1 << 1), // IFA_F_TEMPORARY on linux
kDeprecated = (1 << 2), // IFA_F_DEPRECATED on linux
};

/**
* Constructs an InterfaceAddressIterator object.
*
Expand Down Expand Up @@ -520,6 +527,13 @@ class DLL_EXPORT InterfaceAddressIterator
*/
bool HasBroadcastAddress();

/**
* Fetch the flags for the given interface address.
*
* Note: not all implementations may support all flags.
*/
BitFlags<Flags> GetFlags();

private:
#if CHIP_SYSTEM_CONFIG_USE_LWIP && !CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT
enum
Expand Down
8 changes: 8 additions & 0 deletions src/lib/dnssd/Advertiser_ImplMinimalMdns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -882,11 +882,19 @@ void AdvertiserMinMdns::AdvertiseRecords(BroadcastAdvertiseType type)
continue;
}

if (interfaceAddress.GetFlags().HasAny(chip::Inet::InterfaceAddressIterator::Flags::kNotFinal,
chip::Inet::InterfaceAddressIterator::Flags::kTemporary,
chip::Inet::InterfaceAddressIterator::Flags::kDeprecated))
{
continue;
}

Inet::IPAddress ipAddress;
if (interfaceAddress.GetAddress(ipAddress) != CHIP_NO_ERROR)
{
continue;
}

if (!ShouldAdvertiseOn(interfaceAddress.GetInterfaceId(), ipAddress))
{
continue;
Expand Down
22 changes: 18 additions & 4 deletions src/lib/dnssd/minimal_mdns/responders/IP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,20 @@
namespace mdns {
namespace Minimal {

using namespace chip::Inet;

void IPv4Responder::AddAllResponses(const chip::Inet::IPPacketInfo * source, ResponderDelegate * delegate,
const ResponseConfiguration & configuration)
{
chip::Inet::IPAddress addr;
for (chip::Inet::InterfaceAddressIterator it; it.HasCurrent(); it.Next())
IPAddress addr;
for (InterfaceAddressIterator it; it.HasCurrent(); it.Next())
{
if (it.GetFlags().HasAny(InterfaceAddressIterator::Flags::kNotFinal, InterfaceAddressIterator::Flags::kTemporary,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add comment here and in IPv6 that we will not advertise addresses that are not current and stable

InterfaceAddressIterator::Flags::kDeprecated))
{
continue;
}

if ((it.GetInterfaceId() == source->Interface) && (it.GetAddress(addr) == CHIP_NO_ERROR) && addr.IsIPv4())
{
IPResourceRecord record(GetQName(), addr);
Expand All @@ -39,14 +47,20 @@ void IPv4Responder::AddAllResponses(const chip::Inet::IPPacketInfo * source, Res
void IPv6Responder::AddAllResponses(const chip::Inet::IPPacketInfo * source, ResponderDelegate * delegate,
const ResponseConfiguration & configuration)
{
for (chip::Inet::InterfaceAddressIterator it; it.HasCurrent(); it.Next())
for (InterfaceAddressIterator it; it.HasCurrent(); it.Next())
{
if (it.GetInterfaceId() != source->Interface)
{
continue;
}

chip::Inet::IPAddress addr;
if (it.GetFlags().HasAny(InterfaceAddressIterator::Flags::kNotFinal, InterfaceAddressIterator::Flags::kTemporary,
InterfaceAddressIterator::Flags::kDeprecated))
{
continue;
}

IPAddress addr;
if ((it.GetInterfaceId() == source->Interface) && (it.GetAddress(addr) == CHIP_NO_ERROR) && addr.IsIPv6())
{
IPResourceRecord record(GetQName(), addr);
Expand Down