cfg80211: no cookies in cfg80211_send_XXX()
[safe/jmp/linux-2.6] / net / mac80211 / mlme.c
index 15dbb57..33a696f 100644 (file)
 #define IEEE80211_AUTH_MAX_TRIES 3
 #define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
 #define IEEE80211_ASSOC_MAX_TRIES 3
-#define IEEE80211_MONITORING_INTERVAL (2 * HZ)
-#define IEEE80211_PROBE_WAIT (HZ / 5)
+#define IEEE80211_MAX_PROBE_TRIES 5
+
+/*
+ * beacon loss detection timeout
+ * XXX: should depend on beacon interval
+ */
+#define IEEE80211_BEACON_LOSS_TIME     (2 * HZ)
+/*
+ * Time the connection can be idle before we probe
+ * it to see if we can still talk to the AP.
+ */
+#define IEEE80211_CONNECTION_IDLE_TIME (30 * HZ)
+/*
+ * Time we wait for a probe response after sending
+ * a probe request because of beacon loss or for
+ * checking the connection still works.
+ */
+#define IEEE80211_PROBE_WAIT           (HZ / 2)
 
 #define TMR_RUNNING_TIMER      0
 #define TMR_RUNNING_CHANSW     1
@@ -72,6 +88,35 @@ static inline void ASSERT_MGD_MTX(struct ieee80211_if_managed *ifmgd)
        WARN_ON(!mutex_is_locked(&ifmgd->mtx));
 }
 
+/*
+ * We can have multiple work items (and connection probing)
+ * scheduling this timer, but we need to take care to only
+ * reschedule it when it should fire _earlier_ than it was
+ * asked for before, or if it's not pending right now. This
+ * function ensures that. Note that it then is required to
+ * run this function for all timeouts after the first one
+ * has happened -- the work that runs from this timer will
+ * do that.
+ */
+static void run_again(struct ieee80211_if_managed *ifmgd,
+                            unsigned long timeout)
+{
+       ASSERT_MGD_MTX(ifmgd);
+
+       if (!timer_pending(&ifmgd->timer) ||
+           time_before(timeout, ifmgd->timer.expires))
+               mod_timer(&ifmgd->timer, timeout);
+}
+
+static void mod_beacon_timer(struct ieee80211_sub_if_data *sdata)
+{
+       if (sdata->local->hw.flags & IEEE80211_HW_BEACON_FILTER)
+               return;
+
+       mod_timer(&sdata->u.mgd.bcn_mon_timer,
+                 round_jiffies_up(jiffies + IEEE80211_BEACON_LOSS_TIME));
+}
+
 static int ecw2cw(int ecw)
 {
        return (1 << ecw) - 1;
@@ -386,7 +431,8 @@ static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata,
 
 
 static void ieee80211_send_deauth_disassoc(struct ieee80211_sub_if_data *sdata,
-                                          const u8 *bssid, u16 stype, u16 reason)
+                                          const u8 *bssid, u16 stype, u16 reason,
+                                          void *cookie)
 {
        struct ieee80211_local *local = sdata->local;
        struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
@@ -412,9 +458,15 @@ static void ieee80211_send_deauth_disassoc(struct ieee80211_sub_if_data *sdata,
        mgmt->u.deauth.reason_code = cpu_to_le16(reason);
 
        if (stype == IEEE80211_STYPE_DEAUTH)
-               cfg80211_send_deauth(sdata->dev, (u8 *) mgmt, skb->len);
+               if (cookie)
+                       __cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
+               else
+                       cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
        else
-               cfg80211_send_disassoc(sdata->dev, (u8 *) mgmt, skb->len);
+               if (cookie)
+                       __cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
+               else
+                       cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
        ieee80211_tx_skb(sdata, skb, ifmgd->flags & IEEE80211_STA_MFP_ENABLED);
 }
 
@@ -520,7 +572,7 @@ static void ieee80211_chswitch_timer(unsigned long data)
                return;
        }
 
-       queue_work(sdata->local->hw.workqueue, &ifmgd->chswitch_work);
+       ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
 }
 
 void ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
@@ -536,7 +588,7 @@ void ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
        if (!ifmgd->associated)
                return;
 
-       if (sdata->local->sw_scanning || sdata->local->hw_scanning)
+       if (sdata->local->scanning)
                return;
 
        /* Disregard subsequent beacons if we are already running a timer
@@ -552,7 +604,7 @@ void ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
        sdata->local->csa_channel = new_ch;
 
        if (sw_elem->count <= 1) {
-               queue_work(sdata->local->hw.workqueue, &ifmgd->chswitch_work);
+               ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
        } else {
                ieee80211_stop_queues_by_reason(&sdata->local->hw,
                                        IEEE80211_QUEUE_STOP_REASON_CSA);
@@ -594,7 +646,7 @@ static void ieee80211_enable_ps(struct ieee80211_local *local,
         * If we are scanning right now then the parameters will
         * take effect when scan finishes.
         */
