From b019348fdde223f39c91150e6b7a11e366fade63 Mon Sep 17 00:00:00 2001 From: Dan Winship Date: Sat, 21 Jun 2014 12:44:56 -0400 Subject: [PATCH] core: update data types of some hwaddr properties Now that we have nm_utils_hwaddr_matches() for comparing addresses (even when one is a string and the other binary), there are now places where it's more convenient to store hardware addresses as strings rather than binary, since we want them in string form for most non-comparison purposes. So update for that. In particular, this also changes nm_device_get_hw_address() to return a string. Also, simplify the update_permanent_hw_address() implementations by assuming that they will only be called once. (Since they will.) --- src/devices/bluetooth/nm-device-bt.c | 20 ++++----- src/devices/nm-device-bridge.c | 7 ++- src/devices/nm-device-ethernet.c | 54 ++++++++++------------ src/devices/nm-device-infiniband.c | 17 +++---- src/devices/nm-device-vlan.c | 15 +++---- src/devices/nm-device.c | 62 ++++++++++++-------------- src/devices/nm-device.h | 2 +- src/devices/wifi/nm-device-olpc-mesh.c | 9 ++-- src/devices/wifi/nm-device-wifi.c | 57 ++++++++++++++--------- src/devices/wimax/nm-device-wimax.c | 13 +++--- src/nm-config.c | 9 +--- src/nm-manager.c | 9 ++-- src/settings/nm-settings.c | 17 +++---- 13 files changed, 139 insertions(+), 152 deletions(-) diff --git a/src/devices/bluetooth/nm-device-bt.c b/src/devices/bluetooth/nm-device-bt.c index 755397bd0d..4e4f7354e8 100644 --- a/src/devices/bluetooth/nm-device-bt.c +++ b/src/devices/bluetooth/nm-device-bt.c @@ -63,7 +63,7 @@ typedef struct { NMBluezDevice *bt_device; - guint8 bdaddr[ETH_ALEN]; + char *bdaddr; char *name; guint32 capabilities; @@ -181,7 +181,7 @@ check_connection_compatible (NMDevice *device, NMConnection *connection) array = nm_setting_bluetooth_get_bdaddr (s_bt); if (!array) return FALSE; - if (!nm_utils_hwaddr_matches (priv->bdaddr, ETH_ALEN, array->data, array->len)) + if (!nm_utils_hwaddr_matches (priv->bdaddr, -1, array->data, array->len)) return FALSE; return TRUE; @@ -323,7 +323,7 @@ complete_connection (NMDevice *device, setting_bdaddr = nm_setting_bluetooth_get_bdaddr (s_bt); if (setting_bdaddr) { /* Make sure the setting BT Address (if any) matches the device's */ - if (!nm_utils_hwaddr_matches (setting_bdaddr->data, setting_bdaddr->len, priv->bdaddr, ETH_ALEN)) { + if (!nm_utils_hwaddr_matches (setting_bdaddr->data, setting_bdaddr->len, priv->bdaddr, -1)) { g_set_error_literal (error, NM_SETTING_BLUETOOTH_ERROR, NM_SETTING_BLUETOOTH_ERROR_INVALID_PROPERTY, @@ -334,9 +334,8 @@ complete_connection (NMDevice *device, GByteArray *bdaddr; /* Lock the connection to this device by default */ - if (!nm_utils_hwaddr_matches (priv->bdaddr, ETH_ALEN, NULL, ETH_ALEN)) { - bdaddr = g_byte_array_sized_new (ETH_ALEN); - g_byte_array_append (bdaddr, priv->bdaddr, ETH_ALEN); + if (!nm_utils_hwaddr_matches (priv->bdaddr, -1, NULL, ETH_ALEN)) { + bdaddr = nm_utils_hwaddr_atoba (priv->bdaddr, ETH_ALEN); g_object_set (G_OBJECT (s_bt), NM_SETTING_BLUETOOTH_BDADDR, bdaddr, NULL); g_byte_array_free (bdaddr, TRUE); } @@ -1069,15 +1068,13 @@ static void constructed (GObject *object) { NMDeviceBtPrivate *priv = NM_DEVICE_BT_GET_PRIVATE (object); - const guint8 *my_hwaddr; - guint my_hwaddr_len = 0; + const char *my_hwaddr; G_OBJECT_CLASS (nm_device_bt_parent_class)->constructed (object); - my_hwaddr = nm_device_get_hw_address (NM_DEVICE (object), &my_hwaddr_len); + my_hwaddr = nm_device_get_hw_address (NM_DEVICE (object)); g_assert (my_hwaddr); - g_assert_cmpint (my_hwaddr_len, ==, ETH_ALEN); - memcpy (priv->bdaddr, my_hwaddr, ETH_ALEN); + priv->bdaddr = g_strdup (my_hwaddr); /* Watch for BT device property changes */ g_signal_connect (priv->bt_device, "notify::" NM_BLUEZ_DEVICE_CONNECTED, @@ -1164,6 +1161,7 @@ finalize (GObject *object) g_free (priv->rfcomm_iface); g_free (priv->name); + g_free (priv->bdaddr); G_OBJECT_CLASS (nm_device_bt_parent_class)->finalize (object); } diff --git a/src/devices/nm-device-bridge.c b/src/devices/nm-device-bridge.c index 29bd7f1a78..bb3b584f7a 100644 --- a/src/devices/nm-device-bridge.c +++ b/src/devices/nm-device-bridge.c @@ -117,12 +117,11 @@ check_connection_compatible (NMDevice *device, NMConnection *connection) mac_address = nm_setting_bridge_get_mac_address (s_bridge); if (mac_address) { - guint hw_len; - const guint8 *hw_addr; + const char *hw_addr; - hw_addr = nm_device_get_hw_address (device, &hw_len); + hw_addr = nm_device_get_hw_address (device); if ( !hw_addr - || !nm_utils_hwaddr_matches (mac_address->data, mac_address->len, hw_addr, hw_len)) + || !nm_utils_hwaddr_matches (hw_addr, -1, mac_address->data, mac_address->len)) return FALSE; } diff --git a/src/devices/nm-device-ethernet.c b/src/devices/nm-device-ethernet.c index 3cc5e7b5bd..bc10b1d4f5 100644 --- a/src/devices/nm-device-ethernet.c +++ b/src/devices/nm-device-ethernet.c @@ -101,7 +101,7 @@ typedef enum { } DcbWait; typedef struct { - guint8 perm_hw_addr[ETH_ALEN]; /* Permanent MAC address */ + char * perm_hw_addr; /* Permanent MAC address */ guint8 initial_hw_addr[ETH_ALEN]; /* Initial MAC address (as seen when NM starts) */ guint32 speed; @@ -339,7 +339,9 @@ update_permanent_hw_address (NMDevice *dev) struct ifreq req; struct ethtool_perm_addr *epaddr = NULL; int fd, ret, errsv; - const guint8 *mac; + const char *mac; + + g_return_if_fail (priv->perm_hw_addr == NULL); fd = socket (PF_INET, SOCK_DGRAM, 0); if (fd < 0) { @@ -362,17 +364,14 @@ update_permanent_hw_address (NMDevice *dev) if ((ret < 0) || !nm_ethernet_address_is_valid (epaddr->data)) { _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, NULL); + mac = nm_device_get_hw_address (dev); if (mac) - memcpy (epaddr->data, mac, ETH_ALEN); + nm_utils_hwaddr_aton (mac, epaddr->data, ETH_ALEN); else memset (epaddr->data, 0, ETH_ALEN); } - if (!nm_utils_hwaddr_matches (priv->perm_hw_addr, ETH_ALEN, epaddr->data, ETH_ALEN)) { - memcpy (priv->perm_hw_addr, epaddr->data, ETH_ALEN); - g_object_notify (G_OBJECT (dev), NM_DEVICE_ETHERNET_PERMANENT_HW_ADDRESS); - } + priv->perm_hw_addr = nm_utils_hwaddr_ntoa (epaddr->data, ETH_ALEN); g_free (epaddr); close (fd); @@ -383,18 +382,16 @@ update_initial_hw_address (NMDevice *dev) { NMDeviceEthernet *self = NM_DEVICE_ETHERNET (dev); NMDeviceEthernetPrivate *priv = NM_DEVICE_ETHERNET_GET_PRIVATE (self); - gs_free char *tmp_str = NULL; - const guint8 *mac; + const char *mac; /* This sets initial MAC address from current MAC address. It should only * be called from NMDevice constructor() to really get the initial address. */ - mac = nm_device_get_hw_address (dev, NULL); + mac = nm_device_get_hw_address (dev); if (mac) - memcpy (priv->initial_hw_addr, mac, ETH_ALEN); + nm_utils_hwaddr_aton (mac, priv->initial_hw_addr, ETH_ALEN); - _LOGD (LOGD_DEVICE | LOGD_ETHER, "read initial MAC address %s", - (tmp_str = nm_utils_hwaddr_ntoa (priv->initial_hw_addr, ETH_ALEN))); + _LOGD (LOGD_DEVICE | LOGD_ETHER, "read initial MAC address %s", mac); } static guint32 @@ -473,7 +470,7 @@ check_connection_compatible (NMDevice *device, NMConnection *connection) return FALSE; mac = nm_setting_wired_get_mac_address (s_wired); - if (try_mac && mac && !nm_utils_hwaddr_matches (mac->data, mac->len, priv->perm_hw_addr, ETH_ALEN)) + if (try_mac && mac && !nm_utils_hwaddr_matches (mac->data, mac->len, priv->perm_hw_addr, -1)) return FALSE; /* Check for MAC address blacklist */ @@ -487,7 +484,7 @@ check_connection_compatible (NMDevice *device, NMConnection *connection) return FALSE; } - if (nm_utils_hwaddr_matches (addr, ETH_ALEN, priv->perm_hw_addr, ETH_ALEN)) + if (nm_utils_hwaddr_matches (addr, ETH_ALEN, priv->perm_hw_addr, -1)) return FALSE; } } @@ -1462,7 +1459,7 @@ complete_connection (NMDevice *device, 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->data, setting_mac->len, priv->perm_hw_addr, ETH_ALEN)) { + if (!nm_utils_hwaddr_matches (setting_mac->data, setting_mac->len, priv->perm_hw_addr, -1)) { g_set_error_literal (error, NM_SETTING_WIRED_ERROR, NM_SETTING_WIRED_ERROR_INVALID_PROPERTY, @@ -1473,9 +1470,8 @@ complete_connection (NMDevice *device, GByteArray *mac; /* Lock the connection to this device by default */ - if (!nm_utils_hwaddr_matches (priv->perm_hw_addr, ETH_ALEN, NULL, ETH_ALEN)) { - mac = g_byte_array_sized_new (ETH_ALEN); - g_byte_array_append (mac, priv->perm_hw_addr, ETH_ALEN); + if (!nm_utils_hwaddr_matches (priv->perm_hw_addr, -1, NULL, ETH_ALEN)) { + mac = nm_utils_hwaddr_atoba (priv->perm_hw_addr, ETH_ALEN); g_object_set (G_OBJECT (s_wired), NM_SETTING_WIRED_MAC_ADDRESS, mac, NULL); g_byte_array_free (mac, TRUE); } @@ -1500,8 +1496,7 @@ update_connection (NMDevice *device, NMConnection *connection) { NMDeviceEthernetPrivate *priv = NM_DEVICE_ETHERNET_GET_PRIVATE (device); NMSettingWired *s_wired = nm_connection_get_setting_wired (connection); - guint maclen; - const guint8 *mac = nm_device_get_hw_address (device, &maclen); + const char *mac = nm_device_get_hw_address (device); const char *mac_prop = NM_SETTING_WIRED_MAC_ADDRESS; GByteArray *array; GHashTableIter iter; @@ -1515,20 +1510,18 @@ 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, ETH_ALEN, NULL, ETH_ALEN)) { - array = g_byte_array_sized_new (ETH_ALEN); - g_byte_array_append (array, priv->perm_hw_addr, ETH_ALEN); + if (!nm_utils_hwaddr_matches (priv->perm_hw_addr, -1, NULL, ETH_ALEN)) { + array = nm_utils_hwaddr_atoba (priv->perm_hw_addr, ETH_ALEN); g_object_set (s_wired, NM_SETTING_WIRED_MAC_ADDRESS, array, NULL); g_byte_array_unref (array); mac_prop = NULL; - if (mac && !nm_utils_hwaddr_matches (priv->perm_hw_addr, ETH_ALEN, mac, ETH_ALEN)) + if (mac && !nm_utils_hwaddr_matches (priv->perm_hw_addr, -1, mac, -1)) mac_prop = NM_SETTING_WIRED_CLONED_MAC_ADDRESS; } - if (mac_prop && mac && maclen == ETH_ALEN) { - array = g_byte_array_sized_new (ETH_ALEN); - g_byte_array_append (array, (guint8 *) mac, maclen); + if (mac_prop && mac && nm_utils_hwaddr_valid (mac, ETH_ALEN)) { + array = nm_utils_hwaddr_atoba (mac, ETH_ALEN); g_object_set (s_wired, mac_prop, array, NULL); g_byte_array_unref (array); } @@ -1633,6 +1626,7 @@ finalize (GObject *object) NMDeviceEthernet *self = NM_DEVICE_ETHERNET (object); NMDeviceEthernetPrivate *priv = NM_DEVICE_ETHERNET_GET_PRIVATE (self); + g_free (priv->perm_hw_addr); g_clear_object (&priv->supplicant.mgr); g_free (priv->subchan1); g_free (priv->subchan2); @@ -1653,7 +1647,7 @@ get_property (GObject *object, guint prop_id, switch (prop_id) { case PROP_PERM_HW_ADDRESS: - g_value_take_string (value, nm_utils_hwaddr_ntoa (&priv->perm_hw_addr, ETH_ALEN)); + g_value_set_string (value, priv->perm_hw_addr); break; case PROP_SPEED: g_value_set_uint (value, priv->speed); diff --git a/src/devices/nm-device-infiniband.c b/src/devices/nm-device-infiniband.c index 514a36a2a7..05329f3363 100644 --- a/src/devices/nm-device-infiniband.c +++ b/src/devices/nm-device-infiniband.c @@ -209,7 +209,7 @@ check_connection_compatible (NMDevice *device, NMConnection *connection) if (s_infiniband) { mac = nm_setting_infiniband_get_mac_address (s_infiniband); if (mac && !nm_utils_hwaddr_matches (mac->data, mac->len, - nm_device_get_hw_address (device, NULL), INFINIBAND_ALEN)) + nm_device_get_hw_address (device), -1)) return FALSE; } @@ -225,7 +225,7 @@ complete_connection (NMDevice *device, { NMSettingInfiniband *s_infiniband; const GByteArray *setting_mac; - const guint8 *hw_address; + const char *hw_address; nm_utils_complete_generic (connection, NM_SETTING_INFINIBAND_SETTING_NAME, @@ -241,7 +241,7 @@ complete_connection (NMDevice *device, } setting_mac = nm_setting_infiniband_get_mac_address (s_infiniband); - hw_address = nm_device_get_hw_address (device, NULL); + hw_address = nm_device_get_hw_address (device); if (setting_mac) { /* Make sure the setting MAC (if any) matches the device's MAC */ if (!nm_utils_hwaddr_matches (setting_mac->data, setting_mac->len, hw_address, INFINIBAND_ALEN)) { @@ -255,8 +255,7 @@ 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, hw_address, INFINIBAND_ALEN); + mac = nm_utils_hwaddr_atoba (hw_address, INFINIBAND_ALEN); g_object_set (G_OBJECT (s_infiniband), NM_SETTING_INFINIBAND_MAC_ADDRESS, mac, NULL); g_byte_array_free (mac, TRUE); } @@ -271,8 +270,7 @@ static void update_connection (NMDevice *device, NMConnection *connection) { NMSettingInfiniband *s_infiniband = nm_connection_get_setting_infiniband (connection); - guint maclen; - gconstpointer mac = nm_device_get_hw_address (device, &maclen); + const char *mac = nm_device_get_hw_address (device); GByteArray *array; char *mode_path, *contents = NULL; const char *transport_mode = "datagram"; @@ -282,9 +280,8 @@ update_connection (NMDevice *device, NMConnection *connection) nm_connection_add_setting (connection, (NMSetting *) s_infiniband); } - if (mac && !nm_utils_hwaddr_matches (mac, maclen, NULL, INFINIBAND_ALEN)) { - array = g_byte_array_sized_new (maclen); - g_byte_array_append (array, (guint8 *) mac, maclen); + if (mac && !nm_utils_hwaddr_matches (mac, -1, NULL, INFINIBAND_ALEN)) { + array = nm_utils_hwaddr_atoba (mac, INFINIBAND_ALEN); g_object_set (s_infiniband, NM_SETTING_INFINIBAND_MAC_ADDRESS, array, NULL); g_byte_array_unref (array); } diff --git a/src/devices/nm-device-vlan.c b/src/devices/nm-device-vlan.c index b72b78dbd4..65596ae847 100644 --- a/src/devices/nm-device-vlan.c +++ b/src/devices/nm-device-vlan.c @@ -88,12 +88,12 @@ update_initial_hw_address (NMDevice *dev) { NMDeviceVlan *self = NM_DEVICE_VLAN (dev); NMDeviceVlanPrivate *priv = NM_DEVICE_VLAN_GET_PRIVATE (self); - gs_free char *mac_str = NULL; + const char *mac_str; - memcpy (priv->initial_hw_addr, nm_device_get_hw_address (dev, NULL), ETH_ALEN); + mac_str = nm_device_get_hw_address (dev); + nm_utils_hwaddr_aton (mac_str, priv->initial_hw_addr, ETH_ALEN); - _LOGD (LOGD_DEVICE | LOGD_VLAN, "read initial MAC address %s", - (mac_str = nm_utils_hwaddr_ntoa (priv->initial_hw_addr, ETH_ALEN))); + _LOGD (LOGD_DEVICE | LOGD_VLAN, "read initial MAC address %s", mac_str); } static guint32 @@ -158,8 +158,7 @@ match_hwaddr (NMDevice *device, NMConnection *connection, gboolean fail_if_no_hw { NMSettingWired *s_wired; const GByteArray *mac; - const guint8 *device_mac; - guint device_mac_len; + const char *device_mac; s_wired = nm_connection_get_setting_wired (connection); if (!s_wired) @@ -169,9 +168,9 @@ match_hwaddr (NMDevice *device, NMConnection *connection, gboolean fail_if_no_hw if (!mac) return !fail_if_no_hwaddr; - device_mac = nm_device_get_hw_address (device, &device_mac_len); + device_mac = nm_device_get_hw_address (device); - return nm_utils_hwaddr_matches (mac->data, mac->len, device_mac, device_mac_len); + return nm_utils_hwaddr_matches (mac->data, mac->len, device_mac, -1); } static gboolean diff --git a/src/devices/nm-device.c b/src/devices/nm-device.c index 5665270854..277c10ad3a 100644 --- a/src/devices/nm-device.c +++ b/src/devices/nm-device.c @@ -198,7 +198,7 @@ typedef struct { RfKillType rfkill_type; gboolean firmware_missing; GHashTable * available_connections; - guint8 hw_addr[NM_UTILS_HWADDR_LEN_MAX]; + char * hw_addr; guint hw_addr_len; char * physical_port_id; @@ -6892,17 +6892,14 @@ nm_device_get_state (NMDevice *self) /***********************************************************/ /* NMConfigDevice interface related stuff */ -const guint8 * -nm_device_get_hw_address (NMDevice *self, guint *out_len) +const char * +nm_device_get_hw_address (NMDevice *self) { NMDevicePrivate *priv; g_return_val_if_fail (NM_IS_DEVICE (self), NULL); priv = NM_DEVICE_GET_PRIVATE (self); - if (out_len) - *out_len = priv->hw_addr_len; - return priv->hw_addr_len ? priv->hw_addr : NULL; } @@ -6920,19 +6917,16 @@ nm_device_update_hw_address (NMDevice *self) hwaddr = nm_platform_link_get_address (ifindex, &hwaddrlen); g_assert (hwaddrlen <= sizeof (priv->hw_addr)); if (hwaddrlen) { - if (hwaddrlen != priv->hw_addr_len || memcmp (priv->hw_addr, hwaddr, hwaddrlen)) { - gs_free char *tmp_str = NULL; + if (!nm_utils_hwaddr_matches (priv->hw_addr, -1, hwaddr, hwaddrlen)) { + priv->hw_addr = nm_utils_hwaddr_ntoa (hwaddr, hwaddrlen); - memcpy (priv->hw_addr, hwaddr, hwaddrlen); - - _LOGD (LOGD_HW | LOGD_DEVICE, "hardware address now %s", - (tmp_str = nm_utils_hwaddr_ntoa (hwaddr, hwaddrlen))); + _LOGD (LOGD_HW | LOGD_DEVICE, "hardware address now %s", priv->hw_addr); g_object_notify (G_OBJECT (self), NM_DEVICE_HW_ADDRESS); } } else { /* Invalid or no hardware address */ if (priv->hw_addr_len != 0) { - memset (priv->hw_addr, 0, sizeof (priv->hw_addr)); + g_clear_pointer (&priv->hw_addr, g_free); _LOGD (LOGD_HW | LOGD_DEVICE, "previous hardware address is no longer valid"); g_object_notify (G_OBJECT (self), NM_DEVICE_HW_ADDRESS); @@ -6945,30 +6939,30 @@ gboolean nm_device_set_hw_addr (NMDevice *self, const guint8 *addr, const char *detail, guint64 hw_log_domain) { + NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self); char *mac_str = NULL; gboolean success = FALSE; - guint len; - const guint8 *cur_addr = nm_device_get_hw_address (self, &len); + const char *cur_addr = nm_device_get_hw_address (self); g_return_val_if_fail (addr != NULL, FALSE); /* Do nothing if current MAC is same */ - if (cur_addr && nm_utils_hwaddr_matches (cur_addr, len, addr, len)) { + if (cur_addr && nm_utils_hwaddr_matches (cur_addr, -1, addr, priv->hw_addr_len)) { _LOGD (LOGD_DEVICE | hw_log_domain, "no MAC address change needed"); return TRUE; } - mac_str = nm_utils_hwaddr_ntoa (addr, len); + mac_str = nm_utils_hwaddr_ntoa (addr, priv->hw_addr_len); /* Can't change MAC address while device is up */ nm_device_take_down (self, FALSE); - success = nm_platform_link_set_address (nm_device_get_ip_ifindex (self), addr, len); + success = nm_platform_link_set_address (nm_device_get_ip_ifindex (self), addr, priv->hw_addr_len); if (success) { /* MAC address succesfully changed; update the current MAC to match */ nm_device_update_hw_address (self); - cur_addr = nm_device_get_hw_address (self, NULL); - if (nm_utils_hwaddr_matches (cur_addr, len, addr, len)) { + cur_addr = nm_device_get_hw_address (self); + if (cur_addr && nm_utils_hwaddr_matches (cur_addr, -1, addr, priv->hw_addr_len)) { _LOGI (LOGD_DEVICE | hw_log_domain, "%s MAC address to %s", detail, mac_str); } else { @@ -7022,17 +7016,13 @@ static gboolean spec_match_list (NMDevice *self, const GSList *specs) { NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self); - char *hwaddr_str; gboolean matched = FALSE; if (nm_match_spec_string (specs, "*")) return TRUE; - if (priv->hw_addr_len) { - hwaddr_str = nm_utils_hwaddr_ntoa (priv->hw_addr, priv->hw_addr_len); - matched = nm_match_spec_hwaddr (specs, hwaddr_str); - g_free (hwaddr_str); - } + if (priv->hw_addr_len) + matched = nm_match_spec_hwaddr (specs, priv->hw_addr); if (!matched) matched = nm_match_spec_interface_name (specs, nm_device_get_iface (self)); @@ -7267,6 +7257,7 @@ finalize (GObject *object) _LOGD (LOGD_DEVICE, "finalize(): %s", G_OBJECT_TYPE_NAME (self)); + g_free (priv->hw_addr); g_slist_free_full (priv->pending_actions, g_free); g_clear_pointer (&priv->physical_port_id, g_free); g_free (priv->udi); @@ -7380,14 +7371,20 @@ set_property (GObject *object, guint prop_id, count++; } if (count < ETH_ALEN || count > NM_UTILS_HWADDR_LEN_MAX) { - g_warn_if_fail (!hw_addr || *hw_addr == '\0'); + if (hw_addr && *hw_addr) { + _LOGW (LOGD_DEVICE, "ignoring hardware address '%s' with unexpected length %d", + hw_addr, count); + } break; } priv->hw_addr_len = count; - if (!nm_utils_hwaddr_aton (hw_addr, priv->hw_addr, priv->hw_addr_len)) { - g_warning ("Could not parse hw-address '%s'", hw_addr); - memset (priv->hw_addr, 0, sizeof (priv->hw_addr)); + g_free (priv->hw_addr); + if (nm_utils_hwaddr_valid (hw_addr, priv->hw_addr_len)) + priv->hw_addr = g_strdup (hw_addr); + else { + _LOGW (LOGD_DEVICE, "could not parse hw-address '%s'", hw_addr); + priv->hw_addr = NULL; } break; default: @@ -7517,10 +7514,7 @@ get_property (GObject *object, guint prop_id, g_value_set_object (value, priv->master); break; case PROP_HW_ADDRESS: - if (priv->hw_addr_len) - g_value_take_string (value, nm_utils_hwaddr_ntoa (priv->hw_addr, priv->hw_addr_len)); - else - g_value_set_string (value, NULL); + g_value_set_string (value, priv->hw_addr); break; case PROP_HAS_PENDING_ACTION: g_value_set_boolean (value, nm_device_has_pending_action (self)); diff --git a/src/devices/nm-device.h b/src/devices/nm-device.h index 47519f5fa1..17f68efdf5 100644 --- a/src/devices/nm-device.h +++ b/src/devices/nm-device.h @@ -235,7 +235,7 @@ NMDeviceType nm_device_get_device_type (NMDevice *dev); int nm_device_get_priority (NMDevice *dev); -const guint8 * nm_device_get_hw_address (NMDevice *dev, guint *out_len); +const char * nm_device_get_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-olpc-mesh.c b/src/devices/wifi/nm-device-olpc-mesh.c index 8bba6d3866..6113895496 100644 --- a/src/devices/wifi/nm-device-olpc-mesh.c +++ b/src/devices/wifi/nm-device-olpc-mesh.c @@ -341,15 +341,14 @@ static gboolean check_companion (NMDeviceOlpcMesh *self, NMDevice *other) { NMDeviceOlpcMeshPrivate *priv = NM_DEVICE_OLPC_MESH_GET_PRIVATE (self); - const guint8 *my_addr, *their_addr; - guint their_addr_len; + const char *my_addr, *their_addr; if (!NM_IS_DEVICE_WIFI (other)) return FALSE; - my_addr = nm_device_get_hw_address (NM_DEVICE (self), NULL); - their_addr = nm_device_get_hw_address (other, &their_addr_len); - if (!nm_utils_hwaddr_matches (my_addr, ETH_ALEN, their_addr, their_addr_len)) + my_addr = nm_device_get_hw_address (NM_DEVICE (self)); + their_addr = nm_device_get_hw_address (other); + if (!nm_utils_hwaddr_matches (my_addr, -1, their_addr, -1)) return FALSE; g_assert (priv->companion == NULL); diff --git a/src/devices/wifi/nm-device-wifi.c b/src/devices/wifi/nm-device-wifi.c index 2a8e1870c3..58ff2ab306 100644 --- a/src/devices/wifi/nm-device-wifi.c +++ b/src/devices/wifi/nm-device-wifi.c @@ -116,7 +116,7 @@ static guint signals[LAST_SIGNAL] = { 0 }; struct _NMDeviceWifiPrivate { gboolean disposed; - guint8 perm_hw_addr[ETH_ALEN]; /* Permanent MAC address */ + char * perm_hw_addr; /* Permanent MAC address */ guint8 initial_hw_addr[ETH_ALEN]; /* Initial MAC address (as seen when NM starts) */ gint8 invalid_strength_counter; @@ -828,7 +828,7 @@ check_connection_compatible (NMDevice *device, NMConnection *connection) return FALSE; mac = nm_setting_wireless_get_mac_address (s_wireless); - if (mac && !nm_utils_hwaddr_matches (mac->data, mac->len, priv->perm_hw_addr, ETH_ALEN)) + if (mac && !nm_utils_hwaddr_matches (mac->data, mac->len, priv->perm_hw_addr, -1)) return FALSE; /* Check for MAC address blacklist */ @@ -842,7 +842,7 @@ check_connection_compatible (NMDevice *device, NMConnection *connection) return FALSE; } - if (nm_utils_hwaddr_matches (addr, ETH_ALEN, priv->perm_hw_addr, ETH_ALEN)) + if (nm_utils_hwaddr_matches (addr, ETH_ALEN, priv->perm_hw_addr, -1)) return FALSE; } @@ -1120,7 +1120,7 @@ complete_connection (NMDevice *device, 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->data, setting_mac->len, priv->perm_hw_addr, ETH_ALEN)) { + if (!nm_utils_hwaddr_matches (setting_mac->data, setting_mac->len, priv->perm_hw_addr, -1)) { g_set_error (error, NM_SETTING_WIRELESS_ERROR, NM_SETTING_WIRELESS_ERROR_INVALID_PROPERTY, @@ -1129,14 +1129,15 @@ complete_connection (NMDevice *device, } } else { GByteArray *mac; + guint8 perm_hw_addr[ETH_ALEN]; /* Lock the connection to this device by default if it uses a * permanent MAC address (ie not a 'locally administered' one) */ - if ( !(priv->perm_hw_addr[0] & 0x02) - && !nm_utils_hwaddr_matches (priv->perm_hw_addr, ETH_ALEN, NULL, ETH_ALEN)) { - mac = g_byte_array_sized_new (ETH_ALEN); - g_byte_array_append (mac, priv->perm_hw_addr, ETH_ALEN); + 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)) { + mac = nm_utils_hwaddr_atoba (priv->perm_hw_addr, ETH_ALEN); g_object_set (G_OBJECT (s_wifi), NM_SETTING_WIRELESS_MAC_ADDRESS, mac, NULL); g_byte_array_free (mac, TRUE); } @@ -2513,6 +2514,8 @@ update_permanent_hw_address (NMDevice *device) 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."); @@ -2535,13 +2538,10 @@ update_permanent_hw_address (NMDevice *device) _LOGD (LOGD_HW | LOGD_ETHER, "unable to read permanent MAC address (error %d)", errsv); /* Fall back to current address */ - memcpy (epaddr->data, nm_device_get_hw_address (device, NULL), ETH_ALEN); + nm_utils_hwaddr_aton (nm_device_get_hw_address (device), epaddr->data, ETH_ALEN); } - if (!nm_utils_hwaddr_matches (priv->perm_hw_addr, ETH_ALEN, epaddr->data, ETH_ALEN)) { - memcpy (priv->perm_hw_addr, epaddr->data, ETH_ALEN); - g_object_notify (G_OBJECT (device), NM_DEVICE_WIFI_PERMANENT_HW_ADDRESS); - } + priv->perm_hw_addr = nm_utils_hwaddr_ntoa (epaddr->data, ETH_ALEN); g_free (epaddr); close (fd); @@ -2552,16 +2552,15 @@ update_initial_hw_address (NMDevice *device) { NMDeviceWifi *self = NM_DEVICE_WIFI (device); NMDeviceWifiPrivate *priv = NM_DEVICE_WIFI_GET_PRIVATE (self); - char *mac_str = NULL; + const char *mac_str; /* This sets initial MAC address from current MAC address. It should only * be called from NMDevice constructor() to really get the initial address. */ - memcpy (priv->initial_hw_addr, nm_device_get_hw_address (device, NULL), ETH_ALEN); + mac_str = nm_device_get_hw_address (device); + nm_utils_hwaddr_aton (mac_str, priv->initial_hw_addr, ETH_ALEN); - _LOGD (LOGD_DEVICE | LOGD_ETHER, "read initial MAC address %s", - (mac_str = nm_utils_hwaddr_ntoa (priv->initial_hw_addr, ETH_ALEN))); - g_free (mac_str); + _LOGD (LOGD_DEVICE | LOGD_ETHER, "read initial MAC address %s", mac_str); } static NMActStageReturn @@ -2655,8 +2654,12 @@ act_stage1_prepare (NMDevice *device, NMDeviceStateReason *reason) if (nm_ap_get_mode (ap) == NM_802_11_MODE_INFRA) nm_ap_set_broadcast (ap, FALSE); - else if (nm_ap_is_hotspot (ap)) - nm_ap_set_address (ap, nm_device_get_hw_address (device, NULL)); + else if (nm_ap_is_hotspot (ap)) { + guint8 addr[ETH_ALEN]; + + nm_utils_hwaddr_aton (nm_device_get_hw_address (device), addr, ETH_ALEN); + nm_ap_set_address (ap, addr); + } priv->ap_list = g_slist_prepend (priv->ap_list, ap); nm_ap_export_to_dbus (ap); @@ -3246,6 +3249,17 @@ dispose (GObject *object) G_OBJECT_CLASS (nm_device_wifi_parent_class)->dispose (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_OBJECT_CLASS (nm_device_wifi_parent_class)->finalize (object); +} + static void get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) @@ -3257,7 +3271,7 @@ get_property (GObject *object, guint prop_id, switch (prop_id) { case PROP_PERM_HW_ADDRESS: - g_value_take_string (value, nm_utils_hwaddr_ntoa (priv->perm_hw_addr, ETH_ALEN)); + g_value_set_string (value, priv->perm_hw_addr); break; case PROP_MODE: g_value_set_uint (value, priv->mode); @@ -3313,6 +3327,7 @@ 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; diff --git a/src/devices/wimax/nm-device-wimax.c b/src/devices/wimax/nm-device-wimax.c index 4ad9986787..fad1bb5401 100644 --- a/src/devices/wimax/nm-device-wimax.c +++ b/src/devices/wimax/nm-device-wimax.c @@ -332,7 +332,7 @@ check_connection_compatible (NMDevice *device, NMConnection *connection) return FALSE; mac = nm_setting_wimax_get_mac_address (s_wimax); - if (mac && !nm_utils_hwaddr_matches (mac->data, mac->len, nm_device_get_hw_address (device, NULL), ETH_ALEN)) + if (mac && !nm_utils_hwaddr_matches (mac->data, mac->len, nm_device_get_hw_address (device), -1)) return FALSE; return TRUE; @@ -372,7 +372,7 @@ complete_connection (NMDevice *device, NMDeviceWimaxPrivate *priv = NM_DEVICE_WIMAX_GET_PRIVATE (self); NMSettingWimax *s_wimax; const GByteArray *setting_mac; - const guint8 *hw_address; + const char *hw_address; char *format; const char *nsp_name = NULL; NMWimaxNsp *nsp = NULL; @@ -449,10 +449,10 @@ complete_connection (NMDevice *device, g_object_set (G_OBJECT (s_wimax), NM_SETTING_WIMAX_NETWORK_NAME, nsp_name, NULL); setting_mac = nm_setting_wimax_get_mac_address (s_wimax); - hw_address = nm_device_get_hw_address (device, NULL); + hw_address = nm_device_get_hw_address (device); if (setting_mac) { /* Make sure the setting MAC (if any) matches the device's permanent MAC */ - if (!nm_utils_hwaddr_matches (setting_mac->data, setting_mac->len, hw_address, ETH_ALEN)) { + if (!nm_utils_hwaddr_matches (setting_mac->data, setting_mac->len, hw_address, -1)) { g_set_error (error, NM_SETTING_WIMAX_ERROR, NM_SETTING_WIMAX_ERROR_INVALID_PROPERTY, @@ -463,9 +463,8 @@ complete_connection (NMDevice *device, GByteArray *mac; /* Lock the connection to this device by default */ - if (!nm_utils_hwaddr_matches (hw_address, ETH_ALEN, NULL, ETH_ALEN)) { - mac = g_byte_array_sized_new (ETH_ALEN); - g_byte_array_append (mac, hw_address, ETH_ALEN); + if (!nm_utils_hwaddr_matches (hw_address, -1, NULL, ETH_ALEN)) { + mac = nm_utils_hwaddr_atoba (hw_address, ETH_ALEN); g_object_set (G_OBJECT (s_wimax), NM_SETTING_WIMAX_MAC_ADDRESS, mac, NULL); g_byte_array_free (mac, TRUE); } diff --git a/src/nm-config.c b/src/nm-config.c index cabb10ea32..8fc78602bc 100644 --- a/src/nm-config.c +++ b/src/nm-config.c @@ -260,9 +260,7 @@ void nm_config_set_ethernet_no_auto_default (NMConfig *config, NMDevice *device) { NMConfigPrivate *priv = NM_CONFIG_GET_PRIVATE (config); - const guint8 *hwaddr; - guint hwaddr_len; - char *current, *hwaddr_str; + char *current; GString *updated; GError *error = NULL; @@ -277,10 +275,7 @@ nm_config_set_ethernet_no_auto_default (NMConfig *config, NMDevice *device) g_string_append_c (updated, '\n'); } - hwaddr = nm_device_get_hw_address (device, &hwaddr_len); - hwaddr_str = nm_utils_hwaddr_ntoa (hwaddr, hwaddr_len); - g_string_append (updated, hwaddr_str); - g_free (hwaddr_str); + g_string_append (updated, nm_device_get_hw_address (device)); g_string_append_c (updated, '\n'); if (!g_file_set_contents (priv->no_auto_default_file, updated->str, updated->len, &error)) { diff --git a/src/nm-manager.c b/src/nm-manager.c index 34048b789d..b829c4c018 100644 --- a/src/nm-manager.c +++ b/src/nm-manager.c @@ -845,8 +845,7 @@ static NMDevice * get_device_from_hwaddr (NMManager *self, const GByteArray *setting_mac) { NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self); - const guint8 *device_mac; - guint device_mac_len; + const char *device_mac; GSList *iter; if (!setting_mac) @@ -855,8 +854,10 @@ get_device_from_hwaddr (NMManager *self, const GByteArray *setting_mac) for (iter = priv->devices; iter; iter = g_slist_next (iter)) { NMDevice *device = iter->data; - device_mac = nm_device_get_hw_address (iter->data, &device_mac_len); - if (nm_utils_hwaddr_matches (setting_mac->data, setting_mac->len, device_mac, device_mac_len)) + device_mac = nm_device_get_hw_address (iter->data); + if (!device_mac) + continue; + if (nm_utils_hwaddr_matches (setting_mac->data, setting_mac->len, device_mac, -1)) return device; } return NULL; diff --git a/src/settings/nm-settings.c b/src/settings/nm-settings.c index a9b2a829c5..1b88055c9d 100644 --- a/src/settings/nm-settings.c +++ b/src/settings/nm-settings.c @@ -1472,12 +1472,11 @@ have_connection_for_device (NMSettings *self, NMDevice *device) NMSettingConnection *s_con; NMSettingWired *s_wired; const GByteArray *setting_mac; - const guint8 *hwaddr; - guint hwaddr_len = 0; + const char *hwaddr; g_return_val_if_fail (NM_IS_SETTINGS (self), FALSE); - hwaddr = nm_device_get_hw_address (device, &hwaddr_len); + hwaddr = nm_device_get_hw_address (device); /* Find a wired connection locked to the given MAC address, if any */ g_hash_table_iter_init (&iter, priv->connections); @@ -1506,9 +1505,9 @@ have_connection_for_device (NMSettings *self, NMDevice *device) g_assert (s_wired != NULL); setting_mac = nm_setting_wired_get_mac_address (s_wired); - if (setting_mac) { + if (setting_mac && hwaddr) { /* A connection mac-locked to this device */ - if (nm_utils_hwaddr_matches (setting_mac->data, setting_mac->len, hwaddr, hwaddr_len)) + if (nm_utils_hwaddr_matches (setting_mac->data, setting_mac->len, hwaddr, -1)) return TRUE; } else { /* A connection that applies to any wired device */ @@ -1590,9 +1589,8 @@ nm_settings_device_added (NMSettings *self, NMDevice *device) NMSettingsConnection *added; NMSetting *setting; GError *error = NULL; - const guint8 *hw_address; + const char *hw_address; char *defname, *uuid; - guint len = 0; GByteArray *mac; if (!NM_IS_DEVICE_ETHERNET (device)) @@ -1607,7 +1605,7 @@ nm_settings_device_added (NMSettings *self, NMDevice *device) || !nm_config_get_ethernet_can_auto_default (priv->config, device)) return; - hw_address = nm_device_get_hw_address (device, &len); + hw_address = nm_device_get_hw_address (device); if (!hw_address) return; @@ -1633,8 +1631,7 @@ nm_settings_device_added (NMSettings *self, NMDevice *device) setting = nm_setting_wired_new (); nm_connection_add_setting (connection, setting); - mac = g_byte_array_sized_new (len); - g_byte_array_append (mac, hw_address, len); + mac = nm_utils_hwaddr_atoba (hw_address, ETH_ALEN); g_object_set (setting, NM_SETTING_WIRED_MAC_ADDRESS, mac, NULL); g_byte_array_unref (mac);