mac80211: add missing sanity checks for action frames
[safe/jmp/linux-2.6] / net / mac80211 / mlme.c
index 0779ba1..05a18f4 100644 (file)
@@ -31,6 +31,7 @@
 #define IEEE80211_AUTH_MAX_TRIES 3
 #define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
 #define IEEE80211_ASSOC_MAX_TRIES 3
+#define IEEE80211_MAX_PROBE_TRIES 5
 
 /*
  * beacon loss detection timeout
  * 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 (2 * HZ)
+#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 / 5)
+#define IEEE80211_PROBE_WAIT           (HZ / 2)
 
 #define TMR_RUNNING_TIMER      0
 #define TMR_RUNNING_CHANSW     1
@@ -425,7 +426,8 @@ static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata,
                memcpy(pos, &sband->ht_cap.mcs, sizeof(sband->ht_cap.mcs));
        }
 
-       ieee80211_tx_skb(sdata, skb, 0);
+       IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
+       ieee80211_tx_skb(sdata, skb);
 }
 
 
@@ -457,10 +459,18 @@ 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, cookie);
+               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, cookie);
-       ieee80211_tx_skb(sdata, skb, ifmgd->flags & IEEE80211_STA_MFP_ENABLED);
+               if (cookie)
+                       __cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
+               else
+                       cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
+       if (!(ifmgd->flags & IEEE80211_STA_MFP_ENABLED))
+               IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
+       ieee80211_tx_skb(sdata, skb);
 }
 
 void ieee80211_send_pspoll(struct ieee80211_local *local,
@@ -491,7 +501,8 @@ void ieee80211_send_pspoll(struct ieee80211_local *local,
        memcpy(pspoll->bssid, ifmgd->bssid, ETH_ALEN);
        memcpy(pspoll->ta, sdata->dev->dev_addr, ETH_ALEN);
 
-       ieee80211_tx_skb(sdata, skb, 0);
+       IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
+       ieee80211_tx_skb(sdata, skb);
 }
 
 void ieee80211_send_nullfunc(struct ieee80211_local *local,
@@ -524,7 +535,8 @@ void ieee80211_send_nullfunc(struct ieee80211_local *local,
        memcpy(nullfunc->addr2, sdata->dev->dev_addr, ETH_ALEN);
        memcpy(nullfunc->addr3, sdata->u.mgd.bssid, ETH_ALEN);
 
-       ieee80211_tx_skb(sdata, skb, 0);
+       IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
+       ieee80211_tx_skb(sdata, skb);
 }
 
 /* spectrum management related things */
@@ -879,10 +891,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 */
@@ -895,12 +908,21 @@ 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);
 
+       /*
+        * Always handle WMM once after association regardless
+        * of the first value the AP uses. Setting -1 here has
+        * that effect because the AP values is an unsigned
+        * 4-bit value.
+        */
+       sdata->u.mgd.wmm_last_param_set = -1;
+
        ieee80211_led_assoc(local, 1);
 
        sdata->vif.bss_conf.assoc = 1;
@@ -916,12 +938,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);
@@ -1012,7 +1031,8 @@ ieee80211_authenticate(struct ieee80211_sub_if_data *sdata,
        return RX_MGMT_NONE;
 }
 
-static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata)
+static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
+                                  bool deauth)
 {
        struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
        struct ieee80211_local *local = sdata->local;
@@ -1030,6 +1050,16 @@ static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata)
        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
@@ -1061,8 +1091,6 @@ static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata)
 
        ieee80211_set_wmm_default(sdata);
 
-       ieee80211_recalc_idle(local);
-
        /* channel(_type) changes are handled by ieee80211_hw_config */
        local->oper_channel_type = NL80211_CHAN_NO_HT;
 
@@ -1156,11 +1184,24 @@ void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
                  round_jiffies_up(jiffies + IEEE80211_CONNECTION_IDLE_TIME));
 }
 
+static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
+{
+       struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
+       const u8 *ssid;
+
+       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;
-       const u8 *ssid;
        bool already = false;
 
        if (!netif_running(sdata->dev))
@@ -1203,18 +1244,12 @@ static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
        if (already)
                goto out;
 
-       ifmgd->probe_timeout = jiffies + IEEE80211_PROBE_WAIT;
-
        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);
-
-       run_again(ifmgd, ifmgd->probe_timeout);
-
+       ifmgd->probe_send_count = 0;
+       ieee80211_mgd_probe_ap_send(sdata);
  out:
        mutex_unlock(&ifmgd->mtx);
 }
@@ -1340,7 +1375,8 @@ ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
                        sdata->dev->name, bssid, reason_code);
 
        if (!wk) {
-               ieee80211_set_disassoc(sdata);
+               ieee80211_set_disassoc(sdata, true);
+               ieee80211_recalc_idle(sdata->local);
        } else {
                list_del(&wk->list);
                kfree(wk);
@@ -1370,10 +1406,11 @@ 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);
+       ieee80211_set_disassoc(sdata, false);
+       ieee80211_recalc_idle(sdata->local);
        return RX_MGMT_CFG80211_DISASSOC;
 }
 
