]> Pileus Git - ~andy/linux/blob - net/mac80211/mlme.c
mac80211: disable powersave if pm_qos asks for low latency
[~andy/linux] / net / mac80211 / mlme.c
1 /*
2  * BSS client mode implementation
3  * Copyright 2003-2008, Jouni Malinen <j@w1.fi>
4  * Copyright 2004, Instant802 Networks, Inc.
5  * Copyright 2005, Devicescape Software, Inc.
6  * Copyright 2006-2007  Jiri Benc <jbenc@suse.cz>
7  * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  */
13
14 #include <linux/delay.h>
15 #include <linux/if_ether.h>
16 #include <linux/skbuff.h>
17 #include <linux/if_arp.h>
18 #include <linux/etherdevice.h>
19 #include <linux/rtnetlink.h>
20 #include <linux/pm_qos_params.h>
21 #include <net/mac80211.h>
22 #include <asm/unaligned.h>
23
24 #include "ieee80211_i.h"
25 #include "rate.h"
26 #include "led.h"
27
28 #define IEEE80211_ASSOC_SCANS_MAX_TRIES 2
29 #define IEEE80211_AUTH_TIMEOUT (HZ / 5)
30 #define IEEE80211_AUTH_MAX_TRIES 3
31 #define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
32 #define IEEE80211_ASSOC_MAX_TRIES 3
33 #define IEEE80211_MONITORING_INTERVAL (2 * HZ)
34 #define IEEE80211_PROBE_IDLE_TIME (60 * HZ)
35 #define IEEE80211_RETRY_AUTH_INTERVAL (1 * HZ)
36
37 /* utils */
38 static int ecw2cw(int ecw)
39 {
40         return (1 << ecw) - 1;
41 }
42
43 static u8 *ieee80211_bss_get_ie(struct ieee80211_bss *bss, u8 ie)
44 {
45         u8 *end, *pos;
46
47         pos = bss->cbss.information_elements;
48         if (pos == NULL)
49                 return NULL;
50         end = pos + bss->cbss.len_information_elements;
51
52         while (pos + 1 < end) {
53                 if (pos + 2 + pos[1] > end)
54                         break;
55                 if (pos[0] == ie)
56                         return pos;
57                 pos += 2 + pos[1];
58         }
59
60         return NULL;
61 }
62
63 static int ieee80211_compatible_rates(struct ieee80211_bss *bss,
64                                       struct ieee80211_supported_band *sband,
65                                       u32 *rates)
66 {
67         int i, j, count;
68         *rates = 0;
69         count = 0;
70         for (i = 0; i < bss->supp_rates_len; i++) {
71                 int rate = (bss->supp_rates[i] & 0x7F) * 5;
72
73                 for (j = 0; j < sband->n_bitrates; j++)
74                         if (sband->bitrates[j].bitrate == rate) {
75                                 *rates |= BIT(j);
76                                 count++;
77                                 break;
78                         }
79         }
80
81         return count;
82 }
83
84 /*
85  * ieee80211_enable_ht should be called only after the operating band
86  * has been determined as ht configuration depends on the hw's
87  * HT abilities for a specific band.
88  */
89 static u32 ieee80211_enable_ht(struct ieee80211_sub_if_data *sdata,
90                                struct ieee80211_ht_info *hti,
91                                u16 ap_ht_cap_flags)
92 {
93         struct ieee80211_local *local = sdata->local;
94         struct ieee80211_supported_band *sband;
95         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
96         struct ieee80211_bss_ht_conf ht;
97         struct sta_info *sta;
98         u32 changed = 0;
99         bool enable_ht = true, ht_changed;
100         enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
101
102         sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
103
104         memset(&ht, 0, sizeof(ht));
105
106         /* HT is not supported */
107         if (!sband->ht_cap.ht_supported)
108                 enable_ht = false;
109
110         /* check that channel matches the right operating channel */
111         if (local->hw.conf.channel->center_freq !=
112             ieee80211_channel_to_frequency(hti->control_chan))
113                 enable_ht = false;
114
115         if (enable_ht) {
116                 channel_type = NL80211_CHAN_HT20;
117
118                 if (!(ap_ht_cap_flags & IEEE80211_HT_CAP_40MHZ_INTOLERANT) &&
119                     (sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) &&
120                     (hti->ht_param & IEEE80211_HT_PARAM_CHAN_WIDTH_ANY)) {
121                         switch(hti->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
122                         case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
123                                 channel_type = NL80211_CHAN_HT40PLUS;
124                                 break;
125                         case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
126                                 channel_type = NL80211_CHAN_HT40MINUS;
127                                 break;
128                         }
129                 }
130         }
131
132         ht_changed = conf_is_ht(&local->hw.conf) != enable_ht ||
133                      channel_type != local->hw.conf.channel_type;
134
135         local->oper_channel_type = channel_type;
136
137         if (ht_changed) {
138                 /* channel_type change automatically detected */
139                 ieee80211_hw_config(local, 0);
140
141                 rcu_read_lock();
142
143                 sta = sta_info_get(local, ifmgd->bssid);
144                 if (sta)
145                         rate_control_rate_update(local, sband, sta,
146                                                  IEEE80211_RC_HT_CHANGED);
147
148                 rcu_read_unlock();
149
150         }
151
152         /* disable HT */
153         if (!enable_ht)
154                 return 0;
155
156         ht.operation_mode = le16_to_cpu(hti->operation_mode);
157
158         /* if bss configuration changed store the new one */
159         if (memcmp(&sdata->vif.bss_conf.ht, &ht, sizeof(ht))) {
160                 changed |= BSS_CHANGED_HT;
161                 sdata->vif.bss_conf.ht = ht;
162         }
163
164         return changed;
165 }
166
167 /* frame sending functions */
168
169 static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata)
170 {
171         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
172         struct ieee80211_local *local = sdata->local;
173         struct sk_buff *skb;
174         struct ieee80211_mgmt *mgmt;
175         u8 *pos, *ies, *ht_ie;
176         int i, len, count, rates_len, supp_rates_len;
177         u16 capab;
178         struct ieee80211_bss *bss;
179         int wmm = 0;
180         struct ieee80211_supported_band *sband;
181         u32 rates = 0;
182
183         skb = dev_alloc_skb(local->hw.extra_tx_headroom +
184                             sizeof(*mgmt) + 200 + ifmgd->extra_ie_len +
185                             ifmgd->ssid_len);
186         if (!skb) {
187                 printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
188                        "frame\n", sdata->dev->name);
189                 return;
190         }
191         skb_reserve(skb, local->hw.extra_tx_headroom);
192
193         sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
194
195         capab = ifmgd->capab;
196
197         if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) {
198                 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
199                         capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
200                 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
201                         capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
202         }
203
204         bss = ieee80211_rx_bss_get(local, ifmgd->bssid,
205                                    local->hw.conf.channel->center_freq,
206                                    ifmgd->ssid, ifmgd->ssid_len);
207         if (bss) {
208                 if (bss->cbss.capability & WLAN_CAPABILITY_PRIVACY)
209                         capab |= WLAN_CAPABILITY_PRIVACY;
210                 if (bss->wmm_used)
211                         wmm = 1;
212
213                 /* get all rates supported by the device and the AP as
214                  * some APs don't like getting a superset of their rates
215                  * in the association request (e.g. D-Link DAP 1353 in
216                  * b-only mode) */
217                 rates_len = ieee80211_compatible_rates(bss, sband, &rates);
218
219                 if ((bss->cbss.capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
220                     (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
221                         capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
222
223                 ieee80211_rx_bss_put(local, bss);
224         } else {
225                 rates = ~0;
226                 rates_len = sband->n_bitrates;
227         }
228
229         mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
230         memset(mgmt, 0, 24);
231         memcpy(mgmt->da, ifmgd->bssid, ETH_ALEN);
232         memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
233         memcpy(mgmt->bssid, ifmgd->bssid, ETH_ALEN);
234
235         if (ifmgd->flags & IEEE80211_STA_PREV_BSSID_SET) {
236                 skb_put(skb, 10);
237                 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
238                                                   IEEE80211_STYPE_REASSOC_REQ);
239                 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
240                 mgmt->u.reassoc_req.listen_interval =
241                                 cpu_to_le16(local->hw.conf.listen_interval);
242                 memcpy(mgmt->u.reassoc_req.current_ap, ifmgd->prev_bssid,
243                        ETH_ALEN);
244         } else {
245                 skb_put(skb, 4);
246                 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
247                                                   IEEE80211_STYPE_ASSOC_REQ);
248                 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
249                 mgmt->u.assoc_req.listen_interval =
250                                 cpu_to_le16(local->hw.conf.listen_interval);
251         }
252
253         /* SSID */
254         ies = pos = skb_put(skb, 2 + ifmgd->ssid_len);
255         *pos++ = WLAN_EID_SSID;
256         *pos++ = ifmgd->ssid_len;
257         memcpy(pos, ifmgd->ssid, ifmgd->ssid_len);
258
259         /* add all rates which were marked to be used above */
260         supp_rates_len = rates_len;
261         if (supp_rates_len > 8)
262                 supp_rates_len = 8;
263
264         len = sband->n_bitrates;
265         pos = skb_put(skb, supp_rates_len + 2);
266         *pos++ = WLAN_EID_SUPP_RATES;
267         *pos++ = supp_rates_len;
268
269         count = 0;
270         for (i = 0; i < sband->n_bitrates; i++) {
271                 if (BIT(i) & rates) {
272                         int rate = sband->bitrates[i].bitrate;
273                         *pos++ = (u8) (rate / 5);
274                         if (++count == 8)
275                                 break;
276                 }
277         }
278
279         if (rates_len > count) {
280                 pos = skb_put(skb, rates_len - count + 2);
281                 *pos++ = WLAN_EID_EXT_SUPP_RATES;
282                 *pos++ = rates_len - count;
283
284                 for (i++; i < sband->n_bitrates; i++) {
285                         if (BIT(i) & rates) {
286                                 int rate = sband->bitrates[i].bitrate;
287                                 *pos++ = (u8) (rate / 5);
288                         }
289                 }
290         }
291
292         if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
293                 /* 1. power capabilities */
294                 pos = skb_put(skb, 4);
295                 *pos++ = WLAN_EID_PWR_CAPABILITY;
296                 *pos++ = 2;
297                 *pos++ = 0; /* min tx power */
298                 *pos++ = local->hw.conf.channel->max_power; /* max tx power */
299
300                 /* 2. supported channels */
301                 /* TODO: get this in reg domain format */
302                 pos = skb_put(skb, 2 * sband->n_channels + 2);
303                 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
304                 *pos++ = 2 * sband->n_channels;
305                 for (i = 0; i < sband->n_channels; i++) {
306                         *pos++ = ieee80211_frequency_to_channel(
307                                         sband->channels[i].center_freq);
308                         *pos++ = 1; /* one channel in the subband*/
309                 }
310         }
311
312         if (ifmgd->extra_ie) {
313                 pos = skb_put(skb, ifmgd->extra_ie_len);
314                 memcpy(pos, ifmgd->extra_ie, ifmgd->extra_ie_len);
315         }
316
317         if (wmm && (ifmgd->flags & IEEE80211_STA_WMM_ENABLED)) {
318                 pos = skb_put(skb, 9);
319                 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
320                 *pos++ = 7; /* len */
321                 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
322                 *pos++ = 0x50;
323                 *pos++ = 0xf2;
324                 *pos++ = 2; /* WME */
325                 *pos++ = 0; /* WME info */
326                 *pos++ = 1; /* WME ver */
327                 *pos++ = 0;
328         }
329
330         /* wmm support is a must to HT */
331         /*
332          * IEEE802.11n does not allow TKIP/WEP as pairwise
333          * ciphers in HT mode. We still associate in non-ht
334          * mode (11a/b/g) if any one of these ciphers is
335          * configured as pairwise.
336          */
337         if (wmm && (ifmgd->flags & IEEE80211_STA_WMM_ENABLED) &&
338             sband->ht_cap.ht_supported &&
339             (ht_ie = ieee80211_bss_get_ie(bss, WLAN_EID_HT_INFORMATION)) &&
340             ht_ie[1] >= sizeof(struct ieee80211_ht_info) &&
341             (!(ifmgd->flags & IEEE80211_STA_TKIP_WEP_USED))) {
342                 struct ieee80211_ht_info *ht_info =
343                         (struct ieee80211_ht_info *)(ht_ie + 2);
344                 u16 cap = sband->ht_cap.cap;
345                 __le16 tmp;
346                 u32 flags = local->hw.conf.channel->flags;
347
348                 switch (ht_info->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
349                 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
350                         if (flags & IEEE80211_CHAN_NO_FAT_ABOVE) {
351                                 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
352                                 cap &= ~IEEE80211_HT_CAP_SGI_40;
353                         }
354                         break;
355                 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
356                         if (flags & IEEE80211_CHAN_NO_FAT_BELOW) {
357                                 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
358                                 cap &= ~IEEE80211_HT_CAP_SGI_40;
359                         }
360                         break;
361                 }
362
363                 tmp = cpu_to_le16(cap);
364                 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap)+2);
365                 *pos++ = WLAN_EID_HT_CAPABILITY;
366                 *pos++ = sizeof(struct ieee80211_ht_cap);
367                 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
368                 memcpy(pos, &tmp, sizeof(u16));
369                 pos += sizeof(u16);
370                 /* TODO: needs a define here for << 2 */
371                 *pos++ = sband->ht_cap.ampdu_factor |
372                          (sband->ht_cap.ampdu_density << 2);
373                 memcpy(pos, &sband->ht_cap.mcs, sizeof(sband->ht_cap.mcs));
374         }
375
376         kfree(ifmgd->assocreq_ies);
377         ifmgd->assocreq_ies_len = (skb->data + skb->len) - ies;
378         ifmgd->assocreq_ies = kmalloc(ifmgd->assocreq_ies_len, GFP_KERNEL);
379         if (ifmgd->assocreq_ies)
380                 memcpy(ifmgd->assocreq_ies, ies, ifmgd->assocreq_ies_len);
381
382         ieee80211_tx_skb(sdata, skb, 0);
383 }
384
385
386 static void ieee80211_send_deauth_disassoc(struct ieee80211_sub_if_data *sdata,
387                                            u16 stype, u16 reason)
388 {
389         struct ieee80211_local *local = sdata->local;
390         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
391         struct sk_buff *skb;
392         struct ieee80211_mgmt *mgmt;
393
394         skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
395         if (!skb) {
396                 printk(KERN_DEBUG "%s: failed to allocate buffer for "
397                        "deauth/disassoc frame\n", sdata->dev->name);
398                 return;
399         }
400         skb_reserve(skb, local->hw.extra_tx_headroom);
401
402         mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
403         memset(mgmt, 0, 24);
404         memcpy(mgmt->da, ifmgd->bssid, ETH_ALEN);
405         memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
406         memcpy(mgmt->bssid, ifmgd->bssid, ETH_ALEN);
407         mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | stype);
408         skb_put(skb, 2);
409         /* u.deauth.reason_code == u.disassoc.reason_code */
410         mgmt->u.deauth.reason_code = cpu_to_le16(reason);
411
412         if (stype == IEEE80211_STYPE_DEAUTH)
413                 cfg80211_send_deauth(sdata->dev, (u8 *) mgmt, skb->len);
414         else
415                 cfg80211_send_disassoc(sdata->dev, (u8 *) mgmt, skb->len);
416         ieee80211_tx_skb(sdata, skb, ifmgd->flags & IEEE80211_STA_MFP_ENABLED);
417 }
418
419 void ieee80211_send_pspoll(struct ieee80211_local *local,
420                            struct ieee80211_sub_if_data *sdata)
421 {
422         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
423         struct ieee80211_pspoll *pspoll;
424         struct sk_buff *skb;
425         u16 fc;
426
427         skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*pspoll));
428         if (!skb) {
429                 printk(KERN_DEBUG "%s: failed to allocate buffer for "
430                        "pspoll frame\n", sdata->dev->name);
431                 return;
432         }
433         skb_reserve(skb, local->hw.extra_tx_headroom);
434
435         pspoll = (struct ieee80211_pspoll *) skb_put(skb, sizeof(*pspoll));
436         memset(pspoll, 0, sizeof(*pspoll));
437         fc = IEEE80211_FTYPE_CTL | IEEE80211_STYPE_PSPOLL | IEEE80211_FCTL_PM;
438         pspoll->frame_control = cpu_to_le16(fc);
439         pspoll->aid = cpu_to_le16(ifmgd->aid);
440
441         /* aid in PS-Poll has its two MSBs each set to 1 */
442         pspoll->aid |= cpu_to_le16(1 << 15 | 1 << 14);
443
444         memcpy(pspoll->bssid, ifmgd->bssid, ETH_ALEN);
445         memcpy(pspoll->ta, sdata->dev->dev_addr, ETH_ALEN);
446
447         ieee80211_tx_skb(sdata, skb, 0);
448 }
449
450 void ieee80211_send_nullfunc(struct ieee80211_local *local,
451                              struct ieee80211_sub_if_data *sdata,
452                              int powersave)
453 {
454         struct sk_buff *skb;
455         struct ieee80211_hdr *nullfunc;
456         __le16 fc;
457
458         if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
459                 return;
460
461         skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24);
462         if (!skb) {
463                 printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
464                        "frame\n", sdata->dev->name);
465                 return;
466         }
467         skb_reserve(skb, local->hw.extra_tx_headroom);
468
469         nullfunc = (struct ieee80211_hdr *) skb_put(skb, 24);
470         memset(nullfunc, 0, 24);
471         fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
472                          IEEE80211_FCTL_TODS);
473         if (powersave)
474                 fc |= cpu_to_le16(IEEE80211_FCTL_PM);
475         nullfunc->frame_control = fc;
476         memcpy(nullfunc->addr1, sdata->u.mgd.bssid, ETH_ALEN);
477         memcpy(nullfunc->addr2, sdata->dev->dev_addr, ETH_ALEN);
478         memcpy(nullfunc->addr3, sdata->u.mgd.bssid, ETH_ALEN);
479
480         ieee80211_tx_skb(sdata, skb, 0);
481 }
482
483 /* powersave */
484 static void ieee80211_enable_ps(struct ieee80211_local *local,
485                                 struct ieee80211_sub_if_data *sdata)
486 {
487         struct ieee80211_conf *conf = &local->hw.conf;
488
489         if (conf->dynamic_ps_timeout > 0 &&
490             !(local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)) {
491                 mod_timer(&local->dynamic_ps_timer, jiffies +
492                           msecs_to_jiffies(conf->dynamic_ps_timeout));
493         } else {
494                 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
495                         ieee80211_send_nullfunc(local, sdata, 1);
496                 conf->flags |= IEEE80211_CONF_PS;
497                 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
498         }
499 }
500
501 static void ieee80211_change_ps(struct ieee80211_local *local)
502 {
503         struct ieee80211_conf *conf = &local->hw.conf;
504
505         if (local->ps_sdata) {
506                 if (!(local->ps_sdata->u.mgd.flags & IEEE80211_STA_ASSOCIATED))
507                         return;
508
509                 ieee80211_enable_ps(local, local->ps_sdata);
510         } else if (conf->flags & IEEE80211_CONF_PS) {
511                 conf->flags &= ~IEEE80211_CONF_PS;
512                 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
513                 del_timer_sync(&local->dynamic_ps_timer);
514                 cancel_work_sync(&local->dynamic_ps_enable_work);
515         }
516 }
517
518 /* need to hold RTNL or interface lock */
519 void ieee80211_recalc_ps(struct ieee80211_local *local, s32 latency)
520 {
521         struct ieee80211_sub_if_data *sdata, *found = NULL;
522         int count = 0;
523
524         if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS)) {
525                 local->ps_sdata = NULL;
526                 return;
527         }
528
529         list_for_each_entry(sdata, &local->interfaces, list) {
530                 if (!netif_running(sdata->dev))
531                         continue;
532                 if (sdata->vif.type != NL80211_IFTYPE_STATION)
533                         continue;
534                 found = sdata;
535                 count++;
536         }
537
538         if (count == 1 && found->u.mgd.powersave) {
539                 s32 beaconint_us;
540
541                 if (latency < 0)
542                         latency = pm_qos_requirement(PM_QOS_NETWORK_LATENCY);
543
544                 beaconint_us = ieee80211_tu_to_usec(
545                                         found->vif.bss_conf.beacon_int);
546
547                 if (beaconint_us > latency)
548                         local->ps_sdata = NULL;
549                 else
550                         local->ps_sdata = found;
551         } else {
552                 local->ps_sdata = NULL;
553         }
554
555         ieee80211_change_ps(local);
556 }
557
558 void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
559 {
560         struct ieee80211_local *local =
561                 container_of(work, struct ieee80211_local,
562                              dynamic_ps_disable_work);
563
564         if (local->hw.conf.flags & IEEE80211_CONF_PS) {
565                 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
566                 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
567         }
568
569         ieee80211_wake_queues_by_reason(&local->hw,
570                                         IEEE80211_QUEUE_STOP_REASON_PS);
571 }
572
573 void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
574 {
575         struct ieee80211_local *local =
576                 container_of(work, struct ieee80211_local,
577                              dynamic_ps_enable_work);
578         struct ieee80211_sub_if_data *sdata = local->ps_sdata;
579
580         /* can only happen when PS was just disabled anyway */
581         if (!sdata)
582                 return;
583
584         if (local->hw.conf.flags & IEEE80211_CONF_PS)
585                 return;
586
587         if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
588                 ieee80211_send_nullfunc(local, sdata, 1);
589
590         local->hw.conf.flags |= IEEE80211_CONF_PS;
591         ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
592 }
593
594 void ieee80211_dynamic_ps_timer(unsigned long data)
595 {
596         struct ieee80211_local *local = (void *) data;
597
598         queue_work(local->hw.workqueue, &local->dynamic_ps_enable_work);
599 }
600
601 /* MLME */
602 static void ieee80211_sta_wmm_params(struct ieee80211_local *local,
603                                      struct ieee80211_if_managed *ifmgd,
604                                      u8 *wmm_param, size_t wmm_param_len)
605 {
606         struct ieee80211_tx_queue_params params;
607         size_t left;
608         int count;
609         u8 *pos;
610
611         if (!(ifmgd->flags & IEEE80211_STA_WMM_ENABLED))
612                 return;
613
614         if (!wmm_param)
615                 return;
616
617         if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
618                 return;
619         count = wmm_param[6] & 0x0f;
620         if (count == ifmgd->wmm_last_param_set)
621                 return;
622         ifmgd->wmm_last_param_set = count;
623
624         pos = wmm_param + 8;
625         left = wmm_param_len - 8;
626
627         memset(&params, 0, sizeof(params));
628
629         local->wmm_acm = 0;
630         for (; left >= 4; left -= 4, pos += 4) {
631                 int aci = (pos[0] >> 5) & 0x03;
632                 int acm = (pos[0] >> 4) & 0x01;
633                 int queue;
634
635                 switch (aci) {
636                 case 1: /* AC_BK */
637                         queue = 3;
638                         if (acm)
639                                 local->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
640                         break;
641                 case 2: /* AC_VI */
642                         queue = 1;
643                         if (acm)
644                                 local->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
645                         break;
646                 case 3: /* AC_VO */
647                         queue = 0;
648                         if (acm)
649                                 local->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
650                         break;
651                 case 0: /* AC_BE */
652                 default:
653                         queue = 2;
654                         if (acm)
655                                 local->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
656                         break;
657                 }
658
659                 params.aifs = pos[0] & 0x0f;
660                 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
661                 params.cw_min = ecw2cw(pos[1] & 0x0f);
662                 params.txop = get_unaligned_le16(pos + 2);
663 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
664                 printk(KERN_DEBUG "%s: WMM queue=%d aci=%d acm=%d aifs=%d "
665                        "cWmin=%d cWmax=%d txop=%d\n",
666                        local->mdev->name, queue, aci, acm, params.aifs, params.cw_min,
667                        params.cw_max, params.txop);
668 #endif
669                 if (local->ops->conf_tx &&
670                     local->ops->conf_tx(local_to_hw(local), queue, &params)) {
671                         printk(KERN_DEBUG "%s: failed to set TX queue "
672                                "parameters for queue %d\n", local->mdev->name, queue);
673                 }
674         }
675 }
676
677 static bool ieee80211_check_tim(struct ieee802_11_elems *elems, u16 aid)
678 {
679         u8 mask;
680         u8 index, indexn1, indexn2;
681         struct ieee80211_tim_ie *tim = (struct ieee80211_tim_ie *) elems->tim;
682
683         if (unlikely(!tim || elems->tim_len < 4))
684                 return false;
685
686         aid &= 0x3fff;
687         index = aid / 8;
688         mask  = 1 << (aid & 7);
689
690         indexn1 = tim->bitmap_ctrl & 0xfe;
691         indexn2 = elems->tim_len + indexn1 - 4;
692
693         if (index < indexn1 || index > indexn2)
694                 return false;
695
696         index -= indexn1;
697
698         return !!(tim->virtual_map[index] & mask);
699 }
700
701 static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
702                                            u16 capab, bool erp_valid, u8 erp)
703 {
704         struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
705 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
706         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
707 #endif
708         u32 changed = 0;
709         bool use_protection;
710         bool use_short_preamble;
711         bool use_short_slot;
712
713         if (erp_valid) {
714                 use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
715                 use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
716         } else {
717                 use_protection = false;
718                 use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
719         }
720
721         use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
722
723         if (use_protection != bss_conf->use_cts_prot) {
724 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
725                 if (net_ratelimit()) {
726                         printk(KERN_DEBUG "%s: CTS protection %s (BSSID=%pM)\n",
727                                sdata->dev->name,
728                                use_protection ? "enabled" : "disabled",
729                                ifmgd->bssid);
730                 }
731 #endif
732                 bss_conf->use_cts_prot = use_protection;
733                 changed |= BSS_CHANGED_ERP_CTS_PROT;
734         }
735
736         if (use_short_preamble != bss_conf->use_short_preamble) {
737 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
738                 if (net_ratelimit()) {
739                         printk(KERN_DEBUG "%s: switched to %s barker preamble"
740                                " (BSSID=%pM)\n",
741                                sdata->dev->name,
742                                use_short_preamble ? "short" : "long",
743                                ifmgd->bssid);
744                 }
745 #endif
746                 bss_conf->use_short_preamble = use_short_preamble;
747                 changed |= BSS_CHANGED_ERP_PREAMBLE;
748         }
749
750         if (use_short_slot != bss_conf->use_short_slot) {
751 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
752                 if (net_ratelimit()) {
753                         printk(KERN_DEBUG "%s: switched to %s slot time"
754                                " (BSSID=%pM)\n",
755                                sdata->dev->name,
756                                use_short_slot ? "short" : "long",
757                                ifmgd->bssid);
758                 }
759 #endif
760                 bss_conf->use_short_slot = use_short_slot;
761                 changed |= BSS_CHANGED_ERP_SLOT;
762         }
763
764         return changed;
765 }
766
767 static void ieee80211_sta_send_apinfo(struct ieee80211_sub_if_data *sdata)
768 {
769         union iwreq_data wrqu;
770
771         memset(&wrqu, 0, sizeof(wrqu));
772         if (sdata->u.mgd.flags & IEEE80211_STA_ASSOCIATED)
773                 memcpy(wrqu.ap_addr.sa_data, sdata->u.mgd.bssid, ETH_ALEN);
774         wrqu.ap_addr.sa_family = ARPHRD_ETHER;
775         wireless_send_event(sdata->dev, SIOCGIWAP, &wrqu, NULL);
776 }
777
778 static void ieee80211_sta_send_associnfo(struct ieee80211_sub_if_data *sdata)
779 {
780         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
781         char *buf;
782         size_t len;
783         int i;
784         union iwreq_data wrqu;
785
786         if (!ifmgd->assocreq_ies && !ifmgd->assocresp_ies)
787                 return;
788
789         buf = kmalloc(50 + 2 * (ifmgd->assocreq_ies_len +
790                                 ifmgd->assocresp_ies_len), GFP_KERNEL);
791         if (!buf)
792                 return;
793
794         len = sprintf(buf, "ASSOCINFO(");
795         if (ifmgd->assocreq_ies) {
796                 len += sprintf(buf + len, "ReqIEs=");
797                 for (i = 0; i < ifmgd->assocreq_ies_len; i++) {
798                         len += sprintf(buf + len, "%02x",
799                                        ifmgd->assocreq_ies[i]);
800                 }
801         }
802         if (ifmgd->assocresp_ies) {
803                 if (ifmgd->assocreq_ies)
804                         len += sprintf(buf + len, " ");
805                 len += sprintf(buf + len, "RespIEs=");
806                 for (i = 0; i < ifmgd->assocresp_ies_len; i++) {
807                         len += sprintf(buf + len, "%02x",
808                                        ifmgd->assocresp_ies[i]);
809                 }
810         }
811         len += sprintf(buf + len, ")");
812
813         if (len > IW_CUSTOM_MAX) {
814                 len = sprintf(buf, "ASSOCRESPIE=");
815                 for (i = 0; i < ifmgd->assocresp_ies_len; i++) {
816                         len += sprintf(buf + len, "%02x",
817                                        ifmgd->assocresp_ies[i]);
818                 }
819         }
820
821         if (len <= IW_CUSTOM_MAX) {
822                 memset(&wrqu, 0, sizeof(wrqu));
823                 wrqu.data.length = len;
824                 wireless_send_event(sdata->dev, IWEVCUSTOM, &wrqu, buf);
825         }
826
827         kfree(buf);
828 }
829
830
831 static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
832                                      u32 bss_info_changed)
833 {
834         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
835         struct ieee80211_local *local = sdata->local;
836         struct ieee80211_conf *conf = &local_to_hw(local)->conf;
837
838         struct ieee80211_bss *bss;
839
840         bss_info_changed |= BSS_CHANGED_ASSOC;
841         ifmgd->flags |= IEEE80211_STA_ASSOCIATED;
842
843         bss = ieee80211_rx_bss_get(local, ifmgd->bssid,
844                                    conf->channel->center_freq,
845                                    ifmgd->ssid, ifmgd->ssid_len);
846         if (bss) {
847                 /* set timing information */
848                 sdata->vif.bss_conf.beacon_int = bss->cbss.beacon_interval;
849                 sdata->vif.bss_conf.timestamp = bss->cbss.tsf;
850                 sdata->vif.bss_conf.dtim_period = bss->dtim_period;
851
852                 bss_info_changed |= ieee80211_handle_bss_capability(sdata,
853                         bss->cbss.capability, bss->has_erp_value, bss->erp_value);
854
855                 cfg80211_hold_bss(&bss->cbss);
856
857                 ieee80211_rx_bss_put(local, bss);
858         }
859
860         ifmgd->flags |= IEEE80211_STA_PREV_BSSID_SET;
861         memcpy(ifmgd->prev_bssid, sdata->u.mgd.bssid, ETH_ALEN);
862         ieee80211_sta_send_associnfo(sdata);
863
864         ifmgd->last_probe = jiffies;
865         ieee80211_led_assoc(local, 1);
866
867         sdata->vif.bss_conf.assoc = 1;
868         /*
869          * For now just always ask the driver to update the basic rateset
870          * when we have associated, we aren't checking whether it actually
871          * changed or not.
872          */
873         bss_info_changed |= BSS_CHANGED_BASIC_RATES;
874         ieee80211_bss_info_change_notify(sdata, bss_info_changed);
875
876         /* will be same as sdata */
877         if (local->ps_sdata)
878                 ieee80211_enable_ps(local, sdata);
879
880         netif_tx_start_all_queues(sdata->dev);
881         netif_carrier_on(sdata->dev);
882
883         ieee80211_sta_send_apinfo(sdata);
884 }
885
886 static void ieee80211_direct_probe(struct ieee80211_sub_if_data *sdata)
887 {
888         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
889         struct ieee80211_local *local = sdata->local;
890
891         ifmgd->direct_probe_tries++;
892         if (ifmgd->direct_probe_tries > IEEE80211_AUTH_MAX_TRIES) {
893                 printk(KERN_DEBUG "%s: direct probe to AP %pM timed out\n",
894                        sdata->dev->name, ifmgd->bssid);
895                 ifmgd->state = IEEE80211_STA_MLME_DISABLED;
896                 ieee80211_sta_send_apinfo(sdata);
897
898                 /*
899                  * Most likely AP is not in the range so remove the
900                  * bss information associated to the AP
901                  */
902                 ieee80211_rx_bss_remove(sdata, ifmgd->bssid,
903                                 sdata->local->hw.conf.channel->center_freq,
904                                 ifmgd->ssid, ifmgd->ssid_len);
905
906                 /*
907                  * We might have a pending scan which had no chance to run yet
908                  * due to state == IEEE80211_STA_MLME_DIRECT_PROBE.
909                  * Hence, queue the STAs work again
910                  */
911                 queue_work(local->hw.workqueue, &ifmgd->work);
912                 return;
913         }
914
915         printk(KERN_DEBUG "%s: direct probe to AP %pM try %d\n",
916                         sdata->dev->name, ifmgd->bssid,
917                         ifmgd->direct_probe_tries);
918
919         ifmgd->state = IEEE80211_STA_MLME_DIRECT_PROBE;
920
921         set_bit(IEEE80211_STA_REQ_DIRECT_PROBE, &ifmgd->request);
922
923         /* Direct probe is sent to broadcast address as some APs
924          * will not answer to direct packet in unassociated state.
925          */
926         ieee80211_send_probe_req(sdata, NULL,
927                                  ifmgd->ssid, ifmgd->ssid_len, NULL, 0);
928
929         mod_timer(&ifmgd->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
930 }
931
932
933 static void ieee80211_authenticate(struct ieee80211_sub_if_data *sdata)
934 {
935         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
936         struct ieee80211_local *local = sdata->local;
937         u8 *ies;
938         size_t ies_len;
939
940         ifmgd->auth_tries++;
941         if (ifmgd->auth_tries > IEEE80211_AUTH_MAX_TRIES) {
942                 printk(KERN_DEBUG "%s: authentication with AP %pM"
943                        " timed out\n",
944                        sdata->dev->name, ifmgd->bssid);
945                 ifmgd->state = IEEE80211_STA_MLME_DISABLED;
946                 ieee80211_sta_send_apinfo(sdata);
947                 ieee80211_rx_bss_remove(sdata, ifmgd->bssid,
948                                 sdata->local->hw.conf.channel->center_freq,
949                                 ifmgd->ssid, ifmgd->ssid_len);
950
951                 /*
952                  * We might have a pending scan which had no chance to run yet
953                  * due to state == IEEE80211_STA_MLME_AUTHENTICATE.
954                  * Hence, queue the STAs work again
955                  */
956                 queue_work(local->hw.workqueue, &ifmgd->work);
957                 return;
958         }
959
960         ifmgd->state = IEEE80211_STA_MLME_AUTHENTICATE;
961         printk(KERN_DEBUG "%s: authenticate with AP %pM\n",
962                sdata->dev->name, ifmgd->bssid);
963
964         if (ifmgd->flags & IEEE80211_STA_EXT_SME) {
965                 ies = ifmgd->sme_auth_ie;
966                 ies_len = ifmgd->sme_auth_ie_len;
967         } else {
968                 ies = NULL;
969                 ies_len = 0;
970         }
971         ieee80211_send_auth(sdata, 1, ifmgd->auth_alg, ies, ies_len,
972                             ifmgd->bssid, 0);
973         ifmgd->auth_transaction = 2;
974
975         mod_timer(&ifmgd->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
976 }
977
978 /*
979  * The disassoc 'reason' argument can be either our own reason
980  * if self disconnected or a reason code from the AP.
981  */
982 static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
983                                    bool deauth, bool self_disconnected,
984                                    u16 reason)
985 {
986         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
987         struct ieee80211_local *local = sdata->local;
988         struct ieee80211_conf *conf = &local_to_hw(local)->conf;
989         struct ieee80211_bss *bss;
990         struct sta_info *sta;
991         u32 changed = 0, config_changed = 0;
992
993         rcu_read_lock();
994
995         sta = sta_info_get(local, ifmgd->bssid);
996         if (!sta) {
997                 rcu_read_unlock();
998                 return;
999         }
1000
1001         if (deauth) {
1002                 ifmgd->direct_probe_tries = 0;
1003                 ifmgd->auth_tries = 0;
1004         }
1005         ifmgd->assoc_scan_tries = 0;
1006         ifmgd->assoc_tries = 0;
1007
1008         netif_tx_stop_all_queues(sdata->dev);
1009         netif_carrier_off(sdata->dev);
1010
1011         ieee80211_sta_tear_down_BA_sessions(sta);
1012
1013         bss = ieee80211_rx_bss_get(local, ifmgd->bssid,
1014                                    conf->channel->center_freq,
1015                                    ifmgd->ssid, ifmgd->ssid_len);
1016
1017         if (bss) {
1018                 cfg80211_unhold_bss(&bss->cbss);
1019                 ieee80211_rx_bss_put(local, bss);
1020         }
1021
1022         if (self_disconnected) {
1023                 if (deauth)
1024                         ieee80211_send_deauth_disassoc(sdata,
1025                                 IEEE80211_STYPE_DEAUTH, reason);
1026                 else
1027                         ieee80211_send_deauth_disassoc(sdata,
1028                                 IEEE80211_STYPE_DISASSOC, reason);
1029         }
1030
1031         ifmgd->flags &= ~IEEE80211_STA_ASSOCIATED;
1032         changed |= ieee80211_reset_erp_info(sdata);
1033
1034         ieee80211_led_assoc(local, 0);
1035         changed |= BSS_CHANGED_ASSOC;
1036         sdata->vif.bss_conf.assoc = false;
1037
1038         ieee80211_sta_send_apinfo(sdata);
1039
1040         if (self_disconnected || reason == WLAN_REASON_DISASSOC_STA_HAS_LEFT) {
1041                 ifmgd->state = IEEE80211_STA_MLME_DISABLED;
1042                 ieee80211_rx_bss_remove(sdata, ifmgd->bssid,
1043                                 sdata->local->hw.conf.channel->center_freq,
1044                                 ifmgd->ssid, ifmgd->ssid_len);
1045         }
1046
1047         rcu_read_unlock();
1048
1049         /* channel(_type) changes are handled by ieee80211_hw_config */
1050         local->oper_channel_type = NL80211_CHAN_NO_HT;
1051
1052         local->power_constr_level = 0;
1053
1054         del_timer_sync(&local->dynamic_ps_timer);
1055         cancel_work_sync(&local->dynamic_ps_enable_work);
1056
1057         if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1058                 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1059                 config_changed |= IEEE80211_CONF_CHANGE_PS;
1060         }
1061
1062         ieee80211_hw_config(local, config_changed);
1063         ieee80211_bss_info_change_notify(sdata, changed);
1064
1065         rcu_read_lock();
1066
1067         sta = sta_info_get(local, ifmgd->bssid);
1068         if (!sta) {
1069                 rcu_read_unlock();
1070                 return;
1071         }
1072
1073         sta_info_unlink(&sta);
1074
1075         rcu_read_unlock();
1076
1077         sta_info_destroy(sta);
1078 }
1079
1080 static int ieee80211_sta_wep_configured(struct ieee80211_sub_if_data *sdata)
1081 {
1082         if (!sdata || !sdata->default_key ||
1083             sdata->default_key->conf.alg != ALG_WEP)
1084                 return 0;
1085         return 1;
1086 }
1087
1088 static int ieee80211_privacy_mismatch(struct ieee80211_sub_if_data *sdata)
1089 {
1090         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1091         struct ieee80211_local *local = sdata->local;
1092         struct ieee80211_bss *bss;
1093         int bss_privacy;
1094         int wep_privacy;
1095         int privacy_invoked;
1096
1097         if (!ifmgd || (ifmgd->flags & IEEE80211_STA_EXT_SME))
1098                 return 0;
1099
1100         bss = ieee80211_rx_bss_get(local, ifmgd->bssid,
1101                                    local->hw.conf.channel->center_freq,
1102                                    ifmgd->ssid, ifmgd->ssid_len);
1103         if (!bss)
1104                 return 0;
1105
1106         bss_privacy = !!(bss->cbss.capability & WLAN_CAPABILITY_PRIVACY);
1107         wep_privacy = !!ieee80211_sta_wep_configured(sdata);
1108         privacy_invoked = !!(ifmgd->flags & IEEE80211_STA_PRIVACY_INVOKED);
1109
1110         ieee80211_rx_bss_put(local, bss);
1111
1112         if ((bss_privacy == wep_privacy) || (bss_privacy == privacy_invoked))
1113                 return 0;
1114
1115         return 1;
1116 }
1117
1118 static void ieee80211_associate(struct ieee80211_sub_if_data *sdata)
1119 {
1120         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1121         struct ieee80211_local *local = sdata->local;
1122
1123         ifmgd->assoc_tries++;
1124         if (ifmgd->assoc_tries > IEEE80211_ASSOC_MAX_TRIES) {
1125                 printk(KERN_DEBUG "%s: association with AP %pM"
1126                        " timed out\n",
1127                        sdata->dev->name, ifmgd->bssid);
1128                 ifmgd->state = IEEE80211_STA_MLME_DISABLED;
1129                 ieee80211_sta_send_apinfo(sdata);
1130                 ieee80211_rx_bss_remove(sdata, ifmgd->bssid,
1131                                 sdata->local->hw.conf.channel->center_freq,
1132                                 ifmgd->ssid, ifmgd->ssid_len);
1133                 /*
1134                  * We might have a pending scan which had no chance to run yet
1135                  * due to state == IEEE80211_STA_MLME_ASSOCIATE.
1136                  * Hence, queue the STAs work again
1137                  */
1138                 queue_work(local->hw.workqueue, &ifmgd->work);
1139                 return;
1140         }
1141
1142         ifmgd->state = IEEE80211_STA_MLME_ASSOCIATE;
1143         printk(KERN_DEBUG "%s: associate with AP %pM\n",
1144                sdata->dev->name, ifmgd->bssid);
1145         if (ieee80211_privacy_mismatch(sdata)) {
1146                 printk(KERN_DEBUG "%s: mismatch in privacy configuration and "
1147                        "mixed-cell disabled - abort association\n", sdata->dev->name);
1148                 ifmgd->state = IEEE80211_STA_MLME_DISABLED;
1149                 return;
1150         }
1151
1152         ieee80211_send_assoc(sdata);
1153
1154         mod_timer(&ifmgd->timer, jiffies + IEEE80211_ASSOC_TIMEOUT);
1155 }
1156
1157 void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
1158                              struct ieee80211_hdr *hdr)
1159 {
1160         /*
1161          * We can postpone the mgd.timer whenever receiving unicast frames
1162          * from AP because we know that the connection is working both ways
1163          * at that time. But multicast frames (and hence also beacons) must
1164          * be ignored here, because we need to trigger the timer during
1165          * data idle periods for sending the periodical probe request to
1166          * the AP.
1167          */
1168         if (!is_multicast_ether_addr(hdr->addr1))
1169                 mod_timer(&sdata->u.mgd.timer,
1170                           jiffies + IEEE80211_MONITORING_INTERVAL);
1171 }
1172
1173 void ieee80211_beacon_loss_work(struct work_struct *work)
1174 {
1175         struct ieee80211_sub_if_data *sdata =
1176                 container_of(work, struct ieee80211_sub_if_data,
1177                              u.mgd.beacon_loss_work);
1178         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1179
1180 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1181         if (net_ratelimit()) {
1182                 printk(KERN_DEBUG "%s: driver reports beacon loss from AP %pM "
1183                        "- sending probe request\n", sdata->dev->name,
1184                        sdata->u.mgd.bssid);
1185         }
1186 #endif
1187
1188         ifmgd->flags |= IEEE80211_STA_PROBEREQ_POLL;
1189         ieee80211_send_probe_req(sdata, ifmgd->bssid, ifmgd->ssid,
1190                                  ifmgd->ssid_len, NULL, 0);
1191
1192         mod_timer(&ifmgd->timer, jiffies + IEEE80211_MONITORING_INTERVAL);
1193 }
1194
1195 void ieee80211_beacon_loss(struct ieee80211_vif *vif)
1196 {
1197         struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1198
1199         queue_work(sdata->local->hw.workqueue,
1200                    &sdata->u.mgd.beacon_loss_work);
1201 }
1202 EXPORT_SYMBOL(ieee80211_beacon_loss);
1203
1204 static void ieee80211_associated(struct ieee80211_sub_if_data *sdata)
1205 {
1206         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1207         struct ieee80211_local *local = sdata->local;
1208         struct sta_info *sta;
1209         bool disassoc = false;
1210
1211         /* TODO: start monitoring current AP signal quality and number of
1212          * missed beacons. Scan other channels every now and then and search
1213          * for better APs. */
1214         /* TODO: remove expired BSSes */
1215
1216         ifmgd->state = IEEE80211_STA_MLME_ASSOCIATED;
1217
1218         rcu_read_lock();
1219
1220         sta = sta_info_get(local, ifmgd->bssid);
1221         if (!sta) {
1222                 printk(KERN_DEBUG "%s: No STA entry for own AP %pM\n",
1223                        sdata->dev->name, ifmgd->bssid);
1224                 disassoc = true;
1225                 goto unlock;
1226         }
1227
1228         if ((ifmgd->flags & IEEE80211_STA_PROBEREQ_POLL) &&
1229             time_after(jiffies, sta->last_rx + IEEE80211_MONITORING_INTERVAL)) {
1230                 printk(KERN_DEBUG "%s: no probe response from AP %pM "
1231                        "- disassociating\n",
1232                        sdata->dev->name, ifmgd->bssid);
1233                 disassoc = true;
1234                 ifmgd->flags &= ~IEEE80211_STA_PROBEREQ_POLL;
1235                 goto unlock;
1236         }
1237
1238         /*
1239          * Beacon filtering is only enabled with power save and then the
1240          * stack should not check for beacon loss.
1241          */
1242         if (!((local->hw.flags & IEEE80211_HW_BEACON_FILTER) &&
1243               (local->hw.conf.flags & IEEE80211_CONF_PS)) &&
1244             time_after(jiffies,
1245                        ifmgd->last_beacon + IEEE80211_MONITORING_INTERVAL)) {
1246 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1247                 if (net_ratelimit()) {
1248                         printk(KERN_DEBUG "%s: beacon loss from AP %pM "
1249                                "- sending probe request\n",
1250                                sdata->dev->name, ifmgd->bssid);
1251                 }
1252 #endif
1253                 ifmgd->flags |= IEEE80211_STA_PROBEREQ_POLL;
1254                 ieee80211_send_probe_req(sdata, ifmgd->bssid, ifmgd->ssid,
1255                                          ifmgd->ssid_len, NULL, 0);
1256                 goto unlock;
1257
1258         }
1259
1260         if (time_after(jiffies, sta->last_rx + IEEE80211_PROBE_IDLE_TIME)) {
1261                 ifmgd->flags |= IEEE80211_STA_PROBEREQ_POLL;
1262                 ieee80211_send_probe_req(sdata, ifmgd->bssid, ifmgd->ssid,
1263                                          ifmgd->ssid_len, NULL, 0);
1264         }
1265
1266  unlock:
1267         rcu_read_unlock();
1268
1269         if (disassoc)
1270                 ieee80211_set_disassoc(sdata, true, true,
1271                                         WLAN_REASON_PREV_AUTH_NOT_VALID);
1272         else
1273                 mod_timer(&ifmgd->timer, jiffies +
1274                                       IEEE80211_MONITORING_INTERVAL);
1275 }
1276
1277
1278 static void ieee80211_auth_completed(struct ieee80211_sub_if_data *sdata)
1279 {
1280         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1281
1282         printk(KERN_DEBUG "%s: authenticated\n", sdata->dev->name);
1283         ifmgd->flags |= IEEE80211_STA_AUTHENTICATED;
1284         if (ifmgd->flags & IEEE80211_STA_EXT_SME) {
1285                 /* Wait for SME to request association */
1286                 ifmgd->state = IEEE80211_STA_MLME_DISABLED;
1287         } else
1288                 ieee80211_associate(sdata);
1289 }
1290
1291
1292 static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
1293                                      struct ieee80211_mgmt *mgmt,
1294                                      size_t len)
1295 {
1296         u8 *pos;
1297         struct ieee802_11_elems elems;
1298
1299         pos = mgmt->u.auth.variable;
1300         ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
1301         if (!elems.challenge)
1302                 return;
1303         ieee80211_send_auth(sdata, 3, sdata->u.mgd.auth_alg,
1304                             elems.challenge - 2, elems.challenge_len + 2,
1305                             sdata->u.mgd.bssid, 1);
1306         sdata->u.mgd.auth_transaction = 4;
1307 }
1308
1309 static void ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
1310                                    struct ieee80211_mgmt *mgmt,
1311                                    size_t len)
1312 {
1313         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1314         u16 auth_alg, auth_transaction, status_code;
1315
1316         if (ifmgd->state != IEEE80211_STA_MLME_AUTHENTICATE)
1317                 return;
1318
1319         if (len < 24 + 6)
1320                 return;
1321
1322         if (memcmp(ifmgd->bssid, mgmt->sa, ETH_ALEN) != 0)
1323                 return;
1324
1325         if (memcmp(ifmgd->bssid, mgmt->bssid, ETH_ALEN) != 0)
1326                 return;
1327
1328         auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
1329         auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
1330         status_code = le16_to_cpu(mgmt->u.auth.status_code);
1331
1332         if (auth_alg != ifmgd->auth_alg ||
1333             auth_transaction != ifmgd->auth_transaction)
1334                 return;
1335
1336         if (status_code != WLAN_STATUS_SUCCESS) {
1337                 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG) {
1338                         u8 algs[3];
1339                         const int num_algs = ARRAY_SIZE(algs);
1340                         int i, pos;
1341                         algs[0] = algs[1] = algs[2] = 0xff;
1342                         if (ifmgd->auth_algs & IEEE80211_AUTH_ALG_OPEN)
1343                                 algs[0] = WLAN_AUTH_OPEN;
1344                         if (ifmgd->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
1345                                 algs[1] = WLAN_AUTH_SHARED_KEY;
1346                         if (ifmgd->auth_algs & IEEE80211_AUTH_ALG_LEAP)
1347                                 algs[2] = WLAN_AUTH_LEAP;
1348                         if (ifmgd->auth_alg == WLAN_AUTH_OPEN)
1349                                 pos = 0;
1350                         else if (ifmgd->auth_alg == WLAN_AUTH_SHARED_KEY)
1351                                 pos = 1;
1352                         else
1353                                 pos = 2;
1354                         for (i = 0; i < num_algs; i++) {
1355                                 pos++;
1356                                 if (pos >= num_algs)
1357                                         pos = 0;
1358                                 if (algs[pos] == ifmgd->auth_alg ||
1359                                     algs[pos] == 0xff)
1360                                         continue;
1361                                 if (algs[pos] == WLAN_AUTH_SHARED_KEY &&
1362                                     !ieee80211_sta_wep_configured(sdata))
1363                                         continue;
1364                                 ifmgd->auth_alg = algs[pos];
1365                                 break;
1366                         }
1367                 }
1368                 return;
1369         }
1370
1371         switch (ifmgd->auth_alg) {
1372         case WLAN_AUTH_OPEN:
1373         case WLAN_AUTH_LEAP:
1374         case WLAN_AUTH_FT:
1375                 ieee80211_auth_completed(sdata);
1376                 cfg80211_send_rx_auth(sdata->dev, (u8 *) mgmt, len);
1377                 break;
1378         case WLAN_AUTH_SHARED_KEY:
1379                 if (ifmgd->auth_transaction == 4) {
1380                         ieee80211_auth_completed(sdata);
1381                         cfg80211_send_rx_auth(sdata->dev, (u8 *) mgmt, len);
1382                 } else
1383                         ieee80211_auth_challenge(sdata, mgmt, len);
1384                 break;
1385         }
1386 }
1387
1388
1389 static void ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
1390                                      struct ieee80211_mgmt *mgmt,
1391                                      size_t len)
1392 {
1393         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1394         u16 reason_code;
1395
1396         if (len < 24 + 2)
1397                 return;
1398
1399         if (memcmp(ifmgd->bssid, mgmt->sa, ETH_ALEN))
1400                 return;
1401
1402         reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
1403
1404         if (ifmgd->flags & IEEE80211_STA_AUTHENTICATED)
1405                 printk(KERN_DEBUG "%s: deauthenticated (Reason: %u)\n",
1406                                 sdata->dev->name, reason_code);
1407
1408         if (!(ifmgd->flags & IEEE80211_STA_EXT_SME) &&
1409             (ifmgd->state == IEEE80211_STA_MLME_AUTHENTICATE ||
1410              ifmgd->state == IEEE80211_STA_MLME_ASSOCIATE ||
1411              ifmgd->state == IEEE80211_STA_MLME_ASSOCIATED)) {
1412                 ifmgd->state = IEEE80211_STA_MLME_DIRECT_PROBE;
1413                 mod_timer(&ifmgd->timer, jiffies +
1414                                       IEEE80211_RETRY_AUTH_INTERVAL);
1415         }
1416
1417         ieee80211_set_disassoc(sdata, true, false, 0);
1418         ifmgd->flags &= ~IEEE80211_STA_AUTHENTICATED;
1419         cfg80211_send_deauth(sdata->dev, (u8 *) mgmt, len);
1420 }
1421
1422
1423 static void ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
1424                                        struct ieee80211_mgmt *mgmt,
1425                                        size_t len)
1426 {
1427         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1428         u16 reason_code;
1429
1430         if (len < 24 + 2)
1431                 return;
1432
1433         if (memcmp(ifmgd->bssid, mgmt->sa, ETH_ALEN))
1434                 return;
1435
1436         reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
1437
1438         if (ifmgd->flags & IEEE80211_STA_ASSOCIATED)
1439                 printk(KERN_DEBUG "%s: disassociated (Reason: %u)\n",
1440                                 sdata->dev->name, reason_code);
1441
1442         if (!(ifmgd->flags & IEEE80211_STA_EXT_SME) &&
1443             ifmgd->state == IEEE80211_STA_MLME_ASSOCIATED) {
1444                 ifmgd->state = IEEE80211_STA_MLME_ASSOCIATE;
1445                 mod_timer(&ifmgd->timer, jiffies +
1446                                       IEEE80211_RETRY_AUTH_INTERVAL);
1447         }
1448
1449         ieee80211_set_disassoc(sdata, false, false, reason_code);
1450         cfg80211_send_disassoc(sdata->dev, (u8 *) mgmt, len);
1451 }
1452
1453
1454 static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
1455                                          struct ieee80211_mgmt *mgmt,
1456                                          size_t len,
1457                                          int reassoc)
1458 {
1459         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1460         struct ieee80211_local *local = sdata->local;
1461         struct ieee80211_supported_band *sband;
1462         struct sta_info *sta;
1463         u32 rates, basic_rates;
1464         u16 capab_info, status_code, aid;
1465         struct ieee802_11_elems elems;
1466         struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
1467         u8 *pos;
1468         u32 changed = 0;
1469         int i, j;
1470         bool have_higher_than_11mbit = false, newsta = false;
1471         u16 ap_ht_cap_flags;
1472
1473         /* AssocResp and ReassocResp have identical structure, so process both
1474          * of them in this function. */
1475
1476         if (ifmgd->state != IEEE80211_STA_MLME_ASSOCIATE)
1477                 return;
1478
1479         if (len < 24 + 6)
1480                 return;
1481
1482         if (memcmp(ifmgd->bssid, mgmt->sa, ETH_ALEN) != 0)
1483                 return;
1484
1485         capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
1486         status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
1487         aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
1488
1489         printk(KERN_DEBUG "%s: RX %sssocResp from %pM (capab=0x%x "
1490                "status=%d aid=%d)\n",
1491                sdata->dev->name, reassoc ? "Rea" : "A", mgmt->sa,
1492                capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
1493
1494         pos = mgmt->u.assoc_resp.variable;
1495         ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
1496
1497         if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
1498             elems.timeout_int && elems.timeout_int_len == 5 &&
1499             elems.timeout_int[0] == WLAN_TIMEOUT_ASSOC_COMEBACK) {
1500                 u32 tu, ms;
1501                 tu = get_unaligned_le32(elems.timeout_int + 1);
1502                 ms = tu * 1024 / 1000;
1503                 printk(KERN_DEBUG "%s: AP rejected association temporarily; "
1504                        "comeback duration %u TU (%u ms)\n",
1505                        sdata->dev->name, tu, ms);
1506                 if (ms > IEEE80211_ASSOC_TIMEOUT)
1507                         mod_timer(&ifmgd->timer,
1508                                   jiffies + msecs_to_jiffies(ms));
1509                 return;
1510         }
1511
1512         if (status_code != WLAN_STATUS_SUCCESS) {
1513                 printk(KERN_DEBUG "%s: AP denied association (code=%d)\n",
1514                        sdata->dev->name, status_code);
1515                 /* if this was a reassociation, ensure we try a "full"
1516                  * association next time. This works around some broken APs
1517                  * which do not correctly reject reassociation requests. */
1518                 ifmgd->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
1519                 cfg80211_send_rx_assoc(sdata->dev, (u8 *) mgmt, len);
1520                 if (ifmgd->flags & IEEE80211_STA_EXT_SME) {
1521                         /* Wait for SME to decide what to do next */
1522                         ifmgd->state = IEEE80211_STA_MLME_DISABLED;
1523                 }
1524                 return;
1525         }
1526
1527         if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
1528                 printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
1529                        "set\n", sdata->dev->name, aid);
1530         aid &= ~(BIT(15) | BIT(14));
1531
1532         if (!elems.supp_rates) {
1533                 printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
1534                        sdata->dev->name);
1535                 return;
1536         }
1537
1538         printk(KERN_DEBUG "%s: associated\n", sdata->dev->name);
1539         ifmgd->aid = aid;
1540         ifmgd->ap_capab = capab_info;
1541
1542         kfree(ifmgd->assocresp_ies);
1543         ifmgd->assocresp_ies_len = len - (pos - (u8 *) mgmt);
1544         ifmgd->assocresp_ies = kmalloc(ifmgd->assocresp_ies_len, GFP_KERNEL);
1545         if (ifmgd->assocresp_ies)
1546                 memcpy(ifmgd->assocresp_ies, pos, ifmgd->assocresp_ies_len);
1547
1548         rcu_read_lock();
1549
1550         /* Add STA entry for the AP */
1551         sta = sta_info_get(local, ifmgd->bssid);
1552         if (!sta) {
1553                 newsta = true;
1554
1555                 sta = sta_info_alloc(sdata, ifmgd->bssid, GFP_ATOMIC);
1556                 if (!sta) {
1557                         printk(KERN_DEBUG "%s: failed to alloc STA entry for"
1558                                " the AP\n", sdata->dev->name);
1559                         rcu_read_unlock();
1560                         return;
1561                 }
1562
1563                 /* update new sta with its last rx activity */
1564                 sta->last_rx = jiffies;
1565         }
1566
1567         /*
1568          * FIXME: Do we really need to update the sta_info's information here?
1569          *        We already know about the AP (we found it in our list) so it
1570          *        should already be filled with the right info, no?
1571          *        As is stands, all this is racy because typically we assume
1572          *        the information that is filled in here (except flags) doesn't
1573          *        change while a STA structure is alive. As such, it should move
1574          *        to between the sta_info_alloc() and sta_info_insert() above.
1575          */
1576
1577         set_sta_flags(sta, WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_AP |
1578                            WLAN_STA_AUTHORIZED);
1579
1580         rates = 0;
1581         basic_rates = 0;
1582         sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1583
1584         for (i = 0; i < elems.supp_rates_len; i++) {
1585                 int rate = (elems.supp_rates[i] & 0x7f) * 5;
1586                 bool is_basic = !!(elems.supp_rates[i] & 0x80);
1587
1588                 if (rate > 110)
1589                         have_higher_than_11mbit = true;
1590
1591                 for (j = 0; j < sband->n_bitrates; j++) {
1592                         if (sband->bitrates[j].bitrate == rate) {
1593                                 rates |= BIT(j);
1594                                 if (is_basic)
1595                                         basic_rates |= BIT(j);
1596                                 break;
1597                         }
1598                 }
1599         }
1600
1601         for (i = 0; i < elems.ext_supp_rates_len; i++) {
1602                 int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
1603                 bool is_basic = !!(elems.ext_supp_rates[i] & 0x80);
1604
1605                 if (rate > 110)
1606                         have_higher_than_11mbit = true;
1607
1608                 for (j = 0; j < sband->n_bitrates; j++) {
1609                         if (sband->bitrates[j].bitrate == rate) {
1610                                 rates |= BIT(j);
1611                                 if (is_basic)
1612                                         basic_rates |= BIT(j);
1613                                 break;
1614                         }
1615                 }
1616         }
1617
1618         sta->sta.supp_rates[local->hw.conf.channel->band] = rates;
1619         sdata->vif.bss_conf.basic_rates = basic_rates;
1620
1621         /* cf. IEEE 802.11 9.2.12 */
1622         if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
1623             have_higher_than_11mbit)
1624                 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
1625         else
1626                 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
1627
1628         /* If TKIP/WEP is used, no need to parse AP's HT capabilities */
1629         if (elems.ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_TKIP_WEP_USED))
1630                 ieee80211_ht_cap_ie_to_sta_ht_cap(sband,
1631                                 elems.ht_cap_elem, &sta->sta.ht_cap);
1632
1633         ap_ht_cap_flags = sta->sta.ht_cap.cap;
1634
1635         rate_control_rate_init(sta);
1636
1637         if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED)
1638                 set_sta_flags(sta, WLAN_STA_MFP);
1639
1640         if (elems.wmm_param)
1641                 set_sta_flags(sta, WLAN_STA_WME);
1642
1643         if (newsta) {
1644                 int err = sta_info_insert(sta);
1645                 if (err) {
1646                         printk(KERN_DEBUG "%s: failed to insert STA entry for"
1647                                " the AP (error %d)\n", sdata->dev->name, err);
1648                         rcu_read_unlock();
1649                         return;
1650                 }
1651         }
1652
1653         rcu_read_unlock();
1654
1655         if (elems.wmm_param)
1656                 ieee80211_sta_wmm_params(local, ifmgd, elems.wmm_param,
1657                                          elems.wmm_param_len);
1658
1659         if (elems.ht_info_elem && elems.wmm_param &&
1660             (ifmgd->flags & IEEE80211_STA_WMM_ENABLED) &&
1661             !(ifmgd->flags & IEEE80211_STA_TKIP_WEP_USED))
1662                 changed |= ieee80211_enable_ht(sdata, elems.ht_info_elem,
1663                                                ap_ht_cap_flags);
1664
1665         /* set AID and assoc capability,
1666          * ieee80211_set_associated() will tell the driver */
1667         bss_conf->aid = aid;
1668         bss_conf->assoc_capability = capab_info;
1669         ieee80211_set_associated(sdata, changed);
1670
1671         /*
1672          * initialise the time of last beacon to be the association time,
1673          * otherwise beacon loss check will trigger immediately
1674          */
1675         ifmgd->last_beacon = jiffies;
1676
1677         ieee80211_associated(sdata);
1678         cfg80211_send_rx_assoc(sdata->dev, (u8 *) mgmt, len);
1679 }
1680
1681
1682 static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
1683                                   struct ieee80211_mgmt *mgmt,
1684                                   size_t len,
1685                                   struct ieee80211_rx_status *rx_status,
1686                                   struct ieee802_11_elems *elems,
1687                                   bool beacon)
1688 {
1689         struct ieee80211_local *local = sdata->local;
1690         int freq;
1691         struct ieee80211_bss *bss;
1692         struct ieee80211_channel *channel;
1693
1694         if (elems->ds_params && elems->ds_params_len == 1)
1695                 freq = ieee80211_channel_to_frequency(elems->ds_params[0]);
1696         else
1697                 freq = rx_status->freq;
1698
1699         channel = ieee80211_get_channel(local->hw.wiphy, freq);
1700
1701         if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
1702                 return;
1703
1704         bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
1705                                         channel, beacon);
1706         if (!bss)
1707                 return;
1708
1709         if (elems->ch_switch_elem && (elems->ch_switch_elem_len == 3) &&
1710             (memcmp(mgmt->bssid, sdata->u.mgd.bssid, ETH_ALEN) == 0)) {
1711                 struct ieee80211_channel_sw_ie *sw_elem =
1712                         (struct ieee80211_channel_sw_ie *)elems->ch_switch_elem;
1713                 ieee80211_process_chanswitch(sdata, sw_elem, bss);
1714         }
1715
1716         ieee80211_rx_bss_put(local, bss);
1717 }
1718
1719
1720 static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
1721                                          struct ieee80211_mgmt *mgmt,
1722                                          size_t len,
1723                                          struct ieee80211_rx_status *rx_status)
1724 {
1725         struct ieee80211_if_managed *ifmgd;
1726         size_t baselen;
1727         struct ieee802_11_elems elems;
1728
1729         ifmgd = &sdata->u.mgd;
1730
1731         if (memcmp(mgmt->da, sdata->dev->dev_addr, ETH_ALEN))
1732                 return; /* ignore ProbeResp to foreign address */
1733
1734         baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
1735         if (baselen > len)
1736                 return;
1737
1738         ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
1739                                 &elems);
1740
1741         ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, false);
1742
1743         /* direct probe may be part of the association flow */
1744         if (test_and_clear_bit(IEEE80211_STA_REQ_DIRECT_PROBE,
1745                                &ifmgd->request)) {
1746                 printk(KERN_DEBUG "%s direct probe responded\n",
1747                        sdata->dev->name);
1748                 ieee80211_authenticate(sdata);
1749         }
1750
1751         if (ifmgd->flags & IEEE80211_STA_PROBEREQ_POLL)
1752                 ifmgd->flags &= ~IEEE80211_STA_PROBEREQ_POLL;
1753 }
1754
1755 static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
1756                                      struct ieee80211_mgmt *mgmt,
1757                                      size_t len,
1758                                      struct ieee80211_rx_status *rx_status)
1759 {
1760         struct ieee80211_if_managed *ifmgd;
1761         size_t baselen;
1762         struct ieee802_11_elems elems;
1763         struct ieee80211_local *local = sdata->local;
1764         u32 changed = 0;
1765         bool erp_valid, directed_tim;
1766         u8 erp_value = 0;
1767
1768         /* Process beacon from the current BSS */
1769         baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
1770         if (baselen > len)
1771                 return;
1772
1773         ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
1774
1775         ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, true);
1776
1777         if (sdata->vif.type != NL80211_IFTYPE_STATION)
1778                 return;
1779
1780         ifmgd = &sdata->u.mgd;
1781
1782         if (!(ifmgd->flags & IEEE80211_STA_ASSOCIATED) ||
1783             memcmp(ifmgd->bssid, mgmt->bssid, ETH_ALEN) != 0)
1784                 return;
1785
1786         if (rx_status->freq != local->hw.conf.channel->center_freq)
1787                 return;
1788
1789         ieee80211_sta_wmm_params(local, ifmgd, elems.wmm_param,
1790                                  elems.wmm_param_len);
1791
1792         if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) {
1793                 directed_tim = ieee80211_check_tim(&elems, ifmgd->aid);
1794
1795                 if (directed_tim) {
1796                         if (local->hw.conf.dynamic_ps_timeout > 0) {
1797                                 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1798                                 ieee80211_hw_config(local,
1799                                                     IEEE80211_CONF_CHANGE_PS);
1800                                 ieee80211_send_nullfunc(local, sdata, 0);
1801                         } else {
1802                                 local->pspolling = true;
1803
1804                                 /*
1805                                  * Here is assumed that the driver will be
1806                                  * able to send ps-poll frame and receive a
1807                                  * response even though power save mode is
1808                                  * enabled, but some drivers might require
1809                                  * to disable power save here. This needs
1810                                  * to be investigated.
1811                                  */
1812                                 ieee80211_send_pspoll(local, sdata);
1813                         }
1814                 }
1815         }
1816
1817         if (elems.erp_info && elems.erp_info_len >= 1) {
1818                 erp_valid = true;
1819                 erp_value = elems.erp_info[0];
1820         } else {
1821                 erp_valid = false;
1822         }
1823         changed |= ieee80211_handle_bss_capability(sdata,
1824                         le16_to_cpu(mgmt->u.beacon.capab_info),
1825                         erp_valid, erp_value);
1826
1827
1828         if (elems.ht_cap_elem && elems.ht_info_elem && elems.wmm_param &&
1829             !(ifmgd->flags & IEEE80211_STA_TKIP_WEP_USED)) {
1830                 struct sta_info *sta;
1831                 struct ieee80211_supported_band *sband;
1832                 u16 ap_ht_cap_flags;
1833
1834                 rcu_read_lock();
1835
1836                 sta = sta_info_get(local, ifmgd->bssid);
1837                 if (!sta) {
1838                         rcu_read_unlock();
1839                         return;
1840                 }
1841
1842                 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1843
1844                 ieee80211_ht_cap_ie_to_sta_ht_cap(sband,
1845                                 elems.ht_cap_elem, &sta->sta.ht_cap);
1846
1847                 ap_ht_cap_flags = sta->sta.ht_cap.cap;
1848
1849                 rcu_read_unlock();
1850
1851                 changed |= ieee80211_enable_ht(sdata, elems.ht_info_elem,
1852                                                ap_ht_cap_flags);
1853         }
1854
1855         if (elems.country_elem) {
1856                 /* Note we are only reviewing this on beacons
1857                  * for the BSSID we are associated to */
1858                 regulatory_hint_11d(local->hw.wiphy,
1859                         elems.country_elem, elems.country_elem_len);
1860
1861                 /* TODO: IBSS also needs this */
1862                 if (elems.pwr_constr_elem)
1863                         ieee80211_handle_pwr_constr(sdata,
1864                                 le16_to_cpu(mgmt->u.probe_resp.capab_info),
1865                                 elems.pwr_constr_elem,
1866                                 elems.pwr_constr_elem_len);
1867         }
1868
1869         ieee80211_bss_info_change_notify(sdata, changed);
1870 }
1871
1872 ieee80211_rx_result ieee80211_sta_rx_mgmt(struct ieee80211_sub_if_data *sdata,
1873                                           struct sk_buff *skb,
1874                                           struct ieee80211_rx_status *rx_status)
1875 {
1876         struct ieee80211_local *local = sdata->local;
1877         struct ieee80211_mgmt *mgmt;
1878         u16 fc;
1879
1880         if (skb->len < 24)
1881                 return RX_DROP_MONITOR;
1882
1883         mgmt = (struct ieee80211_mgmt *) skb->data;
1884         fc = le16_to_cpu(mgmt->frame_control);
1885
1886         switch (fc & IEEE80211_FCTL_STYPE) {
1887         case IEEE80211_STYPE_PROBE_REQ:
1888         case IEEE80211_STYPE_PROBE_RESP:
1889         case IEEE80211_STYPE_BEACON:
1890                 memcpy(skb->cb, rx_status, sizeof(*rx_status));
1891         case IEEE80211_STYPE_AUTH:
1892         case IEEE80211_STYPE_ASSOC_RESP:
1893         case IEEE80211_STYPE_REASSOC_RESP:
1894         case IEEE80211_STYPE_DEAUTH:
1895         case IEEE80211_STYPE_DISASSOC:
1896                 skb_queue_tail(&sdata->u.mgd.skb_queue, skb);
1897                 queue_work(local->hw.workqueue, &sdata->u.mgd.work);
1898                 return RX_QUEUED;
1899         }
1900
1901         return RX_DROP_MONITOR;
1902 }
1903
1904 static void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
1905                                          struct sk_buff *skb)
1906 {
1907         struct ieee80211_rx_status *rx_status;
1908         struct ieee80211_mgmt *mgmt;
1909         u16 fc;
1910
1911         rx_status = (struct ieee80211_rx_status *) skb->cb;
1912         mgmt = (struct ieee80211_mgmt *) skb->data;
1913         fc = le16_to_cpu(mgmt->frame_control);
1914
1915         switch (fc & IEEE80211_FCTL_STYPE) {
1916         case IEEE80211_STYPE_PROBE_RESP:
1917                 ieee80211_rx_mgmt_probe_resp(sdata, mgmt, skb->len,
1918                                              rx_status);
1919                 break;
1920         case IEEE80211_STYPE_BEACON:
1921                 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len,
1922                                          rx_status);
1923                 break;
1924         case IEEE80211_STYPE_AUTH:
1925                 ieee80211_rx_mgmt_auth(sdata, mgmt, skb->len);
1926                 break;
1927         case IEEE80211_STYPE_ASSOC_RESP:
1928                 ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len, 0);
1929                 break;
1930         case IEEE80211_STYPE_REASSOC_RESP:
1931                 ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len, 1);
1932                 break;
1933         case IEEE80211_STYPE_DEAUTH:
1934                 ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
1935                 break;
1936         case IEEE80211_STYPE_DISASSOC:
1937                 ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
1938                 break;
1939         }
1940
1941         kfree_skb(skb);
1942 }
1943
1944 static void ieee80211_sta_timer(unsigned long data)
1945 {
1946         struct ieee80211_sub_if_data *sdata =
1947                 (struct ieee80211_sub_if_data *) data;
1948         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1949         struct ieee80211_local *local = sdata->local;
1950
1951         set_bit(IEEE80211_STA_REQ_RUN, &ifmgd->request);
1952         queue_work(local->hw.workqueue, &ifmgd->work);
1953 }
1954
1955 static void ieee80211_sta_reset_auth(struct ieee80211_sub_if_data *sdata)
1956 {
1957         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1958         struct ieee80211_local *local = sdata->local;
1959
1960         if (local->ops->reset_tsf) {
1961                 /* Reset own TSF to allow time synchronization work. */
1962                 local->ops->reset_tsf(local_to_hw(local));
1963         }
1964
1965         ifmgd->wmm_last_param_set = -1; /* allow any WMM update */
1966
1967
1968         if (ifmgd->auth_algs & IEEE80211_AUTH_ALG_OPEN)
1969                 ifmgd->auth_alg = WLAN_AUTH_OPEN;
1970         else if (ifmgd->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
1971                 ifmgd->auth_alg = WLAN_AUTH_SHARED_KEY;
1972         else if (ifmgd->auth_algs & IEEE80211_AUTH_ALG_LEAP)
1973                 ifmgd->auth_alg = WLAN_AUTH_LEAP;
1974         else if (ifmgd->auth_algs & IEEE80211_AUTH_ALG_FT)
1975                 ifmgd->auth_alg = WLAN_AUTH_FT;
1976         else
1977                 ifmgd->auth_alg = WLAN_AUTH_OPEN;
1978         ifmgd->auth_transaction = -1;
1979         ifmgd->flags &= ~IEEE80211_STA_ASSOCIATED;
1980         ifmgd->assoc_scan_tries = 0;
1981         ifmgd->direct_probe_tries = 0;
1982         ifmgd->auth_tries = 0;
1983         ifmgd->assoc_tries = 0;
1984         netif_tx_stop_all_queues(sdata->dev);
1985         netif_carrier_off(sdata->dev);
1986 }
1987
1988 static int ieee80211_sta_config_auth(struct ieee80211_sub_if_data *sdata)
1989 {
1990         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1991         struct ieee80211_local *local = sdata->local;
1992         struct ieee80211_bss *bss;
1993         u8 *bssid = ifmgd->bssid, *ssid = ifmgd->ssid;
1994         u8 ssid_len = ifmgd->ssid_len;
1995         u16 capa_mask = WLAN_CAPABILITY_ESS;
1996         u16 capa_val = WLAN_CAPABILITY_ESS;
1997         struct ieee80211_channel *chan = local->oper_channel;
1998
1999         if (!(ifmgd->flags & IEEE80211_STA_EXT_SME) &&
2000             ifmgd->flags & (IEEE80211_STA_AUTO_SSID_SEL |
2001                             IEEE80211_STA_AUTO_BSSID_SEL |
2002                             IEEE80211_STA_AUTO_CHANNEL_SEL)) {
2003                 capa_mask |= WLAN_CAPABILITY_PRIVACY;
2004                 if (sdata->default_key)
2005                         capa_val |= WLAN_CAPABILITY_PRIVACY;
2006         }
2007
2008         if (ifmgd->flags & IEEE80211_STA_AUTO_CHANNEL_SEL)
2009                 chan = NULL;
2010
2011         if (ifmgd->flags & IEEE80211_STA_AUTO_BSSID_SEL)
2012                 bssid = NULL;
2013
2014         if (ifmgd->flags & IEEE80211_STA_AUTO_SSID_SEL) {
2015                 ssid = NULL;
2016                 ssid_len = 0;
2017         }
2018
2019         bss = (void *)cfg80211_get_bss(local->hw.wiphy, chan,
2020                                        bssid, ssid, ssid_len,
2021                                        capa_mask, capa_val);
2022
2023         if (bss) {
2024                 ieee80211_set_freq(sdata, bss->cbss.channel->center_freq);
2025                 if (!(ifmgd->flags & IEEE80211_STA_SSID_SET))
2026                         ieee80211_sta_set_ssid(sdata, bss->ssid,
2027                                                bss->ssid_len);
2028                 ieee80211_sta_set_bssid(sdata, bss->cbss.bssid);
2029                 ieee80211_sta_def_wmm_params(sdata, bss->supp_rates_len,
2030                                                     bss->supp_rates);
2031                 if (sdata->u.mgd.mfp == IEEE80211_MFP_REQUIRED)
2032                         sdata->u.mgd.flags |= IEEE80211_STA_MFP_ENABLED;
2033                 else
2034                         sdata->u.mgd.flags &= ~IEEE80211_STA_MFP_ENABLED;
2035
2036                 /* Send out direct probe if no probe resp was received or
2037                  * the one we have is outdated
2038                  */
2039                 if (!bss->last_probe_resp ||
2040                     time_after(jiffies, bss->last_probe_resp
2041                                         + IEEE80211_SCAN_RESULT_EXPIRE))
2042                         ifmgd->state = IEEE80211_STA_MLME_DIRECT_PROBE;
2043                 else
2044                         ifmgd->state = IEEE80211_STA_MLME_AUTHENTICATE;
2045
2046                 ieee80211_rx_bss_put(local, bss);
2047                 ieee80211_sta_reset_auth(sdata);
2048                 return 0;
2049         } else {
2050                 if (ifmgd->assoc_scan_tries < IEEE80211_ASSOC_SCANS_MAX_TRIES) {
2051                         ifmgd->assoc_scan_tries++;
2052                         /* XXX maybe racy? */
2053                         if (local->scan_req)
2054                                 return -1;
2055                         memcpy(local->int_scan_req.ssids[0].ssid,
2056                                ifmgd->ssid, IEEE80211_MAX_SSID_LEN);
2057                         if (ifmgd->flags & IEEE80211_STA_AUTO_SSID_SEL)
2058                                 local->int_scan_req.ssids[0].ssid_len = 0;
2059                         else
2060                                 local->int_scan_req.ssids[0].ssid_len = ifmgd->ssid_len;
2061
2062                         if (ieee80211_start_scan(sdata, &local->int_scan_req))
2063                                 ieee80211_scan_failed(local);
2064
2065                         ifmgd->state = IEEE80211_STA_MLME_AUTHENTICATE;
2066                         set_bit(IEEE80211_STA_REQ_AUTH, &ifmgd->request);
2067                 } else {
2068                         ifmgd->assoc_scan_tries = 0;
2069                         ifmgd->state = IEEE80211_STA_MLME_DISABLED;
2070                 }
2071         }
2072         return -1;
2073 }
2074
2075
2076 static void ieee80211_sta_work(struct work_struct *work)
2077 {
2078         struct ieee80211_sub_if_data *sdata =
2079                 container_of(work, struct ieee80211_sub_if_data, u.mgd.work);
2080         struct ieee80211_local *local = sdata->local;
2081         struct ieee80211_if_managed *ifmgd;
2082         struct sk_buff *skb;
2083
2084         if (!netif_running(sdata->dev))
2085                 return;
2086
2087         if (local->sw_scanning || local->hw_scanning)
2088                 return;
2089
2090         if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
2091                 return;
2092         ifmgd = &sdata->u.mgd;
2093
2094         while ((skb = skb_dequeue(&ifmgd->skb_queue)))
2095                 ieee80211_sta_rx_queued_mgmt(sdata, skb);
2096
2097         if (ifmgd->state != IEEE80211_STA_MLME_DIRECT_PROBE &&
2098             ifmgd->state != IEEE80211_STA_MLME_AUTHENTICATE &&
2099             ifmgd->state != IEEE80211_STA_MLME_ASSOCIATE &&
2100             test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifmgd->request)) {
2101                 /*
2102                  * The call to ieee80211_start_scan can fail but ieee80211_request_scan
2103                  * (which queued ieee80211_sta_work) did not return an error. Thus, call
2104                  * ieee80211_scan_failed here if ieee80211_start_scan fails in order to
2105                  * notify the scan requester.
2106                  */
2107                 if (ieee80211_start_scan(sdata, local->scan_req))
2108                         ieee80211_scan_failed(local);
2109                 return;
2110         }
2111
2112         if (test_and_clear_bit(IEEE80211_STA_REQ_AUTH, &ifmgd->request)) {
2113                 if (ieee80211_sta_config_auth(sdata))
2114                         return;
2115                 clear_bit(IEEE80211_STA_REQ_RUN, &ifmgd->request);
2116         } else if (!test_and_clear_bit(IEEE80211_STA_REQ_RUN, &ifmgd->request))
2117                 return;
2118
2119         switch (ifmgd->state) {
2120         case IEEE80211_STA_MLME_DISABLED:
2121                 break;
2122         case IEEE80211_STA_MLME_DIRECT_PROBE:
2123                 ieee80211_direct_probe(sdata);
2124                 break;
2125         case IEEE80211_STA_MLME_AUTHENTICATE:
2126                 ieee80211_authenticate(sdata);
2127                 break;
2128         case IEEE80211_STA_MLME_ASSOCIATE:
2129                 ieee80211_associate(sdata);
2130                 break;
2131         case IEEE80211_STA_MLME_ASSOCIATED:
2132                 ieee80211_associated(sdata);
2133                 break;
2134         default:
2135                 WARN_ON(1);
2136                 break;
2137         }
2138
2139         if (ieee80211_privacy_mismatch(sdata)) {
2140                 printk(KERN_DEBUG "%s: privacy configuration mismatch and "
2141                        "mixed-cell disabled - disassociate\n", sdata->dev->name);
2142
2143                 ieee80211_set_disassoc(sdata, false, true,
2144                                         WLAN_REASON_UNSPECIFIED);
2145         }
2146 }
2147
2148 static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
2149 {
2150         if (sdata->vif.type == NL80211_IFTYPE_STATION) {
2151                 /*
2152                  * Need to update last_beacon to avoid beacon loss
2153                  * test to trigger.
2154                  */
2155                 sdata->u.mgd.last_beacon = jiffies;
2156
2157
2158                 queue_work(sdata->local->hw.workqueue,
2159                            &sdata->u.mgd.work);
2160         }
2161 }
2162
2163 /* interface setup */
2164 void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
2165 {
2166         struct ieee80211_if_managed *ifmgd;
2167
2168         ifmgd = &sdata->u.mgd;
2169         INIT_WORK(&ifmgd->work, ieee80211_sta_work);
2170         INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
2171         INIT_WORK(&ifmgd->beacon_loss_work, ieee80211_beacon_loss_work);
2172         setup_timer(&ifmgd->timer, ieee80211_sta_timer,
2173                     (unsigned long) sdata);
2174         setup_timer(&ifmgd->chswitch_timer, ieee80211_chswitch_timer,
2175                     (unsigned long) sdata);
2176         skb_queue_head_init(&ifmgd->skb_queue);
2177
2178         ifmgd->capab = WLAN_CAPABILITY_ESS;
2179         ifmgd->auth_algs = IEEE80211_AUTH_ALG_OPEN |
2180                 IEEE80211_AUTH_ALG_SHARED_KEY;
2181         ifmgd->flags |= IEEE80211_STA_CREATE_IBSS |
2182                 IEEE80211_STA_AUTO_BSSID_SEL |
2183                 IEEE80211_STA_AUTO_CHANNEL_SEL;
2184         if (sdata->local->hw.queues >= 4)
2185                 ifmgd->flags |= IEEE80211_STA_WMM_ENABLED;
2186 }
2187
2188 /* configuration hooks */
2189 void ieee80211_sta_req_auth(struct ieee80211_sub_if_data *sdata)
2190 {
2191         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2192         struct ieee80211_local *local = sdata->local;
2193
2194         if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
2195                 return;
2196
2197         if ((ifmgd->flags & (IEEE80211_STA_BSSID_SET |
2198                              IEEE80211_STA_AUTO_BSSID_SEL)) &&
2199             (ifmgd->flags & (IEEE80211_STA_SSID_SET |
2200                              IEEE80211_STA_AUTO_SSID_SEL))) {
2201
2202                 if (ifmgd->state == IEEE80211_STA_MLME_ASSOCIATED)
2203                         ieee80211_set_disassoc(sdata, true, true,
2204                                                WLAN_REASON_DEAUTH_LEAVING);
2205
2206                 if (!(ifmgd->flags & IEEE80211_STA_EXT_SME) ||
2207                     ifmgd->state != IEEE80211_STA_MLME_ASSOCIATE)
2208                         set_bit(IEEE80211_STA_REQ_AUTH, &ifmgd->request);
2209                 else if (ifmgd->flags & IEEE80211_STA_EXT_SME)
2210                         set_bit(IEEE80211_STA_REQ_RUN, &ifmgd->request);
2211                 queue_work(local->hw.workqueue, &ifmgd->work);
2212         }
2213 }
2214
2215 int ieee80211_sta_commit(struct ieee80211_sub_if_data *sdata)
2216 {
2217         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2218
2219         if (ifmgd->ssid_len)
2220                 ifmgd->flags |= IEEE80211_STA_SSID_SET;
2221         else
2222                 ifmgd->flags &= ~IEEE80211_STA_SSID_SET;
2223
2224         return 0;
2225 }
2226
2227 int ieee80211_sta_set_ssid(struct ieee80211_sub_if_data *sdata, char *ssid, size_t len)
2228 {
2229         struct ieee80211_if_managed *ifmgd;
2230
2231         if (len > IEEE80211_MAX_SSID_LEN)
2232                 return -EINVAL;
2233
2234         ifmgd = &sdata->u.mgd;
2235
2236         if (ifmgd->ssid_len != len || memcmp(ifmgd->ssid, ssid, len) != 0) {
2237                 /*
2238                  * Do not use reassociation if SSID is changed (different ESS).
2239                  */
2240                 ifmgd->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
2241                 memset(ifmgd->ssid, 0, sizeof(ifmgd->ssid));
2242                 memcpy(ifmgd->ssid, ssid, len);
2243                 ifmgd->ssid_len = len;
2244         }
2245
2246         return ieee80211_sta_commit(sdata);
2247 }
2248
2249 int ieee80211_sta_get_ssid(struct ieee80211_sub_if_data *sdata, char *ssid, size_t *len)
2250 {
2251         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2252         memcpy(ssid, ifmgd->ssid, ifmgd->ssid_len);
2253         *len = ifmgd->ssid_len;
2254         return 0;
2255 }
2256
2257 int ieee80211_sta_set_bssid(struct ieee80211_sub_if_data *sdata, u8 *bssid)
2258 {
2259         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2260
2261         if (is_valid_ether_addr(bssid)) {
2262                 memcpy(ifmgd->bssid, bssid, ETH_ALEN);
2263                 ifmgd->flags |= IEEE80211_STA_BSSID_SET;
2264         } else {
2265                 memset(ifmgd->bssid, 0, ETH_ALEN);
2266                 ifmgd->flags &= ~IEEE80211_STA_BSSID_SET;
2267         }
2268
2269         if (netif_running(sdata->dev)) {
2270                 if (ieee80211_if_config(sdata, IEEE80211_IFCC_BSSID)) {
2271                         printk(KERN_DEBUG "%s: Failed to config new BSSID to "
2272                                "the low-level driver\n", sdata->dev->name);
2273                 }
2274         }
2275
2276         return ieee80211_sta_commit(sdata);
2277 }
2278
2279 int ieee80211_sta_set_extra_ie(struct ieee80211_sub_if_data *sdata,
2280                                const char *ie, size_t len)
2281 {
2282         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2283
2284         kfree(ifmgd->extra_ie);
2285         if (len == 0) {
2286                 ifmgd->extra_ie = NULL;
2287                 ifmgd->extra_ie_len = 0;
2288                 return 0;
2289         }
2290         ifmgd->extra_ie = kmalloc(len, GFP_KERNEL);
2291         if (!ifmgd->extra_ie) {
2292                 ifmgd->extra_ie_len = 0;
2293                 return -ENOMEM;
2294         }
2295         memcpy(ifmgd->extra_ie, ie, len);
2296         ifmgd->extra_ie_len = len;
2297         return 0;
2298 }
2299
2300 int ieee80211_sta_deauthenticate(struct ieee80211_sub_if_data *sdata, u16 reason)
2301 {
2302         printk(KERN_DEBUG "%s: deauthenticating by local choice (reason=%d)\n",
2303                sdata->dev->name, reason);
2304
2305         if (sdata->vif.type != NL80211_IFTYPE_STATION)
2306                 return -EINVAL;
2307
2308         ieee80211_set_disassoc(sdata, true, true, reason);
2309         return 0;
2310 }
2311
2312 int ieee80211_sta_disassociate(struct ieee80211_sub_if_data *sdata, u16 reason)
2313 {
2314         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2315
2316         printk(KERN_DEBUG "%s: disassociating by local choice (reason=%d)\n",
2317                sdata->dev->name, reason);
2318
2319         if (sdata->vif.type != NL80211_IFTYPE_STATION)
2320                 return -EINVAL;
2321
2322         if (!(ifmgd->flags & IEEE80211_STA_ASSOCIATED))
2323                 return -ENOLINK;
2324
2325         ieee80211_set_disassoc(sdata, false, true, reason);
2326         return 0;
2327 }
2328
2329 /* scan finished notification */
2330 void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
2331 {
2332         struct ieee80211_sub_if_data *sdata = local->scan_sdata;
2333
2334         /* Restart STA timers */
2335         rcu_read_lock();
2336         list_for_each_entry_rcu(sdata, &local->interfaces, list)
2337                 ieee80211_restart_sta_timer(sdata);
2338         rcu_read_unlock();
2339 }
2340
2341 int ieee80211_max_network_latency(struct notifier_block *nb,
2342                                   unsigned long data, void *dummy)
2343 {
2344         s32 latency_usec = (s32) data;
2345         struct ieee80211_local *local =
2346                 container_of(nb, struct ieee80211_local,
2347                              network_latency_notifier);
2348
2349         mutex_lock(&local->iflist_mtx);
2350         ieee80211_recalc_ps(local, latency_usec);
2351         mutex_unlock(&local->iflist_mtx);
2352
2353         return 0;
2354 }