-       if (local->hw_scanning || local->sw_scanning)
+       if (local->scanning)
                return;
 
        if (conf->dynamic_ps_timeout > 0 &&
@@ -645,7 +697,8 @@ void ieee80211_recalc_ps(struct ieee80211_local *local, s32 latency)
 
        if (count == 1 && found->u.mgd.powersave &&
            found->u.mgd.associated && list_empty(&found->u.mgd.work_list) &&
-           !(found->u.mgd.flags & IEEE80211_STA_PROBEREQ_POLL)) {
+           !(found->u.mgd.flags & (IEEE80211_STA_BEACON_POLL |
+                                   IEEE80211_STA_CONNECTION_POLL))) {
                s32 beaconint_us;
 
                if (latency < 0)
@@ -714,10 +767,10 @@ void ieee80211_dynamic_ps_timer(unsigned long data)
 {
        struct ieee80211_local *local = (void *) data;
 
-       if (local->quiescing)
+       if (local->quiescing || local->suspended)
                return;
 
-       queue_work(local->hw.workqueue, &local->dynamic_ps_enable_work);
+       ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
 }
 
 /* MLME */
@@ -833,10 +886,11 @@ static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
 }
 
 static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
-                                    struct ieee80211_bss *bss,
+                                    struct ieee80211_mgd_work *wk,
                                     u32 bss_info_changed)
 {
        struct ieee80211_local *local = sdata->local;
+       struct ieee80211_bss *bss = wk->bss;
 
        bss_info_changed |= BSS_CHANGED_ASSOC;
        /* set timing information */
@@ -849,8 +903,13 @@ static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
                bss->cbss.capability, bss->has_erp_value, bss->erp_value);
 
        sdata->u.mgd.associated = bss;
+       sdata->u.mgd.old_associate_work = wk;
        memcpy(sdata->u.mgd.bssid, bss->cbss.bssid, ETH_ALEN);
 
+       /* just to be sure */
+       sdata->u.mgd.flags &= ~(IEEE80211_STA_CONNECTION_POLL |
+                               IEEE80211_STA_BEACON_POLL);
+
        ieee80211_led_assoc(local, 1);
 
        sdata->vif.bss_conf.assoc = 1;
@@ -866,12 +925,9 @@ static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
 
        ieee80211_bss_info_change_notify(sdata, bss_info_changed);
 
-       /* will be same as sdata */
-       if (local->ps_sdata) {
-               mutex_lock(&local->iflist_mtx);
-               ieee80211_recalc_ps(local, -1);
-               mutex_unlock(&local->iflist_mtx);
-       }
+       mutex_lock(&local->iflist_mtx);
+       ieee80211_recalc_ps(local, -1);
+       mutex_unlock(&local->iflist_mtx);
 
        netif_tx_start_all_queues(sdata->dev);
        netif_carrier_on(sdata->dev);
@@ -900,7 +956,7 @@ ieee80211_direct_probe(struct ieee80211_sub_if_data *sdata,
                 * due to work needing to be done. Hence, queue the STAs work
                 * again for that.
                 */
-               queue_work(local->hw.workqueue, &ifmgd->work);
+               ieee80211_queue_work(&local->hw, &ifmgd->work);
                return RX_MGMT_CFG80211_AUTH_TO;
        }
 
@@ -915,7 +971,7 @@ ieee80211_direct_probe(struct ieee80211_sub_if_data *sdata,
        ieee80211_send_probe_req(sdata, NULL, wk->ssid, wk->ssid_len, NULL, 0);
 
        wk->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
-       mod_timer(&ifmgd->timer, wk->timeout);
+       run_again(ifmgd, wk->timeout);
 
        return RX_MGMT_NONE;
 }
@@ -945,7 +1001,7 @@ ieee80211_authenticate(struct ieee80211_sub_if_data *sdata,
                 * due to work needing to be done. Hence, queue the STAs work
                 * again for that.
                 */
-               queue_work(local->hw.workqueue, &ifmgd->work);
+               ieee80211_queue_work(&local->hw, &ifmgd->work);
                return RX_MGMT_CFG80211_AUTH_TO;
        }
 
@@ -953,28 +1009,44 @@ ieee80211_authenticate(struct ieee80211_sub_if_data *sdata,
               sdata->dev->name, wk->bss->cbss.bssid, wk->tries);
 
        ieee80211_send_auth(sdata, 1, wk->auth_alg, wk->ie, wk->ie_len,
-                           wk->bss->cbss.bssid, 0);
+                           wk->bss->cbss.bssid, NULL, 0, 0);
        wk->auth_transaction = 2;
 
        wk->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
