Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 18 Apr 2012 18:37:56 +0000 (UTC)
From:      Hiroki Sato <hrs@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-user@freebsd.org
Subject:   svn commit: r234431 - in user/hrs/ipv6: sbin/ifconfig sbin/route sys/net usr.sbin/rpcbind
Message-ID:  <201204181837.q3IIbuc5045409@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: hrs
Date: Wed Apr 18 18:37:56 2012
New Revision: 234431
URL: http://svn.freebsd.org/changeset/base/234431

Log:
  Extract embed scope id and set it to sin6_scope_id in struct sockaddr_in6 for
  userland from the kernel via routing socket or sysctl.  The
  net.inet6.ip6.deembed_scopeid sysctl variable controls this behavior.  The
  default value is 1, and 0 means no de-embed.
  
  De-embed of scope id in userland utilities have been removed except for
  netstat(1) because it directly uses kvm to read the FIB entries, not sysctl or
  routing socket.

Modified:
  user/hrs/ipv6/sbin/ifconfig/af_inet6.c
  user/hrs/ipv6/sbin/route/route.c
  user/hrs/ipv6/sys/net/rtsock.c
  user/hrs/ipv6/usr.sbin/rpcbind/util.c

Modified: user/hrs/ipv6/sbin/ifconfig/af_inet6.c
==============================================================================
--- user/hrs/ipv6/sbin/ifconfig/af_inet6.c	Wed Apr 18 18:35:19 2012	(r234430)
+++ user/hrs/ipv6/sbin/ifconfig/af_inet6.c	Wed Apr 18 18:37:56 2012	(r234431)
@@ -62,7 +62,6 @@ static	struct in6_aliasreq in6_addreq = 
     .ifra_lifetime = { 0, 0, ND6_INFINITE_LIFETIME, ND6_INFINITE_LIFETIME } };
 static	int ip6lifetime;
 
-static	void in6_fillscopeid(struct sockaddr_in6 *sin6);
 static	int prefix(void *, int);
 static	char *sec2str(time_t);
 static	int explicit_prefix = 0;
@@ -166,18 +165,6 @@ setip6eui64(const char *cmd, int dummy _
 }
 
 static void
-in6_fillscopeid(struct sockaddr_in6 *sin6)
-{
-#if defined(__KAME__) && defined(KAME_SCOPEID)
-	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
-}
-
-static void
 in6_status(int s __unused, const struct ifaddrs *ifa)
 {
 	struct sockaddr_in6 *sin, null_sin;
@@ -217,18 +204,7 @@ in6_status(int s __unused, const struct 
 	lifetime = ifr6.ifr_ifru.ifru_lifetime;
 	close(s6);
 
-	/* XXX: embedded link local addr check */
-	if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) &&
-	    *(u_short *)&sin->sin6_addr.s6_addr[2] != 0) {
-		u_short index;
-
-		index = *(u_short *)&sin->sin6_addr.s6_addr[2];
-		*(u_short *)&sin->sin6_addr.s6_addr[2] = 0;
-		if (sin->sin6_scope_id == 0)
-			sin->sin6_scope_id = ntohs(index);
-	}
 	scopeid = sin->sin6_scope_id;
-
 	error = getnameinfo((struct sockaddr *)sin, sin->sin6_len, addr_buf,
 			    sizeof(addr_buf), NULL, 0, NI_NUMERICHOST);
 	if (error != 0)
