Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 14 Apr 2012 03:03:36 +0900
From:      Hajimu UMEMOTO <ume@FreeBSD.org>
To:        Andrew Thompson <thompsa@FreeBSD.org>
Cc:        freebsd-net@FreeBSD.org, Rainer Bredehorn <Bredehorn@gmx.de>
Subject:   Re: getifaddrs & ipv6 scope
Message-ID:  <yged37becg7.wl%ume@mahoroba.org>
In-Reply-To: <CAFAOGNRLcadCMCZ8WjaFi3XYhzV55Tp0v=%2BoMGyLOv666bFHkA@mail.gmail.com>
References:  <20120413064142.10640@gmx.net> <CAFAOGNRLcadCMCZ8WjaFi3XYhzV55Tp0v=%2BoMGyLOv666bFHkA@mail.gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help
--Multipart_Sat_Apr_14_03:03:35_2012-1
Content-Type: text/plain; charset=US-ASCII

Hi,

>>>>> On Fri, 13 Apr 2012 20:01:39 +1200
>>>>> Andrew Thompson <thompsa@FreeBSD.org> said:

thompsa> On 13 April 2012 18:41, Rainer Bredehorn <Bredehorn@gmx.de> wrote:
> 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
>
> ifconfig shows the scopeid according to the interface:
>
> inet6 fe80::208:9bff:fe13:784e%fxp1 prefixlen 64 scopeid 0x2
>
> Are you talking about the scope value of an multicast address or
> the scopeid for link local addresses?

thompsa> I am talking about the scopeid for link local addresses which (as far
thompsa> as I understand) is the interface index.

The issue you mentioned comes from an implementation decision of the
KAME IPv6 stack.
The attached patch should address it.  However, it may break the
applications which expect that getifaddrs() returns a link-local
address with KAME's embeded scopeid representation.  I'm not sure
there are such applications, for now.

Sincerely,

--Multipart_Sat_Apr_14_03:03:35_2012-1
Content-Type: text/x-patch; type=patch; charset=US-ASCII
Content-Disposition: attachment; filename="getifaddrs.c-scopeid.diff"
Content-Transfer-Encoding: 7bit

Index: lib/libc/net/getifaddrs.c
diff -u -p lib/libc/net/getifaddrs.c.orig lib/libc/net/getifaddrs.c
--- lib/libc/net/getifaddrs.c.orig	2011-09-23 09:51:37.000000000 +0900
+++ lib/libc/net/getifaddrs.c	2012-04-14 02:49:11.000000000 +0900
@@ -43,6 +43,7 @@ __FBSDID("$FreeBSD: src/lib/libc/net/get
 #include <sys/sysctl.h>
 #include <net/if_dl.h>
 #endif
+#include <netinet/in.h>
 
 #include <errno.h>
 #include <ifaddrs.h>
@@ -87,6 +88,8 @@ __FBSDID("$FreeBSD: src/lib/libc/net/get
 
 #define MAX_SYSCTL_TRY 5
 
+static	void in6_fillscopeid(struct sockaddr_in6 *);
+
 int
 getifaddrs(struct ifaddrs **pif)
 {
@@ -341,6 +344,8 @@ getifaddrs(struct ifaddrs **pif)
 					    (struct sockaddr *)(void *)data;
 					memcpy(data, p, len);
 					data += len;
+					if (ift->ifa_addr->sa_family == AF_INET6)
+						in6_fillscopeid((struct sockaddr_in6 *)ift->ifa_addr);
 					break;
 
 				case RTAX_NETMASK:
@@ -416,3 +421,15 @@ freeifaddrs(struct ifaddrs *ifp)
 
 	free(ifp);
 }
+
+static void
+in6_fillscopeid(struct sockaddr_in6 *sin6)
+{
+#if defined(__KAME__)
+	if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
+		sin6->sin6_scope_id =
+		    ntohs(*(u_int16_t *)&sin6->sin6_addr.s6_addr[2]);
+		sin6->sin6_addr.s6_addr[2] = sin6->sin6_addr.s6_addr[3] = 0;
+	}
+#endif
+}

--Multipart_Sat_Apr_14_03:03:35_2012-1
Content-Type: text/plain; charset=US-ASCII


--
Hajimu UMEMOTO @ Internet Mutual Aid Society Yokohama, Japan
ume@mahoroba.org  ume@{,jp.}FreeBSD.org
http://www.imasy.org/~ume/

--Multipart_Sat_Apr_14_03:03:35_2012-1--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?yged37becg7.wl%ume>