-       mod_timer(&ifmgd->timer, wk->timeout);
+       run_again(ifmgd, wk->timeout);
 
        return RX_MGMT_NONE;
 }
 
 static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
-                                  const u8 *bssid, bool deauth)
+                                  bool deauth)
 {
        struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
        struct ieee80211_local *local = sdata->local;
        struct sta_info *sta;
        u32 changed = 0, config_changed = 0;
+       u8 bssid[ETH_ALEN];
 
        ASSERT_MGD_MTX(ifmgd);
 
+       if (WARN_ON(!ifmgd->associated))
+               return;
+
+       memcpy(bssid, ifmgd->associated->cbss.bssid, ETH_ALEN);
+
        ifmgd->associated = NULL;
        memset(ifmgd->bssid, 0, ETH_ALEN);
 
+       if (deauth) {
+               kfree(ifmgd->old_associate_work);
+               ifmgd->old_associate_work = NULL;
+       } else {
+               struct ieee80211_mgd_work *wk = ifmgd->old_associate_work;
+
+               wk->state = IEEE80211_MGD_STATE_IDLE;
+               list_add(&wk->list, &ifmgd->work_list);
+       }
+
        /*
         * we need to commit the associated = NULL change because the
         * scan code uses that to determine whether this iface should
@@ -1069,7 +1141,7 @@ ieee80211_associate(struct ieee80211_sub_if_data *sdata,
                 * due to work needing to be done. Hence, queue the STAs work
                 * again for that.
                 */
-               queue_work(local->hw.workqueue, &ifmgd->work);
+               ieee80211_queue_work(&local->hw, &ifmgd->work);
                return RX_MGMT_CFG80211_ASSOC_TO;
        }
 
@@ -1078,7 +1150,7 @@ ieee80211_associate(struct ieee80211_sub_if_data *sdata,
        ieee80211_send_assoc(sdata, wk);
 
        wk->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
-       mod_timer(&ifmgd->timer, wk->timeout);
+       run_again(ifmgd, wk->timeout);
 
        return RX_MGMT_NONE;
 }
@@ -1091,31 +1163,40 @@ void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
         * from AP because we know that the connection is working both ways
         * at that time. But multicast frames (and hence also beacons) must
         * be ignored here, because we need to trigger the timer during
-        * data idle periods for sending the periodical probe request to
-        * the AP.
+        * data idle periods for sending the periodic probe request to the
+        * AP we're connected to.
         */
-       if (!is_multicast_ether_addr(hdr->addr1))
-               mod_timer(&sdata->u.mgd.timer,
-                         jiffies + IEEE80211_MONITORING_INTERVAL);
+       if (is_multicast_ether_addr(hdr->addr1))
+               return;
+
+       mod_timer(&sdata->u.mgd.conn_mon_timer,
+                 round_jiffies_up(jiffies + IEEE80211_CONNECTION_IDLE_TIME));
 }
 
-void ieee80211_beacon_loss_work(struct work_struct *work)
+static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
 {
-       struct ieee80211_sub_if_data *sdata =
-               container_of(work, struct ieee80211_sub_if_data,
-                            u.mgd.beacon_loss_work);
        struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
        const u8 *ssid;
 
-       /*
-        * The driver has already reported this event and we have
-        * already sent a probe request. Maybe the AP died and the
-        * driver keeps reporting until we disassociate... We have
-        * to ignore that because otherwise we would continually
-        * reset the timer and never check whether we received a
-        * probe response!
-        */
-       if (ifmgd->flags & IEEE80211_STA_PROBEREQ_POLL)
+       ssid = ieee80211_bss_get_ie(&ifmgd->associated->cbss, WLAN_EID_SSID);
+       ieee80211_send_probe_req(sdata, ifmgd->associated->cbss.bssid,
+                                ssid + 2, ssid[1], NULL, 0);
+
+       ifmgd->probe_send_count++;
+       ifmgd->probe_timeout = jiffies + IEEE80211_PROBE_WAIT;
+       run_again(ifmgd, ifmgd->probe_timeout);
+}
+
+static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
+                                  bool beacon)
+{
+       struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
+       bool already = false;
+
+       if (!netif_running(sdata->dev))
+               return;
+
+       if (sdata->local->scanning)
                return;
 
        mutex_lock(&ifmgd->mtx);
@@ -1124,32 +1205,58 @@ void ieee80211_beacon_loss_work(struct work_struct *work)
                goto out;
 
 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
-       if (net_ratelimit())
-               printk(KERN_DEBUG "%s: driver reports beacon loss from AP "
+       if (beacon && net_ratelimit())
+               printk(KERN_DEBUG "%s: detected beacon loss from AP "
                       "- sending probe request\n", sdata->dev->name);
 #endif
 
-       ifmgd->flags |= IEEE80211_STA_PROBEREQ_POLL;
+       /*
+        * The driver/our work has already reported this event or the
+        * connection monitoring has kicked in and we have already sent
+        * a probe request. Or maybe the AP died and the driver keeps
+        * reporting until we disassociate...
+        *
+        * In either case we have to ignore the current call to this
+        * function (except for setting the correct probe reason bit)
+        * because otherwise we would reset the timer every time and
+        * never check whether we received a probe response!
+        */
+       if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
+                           IEEE80211_STA_CONNECTION_POLL))
+               already = true;
+
+       if (beacon)
+               ifmgd->flags |= IEEE80211_STA_BEACON_POLL;
+       else
+               ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
+
+       if (already)
+               goto out;
 
        mutex_lock(&sdata->local->iflist_mtx);
        ieee80211_recalc_ps(sdata->local, -1);
        mutex_unlock(&sdata->local->iflist_mtx);
 
