[posix] pass an interface index to IPV6_BOUND_IF (#13587)

IPV6_BOUND_IF takes an interface index on every BSD (FreeBSD, NetBSD,
macOS), but since #9887 the BSD branch of InfraNetif::CreateIcmp6Socket()
has passed the interface name string instead. On macOS that makes the
setsockopt() fail with EINVAL, so otbr-agent (with border routing
enabled) dies at startup. Resolve the name with if_nametoindex() and
pass the index, exiting with OT_EXIT_INVALID_ARGUMENTS for an unknown
name as SetInfraNetif() does.
This commit is contained in:
Clara
2026-09-06 22:55:26 +02:00
committed by GitHub
parent b54f21fa2f
commit 8e334caa2e
+8 -1
View File
@@ -41,6 +41,7 @@
#include <errno.h>
#include <ifaddrs.h>
#include <net/if.h>
#include <netdb.h>
// clang-format off
#include <netinet/in.h>
@@ -182,7 +183,13 @@ int InfraNetif::CreateIcmp6Socket(const char *aInfraIfName)
#ifdef __linux__
rval = setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, aInfraIfName, strlen(aInfraIfName));
#else // __NetBSD__ || __FreeBSD__ || __APPLE__
rval = setsockopt(sock, IPPROTO_IPV6, IPV6_BOUND_IF, aInfraIfName, strlen(aInfraIfName));
{
// IPV6_BOUND_IF takes an interface index, not a name.
unsigned int ifIndex = if_nametoindex(aInfraIfName);
VerifyOrDie(ifIndex != 0, OT_EXIT_INVALID_ARGUMENTS);
rval = setsockopt(sock, IPPROTO_IPV6, IPV6_BOUND_IF, &ifIndex, sizeof(ifIndex));
}
#endif // __linux__
VerifyOrDie(rval == 0, OT_EXIT_ERROR_ERRNO);