@@ -245,17 +221,6 @@ in6_status(int s __unused, const struct 
 		if (sin != NULL && sin->sin6_family == AF_INET6) {
 			int error;
 
-			/* XXX: embedded link local addr check */
-			if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) &&
-			    *(u_short *)&sin->sin6_addr.s6_addr[2] != 0) {
-				u_short index;
-
-				index = *(u_short *)&sin->sin6_addr.s6_addr[2];
-				*(u_short *)&sin->sin6_addr.s6_addr[2] = 0;
-				if (sin->sin6_scope_id == 0)
-					sin->sin6_scope_id = ntohs(index);
-			}
-
 			error = getnameinfo((struct sockaddr *)sin,
 					    sin->sin6_len, addr_buf,
 					    sizeof(addr_buf), NULL, 0,
@@ -458,7 +423,6 @@ in6_status_tunnel(int s)
 		return;
 	if (sa->sa_family != AF_INET6)
 		return;
-	in6_fillscopeid(&in6_ifr.ifr_addr);
 	if (getnameinfo(sa, sa->sa_len, src, sizeof(src), 0, 0,
 	    NI_NUMERICHOST) != 0)
 		src[0] = '\0';
@@ -467,7 +431,6 @@ in6_status_tunnel(int s)
 		return;
 	if (sa->sa_family != AF_INET6)
 		return;
-	in6_fillscopeid(&in6_ifr.ifr_addr);
 	if (getnameinfo(sa, sa->sa_len, dst, sizeof(dst), 0, 0,
 	    NI_NUMERICHOST) != 0)
 		dst[0] = '\0';

Modified: user/hrs/ipv6/sbin/route/route.c
==============================================================================
--- user/hrs/ipv6/sbin/route/route.c	Wed Apr 18 18:35:19 2012	(r234430)
+++ user/hrs/ipv6/sbin/route/route.c	Wed Apr 18 18:37:56 2012	(r234431)
@@ -372,18 +372,6 @@ routename(struct sockaddr *sa)
 		memcpy(&sin6, sa, sa->sa_len);
 		sin6.sin6_len = sizeof(struct sockaddr_in6);
 		sin6.sin6_family = AF_INET6;
-#ifdef __KAME__
-		if (sa->sa_len == sizeof(struct sockaddr_in6) &&
-		    (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr) ||
-		     IN6_IS_ADDR_MC_LINKLOCAL(&sin6.sin6_addr) ||
-		     IN6_IS_ADDR_MC_NODELOCAL(&sin6.sin6_addr)) &&
-		    sin6.sin6_scope_id == 0) {
-			sin6.sin6_scope_id =
-			    ntohs(*(u_int16_t *)&sin6.sin6_addr.s6_addr[2]);
-			sin6.sin6_addr.s6_addr[2] = 0;
-			sin6.sin6_addr.s6_addr[3] = 0;
-		}
-#endif
 		if (nflag)
 			niflags |= NI_NUMERICHOST;
 		if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
@@ -498,18 +486,6 @@ netname(struct sockaddr *sa)
 		memcpy(&sin6, sa, sa->sa_len);
 		sin6.sin6_len = sizeof(struct sockaddr_in6);
 		sin6.sin6_family = AF_INET6;
-#ifdef __KAME__
-		if (sa->sa_len == sizeof(struct sockaddr_in6) &&
-		    (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr) ||
-		     IN6_IS_ADDR_MC_LINKLOCAL(&sin6.sin6_addr) ||
-		     IN6_IS_ADDR_MC_NODELOCAL(&sin6.sin6_addr)) &&
-		    sin6.sin6_scope_id == 0) {
-			sin6.sin6_scope_id =
-			    ntohs(*(u_int16_t *)&sin6.sin6_addr.s6_addr[2]);
-			sin6.sin6_addr.s6_addr[2] = 0;
-			sin6.sin6_addr.s6_addr[3] = 0;
-		}
-#endif
 		if (nflag)
 			niflags |= NI_NUMERICHOST;
 		if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
@@ -1002,16 +978,6 @@ getaddr(int which, char *str, struct hos
 			exit(1);
 		}
 		memcpy(&su->sin6, res->ai_addr, sizeof(su->sin6));
-#ifdef __KAME__
-		if ((IN6_IS_ADDR_LINKLOCAL(&su->sin6.sin6_addr) ||
-		     IN6_IS_ADDR_MC_LINKLOCAL(&su->sin6.sin6_addr) ||
-		     IN6_IS_ADDR_MC_NODELOCAL(&su->sin6.sin6_addr)) &&
-		    su->sin6.sin6_scope_id) {
-			*(u_int16_t *)&su->sin6.sin6_addr.s6_addr[2] =
-				htons(su->sin6.sin6_scope_id);
-			su->sin6.sin6_scope_id = 0;
-		}
-#endif
 		freeaddrinfo(res);
 		if (q != NULL)
 			*q++ = '/';

