summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Williams <dcbw@redhat.com>2014-10-03 17:37:26 -0500
committerDan Williams <dcbw@redhat.com>2014-12-11 09:44:16 -0600
commit47aef240facc913633d1cdf5bc061d15d7fdab38 (patch)
treec400f2cdc25fa92e044a84cc623ff15baed868b3
parent4080fb7fe5df9965f27db09778b38149768d56e1 (diff)
downloadNetworkManager-47aef240facc913633d1cdf5bc061d15d7fdab38.tar.gz
core: move permanent and initial MAC address reading to NMDevice and NMPlatform
Ethernet, WiFi, and VLAN used the same implementation for initial address. Ethernet and WiFi used the same implementation (and duplicated code) for permanent MAC address, plus they both used ethtool in what should be generic code, which is better done in the platform.
-rw-r--r--src/devices/nm-device-ethernet.c120
-rw-r--r--src/devices/nm-device-vlan.c32
-rw-r--r--src/devices/nm-device.c46
-rw-r--r--src/devices/nm-device.h7
-rw-r--r--src/devices/wifi/nm-device-wifi.c140
-rw-r--r--src/platform/nm-fake-platform.c7
-rw-r--r--src/platform/nm-linux-platform.c29
-rw-r--r--src/platform/nm-platform.c29
-rw-r--r--src/platform/nm-platform.h5
9 files changed, 176 insertions, 239 deletions
diff --git a/src/devices/nm-device-ethernet.c b/src/devices/nm-device-ethernet.c
index c811b36e76..e3f664cb50 100644
--- a/src/devices/nm-device-ethernet.c
+++ b/src/devices/nm-device-ethernet.c
@@ -99,9 +99,6 @@ typedef enum {
} DcbWait;
typedef struct {
- char * perm_hw_addr; /* Permanent MAC address */
- char * initial_hw_addr; /* Initial MAC address (as seen when NM starts) */
-
guint32 speed;
Supplicant supplicant;
@@ -310,66 +307,6 @@ nm_device_ethernet_init (NMDeviceEthernet *self)
priv->s390_options = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
}
-static void
-update_permanent_hw_address (NMDevice *dev)
-{
- NMDeviceEthernet *self = NM_DEVICE_ETHERNET (dev);
- NMDeviceEthernetPrivate *priv = NM_DEVICE_ETHERNET_GET_PRIVATE (self);
- struct ifreq req;
- struct ethtool_perm_addr *epaddr = NULL;
- int fd, ret, errsv;
- const char *mac;
-
- g_return_if_fail (priv->perm_hw_addr == NULL);
-
- fd = socket (PF_INET, SOCK_DGRAM, 0);
- if (fd < 0) {
- _LOGW (LOGD_HW, "couldn't open control socket.");
- return;
- }
-
- /* Get permanent MAC address */
- memset (&req, 0, sizeof (struct ifreq));
- strncpy (req.ifr_name, nm_device_get_iface (dev), IFNAMSIZ);
-
- epaddr = g_malloc0 (sizeof (struct ethtool_perm_addr) + ETH_ALEN);
- epaddr->cmd = ETHTOOL_GPERMADDR;
- epaddr->size = ETH_ALEN;
- req.ifr_data = (void *) epaddr;
-
- errno = 0;
- ret = ioctl (fd, SIOCETHTOOL, &req);
- errsv = errno;
- if ((ret < 0) || !nm_ethernet_address_is_valid (epaddr->data, ETH_ALEN)) {
- _LOGD (LOGD_HW | LOGD_ETHER, "unable to read permanent MAC address (error %d)", errsv);
- /* Fall back to current address */
- mac = nm_device_get_hw_address (dev);
- if (mac)
- nm_utils_hwaddr_aton (mac, epaddr->data, ETH_ALEN);
- else
- memset (epaddr->data, 0, ETH_ALEN);
- }
-
- priv->perm_hw_addr = nm_utils_hwaddr_ntoa (epaddr->data, ETH_ALEN);
-
- g_free (epaddr);
- close (fd);
-}
-
-static void
-update_initial_hw_address (NMDevice *dev)
-{
- NMDeviceEthernet *self = NM_DEVICE_ETHERNET (dev);
- NMDeviceEthernetPrivate *priv = NM_DEVICE_ETHERNET_GET_PRIVATE (self);
-
- /* This sets initial MAC address from current MAC address. It should only
- * be called from NMDevice constructor() to really get the initial address.
- */
- priv->initial_hw_addr = g_strdup (nm_device_get_hw_address (dev));
-
- _LOGD (LOGD_DEVICE | LOGD_ETHER, "read initial MAC address %s", priv->initial_hw_addr);
-}
-
static guint32
get_generic_capabilities (NMDevice *device)
{
@@ -421,7 +358,6 @@ static gboolean
check_connection_compatible (NMDevice *device, NMConnection *connection)
{
NMDeviceEthernet *self = NM_DEVICE_ETHERNET (device);
- NMDeviceEthernetPrivate *priv = NM_DEVICE_ETHERNET_GET_PRIVATE (self);
NMSettingWired *s_wired;
if (!NM_DEVICE_CLASS (nm_device_ethernet_parent_class)->check_connection_compatible (device, connection))
@@ -438,7 +374,7 @@ check_connection_compatible (NMDevice *device, NMConnection *connection)
return FALSE;
if (s_wired) {
- const char *mac;
+ const char *mac, *perm_hw_addr;
gboolean try_mac = TRUE;
const char * const *mac_blacklist;
int i;
@@ -446,8 +382,10 @@ check_connection_compatible (NMDevice *device, NMConnection *connection)
if (!match_subchans (self, s_wired, &try_mac))
return FALSE;
+ perm_hw_addr = nm_device_get_permanent_hw_address (device);
+
mac = nm_setting_wired_get_mac_address (s_wired);
- if (try_mac && mac && !nm_utils_hwaddr_matches (mac, -1, priv->perm_hw_addr, -1))
+ if (try_mac && mac && perm_hw_addr && !nm_utils_hwaddr_matches (mac, -1, perm_hw_addr, -1))
return FALSE;
/* Check for MAC address blacklist */
@@ -458,7 +396,7 @@ check_connection_compatible (NMDevice *device, NMConnection *connection)
return FALSE;
}
- if (nm_utils_hwaddr_matches (mac_blacklist[i], -1, priv->perm_hw_addr, -1))
+ if (perm_hw_addr && nm_utils_hwaddr_matches (mac_blacklist[i], -1, perm_hw_addr, -1))
return FALSE;
}
}
@@ -1391,8 +1329,8 @@ deactivate (NMDevice *device)
NM_DEVICE_ETHERNET_GET_PRIVATE (device)->last_pppoe_time = nm_utils_get_monotonic_timestamp_s ();
/* Reset MAC address back to initial address */
- if (priv->initial_hw_addr)
- nm_device_set_hw_addr (device, priv->initial_hw_addr, "reset", LOGD_ETHER);
+ if (nm_device_get_initial_hw_address (device))
+ nm_device_set_hw_addr (device, nm_device_get_initial_hw_address (device), "reset", LOGD_ETHER);
}
static gboolean
@@ -1402,10 +1340,10 @@ complete_connection (NMDevice *device,
const GSList *existing_connections,
GError **error)
{
- NMDeviceEthernetPrivate *priv = NM_DEVICE_ETHERNET_GET_PRIVATE (device);
NMSettingWired *s_wired;
NMSettingPppoe *s_pppoe;
const char *setting_mac;
+ const char *perm_hw_addr;
s_pppoe = nm_connection_get_setting_pppoe (connection);
@@ -1432,21 +1370,22 @@ complete_connection (NMDevice *device,
nm_connection_add_setting (connection, NM_SETTING (s_wired));
}
- setting_mac = nm_setting_wired_get_mac_address (s_wired);
- if (setting_mac) {
- /* Make sure the setting MAC (if any) matches the device's permanent MAC */
- if (!nm_utils_hwaddr_matches (setting_mac, -1, priv->perm_hw_addr, -1)) {
- g_set_error_literal (error,
- NM_CONNECTION_ERROR,
- NM_CONNECTION_ERROR_INVALID_PROPERTY,
- _("connection does not match device"));
- g_prefix_error (error, "%s.%s: ", NM_SETTING_WIRED_SETTING_NAME, NM_SETTING_WIRED_MAC_ADDRESS);
- return FALSE;
- }
- } else {
- if (!nm_utils_hwaddr_matches (priv->perm_hw_addr, -1, NULL, ETH_ALEN)) {
+ perm_hw_addr = nm_device_get_permanent_hw_address (device);
+ if (perm_hw_addr) {
+ setting_mac = nm_setting_wired_get_mac_address (s_wired);
+ if (setting_mac) {
+ /* Make sure the setting MAC (if any) matches the device's permanent MAC */
+ if (!nm_utils_hwaddr_matches (setting_mac, -1, perm_hw_addr, -1)) {
+ g_set_error_literal (error,
+ NM_CONNECTION_ERROR,
+ NM_CONNECTION_ERROR_INVALID_PROPERTY,
+ _("connection does not match device"));
+ g_prefix_error (error, "%s.%s: ", NM_SETTING_WIRED_SETTING_NAME, NM_SETTING_WIRED_MAC_ADDRESS);
+ return FALSE;
+ }
+ } else {
g_object_set (G_OBJECT (s_wired),
- NM_SETTING_WIRED_MAC_ADDRESS, priv->perm_hw_addr,
+ NM_SETTING_WIRED_MAC_ADDRESS, perm_hw_addr,
NULL);
}
}
@@ -1511,6 +1450,7 @@ update_connection (NMDevice *device, NMConnection *connection)
{
NMDeviceEthernetPrivate *priv = NM_DEVICE_ETHERNET_GET_PRIVATE (device);
NMSettingWired *s_wired = nm_connection_get_setting_wired (connection);
+ const char *perm_hw_addr = nm_device_get_permanent_hw_address (device);
const char *mac = nm_device_get_hw_address (device);
const char *mac_prop = NM_SETTING_WIRED_MAC_ADDRESS;
GHashTableIter iter;
@@ -1524,11 +1464,11 @@ update_connection (NMDevice *device, NMConnection *connection)
/* If the device reports a permanent address, use that for the MAC address
* and the current MAC, if different, is the cloned MAC.
*/
- if (!nm_utils_hwaddr_matches (priv->perm_hw_addr, -1, NULL, ETH_ALEN)) {
- g_object_set (s_wired, NM_SETTING_WIRED_MAC_ADDRESS, priv->perm_hw_addr, NULL);
+ if (perm_hw_addr) {
+ g_object_set (s_wired, NM_SETTING_WIRED_MAC_ADDRESS, perm_hw_addr, NULL);
mac_prop = NULL;
- if (mac && !nm_utils_hwaddr_matches (priv->perm_hw_addr, -1, mac, -1))
+ if (mac && !nm_utils_hwaddr_matches (perm_hw_addr, -1, mac, -1))
mac_prop = NM_SETTING_WIRED_CLONED_MAC_ADDRESS;
}
@@ -1634,8 +1574,6 @@ finalize (GObject *object)
NMDeviceEthernet *self = NM_DEVICE_ETHERNET (object);
NMDeviceEthernetPrivate *priv = NM_DEVICE_ETHERNET_GET_PRIVATE (self);
- g_free (priv->perm_hw_addr);
- g_free (priv->initial_hw_addr);
g_clear_object (&priv->supplicant.mgr);
g_free (priv->subchan1);
g_free (priv->subchan2);
@@ -1656,7 +1594,7 @@ get_property (GObject *object, guint prop_id,
switch (prop_id) {
case PROP_PERM_HW_ADDRESS:
- g_value_set_string (value, priv->perm_hw_addr);
+ g_value_set_string (value, nm_device_get_permanent_hw_address (NM_DEVICE (object)));
break;
case PROP_SPEED:
g_value_set_uint (value, priv->speed);
@@ -1696,8 +1634,6 @@ nm_device_ethernet_class_init (NMDeviceEthernetClass *klass)
object_class->set_property = set_property;
parent_class->get_generic_capabilities = get_generic_capabilities;
- parent_class->update_permanent_hw_address = update_permanent_hw_address;
- parent_class->update_initial_hw_address = update_initial_hw_address;
parent_class->check_connection_compatible = check_connection_compatible;
parent_class->complete_connection = complete_connection;
parent_class->new_default_connection = new_default_connection;
diff --git a/src/devices/nm-device-vlan.c b/src/devices/nm-device-vlan.c
index 9013e495a9..704edcb32e 100644
--- a/src/devices/nm-device-vlan.c
+++ b/src/devices/nm-device-vlan.c
@@ -51,8 +51,6 @@ G_DEFINE_TYPE (NMDeviceVlan, nm_device_vlan, NM_TYPE_DEVICE)
#define NM_DEVICE_VLAN_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_DEVICE_VLAN, NMDeviceVlanPrivate))
typedef struct {
- char *initial_hw_addr;
-
gboolean disposed;
gboolean invalid;
@@ -74,16 +72,6 @@ enum {
/******************************************************************/
-static void
-update_initial_hw_address (NMDevice *dev)
-{
- NMDeviceVlan *self = NM_DEVICE_VLAN (dev);
- NMDeviceVlanPrivate *priv = NM_DEVICE_VLAN_GET_PRIVATE (self);
-
- priv->initial_hw_addr = g_strdup (nm_device_get_hw_address (dev));
- _LOGD (LOGD_DEVICE | LOGD_VLAN, "read initial MAC address %s", priv->initial_hw_addr);
-}
-
static guint32
get_generic_capabilities (NMDevice *dev)
{
@@ -386,12 +374,9 @@ ip4_config_pre_commit (NMDevice *device, NMIP4Config *config)
static void
deactivate (NMDevice *device)
{
- NMDeviceVlan *self = NM_DEVICE_VLAN (device);
- NMDeviceVlanPrivate *priv = NM_DEVICE_VLAN_GET_PRIVATE (self);
-
/* Reset MAC address back to initial address */
- if (priv->initial_hw_addr)
- nm_device_set_hw_addr (device, priv->initial_hw_addr, "reset", LOGD_VLAN);
+ if (nm_device_get_initial_hw_address (device))
+ nm_device_set_hw_addr (device, nm_device_get_initial_hw_address (device), "reset", LOGD_VLAN);
}
/******************************************************************/
@@ -523,17 +508,6 @@ dispose (GObject *object)
}
static void
-finalize (GObject *object)
-{
- NMDeviceVlan *self = NM_DEVICE_VLAN (object);
- NMDeviceVlanPrivate *priv = NM_DEVICE_VLAN_GET_PRIVATE (self);
-
- g_free (priv->initial_hw_addr);
-
- G_OBJECT_CLASS (nm_device_vlan_parent_class)->finalize (object);
-}
-
-static void
nm_device_vlan_class_init (NMDeviceVlanClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
@@ -548,9 +522,7 @@ nm_device_vlan_class_init (NMDeviceVlanClass *klass)
object_class->get_property = get_property;
object_class->set_property = set_property;
object_class->dispose = dispose;
- object_class->finalize = finalize;
- parent_class->update_initial_hw_address = update_initial_hw_address;
parent_class->get_generic_capabilities = get_generic_capabilities;
parent_class->bring_up = bring_up;
parent_class->act_stage1_prepare = act_stage1_prepare;
diff --git a/src/devices/nm-device.c b/src/devices/nm-device.c
index 1fcbfd2bc0..39ab997ee7 100644
--- a/src/devices/nm-device.c
+++ b/src/devices/nm-device.c
@@ -197,6 +197,8 @@ typedef struct {
GHashTable * available_connections;
char * hw_addr;
guint hw_addr_len;
+ char * perm_hw_addr;
+ char * initial_hw_addr;
char * physical_port_id;
NMUnmanagedFlags unmanaged_flags;
@@ -7814,6 +7816,22 @@ nm_device_set_hw_addr (NMDevice *self, const char *addr,
return success;
}
+const char *
+nm_device_get_permanent_hw_address (NMDevice *self)
+{
+ g_return_val_if_fail (NM_IS_DEVICE (self), NULL);
+
+ return NM_DEVICE_GET_PRIVATE (self)->perm_hw_addr;
+}
+
+const char *
+nm_device_get_initial_hw_address (NMDevice *self)
+{
+ g_return_val_if_fail (NM_IS_DEVICE (self), NULL);
+
+ return NM_DEVICE_GET_PRIVATE (self)->initial_hw_addr;
+}
+
/**
* nm_device_spec_match_list:
* @self: an #NMDevice
@@ -7957,13 +7975,29 @@ constructed (GObject *object)
nm_device_update_hw_address (self);
- if (NM_DEVICE_GET_CLASS (self)->update_permanent_hw_address)
- NM_DEVICE_GET_CLASS (self)->update_permanent_hw_address (self);
+ if (priv->hw_addr_len) {
+ priv->initial_hw_addr = g_strdup (priv->hw_addr);
+ _LOGD (LOGD_DEVICE | LOGD_HW, "read initial MAC address %s", priv->initial_hw_addr);
+
+ if (priv->ifindex > 0) {
+ guint8 buf[NM_UTILS_HWADDR_LEN_MAX];
+ size_t len = 0;
- if (NM_DEVICE_GET_CLASS (self)->update_initial_hw_address)
- NM_DEVICE_GET_CLASS (self)->update_initial_hw_address (self);
+ if (nm_platform_link_get_permanent_address (priv->ifindex, buf, &len)) {
+ g_warn_if_fail (len == priv->hw_addr_len);
+ priv->perm_hw_addr = nm_utils_hwaddr_ntoa (buf, priv->hw_addr_len);
+ _LOGD (LOGD_DEVICE | LOGD_HW, "read permanent MAC address %s",
+ priv->perm_hw_addr);
+ } else {
+ /* Fall back to current address */
+ _LOGD (LOGD_HW | LOGD_ETHER, "unable to read permanent MAC address (error %d)",
+ nm_platform_get_error ());
+ priv->perm_hw_addr = g_strdup (priv->hw_addr);
+ }
+ }
+ }
- /* Have to call update_initial_hw_address() before calling get_ignore_carrier() */
+ /* Note: initial hardware address must be read before calling get_ignore_carrier() */
if (device_has_capability (self, NM_DEVICE_CAP_CARRIER_DETECT)) {
priv->ignore_carrier = nm_config_get_ignore_carrier (nm_config_get (), self);
@@ -8081,6 +8115,8 @@ finalize (GObject *object)
_LOGD (LOGD_DEVICE, "finalize(): %s", G_OBJECT_TYPE_NAME (self));
g_free (priv->hw_addr);
+ g_free (priv->perm_hw_addr);
+ g_free (priv->initial_hw_addr);
g_slist_free_full (priv->pending_actions, g_free);
g_clear_pointer (&priv->physical_port_id, g_free);
g_free (priv->udi);
diff --git a/src/devices/nm-device.h b/src/devices/nm-device.h
index 38eddaa8df..10a3b3388f 100644
--- a/src/devices/nm-device.h
+++ b/src/devices/nm-device.h
@@ -109,9 +109,6 @@ typedef struct {
/* Carrier state (IFF_LOWER_UP) */
void (*carrier_changed) (NMDevice *, gboolean carrier);
- void (* update_permanent_hw_address) (NMDevice *self);
- void (* update_initial_hw_address) (NMDevice *self);
-
gboolean (* get_ip_iface_identifier) (NMDevice *self, NMUtilsIPv6IfaceId *out_iid);
guint32 (* get_generic_capabilities) (NMDevice *self);
@@ -233,7 +230,9 @@ int nm_device_get_priority (NMDevice *dev);
guint32 nm_device_get_ip4_route_metric (NMDevice *dev);
guint32 nm_device_get_ip6_route_metric (NMDevice *dev);
-const char * nm_device_get_hw_address (NMDevice *dev);
+const char * nm_device_get_hw_address (NMDevice *dev);
+const char * nm_device_get_permanent_hw_address (NMDevice *dev);
+const char * nm_device_get_initial_hw_address (NMDevice *dev);
NMDhcp4Config * nm_device_get_dhcp4_config (NMDevice *dev);
NMDhcp6Config * nm_device_get_dhcp6_config (NMDevice *dev);
diff --git a/src/devices/wifi/nm-device-wifi.c b/src/devices/wifi/nm-device-wifi.c
index dd7754b33e..b0d2f654be 100644
--- a/src/devices/wifi/nm-device-wifi.c
+++ b/src/devices/wifi/nm-device-wifi.c
@@ -26,13 +26,7 @@
#include <dbus/dbus.h>
#include <netinet/in.h>
#include <string.h>
-#include <sys/stat.h>
-#include <sys/wait.h>
-#include <signal.h>
#include <unistd.h>
-#include <linux/sockios.h>
-#include <linux/ethtool.h>
-#include <sys/ioctl.h>
#include <errno.h>
#include "nm-glib-compat.h"
@@ -118,9 +112,6 @@ static guint signals[LAST_SIGNAL] = { 0 };
struct _NMDeviceWifiPrivate {
gboolean disposed;
- char * perm_hw_addr; /* Permanent MAC address */
- char * initial_hw_addr; /* Initial MAC address (as seen when NM starts) */
-
gint8 invalid_strength_counter;
GSList * ap_list;
@@ -741,8 +732,8 @@ deactivate (NMDevice *device)
nm_platform_wifi_indicate_addressing_running (ifindex, FALSE);
/* Reset MAC address back to initial address */
- if (priv->initial_hw_addr)
- nm_device_set_hw_addr (device, priv->initial_hw_addr, "reset", LOGD_WIFI);
+ if (nm_device_get_initial_hw_address (device))
+ nm_device_set_hw_addr (device, nm_device_get_initial_hw_address (device), "reset", LOGD_WIFI);
/* Ensure we're in infrastructure mode after deactivation; some devices
* (usually older ones) don't scan well in adhoc mode.
@@ -806,6 +797,7 @@ check_connection_compatible (NMDevice *device, NMConnection *connection)
const char * const *mac_blacklist;
int i;
const char *mode;
+ const char *perm_hw_addr;
if (!NM_DEVICE_CLASS (nm_device_wifi_parent_class)->check_connection_compatible (device, connection))
return FALSE;
@@ -820,8 +812,10 @@ check_connection_compatible (NMDevice *device, NMConnection *connection)
if (!s_wireless)
return FALSE;
+ perm_hw_addr = nm_device_get_permanent_hw_address (device);
+
mac = nm_setting_wireless_get_mac_address (s_wireless);
- if (mac && !nm_utils_hwaddr_matches (mac, -1, priv->perm_hw_addr, -1))
+ if (mac && perm_hw_addr && !nm_utils_hwaddr_matches (mac, -1, perm_hw_addr, -1))
return FALSE;
/* Check for MAC address blacklist */
@@ -832,7 +826,7 @@ check_connection_compatible (NMDevice *device, NMConnection *connection)
return FALSE;
}
- if (nm_utils_hwaddr_matches (mac_blacklist[i], -1, priv->perm_hw_addr, -1))
+ if (perm_hw_addr && nm_utils_hwaddr_matches (mac_blacklist[i], -1, perm_hw_addr, -1))
return FALSE;
}
@@ -978,6 +972,7 @@ complete_connection (NMDevice *device,
GBytes *setting_ssid = NULL;
GSList *iter;
gboolean hidden = FALSE;
+ const char *perm_hw_addr;
s_wifi = nm_connection_get_setting_wireless (connection);
s_wsec = nm_connection_get_setting_wireless_security (connection);
@@ -1113,29 +1108,31 @@ complete_connection (NMDevice *device,
if (hidden)
g_object_set (s_wifi, NM_SETTING_WIRELESS_HIDDEN, TRUE, NULL);
- setting_mac = nm_setting_wireless_get_mac_address (s_wifi);
- if (setting_mac) {
- /* Make sure the setting MAC (if any) matches the device's permanent MAC */
- if (!nm_utils_hwaddr_matches (setting_mac, -1, priv->perm_hw_addr, -1)) {
- g_set_error_literal (error,
- NM_CONNECTION_ERROR,
- NM_CONNECTION_ERROR_INVALID_PROPERTY,
- _("connection does not match device"));
- g_prefix_error (error, "%s.%s: ", NM_SETTING_WIRELESS_SETTING_NAME, NM_SETTING_WIRELESS_MAC_ADDRESS);
- return FALSE;
- }
- } else {
- guint8 perm_hw_addr[ETH_ALEN];
+ perm_hw_addr = nm_device_get_permanent_hw_address (device);
+ if (perm_hw_addr) {
+ setting_mac = nm_setting_wireless_get_mac_address (s_wifi);
+ if (setting_mac) {
+ /* Make sure the setting MAC (if any) matches the device's permanent MAC */
+ if (!nm_utils_hwaddr_matches (setting_mac, -1, perm_hw_addr, -1)) {
+ g_set_error_literal (error,
+ NM_CONNECTION_ERROR,
+ NM_CONNECTION_ERROR_INVALID_PROPERTY,
+ _("connection does not match device"));
+ g_prefix_error (error, "%s.%s: ", NM_SETTING_WIRELESS_SETTING_NAME, NM_SETTING_WIRELESS_MAC_ADDRESS);
+ return FALSE;
+ }
+ } else {
+ guint8 tmp[ETH_ALEN];
- /* Lock the connection to this device by default if it uses a
- * permanent MAC address (ie not a 'locally administered' one)
- */
- nm_utils_hwaddr_aton (priv->perm_hw_addr, perm_hw_addr, ETH_ALEN);
- if ( !(perm_hw_addr[0] & 0x02)
- && !nm_utils_hwaddr_matches (perm_hw_addr, ETH_ALEN, NULL, ETH_ALEN)) {
- g_object_set (G_OBJECT (s_wifi),
- NM_SETTING_WIRELESS_MAC_ADDRESS, priv->perm_hw_addr,
- NULL);
+ /* Lock the connection to this device by default if it uses a
+ * permanent MAC address (ie not a 'locally administered' one)
+ */
+ nm_utils_hwaddr_aton (perm_hw_addr, tmp, ETH_ALEN);
+ if (!(tmp[0] & 0x02)) {
+ g_object_set (G_OBJECT (s_wifi),
+ NM_SETTING_WIRELESS_MAC_ADDRESS, perm_hw_addr,
+ NULL);
+ }
}
}
@@ -2504,62 +2501,6 @@ error:
/****************************************************************************/
-static void
-update_permanent_hw_address (NMDevice *device)
-{
- NMDeviceWifi *self = NM_DEVICE_WIFI (device);
- NMDeviceWifiPrivate *priv = NM_DEVICE_WIFI_GET_PRIVATE (self);
- struct ifreq req;
- struct ethtool_perm_addr *epaddr = NULL;
- int fd, ret, errsv;
-
- g_return_if_fail (priv->perm_hw_addr == NULL);
-
- fd = socket (PF_INET, SOCK_DGRAM, 0);
- if (fd < 0) {
- _LOGE (LOGD_HW, "could not open control socket.");
- return;
- }
-
- /* Get permanent MAC address */
- memset (&req, 0, sizeof (struct ifreq));
- strncpy (req.ifr_name, nm_device_get_iface (device), IFNAMSIZ);
-
- epaddr = g_malloc0 (sizeof (struct ethtool_perm_addr) + ETH_ALEN);
- epaddr->cmd = ETHTOOL_GPERMADDR;
- epaddr->size = ETH_ALEN;
- req.ifr_data = (void *) epaddr;
-
- errno = 0;
- ret = ioctl (fd, SIOCETHTOOL, &req);
- errsv = errno;
- if ((ret < 0) || !nm_ethernet_address_is_valid (epaddr->data, ETH_ALEN)) {
- _LOGD (LOGD_HW | LOGD_ETHER, "unable to read permanent MAC address (error %d)",
- errsv);
- /* Fall back to current address */
- nm_utils_hwaddr_aton (nm_device_get_hw_address (device), epaddr->data, ETH_ALEN);
- }
-
- priv->perm_hw_addr = nm_utils_hwaddr_ntoa (epaddr->data, ETH_ALEN);
-
- g_free (epaddr);
- close (fd);
-}
-
-static void
-update_initial_hw_address (NMDevice *device)
-{
- NMDeviceWifi *self = NM_DEVICE_WIFI (device);
- NMDeviceWifiPrivate *priv = NM_DEVICE_WIFI_GET_PRIVATE (self);
-
- /* This sets initial MAC address from current MAC address. It should only
- * be called from NMDevice constructor() to really get the initial address.
- */
- priv->initial_hw_addr = g_strdup (nm_device_get_hw_address (device));
-
- _LOGD (LOGD_DEVICE | LOGD_ETHER, "read initial MAC address %s", priv->initial_hw_addr);
-}
-
static NMActStageReturn
act_stage1_prepare (NMDevice *device, NMDeviceStateReason *reason)
{
@@ -3248,18 +3189,6 @@ dispose (GObject *object)
}
static void
-finalize (GObject *object)
-{
- NMDeviceWifi *self = NM_DEVICE_WIFI (object);
- NMDeviceWifiPrivate *priv = NM_DEVICE_WIFI_GET_PRIVATE (self);
-
- g_free (priv->perm_hw_addr);
- g_free (priv->initial_hw_addr);
-
- G_OBJECT_CLASS (nm_device_wifi_parent_class)->finalize (object);
-}
-
-static void
get_property (GObject *object, guint prop_id,
GValue *value, GParamSpec *pspec)
{
@@ -3270,7 +3199,7 @@ get_property (GObject *object, guint prop_id,
switch (prop_id) {
case PROP_PERM_HW_ADDRESS:
- g_value_set_string (value, priv->perm_hw_addr);
+ g_value_set_string (value, nm_device_get_permanent_hw_address (NM_DEVICE (device)));
break;
case PROP_MODE:
g_value_set_uint (value, priv->mode);
@@ -3326,11 +3255,8 @@ nm_device_wifi_class_init (NMDeviceWifiClass *klass)
object_class->get_property = get_property;
object_class->set_property = set_property;
object_class->dispose = dispose;
- object_class->finalize = finalize;
parent_class->bring_up = bring_up;
- parent_class->update_permanent_hw_address = update_permanent_hw_address;
- parent_class->update_initial_hw_address = update_initial_hw_address;
parent_class->can_auto_connect = can_auto_connect;
parent_class->is_available = is_available;
parent_class->check_connection_compatible = check_connection_compatible;
diff --git a/src/platform/nm-fake-platform.c b/src/platform/nm-fake-platform.c
index ae6c0f35fd..45b1508ba9 100644
--- a/src/platform/nm-fake-platform.c
+++ b/src/platform/nm-fake-platform.c
@@ -456,6 +456,12 @@ link_get_address (NMPlatform *platform, int ifindex, size_t *length)
}
static gboolean
+link_get_permanent_address (NMPlatform *platform, int ifindex, guint8 *buf, size_t *length)
+{
+ return FALSE;
+}
+
+static gboolean
link_set_mtu (NMPlatform *platform, int ifindex, guint32 mtu)
{
NMFakePlatformLink *device = link_get (platform, ifindex);
@@ -1346,6 +1352,7 @@ nm_fake_platform_class_init (NMFakePlatformClass *klass)
platform_class->link_set_address = link_set_address;
platform_class->link_get_address = link_get_address;
+ platform_class->link_get_permanent_address = link_get_permanent_address;
platform_class->link_get_mtu = link_get_mtu;
platform_class->link_set_mtu = link_set_mtu;
diff --git a/src/platform/nm-linux-platform.c b/src/platform/nm-linux-platform.c
index 4405848ee6..4d8f36cac9 100644
--- a/src/platform/nm-linux-platform.c
+++ b/src/platform/nm-linux-platform.c
@@ -2715,6 +2715,34 @@ link_get_address (NMPlatform *platform, int ifindex, size_t *length)
}
static gboolean
+link_get_permanent_address (NMPlatform *platform,
+ int ifindex,
+ guint8 *buf,
+ size_t *length)
+{
+ gs_free struct ethtool_perm_addr *epaddr = NULL;
+ const char *ifname;
+
+ ifname = nm_platform_link_get_name (ifindex);
+ if (!ifname)
+ return FALSE;
+
+ epaddr = g_malloc0 (sizeof (*epaddr) + NM_UTILS_HWADDR_LEN_MAX);
+ epaddr->cmd = ETHTOOL_GPERMADDR;
+ epaddr->size = NM_UTILS_HWADDR_LEN_MAX;
+
+ if (!ethtool_get (ifname, epaddr))
+ return FALSE;
+ if (!nm_ethernet_address_is_valid (epaddr->data, epaddr->size))
+ return FALSE;
+
+ g_assert (epaddr->size <= NM_UTILS_HWADDR_LEN_MAX);
+ memcpy (buf, epaddr->data, epaddr->size);
+ *length = epaddr->size;
+ return TRUE;
+}
+
+static gboolean
link_set_mtu (NMPlatform *platform, int ifindex, guint32 mtu)
{
auto_nl_object struct rtnl_link *change = _nm_rtnl_link_alloc (ifindex, NULL);
@@ -4568,6 +4596,7 @@ nm_linux_platform_class_init (NMLinuxPlatformClass *klass)
platform_class->link_get_address = link_get_address;
platform_class->link_set_address = link_set_address;
+ platform_class->link_get_permanent_address = link_get_permanent_address;
platform_class->link_get_mtu = link_get_mtu;
platform_class->link_set_mtu = link_set_mtu;
diff --git a/src/platform/nm-platform.c b/src/platform/nm-platform.c
index f794a634cb..c5ed11e3d1 100644
--- a/src/platform/nm-platform.c
+++ b/src/platform/nm-platform.c
@@ -860,7 +860,8 @@ nm_platform_link_set_address (int ifindex, gconstpointer address, size_t length)
* @ifindex: Interface index
* @length: Pointer to a variable to store address length
*
- * Saves interface hardware address to @address.
+ * Returns: the interface hardware address as an array of bytes of
+ * length @length.
*/
gconstpointer
nm_platform_link_get_address (int ifindex, size_t *length)
@@ -876,6 +877,32 @@ nm_platform_link_get_address (int ifindex, size_t *length)
return klass->link_get_address (platform, ifindex, length);
}
+/**
+ * nm_platform_link_get_permanent_address:
+ * @ifindex: Interface index
+ * @buf: buffer of at least %NM_UTILS_HWADDR_LEN_MAX bytes, on success
+ * the permanent hardware address
+ * @length: Pointer to a variable to store address length
+ *
+ * Returns: %TRUE on success, %FALSE on failure to read the permanent hardware
+ * address.
+ */
+gboolean
+nm_platform_link_get_permanent_address (int ifindex, guint8 *buf, size_t *length)
+{
+ reset_error ();
+
+ if (length)
+ *length = 0;
+
+ g_return_val_if_fail (ifindex > 0, FALSE);
+ g_return_val_if_fail (klass->link_get_permanent_address, FALSE);
+ g_return_val_if_fail (buf, FALSE);
+ g_return_val_if_fail (length, FALSE);
+
+ return klass->link_get_permanent_address (platform, ifindex, buf, length);
+}
+
gboolean
nm_platform_link_supports_carrier_detect (int ifindex)
{
diff --git a/src/platform/nm-platform.h b/src/platform/nm-platform.h
index 8c7706cf3a..dcf558c064 100644
--- a/src/platform/nm-platform.h
+++ b/src/platform/nm-platform.h
@@ -369,6 +369,10 @@ typedef struct {
gboolean (*link_set_user_ipv6ll_enabled) (NMPlatform *, int ifindex, gboolean enabled);
gconstpointer (*link_get_address) (NMPlatform *, int ifindex, size_t *length);
+ gboolean (*link_get_permanent_address) (NMPlatform *,
+ int ifindex,
+ guint8 *buf,
+ size_t *length);
gboolean (*link_set_address) (NMPlatform *, int ifindex, gconstpointer address, size_t length);
guint32 (*link_get_mtu) (NMPlatform *, int ifindex);
gboolean (*link_set_mtu) (NMPlatform *, int ifindex, guint32 mtu);
@@ -520,6 +524,7 @@ gboolean nm_platform_link_get_user_ipv6ll_enabled (int ifindex);
gboolean nm_platform_link_set_user_ipv6ll_enabled (int ifindex, gboolean enabled);
gconstpointer nm_platform_link_get_address (int ifindex, size_t *length);
+gboolean nm_platform_link_get_permanent_address (int ifindex, guint8 *buf, size_t *length);
gboolean nm_platform_link_set_address (int ifindex, const void *address, size_t length);
guint32 nm_platform_link_get_mtu (int ifindex);
gboolean nm_platform_link_set_mtu (int ifindex, guint32 mtu);