From 11d0f68b2313f671cc245e2348939817a0191a6e Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Wed, 13 Jun 2012 12:53:39 -0500 Subject: [PATCH] core: add generic way of getting device hardware addresses --- src/nm-device-adsl.c | 20 ++++++++++++++++++++ src/nm-device-bt.c | 21 +++++++++++++-------- src/nm-device-bt.h | 2 -- src/nm-device-olpc-mesh.c | 18 ++++++++++++++---- src/nm-device-vlan.c | 10 ++++++++++ src/nm-device-wifi.c | 29 ++++++++++------------------- src/nm-device-wifi.h | 2 -- src/nm-device-wired.c | 10 ++++++++++ src/nm-device.c | 12 ++++++++++++ src/nm-device.h | 3 +++ src/wimax/nm-device-wimax.c | 10 ++++++++++ 11 files changed, 102 insertions(+), 35 deletions(-) diff --git a/src/nm-device-adsl.c b/src/nm-device-adsl.c index 29d30cd01..ac10d9cf9 100644 --- a/src/nm-device-adsl.c +++ b/src/nm-device-adsl.c @@ -82,6 +82,7 @@ typedef struct { int brfd; int nas_ifindex; char * nas_ifname; + guint8 nas_hw_addr[ETH_ALEN]; } NMDeviceAdslPrivate; enum { @@ -233,6 +234,7 @@ static void set_nas_iface (NMDeviceAdsl *self, int idx, const char *name) { NMDeviceAdslPrivate *priv = NM_DEVICE_ADSL_GET_PRIVATE (self); + gsize addrlen; g_return_if_fail (name != NULL); @@ -245,6 +247,13 @@ set_nas_iface (NMDeviceAdsl *self, int idx, const char *name) g_warn_if_fail (priv->nas_ifname == NULL); priv->nas_ifname = g_strdup (name); + + /* Update NAS interface's MAC address */ + addrlen = nm_device_read_hwaddr (NM_DEVICE (self), + priv->nas_hw_addr, + sizeof (priv->nas_hw_addr), + NULL); + g_warn_if_fail (addrlen == sizeof (priv->nas_hw_addr)); } static gboolean @@ -591,10 +600,20 @@ deactivate (NMDevice *device) priv->nas_ifindex = -1; g_free (priv->nas_ifname); priv->nas_ifname = NULL; + memset (priv->nas_hw_addr, 0, sizeof (priv->nas_hw_addr)); } /**************************************************************/ +static const guint8 * +get_hw_address (NMDevice *device, guint *out_len) +{ + NMDeviceAdslPrivate *priv = NM_DEVICE_ADSL_GET_PRIVATE (device); + + *out_len = priv->nas_ifname ? sizeof (priv->nas_hw_addr) : 0; + return priv->nas_hw_addr; +} + static void set_carrier (NMDeviceAdsl *self, const gboolean carrier) { @@ -818,6 +837,7 @@ nm_device_adsl_class_init (NMDeviceAdslClass *klass) parent_class->get_best_auto_connection = get_best_auto_connection; parent_class->complete_connection = complete_connection; + parent_class->get_hw_address = get_hw_address; parent_class->act_stage2_config = act_stage2_config; parent_class->act_stage3_ip4_config_start = act_stage3_ip4_config_start; parent_class->deactivate = deactivate; diff --git a/src/nm-device-bt.c b/src/nm-device-bt.c index cadedcf7b..448ec87c2 100644 --- a/src/nm-device-bt.c +++ b/src/nm-device-bt.c @@ -63,6 +63,7 @@ typedef struct { guint mm_watch_id; gboolean mm_running; + guint8 hw_addr[ETH_ALEN]; /* binary representation of bdaddr */ char *bdaddr; char *name; guint32 capabilities; @@ -117,14 +118,6 @@ guint32 nm_device_bt_get_capabilities (NMDeviceBt *self) return NM_DEVICE_BT_GET_PRIVATE (self)->capabilities; } -const char *nm_device_bt_get_hw_address (NMDeviceBt *self) -{ - g_return_val_if_fail (self != NULL, NULL); - g_return_val_if_fail (NM_IS_DEVICE_BT (self), NULL); - - return NM_DEVICE_BT_GET_PRIVATE (self)->bdaddr; -} - static guint32 get_connection_bt_type (NMConnection *connection) { @@ -401,6 +394,15 @@ get_generic_capabilities (NMDevice *dev) return NM_DEVICE_CAP_NM_SUPPORTED; } +static const guint8 * +get_hw_address (NMDevice *device, guint *out_len) +{ + NMDeviceBtPrivate *priv = NM_DEVICE_BT_GET_PRIVATE (device); + + *out_len = sizeof (priv->hw_addr); + return priv->hw_addr; +} + static gboolean hwaddr_matches (NMDevice *device, NMConnection *connection, @@ -1231,6 +1233,8 @@ set_property (GObject *object, guint prop_id, case PROP_HW_ADDRESS: /* Construct only */ priv->bdaddr = g_ascii_strup (g_value_get_string (value), -1); + if (!nm_utils_hwaddr_aton (priv->bdaddr, ARPHRD_ETHER, &priv->hw_addr)) + nm_log_err (LOGD_HW, "Failed to convert BT address '%s'", priv->bdaddr); break; case PROP_BT_NAME: /* Construct only */ @@ -1326,6 +1330,7 @@ nm_device_bt_class_init (NMDeviceBtClass *klass) device_class->check_connection_available = check_connection_available; device_class->complete_connection = complete_connection; device_class->hwaddr_matches = hwaddr_matches; + device_class->get_hw_address = get_hw_address; device_class->is_available = is_available; device_class->state_changed = device_state_changed; diff --git a/src/nm-device-bt.h b/src/nm-device-bt.h index 0d54d6079..9382ee254 100644 --- a/src/nm-device-bt.h +++ b/src/nm-device-bt.h @@ -65,8 +65,6 @@ NMDevice *nm_device_bt_new (const char *udi, guint32 nm_device_bt_get_capabilities (NMDeviceBt *device); -const char *nm_device_bt_get_hw_address (NMDeviceBt *device); - gboolean nm_device_bt_modem_added (NMDeviceBt *device, NMModem *modem, const char *driver); diff --git a/src/nm-device-olpc-mesh.c b/src/nm-device-olpc-mesh.c index 93281dcf7..1417736fb 100644 --- a/src/nm-device-olpc-mesh.c +++ b/src/nm-device-olpc-mesh.c @@ -315,6 +315,14 @@ update_hw_address (NMDevice *dev) } } +static const guint8 * +get_hw_address (NMDevice *device, guint *out_len) +{ + NMDeviceOlpcMeshPrivate *priv = NM_DEVICE_OLPC_MESH_GET_PRIVATE (device); + + *out_len = sizeof (priv->hw_addr); + return priv->hw_addr; +} static NMActStageReturn act_stage1_prepare (NMDevice *dev, NMDeviceStateReason *reason) @@ -505,6 +513,7 @@ nm_device_olpc_mesh_class_init (NMDeviceOlpcMeshClass *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->check_connection_compatible = check_connection_compatible; parent_class->complete_connection = complete_connection; @@ -616,15 +625,16 @@ static gboolean is_companion (NMDeviceOlpcMesh *self, NMDevice *other) { NMDeviceOlpcMeshPrivate *priv = NM_DEVICE_OLPC_MESH_GET_PRIVATE (self); - struct ether_addr their_addr; + const guint8 *their_addr; + guint their_addr_len = 0; NMManager *manager; if (!NM_IS_DEVICE_WIFI (other)) return FALSE; - nm_device_wifi_get_address (NM_DEVICE_WIFI (other), &their_addr); - - if (memcmp (priv->hw_addr, their_addr.ether_addr_octet, ETH_ALEN) != 0) + their_addr = nm_device_get_hw_address (other, &their_addr_len); + if ( (their_addr_len != ETH_ALEN) + || (memcmp (priv->hw_addr, their_addr, ETH_ALEN) != 0)) return FALSE; priv->companion = other; diff --git a/src/nm-device-vlan.c b/src/nm-device-vlan.c index c8b777022..8bbc0f0ee 100644 --- a/src/nm-device-vlan.c +++ b/src/nm-device-vlan.c @@ -172,6 +172,15 @@ update_hw_address (NMDevice *dev) } } +static const guint8 * +get_hw_address (NMDevice *device, guint *out_len) +{ + NMDeviceVlanPrivate *priv = NM_DEVICE_VLAN_GET_PRIVATE (device); + + *out_len = priv->hw_addr_len; + return priv->hw_addr; +} + static gboolean can_interrupt_activation (NMDevice *dev) { @@ -724,6 +733,7 @@ nm_device_vlan_class_init (NMDeviceVlanClass *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->hw_bring_up = hw_bring_up; parent_class->can_interrupt_activation = can_interrupt_activation; parent_class->is_available = is_available; diff --git a/src/nm-device-wifi.c b/src/nm-device-wifi.c index fe85150f5..e7d5b8c5e 100644 --- a/src/nm-device-wifi.c +++ b/src/nm-device-wifi.c @@ -1460,25 +1460,6 @@ get_best_auto_connection (NMDevice *dev, return NULL; } -/* - * nm_device_wifi_get_address - * - * Get a device's hardware address - * - */ -void -nm_device_wifi_get_address (NMDeviceWifi *self, - struct ether_addr *addr) -{ - NMDeviceWifiPrivate *priv; - - g_return_if_fail (self != NULL); - g_return_if_fail (addr != NULL); - - priv = NM_DEVICE_WIFI_GET_PRIVATE (self); - memcpy (addr, &priv->hw_addr, sizeof (struct ether_addr)); -} - static void ap_list_dump (NMDeviceWifi *self) { @@ -2936,6 +2917,15 @@ update_initial_hw_address (NMDevice *dev) g_free (mac_str); } +static const guint8 * +get_hw_address (NMDevice *device, guint *out_len) +{ + NMDeviceWifiPrivate *priv = NM_DEVICE_WIFI_GET_PRIVATE (device); + + *out_len = ETH_ALEN; + return priv->hw_addr; +} + static NMActStageReturn act_stage1_prepare (NMDevice *dev, NMDeviceStateReason *reason) { @@ -3761,6 +3751,7 @@ nm_device_wifi_class_init (NMDeviceWifiClass *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-wifi.h b/src/nm-device-wifi.h index 26d66ffcf..425b85251 100644 --- a/src/nm-device-wifi.h +++ b/src/nm-device-wifi.h @@ -94,8 +94,6 @@ NMDevice *nm_device_wifi_new (const char *udi, const char *iface, const char *driver); -void nm_device_wifi_get_address (NMDeviceWifi *dev, struct ether_addr *addr); - NMAccessPoint * nm_device_wifi_get_activation_ap (NMDeviceWifi *self); RfKillState nm_device_wifi_get_ipw_rfkill_state (NMDeviceWifi *self); diff --git a/src/nm-device-wired.c b/src/nm-device-wired.c index 4440c6971..c8c0c58c3 100644 --- a/src/nm-device-wired.c +++ b/src/nm-device-wired.c @@ -387,6 +387,15 @@ update_hw_address (NMDevice *dev) } } +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) { @@ -472,6 +481,7 @@ 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; } diff --git a/src/nm-device.c b/src/nm-device.c index c43af9b01..a7321fd90 100644 --- a/src/nm-device.c +++ b/src/nm-device.c @@ -611,6 +611,18 @@ nm_device_set_ip_iface (NMDevice *self, const char *iface) g_free (old_ip_iface); } +const guint8 * +nm_device_get_hw_address (NMDevice *dev, guint *out_len) +{ + g_return_val_if_fail (dev != NULL, NULL); + g_return_val_if_fail (NM_IS_DEVICE (dev), NULL); + g_return_val_if_fail (out_len != NULL, NULL); + g_return_val_if_fail (*out_len == 0, NULL); + + if (NM_DEVICE_GET_CLASS (dev)->get_hw_address) + return NM_DEVICE_GET_CLASS (dev)->get_hw_address (dev, out_len); + return NULL; +} /* * Get/set functions for driver diff --git a/src/nm-device.h b/src/nm-device.h index 05b396225..9b2a7f54f 100644 --- a/src/nm-device.h +++ b/src/nm-device.h @@ -112,6 +112,7 @@ typedef struct { void (* update_hw_address) (NMDevice *self); void (* update_permanent_hw_address) (NMDevice *self); void (* update_initial_hw_address) (NMDevice *self); + const guint8 * (* get_hw_address) (NMDevice *self, guint *out_len); guint32 (* get_type_capabilities) (NMDevice *self); guint32 (* get_generic_capabilities) (NMDevice *self); @@ -212,6 +213,8 @@ 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); + NMDHCP4Config * nm_device_get_dhcp4_config (NMDevice *dev); NMDHCP6Config * nm_device_get_dhcp6_config (NMDevice *dev); diff --git a/src/wimax/nm-device-wimax.c b/src/wimax/nm-device-wimax.c index d297d7c05..3e2e2833b 100644 --- a/src/wimax/nm-device-wimax.c +++ b/src/wimax/nm-device-wimax.c @@ -370,6 +370,15 @@ update_hw_address (NMDevice *dev) } } +static const guint8 * +get_hw_address (NMDevice *device, guint *out_len) +{ + NMDeviceWimaxPrivate *priv = NM_DEVICE_WIMAX_GET_PRIVATE (device); + + *out_len = sizeof (priv->hw_addr); + return priv->hw_addr; +} + static gboolean hwaddr_matches (NMDevice *device, NMConnection *connection, @@ -1477,6 +1486,7 @@ nm_device_wimax_class_init (NMDeviceWimaxClass *klass) device_class->take_down = take_down; device_class->hw_bring_up = hw_bring_up; device_class->update_hw_address = update_hw_address; + device_class->get_hw_address = get_hw_address; device_class->check_connection_compatible = check_connection_compatible; device_class->check_connection_available = check_connection_available; device_class->complete_connection = complete_connection;