mac80211: close AP_VLAN interfaces before unregistering all
[linux-3.10.git] / net / mac80211 / iface.c
index 1a6fe13..514e90f 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Interface handling (except master interface)
+ * Interface handling
  *
  * Copyright 2002-2005, Instant802 Networks, Inc.
  * Copyright 2005-2006, Devicescape Software, Inc.
  * by either the RTNL, the iflist_mtx or RCU.
  */
 
+bool __ieee80211_recalc_txpower(struct ieee80211_sub_if_data *sdata)
+{
+       struct ieee80211_chanctx_conf *chanctx_conf;
+       int power;
+
+       rcu_read_lock();
+       chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
+       if (!chanctx_conf) {
+               rcu_read_unlock();
+               return false;
+       }
+
+       power = chanctx_conf->def.chan->max_power;
+       rcu_read_unlock();
+
+       if (sdata->user_power_level != IEEE80211_UNSET_POWER_LEVEL)
+               power = min(power, sdata->user_power_level);
+
+       if (sdata->ap_power_level != IEEE80211_UNSET_POWER_LEVEL)
+               power = min(power, sdata->ap_power_level);
+
+       if (power != sdata->vif.bss_conf.txpower) {
+               sdata->vif.bss_conf.txpower = power;
+               ieee80211_hw_config(sdata->local, 0);
+               return true;
+       }
 
-static u32 ieee80211_idle_off(struct ieee80211_local *local,
-                             const char *reason)
+       return false;
+}
+
+void ieee80211_recalc_txpower(struct ieee80211_sub_if_data *sdata)
+{
+       if (__ieee80211_recalc_txpower(sdata))
+               ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_TXPOWER);
+}
+
+static u32 __ieee80211_idle_off(struct ieee80211_local *local)
 {
        if (!(local->hw.conf.flags & IEEE80211_CONF_IDLE))
                return 0;
@@ -53,141 +87,123 @@ static u32 ieee80211_idle_off(struct ieee80211_local *local,
        return IEEE80211_CONF_CHANGE_IDLE;
 }
 
-static u32 ieee80211_idle_on(struct ieee80211_local *local)
+static u32 __ieee80211_idle_on(struct ieee80211_local *local)
 {
        if (local->hw.conf.flags & IEEE80211_CONF_IDLE)
                return 0;
 
-       drv_flush(local, false);
+       ieee80211_flush_queues(local, NULL);
 
        local->hw.conf.flags |= IEEE80211_CONF_IDLE;
        return IEEE80211_CONF_CHANGE_IDLE;
 }
 
-static u32 __ieee80211_recalc_idle(struct ieee80211_local *local)
+static u32 __ieee80211_recalc_idle(struct ieee80211_local *local,
+                                  bool force_active)
 {
-       struct ieee80211_sub_if_data *sdata;
-       int count = 0;
-       bool working = false, scanning = false;
+       bool working = false, scanning, active;
        unsigned int led_trig_start = 0, led_trig_stop = 0;
        struct ieee80211_roc_work *roc;
 
-#ifdef CONFIG_PROVE_LOCKING
-       WARN_ON(debug_locks && !lockdep_rtnl_is_held() &&
-               !lockdep_is_held(&local->iflist_mtx));
-#endif
        lockdep_assert_held(&local->mtx);
 
-       list_for_each_entry(sdata, &local->interfaces, list) {
-               if (!ieee80211_sdata_running(sdata)) {
-                       sdata->vif.bss_conf.idle = true;
-                       continue;
-               }
-
-               sdata->old_idle = sdata->vif.bss_conf.idle;
-
-               /* do not count disabled managed interfaces */
-               if (sdata->vif.type == NL80211_IFTYPE_STATION &&
-                   !sdata->u.mgd.associated &&
-                   !sdata->u.mgd.auth_data &&
-                   !sdata->u.mgd.assoc_data) {
-                       sdata->vif.bss_conf.idle = true;
-                       continue;
-               }
-               /* do not count unused IBSS interfaces */
-               if (sdata->vif.type == NL80211_IFTYPE_ADHOC &&
-                   !sdata->u.ibss.ssid_len) {
-                       sdata->vif.bss_conf.idle = true;
-                       continue;
-               }
-
-               if (sdata->vif.type == NL80211_IFTYPE_P2P_DEVICE)
-                       continue;
-
-               /* count everything else */
-               sdata->vif.bss_conf.idle = false;
-               count++;
-       }
+       active = force_active ||
+                !list_empty(&local->chanctx_list) ||
+                local->monitors;
 
        if (!local->ops->remain_on_channel) {
                list_for_each_entry(roc, &local->roc_list, list) {
                        working = true;
-                       roc->sdata->vif.bss_conf.idle = false;
+                       break;
                }
        }
 
-       sdata = rcu_dereference_protected(local->scan_sdata,
-                                         lockdep_is_held(&local->mtx));
-       if (sdata && !(local->hw.flags & IEEE80211_HW_SCAN_WHILE_IDLE)) {
-               scanning = true;
-               sdata->vif.bss_conf.idle = false;
-       }
-
-       list_for_each_entry(sdata, &local->interfaces, list) {
-               if (sdata->vif.type == NL80211_IFTYPE_MONITOR ||
-                   sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
-                   sdata->vif.type == NL80211_IFTYPE_P2P_DEVICE)
-                       continue;
-               if (sdata->old_idle == sdata->vif.bss_conf.idle)
-                       continue;
-               if (!ieee80211_sdata_running(sdata))
-                       continue;
-               ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_IDLE);
-       }
+       scanning = test_bit(SCAN_SW_SCANNING, &local->scanning) ||
+                  test_bit(SCAN_ONCHANNEL_SCANNING, &local->scanning);
 
        if (working || scanning)
                led_trig_start |= IEEE80211_TPT_LEDTRIG_FL_WORK;
        else
                led_trig_stop |= IEEE80211_TPT_LEDTRIG_FL_WORK;
 
-       if (count)
+       if (active)
                led_trig_start |= IEEE80211_TPT_LEDTRIG_FL_CONNECTED;
        else
                led_trig_stop |= IEEE80211_TPT_LEDTRIG_FL_CONNECTED;
 
        ieee80211_mod_tpt_led_trig(local, led_trig_start, led_trig_stop);
 
-       if (working)
-               return ieee80211_idle_off(local, "working");
-       if (scanning)
-               return ieee80211_idle_off(local, "scanning");
-       if (!count)
-               return ieee80211_idle_on(local);
-       else
-               return ieee80211_idle_off(local, "in use");
+       if (working || scanning || active)
+               return __ieee80211_idle_off(local);
+       return __ieee80211_idle_on(local);
+}
 
