summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2014-10-14 19:02:50 +0200
committerThomas Haller <thaller@redhat.com>2014-11-05 23:54:19 +0100
commitbe9ae5c124ac0e978e0a1ad60d1bdeaf60546603 (patch)
treec47b7a312fef04ce736f29ea1587764102fa3660
parent0979ce7ddb2c9d82477c4a99c08c992382df65d4 (diff)
downloadNetworkManager-be9ae5c124ac0e978e0a1ad60d1bdeaf60546603.tar.gz
platform: extend nm_platform_ipX_route_get_all() to return default-routes only
Add a new enum NMPlatformGetRouteMode. This extends the existing functions nm_platform_ip4_route_get_all() and nm_platform_ip6_route_get_all() to return default routes only. Signed-off-by: Thomas Haller <thaller@redhat.com>
-rw-r--r--src/nm-ip4-config.c2
-rw-r--r--src/nm-ip6-config.c2
-rw-r--r--src/platform/nm-fake-platform.c26
-rw-r--r--src/platform/nm-linux-platform.c40
-rw-r--r--src/platform/nm-platform.c12
-rw-r--r--src/platform/nm-platform.h14
-rw-r--r--src/platform/tests/dump.c4
-rw-r--r--src/platform/tests/platform.c4
-rw-r--r--src/platform/tests/test-cleanup.c8
-rw-r--r--src/platform/tests/test-route.c4
10 files changed, 80 insertions, 36 deletions
diff --git a/src/nm-ip4-config.c b/src/nm-ip4-config.c
index 83f24b7a76..371af35909 100644
--- a/src/nm-ip4-config.c
+++ b/src/nm-ip4-config.c
@@ -191,7 +191,7 @@ nm_ip4_config_capture (int ifindex, gboolean capture_resolv_conf)
g_array_unref (priv->routes);
priv->addresses = nm_platform_ip4_address_get_all (ifindex);
- priv->routes = nm_platform_ip4_route_get_all (ifindex, TRUE);
+ priv->routes = nm_platform_ip4_route_get_all (ifindex, NM_PLATFORM_GET_ROUTE_MODE_ALL);
/* Extract gateway from default route */
old_gateway = priv->gateway;
diff --git a/src/nm-ip6-config.c b/src/nm-ip6-config.c
index 76fe1c14c8..a0301b176b 100644
--- a/src/nm-ip6-config.c
+++ b/src/nm-ip6-config.c
@@ -302,7 +302,7 @@ nm_ip6_config_capture (int ifindex, gboolean capture_resolv_conf, NMSettingIP6Co
g_array_unref (priv->routes);
priv->addresses = nm_platform_ip6_address_get_all (ifindex);
- priv->routes = nm_platform_ip6_route_get_all (ifindex, TRUE);
+ priv->routes = nm_platform_ip6_route_get_all (ifindex, NM_PLATFORM_GET_ROUTE_MODE_ALL);
/* Extract gateway from default route */
old_gateway = priv->gateway;
diff --git a/src/platform/nm-fake-platform.c b/src/platform/nm-fake-platform.c
index ad82751e26..f397f5b731 100644
--- a/src/platform/nm-fake-platform.c
+++ b/src/platform/nm-fake-platform.c
@@ -983,13 +983,15 @@ ip6_address_exists (NMPlatform *platform, int ifindex, struct in6_addr addr, int
/******************************************************************/
static GArray *
-ip4_route_get_all (NMPlatform *platform, int ifindex, gboolean include_default)
+ip4_route_get_all (NMPlatform *platform, int ifindex, NMPlatformGetRouteMode mode)
{
NMFakePlatformPrivate *priv = NM_FAKE_PLATFORM_GET_PRIVATE (platform);
GArray *routes;
NMPlatformIP4Route *route;
int count = 0, i;
+ g_return_val_if_fail (NM_IN_SET (mode, NM_PLATFORM_GET_ROUTE_MODE_ALL, NM_PLATFORM_GET_ROUTE_MODE_NO_DEFAULT, NM_PLATFORM_GET_ROUTE_MODE_ONLY_DEFAULT), NULL);
+
/* Count routes */
for (i = 0; i < priv->ip4_routes->len; i++) {
route = &g_array_index (priv->ip4_routes, NMPlatformIP4Route, i);
@@ -1003,8 +1005,13 @@ ip4_route_get_all (NMPlatform *platform, int ifindex, gboolean include_default)
for (i = 0; i < priv->ip4_routes->len; i++) {
route = &g_array_index (priv->ip4_routes, NMPlatformIP4Route, i);
if (route && route->ifindex == ifindex) {
- if (route->plen != 0 || include_default)
- g_array_append_val (routes, *route);
+ if (NM_PLATFORM_IP_ROUTE_IS_DEFAULT (route)) {
+ if (mode != NM_PLATFORM_GET_ROUTE_MODE_NO_DEFAULT)
+ g_array_append_val (routes, *route);
+ } else {
+ if (mode != NM_PLATFORM_GET_ROUTE_MODE_ONLY_DEFAULT)
+ g_array_append_val (routes, *route);
+ }
}
}
@@ -1012,13 +1019,15 @@ ip4_route_get_all (NMPlatform *platform, int ifindex, gboolean include_default)
}
static GArray *
-ip6_route_get_all (NMPlatform *platform, int ifindex, gboolean include_default)
+ip6_route_get_all (NMPlatform *platform, int ifindex, NMPlatformGetRouteMode mode)
{
NMFakePlatformPrivate *priv = NM_FAKE_PLATFORM_GET_PRIVATE (platform);
GArray *routes;
NMPlatformIP6Route *route;
int count = 0, i;
+ g_return_val_if_fail (NM_IN_SET (mode, NM_PLATFORM_GET_ROUTE_MODE_ALL, NM_PLATFORM_GET_ROUTE_MODE_NO_DEFAULT, NM_PLATFORM_GET_ROUTE_MODE_ONLY_DEFAULT), NULL);
+
/* Count routes */
for (i = 0; i < priv->ip6_routes->len; i++) {
route = &g_array_index (priv->ip6_routes, NMPlatformIP6Route, i);
@@ -1032,8 +1041,13 @@ ip6_route_get_all (NMPlatform *platform, int ifindex, gboolean include_default)
for (i = 0; i < priv->ip6_routes->len; i++) {
route = &g_array_index (priv->ip6_routes, NMPlatformIP6Route, i);
if (route && route->ifindex == ifindex) {
- if (route->plen != 0 || include_default)
- g_array_append_val (routes, *route);
+ if (NM_PLATFORM_IP_ROUTE_IS_DEFAULT (route)) {
+ if (mode != NM_PLATFORM_GET_ROUTE_MODE_NO_DEFAULT)
+ g_array_append_val (routes, *route);
+ } else {
+ if (mode != NM_PLATFORM_GET_ROUTE_MODE_ONLY_DEFAULT)
+ g_array_append_val (routes, *route);
+ }
}
}
diff --git a/src/platform/nm-linux-platform.c b/src/platform/nm-linux-platform.c
index e3b12a432b..37eb5c7cce 100644
--- a/src/platform/nm-linux-platform.c
+++ b/src/platform/nm-linux-platform.c
@@ -1298,6 +1298,16 @@ rtprot_to_source (guint rtprot)
}
static gboolean
+_rtnl_route_is_default (const struct rtnl_route *rtnlroute)
+{
+ struct nl_addr *dst;
+
+ return rtnlroute
+ && (dst = rtnl_route_get_dst ((struct rtnl_route *) rtnlroute))
+ && nl_addr_get_prefixlen (dst) == 0;
+}
+
+static gboolean
init_ip4_route (NMPlatformIP4Route *route, struct rtnl_route *rtnlroute)
{
struct nl_addr *dst, *gw;
@@ -3647,21 +3657,28 @@ _route_match (struct rtnl_route *rtnlroute, int family, int ifindex)
}
static GArray *
-ip4_route_get_all (NMPlatform *platform, int ifindex, gboolean include_default)
+ip4_route_get_all (NMPlatform *platform, int ifindex, NMPlatformGetRouteMode mode)
{
NMLinuxPlatformPrivate *priv = NM_LINUX_PLATFORM_GET_PRIVATE (platform);
GArray *routes;
NMPlatformIP4Route route;
struct nl_object *object;
+ g_return_val_if_fail (NM_IN_SET (mode, NM_PLATFORM_GET_ROUTE_MODE_ALL, NM_PLATFORM_GET_ROUTE_MODE_NO_DEFAULT, NM_PLATFORM_GET_ROUTE_MODE_ONLY_DEFAULT), NULL);
+
routes = g_array_new (FALSE, FALSE, sizeof (NMPlatformIP4Route));
for (object = nl_cache_get_first (priv->route_cache); object; object = nl_cache_get_next (object)) {
if (_route_match ((struct rtnl_route *) object, AF_INET, ifindex)) {
- if (init_ip4_route (&route, (struct rtnl_route *) object)) {
- if (!NM_PLATFORM_IP_ROUTE_IS_DEFAULT (&route) || include_default)
- g_array_append_val (routes, route);
+ if (_rtnl_route_is_default ((struct rtnl_route *) object)) {
+ if (mode == NM_PLATFORM_GET_ROUTE_MODE_NO_DEFAULT)
+ continue;
+ } else {
+ if (mode == NM_PLATFORM_GET_ROUTE_MODE_ONLY_DEFAULT)
+ continue;
}
+ if (init_ip4_route (&route, (struct rtnl_route *) object))
+ g_array_append_val (routes, route);
}
}
@@ -3669,21 +3686,28 @@ ip4_route_get_all (NMPlatform *platform, int ifindex, gboolean include_default)
}
static GArray *
-ip6_route_get_all (NMPlatform *platform, int ifindex, gboolean include_default)
+ip6_route_get_all (NMPlatform *platform, int ifindex, NMPlatformGetRouteMode mode)
{
NMLinuxPlatformPrivate *priv = NM_LINUX_PLATFORM_GET_PRIVATE (platform);
GArray *routes;
NMPlatformIP6Route route;
struct nl_object *object;
+ g_return_val_if_fail (NM_IN_SET (mode, NM_PLATFORM_GET_ROUTE_MODE_ALL, NM_PLATFORM_GET_ROUTE_MODE_NO_DEFAULT, NM_PLATFORM_GET_ROUTE_MODE_ONLY_DEFAULT), NULL);
+
routes = g_array_new (FALSE, FALSE, sizeof (NMPlatformIP6Route));
for (object = nl_cache_get_first (priv->route_cache); object; object = nl_cache_get_next (object)) {
if (_route_match ((struct rtnl_route *) object, AF_INET6, ifindex)) {
- if (init_ip6_route (&route, (struct rtnl_route *) object)) {
- if (!NM_PLATFORM_IP_ROUTE_IS_DEFAULT (&route) || include_default)
- g_array_append_val (routes, route);
+ if (_rtnl_route_is_default ((struct rtnl_route *) object)) {
+ if (mode == NM_PLATFORM_GET_ROUTE_MODE_NO_DEFAULT)
+ continue;
+ } else {
+ if (mode == NM_PLATFORM_GET_ROUTE_MODE_ONLY_DEFAULT)
+ continue;
}
+ if (init_ip6_route (&route, (struct rtnl_route *) object))
+ g_array_append_val (routes, route);
}
}
diff --git a/src/platform/nm-platform.c b/src/platform/nm-platform.c
index 09ec69cde6..70395ca3cf 100644
--- a/src/platform/nm-platform.c
+++ b/src/platform/nm-platform.c
@@ -1837,25 +1837,25 @@ nm_platform_address_flush (int ifindex)
/******************************************************************/
GArray *
-nm_platform_ip4_route_get_all (int ifindex, gboolean include_default)
+nm_platform_ip4_route_get_all (int ifindex, NMPlatformGetRouteMode mode)
{
reset_error ();
g_return_val_if_fail (ifindex > 0, NULL);
g_return_val_if_fail (klass->ip4_route_get_all, NULL);
- return klass->ip4_route_get_all (platform, ifindex, include_default);
+ return klass->ip4_route_get_all (platform, ifindex, mode);
}
GArray *
-nm_platform_ip6_route_get_all (int ifindex, gboolean include_default)
+nm_platform_ip6_route_get_all (int ifindex, NMPlatformGetRouteMode mode)
{
reset_error ();
g_return_val_if_fail (ifindex > 0, NULL);
g_return_val_if_fail (klass->ip6_route_get_all, NULL);
- return klass->ip6_route_get_all (platform, ifindex, include_default);
+ return klass->ip6_route_get_all (platform, ifindex, mode);
}
gboolean
@@ -2026,7 +2026,7 @@ nm_platform_ip4_route_sync (int ifindex, const GArray *known_routes)
int i, i_type;
/* Delete unknown routes */
- routes = nm_platform_ip4_route_get_all (ifindex, FALSE);
+ routes = nm_platform_ip4_route_get_all (ifindex, NM_PLATFORM_GET_ROUTE_MODE_NO_DEFAULT);
for (i = 0; i < routes->len; i++) {
route = &g_array_index (routes, NMPlatformIP4Route, i);
@@ -2099,7 +2099,7 @@ nm_platform_ip6_route_sync (int ifindex, const GArray *known_routes)
int i, i_type;
/* Delete unknown routes */
- routes = nm_platform_ip6_route_get_all (ifindex, FALSE);
+ routes = nm_platform_ip6_route_get_all (ifindex, NM_PLATFORM_GET_ROUTE_MODE_NO_DEFAULT);
for (i = 0; i < routes->len; i++) {
route = &g_array_index (routes, NMPlatformIP6Route, i);
route->ifindex = 0;
diff --git a/src/platform/nm-platform.h b/src/platform/nm-platform.h
index f3970f7978..3514dd084d 100644
--- a/src/platform/nm-platform.h
+++ b/src/platform/nm-platform.h
@@ -99,6 +99,12 @@ typedef enum {
#define NM_PLATFORM_LIFETIME_PERMANENT G_MAXUINT32
+typedef enum {
+ NM_PLATFORM_GET_ROUTE_MODE_ALL,
+ NM_PLATFORM_GET_ROUTE_MODE_NO_DEFAULT,
+ NM_PLATFORM_GET_ROUTE_MODE_ONLY_DEFAULT,
+} NMPlatformGetRouteMode;
+
typedef struct {
__NMPlatformObject_COMMON;
} NMPlatformObject;
@@ -395,8 +401,8 @@ typedef struct {
gboolean (*ip4_address_exists) (NMPlatform *, int ifindex, in_addr_t address, int plen);
gboolean (*ip6_address_exists) (NMPlatform *, int ifindex, struct in6_addr address, int plen);
- GArray * (*ip4_route_get_all) (NMPlatform *, int ifindex, gboolean include_default);
- GArray * (*ip6_route_get_all) (NMPlatform *, int ifindex, gboolean include_default);
+ GArray * (*ip4_route_get_all) (NMPlatform *, int ifindex, NMPlatformGetRouteMode mode);
+ GArray * (*ip6_route_get_all) (NMPlatform *, int ifindex, NMPlatformGetRouteMode mode);
gboolean (*ip4_route_add) (NMPlatform *, int ifindex, NMIPConfigSource source,
in_addr_t network, int plen, in_addr_t gateway,
guint32 metric, guint32 mss);
@@ -542,8 +548,8 @@ gboolean nm_platform_ip4_address_sync (int ifindex, const GArray *known_addresse
gboolean nm_platform_ip6_address_sync (int ifindex, const GArray *known_addresses);
gboolean nm_platform_address_flush (int ifindex);
-GArray *nm_platform_ip4_route_get_all (int ifindex, gboolean include_default);
-GArray *nm_platform_ip6_route_get_all (int ifindex, gboolean include_default);
+GArray *nm_platform_ip4_route_get_all (int ifindex, NMPlatformGetRouteMode mode);
+GArray *nm_platform_ip6_route_get_all (int ifindex, NMPlatformGetRouteMode mode);
gboolean nm_platform_ip4_route_add (int ifindex, NMIPConfigSource source,
in_addr_t network, int plen, in_addr_t gateway,
guint32 metric, guint32 mss);
diff --git a/src/platform/tests/dump.c b/src/platform/tests/dump.c
index e97ef138cb..8aea3aec59 100644
--- a/src/platform/tests/dump.c
+++ b/src/platform/tests/dump.c
@@ -83,8 +83,8 @@ dump_interface (NMPlatformLink *link)
g_array_unref (ip4_addresses);
g_array_unref (ip6_addresses);
- ip4_routes = nm_platform_ip4_route_get_all (link->ifindex, TRUE);
- ip6_routes = nm_platform_ip6_route_get_all (link->ifindex, TRUE);
+ ip4_routes = nm_platform_ip4_route_get_all (link->ifindex, NM_PLATFORM_GET_ROUTE_MODE_ALL);
+ ip6_routes = nm_platform_ip6_route_get_all (link->ifindex, NM_PLATFORM_GET_ROUTE_MODE_ALL);
g_assert (ip4_routes);
g_assert (ip6_routes);
diff --git a/src/platform/tests/platform.c b/src/platform/tests/platform.c
index 90513d4579..a70d43601d 100644
--- a/src/platform/tests/platform.c
+++ b/src/platform/tests/platform.c
@@ -631,7 +631,7 @@ do_ip4_route_get_all (char **argv)
int i;
if (ifindex) {
- routes = nm_platform_ip4_route_get_all (ifindex, TRUE);
+ routes = nm_platform_ip4_route_get_all (ifindex, NM_PLATFORM_GET_ROUTE_MODE_ALL);
for (i = 0; i < routes->len; i++) {
route = &g_array_index (routes, NMPlatformIP4Route, i);
inet_ntop (AF_INET, &route->network, networkstr, sizeof (networkstr));
@@ -655,7 +655,7 @@ do_ip6_route_get_all (char **argv)
int i;
if (ifindex) {
- routes = nm_platform_ip6_route_get_all (ifindex, TRUE);
+ routes = nm_platform_ip6_route_get_all (ifindex, NM_PLATFORM_GET_ROUTE_MODE_ALL);
for (i = 0; i < routes->len; i++) {
route = &g_array_index (routes, NMPlatformIP6Route, i);
inet_ntop (AF_INET6, &route->network, networkstr, sizeof (networkstr));
diff --git a/src/platform/tests/test-cleanup.c b/src/platform/tests/test-cleanup.c
index 646fb8a8db..6bf7f12396 100644
--- a/src/platform/tests/test-cleanup.c
+++ b/src/platform/tests/test-cleanup.c
@@ -52,8 +52,8 @@ test_cleanup_internal (void)
addresses4 = nm_platform_ip4_address_get_all (ifindex);
addresses6 = nm_platform_ip6_address_get_all (ifindex);
- routes4 = nm_platform_ip4_route_get_all (ifindex, TRUE);
- routes6 = nm_platform_ip6_route_get_all (ifindex, TRUE);
+ routes4 = nm_platform_ip4_route_get_all (ifindex, NM_PLATFORM_GET_ROUTE_MODE_ALL);
+ routes6 = nm_platform_ip6_route_get_all (ifindex, NM_PLATFORM_GET_ROUTE_MODE_ALL);
g_assert_cmpint (addresses4->len, ==, 1);
g_assert_cmpint (addresses6->len, ==, 1);
@@ -70,8 +70,8 @@ test_cleanup_internal (void)
addresses4 = nm_platform_ip4_address_get_all (ifindex);
addresses6 = nm_platform_ip6_address_get_all (ifindex);
- routes4 = nm_platform_ip4_route_get_all (ifindex, TRUE);
- routes6 = nm_platform_ip6_route_get_all (ifindex, TRUE);
+ routes4 = nm_platform_ip4_route_get_all (ifindex, NM_PLATFORM_GET_ROUTE_MODE_ALL);
+ routes6 = nm_platform_ip6_route_get_all (ifindex, NM_PLATFORM_GET_ROUTE_MODE_ALL);
g_assert_cmpint (addresses4->len, ==, 0);
g_assert_cmpint (addresses6->len, ==, 0);
diff --git a/src/platform/tests/test-route.c b/src/platform/tests/test-route.c
index 44598f4c78..299eddda95 100644
--- a/src/platform/tests/test-route.c
+++ b/src/platform/tests/test-route.c
@@ -99,7 +99,7 @@ test_ip4_route (void)
accept_signal (route_changed);
/* Test route listing */
- routes = nm_platform_ip4_route_get_all (ifindex, TRUE);
+ routes = nm_platform_ip4_route_get_all (ifindex, NM_PLATFORM_GET_ROUTE_MODE_ALL);
memset (rts, 0, sizeof (rts));
rts[0].source = NM_IP_CONFIG_SOURCE_USER;
rts[0].network = gateway;
@@ -194,7 +194,7 @@ test_ip6_route (void)
accept_signal (route_changed);
/* Test route listing */
- routes = nm_platform_ip6_route_get_all (ifindex, TRUE);
+ routes = nm_platform_ip6_route_get_all (ifindex, NM_PLATFORM_GET_ROUTE_MODE_ALL);
memset (rts, 0, sizeof (rts));
rts[0].source = NM_IP_CONFIG_SOURCE_USER;
rts[0].network = gateway;