-       ssid = ieee80211_bss_get_ie(&ifmgd->associated->cbss, WLAN_EID_SSID);
-       ieee80211_send_probe_req(sdata, ifmgd->associated->cbss.bssid,
-                                ssid + 2, ssid[1], NULL, 0);
-
-       mod_timer(&ifmgd->timer, jiffies + IEEE80211_PROBE_WAIT);
+       ifmgd->probe_send_count = 0;
+       ieee80211_mgd_probe_ap_send(sdata);
  out:
        mutex_unlock(&ifmgd->mtx);
 }
 
+void ieee80211_beacon_loss_work(struct work_struct *work)
+{
+       struct ieee80211_sub_if_data *sdata =
+               container_of(work, struct ieee80211_sub_if_data,
+                            u.mgd.beacon_loss_work);
+
+       ieee80211_mgd_probe_ap(sdata, true);
+}
+
 void ieee80211_beacon_loss(struct ieee80211_vif *vif)
 {
        struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
 
-       queue_work(sdata->local->hw.workqueue,
-                  &sdata->u.mgd.beacon_loss_work);
+       ieee80211_queue_work(&sdata->local->hw, &sdata->u.mgd.beacon_loss_work);
 }
 EXPORT_SYMBOL(ieee80211_beacon_loss);
 
@@ -1175,7 +1282,8 @@ static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
                return;
        ieee80211_send_auth(sdata, 3, wk->auth_alg,
                            elems.challenge - 2, elems.challenge_len + 2,
-                           wk->bss->cbss.bssid, 1);
+                           wk->bss->cbss.bssid,
+                           wk->key, wk->key_len, wk->key_idx);
        wk->auth_transaction = 4;
 }
 
@@ -1256,7 +1364,7 @@ ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
                        sdata->dev->name, bssid, reason_code);
 
        if (!wk) {
-               ieee80211_set_disassoc(sdata, bssid, true);
+               ieee80211_set_disassoc(sdata, true);
        } else {
                list_del(&wk->list);
                kfree(wk);
@@ -1286,10 +1394,10 @@ ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
 
        reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
 
-       printk(KERN_DEBUG "%s: disassociated (Reason: %u)\n",
-                       sdata->dev->name, reason_code);
+       printk(KERN_DEBUG "%s: disassociated from %pM (Reason: %u)\n",
+                       sdata->dev->name, mgmt->sa, reason_code);
 
-       ieee80211_set_disassoc(sdata, ifmgd->associated->cbss.bssid, false);
+       ieee80211_set_disassoc(sdata, false);
        return RX_MGMT_CFG80211_DISASSOC;
 }
 
@@ -1348,8 +1456,7 @@ ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
                       sdata->dev->name, tu, ms);
                wk->timeout = jiffies + msecs_to_jiffies(ms);
                if (ms > IEEE80211_ASSOC_TIMEOUT)
-                       mod_timer(&ifmgd->timer,
-                                 jiffies + msecs_to_jiffies(ms));
+                       run_again(ifmgd, jiffies + msecs_to_jiffies(ms));
                return RX_MGMT_NONE;
        }
 
@@ -1391,9 +1498,6 @@ ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
                        return RX_MGMT_NONE;
                }
 
