ra: align ifindex resolving
authorHans Dedecker <dedeckeh@gmail.com>
Wed, 6 Jan 2021 20:15:49 +0000 (21:15 +0100)
committerHans Dedecker <dedeckeh@gmail.com>
Wed, 20 Jan 2021 20:02:57 +0000 (21:02 +0100)
Use ioctl SIOCGIFINDEX to resolve ifindex similar as in init_dhcpv6

Signed-off-by: Hans Dedecker <dedeckeh@gmail.com>
(cherry picked from commit 2b6959dfe2095402e004b63d9aca9900c074abb6)

src/ra.c

index 898f449168e29bae08e48e9395f420d13b2ec899..9fcdb31329f788a328993be9f68c15514bc56cc3 100644 (file)
--- a/src/ra.c
+++ b/src/ra.c
@@ -27,6 +27,7 @@
 
 #include <net/if.h>
 #include <arpa/inet.h>
+#include <sys/ioctl.h>
 #include <sys/socket.h>
 #include <sys/types.h>
 #include <netinet/in.h>
@@ -76,6 +77,8 @@ static void ra_send_rs(int signal __attribute__((unused)));
 int ra_init(const char *ifname, const struct in6_addr *ifid,
                unsigned int options, unsigned int holdoff_interval)
 {
+       struct ifreq ifr;
+
        ra_options = options;
        ra_holdoff_interval = holdoff_interval;
 
@@ -84,11 +87,12 @@ int ra_init(const char *ifname, const struct in6_addr *ifid,
        if (sock < 0)
                goto failure;
 
-       if_index = if_nametoindex(ifname);
-       if (!if_index)
+       memset(&ifr, 0, sizeof(ifr));
+       strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name) - 1);
+       if (ioctl(sock, SIOCGIFINDEX, &ifr) < 0)
                goto failure;
 
-       strncpy(if_name, ifname, sizeof(if_name) - 1);
+       if_index = ifr.ifr_ifindex;
        lladdr = *ifid;
 
        rtnl = socket(AF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_ROUTE);