mailing list of musl libc
 help / color / mirror / code / Atom feed
* Fix handling of peer-to-peer interfaces in getifaddrs()
@ 2015-11-17 10:33 Jo-Philipp Wich
  2015-11-17 10:33 ` [PATCH] properly handle point-to-point " Jo-Philipp Wich
  0 siblings, 1 reply; 8+ messages in thread
From: Jo-Philipp Wich @ 2015-11-17 10:33 UTC (permalink / raw)
  To: musl

properly handle point-to-point interfaces in getifaddrs()

With point-to-point interfaces, the IFA_ADDRESS netlink attribute contains
the peer address while an extra attribute IFA_LOCAL carries the actual local
interface address.

Currently musl lacks any treatment of IFA_LOCAL leading to bogus results
when using getifaddrs() to obtain the local and remote IP addresses of
point-to-point interfaces like ppp ones.

The following test case illustrates the problem:

-- 8< --
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <sys/types.h>
#include <ifaddrs.h>
#include <stdio.h>
 
int main(int argc, char **argv) {
        struct ifaddrs *ifa, *ifap;
        char local[32];
        char remote[32];
 
        if (!getifaddrs(&ifa)) {
                for (ifap = ifa; ifap; ifap = ifap->ifa_next) {
                        if (!ifap->ifa_name || strcmp(ifap->ifa_name, argv[1]) ||
                            !ifap->ifa_addr || ifap->ifa_addr->sa_family != AF_INET)
                                continue;
 
                        inet_ntop(AF_INET, &((struct sockaddr_in *)ifap->ifa_addr)->sin_addr, local, sizeof(local));
 
                        if (ifap->ifa_dstaddr)
                                inet_ntop(AF_INET, &((struct sockaddr_in *)ifap->ifa_dstaddr)->sin_addr, remote, sizeof(remote));
                        else
                                strcpy(remote, "(null)");
 
                        printf("local addr = %s / remote addr = %s\n", local, remote);
                        break;
                }
 
                freeifaddrs(ifa);
        }
 
        return 0;
}
-- >8 --

I used the following command sequence to assert the results:

$ gcc -o /tmp/ifa /tmp/ifa.c
$ ip link add name dummy0 type dummy
$ ip addr add 1.1.1.1 peer 2.2.2.2 dev dummy0
$ /tmp/ifa dummy0

That led to the following result on musl:

  local addr = 2.2.2.2 / remote addr = (null)

Note that the local address is unexpectedly 2.2.2.2 while it should be 1.1.1.1
and the remote address is not set at all.

Running the same test on an ordinary glibc system (Debian) and on an uclibc
based OpenWrt gives the correct result:

  local addr = 1.1.1.1 / remote addr = 2.2.2.2


The proposed change adds special treatment of IFA_LOCAL to getifaddrs(),
following the logic used in uclibc and glibc implementations.

Reagrds,
Jo-Philipp



^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2015-11-30 20:01 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-11-17 10:33 Fix handling of peer-to-peer interfaces in getifaddrs() Jo-Philipp Wich
2015-11-17 10:33 ` [PATCH] properly handle point-to-point " Jo-Philipp Wich
2015-11-19 20:43   ` [PATCHv2] " Jo-Philipp Wich
2015-11-21  9:14     ` Timo Teras
2015-11-21 19:20       ` Rich Felker
2015-11-23  9:12         ` Timo Teras
2015-11-23 17:01           ` Rich Felker
2015-11-30 20:01     ` Rich Felker

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).