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