-               /* update new sta with its last rx activity */
-               sta->last_rx = jiffies;
-
                set_sta_flags(sta, WLAN_STA_AUTH | WLAN_STA_ASSOC |
                                   WLAN_STA_ASSOC_AP);
                if (!(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
@@ -1489,20 +1593,23 @@ ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
                                               wk->bss->cbss.bssid,
                                               ap_ht_cap_flags);
 
+        /* delete work item -- must be before set_associated for PS */
+       list_del(&wk->list);
+
        /* set AID and assoc capability,
         * ieee80211_set_associated() will tell the driver */
        bss_conf->aid = aid;
        bss_conf->assoc_capability = capab_info;
-       ieee80211_set_associated(sdata, wk->bss, changed);
+       /* this will take ownership of wk */
+       ieee80211_set_associated(sdata, wk, changed);
 
        /*
-        * initialise the time of last beacon to be the association time,
-        * otherwise beacon loss check will trigger immediately
+        * Start timer to probe the connection to the AP now.
+        * Also start the timer that will detect beacon loss.
         */
-       ifmgd->last_beacon = jiffies;
+       ieee80211_sta_rx_notify(sdata, (struct ieee80211_hdr *)mgmt);
+       mod_beacon_timer(sdata);
 
-       list_del(&wk->list);
-       kfree(wk);
        return RX_MGMT_CFG80211_ASSOC;
 }
 
@@ -1574,7 +1681,7 @@ static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
 
        /* direct probe may be part of the association flow */
        if (wk && wk->state == IEEE80211_MGD_STATE_PROBE) {
-               printk(KERN_DEBUG "%s direct probe responded\n",
+               printk(KERN_DEBUG "%s: direct probe responded\n",
                       sdata->dev->name);
                wk->tries = 0;
                wk->state = IEEE80211_MGD_STATE_AUTH;
@@ -1583,11 +1690,22 @@ static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
 
        if (ifmgd->associated &&
            memcmp(mgmt->bssid, ifmgd->associated->cbss.bssid, ETH_ALEN) == 0 &&
-           ifmgd->flags & IEEE80211_STA_PROBEREQ_POLL) {
-               ifmgd->flags &= ~IEEE80211_STA_PROBEREQ_POLL;
+           ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
+                           IEEE80211_STA_CONNECTION_POLL)) {
+               ifmgd->flags &= ~(IEEE80211_STA_CONNECTION_POLL |
+                                 IEEE80211_STA_BEACON_POLL);
                mutex_lock(&sdata->local->iflist_mtx);
                ieee80211_recalc_ps(sdata->local, -1);
                mutex_unlock(&sdata->local->iflist_mtx);
+               /*
+                * We've received a probe response, but are not sure whether
+                * we have or will be receiving any beacons or data, so let's
+                * schedule the timers again, just in case.
+                */
+               mod_beacon_timer(sdata);
+               mod_timer(&ifmgd->conn_mon_timer,
+                         round_jiffies_up(jiffies +
+                                          IEEE80211_CONNECTION_IDLE_TIME));
        }
 }
 
@@ -1637,27 +1755,41 @@ static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
        if (rx_status->freq != local->hw.conf.channel->center_freq)
                return;
 
-       if (WARN_ON(!ifmgd->associated))
+       /*
+        * We might have received a number of frames, among them a
+        * disassoc frame and a beacon...
+        */
+       if (!ifmgd->associated)
                return;
 
        bssid = ifmgd->associated->cbss.bssid;
 
-       if (WARN_ON(memcmp(bssid, mgmt->bssid, ETH_ALEN) != 0))
+       /*
+        * And in theory even frames from a different AP we were just
+        * associated to a split-second ago!
+        */
+       if (memcmp(bssid, mgmt->bssid, ETH_ALEN) != 0)
                return;
 
-       if (ifmgd->flags & IEEE80211_STA_PROBEREQ_POLL) {
+       if (ifmgd->flags & IEEE80211_STA_BEACON_POLL) {
 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
                if (net_ratelimit()) {
                        printk(KERN_DEBUG "%s: cancelling probereq poll due "
                               "to a received beacon\n", sdata->dev->name);
                }
 #endif
-               ifmgd->flags &= ~IEEE80211_STA_PROBEREQ_POLL;
+               ifmgd->flags &= ~IEEE80211_STA_BEACON_POLL;
                mutex_lock(&local->iflist_mtx);
                ieee80211_recalc_ps(local, -1);
                mutex_unlock(&local->iflist_mtx);
        }
 
+       /*
+        * Push the beacon loss detection into the future since
+        * we are processing a beacon from the AP just now.
+        */
+       mod_beacon_timer(sdata);
+
        ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
        ncrc = ieee802_11_parse_elems_crc(mgmt->u.beacon.variable,
                                          len - baselen, &elems,
@@ -1740,12 +1872,8 @@ static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
                                               bssid, ap_ht_cap_flags);
        }
 
