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