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