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