Changing the MAC address of a running station interface makes mac80211 remove the interface from the driver and add it back with the new address. The code assumes that adding it back cannot fail and only warns if it does, which is not true: drivers may legitimately refuse to add the interface, for example a USB driver that has concluded its device has disappeared. rt2x00 returns -ENODEV from ->add_interface() once it has seen too many consecutive -EPROTO errors and marked the device as gone, which produces WARNING: net/mac80211/iface.c:306 at ieee80211_change_mac and, worse, the address change still returns success to userspace even though the interface is now running but no longer present in the driver. Any further address change then attempts to remove the interface from the driver again and trips the check_sdata_in_driver() warning. Handle the error properly: keep the remove/add sequence, but propagate the driver's error to userspace instead of warning about it, restore the previous address so that the failed change is not visible to userspace, and reject further address changes on an interface that the driver no longer knows about. Fixes: 3c06e91b40db6 ("wifi: mac80211: Support POWERED_ADDR_CHANGE feature") Cc: stable@vger.kernel.org Co-developed-by: Bin Yu Signed-off-by: Bin Yu Co-developed-by: Cheng Wen Signed-off-by: Cheng Wen Signed-off-by: YuanZhe Liu <25031212351@stu.xidian.edu.cn> --- v2: - Restore the previous address when the driver refuses to add the interface back, so that the failed change is not left in dev->dev_addr; otherwise a retry with the same address would be filtered out by netif_set_mac_address() and never reach mac80211. --- net/mac80211/iface.c | 40 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/net/mac80211/iface.c b/net/mac80211/iface.c index b6ed6541bc42..c45b42019d69 100644 --- a/net/mac80211/iface.c +++ b/net/mac80211/iface.c @@ -272,6 +272,7 @@ static int _ieee80211_change_mac(struct ieee80211_sub_if_data *sdata, struct sockaddr *sa = addr; bool check_dup = true; bool live = false; + u8 old_addr[ETH_ALEN]; int ret; if (ieee80211_sdata_running(sdata)) { @@ -279,6 +280,15 @@ static int _ieee80211_change_mac(struct ieee80211_sub_if_data *sdata, if (ret) return ret; + /* + * The driver may have refused to add the interface back + * after a previous address change, e.g. because the device + * disappeared. In that case the interface is not in the + * driver, so don't try to remove/add it again. + */ + if (!(sdata->flags & IEEE80211_SDATA_IN_DRIVER)) + return -ENODEV; + live = true; } @@ -290,8 +300,10 @@ static int _ieee80211_change_mac(struct ieee80211_sub_if_data *sdata, if (ret) return ret; - if (live) + if (live) { + ether_addr_copy(old_addr, sdata->dev->dev_addr); drv_remove_interface(local, sdata); + } ret = eth_mac_addr(sdata->dev, sa); if (ret == 0) { @@ -300,10 +312,30 @@ static int _ieee80211_change_mac(struct ieee80211_sub_if_data *sdata, } /* Regardless of eth_mac_addr() return we still want to add the - * interface back. This should not fail... + * interface back, but the driver may refuse, e.g. if the device + * disappeared in the meantime. */ - if (live) - WARN_ON(drv_add_interface(local, sdata)); + if (live) { + int err = drv_add_interface(local, sdata); + + if (err && !ret) { + struct sockaddr old_sa = { + .sa_family = sdata->dev->type, + }; + + /* + * Undo the address update, the change didn't happen. + * This also makes sure that a retry with the same + * address reaches us again instead of being filtered + * out by netif_set_mac_address(). + */ + ether_addr_copy(old_sa.sa_data, old_addr); + eth_mac_addr(sdata->dev, &old_sa); + memcpy(sdata->vif.addr, old_addr, ETH_ALEN); + ether_addr_copy(sdata->vif.bss_conf.addr, old_addr); + ret = err; + } + } return ret; } -- 2.34.1