+       /* Note: country IE parsing is done for us by cfg80211 */
        if (elems.country_elem) {
-               /* Note we are only reviewing this on beacons
-                * for the BSSID we are associated to */
-               regulatory_hint_11d(local->hw.wiphy,
-                       elems.country_elem, elems.country_elem_len);
-
                /* TODO: IBSS also needs this */
                if (elems.pwr_constr_elem)
                        ieee80211_handle_pwr_constr(sdata,
@@ -1781,7 +1909,7 @@ ieee80211_rx_result ieee80211_sta_rx_mgmt(struct ieee80211_sub_if_data *sdata,
        case IEEE80211_STYPE_DISASSOC:
        case IEEE80211_STYPE_ACTION:
                skb_queue_tail(&sdata->u.mgd.skb_queue, skb);
-               queue_work(local->hw.workqueue, &sdata->u.mgd.work);
+               ieee80211_queue_work(&local->hw, &sdata->u.mgd.work);
                return RX_QUEUED;
        }
 
@@ -1894,6 +2022,9 @@ static void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
        case RX_MGMT_CFG80211_ASSOC:
                cfg80211_send_rx_assoc(sdata->dev, (u8 *) mgmt, skb->len);
                break;
+       case RX_MGMT_CFG80211_DEAUTH:
+               cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
+               break;
        default:
                WARN(1, "unexpected: %d", rma);
        }
@@ -1914,7 +2045,7 @@ static void ieee80211_sta_timer(unsigned long data)
                return;
        }
 
-       queue_work(local->hw.workqueue, &ifmgd->work);
+       ieee80211_queue_work(&local->hw, &ifmgd->work);
 }
 
 static void ieee80211_sta_work(struct work_struct *work)
@@ -1932,20 +2063,18 @@ static void ieee80211_sta_work(struct work_struct *work)
        if (!netif_running(sdata->dev))
                return;
 
-       if (local->sw_scanning || local->hw_scanning)
+       if (local->scanning)
                return;
 
        if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
                return;
 
        /*
-        * Nothing should have been stuffed into the workqueue during
-        * the suspend->resume cycle. If this WARN is seen then there
-        * is a bug with either the driver suspend or something in
-        * mac80211 stuffing into the workqueue which we haven't yet
-        * cleared during mac80211's suspend cycle.
+        * ieee80211_queue_work() should have picked up most cases,
+        * here we'll pick the the rest.
         */
-       if (WARN_ON(local->suspended))
+       if (WARN(local->suspended, "STA MLME work scheduled while "
+                "going to suspend\n"))
                return;
 
        ifmgd = &sdata->u.mgd;
@@ -1957,28 +2086,60 @@ static void ieee80211_sta_work(struct work_struct *work)
        /* then process the rest of the work */
        mutex_lock(&ifmgd->mtx);
 
-       list_for_each_entry(wk, &ifmgd->work_list, list) {
-               if (wk->state != IEEE80211_MGD_STATE_IDLE) {
-                       anybusy = true;
-                       break;
+       if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
+                           IEEE80211_STA_CONNECTION_POLL) &&
+           ifmgd->associated) {
+               u8 bssid[ETH_ALEN];
+
+               memcpy(bssid, ifmgd->associated->cbss.bssid, ETH_ALEN);
+               if (time_is_after_jiffies(ifmgd->probe_timeout))
+                       run_again(ifmgd, ifmgd->probe_timeout);
+
+               else if (ifmgd->probe_send_count < IEEE80211_MAX_PROBE_TRIES) {
+#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
+                       printk(KERN_DEBUG "No probe response from AP %pM"
+                               " after %dms, try %d\n", bssid,
+                               (1000 * IEEE80211_PROBE_WAIT)/HZ,
+                               ifmgd->probe_send_count);
+#endif
+                       ieee80211_mgd_probe_ap_send(sdata);
+               } else {
+                       /*
+                        * We actually lost the connection ... or did we?
+                        * Let's make sure!
+                        */
+                       ifmgd->flags &= ~(IEEE80211_STA_CONNECTION_POLL |
+                                         IEEE80211_STA_BEACON_POLL);
+                       printk(KERN_DEBUG "No probe response from AP %pM"
+                               " after %dms, disconnecting.\n",
+                               bssid, (1000 * IEEE80211_PROBE_WAIT)/HZ);
+                       ieee80211_set_disassoc(sdata, true);
+                       mutex_unlock(&ifmgd->mtx);
+                       /*
+                        * must be outside lock due to cfg80211,
+                        * but that's not a problem.
+                        */
+                       ieee80211_send_deauth_disassoc(sdata, bssid,
+                                       IEEE80211_STYPE_DEAUTH,
+                                       WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
+                                       NULL);
+                       mutex_lock(&ifmgd->mtx);
                }
        }
 
