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