-       return 0;
+u32 ieee80211_idle_off(struct ieee80211_local *local)
+{
+       return __ieee80211_recalc_idle(local, true);
 }
 
 void ieee80211_recalc_idle(struct ieee80211_local *local)
 {
-       u32 chg;
-
-       mutex_lock(&local->iflist_mtx);
-       chg = __ieee80211_recalc_idle(local);
-       mutex_unlock(&local->iflist_mtx);
-       if (chg)
-               ieee80211_hw_config(local, chg);
+       u32 change = __ieee80211_recalc_idle(local, false);
+       if (change)
+               ieee80211_hw_config(local, change);
 }
 
 static int ieee80211_change_mtu(struct net_device *dev, int new_mtu)
 {
-       int meshhdrlen;
-       struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
-
-       meshhdrlen = (sdata->vif.type == NL80211_IFTYPE_MESH_POINT) ? 5 : 0;
-
-       /* FIX: what would be proper limits for MTU?
-        * This interface uses 802.3 frames. */
-       if (new_mtu < 256 ||
-           new_mtu > IEEE80211_MAX_DATA_LEN - 24 - 6 - meshhdrlen) {
+       if (new_mtu < 256 || new_mtu > IEEE80211_MAX_DATA_LEN)
                return -EINVAL;
-       }
 
        dev->mtu = new_mtu;
        return 0;
 }
 