-       ieee80211_recalc_idle(local);
-
-       if (!anybusy) {
-               mutex_unlock(&ifmgd->mtx);
 
-               if (test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifmgd->request))
-                       queue_delayed_work(local->hw.workqueue,
-                                          &local->scan_work,
-                                          round_jiffies_relative(0));
-               return;
-       }
+       ieee80211_recalc_idle(local);
 
        list_for_each_entry_safe(wk, tmp, &ifmgd->work_list, list) {
-               if (time_before(jiffies, wk->timeout))
+               if (time_is_after_jiffies(wk->timeout)) {
+                       /*
+                        * This work item isn't supposed to be worked on
+                        * right now, but take care to adjust the timer
+                        * properly.
+                        */
+                       run_again(ifmgd, wk->timeout);
                        continue;
+               }
 
                switch (wk->state) {
                default:
@@ -2014,6 +2175,18 @@ static void ieee80211_sta_work(struct work_struct *work)
                }
        }
 
+       list_for_each_entry(wk, &ifmgd->work_list, list) {
+               if (wk->state != IEEE80211_MGD_STATE_IDLE) {
+                       anybusy = true;
+                       break;
+               }
+       }
+       if (!anybusy &&
+           test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifmgd->request))
+               ieee80211_queue_delayed_work(&local->hw,
+                                            &local->scan_work,
+                                            round_jiffies_relative(0));
+
        mutex_unlock(&ifmgd->mtx);
 
        list_for_each_entry_safe(wk, tmp, &free_work, list) {
@@ -2037,16 +2210,51 @@ static void ieee80211_sta_work(struct work_struct *work)
        ieee80211_recalc_idle(local);
 }
 
+static void ieee80211_sta_bcn_mon_timer(unsigned long data)
+{
+       struct ieee80211_sub_if_data *sdata =
+               (struct ieee80211_sub_if_data *) data;
+       struct ieee80211_local *local = sdata->local;
+
+       if (local->quiescing)
+               return;
+
+       ieee80211_queue_work(&sdata->local->hw, &sdata->u.mgd.beacon_loss_work);
+}
+
+static void ieee80211_sta_conn_mon_timer(unsigned long data)
+{
+       struct ieee80211_sub_if_data *sdata =
+               (struct ieee80211_sub_if_data *) data;
+       struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
+       struct ieee80211_local *local = sdata->local;
+
+       if (local->quiescing)
+               return;
+
+       ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
+}
+
+static void ieee80211_sta_monitor_work(struct work_struct *work)
+{
+       struct ieee80211_sub_if_data *sdata =
+               container_of(work, struct ieee80211_sub_if_data,
+                            u.mgd.monitor_work);
+
+       ieee80211_mgd_probe_ap(sdata, false);
+}
+
 static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
 {
        if (sdata->vif.type == NL80211_IFTYPE_STATION) {
-               /*
-                * Need to update last_beacon to avoid beacon loss
-                * test to trigger.
-                */
-               sdata->u.mgd.last_beacon = jiffies;
-
-               queue_work(sdata->local->hw.workqueue,
+               sdata->u.mgd.flags &= ~(IEEE80211_STA_BEACON_POLL |
+                                       IEEE80211_STA_CONNECTION_POLL);
+
+               /* let's probe the connection once */
+               ieee80211_queue_work(&sdata->local->hw,
+                          &sdata->u.mgd.monitor_work);
+               /* and do all the other regular work too */
+               ieee80211_queue_work(&sdata->local->hw,
                           &sdata->u.mgd.work);
        }
 }
@@ -2070,6 +2278,11 @@ void ieee80211_sta_quiesce(struct ieee80211_sub_if_data *sdata)
        cancel_work_sync(&ifmgd->chswitch_work);
        if (del_timer_sync(&ifmgd->chswitch_timer))
                set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
+
+       cancel_work_sync(&ifmgd->monitor_work);
+       /* these will just be re-established on connection */
+       del_timer_sync(&ifmgd->conn_mon_timer);
+       del_timer_sync(&ifmgd->bcn_mon_timer);
 }
 
 void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
@@ -2090,10 +2303,15 @@ void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
 
        ifmgd = &sdata->u.mgd;
        INIT_WORK(&ifmgd->work, ieee80211_sta_work);
+       INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
        INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
        INIT_WORK(&ifmgd->beacon_loss_work, ieee80211_beacon_loss_work);
        setup_timer(&ifmgd->timer, ieee80211_sta_timer,
                    (unsigned long) sdata);
+       setup_timer(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer,
+                   (unsigned long) sdata);
+       setup_timer(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer,
+                   (unsigned long) sdata);
        setup_timer(&ifmgd->chswitch_timer, ieee80211_chswitch_timer,
                    (unsigned long) sdata);
        skb_queue_head_init(&ifmgd->skb_queue);
@@ -2172,12 +2390,19 @@ int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
                wk->ie_len = req->ie_len;
        }
 
+       if (req->key && req->key_len) {
+               wk->key_len = req->key_len;
+               wk->key_idx = req->key_idx;
+               memcpy(wk->key, req->key, req->key_len);
+       }
+
        ssid = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
        memcpy(wk->ssid, ssid + 2, ssid[1]);
        wk->ssid_len = ssid[1];
 
        wk->state = IEEE80211_MGD_STATE_PROBE;
        wk->auth_alg = auth_alg;
