From 57df4ce5ed4516d7082b9fd6e8a5bd65d0ab4746 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20=C5=A0imerda?= Date: Mon, 4 Mar 2013 11:53:11 +0100 Subject: [PATCH] core: use nm-platform for route management --- src/devices/nm-device.c | 5 +- src/modem-manager/nm-modem.c | 2 +- src/nm-system.c | 137 +++++++++------------------- src/vpn-manager/nm-vpn-connection.c | 2 +- 4 files changed, 46 insertions(+), 100 deletions(-) diff --git a/src/devices/nm-device.c b/src/devices/nm-device.c index 3eaef05e53..9cd2271620 100644 --- a/src/devices/nm-device.c +++ b/src/devices/nm-device.c @@ -4034,7 +4034,7 @@ nm_device_deactivate (NMDevice *self, NMDeviceStateReason reason) NMConnection *connection = NULL; NMSettingConnection *s_con = NULL; gboolean tried_ipv6 = FALSE; - int ifindex, family; + int ifindex; g_return_if_fail (NM_IS_DEVICE (self)); @@ -4103,9 +4103,8 @@ nm_device_deactivate (NMDevice *self, NMDeviceStateReason reason) /* Take out any entries in the routing table and any IP address the device had. */ ifindex = nm_device_get_ip_ifindex (self); - family = tried_ipv6 ? AF_UNSPEC : AF_INET; if (ifindex > 0) { - nm_system_iface_flush_routes (ifindex, family); + nm_platform_route_flush (ifindex); nm_platform_address_flush (ifindex); } diff --git a/src/modem-manager/nm-modem.c b/src/modem-manager/nm-modem.c index 97f878a25c..44ff6161bd 100644 --- a/src/modem-manager/nm-modem.c +++ b/src/modem-manager/nm-modem.c @@ -547,7 +547,7 @@ deactivate (NMModem *self, NMDevice *device) case MM_MODEM_IP_METHOD_DHCP: ifindex = nm_device_get_ip_ifindex (device); if (ifindex > 0) { - nm_system_iface_flush_routes (ifindex, AF_UNSPEC); + nm_platform_route_flush (ifindex); nm_platform_address_flush (ifindex); nm_platform_link_set_down (ifindex); } diff --git a/src/nm-system.c b/src/nm-system.c index b2287dc8ff..9e21a689e5 100644 --- a/src/nm-system.c +++ b/src/nm-system.c @@ -91,59 +91,6 @@ ip4_dest_in_same_subnet (NMIP4Config *config, guint32 dest, guint32 dest_prefix) return FALSE; } -static struct rtnl_route * -nm_system_device_set_ip4_route (int ifindex, - guint32 ip4_dest, - guint32 ip4_prefix, - guint32 ip4_gateway, - guint32 metric, - int mss) -{ - struct nl_sock *nlh; - struct rtnl_route *route; - int err; - - g_return_val_if_fail (ifindex > 0, NULL); - - nlh = nm_netlink_get_default_handle (); - g_return_val_if_fail (nlh != NULL, NULL); - - route = nm_netlink_route_new (ifindex, AF_INET, mss, - NMNL_PROP_PRIO, metric, - NULL); - g_return_val_if_fail (route != NULL, NULL); - - /* Add the route */ - err = nm_netlink_route4_add (route, &ip4_dest, ip4_prefix, &ip4_gateway, 0); - if (err == -NLE_OBJ_NOTFOUND && ip4_gateway) { - /* Gateway might be over a bridge; try adding a route to gateway first */ - struct rtnl_route *route2; - - route2 = nm_netlink_route_new (ifindex, AF_INET, mss, NULL); - if (route2) { - /* Add route to gateway over bridge */ - err = nm_netlink_route4_add (route2, &ip4_gateway, 32, NULL, 0); - if (!err) { - err = nm_netlink_route4_add (route, &ip4_dest, ip4_prefix, &ip4_gateway, 0); - if (err) - nm_netlink_route_delete (route2); - } - rtnl_route_put (route2); - } - } - - if (err) { - nm_log_err (LOGD_DEVICE | LOGD_IP4, - "(%s): failed to set IPv4 route: %s", - nm_platform_link_get_name (ifindex), nl_geterror (err)); - - rtnl_route_put (route); - route = NULL; - } - - return route; -} - NMPlatformIP4Route * nm_system_add_ip4_vpn_gateway_route (NMDevice *parent_device, guint32 vpn_gw) { @@ -243,33 +190,36 @@ nm_system_apply_ip4_config (int ifindex, } if (flags & NM_IP4_COMPARE_FLAG_ROUTES) { - for (i = 0; i < nm_ip4_config_get_num_routes (config); i++) { - NMIP4Route *route = nm_ip4_config_get_route (config, i); - struct rtnl_route *tmp; + int count = nm_ip4_config_get_num_routes (config); + NMIP4Route *config_route; + GArray *routes = g_array_sized_new (FALSE, FALSE, sizeof (NMPlatformIP4Route), count); + NMPlatformIP4Route route; + + for (i = 0; i < count; i++) { + config_route = nm_ip4_config_get_route (config, i); + memset (&route, 0, sizeof (route)); + route.network = nm_ip4_route_get_dest (config_route); + route.plen = nm_ip4_route_get_prefix (config_route); + route.gateway = nm_ip4_route_get_next_hop (config_route); + route.metric = priority; /* Don't add the route if it's more specific than one of the subnets * the device already has an IP address on. */ - if (ip4_dest_in_same_subnet (config, - nm_ip4_route_get_dest (route), - nm_ip4_route_get_prefix (route))) + if (ip4_dest_in_same_subnet (config, route.network, route.plen)) continue; - /* Don't add the route if it doesn't have a gateway and the connection + /* Don't add the default route when and the connection * is never supposed to be the default connection. */ - if ( nm_ip4_config_get_never_default (config) - && nm_ip4_route_get_dest (route) == 0) + if (nm_ip4_config_get_never_default (config) && route.network == 0) continue; - tmp = nm_system_device_set_ip4_route (ifindex, - nm_ip4_route_get_dest (route), - nm_ip4_route_get_prefix (route), - nm_ip4_route_get_next_hop (route), - nm_ip4_route_get_metric (route), - nm_ip4_config_get_mss (config)); - rtnl_route_put (tmp); + g_array_append_val (routes, route); } + + nm_platform_ip4_route_sync (ifindex, routes); + g_array_unref (routes); } if (flags & NM_IP4_COMPARE_FLAG_MTU) { @@ -277,9 +227,6 @@ nm_system_apply_ip4_config (int ifindex, nm_platform_link_set_mtu (ifindex, nm_ip4_config_get_mtu (config)); } - if (priority > 0) - nm_system_device_set_priority (ifindex, config, priority); - return TRUE; } @@ -475,35 +422,35 @@ nm_system_apply_ip6_config (int ifindex, } if (flags & NM_IP6_COMPARE_FLAG_ROUTES) { - const char *iface = nm_platform_link_get_name (ifindex); + int count = nm_ip6_config_get_num_routes (config); + NMIP6Route *config_route; + GArray *routes = g_array_sized_new (FALSE, FALSE, sizeof (NMPlatformIP6Route), count); + NMPlatformIP6Route route; - for (i = 0; i < nm_ip6_config_get_num_routes (config); i++) { - NMIP6Route *route = nm_ip6_config_get_route (config, i); - int err; + for (i = 0; i < count; i++) { + config_route = nm_ip6_config_get_route (config, i); + memset (&route, 0, sizeof (route)); + route.network = *nm_ip6_route_get_dest (config_route); + route.plen = nm_ip6_route_get_prefix (config_route); + route.gateway = *nm_ip6_route_get_next_hop (config_route); - /* Don't add the route if it doesn't have a gateway and the connection - * is never supposed to be the default connection. + /* Don't add the route if it's more specific than one of the subnets + * the device already has an IP address on. */ - if ( nm_ip6_config_get_never_default (config) - && IN6_IS_ADDR_UNSPECIFIED (nm_ip6_route_get_dest (route))) + if (ip6_dest_in_same_subnet (config, &route.network, route.plen)) continue; - err = nm_system_set_ip6_route (ifindex, - nm_ip6_route_get_dest (route), - nm_ip6_route_get_prefix (route), - nm_ip6_route_get_next_hop (route), - nm_ip6_route_get_metric (route), - nm_ip6_config_get_mss (config), - RTPROT_UNSPEC, - RT_TABLE_UNSPEC, - NULL); - if (err && (err != -NLE_EXIST)) { - nm_log_err (LOGD_DEVICE | LOGD_IP6, - "(%s): failed to set IPv6 route: %s", - iface ? iface : "unknown", - nl_geterror (err)); - } + /* Don't add the default route when and the connection + * is never supposed to be the default connection. + */ + if (nm_ip6_config_get_never_default (config) && IN6_IS_ADDR_UNSPECIFIED (&route.network)) + continue; + + g_array_append_val (routes, route); } + + nm_platform_ip6_route_sync (ifindex, routes); + g_array_unref (routes); } // FIXME diff --git a/src/vpn-manager/nm-vpn-connection.c b/src/vpn-manager/nm-vpn-connection.c index 3ada8a0afc..8526d07321 100644 --- a/src/vpn-manager/nm-vpn-connection.c +++ b/src/vpn-manager/nm-vpn-connection.c @@ -164,7 +164,7 @@ vpn_cleanup (NMVPNConnection *connection) if (priv->ip_ifindex) { nm_platform_link_set_down (priv->ip_ifindex); - nm_system_iface_flush_routes (priv->ip_ifindex, AF_UNSPEC); + nm_platform_route_flush (priv->ip_ifindex); nm_platform_address_flush (priv->ip_ifindex); }