summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2022-07-22 20:28:35 +0200
committerThomas Haller <thaller@redhat.com>2022-07-26 12:28:05 +0200
commite25b7a579e8ca9fb6f7764f898481af37f11ab33 (patch)
treef8349ccccbca232dec2036d1011d790e699a5d25
parentdc98ab807ca89cfec9a6ec8b441088a4bed30c8f (diff)
platform: add nm_platform_ip{4,6,}_address_get_scope() helper
-rw-r--r--src/libnm-platform/nm-platform.h31
1 files changed, 31 insertions, 0 deletions
diff --git a/src/libnm-platform/nm-platform.h b/src/libnm-platform/nm-platform.h
index 121886707d..26fffe23ac 100644
--- a/src/libnm-platform/nm-platform.h
+++ b/src/libnm-platform/nm-platform.h
@@ -1549,6 +1549,37 @@ nm_platform_route_type_uncoerce(guint8 type_coerced)
return nm_platform_route_type_coerce(type_coerced);
}
+static inline guint8
+nm_platform_ip4_address_get_scope(in_addr_t addr)
+{
+ /* For IPv4 addresses, we can set any scope we want (for any address).
+ * However, there are scopes that make sense based on the address,
+ * so choose those. */
+ return nm_utils_ip4_address_is_loopback(addr) ? (254 /* RT_SCOPE_HOST */)
+ : nm_utils_ip4_address_is_link_local(addr) ? (253 /* RT_SCOPE_LINK */)
+ : (0 /* RT_SCOPE_UNIVERSE */);
+}
+
+static inline guint8
+nm_platform_ip6_address_get_scope(const struct in6_addr *addr)
+{
+ /* For IPv6, kernel does not allow userspace to configure the address scope.
+ * Instead, it is calculated based on the address. See rt_scope() and
+ * ipv6_addr_scope(). We do the same here. */
+ return IN6_IS_ADDR_LOOPBACK(addr) ? (254 /* RT_SCOPE_HOST */)
+ : IN6_IS_ADDR_LINKLOCAL(addr) ? (253 /* RT_SCOPE_LINK */)
+ : IN6_IS_ADDR_SITELOCAL(addr) ? (200 /* RT_SCOPE_SITE */)
+ : (0 /* RT_SCOPE_UNIVERSE */);
+}
+
+static inline guint8
+nm_platform_ip_address_get_scope(int addr_family, gconstpointer addr)
+{
+ if (NM_IS_IPv4(addr_family))
+ return nm_platform_ip4_address_get_scope(*((in_addr_t *) addr));
+ return nm_platform_ip6_address_get_scope(addr);
+}
+
gboolean nm_platform_get_use_udev(NMPlatform *self);
gboolean nm_platform_get_log_with_ptr(NMPlatform *self);
gboolean nm_platform_get_cache_tc(NMPlatform *self);