summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2015-05-03 10:47:41 +0200
committerThomas Haller <thaller@redhat.com>2015-05-06 12:33:27 +0200
commitb29cfa09b16de7eac254bd6654fe6ac86492db99 (patch)
treed7d48577ff75c384e673470814d18fb6a55b38b1
parent61d08dafd1a1ac6f260f7f79fbcb2637822bfd7c (diff)
downloadNetworkManager-b29cfa09b16de7eac254bd6654fe6ac86492db99.tar.gz
platform: add scope parameter to NMPlatformIP4Route
We will later need the scope for NMPlatformIP4Route because for route deletion with old netlink library, we must explicitly set the scope of the route we want to delete.
-rw-r--r--src/platform/nm-fake-platform.c6
-rw-r--r--src/platform/nm-linux-platform.c3
-rw-r--r--src/platform/nm-platform-utils.h36
-rw-r--r--src/platform/nm-platform.c10
-rw-r--r--src/platform/nm-platform.h4
-rw-r--r--src/platform/tests/test-general.c30
-rw-r--r--src/platform/tests/test-route.c8
-rw-r--r--src/tests/Makefile.am2
-rw-r--r--src/tests/test-route-manager.c10
9 files changed, 104 insertions, 5 deletions
diff --git a/src/platform/nm-fake-platform.c b/src/platform/nm-fake-platform.c
index 0da6590ffb..47fbd5a540 100644
--- a/src/platform/nm-fake-platform.c
+++ b/src/platform/nm-fake-platform.c
@@ -24,10 +24,12 @@
#include <unistd.h>
#include <netinet/icmp6.h>
#include <netinet/in.h>
+#include <linux/rtnetlink.h>
#include "gsystem-local-alloc.h"
#include "NetworkManagerUtils.h"
#include "nm-fake-platform.h"
+#include "nm-platform-utils.h"
#include "nm-logging.h"
#define debug(format, ...) nm_log_dbg (LOGD_PLATFORM, format, __VA_ARGS__)
@@ -1114,6 +1116,9 @@ ip4_route_add (NMPlatform *platform, int ifindex, NMIPConfigSource source,
NMFakePlatformPrivate *priv = NM_FAKE_PLATFORM_GET_PRIVATE (platform);
NMPlatformIP4Route route;
guint i;
+ guint8 scope;
+
+ scope = gateway == 0 ? RT_SCOPE_LINK : RT_SCOPE_UNIVERSE;
memset (&route, 0, sizeof (route));
route.source = NM_IP_CONFIG_SOURCE_KERNEL;
@@ -1124,6 +1129,7 @@ ip4_route_add (NMPlatform *platform, int ifindex, NMIPConfigSource source,
route.gateway = gateway;
route.metric = metric;
route.mss = mss;
+ route.scope_nm = nmp_utils_ip_route_scope_native_to_nm (scope);
if (gateway) {
for (i = 0; i < priv->ip4_routes->len; i++) {
diff --git a/src/platform/nm-linux-platform.c b/src/platform/nm-linux-platform.c
index 0b4d29e7fd..7ae6e9c9be 100644
--- a/src/platform/nm-linux-platform.c
+++ b/src/platform/nm-linux-platform.c
@@ -1283,6 +1283,7 @@ init_ip4_route (NMPlatformIP4Route *route, struct rtnl_route *rtnlroute)
route->metric = rtnl_route_get_priority (rtnlroute);
rtnl_route_get_metric (rtnlroute, RTAX_ADVMSS, &route->mss);
route->source = rtprot_to_source (rtnl_route_get_protocol (rtnlroute));
+ route->scope_nm = nmp_utils_ip_route_scope_native_to_nm (rtnl_route_get_scope (rtnlroute));
return TRUE;
}
@@ -3473,7 +3474,7 @@ build_rtnl_addr (NMPlatform *platform,
/* Tighten scope (IPv4 only) */
if (family == AF_INET && ip4_is_link_local (addr))
- rtnl_addr_set_scope (rtnladdr, rtnl_str2scope ("link"));
+ rtnl_addr_set_scope (rtnladdr, RT_SCOPE_LINK);
/* IPv4 Broadcast address */
if (family == AF_INET) {
diff --git a/src/platform/nm-platform-utils.h b/src/platform/nm-platform-utils.h
index d021113a52..396ab9bd86 100644
--- a/src/platform/nm-platform-utils.h
+++ b/src/platform/nm-platform-utils.h
@@ -28,6 +28,42 @@
#include "nm-platform.h"
+/**
+ * nmp_utils_ip_route_scope_native_to_nm:
+ * @scope_native: the scope field of the route in native representation
+ *
+ * The scope field of an IP address defaults to non-zero value
+ * RT_SCOPE_NOWHERE. NMPlatformIP4Route doesn't have a constructor
+ * and we initialize the struct with memset(). Hence the default value
+ * of the @scope_nm field is zero.
+ *
+ * That is the reason for the @scope_nm field having a different meaning
+ * then the scope in kernel/netlink. This function maps between those
+ * two.
+ *
+ * Returns: @scope in NM representation.
+ */
+static inline guint8
+nmp_utils_ip_route_scope_native_to_nm (guint8 scope_native)
+{
+ return ~scope_native;
+}
+
+/**
+ * nmp_utils_ip_route_scope_nm_to_native:
+ * @scope_nm: the scope field of the route in NM representation
+ *
+ * The inverse of nmp_utils_ip_route_scope_native_to_nm.
+ *
+ * Returns: @scope in native representation.
+ */
+static inline guint8
+nmp_utils_ip_route_scope_nm_to_native (guint8 scope_nm)
+{
+ return ~scope_nm;
+}
+
+
const char *nmp_utils_ethtool_get_driver (const char *ifname);
gboolean nmp_utils_ethtool_supports_carrier_detect (const char *ifname);
gboolean nmp_utils_ethtool_supports_vlans (const char *ifname);
diff --git a/src/platform/nm-platform.c b/src/platform/nm-platform.c
index f4a457ed09..659942ca6b 100644
--- a/src/platform/nm-platform.c
+++ b/src/platform/nm-platform.c
@@ -27,11 +27,13 @@
#include <arpa/inet.h>
#include <string.h>
#include <netlink/route/addr.h>
+#include <netlink/route/rtnl.h>
#include "gsystem-local-alloc.h"
#include "NetworkManagerUtils.h"
#include "nm-utils.h"
#include "nm-platform.h"
+#include "nm-platform-utils.h"
#include "NetworkManagerUtils.h"
#include "nm-logging.h"
#include "nm-enum-types.h"
@@ -2521,6 +2523,7 @@ nm_platform_ip4_route_to_string (const NMPlatformIP4Route *route)
{
char s_network[INET_ADDRSTRLEN], s_gateway[INET_ADDRSTRLEN];
char str_dev[TO_STRING_DEV_BUF_SIZE];
+ char str_scope[30];
g_return_val_if_fail (route, "(unknown)");
@@ -2529,11 +2532,13 @@ nm_platform_ip4_route_to_string (const NMPlatformIP4Route *route)
_to_string_dev (NULL, route->ifindex, str_dev, sizeof (str_dev));
- g_snprintf (to_string_buffer, sizeof (to_string_buffer), "%s/%d via %s%s metric %"G_GUINT32_FORMAT" mss %"G_GUINT32_FORMAT" src %s",
+ g_snprintf (to_string_buffer, sizeof (to_string_buffer), "%s/%d via %s%s metric %"G_GUINT32_FORMAT" mss %"G_GUINT32_FORMAT" src %s%s%s",
s_network, route->plen, s_gateway,
str_dev,
route->metric, route->mss,
- source_to_string (route->source));
+ source_to_string (route->source),
+ route->scope_nm ? " scope " : "",
+ route->scope_nm ? (rtnl_scope2str (nmp_utils_ip_route_scope_nm_to_native (route->scope_nm), str_scope, sizeof (str_scope))) : "");
return to_string_buffer;
}
@@ -2687,6 +2692,7 @@ nm_platform_ip4_route_cmp (const NMPlatformIP4Route *a, const NMPlatformIP4Route
_CMP_FIELD (a, b, gateway);
_CMP_FIELD (a, b, metric);
_CMP_FIELD (a, b, mss);
+ _CMP_FIELD (a, b, scope_nm);
return 0;
}
diff --git a/src/platform/nm-platform.h b/src/platform/nm-platform.h
index 4f16aba5df..423a9ecd51 100644
--- a/src/platform/nm-platform.h
+++ b/src/platform/nm-platform.h
@@ -237,6 +237,10 @@ struct _NMPlatformIP4Route {
__NMPlatformIPRoute_COMMON;
in_addr_t network;
in_addr_t gateway;
+
+ /* The route scope, but converted to NM representation.
+ * See nmp_utils_ip_route_scope_native_to_nm(). */
+ guint8 scope_nm;
};
G_STATIC_ASSERT (G_STRUCT_OFFSET (NMPlatformIPRoute, network_ptr) == G_STRUCT_OFFSET (NMPlatformIP4Route, network));
diff --git a/src/platform/tests/test-general.c b/src/platform/tests/test-general.c
index 2f29d7de70..195ac93de9 100644
--- a/src/platform/tests/test-general.c
+++ b/src/platform/tests/test-general.c
@@ -20,6 +20,8 @@
#include "nm-platform-utils.h"
+#include <linux/rtnetlink.h>
+
#include "nm-logging.h"
#include "nm-test-utils.h"
@@ -27,6 +29,32 @@
/******************************************************************/
+static void
+test_nmp_utils_ip_route_scope_native_to_nm ()
+{
+ int i;
+
+ /* see also /etc/iproute2/rt_scopes */
+ G_STATIC_ASSERT (((guint8) RT_SCOPE_LINK) == 253);
+ G_STATIC_ASSERT (((guint8) RT_SCOPE_NOWHERE) == 255);
+
+ for (i = 0; i <= 255; i++) {
+ guint8 scope = i;
+
+ g_assert_cmpint ((int) scope, ==, i);
+
+ g_assert_cmpint (nmp_utils_ip_route_scope_nm_to_native (nmp_utils_ip_route_scope_native_to_nm (scope)), ==, scope);
+ g_assert_cmpint (nmp_utils_ip_route_scope_native_to_nm (nmp_utils_ip_route_scope_nm_to_native (scope)), ==, scope);
+
+ if (scope == 0)
+ g_assert_cmpint (scope, ==, nmp_utils_ip_route_scope_native_to_nm (RT_SCOPE_NOWHERE));
+ else if (scope == RT_SCOPE_NOWHERE)
+ g_assert_cmpint (scope, ==, nmp_utils_ip_route_scope_nm_to_native (0));
+ }
+}
+
+/******************************************************************/
+
NMTST_DEFINE ();
int
@@ -34,5 +62,7 @@ main (int argc, char **argv)
{
nmtst_init_assert_logging (&argc, &argv, "INFO", "DEFAULT");
+ g_test_add_func ("/general/nmp_utils_ip_route_scope_native_to_nm", test_nmp_utils_ip_route_scope_native_to_nm);
+
return g_test_run ();
}
diff --git a/src/platform/tests/test-route.c b/src/platform/tests/test-route.c
index 6296dc89bb..7d2f577815 100644
--- a/src/platform/tests/test-route.c
+++ b/src/platform/tests/test-route.c
@@ -1,8 +1,11 @@
#include "config.h"
+#include <linux/rtnetlink.h>
+
#include "test-common.h"
#include "nm-test-utils.h"
#include "NetworkManagerUtils.h"
+#include "nm-platform-utils.h"
#define DEVICE_NAME "nm-test-device"
@@ -177,6 +180,7 @@ test_ip4_route (void)
rts[0].gateway = INADDR_ANY;
rts[0].metric = metric;
rts[0].mss = mss;
+ rts[0].scope_nm = nmp_utils_ip_route_scope_native_to_nm (RT_SCOPE_LINK);
rts[1].source = NM_IP_CONFIG_SOURCE_USER;
rts[1].network = network;
rts[1].plen = plen;
@@ -184,6 +188,7 @@ test_ip4_route (void)
rts[1].gateway = gateway;
rts[1].metric = metric;
rts[1].mss = mss;
+ rts[1].scope_nm = nmp_utils_ip_route_scope_native_to_nm (RT_SCOPE_UNIVERSE);
rts[2].source = NM_IP_CONFIG_SOURCE_USER;
rts[2].network = 0;
rts[2].plen = 0;
@@ -191,8 +196,8 @@ test_ip4_route (void)
rts[2].gateway = gateway;
rts[2].metric = metric;
rts[2].mss = mss;
+ rts[2].scope_nm = nmp_utils_ip_route_scope_native_to_nm (RT_SCOPE_UNIVERSE);
g_assert_cmpint (routes->len, ==, 3);
- g_assert (!memcmp (routes->data, rts, sizeof (rts)));
nmtst_platform_ip4_routes_equal ((NMPlatformIP4Route *) routes->data, rts, routes->len, TRUE);
g_array_unref (routes);
@@ -288,7 +293,6 @@ test_ip6_route (void)
rts[2].metric = nm_utils_ip6_route_metric_normalize (metric);
rts[2].mss = mss;
g_assert_cmpint (routes->len, ==, 3);
- g_assert (!memcmp (routes->data, rts, sizeof (rts)));
nmtst_platform_ip6_routes_equal ((NMPlatformIP6Route *) routes->data, rts, routes->len, TRUE);
g_array_unref (routes);
diff --git a/src/tests/Makefile.am b/src/tests/Makefile.am
index 83632bd198..10f1f1ef96 100644
--- a/src/tests/Makefile.am
+++ b/src/tests/Makefile.am
@@ -46,6 +46,7 @@ test_ip6_config_LDADD = \
test_route_manager_fake_CPPFLAGS = \
$(AM_CPPFLAGS) \
+ $(GUDEV_CFLAGS) \
-I$(top_srcdir)/src/platform/tests \
-DSETUP=nm_fake_platform_setup \
-DKERNEL_HACKS=0
@@ -63,6 +64,7 @@ test_route_manager_linux_SOURCES = \
test_route_manager_linux_CPPFLAGS = \
$(AM_CPPFLAGS) \
+ $(GUDEV_CFLAGS) \
-I$(top_srcdir)/src/platform/tests \
-DSETUP=nm_linux_platform_setup \
-DKERNEL_HACKS=1
diff --git a/src/tests/test-route-manager.c b/src/tests/test-route-manager.c
index 260a0791b9..1bae93e137 100644
--- a/src/tests/test-route-manager.c
+++ b/src/tests/test-route-manager.c
@@ -22,10 +22,12 @@
#include <glib.h>
#include <arpa/inet.h>
+#include <linux/rtnetlink.h>
#include "test-common.h"
#include "nm-platform.h"
+#include "nm-platform-utils.h"
#include "nm-route-manager.h"
#include "nm-logging.h"
@@ -168,6 +170,7 @@ test_ip4 (test_fixture *fixture, gconstpointer user_data)
.gateway = INADDR_ANY,
.metric = 20,
.mss = 1000,
+ .scope_nm = nmp_utils_ip_route_scope_native_to_nm (RT_SCOPE_LINK),
},
{
.source = NM_IP_CONFIG_SOURCE_USER,
@@ -177,6 +180,7 @@ test_ip4 (test_fixture *fixture, gconstpointer user_data)
.gateway = nmtst_inet4_from_string ("6.6.6.1"),
.metric = 21021,
.mss = 0,
+ .scope_nm = nmp_utils_ip_route_scope_native_to_nm (RT_SCOPE_UNIVERSE),
},
{
.source = NM_IP_CONFIG_SOURCE_USER,
@@ -186,6 +190,7 @@ test_ip4 (test_fixture *fixture, gconstpointer user_data)
.gateway = INADDR_ANY,
.metric = 22,
.mss = 0,
+ .scope_nm = nmp_utils_ip_route_scope_native_to_nm (RT_SCOPE_LINK),
},
};
@@ -198,6 +203,7 @@ test_ip4 (test_fixture *fixture, gconstpointer user_data)
.gateway = INADDR_ANY,
.metric = 20,
.mss = 0,
+ .scope_nm = nmp_utils_ip_route_scope_native_to_nm (RT_SCOPE_LINK),
},
{
.source = NM_IP_CONFIG_SOURCE_USER,
@@ -207,6 +213,7 @@ test_ip4 (test_fixture *fixture, gconstpointer user_data)
.gateway = INADDR_ANY,
.metric = 21,
.mss = 0,
+ .scope_nm = nmp_utils_ip_route_scope_native_to_nm (RT_SCOPE_LINK),
},
{
.source = NM_IP_CONFIG_SOURCE_USER,
@@ -216,6 +223,7 @@ test_ip4 (test_fixture *fixture, gconstpointer user_data)
.gateway = INADDR_ANY,
.metric = 22,
.mss = 0,
+ .scope_nm = nmp_utils_ip_route_scope_native_to_nm (RT_SCOPE_LINK),
},
};
@@ -228,6 +236,7 @@ test_ip4 (test_fixture *fixture, gconstpointer user_data)
.gateway = INADDR_ANY,
.metric = 22,
.mss = 0,
+ .scope_nm = nmp_utils_ip_route_scope_native_to_nm (RT_SCOPE_LINK),
},
{
.source = NM_IP_CONFIG_SOURCE_USER,
@@ -237,6 +246,7 @@ test_ip4 (test_fixture *fixture, gconstpointer user_data)
.gateway = INADDR_ANY,
.metric = 20,
.mss = 0,
+ .scope_nm = nmp_utils_ip_route_scope_native_to_nm (RT_SCOPE_LINK),
},
};