From 6ca2852f6560039e74d1c7299ffe9ac1833910d8 Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Fri, 1 Jan 2016 19:38:59 +0100 Subject: [PATCH] move mac address validation to core Signed-off-by: Felix Fietkau --- eeprom.c | 13 +++++++++++-- mac80211.c | 1 + mt76.h | 11 ++--------- mt7603.h | 1 - mt7603_init.c | 6 +++--- mt76x2.h | 1 - mt76x2_eeprom.c | 12 ++---------- mt76x2_init.c | 14 ++++++-------- mt76x2_main.c | 2 +- 9 files changed, 26 insertions(+), 35 deletions(-) diff --git a/eeprom.c b/eeprom.c index 00e0199c..f4d7659f 100644 --- a/eeprom.c +++ b/eeprom.c @@ -13,6 +13,7 @@ #include #include #include +#include #include "mt76.h" static int @@ -70,10 +71,10 @@ mt76_get_of_eeprom(struct mt76_dev *dev, int len) #endif } -#ifdef CONFIG_OF void mt76_eeprom_override(struct mt76_dev *dev) { +#ifdef CONFIG_OF struct device_node *np = dev->dev->of_node; const __be32 *val; int size; @@ -88,9 +89,17 @@ mt76_eeprom_override(struct mt76_dev *dev) val = of_get_property(np, "mediatek,5ghz", &size); if (val) dev->cap.has_5ghz = be32_to_cpup(val); +#endif + + if (!is_valid_ether_addr(dev->macaddr)) { + eth_random_addr(dev->macaddr); + dev_printk(KERN_INFO, dev->dev, + "Invalid MAC address, using random address %pM\n", + dev->macaddr); + } + } EXPORT_SYMBOL_GPL(mt76_eeprom_override); -#endif int mt76_eeprom_init(struct mt76_dev *dev, int len) diff --git a/mac80211.c b/mac80211.c index 7fbeaa85..c777c6fa 100644 --- a/mac80211.c +++ b/mac80211.c @@ -170,6 +170,7 @@ int mt76_register_device(struct mt76_dev *dev, bool vht, INIT_LIST_HEAD(&dev->txwi_cache); SET_IEEE80211_DEV(hw, dev->dev); + SET_IEEE80211_PERM_ADDR(hw, dev->macaddr); wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) | diff --git a/mt76.h b/mt76.h index 0f53f520..75320ac0 100644 --- a/mt76.h +++ b/mt76.h @@ -162,6 +162,7 @@ struct mt76_dev { struct mt76_queue q_tx[__MT_TXQ_MAX]; const struct mt76_queue_ops *queue_ops; + u8 macaddr[ETH_ALEN]; u32 rev; unsigned long state; @@ -229,16 +230,8 @@ void mt76_unregister_device(struct mt76_dev *dev); struct dentry *mt76_register_debugfs(struct mt76_dev *dev); - -#ifdef CONFIG_OF -void mt76_eeprom_override(struct mt76_dev *dev); -#else -static inline void mt76_eeprom_override(struct mt76_dev *dev) -{ -} -#endif - int mt76_eeprom_init(struct mt76_dev *dev, int len); +void mt76_eeprom_override(struct mt76_dev *dev); static inline struct ieee80211_txq * mtxq_to_txq(struct mt76_txq *mtxq) diff --git a/mt7603.h b/mt7603.h index 85ccec5a..a43124ae 100644 --- a/mt7603.h +++ b/mt7603.h @@ -77,7 +77,6 @@ struct mt7603_dev { u8 tx_chains; u8 rssi_offset[3]; - u8 macaddr[ETH_ALEN]; struct mt7603_mcu mcu; struct mt76_queue q_rx; diff --git a/mt7603_init.c b/mt7603_init.c index be6a07bd..08fbb711 100644 --- a/mt7603_init.c +++ b/mt7603_init.c @@ -229,7 +229,9 @@ mt7603_init_hardware(struct mt7603_dev *dev) return ret; dev->mt76.cap.has_2ghz = true; - memcpy(dev->macaddr, dev->mt76.eeprom.data + MT_EE_MAC_ADDR, ETH_ALEN); + memcpy(dev->mt76.macaddr, dev->mt76.eeprom.data + MT_EE_MAC_ADDR, + ETH_ALEN); + mt76_eeprom_override(&dev->mt76); ret = mt7603_dma_init(dev); if (ret) @@ -331,8 +333,6 @@ int mt7603_register_device(struct mt7603_dev *dev) hw->sta_data_size = sizeof(struct mt7603_sta); hw->vif_data_size = sizeof(struct mt7603_vif); - SET_IEEE80211_PERM_ADDR(hw, dev->macaddr); - wiphy->iface_combinations = if_comb; wiphy->n_iface_combinations = ARRAY_SIZE(if_comb); diff --git a/mt76x2.h b/mt76x2.h index 07dfc45a..6325abc0 100644 --- a/mt76x2.h +++ b/mt76x2.h @@ -89,7 +89,6 @@ struct mt76x2_rate_power { struct mt76x2_dev { struct mt76_dev mt76; /* must be first */ - u8 macaddr[ETH_ALEN]; struct mac_address macaddr_list[8]; struct mutex mutex; diff --git a/mt76x2_eeprom.c b/mt76x2_eeprom.c index 5f8d191e..bd6ef259 100644 --- a/mt76x2_eeprom.c +++ b/mt76x2_eeprom.c @@ -11,7 +11,6 @@ * GNU General Public License for more details. */ -#include #include #include "mt76x2.h" #include "mt76x2_eeprom.h" @@ -34,7 +33,7 @@ mt76x2_eeprom_get_macaddr(struct mt76x2_dev *dev) { void *src = dev->mt76.eeprom.data + MT_EE_MAC_ADDR; - memcpy(dev->macaddr, src, ETH_ALEN); + memcpy(dev->mt76.macaddr, src, ETH_ALEN); return 0; } @@ -617,15 +616,8 @@ int mt76x2_eeprom_init(struct mt76x2_dev *dev) return ret; mt76x2_eeprom_parse_hw_cap(dev); - mt76_eeprom_override(&dev->mt76); - mt76x2_eeprom_get_macaddr(dev); - if (!is_valid_ether_addr(dev->macaddr)) { - eth_random_addr(dev->macaddr); - dev_printk(KERN_INFO, dev->mt76.dev, - "Invalid MAC address, using random address %pM\n", - dev->macaddr); - } + mt76_eeprom_override(&dev->mt76); return 0; } diff --git a/mt76x2_init.c b/mt76x2_init.c index dfe46d9d..d0302714 100644 --- a/mt76x2_init.c +++ b/mt76x2_init.c @@ -185,6 +185,7 @@ mt76x2_init_beacon_offsets(struct mt76x2_dev *dev) int mt76x2_mac_reset(struct mt76x2_dev *dev, bool hard) { static const u8 null_addr[ETH_ALEN] = {}; + const u8 *macaddr = dev->mt76.macaddr; u32 val; int i, k; @@ -224,11 +225,11 @@ int mt76x2_mac_reset(struct mt76x2_dev *dev, bool hard) mt76_wr(dev, MT_MCU_CLOCK_CTL, 0x1401); mt76_clear(dev, MT_FCE_L2_STUFF, MT_FCE_L2_STUFF_WR_MPDU_LEN_EN); - mt76_wr(dev, MT_MAC_ADDR_DW0, get_unaligned_le32(dev->macaddr)); - mt76_wr(dev, MT_MAC_ADDR_DW1, get_unaligned_le16(dev->macaddr + 4)); + mt76_wr(dev, MT_MAC_ADDR_DW0, get_unaligned_le32(macaddr)); + mt76_wr(dev, MT_MAC_ADDR_DW1, get_unaligned_le16(macaddr + 4)); - mt76_wr(dev, MT_MAC_BSSID_DW0, get_unaligned_le32(dev->macaddr)); - mt76_wr(dev, MT_MAC_BSSID_DW1, get_unaligned_le16(dev->macaddr + 4) | + mt76_wr(dev, MT_MAC_BSSID_DW0, get_unaligned_le32(macaddr)); + mt76_wr(dev, MT_MAC_BSSID_DW1, get_unaligned_le16(macaddr + 4) | MT76_SET(MT_MAC_BSSID_DW1_MBSS_MODE, 3) | /* 8 beacons */ MT_MAC_BSSID_DW1_MBSS_LOCAL_BIT); @@ -662,12 +663,9 @@ int mt76x2_register_device(struct mt76x2_dev *dev) hw->sta_data_size = sizeof(struct mt76x2_sta); hw->vif_data_size = sizeof(struct mt76x2_vif); - dev->macaddr[0] &= ~BIT(1); - SET_IEEE80211_PERM_ADDR(hw, dev->macaddr); - for (i = 0; i < ARRAY_SIZE(dev->macaddr_list); i++) { u8 *addr = dev->macaddr_list[i].addr; - memcpy(addr, dev->macaddr, ETH_ALEN); + memcpy(addr, dev->mt76.macaddr, ETH_ALEN); if (!i) continue; diff --git a/mt76x2_main.c b/mt76x2_main.c index 326f5bba..a586d7bd 100644 --- a/mt76x2_main.c +++ b/mt76x2_main.c @@ -61,7 +61,7 @@ mt76x2_add_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif) int ret = 0; if (vif->addr[0] & BIT(1)) - idx = 1 + (((dev->macaddr[0] ^ vif->addr[0]) >> 2) & 7); + idx = 1 + (((dev->mt76.macaddr[0] ^ vif->addr[0]) >> 2) & 7); /* * Client mode typically only has one configurable BSSID register,