@@ -1439,8 +1476,7 @@ ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
        if (status_code != WLAN_STATUS_SUCCESS) {
                printk(KERN_DEBUG "%s: AP denied association (code=%d)\n",
                       sdata->dev->name, status_code);
-               list_del(&wk->list);
-               kfree(wk);
+               wk->state = IEEE80211_MGD_STATE_IDLE;
                return RX_MGMT_CFG80211_ASSOC;
        }
 
@@ -1569,11 +1605,15 @@ 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);
 
        /*
         * Start timer to probe the connection to the AP now.
@@ -1582,8 +1622,6 @@ ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
        ieee80211_sta_rx_notify(sdata, (struct ieee80211_hdr *)mgmt);
        mod_beacon_timer(sdata);
 
-       list_del(&wk->list);
-       kfree(wk);
        return RX_MGMT_CFG80211_ASSOC;
 }
 
@@ -1655,7 +1693,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;
@@ -1846,12 +1884,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,
@@ -1877,7 +1911,6 @@ ieee80211_rx_result ieee80211_sta_rx_mgmt(struct ieee80211_sub_if_data *sdata,
        fc = le16_to_cpu(mgmt->frame_control);
 
        switch (fc & IEEE80211_FCTL_STYPE) {
-       case IEEE80211_STYPE_PROBE_REQ:
        case IEEE80211_STYPE_PROBE_RESP:
        case IEEE80211_STYPE_BEACON:
        case IEEE80211_STYPE_AUTH:
@@ -1930,7 +1963,9 @@ static void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
                        rma = ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
                        break;
                case IEEE80211_STYPE_ACTION:
-                       /* XXX: differentiate, can only happen for CSA now! */
+                       if (mgmt->u.action.category != WLAN_CATEGORY_SPECTRUM_MGMT)
+                               break;
+
                        ieee80211_sta_process_chanswitch(sdata,
                                        &mgmt->u.action.u.chan_switch.sw_elem,
                                        ifmgd->associated);
@@ -1943,12 +1978,10 @@ static void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
                        /* no action */
                        break;
                case RX_MGMT_CFG80211_DEAUTH:
-                       cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len,
-                                            NULL);
+                       cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
                        break;
                case RX_MGMT_CFG80211_DISASSOC:
-                       cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len,
-                                              NULL);
+                       cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
                        break;
                default:
                        WARN(1, "unexpected: %d", rma);
@@ -2003,7 +2036,7 @@ static void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
                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, NULL);
+               cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
                break;
        default:
                WARN(1, "unexpected: %d", rma);
@@ -2069,21 +2102,32 @@ static void ieee80211_sta_work(struct work_struct *work)
        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 {
-                       u8 bssid[ETH_ALEN];
+
+               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);
-                       memcpy(bssid, ifmgd->associated->cbss.bssid, ETH_ALEN);
                        printk(KERN_DEBUG "No probe response from AP %pM"
                                " after %dms, disconnecting.\n",
                                bssid, (1000 * IEEE80211_PROBE_WAIT)/HZ);
-                       ieee80211_set_disassoc(sdata);
+                       ieee80211_set_disassoc(sdata, true);
+                       ieee80211_recalc_idle(local);
                        mutex_unlock(&ifmgd->mtx);
                        /*
                         * must be outside lock due to cfg80211,
@@ -2097,25 +2141,9 @@ 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;
-               }
-       }
 
        ieee80211_recalc_idle(local);
 
-       if (!anybusy) {
-               mutex_unlock(&ifmgd->mtx);
-
-               if (test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifmgd->request))
-                       ieee80211_queue_delayed_work(&local->hw,
-                                                    &local->scan_work,
-                                                    round_jiffies_relative(0));
-               return;
-       }
-
        list_for_each_entry_safe(wk, tmp, &ifmgd->work_list, list) {
                if (time_is_after_jiffies(wk->timeout)) {
                        /*
@@ -2161,6 +2189,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) {
@@ -2479,18 +2519,18 @@ int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
        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);
+       bool not_auth_yet = false;
 
        mutex_lock(&ifmgd->mtx);
 
        if (ifmgd->associated && &ifmgd->associated->cbss == req->bss) {
                bssid = req->bss->bssid;
-               ieee80211_set_disassoc(sdata);
+               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;
+                       if (wk->state == IEEE80211_MGD_STATE_PROBE)
+                               not_auth_yet = true;
                        list_del(&wk->list);
                        kfree(wk);
                        break;
@@ -2498,6 +2538,20 @@ int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
        }
 
        /*
+        * If somebody requests authentication and we haven't
+        * sent out an auth frame yet there's no need to send
+        * out a deauth frame either. If the state was PROBE,
+        * then this is the case. If it's AUTH we have sent a
+        * frame, and if it's IDLE we have completed the auth
+        * process already.
+        */
+       if (not_auth_yet) {
+               mutex_unlock(&ifmgd->mtx);
+               __cfg80211_auth_canceled(sdata->dev, bssid);
+               return 0;
+       }
+
+       /*
         * 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
@@ -2510,10 +2564,15 @@ int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
 
        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,
                        cookie);
 
+       ieee80211_recalc_idle(sdata->local);
+
        return 0;
 }
 
@@ -2523,9 +2582,6 @@ int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
 {
        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);
 
        /*
@@ -2539,12 +2595,18 @@ int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
                return -ENOLINK;
        }
 
-       ieee80211_set_disassoc(sdata);
+       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,
                        cookie);
+
+       ieee80211_recalc_idle(sdata->local);
+
        return 0;
 }