+       wk->timeout = jiffies; /* run right away */
 
        /*
         * XXX: if still associated need to tell AP that we're going
@@ -2190,7 +2415,7 @@ int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
        list_add(&wk->list, &sdata->u.mgd.work_list);
        mutex_unlock(&ifmgd->mtx);
 
-       queue_work(sdata->local->hw.workqueue, &sdata->u.mgd.work);
+       ieee80211_queue_work(&sdata->local->hw, &sdata->u.mgd.work);
        return 0;
 }
 
@@ -2249,6 +2474,7 @@ int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
 
        wk->state = IEEE80211_MGD_STATE_ASSOC;
        wk->tries = 0;
+       wk->timeout = jiffies; /* run right away */
 
        if (req->use_mfp) {
                ifmgd->mfp = IEEE80211_MFP_REQUIRED;
@@ -2263,7 +2489,7 @@ int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
        else
                ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
 
-       queue_work(sdata->local->hw.workqueue, &sdata->u.mgd.work);
+       ieee80211_queue_work(&sdata->local->hw, &sdata->u.mgd.work);
 
        err = 0;
 
@@ -2273,20 +2499,18 @@ int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
 }
 
 int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
-                        struct cfg80211_deauth_request *req)
+                        struct cfg80211_deauth_request *req,
+                        void *cookie)
 {
        struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
        struct ieee80211_mgd_work *wk;
        const u8 *bssid = NULL;
 
-       printk(KERN_DEBUG "%s: deauthenticating by local choice (reason=%d)\n",
-              sdata->dev->name, req->reason_code);
-
        mutex_lock(&ifmgd->mtx);
 
        if (ifmgd->associated && &ifmgd->associated->cbss == req->bss) {
                bssid = req->bss->bssid;
-               ieee80211_set_disassoc(sdata, bssid, true);
+               ieee80211_set_disassoc(sdata, true);
        } else list_for_each_entry(wk, &ifmgd->work_list, list) {
                if (&wk->bss->cbss == req->bss) {
                        bssid = req->bss->bssid;
@@ -2296,41 +2520,57 @@ int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
                }
        }
 
-       /* cfg80211 should catch this... */
-       if (WARN_ON(!bssid)) {
+       /*
+        * cfg80211 should catch this ... but it's racy since
+        * we can receive a deauth frame, process it, hand it
+        * to cfg80211 while that's in a locked section already
+        * trying to tell us that the user wants to disconnect.
+        */
+       if (!bssid) {
                mutex_unlock(&ifmgd->mtx);
                return -ENOLINK;
        }
 
        mutex_unlock(&ifmgd->mtx);
 
+       printk(KERN_DEBUG "%s: deauthenticating from %pM by local choice (reason=%d)\n",
+              sdata->dev->name, bssid, req->reason_code);
+
        ieee80211_send_deauth_disassoc(sdata, bssid,
-                       IEEE80211_STYPE_DEAUTH, req->reason_code);
+                       IEEE80211_STYPE_DEAUTH, req->reason_code,
+                       cookie);
 
        return 0;
 }
 
 int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
-                          struct cfg80211_disassoc_request *req)
+                          struct cfg80211_disassoc_request *req,
+                          void *cookie)
 {
        struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
 
-       printk(KERN_DEBUG "%s: disassociating by local choice (reason=%d)\n",
-              sdata->dev->name, req->reason_code);
-
        mutex_lock(&ifmgd->mtx);
 
-       /* cfg80211 should catch that */
-       if (WARN_ON(&ifmgd->associated->cbss != req->bss)) {
+       /*
+        * cfg80211 should catch this ... but it's racy since
+        * we can receive a disassoc frame, process it, hand it
+        * to cfg80211 while that's in a locked section already
+        * trying to tell us that the user wants to disconnect.
+        */
+       if (&ifmgd->associated->cbss != req->bss) {
                mutex_unlock(&ifmgd->mtx);
                return -ENOLINK;
        }
 
-       ieee80211_set_disassoc(sdata, req->bss->bssid, false);
+       printk(KERN_DEBUG "%s: disassociating from %pM by local choice (reason=%d)\n",
+              sdata->dev->name, req->bss->bssid, req->reason_code);
+
+       ieee80211_set_disassoc(sdata, false);
 
        mutex_unlock(&ifmgd->mtx);
 
        ieee80211_send_deauth_disassoc(sdata, req->bss->bssid,
-                       IEEE80211_STYPE_DISASSOC, req->reason_code);
+                       IEEE80211_STYPE_DISASSOC, req->reason_code,
+                       cookie);
        return 0;
 }