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