+static int ieee80211_verify_mac(struct ieee80211_sub_if_data *sdata, u8 *addr)
+{
+       struct ieee80211_local *local = sdata->local;
+       struct ieee80211_sub_if_data *iter;
+       u64 new, mask, tmp;
+       u8 *m;
+       int ret = 0;
+
+       if (is_zero_ether_addr(local->hw.wiphy->addr_mask))
+               return 0;
+
+       m = addr;
+       new =   ((u64)m[0] << 5*8) | ((u64)m[1] << 4*8) |
+               ((u64)m[2] << 3*8) | ((u64)m[3] << 2*8) |
+               ((u64)m[4] << 1*8) | ((u64)m[5] << 0*8);
+
+       m = local->hw.wiphy->addr_mask;
+       mask =  ((u64)m[0] << 5*8) | ((u64)m[1] << 4*8) |
+               ((u64)m[2] << 3*8) | ((u64)m[3] << 2*8) |
+               ((u64)m[4] << 1*8) | ((u64)m[5] << 0*8);
+
+
+       mutex_lock(&local->iflist_mtx);
+       list_for_each_entry(iter, &local->interfaces, list) {
+               if (iter == sdata)
+                       continue;
+
+               if (iter->vif.type == NL80211_IFTYPE_MONITOR)
+                       continue;
+
+               m = iter->vif.addr;
+               tmp =   ((u64)m[0] << 5*8) | ((u64)m[1] << 4*8) |
+                       ((u64)m[2] << 3*8) | ((u64)m[3] << 2*8) |
+                       ((u64)m[4] << 1*8) | ((u64)m[5] << 0*8);
+
+               if ((new & ~mask) != (tmp & ~mask)) {
+                       ret = -EINVAL;
+                       break;
+               }
+       }
+       mutex_unlock(&local->iflist_mtx);
+
+       return ret;
+}
+
 static int ieee80211_change_mac(struct net_device *dev, void *addr)
 {
        struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
@@ -197,6 +213,10 @@ static int ieee80211_change_mac(struct net_device *dev, void *addr)
        if (ieee80211_sdata_running(sdata))
                return -EBUSY;
 
+       ret = ieee80211_verify_mac(sdata, sa->sa_data);
+       if (ret)
+               return ret;
+
        ret = eth_mac_addr(dev, sa);
 
        if (ret == 0)
@@ -289,7 +309,8 @@ static int ieee80211_check_queues(struct ieee80211_sub_if_data *sdata)
                }
        }
 