Modified: user/hrs/ipv6/sys/net/rtsock.c
==============================================================================
--- user/hrs/ipv6/sys/net/rtsock.c	Wed Apr 18 18:35:19 2012	(r234430)
+++ user/hrs/ipv6/sys/net/rtsock.c	Wed Apr 18 18:37:56 2012	(r234431)
@@ -175,6 +175,14 @@ MTX_SYSINIT(rtsock, &rtsock_mtx, "rtsock
 #define	RTSOCK_LOCK_ASSERT()	mtx_assert(&rtsock_mtx, MA_OWNED)
 
 static SYSCTL_NODE(_net, OID_AUTO, route, CTLFLAG_RD, 0, "");
+#ifdef INET6
+static VNET_DEFINE(int, deembed_scopeid) = 1;
+#define	V_deembed_scopeid	VNET(deembed_scopeid)
+SYSCTL_DECL(_net_inet6_ip6);
+SYSCTL_VNET_INT(_net_inet6_ip6, OID_AUTO, deembed_scopeid, CTLFLAG_RW,
+    &VNET_NAME(deembed_scopeid), 0,
+    "Extract embedded zone ID and set it to sin6_scope_id in sockaddr_in6.");
+#endif
 
 struct walkarg {
 	int	w_tmemsize;
@@ -573,6 +581,11 @@ route_output(struct mbuf *m, struct sock
 	struct rtentry *rt = NULL;
 	struct radix_node_head *rnh;
 	struct rt_addrinfo info;
+#ifdef INET6
+	struct sockaddr_storage ss_dst;
+	struct sockaddr_storage ss_gw;
+	struct sockaddr_in6 *sin6;
+#endif
 	int len, error = 0;
 	struct ifnet *ifp = NULL;
 	union sockaddr_union saun;
@@ -800,7 +813,31 @@ route_output(struct mbuf *m, struct sock
 				senderr(ESRCH);
 			}
 			info.rti_info[RTAX_DST] = rt_key(rt);
+#ifdef INET6
+			switch (rt_key(rt)->sa_family) {
+			case AF_INET6:
+				if (V_deembed_scopeid == 0)
+					break;
+				sin6 = (struct sockaddr_in6 *)&ss_dst;
+				bcopy(rt_key(rt), sin6, sizeof(*sin6));
+				if (sa6_recoverscope(sin6) == 0)
+					info.rti_info[RTAX_DST] =
+					    (struct sockaddr *)sin6;
+				break;
+			}
+#endif
 			info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
+#ifdef INET6
+			switch (rt->rt_gateway->sa_family) {
+			case AF_INET6:
+				sin6 = (struct sockaddr_in6 *)&ss_gw;
+				bcopy(rt->rt_gateway, sin6, sizeof(*sin6));
+				if (sa6_recoverscope(sin6) == 0)
+					info.rti_info[RTAX_GATEWAY] =
+					    (struct sockaddr *)sin6;
+				break;
+			}
+#endif
 			info.rti_info[RTAX_NETMASK] = rt_mask(rt);
 			info.rti_info[RTAX_GENMASK] = 0;
 			if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
@@ -1051,6 +1088,10 @@ rt_msg1(int type, struct rt_addrinfo *rt
 	struct mbuf *m;
 	int i;
 	struct sockaddr *sa;
+#ifdef INET6
+	struct sockaddr_storage ss;
+	struct sockaddr_in6 *sin6;
+#endif
 	int len, dlen;
 
 	switch (type) {
@@ -1098,6 +1139,18 @@ rt_msg1(int type, struct rt_addrinfo *rt
 			continue;
 		rtinfo->rti_addrs |= (1 << i);
 		dlen = SA_SIZE(sa);
+#ifdef INET6
+		switch (sa->sa_family) {
+		case AF_INET6:
+			if (V_deembed_scopeid == 0)
+				break;
+			sin6 = (struct sockaddr_in6 *)&ss;
+			bcopy(sa, sin6, sizeof(*sin6));
+			if (sa6_recoverscope(sin6) == 0)
+				sa = (struct sockaddr *)sin6;
+			break;
+		}
+#endif
 		m_copyback(m, len, dlen, (caddr_t)sa);
 		len += dlen;
 	}
@@ -1120,6 +1173,10 @@ rt_msg2(int type, struct rt_addrinfo *rt
 	int i;
 	int len, dlen, second_time = 0;
 	caddr_t cp0;
+#ifdef INET6
+	struct sockaddr_storage ss;
+	struct sockaddr_in6 *sin6;
+#endif
 
 	rtinfo->rti_addrs = 0;
 again:
@@ -1171,6 +1228,18 @@ again:
 			continue;
 		rtinfo->rti_addrs |= (1 << i);
 		dlen = SA_SIZE(sa);
+#ifdef INET6
+		switch (sa->sa_family) {
+		case AF_INET6:
+			if (V_deembed_scopeid == 0)
+				break;
+			sin6 = (struct sockaddr_in6 *)&ss;
+			bcopy(sa, sin6, sizeof(*sin6));
+			if (sa6_recoverscope(sin6) == 0)
+				sa = (struct sockaddr *)sin6;
+			break;
+		}
+#endif
 		if (cp) {
 			bcopy((caddr_t)sa, cp, (unsigned)dlen);
 			cp += dlen;
@@ -1511,6 +1580,11 @@ sysctl_dumpentry(struct radix_node *rn, 
 	struct rtentry *rt = (struct rtentry *)rn;
 	int error = 0, size;
 	struct rt_addrinfo info;
+#ifdef INET6
+	struct sockaddr_storage ss[RTAX_MAX];
+	struct sockaddr_in6 *sin6;
+#endif
+	int i;
 
 	if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
 		return 0;
@@ -1529,6 +1603,22 @@ sysctl_dumpentry(struct radix_node *rn, 
 		if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
 			info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr;
 	}
+#ifdef INET6
+	for (i = 0; i < RTAX_MAX; i++) {
+		if (info.rti_info[i] == NULL)
+			continue;
+		switch (info.rti_info[i]->sa_family) {
+		case AF_INET6:
+			if (V_deembed_scopeid == 0)
+				break;
+			sin6 = (struct sockaddr_in6 *)&ss[i];
+			bcopy(info.rti_info[i], sin6, sizeof(*sin6));
+			if (sa6_recoverscope(sin6) == 0)
+				info.rti_info[i] = (struct sockaddr *)sin6;
+			break;
+		}
+	}
+#endif
 	size = rt_msg2(RTM_GET, &info, NULL, w);
 	if (w->w_req && w->w_tmem) {
 		struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;

Modified: user/hrs/ipv6/usr.sbin/rpcbind/util.c
==============================================================================
--- user/hrs/ipv6/usr.sbin/rpcbind/util.c	Wed Apr 18 18:35:19 2012	(r234430)
+++ user/hrs/ipv6/usr.sbin/rpcbind/util.c	Wed Apr 18 18:37:56 2012	(r234431)
@@ -57,9 +57,6 @@ static struct sockaddr_in6 *local_in6;
 #endif
 
 static int bitmaskcmp(void *, void *, void *, int);
-#ifdef INET6
-static void in6_fillscopeid(struct sockaddr_in6 *);
-#endif
 
 /*
  * For all bits set in "mask", compare the corresponding bits in
@@ -79,26 +76,6 @@ bitmaskcmp(void *dst, void *src, void *m
 }
 
 /*
- * Similar to code in ifconfig.c. Fill in the scope ID for link-local
- * addresses returned by getifaddrs().
- */
-#ifdef INET6
-static void
-in6_fillscopeid(struct sockaddr_in6 *sin6)
-{
-	u_int16_t ifindex;
-
-        if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
-		ifindex = ntohs(*(u_int16_t *)&sin6->sin6_addr.s6_addr[2]);
-		if (sin6->sin6_scope_id == 0 && ifindex != 0) {
-			sin6->sin6_scope_id = ifindex;
-			*(u_int16_t *)&sin6->sin6_addr.s6_addr[2] = 0;
-		}
-	}
-}
-#endif
-
-/*
  * Find a server address that can be used by `caller' to contact
  * the local service specified by `serv_uaddr'. If `clnt_uaddr' is
  * non-NULL, it is used instead of `caller' as a hint suggesting
@@ -202,7 +179,6 @@ addrmerge(struct netbuf *caller, char *s
 			 * a link-local address then use the scope id to see
 			 * which one.
 			 */
-			in6_fillscopeid(SA2SIN6(ifsa));
 			if (IN6_IS_ADDR_LINKLOCAL(&SA2SIN6ADDR(ifsa)) &&
 			    IN6_IS_ADDR_LINKLOCAL(&SA2SIN6ADDR(caller_sa)) &&
 			    IN6_IS_ADDR_LINKLOCAL(&SA2SIN6ADDR(hint_sa))) {



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201204181837.q3IIbuc5045409>