From cd5da9bf883b9106fc1b2f264138d8f206487b08 Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Wed, 16 Jan 2013 12:23:44 -0600 Subject: [PATCH] wired: move hardware address handling back to NMDeviceWired subclasses The code flow is actually somewhat simpler this way since the subclasses don't have to ask NMDeviceWired for the address every time. Plus then NMDeviceWired doesn't have to know anything about its subclasses in the constructor. --- src/nm-device-bond.c | 43 ++++++++------- src/nm-device-bridge.c | 43 ++++++++------- src/nm-device-ethernet.c | 100 ++++++++++++++++------------------ src/nm-device-infiniband.c | 66 +++++++++++------------ src/nm-device-wired.c | 106 ------------------------------------- src/nm-device-wired.h | 5 -- src/settings/nm-settings.c | 16 +++--- 7 files changed, 136 insertions(+), 243 deletions(-) diff --git a/src/nm-device-bond.c b/src/nm-device-bond.c index 30df94fa2a..0986bc7d11 100644 --- a/src/nm-device-bond.c +++ b/src/nm-device-bond.c @@ -48,6 +48,8 @@ G_DEFINE_TYPE (NMDeviceBond, nm_device_bond, NM_TYPE_DEVICE_WIRED) typedef struct { gboolean ip4_waiting; gboolean ip6_waiting; + guint8 hw_addr[NM_UTILS_HWADDR_LEN_MAX]; + gsize hw_addr_len; } NMDeviceBondPrivate; enum { @@ -102,23 +104,23 @@ device_state_changed (NMDevice *device, static void update_hw_address (NMDevice *dev) { - const guint8 *hw_addr; - guint8 old_addr[NM_UTILS_HWADDR_LEN_MAX]; - int addrtype, addrlen; + NMDeviceBondPrivate *priv = NM_DEVICE_BOND_GET_PRIVATE (dev); + gsize addrlen; + gboolean changed = FALSE; - addrtype = nm_device_wired_get_hwaddr_type (NM_DEVICE_WIRED (dev)); - g_assert (addrtype >= 0); - addrlen = nm_utils_hwaddr_len (addrtype); - g_assert (addrlen > 0); + addrlen = nm_device_read_hwaddr (dev, priv->hw_addr, sizeof (priv->hw_addr), &changed); + if (addrlen) { + priv->hw_addr_len = addrlen; + if (changed) + g_object_notify (G_OBJECT (dev), NM_DEVICE_BOND_HW_ADDRESS); + } +} - hw_addr = nm_device_wired_get_hwaddr (NM_DEVICE_WIRED (dev)); - memcpy (old_addr, hw_addr, addrlen); - - NM_DEVICE_CLASS (nm_device_bond_parent_class)->update_hw_address (dev); - - hw_addr = nm_device_wired_get_hwaddr (NM_DEVICE_WIRED (dev)); - if (memcmp (old_addr, hw_addr, addrlen)) - g_object_notify (G_OBJECT (dev), NM_DEVICE_BOND_HW_ADDRESS); +static const guint8 * +get_hw_address (NMDevice *device, guint *out_len) +{ + *out_len = NM_DEVICE_BOND_GET_PRIVATE (device)->hw_addr_len; + return NM_DEVICE_BOND_GET_PRIVATE (device)->hw_addr; } static guint32 @@ -241,10 +243,11 @@ complete_connection (NMDevice *device, static gboolean spec_match_list (NMDevice *device, const GSList *specs) { + NMDeviceBondPrivate *priv = NM_DEVICE_BOND_GET_PRIVATE (device); char *hwaddr; gboolean matched; - hwaddr = nm_utils_hwaddr_ntoa (nm_device_wired_get_hwaddr (NM_DEVICE_WIRED (device)), ARPHRD_ETHER); + hwaddr = nm_utils_hwaddr_ntoa (priv->hw_addr, nm_utils_hwaddr_type (priv->hw_addr_len)); matched = nm_match_spec_hwaddr (specs, hwaddr); g_free (hwaddr); @@ -509,14 +512,15 @@ static void get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) { - const guint8 *current_addr; + NMDeviceBondPrivate *priv = NM_DEVICE_BOND_GET_PRIVATE (object); GPtrArray *slaves; GSList *list, *iter; + char *hwaddr; switch (prop_id) { case PROP_HW_ADDRESS: - current_addr = nm_device_wired_get_hwaddr (NM_DEVICE_WIRED (object)); - g_value_take_string (value, nm_utils_hwaddr_ntoa (current_addr, ARPHRD_ETHER)); + hwaddr = nm_utils_hwaddr_ntoa (priv->hw_addr, nm_utils_hwaddr_type (priv->hw_addr_len)); + g_value_take_string (value, hwaddr); break; case PROP_CARRIER: g_value_set_boolean (value, nm_device_wired_get_carrier (NM_DEVICE_WIRED (object))); @@ -561,6 +565,7 @@ nm_device_bond_class_init (NMDeviceBondClass *klass) parent_class->get_generic_capabilities = get_generic_capabilities; parent_class->update_hw_address = update_hw_address; + parent_class->get_hw_address = get_hw_address; parent_class->get_best_auto_connection = get_best_auto_connection; parent_class->check_connection_compatible = check_connection_compatible; parent_class->complete_connection = complete_connection; diff --git a/src/nm-device-bridge.c b/src/nm-device-bridge.c index 6f3d70e4b3..271fa621cd 100644 --- a/src/nm-device-bridge.c +++ b/src/nm-device-bridge.c @@ -48,6 +48,8 @@ G_DEFINE_TYPE (NMDeviceBridge, nm_device_bridge, NM_TYPE_DEVICE_WIRED) typedef struct { gboolean ip4_waiting; gboolean ip6_waiting; + guint8 hw_addr[NM_UTILS_HWADDR_LEN_MAX]; + gsize hw_addr_len; } NMDeviceBridgePrivate; enum { @@ -102,23 +104,23 @@ device_state_changed (NMDevice *device, static void update_hw_address (NMDevice *dev) { - const guint8 *hw_addr; - guint8 old_addr[NM_UTILS_HWADDR_LEN_MAX]; - int addrtype, addrlen; + NMDeviceBridgePrivate *priv = NM_DEVICE_BRIDGE_GET_PRIVATE (dev); + gsize addrlen; + gboolean changed = FALSE; - addrtype = nm_device_wired_get_hwaddr_type (NM_DEVICE_WIRED (dev)); - g_assert (addrtype >= 0); - addrlen = nm_utils_hwaddr_len (addrtype); - g_assert (addrlen > 0); + addrlen = nm_device_read_hwaddr (dev, priv->hw_addr, sizeof (priv->hw_addr), &changed); + if (addrlen) { + priv->hw_addr_len = addrlen; + if (changed) + g_object_notify (G_OBJECT (dev), NM_DEVICE_BRIDGE_HW_ADDRESS); + } +} - hw_addr = nm_device_wired_get_hwaddr (NM_DEVICE_WIRED (dev)); - memcpy (old_addr, hw_addr, addrlen); - - NM_DEVICE_CLASS (nm_device_bridge_parent_class)->update_hw_address (dev); - - hw_addr = nm_device_wired_get_hwaddr (NM_DEVICE_WIRED (dev)); - if (memcmp (old_addr, hw_addr, addrlen)) - g_object_notify (G_OBJECT (dev), NM_DEVICE_BRIDGE_HW_ADDRESS); +static const guint8 * +get_hw_address (NMDevice *device, guint *out_len) +{ + *out_len = NM_DEVICE_BRIDGE_GET_PRIVATE (device)->hw_addr_len; + return NM_DEVICE_BRIDGE_GET_PRIVATE (device)->hw_addr; } static guint32 @@ -247,10 +249,11 @@ complete_connection (NMDevice *device, static gboolean spec_match_list (NMDevice *device, const GSList *specs) { + NMDeviceBridgePrivate *priv = NM_DEVICE_BRIDGE_GET_PRIVATE (device); char *hwaddr; gboolean matched; - hwaddr = nm_utils_hwaddr_ntoa (nm_device_wired_get_hwaddr (NM_DEVICE_WIRED (device)), ARPHRD_ETHER); + hwaddr = nm_utils_hwaddr_ntoa (priv->hw_addr, nm_utils_hwaddr_type (priv->hw_addr_len)); matched = nm_match_spec_hwaddr (specs, hwaddr); g_free (hwaddr); @@ -578,14 +581,15 @@ static void get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) { - const guint8 *current_addr; + NMDeviceBridgePrivate *priv = NM_DEVICE_BRIDGE_GET_PRIVATE (object); GPtrArray *slaves; GSList *list, *iter; + char *hwaddr; switch (prop_id) { case PROP_HW_ADDRESS: - current_addr = nm_device_wired_get_hwaddr (NM_DEVICE_WIRED (object)); - g_value_take_string (value, nm_utils_hwaddr_ntoa (current_addr, ARPHRD_ETHER)); + hwaddr = nm_utils_hwaddr_ntoa (priv->hw_addr, nm_utils_hwaddr_type (priv->hw_addr_len)); + g_value_take_string (value, hwaddr); break; case PROP_CARRIER: g_value_set_boolean (value, nm_device_wired_get_carrier (NM_DEVICE_WIRED (object))); @@ -630,6 +634,7 @@ nm_device_bridge_class_init (NMDeviceBridgeClass *klass) parent_class->get_generic_capabilities = get_generic_capabilities; parent_class->update_hw_address = update_hw_address; + parent_class->get_hw_address = get_hw_address; parent_class->is_available = is_available; parent_class->get_best_auto_connection = get_best_auto_connection; parent_class->check_connection_compatible = check_connection_compatible; diff --git a/src/nm-device-ethernet.c b/src/nm-device-ethernet.c index 4b8f9497fb..385fce564e 100644 --- a/src/nm-device-ethernet.c +++ b/src/nm-device-ethernet.c @@ -81,6 +81,7 @@ typedef struct Supplicant { } Supplicant; typedef struct { + guint8 hw_addr[ETH_ALEN]; /* Current MAC address */ guint8 perm_hw_addr[ETH_ALEN]; /* Permanent MAC address */ guint8 initial_hw_addr[ETH_ALEN]; /* Initial MAC address (as seen when NM starts) */ @@ -350,16 +351,17 @@ nm_device_ethernet_new (const char *udi, } static void -_update_hw_addr (NMDeviceEthernet *self, const guint8 *addr) +update_hw_address (NMDevice *dev) { - const guint8 *current_addr; + NMDeviceEthernetPrivate *priv = NM_DEVICE_ETHERNET_GET_PRIVATE (dev); + gsize addrlen; + gboolean changed = FALSE; - g_return_if_fail (addr != NULL); - - current_addr = nm_device_wired_get_hwaddr (NM_DEVICE_WIRED (self)); - if (memcmp (current_addr, addr, ETH_ALEN)) { - nm_device_wired_set_hwaddr (NM_DEVICE_WIRED (self), addr, ETH_ALEN); - g_object_notify (G_OBJECT (self), NM_DEVICE_ETHERNET_HW_ADDRESS); + addrlen = nm_device_read_hwaddr (dev, priv->hw_addr, sizeof (priv->hw_addr), &changed); + if (addrlen) { + g_return_if_fail (addrlen == ETH_ALEN); + if (changed) + g_object_notify (G_OBJECT (dev), NM_DEVICE_ETHERNET_HW_ADDRESS); } } @@ -367,7 +369,7 @@ static gboolean _set_hw_addr (NMDeviceEthernet *self, const guint8 *addr, const char *detail) { NMDevice *dev = NM_DEVICE (self); - const guint8 *current_addr; + NMDeviceEthernetPrivate *priv = NM_DEVICE_ETHERNET_GET_PRIVATE (self); const char *iface; char *mac_str = NULL; gboolean success = FALSE; @@ -377,8 +379,7 @@ _set_hw_addr (NMDeviceEthernet *self, const guint8 *addr, const char *detail) iface = nm_device_get_iface (dev); /* Do nothing if current MAC is same */ - current_addr = nm_device_wired_get_hwaddr (NM_DEVICE_WIRED (self)); - if (!memcmp (current_addr, addr, ETH_ALEN)) { + if (!memcmp (priv->hw_addr, addr, ETH_ALEN)) { nm_log_dbg (LOGD_DEVICE | LOGD_ETHER, "(%s): no MAC address change needed", iface); return TRUE; } @@ -392,9 +393,15 @@ _set_hw_addr (NMDeviceEthernet *self, const guint8 *addr, const char *detail) success = nm_system_iface_set_mac (nm_device_get_ip_ifindex (dev), (struct ether_addr *) addr); if (success) { /* MAC address succesfully changed; update the current MAC to match */ - _update_hw_addr (self, addr); - nm_log_info (LOGD_DEVICE | LOGD_ETHER, "(%s): %s MAC address to %s", - iface, detail, mac_str); + update_hw_address (dev); + if (memcmp (priv->hw_addr, addr, ETH_ALEN) == 0) { + nm_log_info (LOGD_DEVICE | LOGD_ETHER, "(%s): %s MAC address to %s", + iface, detail, mac_str); + } else { + nm_log_warn (LOGD_DEVICE | LOGD_ETHER, "(%s): new MAC address %s " + "not successfully set", + iface, mac_str); + } } else { nm_log_warn (LOGD_DEVICE | LOGD_ETHER, "(%s): failed to %s MAC address to %s", iface, detail, mac_str); @@ -405,22 +412,6 @@ _set_hw_addr (NMDeviceEthernet *self, const guint8 *addr, const char *detail) return success; } -static void -update_hw_address (NMDevice *dev) -{ - const guint8 *hw_addr; - guint8 old_addr[ETH_ALEN]; - - hw_addr = nm_device_wired_get_hwaddr (NM_DEVICE_WIRED (dev)); - memcpy (old_addr, hw_addr, ETH_ALEN); - - NM_DEVICE_CLASS (nm_device_ethernet_parent_class)->update_hw_address (dev); - - hw_addr = nm_device_wired_get_hwaddr (NM_DEVICE_WIRED (dev)); - if (memcmp (old_addr, hw_addr, ETH_ALEN)) - g_object_notify (G_OBJECT (dev), NM_DEVICE_ETHERNET_HW_ADDRESS); -} - static void update_permanent_hw_address (NMDevice *dev) { @@ -448,13 +439,10 @@ update_permanent_hw_address (NMDevice *dev) errno = 0; ret = ioctl (fd, SIOCETHTOOL, &req); if ((ret < 0) || !nm_ethernet_address_is_valid ((struct ether_addr *) epaddr->data)) { - const guint8 *current_addr; - nm_log_err (LOGD_HW | LOGD_ETHER, "(%s): unable to read permanent MAC address (error %d)", nm_device_get_iface (dev), errno); /* Fall back to current address */ - current_addr = nm_device_wired_get_hwaddr (NM_DEVICE_WIRED (self)); - memcpy (epaddr->data, current_addr, ETH_ALEN); + memcpy (epaddr->data, priv->hw_addr, ETH_ALEN); } if (memcmp (&priv->perm_hw_addr, epaddr->data, ETH_ALEN)) { @@ -470,23 +458,26 @@ update_initial_hw_address (NMDevice *dev) { NMDeviceEthernet *self = NM_DEVICE_ETHERNET (dev); NMDeviceEthernetPrivate *priv = NM_DEVICE_ETHERNET_GET_PRIVATE (self); - const guint8 *current_addr; + gsize addrlen; char *mac_str = NULL; - guint8 *addr = priv->initial_hw_addr; - guint8 zero[ETH_ALEN] = {0,0,0,0,0,0}; /* This sets initial MAC address from current MAC address. It should only * be called from NMDevice constructor() to really get the initial address. */ - current_addr = nm_device_wired_get_hwaddr (NM_DEVICE_WIRED (self)); - if (!memcmp (current_addr, &zero, ETH_ALEN)) - update_hw_address (dev); - - if (memcmp (&priv->initial_hw_addr, current_addr, ETH_ALEN)) - memcpy (&priv->initial_hw_addr, current_addr, ETH_ALEN); + addrlen = nm_device_read_hwaddr (dev, + priv->initial_hw_addr, + sizeof (priv->initial_hw_addr), + NULL); + if (addrlen) + g_return_if_fail (addrlen == ETH_ALEN); mac_str = g_strdup_printf ("%02X:%02X:%02X:%02X:%02X:%02X", - addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]); + priv->initial_hw_addr[0], + priv->initial_hw_addr[1], + priv->initial_hw_addr[2], + priv->initial_hw_addr[3], + priv->initial_hw_addr[4], + priv->initial_hw_addr[5]); nm_log_dbg (LOGD_DEVICE | LOGD_ETHER, "(%s): read initial MAC address %s", nm_device_get_iface (dev), mac_str); @@ -494,6 +485,13 @@ update_initial_hw_address (NMDevice *dev) g_free (mac_str); } +static const guint8 * +get_hw_address (NMDevice *device, guint *out_len) +{ + *out_len = ETH_ALEN; + return NM_DEVICE_ETHERNET_GET_PRIVATE (device)->hw_addr; +} + static guint32 get_generic_capabilities (NMDevice *dev) { @@ -1418,14 +1416,9 @@ hwaddr_matches (NMDevice *device, guint other_hwaddr_len, gboolean fail_if_no_hwaddr) { + NMDeviceEthernetPrivate *priv = NM_DEVICE_ETHERNET_GET_PRIVATE (device); NMSettingWired *s_wired; - const guint8 *devaddr; const GByteArray *mac = NULL; - int devtype; - - devtype = nm_device_wired_get_hwaddr_type (NM_DEVICE_WIRED (device)); - devaddr = nm_device_wired_get_hwaddr (NM_DEVICE_WIRED (device)); - g_return_val_if_fail (devaddr != NULL, FALSE); s_wired = nm_connection_get_setting_wired (connection); if (s_wired) @@ -1437,7 +1430,7 @@ hwaddr_matches (NMDevice *device, g_return_val_if_fail (other_hwaddr_len == ETH_ALEN, FALSE); if (memcmp (mac->data, other_hwaddr, mac->len) == 0) return TRUE; - } else if (memcmp (mac->data, devaddr, mac->len) == 0) + } else if (memcmp (mac->data, priv->hw_addr, mac->len) == 0) return TRUE; } else if (fail_if_no_hwaddr == FALSE) return TRUE; @@ -1465,12 +1458,10 @@ get_property (GObject *object, guint prop_id, { NMDeviceEthernet *self = NM_DEVICE_ETHERNET (object); NMDeviceEthernetPrivate *priv = NM_DEVICE_ETHERNET_GET_PRIVATE (self); - const guint8 *current_addr; switch (prop_id) { case PROP_HW_ADDRESS: - current_addr = nm_device_wired_get_hwaddr (NM_DEVICE_WIRED (self)); - g_value_take_string (value, nm_utils_hwaddr_ntoa (current_addr, ARPHRD_ETHER)); + g_value_take_string (value, nm_utils_hwaddr_ntoa (priv->hw_addr, ARPHRD_ETHER)); break; case PROP_PERM_HW_ADDRESS: g_value_take_string (value, nm_utils_hwaddr_ntoa (&priv->perm_hw_addr, ARPHRD_ETHER)); @@ -1517,6 +1508,7 @@ nm_device_ethernet_class_init (NMDeviceEthernetClass *klass) parent_class->bring_up = bring_up; parent_class->take_down = take_down; parent_class->update_hw_address = update_hw_address; + parent_class->get_hw_address = get_hw_address; parent_class->update_permanent_hw_address = update_permanent_hw_address; parent_class->update_initial_hw_address = update_initial_hw_address; parent_class->get_best_auto_connection = get_best_auto_connection; diff --git a/src/nm-device-infiniband.c b/src/nm-device-infiniband.c index b2915f9ee9..aa50ef9afd 100644 --- a/src/nm-device-infiniband.c +++ b/src/nm-device-infiniband.c @@ -44,7 +44,7 @@ G_DEFINE_TYPE (NMDeviceInfiniband, nm_device_infiniband, NM_TYPE_DEVICE_WIRED) #define NM_INFINIBAND_ERROR (nm_infiniband_error_quark ()) typedef struct { - int dummy; + guint8 hw_addr[INFINIBAND_ALEN]; } NMDeviceInfinibandPrivate; enum { @@ -120,21 +120,26 @@ nm_device_infiniband_new (const char *udi, NULL); } - static void update_hw_address (NMDevice *dev) { - const guint8 *hw_addr; - guint8 old_addr[INFINIBAND_ALEN]; + NMDeviceInfinibandPrivate *priv = NM_DEVICE_INFINIBAND_GET_PRIVATE (dev); + gsize addrlen; + gboolean changed = FALSE; - hw_addr = nm_device_wired_get_hwaddr (NM_DEVICE_WIRED (dev)); - memcpy (old_addr, hw_addr, INFINIBAND_ALEN); + addrlen = nm_device_read_hwaddr (dev, priv->hw_addr, sizeof (priv->hw_addr), &changed); + if (addrlen) { + g_return_if_fail (addrlen == INFINIBAND_ALEN); + if (changed) + g_object_notify (G_OBJECT (dev), NM_DEVICE_INFINIBAND_HW_ADDRESS); + } +} - NM_DEVICE_CLASS (nm_device_infiniband_parent_class)->update_hw_address (dev); - - hw_addr = nm_device_wired_get_hwaddr (NM_DEVICE_WIRED (dev)); - if (memcmp (old_addr, hw_addr, INFINIBAND_ALEN)) - g_object_notify (G_OBJECT (dev), NM_DEVICE_INFINIBAND_HW_ADDRESS); +static const guint8 * +get_hw_address (NMDevice *device, guint *out_len) +{ + *out_len = INFINIBAND_ALEN; + return NM_DEVICE_INFINIBAND_GET_PRIVATE (device)->hw_addr; } static guint32 @@ -148,6 +153,7 @@ get_best_auto_connection (NMDevice *dev, GSList *connections, char **specific_object) { + NMDeviceInfinibandPrivate *priv = NM_DEVICE_INFINIBAND_GET_PRIVATE (dev); GSList *iter; for (iter = connections; iter; iter = g_slist_next (iter)) { @@ -168,11 +174,10 @@ get_best_auto_connection (NMDevice *dev, continue; if (s_infiniband) { - const guint8 *hwaddr = nm_device_wired_get_hwaddr (NM_DEVICE_WIRED (dev)); const GByteArray *mac; mac = nm_setting_infiniband_get_mac_address (s_infiniband); - if (mac && memcmp (mac->data, hwaddr, INFINIBAND_ALEN)) + if (mac && memcmp (mac->data, priv->hw_addr, INFINIBAND_ALEN)) continue; } @@ -249,6 +254,7 @@ check_connection_compatible (NMDevice *device, NMConnection *connection, GError **error) { + NMDeviceInfinibandPrivate *priv = NM_DEVICE_INFINIBAND_GET_PRIVATE (device); NMSettingInfiniband *s_infiniband; const GByteArray *mac; @@ -269,10 +275,8 @@ check_connection_compatible (NMDevice *device, } if (s_infiniband) { - const guint8 *hwaddr = nm_device_wired_get_hwaddr (NM_DEVICE_WIRED (device)); - mac = nm_setting_infiniband_get_mac_address (s_infiniband); - if (mac && memcmp (mac->data, hwaddr, INFINIBAND_ALEN)) { + if (mac && memcmp (mac->data, priv->hw_addr, INFINIBAND_ALEN)) { g_set_error (error, NM_INFINIBAND_ERROR, NM_INFINIBAND_ERROR_CONNECTION_INCOMPATIBLE, @@ -291,9 +295,9 @@ complete_connection (NMDevice *device, const GSList *existing_connections, GError **error) { + NMDeviceInfinibandPrivate *priv = NM_DEVICE_INFINIBAND_GET_PRIVATE (device); NMSettingInfiniband *s_infiniband; const GByteArray *setting_mac; - const guint8 *hwaddr; nm_utils_complete_generic (connection, NM_SETTING_INFINIBAND_SETTING_NAME, @@ -308,11 +312,10 @@ complete_connection (NMDevice *device, nm_connection_add_setting (connection, NM_SETTING (s_infiniband)); } - hwaddr = nm_device_wired_get_hwaddr (NM_DEVICE_WIRED (device)); setting_mac = nm_setting_infiniband_get_mac_address (s_infiniband); if (setting_mac) { /* Make sure the setting MAC (if any) matches the device's MAC */ - if (memcmp (setting_mac->data, hwaddr, INFINIBAND_ALEN)) { + if (memcmp (setting_mac->data, priv->hw_addr, INFINIBAND_ALEN)) { g_set_error_literal (error, NM_SETTING_INFINIBAND_ERROR, NM_SETTING_INFINIBAND_ERROR_INVALID_PROPERTY, @@ -323,8 +326,8 @@ complete_connection (NMDevice *device, GByteArray *mac; /* Lock the connection to this device by default */ - mac = g_byte_array_sized_new (INFINIBAND_ALEN); - g_byte_array_append (mac, hwaddr, INFINIBAND_ALEN); + mac = g_byte_array_sized_new (sizeof (priv->hw_addr)); + g_byte_array_append (mac, priv->hw_addr, sizeof (priv->hw_addr)); g_object_set (G_OBJECT (s_infiniband), NM_SETTING_INFINIBAND_MAC_ADDRESS, mac, NULL); g_byte_array_free (mac, TRUE); } @@ -338,10 +341,11 @@ complete_connection (NMDevice *device, static gboolean spec_match_list (NMDevice *device, const GSList *specs) { + NMDeviceInfinibandPrivate *priv = NM_DEVICE_INFINIBAND_GET_PRIVATE (device); char *hwaddr; gboolean matched; - hwaddr = nm_utils_hwaddr_ntoa (nm_device_wired_get_hwaddr (NM_DEVICE_WIRED (device)), ARPHRD_INFINIBAND); + hwaddr = nm_utils_hwaddr_ntoa (priv->hw_addr, ARPHRD_INFINIBAND); matched = nm_match_spec_hwaddr (specs, hwaddr); g_free (hwaddr); @@ -351,6 +355,7 @@ spec_match_list (NMDevice *device, const GSList *specs) static gboolean infiniband_match_config (NMDevice *self, NMConnection *connection) { + NMDeviceInfinibandPrivate *priv = NM_DEVICE_INFINIBAND_GET_PRIVATE (self); NMSettingInfiniband *s_infiniband; const GByteArray *s_mac; @@ -360,7 +365,7 @@ infiniband_match_config (NMDevice *self, NMConnection *connection) /* MAC address check */ s_mac = nm_setting_infiniband_get_mac_address (s_infiniband); - if (s_mac && memcmp (s_mac->data, nm_device_wired_get_hwaddr (NM_DEVICE_WIRED (self)), INFINIBAND_ALEN)) + if (s_mac && memcmp (s_mac->data, priv->hw_addr, INFINIBAND_ALEN)) return FALSE; return TRUE; @@ -403,14 +408,9 @@ hwaddr_matches (NMDevice *device, guint other_hwaddr_len, gboolean fail_if_no_hwaddr) { + NMDeviceInfinibandPrivate *priv = NM_DEVICE_INFINIBAND_GET_PRIVATE (device); NMSettingInfiniband *s_ib; - const guint8 *devaddr; const GByteArray *mac = NULL; - int devtype; - - devtype = nm_device_wired_get_hwaddr_type (NM_DEVICE_WIRED (device)); - devaddr = nm_device_wired_get_hwaddr (NM_DEVICE_WIRED (device)); - g_return_val_if_fail (devaddr != NULL, FALSE); s_ib = nm_connection_get_setting_infiniband (connection); if (s_ib) @@ -422,7 +422,7 @@ hwaddr_matches (NMDevice *device, g_return_val_if_fail (other_hwaddr_len == INFINIBAND_ALEN, FALSE); if (memcmp (mac->data, other_hwaddr, mac->len) == 0) return TRUE; - } else if (memcmp (mac->data, devaddr, mac->len) == 0) + } else if (memcmp (mac->data, priv->hw_addr, mac->len) == 0) return TRUE; } else if (fail_if_no_hwaddr == FALSE) return TRUE; @@ -434,12 +434,11 @@ static void get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) { - const guint8 *current_addr; + NMDeviceInfinibandPrivate *priv = NM_DEVICE_INFINIBAND_GET_PRIVATE (object); switch (prop_id) { case PROP_HW_ADDRESS: - current_addr = nm_device_wired_get_hwaddr (NM_DEVICE_WIRED (object)); - g_value_take_string (value, nm_utils_hwaddr_ntoa (current_addr, ARPHRD_INFINIBAND)); + g_value_take_string (value, nm_utils_hwaddr_ntoa (priv->hw_addr, ARPHRD_INFINIBAND)); break; case PROP_CARRIER: g_value_set_boolean (value, nm_device_wired_get_carrier (NM_DEVICE_WIRED (object))); @@ -476,6 +475,7 @@ nm_device_infiniband_class_init (NMDeviceInfinibandClass *klass) parent_class->get_generic_capabilities = get_generic_capabilities; parent_class->update_hw_address = update_hw_address; + parent_class->get_hw_address = get_hw_address; parent_class->get_best_auto_connection = get_best_auto_connection; parent_class->check_connection_compatible = check_connection_compatible; parent_class->complete_connection = complete_connection; diff --git a/src/nm-device-wired.c b/src/nm-device-wired.c index c8c0c58c38..a5d690b7a5 100644 --- a/src/nm-device-wired.c +++ b/src/nm-device-wired.c @@ -49,9 +49,6 @@ G_DEFINE_TYPE (NMDeviceWired, nm_device_wired, NM_TYPE_DEVICE) #define NM_DEVICE_WIRED_LOG_LEVEL(dev) ((nm_device_get_device_type (dev) == NM_DEVICE_TYPE_INFINIBAND) ? LOGD_INFINIBAND : LOGD_ETHER) typedef struct { - guint8 hw_addr[NM_UTILS_HWADDR_LEN_MAX]; /* Currently set MAC address */ - guint hw_addr_type; - guint hw_addr_len; gboolean carrier; guint32 speed; @@ -301,22 +298,6 @@ constructor (GType type, nm_device_get_iface (NM_DEVICE (self)), nm_device_get_ifindex (NM_DEVICE (self))); - if (nm_device_get_device_type (self) == NM_DEVICE_TYPE_ETHERNET) { - priv->hw_addr_type = ARPHRD_ETHER; - priv->hw_addr_len = ETH_ALEN; - } else if (nm_device_get_device_type (self) == NM_DEVICE_TYPE_INFINIBAND) { - priv->hw_addr_type = ARPHRD_INFINIBAND; - priv->hw_addr_len = INFINIBAND_ALEN; - } else if (nm_device_get_device_type (self) == NM_DEVICE_TYPE_BOND) { - /* We may not know the hardware address type until a slave is added */ - priv->hw_addr_type = ARPHRD_ETHER; - priv->hw_addr_len = ETH_ALEN; - } else if (nm_device_get_device_type (self) == NM_DEVICE_TYPE_BRIDGE) { - priv->hw_addr_type = ARPHRD_ETHER; - priv->hw_addr_len = ETH_ALEN; - } else - g_assert_not_reached (); - caps = nm_device_get_capabilities (self); if (caps & NM_DEVICE_CAP_CARRIER_DETECT) { /* Only listen to netlink for cards that support carrier detect */ @@ -373,29 +354,6 @@ hw_bring_up (NMDevice *dev, gboolean *no_firmware) return result; } -static void -update_hw_address (NMDevice *dev) -{ - NMDeviceWired *self = NM_DEVICE_WIRED (dev); - NMDeviceWiredPrivate *priv = NM_DEVICE_WIRED_GET_PRIVATE (self); - gsize addrlen; - - addrlen = nm_device_read_hwaddr (dev, priv->hw_addr, sizeof (priv->hw_addr), NULL); - if (addrlen) { - g_warn_if_fail (addrlen == priv->hw_addr_len); - priv->hw_addr_len = addrlen; - } -} - -static const guint8 * -get_hw_address (NMDevice *dev, guint *out_len) -{ - NMDeviceWiredPrivate *priv = NM_DEVICE_WIRED_GET_PRIVATE (dev); - - *out_len = priv->hw_addr_len; - return priv->hw_addr; -} - static gboolean can_interrupt_activation (NMDevice *dev) { @@ -480,74 +438,10 @@ nm_device_wired_class_init (NMDeviceWiredClass *klass) parent_class->hw_bring_up = hw_bring_up; parent_class->can_interrupt_activation = can_interrupt_activation; - parent_class->update_hw_address = update_hw_address; - parent_class->get_hw_address = get_hw_address; parent_class->is_available = is_available; parent_class->connection_match_config = connection_match_config; } -/** - * nm_device_wired_get_hwaddr: - * @dev: an #NMDeviceWired - * - * Get a device's hardware address - * - * Returns: (transfer none): @dev's hardware address - */ -const guint8 * -nm_device_wired_get_hwaddr (NMDeviceWired *dev) -{ - NMDeviceWiredPrivate *priv; - - g_return_val_if_fail (dev != NULL, NULL); - - priv = NM_DEVICE_WIRED_GET_PRIVATE (dev); - return priv->hw_addr; -} - -/** - * nm_device_wired_set_hwaddr: - * @dev: an #NMDeviceWired - * @addr: the new hardware address, @addrlen bytes in length - * @addrlen: the length in bytes of @addr - * - * Sets the device's hardware address. - */ -void -nm_device_wired_set_hwaddr (NMDeviceWired *dev, - const guint8 *addr, - guint addrlen) -{ - NMDeviceWiredPrivate *priv; - - g_return_if_fail (dev != NULL); - g_return_if_fail (addr != NULL); - - priv = NM_DEVICE_WIRED_GET_PRIVATE (dev); - g_return_if_fail (addrlen == priv->hw_addr_len); - - memcpy (priv->hw_addr, addr, priv->hw_addr_len); -} - -/** - * nm_device_wired_get_hwaddr_type: - * @dev: an #NMDeviceWired - * - * Get the type of a device's hardware address - * - * Returns: the type of @dev's hardware address - */ -int -nm_device_wired_get_hwaddr_type (NMDeviceWired *dev) -{ - NMDeviceWiredPrivate *priv; - - g_return_val_if_fail (dev != NULL, -1); - - priv = NM_DEVICE_WIRED_GET_PRIVATE (dev); - return priv->hw_addr_type; -} - /** * nm_device_wired_get_carrier: * @dev: an #NMDeviceWired diff --git a/src/nm-device-wired.h b/src/nm-device-wired.h index 3180ee45ae..c88f2b8f3e 100644 --- a/src/nm-device-wired.h +++ b/src/nm-device-wired.h @@ -46,11 +46,6 @@ typedef struct { GType nm_device_wired_get_type (void); -const guint8 *nm_device_wired_get_hwaddr (NMDeviceWired *dev); -void nm_device_wired_set_hwaddr (NMDeviceWired *dev, - const guint8 *addr, - guint addrlen); -int nm_device_wired_get_hwaddr_type (NMDeviceWired *dev); gboolean nm_device_wired_get_carrier (NMDeviceWired *dev); guint32 nm_device_wired_get_speed (NMDeviceWired *dev); diff --git a/src/settings/nm-settings.c b/src/settings/nm-settings.c index 0c139174eb..fa474e09b4 100644 --- a/src/settings/nm-settings.c +++ b/src/settings/nm-settings.c @@ -1379,12 +1379,13 @@ have_connection_for_device (NMSettings *self, GByteArray *mac, NMDevice *device) /* Search through the list of blacklisted MAC addresses in the config file. */ static gboolean -is_mac_auto_wired_blacklisted (NMSettings *self, const GByteArray *mac, int hwaddr_type) +is_mac_auto_wired_blacklisted (NMSettings *self, const GByteArray *mac) { NMSettingsPrivate *priv = NM_SETTINGS_GET_PRIVATE (self); GKeyFile *config; char **list, **iter; gboolean found = FALSE; + int hwaddr_type; g_return_val_if_fail (mac != NULL, FALSE); @@ -1401,6 +1402,8 @@ is_mac_auto_wired_blacklisted (NMSettings *self, const GByteArray *mac, int hwad if (!g_key_file_load_from_file (config, priv->config_file, G_KEY_FILE_NONE, NULL)) goto out; + hwaddr_type = nm_utils_hwaddr_type (mac->len); + list = g_key_file_get_string_list (config, "main", CONFIG_KEY_NO_AUTO_DEFAULT, NULL, NULL); for (iter = list; iter && *iter; iter++) { guint8 *candidate, buffer[NM_UTILS_HWADDR_LEN_MAX]; @@ -1584,7 +1587,7 @@ nm_settings_device_added (NMSettings *self, NMDevice *device) NMSettingsPrivate *priv = NM_SETTINGS_GET_PRIVATE (self); GByteArray *mac = NULL; const guint8 *hwaddr; - int hwaddr_type; + guint hwaddr_len = 0; NMDefaultWiredConnection *wired; gboolean read_only = TRUE; const char *id; @@ -1600,14 +1603,13 @@ nm_settings_device_added (NMSettings *self, NMDevice *device) || g_object_get_data (G_OBJECT (device), DEFAULT_WIRED_TAG)) return; - hwaddr = nm_device_wired_get_hwaddr (NM_DEVICE_WIRED (device)); - hwaddr_type = nm_device_wired_get_hwaddr_type (NM_DEVICE_WIRED (device)); + hwaddr = nm_device_get_hw_address (device, &hwaddr_len); - mac = g_byte_array_new (); - g_byte_array_append (mac, hwaddr, nm_utils_hwaddr_len (hwaddr_type)); + mac = g_byte_array_sized_new (hwaddr_len); + g_byte_array_append (mac, hwaddr, hwaddr_len); if ( have_connection_for_device (self, mac, device) - || is_mac_auto_wired_blacklisted (self, mac, hwaddr_type)) + || is_mac_auto_wired_blacklisted (self, mac)) goto ignore; if (get_plugin (self, NM_SYSTEM_CONFIG_INTERFACE_CAP_MODIFY_CONNECTIONS))