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