getifaddrs & ipv6 scope

Andrew Thompson thompsa at FreeBSD.org
Thu Apr 12 23:54:06 UTC 2012


Hi,


I have noticed that getifaddrs() does not have sin6_scope_id set to
the interface id for link local addresses on AF_INET6 types.  Running
the following program gives different results on Linux

FreeBSD:

dev: bge0     address: <fe80:2::a6ba:dbff:fe03:d69> scope 0
dev: xl0      address: <fe80:4::204:75ff:fedc:39c1> scope 0
dev: lo0      address: <::1> scope 0
dev: lo0      address: <fe80:5::1> scope 0

Linux:

dev: lo       address: <::1> scope 0
dev: eth1     address: <2404:130:0:1000:204:75ff:febc:b8f0> scope 0
dev: eth1     address: <fe80::204:75ff:febc:b8f0%eth1> scope 2
dev: eth0     address: <fe80::222:4dff:fe54:3b1d%eth0> scope 3


Should FreeBSD be setting sin6_scope_id?

Andrew

----

#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netdb.h>
#include <ifaddrs.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <netinet/in.h>
#include <arpa/inet.h>

int
main(int argc, char *argv[])
{
        struct ifaddrs *ifaddr, *ifa;
        char host[NI_MAXHOST];
        int rc;

        if (getifaddrs(&ifaddr) == -1) {
                perror("getifaddrs");
                exit(EXIT_FAILURE);
        }

        for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
                struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)ifa->ifa_addr;

                if (ifa->ifa_addr == NULL)
                        continue;
                if (ifa->ifa_addr->sa_family != AF_INET6)
                        continue;

                rc = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in6),
                    host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
                if (rc != 0) {
                        printf("getnameinfo() failed: %s\n", gai_strerror(rc));
                        exit(EXIT_FAILURE);
                }
                printf("dev: %-8s address: <%s> scope %d\n",
                    ifa->ifa_name, host, in6->sin6_scope_id);
        }

        freeifaddrs(ifaddr);
        exit(EXIT_SUCCESS);
}


More information about the freebsd-net mailing list