-       if ((sdata->vif.type != NL80211_IFTYPE_AP) ||
+       if ((sdata->vif.type != NL80211_IFTYPE_AP &&
+            sdata->vif.type != NL80211_IFTYPE_MESH_POINT) ||
            !(sdata->local->hw.flags & IEEE80211_HW_QUEUE_CONTROL)) {
                sdata->vif.cab_queue = IEEE80211_INVAL_HW_QUEUE;
                return 0;
@@ -340,24 +361,22 @@ static void ieee80211_set_default_queues(struct ieee80211_sub_if_data *sdata)
        sdata->vif.cab_queue = IEEE80211_INVAL_HW_QUEUE;
 }
 
-static int ieee80211_add_virtual_monitor(struct ieee80211_local *local)
+int ieee80211_add_virtual_monitor(struct ieee80211_local *local)
 {
        struct ieee80211_sub_if_data *sdata;
-       int ret = 0;
+       int ret;
 
        if (!(local->hw.flags & IEEE80211_HW_WANT_MONITOR_VIF))
                return 0;
 
-       mutex_lock(&local->iflist_mtx);
+       ASSERT_RTNL();
 
        if (local->monitor_sdata)
-               goto out_unlock;
+               return 0;
 
        sdata = kzalloc(sizeof(*sdata) + local->hw.vif_data_size, GFP_KERNEL);
-       if (!sdata) {
-               ret = -ENOMEM;
-               goto out_unlock;
-       }
+       if (!sdata)
+               return -ENOMEM;
 
        /* set up data */
        sdata->local = local;
@@ -371,45 +390,51 @@ static int ieee80211_add_virtual_monitor(struct ieee80211_local *local)
        if (WARN_ON(ret)) {
                /* ok .. stupid driver, it asked for this! */
                kfree(sdata);
-               goto out_unlock;
+               return ret;
        }
 
        ret = ieee80211_check_queues(sdata);
        if (ret) {
                kfree(sdata);
-               goto out_unlock;
+               return ret;
        }
 
-       ret = ieee80211_vif_use_channel(sdata, local->monitor_channel,
-                                       local->monitor_channel_type,
+       ret = ieee80211_vif_use_channel(sdata, &local->monitor_chandef,
                                        IEEE80211_CHANCTX_EXCLUSIVE);
        if (ret) {
                drv_remove_interface(local, sdata);
                kfree(sdata);
-               goto out_unlock;
+               return ret;
        }
 
+       mutex_lock(&local->iflist_mtx);
        rcu_assign_pointer(local->monitor_sdata, sdata);
- out_unlock:
        mutex_unlock(&local->iflist_mtx);
-       return ret;
+
+       return 0;
 }
 
-static void ieee80211_del_virtual_monitor(struct ieee80211_local *local)
+void ieee80211_del_virtual_monitor(struct ieee80211_local *local)
 {
        struct ieee80211_sub_if_data *sdata;
 
        if (!(local->hw.flags & IEEE80211_HW_WANT_MONITOR_VIF))
                return;
 
+       ASSERT_RTNL();
+
        mutex_lock(&local->iflist_mtx);
 
        sdata = rcu_dereference_protected(local->monitor_sdata,
                                          lockdep_is_held(&local->iflist_mtx));
-       if (!sdata)
-               goto out_unlock;
+       if (!sdata) {
+               mutex_unlock(&local->iflist_mtx);
+               return;
+       }
 
        rcu_assign_pointer(local->monitor_sdata, NULL);
+       mutex_unlock(&local->iflist_mtx);
+
        synchronize_net();
 
        ieee80211_vif_release_channel(sdata);
@@ -417,8 +442,6 @@ static void ieee80211_del_virtual_monitor(struct ieee80211_local *local)
        drv_remove_interface(local, sdata);
 
        kfree(sdata);
- out_unlock:
-       mutex_unlock(&local->iflist_mtx);
 }
 
 /*
@@ -455,6 +478,9 @@ int ieee80211_do_open(struct wireless_dev *wdev, bool coming_up)
                        master->control_port_protocol;
                sdata->control_port_no_encrypt =
                        master->control_port_no_encrypt;
+               sdata->vif.cab_queue = master->vif.cab_queue;
+               memcpy(sdata->vif.hw_queue, master->vif.hw_queue,
+                      sizeof(sdata->vif.hw_queue));
                break;
                }
        case NL80211_IFTYPE_AP:
@@ -480,8 +506,6 @@ int ieee80211_do_open(struct wireless_dev *wdev, bool coming_up)
                res = drv_start(local);
                if (res)
                        goto err_del_bss;
-               if (local->ops->napi_poll)
-                       napi_enable(&local->napi);
                /* we're brought up, everything changes */
                hw_reconf_flags = ~0;
                ieee80211_led_radio(local, true);
@@ -507,11 +531,13 @@ int ieee80211_do_open(struct wireless_dev *wdev, bool coming_up)
 
        switch (sdata->vif.type) {
        case NL80211_IFTYPE_AP_VLAN:
-               /* no need to tell driver, but set carrier */
-               if (rtnl_dereference(sdata->bss->beacon))
+               /* no need to tell driver, but set carrier and chanctx */
+               if (rtnl_dereference(sdata->bss->beacon)) {
+                       ieee80211_vif_vlan_copy_chanctx(sdata);
                        netif_carrier_on(dev);
-               else
+               } else {
                        netif_carrier_off(dev);
+               }
                break;
        case NL80211_IFTYPE_MONITOR:
                if (sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES) {
@@ -534,6 +560,9 @@ int ieee80211_do_open(struct wireless_dev *wdev, bool coming_up)
 
                ieee80211_adjust_monitor_flags(sdata, 1);
                ieee80211_configure_filter(local);
+               mutex_lock(&local->mtx);
+               ieee80211_recalc_idle(local);
+               mutex_unlock(&local->mtx);
 
                netif_carrier_on(dev);
                break;
@@ -573,7 +602,8 @@ int ieee80211_do_open(struct wireless_dev *wdev, bool coming_up)
                case NL80211_IFTYPE_P2P_DEVICE:
                        break;
                default:
-                       netif_carrier_on(dev);
+                       /* not reached */
+                       WARN_ON(1);
                }
 
                /*
@@ -622,10 +652,6 @@ int ieee80211_do_open(struct wireless_dev *wdev, bool coming_up)
        if (sdata->flags & IEEE80211_SDATA_PROMISC)
                atomic_inc(&local->iff_promiscs);
 
-       mutex_lock(&local->mtx);
-       hw_reconf_flags |= __ieee80211_recalc_idle(local);
-       mutex_unlock(&local->mtx);
-
        if (coming_up)
                local->open_count++;
 
@@ -634,8 +660,32 @@ int ieee80211_do_open(struct wireless_dev *wdev, bool coming_up)
 
        ieee80211_recalc_ps(local, -1);
 
-       if (dev)
+       if (sdata->vif.type == NL80211_IFTYPE_MONITOR ||
+           sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
+               /* XXX: for AP_VLAN, actually track AP queues */
                netif_tx_start_all_queues(dev);
+       } else if (dev) {
+               unsigned long flags;
+               int n_acs = IEEE80211_NUM_ACS;
+               int ac;
+
+               if (local->hw.queues < IEEE80211_NUM_ACS)
+                       n_acs = 1;
+
+               spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
+               if (sdata->vif.cab_queue == IEEE80211_INVAL_HW_QUEUE ||
+                   (local->queue_stop_reasons[sdata->vif.cab_queue] == 0 &&
+                    skb_queue_empty(&local->pending[sdata->vif.cab_queue]))) {
+                       for (ac = 0; ac < n_acs; ac++) {
+                               int ac_queue = sdata->vif.hw_queue[ac];
+
+                               if (local->queue_stop_reasons[ac_queue] == 0 &&
+                                   skb_queue_empty(&local->pending[ac_queue]))
+                                       netif_start_subqueue(dev, ac);
+                       }
+               }
+               spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
+       }
 
        return 0;
  err_del_interface:
@@ -675,7 +725,8 @@ static void ieee80211_do_stop(struct ieee80211_sub_if_data *sdata,
        unsigned long flags;
        struct sk_buff *skb, *tmp;
        u32 hw_reconf_flags = 0;
-       int i;
+       int i, flushed;
+       struct ps_data *ps;
 
        clear_bit(SDATA_STATE_RUNNING, &sdata->state);
 
@@ -688,7 +739,10 @@ static void ieee80211_do_stop(struct ieee80211_sub_if_data *sdata,
        if (sdata->dev)
                netif_tx_stop_all_queues(sdata->dev);
 
-       ieee80211_roc_purge(sdata);
+       ieee80211_roc_purge(local, sdata);
+
+       if (sdata->vif.type == NL80211_IFTYPE_STATION)
+               ieee80211_mgd_stop(sdata);
 
        /*
         * Remove all stations associated with this interface.
@@ -700,18 +754,17 @@ static void ieee80211_do_stop(struct ieee80211_sub_if_data *sdata,
         * (because if we remove a STA after ops->remove_interface()
         * the driver will have removed the vif info already!)
         *
-        * This is relevant only in AP, WDS and mesh modes, since in
-        * all other modes we've already removed all stations when
-        * disconnecting etc.
+        * This is relevant only in WDS mode, in all other modes we've
+        * already removed all stations when disconnecting or similar,
+        * so warn otherwise.
+        *
+        * We call sta_info_flush_cleanup() later, to combine RCU waits.
         */
-       sta_info_flush(local, sdata);
+       flushed = sta_info_flush_defer(sdata);
+       WARN_ON_ONCE((sdata->vif.type != NL80211_IFTYPE_WDS && flushed > 0) ||
+                    (sdata->vif.type == NL80211_IFTYPE_WDS && flushed != 1));
 
-       /*
-        * Don't count this interface for promisc/allmulti while it
-        * is down. dev_mc_unsync() will invoke set_multicast_list
-        * on the master interface which will sync these down to the
-        * hardware as filter flags.
-        */
+       /* don't count this interface for promisc/allmulti while it is down */
        if (sdata->flags & IEEE80211_SDATA_ALLMULTI)
                atomic_dec(&local->iff_allmultis);
 
@@ -732,8 +785,6 @@ static void ieee80211_do_stop(struct ieee80211_sub_if_data *sdata,
                                 sdata->dev->addr_len);
                spin_unlock_bh(&local->filter_lock);
                netif_addr_unlock_bh(sdata->dev);
-
-               ieee80211_configure_filter(local);
        }
 
        del_timer_sync(&local->dynamic_ps_timer);
@@ -741,6 +792,17 @@ static void ieee80211_do_stop(struct ieee80211_sub_if_data *sdata,
 
        cancel_work_sync(&sdata->recalc_smps);
 
+       cancel_delayed_work_sync(&sdata->dfs_cac_timer_work);
+
+       if (sdata->wdev.cac_started) {
+               WARN_ON(local->suspended);
+               mutex_lock(&local->iflist_mtx);
+               ieee80211_vif_release_channel(sdata);
+               mutex_unlock(&local->iflist_mtx);
+               cfg80211_cac_event(sdata->dev, NL80211_RADAR_CAC_ABORTED,
+                                  GFP_KERNEL);
+       }
+
        /* APs need special treatment */
        if (sdata->vif.type == NL80211_IFTYPE_AP) {
                struct ieee80211_sub_if_data *vlan, *tmpsdata;
@@ -750,8 +812,19 @@ static void ieee80211_do_stop(struct ieee80211_sub_if_data *sdata,
                                         u.vlan.list)
                        dev_close(vlan->dev);
                WARN_ON(!list_empty(&sdata->u.ap.vlans));
-       } else if (sdata->vif.type == NL80211_IFTYPE_STATION) {
-               ieee80211_mgd_stop(sdata);
+       } else if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
+               /* remove all packets in parent bc_buf pointing to this dev */
+               ps = &sdata->bss->ps;
+
+               spin_lock_irqsave(&ps->bc_buf.lock, flags);
+               skb_queue_walk_safe(&ps->bc_buf, skb, tmp) {
+                       if (skb->dev == sdata->dev) {
+                               __skb_unlink(skb, &ps->bc_buf);
+                               local->total_ps_buffered--;
+                               ieee80211_free_txskb(&local->hw, skb);
+                       }
+               }
+               spin_unlock_irqrestore(&ps->bc_buf.lock, flags);
        }
 
        if (going_down)
@@ -760,6 +833,7 @@ static void ieee80211_do_stop(struct ieee80211_sub_if_data *sdata,
        switch (sdata->vif.type) {
        case NL80211_IFTYPE_AP_VLAN:
                list_del(&sdata->u.vlan.list);
+               rcu_assign_pointer(sdata->vif.chanctx_conf, NULL);
                /* no need to tell driver */
                break;
        case NL80211_IFTYPE_MONITOR:
@@ -772,68 +846,43 @@ static void ieee80211_do_stop(struct ieee80211_sub_if_data *sdata,
                if (local->monitors == 0) {
                        local->hw.conf.flags &= ~IEEE80211_CONF_MONITOR;
                        hw_reconf_flags |= IEEE80211_CONF_CHANGE_MONITOR;
-                       ieee80211_del_virtual_monitor(local);
                }
 
                ieee80211_adjust_monitor_flags(sdata, -1);
-               ieee80211_configure_filter(local);
                break;
        case NL80211_IFTYPE_P2P_DEVICE:
                /* relies on synchronize_rcu() below */
                rcu_assign_pointer(local->p2p_sdata, NULL);
                /* fall through */
        default:
-               flush_work(&sdata->work);
+               cancel_work_sync(&sdata->work);
                /*
                 * When we get here, the interface is marked down.
-                * Call rcu_barrier() to wait both for the RX path
-                * should it be using the interface and enqueuing
-                * frames at this very time on another CPU, and
-                * for the sta free call_rcu callbacks.
+                *
+                * sta_info_flush_cleanup() requires rcu_barrier()
+                * first to wait for the station call_rcu() calls
+                * to complete, and we also need synchronize_rcu()
+                * to wait for the RX path in case it is using the
+                * interface and enqueuing frames at this very time on
+                * another CPU.
                 */
+               synchronize_rcu();
                rcu_barrier();
-
-               /*
-                * free_sta_rcu() enqueues a work for the actual
-                * sta cleanup, so we need to flush it while
-                * sdata is still valid.
-                */
-               flush_workqueue(local->workqueue);
-
-               skb_queue_purge(&sdata->skb_queue);
+               sta_info_flush_cleanup(sdata);
 
                /*
                 * Free all remaining keys, there shouldn't be any,
-                * except maybe group keys in AP more or WDS?
+                * except maybe in WDS mode?
                 */
                ieee80211_free_keys(sdata);
 
-               if (going_down)
-                       drv_remove_interface(local, sdata);
+               /* fall through */
+       case NL80211_IFTYPE_AP:
+               skb_queue_purge(&sdata->skb_queue);
        }
 
        sdata->bss = NULL;
 
-       mutex_lock(&local->mtx);
-       hw_reconf_flags |= __ieee80211_recalc_idle(local);
-       mutex_unlock(&local->mtx);
-
-       ieee80211_recalc_ps(local, -1);
-
-       if (local->open_count == 0) {
-               if (local->ops->napi_poll)
-                       napi_disable(&local->napi);
-               ieee80211_clear_tx_pending(local);
-               ieee80211_stop_device(local);
-
-               /* no reconfiguring after stop! */
-               hw_reconf_flags = 0;
-       }
-
-       /* do after stop to avoid reconfiguring when we stop anyway */
-       if (hw_reconf_flags)
-               ieee80211_hw_config(local, hw_reconf_flags);
-
        spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
        for (i = 0; i < IEEE80211_MAX_QUEUES; i++) {
                skb_queue_walk_safe(&local->pending[i], skb, tmp) {
@@ -846,7 +895,54 @@ static void ieee80211_do_stop(struct ieee80211_sub_if_data *sdata,
        }
        spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
 
-       if (local->monitors == local->open_count && local->monitors > 0)
+       if (local->open_count == 0)
+               ieee80211_clear_tx_pending(local);
+
+       /*
+        * If the interface goes down while suspended, presumably because
+        * the device was unplugged and that happens before our resume,
+        * then the driver is already unconfigured and the remainder of
+        * this function isn't needed.
+        * XXX: what about WoWLAN? If the device has software state, e.g.
+        *      memory allocated, it might expect teardown commands from
+        *      mac80211 here?
+        */
+       if (local->suspended) {
+               WARN_ON(local->wowlan);
+               WARN_ON(rtnl_dereference(local->monitor_sdata));
+               return;
+       }
+
+       switch (sdata->vif.type) {
+       case NL80211_IFTYPE_AP_VLAN:
+               break;
+       case NL80211_IFTYPE_MONITOR:
+               if (local->monitors == 0)
+                       ieee80211_del_virtual_monitor(local);
+
+               mutex_lock(&local->mtx);
+               ieee80211_recalc_idle(local);
+               mutex_unlock(&local->mtx);
+               break;
+       default:
+               if (going_down)
+                       drv_remove_interface(local, sdata);
+       }
+
+       ieee80211_recalc_ps(local, -1);
+
+       if (local->open_count == 0) {
+               ieee80211_stop_device(local);
+
+               /* no reconfiguring after stop! */
+               return;
+       }
+
+       /* do after stop to avoid reconfiguring when we stop anyway */
+       ieee80211_configure_filter(local);
+       ieee80211_hw_config(local, hw_reconf_flags);
+
+       if (local->monitors == local->open_count)
                ieee80211_add_virtual_monitor(local);
 }
 
@@ -885,6 +981,17 @@ static void ieee80211_set_multicast_list(struct net_device *dev)
                        atomic_dec(&local->iff_promiscs);
                sdata->flags ^= IEEE80211_SDATA_PROMISC;
        }
+
+       /*
+        * TODO: If somebody needs this on AP interfaces,
+        *       it can be enabled easily but multicast
+        *       addresses from VLANs need to be synced.
+        */
+       if (sdata->vif.type != NL80211_IFTYPE_MONITOR &&
+           sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
+           sdata->vif.type != NL80211_IFTYPE_AP)
+               drv_set_multicast_list(local, sdata, &dev->mc);
+
        spin_lock_bh(&local->filter_lock);
        __hw_addr_sync(&local->mc_list, &dev->mc, dev->addr_len);
        spin_unlock_bh(&local->filter_lock);
@@ -897,7 +1004,6 @@ static void ieee80211_set_multicast_list(struct net_device *dev)
  */
 static void ieee80211_teardown_sdata(struct ieee80211_sub_if_data *sdata)
 {
-       struct ieee80211_local *local = sdata->local;
        int flushed;
        int i;
 
@@ -913,7 +1019,7 @@ static void ieee80211_teardown_sdata(struct ieee80211_sub_if_data *sdata)
        if (ieee80211_vif_is_mesh(&sdata->vif))
                mesh_rmc_free(sdata);
 
-       flushed = sta_info_flush(local, sdata);
+       flushed = sta_info_flush(sdata);
        WARN_ON(flushed);
 }
 
@@ -1154,6 +1260,7 @@ static void ieee80211_setup_sdata(struct ieee80211_sub_if_data *sdata,
        case NL80211_IFTYPE_AP:
                skb_queue_head_init(&sdata->u.ap.ps.bc_buf);
                INIT_LIST_HEAD(&sdata->u.ap.vlans);
+               sdata->vif.bss_conf.bssid = sdata->vif.addr;
                break;
        case NL80211_IFTYPE_P2P_CLIENT:
                type = NL80211_IFTYPE_STATION;
@@ -1161,9 +1268,11 @@ static void ieee80211_setup_sdata(struct ieee80211_sub_if_data *sdata,
                sdata->vif.p2p = true;
                /* fall through */
        case NL80211_IFTYPE_STATION:
+               sdata->vif.bss_conf.bssid = sdata->u.mgd.bssid;
                ieee80211_sta_setup_sdata(sdata);
                break;
        case NL80211_IFTYPE_ADHOC:
+               sdata->vif.bss_conf.bssid = sdata->u.ibss.bssid;
                ieee80211_ibss_setup_sdata(sdata);
                break;
        case NL80211_IFTYPE_MESH_POINT:
@@ -1177,8 +1286,12 @@ static void ieee80211_setup_sdata(struct ieee80211_sub_if_data *sdata,
                                      MONITOR_FLAG_OTHER_BSS;
                break;
        case NL80211_IFTYPE_WDS:
+               sdata->vif.bss_conf.bssid = NULL;
+               break;
        case NL80211_IFTYPE_AP_VLAN:
+               break;
        case NL80211_IFTYPE_P2P_DEVICE:
+               sdata->vif.bss_conf.bssid = sdata->vif.addr;
                break;
        case NL80211_IFTYPE_UNSPECIFIED:
        case NUM_NL80211_IFTYPES:
@@ -1377,7 +1490,17 @@ static void ieee80211_assign_perm_addr(struct ieee80211_local *local,
                        break;
                }
 
+               /*
+                * Pick address of existing interface in case user changed
+                * MAC address manually, default to perm_addr.
+                */
                m = local->hw.wiphy->perm_addr;
+               list_for_each_entry(sdata, &local->interfaces, list) {
+                       if (sdata->vif.type == NL80211_IFTYPE_MONITOR)
+                               continue;
+                       m = sdata->vif.addr;
+                       break;
+               }
                start = ((u64)m[0] << 5*8) | ((u64)m[1] << 4*8) |
                        ((u64)m[2] << 3*8) | ((u64)m[3] << 2*8) |
                        ((u64)m[4] << 1*8) | ((u64)m[5] << 0*8);
@@ -1419,6 +1542,15 @@ static void ieee80211_assign_perm_addr(struct ieee80211_local *local,
        mutex_unlock(&local->iflist_mtx);
 }
 
+static void ieee80211_cleanup_sdata_stas_wk(struct work_struct *wk)
+{
+       struct ieee80211_sub_if_data *sdata;
+
+       sdata = container_of(wk, struct ieee80211_sub_if_data, cleanup_stations_wk);
+
+       ieee80211_cleanup_sdata_stas(sdata);
+}
+
 int ieee80211_if_add(struct ieee80211_local *local, const char *name,
                     struct wireless_dev **new_wdev, enum nl80211_iftype type,
                     struct vif_params *params)
@@ -1485,15 +1617,20 @@ int ieee80211_if_add(struct ieee80211_local *local, const char *name,
        /* initialise type-independent data */
        sdata->wdev.wiphy = local->hw.wiphy;
        sdata->local = local;
-#ifdef CONFIG_INET
-       sdata->arp_filter_state = true;
-#endif
 
        for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++)
                skb_queue_head_init(&sdata->fragments[i].skb_list);
 
        INIT_LIST_HEAD(&sdata->key_list);
 
+       spin_lock_init(&sdata->cleanup_stations_lock);
+       INIT_LIST_HEAD(&sdata->cleanup_stations);
+       INIT_WORK(&sdata->cleanup_stations_wk, ieee80211_cleanup_sdata_stas_wk);
+       INIT_DELAYED_WORK(&sdata->dfs_cac_timer_work,
+                         ieee80211_dfs_cac_timer_work);
+       INIT_DELAYED_WORK(&sdata->dec_tailroom_needed_wk,
+                         ieee80211_delayed_tailroom_dec);
+
        for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
                struct ieee80211_supported_band *sband;
                sband = local->hw.wiphy->bands[i];
@@ -1510,6 +1647,9 @@ int ieee80211_if_add(struct ieee80211_local *local, const char *name,
 
        ieee80211_set_default_queues(sdata);
 
+       sdata->ap_power_level = IEEE80211_UNSET_POWER_LEVEL;
+       sdata->user_power_level = local->user_power_level;
+
        /* setup type-dependent data */
        ieee80211_setup_sdata(sdata, type);
 
@@ -1577,6 +1717,24 @@ void ieee80211_remove_interfaces(struct ieee80211_local *local)
 
        ASSERT_RTNL();
 
+       /*
+        * Close all AP_VLAN interfaces first, as otherwise they
+        * might be closed while the AP interface they belong to
+        * is closed, causing unregister_netdevice_many() to crash.
+        */
+       list_for_each_entry(sdata, &local->interfaces, list)
+               if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
+                       dev_close(sdata->dev);
+
+       /*
+        * Close all AP_VLAN interfaces first, as otherwise they
+        * might be closed while the AP interface they belong to
+        * is closed, causing unregister_netdevice_many() to crash.
+        */
+       list_for_each_entry(sdata, &local->interfaces, list)
+               if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
+                       dev_close(sdata->dev);
+
        mutex_lock(&local->iflist_mtx);
        list_for_each_entry_safe(sdata, tmp, &local->interfaces, list) {
                list_del(&sdata->list);