]> Pileus Git - ~andy/linux/blob - net/mac80211/mlme.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[~andy/linux] / net / mac80211 / mlme.c
1 /*
2  * BSS client mode implementation
3  * Copyright 2003-2008, Jouni Malinen <j@w1.fi>
4  * Copyright 2004, Instant802 Networks, Inc.
5  * Copyright 2005, Devicescape Software, Inc.
6  * Copyright 2006-2007  Jiri Benc <jbenc@suse.cz>
7  * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  */
13
14 #include <linux/delay.h>
15 #include <linux/if_ether.h>
16 #include <linux/skbuff.h>
17 #include <linux/if_arp.h>
18 #include <linux/etherdevice.h>
19 #include <linux/moduleparam.h>
20 #include <linux/rtnetlink.h>
21 #include <linux/pm_qos.h>
22 #include <linux/crc32.h>
23 #include <linux/slab.h>
24 #include <linux/export.h>
25 #include <net/mac80211.h>
26 #include <asm/unaligned.h>
27
28 #include "ieee80211_i.h"
29 #include "driver-ops.h"
30 #include "rate.h"
31 #include "led.h"
32
33 #define IEEE80211_AUTH_TIMEOUT          (HZ / 5)
34 #define IEEE80211_AUTH_TIMEOUT_SHORT    (HZ / 10)
35 #define IEEE80211_AUTH_MAX_TRIES        3
36 #define IEEE80211_AUTH_WAIT_ASSOC       (HZ * 5)
37 #define IEEE80211_ASSOC_TIMEOUT         (HZ / 5)
38 #define IEEE80211_ASSOC_TIMEOUT_SHORT   (HZ / 10)
39 #define IEEE80211_ASSOC_MAX_TRIES       3
40
41 static int max_nullfunc_tries = 2;
42 module_param(max_nullfunc_tries, int, 0644);
43 MODULE_PARM_DESC(max_nullfunc_tries,
44                  "Maximum nullfunc tx tries before disconnecting (reason 4).");
45
46 static int max_probe_tries = 5;
47 module_param(max_probe_tries, int, 0644);
48 MODULE_PARM_DESC(max_probe_tries,
49                  "Maximum probe tries before disconnecting (reason 4).");
50
51 /*
52  * Beacon loss timeout is calculated as N frames times the
53  * advertised beacon interval.  This may need to be somewhat
54  * higher than what hardware might detect to account for
55  * delays in the host processing frames. But since we also
56  * probe on beacon miss before declaring the connection lost
57  * default to what we want.
58  */
59 #define IEEE80211_BEACON_LOSS_COUNT     7
60
61 /*
62  * Time the connection can be idle before we probe
63  * it to see if we can still talk to the AP.
64  */
65 #define IEEE80211_CONNECTION_IDLE_TIME  (30 * HZ)
66 /*
67  * Time we wait for a probe response after sending
68  * a probe request because of beacon loss or for
69  * checking the connection still works.
70  */
71 static int probe_wait_ms = 500;
72 module_param(probe_wait_ms, int, 0644);
73 MODULE_PARM_DESC(probe_wait_ms,
74                  "Maximum time(ms) to wait for probe response"
75                  " before disconnecting (reason 4).");
76
77 /*
78  * Weight given to the latest Beacon frame when calculating average signal
79  * strength for Beacon frames received in the current BSS. This must be
80  * between 1 and 15.
81  */
82 #define IEEE80211_SIGNAL_AVE_WEIGHT     3
83
84 /*
85  * How many Beacon frames need to have been used in average signal strength
86  * before starting to indicate signal change events.
87  */
88 #define IEEE80211_SIGNAL_AVE_MIN_COUNT  4
89
90 /*
91  * All cfg80211 functions have to be called outside a locked
92  * section so that they can acquire a lock themselves... This
93  * is much simpler than queuing up things in cfg80211, but we
94  * do need some indirection for that here.
95  */
96 enum rx_mgmt_action {
97         /* no action required */
98         RX_MGMT_NONE,
99
100         /* caller must call cfg80211_send_deauth() */
101         RX_MGMT_CFG80211_DEAUTH,
102
103         /* caller must call cfg80211_send_disassoc() */
104         RX_MGMT_CFG80211_DISASSOC,
105
106         /* caller must call cfg80211_send_rx_auth() */
107         RX_MGMT_CFG80211_RX_AUTH,
108
109         /* caller must call cfg80211_send_rx_assoc() */
110         RX_MGMT_CFG80211_RX_ASSOC,
111
112         /* caller must call cfg80211_send_assoc_timeout() */
113         RX_MGMT_CFG80211_ASSOC_TIMEOUT,
114
115         /* used when a processed beacon causes a deauth */
116         RX_MGMT_CFG80211_TX_DEAUTH,
117 };
118
119 /* utils */
120 static inline void ASSERT_MGD_MTX(struct ieee80211_if_managed *ifmgd)
121 {
122         lockdep_assert_held(&ifmgd->mtx);
123 }
124
125 /*
126  * We can have multiple work items (and connection probing)
127  * scheduling this timer, but we need to take care to only
128  * reschedule it when it should fire _earlier_ than it was
129  * asked for before, or if it's not pending right now. This
130  * function ensures that. Note that it then is required to
131  * run this function for all timeouts after the first one
132  * has happened -- the work that runs from this timer will
133  * do that.
134  */
135 static void run_again(struct ieee80211_if_managed *ifmgd, unsigned long timeout)
136 {
137         ASSERT_MGD_MTX(ifmgd);
138
139         if (!timer_pending(&ifmgd->timer) ||
140             time_before(timeout, ifmgd->timer.expires))
141                 mod_timer(&ifmgd->timer, timeout);
142 }
143
144 void ieee80211_sta_reset_beacon_monitor(struct ieee80211_sub_if_data *sdata)
145 {
146         if (sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)
147                 return;
148
149         if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
150                 return;
151
152         mod_timer(&sdata->u.mgd.bcn_mon_timer,
153                   round_jiffies_up(jiffies + sdata->u.mgd.beacon_timeout));
154 }
155
156 void ieee80211_sta_reset_conn_monitor(struct ieee80211_sub_if_data *sdata)
157 {
158         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
159
160         if (unlikely(!sdata->u.mgd.associated))
161                 return;
162
163         if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
164                 return;
165
166         mod_timer(&sdata->u.mgd.conn_mon_timer,
167                   round_jiffies_up(jiffies + IEEE80211_CONNECTION_IDLE_TIME));
168
169         ifmgd->probe_send_count = 0;
170 }
171
172 static int ecw2cw(int ecw)
173 {
174         return (1 << ecw) - 1;
175 }
176
177 static u32 chandef_downgrade(struct cfg80211_chan_def *c)
178 {
179         u32 ret;
180         int tmp;
181
182         switch (c->width) {
183         case NL80211_CHAN_WIDTH_20:
184                 c->width = NL80211_CHAN_WIDTH_20_NOHT;
185                 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
186                 break;
187         case NL80211_CHAN_WIDTH_40:
188                 c->width = NL80211_CHAN_WIDTH_20;
189                 c->center_freq1 = c->chan->center_freq;
190                 ret = IEEE80211_STA_DISABLE_40MHZ |
191                       IEEE80211_STA_DISABLE_VHT;
192                 break;
193         case NL80211_CHAN_WIDTH_80:
194                 tmp = (30 + c->chan->center_freq - c->center_freq1)/20;
195                 /* n_P40 */
196                 tmp /= 2;
197                 /* freq_P40 */
198                 c->center_freq1 = c->center_freq1 - 20 + 40 * tmp;
199                 c->width = NL80211_CHAN_WIDTH_40;
200                 ret = IEEE80211_STA_DISABLE_VHT;
201                 break;
202         case NL80211_CHAN_WIDTH_80P80:
203                 c->center_freq2 = 0;
204                 c->width = NL80211_CHAN_WIDTH_80;
205                 ret = IEEE80211_STA_DISABLE_80P80MHZ |
206                       IEEE80211_STA_DISABLE_160MHZ;
207                 break;
208         case NL80211_CHAN_WIDTH_160:
209                 /* n_P20 */
210                 tmp = (70 + c->chan->center_freq - c->center_freq1)/20;
211                 /* n_P80 */
212                 tmp /= 4;
213                 c->center_freq1 = c->center_freq1 - 40 + 80 * tmp;
214                 c->width = NL80211_CHAN_WIDTH_80;
215                 ret = IEEE80211_STA_DISABLE_80P80MHZ |
216                       IEEE80211_STA_DISABLE_160MHZ;
217                 break;
218         default:
219         case NL80211_CHAN_WIDTH_20_NOHT:
220                 WARN_ON_ONCE(1);
221                 c->width = NL80211_CHAN_WIDTH_20_NOHT;
222                 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
223                 break;
224         }
225
226         WARN_ON_ONCE(!cfg80211_chandef_valid(c));
227
228         return ret;
229 }
230
231 static u32
232 ieee80211_determine_chantype(struct ieee80211_sub_if_data *sdata,
233                              struct ieee80211_supported_band *sband,
234                              struct ieee80211_channel *channel,
235                              const struct ieee80211_ht_operation *ht_oper,
236                              const struct ieee80211_vht_operation *vht_oper,
237                              struct cfg80211_chan_def *chandef, bool verbose)
238 {
239         struct cfg80211_chan_def vht_chandef;
240         u32 ht_cfreq, ret;
241
242         chandef->chan = channel;
243         chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
244         chandef->center_freq1 = channel->center_freq;
245         chandef->center_freq2 = 0;
246
247         if (!ht_oper || !sband->ht_cap.ht_supported) {
248                 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
249                 goto out;
250         }
251
252         chandef->width = NL80211_CHAN_WIDTH_20;
253
254         ht_cfreq = ieee80211_channel_to_frequency(ht_oper->primary_chan,
255                                                   channel->band);
256         /* check that channel matches the right operating channel */
257         if (channel->center_freq != ht_cfreq) {
258                 /*
259                  * It's possible that some APs are confused here;
260                  * Netgear WNDR3700 sometimes reports 4 higher than
261                  * the actual channel in association responses, but
262                  * since we look at probe response/beacon data here
263                  * it should be OK.
264                  */
265                 if (verbose)
266                         sdata_info(sdata,
267                                    "Wrong control channel: center-freq: %d ht-cfreq: %d ht->primary_chan: %d band: %d - Disabling HT\n",
268                                    channel->center_freq, ht_cfreq,
269                                    ht_oper->primary_chan, channel->band);
270                 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
271                 goto out;
272         }
273
274         /* check 40 MHz support, if we have it */
275         if (sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) {
276                 switch (ht_oper->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
277                 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
278                         chandef->width = NL80211_CHAN_WIDTH_40;
279                         chandef->center_freq1 += 10;
280                         break;
281                 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
282                         chandef->width = NL80211_CHAN_WIDTH_40;
283                         chandef->center_freq1 -= 10;
284                         break;
285                 }
286         } else {
287                 /* 40 MHz (and 80 MHz) must be supported for VHT */
288                 ret = IEEE80211_STA_DISABLE_VHT;
289                 goto out;
290         }
291
292         if (!vht_oper || !sband->vht_cap.vht_supported) {
293                 ret = IEEE80211_STA_DISABLE_VHT;
294                 goto out;
295         }
296
297         vht_chandef.chan = channel;
298         vht_chandef.center_freq1 =
299                 ieee80211_channel_to_frequency(vht_oper->center_freq_seg1_idx,
300                                                channel->band);
301         vht_chandef.center_freq2 = 0;
302
303         if (vht_oper->center_freq_seg2_idx)
304                 vht_chandef.center_freq2 =
305                         ieee80211_channel_to_frequency(
306                                 vht_oper->center_freq_seg2_idx,
307                                 channel->band);
308
309         switch (vht_oper->chan_width) {
310         case IEEE80211_VHT_CHANWIDTH_USE_HT:
311                 vht_chandef.width = chandef->width;
312                 break;
313         case IEEE80211_VHT_CHANWIDTH_80MHZ:
314                 vht_chandef.width = NL80211_CHAN_WIDTH_80;
315                 break;
316         case IEEE80211_VHT_CHANWIDTH_160MHZ:
317                 vht_chandef.width = NL80211_CHAN_WIDTH_160;
318                 break;
319         case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
320                 vht_chandef.width = NL80211_CHAN_WIDTH_80P80;
321                 break;
322         default:
323                 if (verbose)
324                         sdata_info(sdata,
325                                    "AP VHT operation IE has invalid channel width (%d), disable VHT\n",
326                                    vht_oper->chan_width);
327                 ret = IEEE80211_STA_DISABLE_VHT;
328                 goto out;
329         }
330
331         if (!cfg80211_chandef_valid(&vht_chandef)) {
332                 if (verbose)
333                         sdata_info(sdata,
334                                    "AP VHT information is invalid, disable VHT\n");
335                 ret = IEEE80211_STA_DISABLE_VHT;
336                 goto out;
337         }
338
339         if (cfg80211_chandef_identical(chandef, &vht_chandef)) {
340                 ret = 0;
341                 goto out;
342         }
343
344         if (!cfg80211_chandef_compatible(chandef, &vht_chandef)) {
345                 if (verbose)
346                         sdata_info(sdata,
347                                    "AP VHT information doesn't match HT, disable VHT\n");
348                 ret = IEEE80211_STA_DISABLE_VHT;
349                 goto out;
350         }
351
352         *chandef = vht_chandef;
353
354         ret = 0;
355
356 out:
357         /* don't print the message below for VHT mismatch if VHT is disabled */
358         if (ret & IEEE80211_STA_DISABLE_VHT)
359                 vht_chandef = *chandef;
360
361         while (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
362                                         IEEE80211_CHAN_DISABLED)) {
363                 if (WARN_ON(chandef->width == NL80211_CHAN_WIDTH_20_NOHT)) {
364                         ret = IEEE80211_STA_DISABLE_HT |
365                               IEEE80211_STA_DISABLE_VHT;
366                         goto out;
367                 }
368
369                 ret |= chandef_downgrade(chandef);
370         }
371
372         if (chandef->width != vht_chandef.width && verbose)
373                 sdata_info(sdata,
374                            "capabilities/regulatory prevented using AP HT/VHT configuration, downgraded\n");
375
376         WARN_ON_ONCE(!cfg80211_chandef_valid(chandef));
377         return ret;
378 }
379
380 static int ieee80211_config_bw(struct ieee80211_sub_if_data *sdata,
381                                struct sta_info *sta,
382                                const struct ieee80211_ht_operation *ht_oper,
383                                const struct ieee80211_vht_operation *vht_oper,
384                                const u8 *bssid, u32 *changed)
385 {
386         struct ieee80211_local *local = sdata->local;
387         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
388         struct ieee80211_supported_band *sband;
389         struct ieee80211_channel *chan;
390         struct cfg80211_chan_def chandef;
391         u16 ht_opmode;
392         u32 flags;
393         enum ieee80211_sta_rx_bandwidth new_sta_bw;
394         int ret;
395
396         /* if HT was/is disabled, don't track any bandwidth changes */
397         if (ifmgd->flags & IEEE80211_STA_DISABLE_HT || !ht_oper)
398                 return 0;
399
400         /* don't check VHT if we associated as non-VHT station */
401         if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
402                 vht_oper = NULL;
403
404         if (WARN_ON_ONCE(!sta))
405                 return -EINVAL;
406
407         chan = sdata->vif.bss_conf.chandef.chan;
408         sband = local->hw.wiphy->bands[chan->band];
409
410         /* calculate new channel (type) based on HT/VHT operation IEs */
411         flags = ieee80211_determine_chantype(sdata, sband, chan, ht_oper,
412                                              vht_oper, &chandef, false);
413
414         /*
415          * Downgrade the new channel if we associated with restricted
416          * capabilities. For example, if we associated as a 20 MHz STA
417          * to a 40 MHz AP (due to regulatory, capabilities or config
418          * reasons) then switching to a 40 MHz channel now won't do us
419          * any good -- we couldn't use it with the AP.
420          */
421         if (ifmgd->flags & IEEE80211_STA_DISABLE_80P80MHZ &&
422             chandef.width == NL80211_CHAN_WIDTH_80P80)
423                 flags |= chandef_downgrade(&chandef);
424         if (ifmgd->flags & IEEE80211_STA_DISABLE_160MHZ &&
425             chandef.width == NL80211_CHAN_WIDTH_160)
426                 flags |= chandef_downgrade(&chandef);
427         if (ifmgd->flags & IEEE80211_STA_DISABLE_40MHZ &&
428             chandef.width > NL80211_CHAN_WIDTH_20)
429                 flags |= chandef_downgrade(&chandef);
430
431         if (cfg80211_chandef_identical(&chandef, &sdata->vif.bss_conf.chandef))
432                 return 0;
433
434         sdata_info(sdata,
435                    "AP %pM changed bandwidth, new config is %d MHz, width %d (%d/%d MHz)\n",
436                    ifmgd->bssid, chandef.chan->center_freq, chandef.width,
437                    chandef.center_freq1, chandef.center_freq2);
438
439         if (flags != (ifmgd->flags & (IEEE80211_STA_DISABLE_HT |
440                                       IEEE80211_STA_DISABLE_VHT |
441                                       IEEE80211_STA_DISABLE_40MHZ |
442                                       IEEE80211_STA_DISABLE_80P80MHZ |
443                                       IEEE80211_STA_DISABLE_160MHZ)) ||
444             !cfg80211_chandef_valid(&chandef)) {
445                 sdata_info(sdata,
446                            "AP %pM changed bandwidth in a way we can't support - disconnect\n",
447                            ifmgd->bssid);
448                 return -EINVAL;
449         }
450
451         switch (chandef.width) {
452         case NL80211_CHAN_WIDTH_20_NOHT:
453         case NL80211_CHAN_WIDTH_20:
454                 new_sta_bw = IEEE80211_STA_RX_BW_20;
455                 break;
456         case NL80211_CHAN_WIDTH_40:
457                 new_sta_bw = IEEE80211_STA_RX_BW_40;
458                 break;
459         case NL80211_CHAN_WIDTH_80:
460                 new_sta_bw = IEEE80211_STA_RX_BW_80;
461                 break;
462         case NL80211_CHAN_WIDTH_80P80:
463         case NL80211_CHAN_WIDTH_160:
464                 new_sta_bw = IEEE80211_STA_RX_BW_160;
465                 break;
466         default:
467                 return -EINVAL;
468         }
469
470         if (new_sta_bw > sta->cur_max_bandwidth)
471                 new_sta_bw = sta->cur_max_bandwidth;
472
473         if (new_sta_bw < sta->sta.bandwidth) {
474                 sta->sta.bandwidth = new_sta_bw;
475                 rate_control_rate_update(local, sband, sta,
476                                          IEEE80211_RC_BW_CHANGED);
477         }
478
479         ret = ieee80211_vif_change_bandwidth(sdata, &chandef, changed);
480         if (ret) {
481                 sdata_info(sdata,
482                            "AP %pM changed bandwidth to incompatible one - disconnect\n",
483                            ifmgd->bssid);
484                 return ret;
485         }
486
487         if (new_sta_bw > sta->sta.bandwidth) {
488                 sta->sta.bandwidth = new_sta_bw;
489                 rate_control_rate_update(local, sband, sta,
490                                          IEEE80211_RC_BW_CHANGED);
491         }
492
493         ht_opmode = le16_to_cpu(ht_oper->operation_mode);
494
495         /* if bss configuration changed store the new one */
496         if (sdata->vif.bss_conf.ht_operation_mode != ht_opmode) {
497                 *changed |= BSS_CHANGED_HT;
498                 sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
499         }
500
501         return 0;
502 }
503
504 /* frame sending functions */
505
506 static int ieee80211_compatible_rates(const u8 *supp_rates, int supp_rates_len,
507                                       struct ieee80211_supported_band *sband,
508                                       u32 *rates)
509 {
510         int i, j, count;
511         *rates = 0;
512         count = 0;
513         for (i = 0; i < supp_rates_len; i++) {
514                 int rate = (supp_rates[i] & 0x7F) * 5;
515
516                 for (j = 0; j < sband->n_bitrates; j++)
517                         if (sband->bitrates[j].bitrate == rate) {
518                                 *rates |= BIT(j);
519                                 count++;
520                                 break;
521                         }
522         }
523
524         return count;
525 }
526
527 static void ieee80211_add_ht_ie(struct ieee80211_sub_if_data *sdata,
528                                 struct sk_buff *skb, u8 ap_ht_param,
529                                 struct ieee80211_supported_band *sband,
530                                 struct ieee80211_channel *channel,
531                                 enum ieee80211_smps_mode smps)
532 {
533         u8 *pos;
534         u32 flags = channel->flags;
535         u16 cap;
536         struct ieee80211_sta_ht_cap ht_cap;
537
538         BUILD_BUG_ON(sizeof(ht_cap) != sizeof(sband->ht_cap));
539
540         memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
541         ieee80211_apply_htcap_overrides(sdata, &ht_cap);
542
543         /* determine capability flags */
544         cap = ht_cap.cap;
545
546         switch (ap_ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
547         case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
548                 if (flags & IEEE80211_CHAN_NO_HT40PLUS) {
549                         cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
550                         cap &= ~IEEE80211_HT_CAP_SGI_40;
551                 }
552                 break;
553         case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
554                 if (flags & IEEE80211_CHAN_NO_HT40MINUS) {
555                         cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
556                         cap &= ~IEEE80211_HT_CAP_SGI_40;
557                 }
558                 break;
559         }
560
561         /*
562          * If 40 MHz was disabled associate as though we weren't
563          * capable of 40 MHz -- some broken APs will never fall
564          * back to trying to transmit in 20 MHz.
565          */
566         if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_40MHZ) {
567                 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
568                 cap &= ~IEEE80211_HT_CAP_SGI_40;
569         }
570
571         /* set SM PS mode properly */
572         cap &= ~IEEE80211_HT_CAP_SM_PS;
573         switch (smps) {
574         case IEEE80211_SMPS_AUTOMATIC:
575         case IEEE80211_SMPS_NUM_MODES:
576                 WARN_ON(1);
577         case IEEE80211_SMPS_OFF:
578                 cap |= WLAN_HT_CAP_SM_PS_DISABLED <<
579                         IEEE80211_HT_CAP_SM_PS_SHIFT;
580                 break;
581         case IEEE80211_SMPS_STATIC:
582                 cap |= WLAN_HT_CAP_SM_PS_STATIC <<
583                         IEEE80211_HT_CAP_SM_PS_SHIFT;
584                 break;
585         case IEEE80211_SMPS_DYNAMIC:
586                 cap |= WLAN_HT_CAP_SM_PS_DYNAMIC <<
587                         IEEE80211_HT_CAP_SM_PS_SHIFT;
588                 break;
589         }
590
591         /* reserve and fill IE */
592         pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
593         ieee80211_ie_build_ht_cap(pos, &ht_cap, cap);
594 }
595
596 static void ieee80211_add_vht_ie(struct ieee80211_sub_if_data *sdata,
597                                  struct sk_buff *skb,
598                                  struct ieee80211_supported_band *sband,
599                                  struct ieee80211_vht_cap *ap_vht_cap)
600 {
601         u8 *pos;
602         u32 cap;
603         struct ieee80211_sta_vht_cap vht_cap;
604         int i;
605
606         BUILD_BUG_ON(sizeof(vht_cap) != sizeof(sband->vht_cap));
607
608         memcpy(&vht_cap, &sband->vht_cap, sizeof(vht_cap));
609         ieee80211_apply_vhtcap_overrides(sdata, &vht_cap);
610
611         /* determine capability flags */
612         cap = vht_cap.cap;
613
614         if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_80P80MHZ) {
615                 cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ;
616                 cap |= IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
617         }
618
619         if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_160MHZ) {
620                 cap &= ~IEEE80211_VHT_CAP_SHORT_GI_160;
621                 cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
622         }
623
624         /*
625          * Some APs apparently get confused if our capabilities are better
626          * than theirs, so restrict what we advertise in the assoc request.
627          */
628         if (!(ap_vht_cap->vht_cap_info &
629                         cpu_to_le32(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)))
630                 cap &= ~IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE;
631
632         if (!(ap_vht_cap->vht_cap_info &
633                         cpu_to_le32(IEEE80211_VHT_CAP_TXSTBC)))
634                 cap &= ~(IEEE80211_VHT_CAP_RXSTBC_1 |
635                          IEEE80211_VHT_CAP_RXSTBC_3 |
636                          IEEE80211_VHT_CAP_RXSTBC_4);
637
638         for (i = 0; i < 8; i++) {
639                 int shift = i * 2;
640                 u16 mask = IEEE80211_VHT_MCS_NOT_SUPPORTED << shift;
641                 u16 ap_mcs, our_mcs;
642
643                 ap_mcs = (le16_to_cpu(ap_vht_cap->supp_mcs.tx_mcs_map) &
644                                                                 mask) >> shift;
645                 our_mcs = (le16_to_cpu(vht_cap.vht_mcs.rx_mcs_map) &
646                                                                 mask) >> shift;
647
648                 if (our_mcs == IEEE80211_VHT_MCS_NOT_SUPPORTED)
649                         continue;
650
651                 switch (ap_mcs) {
652                 default:
653                         if (our_mcs <= ap_mcs)
654                                 break;
655                         /* fall through */
656                 case IEEE80211_VHT_MCS_NOT_SUPPORTED:
657                         vht_cap.vht_mcs.rx_mcs_map &= cpu_to_le16(~mask);
658                         vht_cap.vht_mcs.rx_mcs_map |=
659                                 cpu_to_le16(ap_mcs << shift);
660                 }
661         }
662
663         /* reserve and fill IE */
664         pos = skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2);
665         ieee80211_ie_build_vht_cap(pos, &vht_cap, cap);
666 }
667
668 static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata)
669 {
670         struct ieee80211_local *local = sdata->local;
671         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
672         struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
673         struct sk_buff *skb;
674         struct ieee80211_mgmt *mgmt;
675         u8 *pos, qos_info;
676         size_t offset = 0, noffset;
677         int i, count, rates_len, supp_rates_len;
678         u16 capab;
679         struct ieee80211_supported_band *sband;
680         struct ieee80211_chanctx_conf *chanctx_conf;
681         struct ieee80211_channel *chan;
682         u32 rates = 0;
683
684         lockdep_assert_held(&ifmgd->mtx);
685
686         rcu_read_lock();
687         chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
688         if (WARN_ON(!chanctx_conf)) {
689                 rcu_read_unlock();
690                 return;
691         }
692         chan = chanctx_conf->def.chan;
693         rcu_read_unlock();
694         sband = local->hw.wiphy->bands[chan->band];
695
696         if (assoc_data->supp_rates_len) {
697                 /*
698                  * Get all rates supported by the device and the AP as
699                  * some APs don't like getting a superset of their rates
700                  * in the association request (e.g. D-Link DAP 1353 in
701                  * b-only mode)...
702                  */
703                 rates_len = ieee80211_compatible_rates(assoc_data->supp_rates,
704                                                        assoc_data->supp_rates_len,
705                                                        sband, &rates);
706         } else {
707                 /*
708                  * In case AP not provide any supported rates information
709                  * before association, we send information element(s) with
710                  * all rates that we support.
711                  */
712                 rates = ~0;
713                 rates_len = sband->n_bitrates;
714         }
715
716         skb = alloc_skb(local->hw.extra_tx_headroom +
717                         sizeof(*mgmt) + /* bit too much but doesn't matter */
718                         2 + assoc_data->ssid_len + /* SSID */
719                         4 + rates_len + /* (extended) rates */
720                         4 + /* power capability */
721                         2 + 2 * sband->n_channels + /* supported channels */
722                         2 + sizeof(struct ieee80211_ht_cap) + /* HT */
723                         2 + sizeof(struct ieee80211_vht_cap) + /* VHT */
724                         assoc_data->ie_len + /* extra IEs */
725                         9, /* WMM */
726                         GFP_KERNEL);
727         if (!skb)
728                 return;
729
730         skb_reserve(skb, local->hw.extra_tx_headroom);
731
732         capab = WLAN_CAPABILITY_ESS;
733
734         if (sband->band == IEEE80211_BAND_2GHZ) {
735                 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
736                         capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
737                 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
738                         capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
739         }
740
741         if (assoc_data->capability & WLAN_CAPABILITY_PRIVACY)
742                 capab |= WLAN_CAPABILITY_PRIVACY;
743
744         if ((assoc_data->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
745             (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
746                 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
747
748         mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
749         memset(mgmt, 0, 24);
750         memcpy(mgmt->da, assoc_data->bss->bssid, ETH_ALEN);
751         memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
752         memcpy(mgmt->bssid, assoc_data->bss->bssid, ETH_ALEN);
753
754         if (!is_zero_ether_addr(assoc_data->prev_bssid)) {
755                 skb_put(skb, 10);
756                 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
757                                                   IEEE80211_STYPE_REASSOC_REQ);
758                 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
759                 mgmt->u.reassoc_req.listen_interval =
760                                 cpu_to_le16(local->hw.conf.listen_interval);
761                 memcpy(mgmt->u.reassoc_req.current_ap, assoc_data->prev_bssid,
762                        ETH_ALEN);
763         } else {
764                 skb_put(skb, 4);
765                 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
766                                                   IEEE80211_STYPE_ASSOC_REQ);
767                 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
768                 mgmt->u.assoc_req.listen_interval =
769                                 cpu_to_le16(local->hw.conf.listen_interval);
770         }
771
772         /* SSID */
773         pos = skb_put(skb, 2 + assoc_data->ssid_len);
774         *pos++ = WLAN_EID_SSID;
775         *pos++ = assoc_data->ssid_len;
776         memcpy(pos, assoc_data->ssid, assoc_data->ssid_len);
777
778         /* add all rates which were marked to be used above */
779         supp_rates_len = rates_len;
780         if (supp_rates_len > 8)
781                 supp_rates_len = 8;
782
783         pos = skb_put(skb, supp_rates_len + 2);
784         *pos++ = WLAN_EID_SUPP_RATES;
785         *pos++ = supp_rates_len;
786
787         count = 0;
788         for (i = 0; i < sband->n_bitrates; i++) {
789                 if (BIT(i) & rates) {
790                         int rate = sband->bitrates[i].bitrate;
791                         *pos++ = (u8) (rate / 5);
792                         if (++count == 8)
793                                 break;
794                 }
795         }
796
797         if (rates_len > count) {
798                 pos = skb_put(skb, rates_len - count + 2);
799                 *pos++ = WLAN_EID_EXT_SUPP_RATES;
800                 *pos++ = rates_len - count;
801
802                 for (i++; i < sband->n_bitrates; i++) {
803                         if (BIT(i) & rates) {
804                                 int rate = sband->bitrates[i].bitrate;
805                                 *pos++ = (u8) (rate / 5);
806                         }
807                 }
808         }
809
810         if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
811                 /* 1. power capabilities */
812                 pos = skb_put(skb, 4);
813                 *pos++ = WLAN_EID_PWR_CAPABILITY;
814                 *pos++ = 2;
815                 *pos++ = 0; /* min tx power */
816                 *pos++ = chan->max_power; /* max tx power */
817
818                 /* 2. supported channels */
819                 /* TODO: get this in reg domain format */
820                 pos = skb_put(skb, 2 * sband->n_channels + 2);
821                 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
822                 *pos++ = 2 * sband->n_channels;
823                 for (i = 0; i < sband->n_channels; i++) {
824                         *pos++ = ieee80211_frequency_to_channel(
825                                         sband->channels[i].center_freq);
826                         *pos++ = 1; /* one channel in the subband*/
827                 }
828         }
829
830         /* if present, add any custom IEs that go before HT */
831         if (assoc_data->ie_len && assoc_data->ie) {
832                 static const u8 before_ht[] = {
833                         WLAN_EID_SSID,
834                         WLAN_EID_SUPP_RATES,
835                         WLAN_EID_EXT_SUPP_RATES,
836                         WLAN_EID_PWR_CAPABILITY,
837                         WLAN_EID_SUPPORTED_CHANNELS,
838                         WLAN_EID_RSN,
839                         WLAN_EID_QOS_CAPA,
840                         WLAN_EID_RRM_ENABLED_CAPABILITIES,
841                         WLAN_EID_MOBILITY_DOMAIN,
842                         WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
843                 };
844                 noffset = ieee80211_ie_split(assoc_data->ie, assoc_data->ie_len,
845                                              before_ht, ARRAY_SIZE(before_ht),
846                                              offset);
847                 pos = skb_put(skb, noffset - offset);
848                 memcpy(pos, assoc_data->ie + offset, noffset - offset);
849                 offset = noffset;
850         }
851
852         if (WARN_ON_ONCE((ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
853                          !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)))
854                 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
855
856         if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
857                 ieee80211_add_ht_ie(sdata, skb, assoc_data->ap_ht_param,
858                                     sband, chan, sdata->smps_mode);
859
860         if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
861                 ieee80211_add_vht_ie(sdata, skb, sband,
862                                      &assoc_data->ap_vht_cap);
863
864         /* if present, add any custom non-vendor IEs that go after HT */
865         if (assoc_data->ie_len && assoc_data->ie) {
866                 noffset = ieee80211_ie_split_vendor(assoc_data->ie,
867                                                     assoc_data->ie_len,
868                                                     offset);
869                 pos = skb_put(skb, noffset - offset);
870                 memcpy(pos, assoc_data->ie + offset, noffset - offset);
871                 offset = noffset;
872         }
873
874         if (assoc_data->wmm) {
875                 if (assoc_data->uapsd) {
876                         qos_info = ifmgd->uapsd_queues;
877                         qos_info |= (ifmgd->uapsd_max_sp_len <<
878                                      IEEE80211_WMM_IE_STA_QOSINFO_SP_SHIFT);
879                 } else {
880                         qos_info = 0;
881                 }
882
883                 pos = skb_put(skb, 9);
884                 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
885                 *pos++ = 7; /* len */
886                 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
887                 *pos++ = 0x50;
888                 *pos++ = 0xf2;
889                 *pos++ = 2; /* WME */
890                 *pos++ = 0; /* WME info */
891                 *pos++ = 1; /* WME ver */
892                 *pos++ = qos_info;
893         }
894
895         /* add any remaining custom (i.e. vendor specific here) IEs */
896         if (assoc_data->ie_len && assoc_data->ie) {
897                 noffset = assoc_data->ie_len;
898                 pos = skb_put(skb, noffset - offset);
899                 memcpy(pos, assoc_data->ie + offset, noffset - offset);
900         }
901
902         drv_mgd_prepare_tx(local, sdata);
903
904         IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
905         if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
906                 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS |
907                                                 IEEE80211_TX_INTFL_MLME_CONN_TX;
908         ieee80211_tx_skb(sdata, skb);
909 }
910
911 void ieee80211_send_pspoll(struct ieee80211_local *local,
912                            struct ieee80211_sub_if_data *sdata)
913 {
914         struct ieee80211_pspoll *pspoll;
915         struct sk_buff *skb;
916
917         skb = ieee80211_pspoll_get(&local->hw, &sdata->vif);
918         if (!skb)
919                 return;
920
921         pspoll = (struct ieee80211_pspoll *) skb->data;
922         pspoll->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
923
924         IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
925         ieee80211_tx_skb(sdata, skb);
926 }
927
928 void ieee80211_send_nullfunc(struct ieee80211_local *local,
929                              struct ieee80211_sub_if_data *sdata,
930                              int powersave)
931 {
932         struct sk_buff *skb;
933         struct ieee80211_hdr_3addr *nullfunc;
934         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
935
936         skb = ieee80211_nullfunc_get(&local->hw, &sdata->vif);
937         if (!skb)
938                 return;
939
940         nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
941         if (powersave)
942                 nullfunc->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
943
944         IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
945                                         IEEE80211_TX_INTFL_OFFCHAN_TX_OK;
946         if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
947                             IEEE80211_STA_CONNECTION_POLL))
948                 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_USE_MINRATE;
949
950         ieee80211_tx_skb(sdata, skb);
951 }
952
953 static void ieee80211_send_4addr_nullfunc(struct ieee80211_local *local,
954                                           struct ieee80211_sub_if_data *sdata)
955 {
956         struct sk_buff *skb;
957         struct ieee80211_hdr *nullfunc;
958         __le16 fc;
959
960         if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
961                 return;
962
963         skb = dev_alloc_skb(local->hw.extra_tx_headroom + 30);
964         if (!skb)
965                 return;
966
967         skb_reserve(skb, local->hw.extra_tx_headroom);
968
969         nullfunc = (struct ieee80211_hdr *) skb_put(skb, 30);
970         memset(nullfunc, 0, 30);
971         fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
972                          IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
973         nullfunc->frame_control = fc;
974         memcpy(nullfunc->addr1, sdata->u.mgd.bssid, ETH_ALEN);
975         memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
976         memcpy(nullfunc->addr3, sdata->u.mgd.bssid, ETH_ALEN);
977         memcpy(nullfunc->addr4, sdata->vif.addr, ETH_ALEN);
978
979         IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
980         ieee80211_tx_skb(sdata, skb);
981 }
982
983 /* spectrum management related things */
984 static void ieee80211_chswitch_work(struct work_struct *work)
985 {
986         struct ieee80211_sub_if_data *sdata =
987                 container_of(work, struct ieee80211_sub_if_data, u.mgd.chswitch_work);
988         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
989
990         if (!ieee80211_sdata_running(sdata))
991                 return;
992
993         mutex_lock(&ifmgd->mtx);
994         if (!ifmgd->associated)
995                 goto out;
996
997         sdata->local->_oper_channel = sdata->local->csa_channel;
998         if (!sdata->local->ops->channel_switch) {
999                 /* call "hw_config" only if doing sw channel switch */
1000                 ieee80211_hw_config(sdata->local,
1001                         IEEE80211_CONF_CHANGE_CHANNEL);
1002         } else {
1003                 /* update the device channel directly */
1004                 sdata->local->hw.conf.channel = sdata->local->_oper_channel;
1005         }
1006
1007         /* XXX: shouldn't really modify cfg80211-owned data! */
1008         ifmgd->associated->channel = sdata->local->_oper_channel;
1009
1010         /* XXX: wait for a beacon first? */
1011         ieee80211_wake_queues_by_reason(&sdata->local->hw,
1012                                         IEEE80211_MAX_QUEUE_MAP,
1013                                         IEEE80211_QUEUE_STOP_REASON_CSA);
1014  out:
1015         ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
1016         mutex_unlock(&ifmgd->mtx);
1017 }
1018
1019 void ieee80211_chswitch_done(struct ieee80211_vif *vif, bool success)
1020 {
1021         struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1022         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1023
1024         trace_api_chswitch_done(sdata, success);
1025         if (!success) {
1026                 sdata_info(sdata,
1027                            "driver channel switch failed, disconnecting\n");
1028                 ieee80211_queue_work(&sdata->local->hw,
1029                                      &ifmgd->csa_connection_drop_work);
1030         } else {
1031                 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
1032         }
1033 }
1034 EXPORT_SYMBOL(ieee80211_chswitch_done);
1035
1036 static void ieee80211_chswitch_timer(unsigned long data)
1037 {
1038         struct ieee80211_sub_if_data *sdata =
1039                 (struct ieee80211_sub_if_data *) data;
1040
1041         ieee80211_queue_work(&sdata->local->hw, &sdata->u.mgd.chswitch_work);
1042 }
1043
1044 void
1045 ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
1046                                  const struct ieee80211_channel_sw_ie *sw_elem,
1047                                  struct ieee80211_bss *bss, u64 timestamp)
1048 {
1049         struct cfg80211_bss *cbss =
1050                 container_of((void *)bss, struct cfg80211_bss, priv);
1051         struct ieee80211_channel *new_ch;
1052         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1053         int new_freq = ieee80211_channel_to_frequency(sw_elem->new_ch_num,
1054                                                       cbss->channel->band);
1055         struct ieee80211_chanctx *chanctx;
1056
1057         ASSERT_MGD_MTX(ifmgd);
1058
1059         if (!ifmgd->associated)
1060                 return;
1061
1062         if (sdata->local->scanning)
1063                 return;
1064
1065         /* Disregard subsequent beacons if we are already running a timer
1066            processing a CSA */
1067
1068         if (ifmgd->flags & IEEE80211_STA_CSA_RECEIVED)
1069                 return;
1070
1071         new_ch = ieee80211_get_channel(sdata->local->hw.wiphy, new_freq);
1072         if (!new_ch || new_ch->flags & IEEE80211_CHAN_DISABLED) {
1073                 sdata_info(sdata,
1074                            "AP %pM switches to unsupported channel (%d MHz), disconnecting\n",
1075                            ifmgd->associated->bssid, new_freq);
1076                 ieee80211_queue_work(&sdata->local->hw,
1077                                      &ifmgd->csa_connection_drop_work);
1078                 return;
1079         }
1080
1081         ifmgd->flags |= IEEE80211_STA_CSA_RECEIVED;
1082
1083         if (sdata->local->use_chanctx) {
1084                 sdata_info(sdata,
1085                            "not handling channel switch with channel contexts\n");
1086                 ieee80211_queue_work(&sdata->local->hw,
1087                                      &ifmgd->csa_connection_drop_work);
1088                 return;
1089         }
1090
1091         mutex_lock(&sdata->local->chanctx_mtx);
1092         if (WARN_ON(!rcu_access_pointer(sdata->vif.chanctx_conf))) {
1093                 mutex_unlock(&sdata->local->chanctx_mtx);
1094                 return;
1095         }
1096         chanctx = container_of(rcu_access_pointer(sdata->vif.chanctx_conf),
1097                                struct ieee80211_chanctx, conf);
1098         if (chanctx->refcount > 1) {
1099                 sdata_info(sdata,
1100                            "channel switch with multiple interfaces on the same channel, disconnecting\n");
1101                 ieee80211_queue_work(&sdata->local->hw,
1102                                      &ifmgd->csa_connection_drop_work);
1103                 mutex_unlock(&sdata->local->chanctx_mtx);
1104                 return;
1105         }
1106         mutex_unlock(&sdata->local->chanctx_mtx);
1107
1108         sdata->local->csa_channel = new_ch;
1109
1110         if (sw_elem->mode)
1111                 ieee80211_stop_queues_by_reason(&sdata->local->hw,
1112                                 IEEE80211_MAX_QUEUE_MAP,
1113                                 IEEE80211_QUEUE_STOP_REASON_CSA);
1114
1115         if (sdata->local->ops->channel_switch) {
1116                 /* use driver's channel switch callback */
1117                 struct ieee80211_channel_switch ch_switch = {
1118                         .timestamp = timestamp,
1119                         .block_tx = sw_elem->mode,
1120                         .channel = new_ch,
1121                         .count = sw_elem->count,
1122                 };
1123
1124                 drv_channel_switch(sdata->local, &ch_switch);
1125                 return;
1126         }
1127
1128         /* channel switch handled in software */
1129         if (sw_elem->count <= 1)
1130                 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
1131         else
1132                 mod_timer(&ifmgd->chswitch_timer,
1133                           TU_TO_EXP_TIME(sw_elem->count *
1134                                          cbss->beacon_interval));
1135 }
1136
1137 static u32 ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data *sdata,
1138                                        struct ieee80211_channel *channel,
1139                                        const u8 *country_ie, u8 country_ie_len,
1140                                        const u8 *pwr_constr_elem)
1141 {
1142         struct ieee80211_country_ie_triplet *triplet;
1143         int chan = ieee80211_frequency_to_channel(channel->center_freq);
1144         int i, chan_pwr, chan_increment, new_ap_level;
1145         bool have_chan_pwr = false;
1146
1147         /* Invalid IE */
1148         if (country_ie_len % 2 || country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
1149                 return 0;
1150
1151         triplet = (void *)(country_ie + 3);
1152         country_ie_len -= 3;
1153
1154         switch (channel->band) {
1155         default:
1156                 WARN_ON_ONCE(1);
1157                 /* fall through */
1158         case IEEE80211_BAND_2GHZ:
1159         case IEEE80211_BAND_60GHZ:
1160                 chan_increment = 1;
1161                 break;
1162         case IEEE80211_BAND_5GHZ:
1163                 chan_increment = 4;
1164                 break;
1165         }
1166
1167         /* find channel */
1168         while (country_ie_len >= 3) {
1169                 u8 first_channel = triplet->chans.first_channel;
1170
1171                 if (first_channel >= IEEE80211_COUNTRY_EXTENSION_ID)
1172                         goto next;
1173
1174                 for (i = 0; i < triplet->chans.num_channels; i++) {
1175                         if (first_channel + i * chan_increment == chan) {
1176                                 have_chan_pwr = true;
1177                                 chan_pwr = triplet->chans.max_power;
1178                                 break;
1179                         }
1180                 }
1181                 if (have_chan_pwr)
1182                         break;
1183
1184  next:
1185                 triplet++;
1186                 country_ie_len -= 3;
1187         }
1188
1189         if (!have_chan_pwr)
1190                 return 0;
1191
1192         new_ap_level = max_t(int, 0, chan_pwr - *pwr_constr_elem);
1193
1194         if (sdata->ap_power_level == new_ap_level)
1195                 return 0;
1196
1197         sdata_info(sdata,
1198                    "Limiting TX power to %d (%d - %d) dBm as advertised by %pM\n",
1199                    new_ap_level, chan_pwr, *pwr_constr_elem,
1200                    sdata->u.mgd.bssid);
1201         sdata->ap_power_level = new_ap_level;
1202         if (__ieee80211_recalc_txpower(sdata))
1203                 return BSS_CHANGED_TXPOWER;
1204         return 0;
1205 }
1206
1207 /* powersave */
1208 static void ieee80211_enable_ps(struct ieee80211_local *local,
1209                                 struct ieee80211_sub_if_data *sdata)
1210 {
1211         struct ieee80211_conf *conf = &local->hw.conf;
1212
1213         /*
1214          * If we are scanning right now then the parameters will
1215          * take effect when scan finishes.
1216          */
1217         if (local->scanning)
1218                 return;
1219
1220         if (conf->dynamic_ps_timeout > 0 &&
1221             !(local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)) {
1222                 mod_timer(&local->dynamic_ps_timer, jiffies +
1223                           msecs_to_jiffies(conf->dynamic_ps_timeout));
1224         } else {
1225                 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
1226                         ieee80211_send_nullfunc(local, sdata, 1);
1227
1228                 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
1229                     (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS))
1230                         return;
1231
1232                 conf->flags |= IEEE80211_CONF_PS;
1233                 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1234         }
1235 }
1236
1237 static void ieee80211_change_ps(struct ieee80211_local *local)
1238 {
1239         struct ieee80211_conf *conf = &local->hw.conf;
1240
1241         if (local->ps_sdata) {
1242                 ieee80211_enable_ps(local, local->ps_sdata);
1243         } else if (conf->flags & IEEE80211_CONF_PS) {
1244                 conf->flags &= ~IEEE80211_CONF_PS;
1245                 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1246                 del_timer_sync(&local->dynamic_ps_timer);
1247                 cancel_work_sync(&local->dynamic_ps_enable_work);
1248         }
1249 }
1250
1251 static bool ieee80211_powersave_allowed(struct ieee80211_sub_if_data *sdata)
1252 {
1253         struct ieee80211_if_managed *mgd = &sdata->u.mgd;
1254         struct sta_info *sta = NULL;
1255         bool authorized = false;
1256
1257         if (!mgd->powersave)
1258                 return false;
1259
1260         if (mgd->broken_ap)
1261                 return false;
1262
1263         if (!mgd->associated)
1264                 return false;
1265
1266         if (mgd->flags & (IEEE80211_STA_BEACON_POLL |
1267                           IEEE80211_STA_CONNECTION_POLL))
1268                 return false;
1269
1270         rcu_read_lock();
1271         sta = sta_info_get(sdata, mgd->bssid);
1272         if (sta)
1273                 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
1274         rcu_read_unlock();
1275
1276         return authorized;
1277 }
1278
1279 /* need to hold RTNL or interface lock */
1280 void ieee80211_recalc_ps(struct ieee80211_local *local, s32 latency)
1281 {
1282         struct ieee80211_sub_if_data *sdata, *found = NULL;
1283         int count = 0;
1284         int timeout;
1285
1286         if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS)) {
1287                 local->ps_sdata = NULL;
1288                 return;
1289         }
1290
1291         list_for_each_entry(sdata, &local->interfaces, list) {
1292                 if (!ieee80211_sdata_running(sdata))
1293                         continue;
1294                 if (sdata->vif.type == NL80211_IFTYPE_AP) {
1295                         /* If an AP vif is found, then disable PS
1296                          * by setting the count to zero thereby setting
1297                          * ps_sdata to NULL.
1298                          */
1299                         count = 0;
1300                         break;
1301                 }
1302                 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1303                         continue;
1304                 found = sdata;
1305                 count++;
1306         }
1307
1308         if (count == 1 && ieee80211_powersave_allowed(found)) {
1309                 s32 beaconint_us;
1310
1311                 if (latency < 0)
1312                         latency = pm_qos_request(PM_QOS_NETWORK_LATENCY);
1313
1314                 beaconint_us = ieee80211_tu_to_usec(
1315                                         found->vif.bss_conf.beacon_int);
1316
1317                 timeout = local->dynamic_ps_forced_timeout;
1318                 if (timeout < 0) {
1319                         /*
1320                          * Go to full PSM if the user configures a very low
1321                          * latency requirement.
1322                          * The 2000 second value is there for compatibility
1323                          * until the PM_QOS_NETWORK_LATENCY is configured
1324                          * with real values.
1325                          */
1326                         if (latency > (1900 * USEC_PER_MSEC) &&
1327                             latency != (2000 * USEC_PER_SEC))
1328                                 timeout = 0;
1329                         else
1330                                 timeout = 100;
1331                 }
1332                 local->hw.conf.dynamic_ps_timeout = timeout;
1333
1334                 if (beaconint_us > latency) {
1335                         local->ps_sdata = NULL;
1336                 } else {
1337                         int maxslp = 1;
1338                         u8 dtimper = found->u.mgd.dtim_period;
1339
1340                         /* If the TIM IE is invalid, pretend the value is 1 */
1341                         if (!dtimper)
1342                                 dtimper = 1;
1343                         else if (dtimper > 1)
1344                                 maxslp = min_t(int, dtimper,
1345                                                     latency / beaconint_us);
1346
1347                         local->hw.conf.max_sleep_period = maxslp;
1348                         local->hw.conf.ps_dtim_period = dtimper;
1349                         local->ps_sdata = found;
1350                 }
1351         } else {
1352                 local->ps_sdata = NULL;
1353         }
1354
1355         ieee80211_change_ps(local);
1356 }
1357
1358 void ieee80211_recalc_ps_vif(struct ieee80211_sub_if_data *sdata)
1359 {
1360         bool ps_allowed = ieee80211_powersave_allowed(sdata);
1361
1362         if (sdata->vif.bss_conf.ps != ps_allowed) {
1363                 sdata->vif.bss_conf.ps = ps_allowed;
1364                 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_PS);
1365         }
1366 }
1367
1368 void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
1369 {
1370         struct ieee80211_local *local =
1371                 container_of(work, struct ieee80211_local,
1372                              dynamic_ps_disable_work);
1373
1374         if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1375                 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1376                 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1377         }
1378
1379         ieee80211_wake_queues_by_reason(&local->hw,
1380                                         IEEE80211_MAX_QUEUE_MAP,
1381                                         IEEE80211_QUEUE_STOP_REASON_PS);
1382 }
1383
1384 void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
1385 {
1386         struct ieee80211_local *local =
1387                 container_of(work, struct ieee80211_local,
1388                              dynamic_ps_enable_work);
1389         struct ieee80211_sub_if_data *sdata = local->ps_sdata;
1390         struct ieee80211_if_managed *ifmgd;
1391         unsigned long flags;
1392         int q;
1393
1394         /* can only happen when PS was just disabled anyway */
1395         if (!sdata)
1396                 return;
1397
1398         ifmgd = &sdata->u.mgd;
1399
1400         if (local->hw.conf.flags & IEEE80211_CONF_PS)
1401                 return;
1402
1403         if (local->hw.conf.dynamic_ps_timeout > 0) {
1404                 /* don't enter PS if TX frames are pending */
1405                 if (drv_tx_frames_pending(local)) {
1406                         mod_timer(&local->dynamic_ps_timer, jiffies +
1407                                   msecs_to_jiffies(
1408                                   local->hw.conf.dynamic_ps_timeout));
1409                         return;
1410                 }
1411
1412                 /*
1413                  * transmission can be stopped by others which leads to
1414                  * dynamic_ps_timer expiry. Postpone the ps timer if it
1415                  * is not the actual idle state.
1416                  */
1417                 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
1418                 for (q = 0; q < local->hw.queues; q++) {
1419                         if (local->queue_stop_reasons[q]) {
1420                                 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1421                                                        flags);
1422                                 mod_timer(&local->dynamic_ps_timer, jiffies +
1423                                           msecs_to_jiffies(
1424                                           local->hw.conf.dynamic_ps_timeout));
1425                                 return;
1426                         }
1427                 }
1428                 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
1429         }
1430
1431         if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
1432             !(ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
1433                 netif_tx_stop_all_queues(sdata->dev);
1434
1435                 if (drv_tx_frames_pending(local))
1436                         mod_timer(&local->dynamic_ps_timer, jiffies +
1437                                   msecs_to_jiffies(
1438                                   local->hw.conf.dynamic_ps_timeout));
1439                 else {
1440                         ieee80211_send_nullfunc(local, sdata, 1);
1441                         /* Flush to get the tx status of nullfunc frame */
1442                         ieee80211_flush_queues(local, sdata);
1443                 }
1444         }
1445
1446         if (!((local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) &&
1447               (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)) ||
1448             (ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
1449                 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
1450                 local->hw.conf.flags |= IEEE80211_CONF_PS;
1451                 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1452         }
1453
1454         if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
1455                 netif_tx_wake_all_queues(sdata->dev);
1456 }
1457
1458 void ieee80211_dynamic_ps_timer(unsigned long data)
1459 {
1460         struct ieee80211_local *local = (void *) data;
1461
1462         if (local->quiescing || local->suspended)
1463                 return;
1464
1465         ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
1466 }
1467
1468 void ieee80211_dfs_cac_timer_work(struct work_struct *work)
1469 {
1470         struct delayed_work *delayed_work =
1471                 container_of(work, struct delayed_work, work);
1472         struct ieee80211_sub_if_data *sdata =
1473                 container_of(delayed_work, struct ieee80211_sub_if_data,
1474                              dfs_cac_timer_work);
1475
1476         ieee80211_vif_release_channel(sdata);
1477
1478         cfg80211_cac_event(sdata->dev, NL80211_RADAR_CAC_FINISHED, GFP_KERNEL);
1479 }
1480
1481 /* MLME */
1482 static bool ieee80211_sta_wmm_params(struct ieee80211_local *local,
1483                                      struct ieee80211_sub_if_data *sdata,
1484                                      const u8 *wmm_param, size_t wmm_param_len)
1485 {
1486         struct ieee80211_tx_queue_params params;
1487         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1488         size_t left;
1489         int count;
1490         const u8 *pos;
1491         u8 uapsd_queues = 0;
1492
1493         if (!local->ops->conf_tx)
1494                 return false;
1495
1496         if (local->hw.queues < IEEE80211_NUM_ACS)
1497                 return false;
1498
1499         if (!wmm_param)
1500                 return false;
1501
1502         if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
1503                 return false;
1504
1505         if (ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED)
1506                 uapsd_queues = ifmgd->uapsd_queues;
1507
1508         count = wmm_param[6] & 0x0f;
1509         if (count == ifmgd->wmm_last_param_set)
1510                 return false;
1511         ifmgd->wmm_last_param_set = count;
1512
1513         pos = wmm_param + 8;
1514         left = wmm_param_len - 8;
1515
1516         memset(&params, 0, sizeof(params));
1517
1518         sdata->wmm_acm = 0;
1519         for (; left >= 4; left -= 4, pos += 4) {
1520                 int aci = (pos[0] >> 5) & 0x03;
1521                 int acm = (pos[0] >> 4) & 0x01;
1522                 bool uapsd = false;
1523                 int queue;
1524
1525                 switch (aci) {
1526                 case 1: /* AC_BK */
1527                         queue = 3;
1528                         if (acm)
1529                                 sdata->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
1530                         if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
1531                                 uapsd = true;
1532                         break;
1533                 case 2: /* AC_VI */
1534                         queue = 1;
1535                         if (acm)
1536                                 sdata->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
1537                         if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
1538                                 uapsd = true;
1539                         break;
1540                 case 3: /* AC_VO */
1541                         queue = 0;
1542                         if (acm)
1543                                 sdata->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
1544                         if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
1545                                 uapsd = true;
1546                         break;
1547                 case 0: /* AC_BE */
1548                 default:
1549                         queue = 2;
1550                         if (acm)
1551                                 sdata->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
1552                         if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
1553                                 uapsd = true;
1554                         break;
1555                 }
1556
1557                 params.aifs = pos[0] & 0x0f;
1558                 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
1559                 params.cw_min = ecw2cw(pos[1] & 0x0f);
1560                 params.txop = get_unaligned_le16(pos + 2);
1561                 params.uapsd = uapsd;
1562
1563                 mlme_dbg(sdata,
1564                          "WMM queue=%d aci=%d acm=%d aifs=%d cWmin=%d cWmax=%d txop=%d uapsd=%d\n",
1565                          queue, aci, acm,
1566                          params.aifs, params.cw_min, params.cw_max,
1567                          params.txop, params.uapsd);
1568                 sdata->tx_conf[queue] = params;
1569                 if (drv_conf_tx(local, sdata, queue, &params))
1570                         sdata_err(sdata,
1571                                   "failed to set TX queue parameters for queue %d\n",
1572                                   queue);
1573         }
1574
1575         /* enable WMM or activate new settings */
1576         sdata->vif.bss_conf.qos = true;
1577         return true;
1578 }
1579
1580 static void __ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
1581 {
1582         lockdep_assert_held(&sdata->local->mtx);
1583
1584         sdata->u.mgd.flags &= ~(IEEE80211_STA_CONNECTION_POLL |
1585                                 IEEE80211_STA_BEACON_POLL);
1586         ieee80211_run_deferred_scan(sdata->local);
1587 }
1588
1589 static void ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
1590 {
1591         mutex_lock(&sdata->local->mtx);
1592         __ieee80211_stop_poll(sdata);
1593         mutex_unlock(&sdata->local->mtx);
1594 }
1595
1596 static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
1597                                            u16 capab, bool erp_valid, u8 erp)
1598 {
1599         struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
1600         u32 changed = 0;
1601         bool use_protection;
1602         bool use_short_preamble;
1603         bool use_short_slot;
1604
1605         if (erp_valid) {
1606                 use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
1607                 use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
1608         } else {
1609                 use_protection = false;
1610                 use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
1611         }
1612
1613         use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
1614         if (ieee80211_get_sdata_band(sdata) == IEEE80211_BAND_5GHZ)
1615                 use_short_slot = true;
1616
1617         if (use_protection != bss_conf->use_cts_prot) {
1618                 bss_conf->use_cts_prot = use_protection;
1619                 changed |= BSS_CHANGED_ERP_CTS_PROT;
1620         }
1621
1622         if (use_short_preamble != bss_conf->use_short_preamble) {
1623                 bss_conf->use_short_preamble = use_short_preamble;
1624                 changed |= BSS_CHANGED_ERP_PREAMBLE;
1625         }
1626
1627         if (use_short_slot != bss_conf->use_short_slot) {
1628                 bss_conf->use_short_slot = use_short_slot;
1629                 changed |= BSS_CHANGED_ERP_SLOT;
1630         }
1631
1632         return changed;
1633 }
1634
1635 static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
1636                                      struct cfg80211_bss *cbss,
1637                                      u32 bss_info_changed)
1638 {
1639         struct ieee80211_bss *bss = (void *)cbss->priv;
1640         struct ieee80211_local *local = sdata->local;
1641         struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
1642
1643         bss_info_changed |= BSS_CHANGED_ASSOC;
1644         bss_info_changed |= ieee80211_handle_bss_capability(sdata,
1645                 bss_conf->assoc_capability, bss->has_erp_value, bss->erp_value);
1646
1647         sdata->u.mgd.beacon_timeout = usecs_to_jiffies(ieee80211_tu_to_usec(
1648                 IEEE80211_BEACON_LOSS_COUNT * bss_conf->beacon_int));
1649
1650         sdata->u.mgd.associated = cbss;
1651         memcpy(sdata->u.mgd.bssid, cbss->bssid, ETH_ALEN);
1652
1653         sdata->u.mgd.flags |= IEEE80211_STA_RESET_SIGNAL_AVE;
1654
1655         if (sdata->vif.p2p) {
1656                 const struct cfg80211_bss_ies *ies;
1657
1658                 rcu_read_lock();
1659                 ies = rcu_dereference(cbss->ies);
1660                 if (ies) {
1661                         u8 noa[2];
1662                         int ret;
1663
1664                         ret = cfg80211_get_p2p_attr(
1665                                         ies->data, ies->len,
1666                                         IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
1667                                         noa, sizeof(noa));
1668                         if (ret >= 2) {
1669                                 bss_conf->p2p_oppps = noa[1] & 0x80;
1670                                 bss_conf->p2p_ctwindow = noa[1] & 0x7f;
1671                                 bss_info_changed |= BSS_CHANGED_P2P_PS;
1672                                 sdata->u.mgd.p2p_noa_index = noa[0];
1673                         }
1674                 }
1675                 rcu_read_unlock();
1676         }
1677
1678         /* just to be sure */
1679         ieee80211_stop_poll(sdata);
1680
1681         ieee80211_led_assoc(local, 1);
1682
1683         if (sdata->u.mgd.assoc_data->have_beacon) {
1684                 /*
1685                  * If the AP is buggy we may get here with no DTIM period
1686                  * known, so assume it's 1 which is the only safe assumption
1687                  * in that case, although if the TIM IE is broken powersave
1688                  * probably just won't work at all.
1689                  */
1690                 bss_conf->dtim_period = sdata->u.mgd.dtim_period ?: 1;
1691                 bss_info_changed |= BSS_CHANGED_DTIM_PERIOD;
1692         } else {
1693                 bss_conf->dtim_period = 0;
1694         }
1695
1696         bss_conf->assoc = 1;
1697
1698         /* Tell the driver to monitor connection quality (if supported) */
1699         if (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI &&
1700             bss_conf->cqm_rssi_thold)
1701                 bss_info_changed |= BSS_CHANGED_CQM;
1702
1703         /* Enable ARP filtering */
1704         if (bss_conf->arp_addr_cnt)
1705                 bss_info_changed |= BSS_CHANGED_ARP_FILTER;
1706
1707         ieee80211_bss_info_change_notify(sdata, bss_info_changed);
1708
1709         mutex_lock(&local->iflist_mtx);
1710         ieee80211_recalc_ps(local, -1);
1711         mutex_unlock(&local->iflist_mtx);
1712
1713         ieee80211_recalc_smps(sdata);
1714         ieee80211_recalc_ps_vif(sdata);
1715
1716         netif_tx_start_all_queues(sdata->dev);
1717         netif_carrier_on(sdata->dev);
1718 }
1719
1720 static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
1721                                    u16 stype, u16 reason, bool tx,
1722                                    u8 *frame_buf)
1723 {
1724         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1725         struct ieee80211_local *local = sdata->local;
1726         u32 changed = 0;
1727
1728         ASSERT_MGD_MTX(ifmgd);
1729
1730         if (WARN_ON_ONCE(tx && !frame_buf))
1731                 return;
1732
1733         if (WARN_ON(!ifmgd->associated))
1734                 return;
1735
1736         ieee80211_stop_poll(sdata);
1737
1738         ifmgd->associated = NULL;
1739
1740         /*
1741          * we need to commit the associated = NULL change because the
1742          * scan code uses that to determine whether this iface should
1743          * go to/wake up from powersave or not -- and could otherwise
1744          * wake the queues erroneously.
1745          */
1746         smp_mb();
1747
1748         /*
1749          * Thus, we can only afterwards stop the queues -- to account
1750          * for the case where another CPU is finishing a scan at this
1751          * time -- we don't want the scan code to enable queues.
1752          */
1753
1754         netif_tx_stop_all_queues(sdata->dev);
1755         netif_carrier_off(sdata->dev);
1756
1757         /*
1758          * if we want to get out of ps before disassoc (why?) we have
1759          * to do it before sending disassoc, as otherwise the null-packet
1760          * won't be valid.
1761          */
1762         if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1763                 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1764                 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1765         }
1766         local->ps_sdata = NULL;
1767
1768         /* disable per-vif ps */
1769         ieee80211_recalc_ps_vif(sdata);
1770
1771         /* flush out any pending frame (e.g. DELBA) before deauth/disassoc */
1772         if (tx)
1773                 ieee80211_flush_queues(local, sdata);
1774
1775         /* deauthenticate/disassociate now */
1776         if (tx || frame_buf)
1777                 ieee80211_send_deauth_disassoc(sdata, ifmgd->bssid, stype,
1778                                                reason, tx, frame_buf);
1779
1780         /* flush out frame */
1781         if (tx)
1782                 ieee80211_flush_queues(local, sdata);
1783
1784         /* clear bssid only after building the needed mgmt frames */
1785         memset(ifmgd->bssid, 0, ETH_ALEN);
1786
1787         /* remove AP and TDLS peers */
1788         sta_info_flush_defer(sdata);
1789
1790         /* finally reset all BSS / config parameters */
1791         changed |= ieee80211_reset_erp_info(sdata);
1792
1793         ieee80211_led_assoc(local, 0);
1794         changed |= BSS_CHANGED_ASSOC;
1795         sdata->vif.bss_conf.assoc = false;
1796
1797         sdata->vif.bss_conf.p2p_ctwindow = 0;
1798         sdata->vif.bss_conf.p2p_oppps = false;
1799
1800         /* on the next assoc, re-program HT/VHT parameters */
1801         memset(&ifmgd->ht_capa, 0, sizeof(ifmgd->ht_capa));
1802         memset(&ifmgd->ht_capa_mask, 0, sizeof(ifmgd->ht_capa_mask));
1803         memset(&ifmgd->vht_capa, 0, sizeof(ifmgd->vht_capa));
1804         memset(&ifmgd->vht_capa_mask, 0, sizeof(ifmgd->vht_capa_mask));
1805
1806         sdata->ap_power_level = IEEE80211_UNSET_POWER_LEVEL;
1807
1808         del_timer_sync(&local->dynamic_ps_timer);
1809         cancel_work_sync(&local->dynamic_ps_enable_work);
1810
1811         /* Disable ARP filtering */
1812         if (sdata->vif.bss_conf.arp_addr_cnt)
1813                 changed |= BSS_CHANGED_ARP_FILTER;
1814
1815         sdata->vif.bss_conf.qos = false;
1816         changed |= BSS_CHANGED_QOS;
1817
1818         /* The BSSID (not really interesting) and HT changed */
1819         changed |= BSS_CHANGED_BSSID | BSS_CHANGED_HT;
1820         ieee80211_bss_info_change_notify(sdata, changed);
1821
1822         /* disassociated - set to defaults now */
1823         ieee80211_set_wmm_default(sdata, false);
1824
1825         del_timer_sync(&sdata->u.mgd.conn_mon_timer);
1826         del_timer_sync(&sdata->u.mgd.bcn_mon_timer);
1827         del_timer_sync(&sdata->u.mgd.timer);
1828         del_timer_sync(&sdata->u.mgd.chswitch_timer);
1829
1830         sdata->vif.bss_conf.dtim_period = 0;
1831
1832         ifmgd->flags = 0;
1833         ieee80211_vif_release_channel(sdata);
1834 }
1835
1836 void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
1837                              struct ieee80211_hdr *hdr)
1838 {
1839         /*
1840          * We can postpone the mgd.timer whenever receiving unicast frames
1841          * from AP because we know that the connection is working both ways
1842          * at that time. But multicast frames (and hence also beacons) must
1843          * be ignored here, because we need to trigger the timer during
1844          * data idle periods for sending the periodic probe request to the
1845          * AP we're connected to.
1846          */
1847         if (is_multicast_ether_addr(hdr->addr1))
1848                 return;
1849
1850         ieee80211_sta_reset_conn_monitor(sdata);
1851 }
1852
1853 static void ieee80211_reset_ap_probe(struct ieee80211_sub_if_data *sdata)
1854 {
1855         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1856         struct ieee80211_local *local = sdata->local;
1857
1858         mutex_lock(&local->mtx);
1859         if (!(ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
1860                               IEEE80211_STA_CONNECTION_POLL))) {
1861                 mutex_unlock(&local->mtx);
1862                 return;
1863         }
1864
1865         __ieee80211_stop_poll(sdata);
1866
1867         mutex_lock(&local->iflist_mtx);
1868         ieee80211_recalc_ps(local, -1);
1869         mutex_unlock(&local->iflist_mtx);
1870
1871         if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
1872                 goto out;
1873
1874         /*
1875          * We've received a probe response, but are not sure whether
1876          * we have or will be receiving any beacons or data, so let's
1877          * schedule the timers again, just in case.
1878          */
1879         ieee80211_sta_reset_beacon_monitor(sdata);
1880
1881         mod_timer(&ifmgd->conn_mon_timer,
1882                   round_jiffies_up(jiffies +
1883                                    IEEE80211_CONNECTION_IDLE_TIME));
1884 out:
1885         mutex_unlock(&local->mtx);
1886 }
1887
1888 void ieee80211_sta_tx_notify(struct ieee80211_sub_if_data *sdata,
1889                              struct ieee80211_hdr *hdr, bool ack)
1890 {
1891         if (!ieee80211_is_data(hdr->frame_control))
1892             return;
1893
1894         if (ieee80211_is_nullfunc(hdr->frame_control) &&
1895             sdata->u.mgd.probe_send_count > 0) {
1896                 if (ack)
1897                         ieee80211_sta_reset_conn_monitor(sdata);
1898                 else
1899                         sdata->u.mgd.nullfunc_failed = true;
1900                 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
1901                 return;
1902         }
1903
1904         if (ack)
1905                 ieee80211_sta_reset_conn_monitor(sdata);
1906 }
1907
1908 static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
1909 {
1910         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1911         const u8 *ssid;
1912         u8 *dst = ifmgd->associated->bssid;
1913         u8 unicast_limit = max(1, max_probe_tries - 3);
1914
1915         /*
1916          * Try sending broadcast probe requests for the last three
1917          * probe requests after the first ones failed since some
1918          * buggy APs only support broadcast probe requests.
1919          */
1920         if (ifmgd->probe_send_count >= unicast_limit)
1921                 dst = NULL;
1922
1923         /*
1924          * When the hardware reports an accurate Tx ACK status, it's
1925          * better to send a nullfunc frame instead of a probe request,
1926          * as it will kick us off the AP quickly if we aren't associated
1927          * anymore. The timeout will be reset if the frame is ACKed by
1928          * the AP.
1929          */
1930         ifmgd->probe_send_count++;
1931
1932         if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
1933                 ifmgd->nullfunc_failed = false;
1934                 ieee80211_send_nullfunc(sdata->local, sdata, 0);
1935         } else {
1936                 int ssid_len;
1937
1938                 rcu_read_lock();
1939                 ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
1940                 if (WARN_ON_ONCE(ssid == NULL))
1941                         ssid_len = 0;
1942                 else
1943                         ssid_len = ssid[1];
1944
1945                 ieee80211_send_probe_req(sdata, dst, ssid + 2, ssid_len, NULL,
1946                                          0, (u32) -1, true, 0,
1947                                          ifmgd->associated->channel, false);
1948                 rcu_read_unlock();
1949         }
1950
1951         ifmgd->probe_timeout = jiffies + msecs_to_jiffies(probe_wait_ms);
1952         run_again(ifmgd, ifmgd->probe_timeout);
1953         if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
1954                 ieee80211_flush_queues(sdata->local, sdata);
1955 }
1956
1957 static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
1958                                    bool beacon)
1959 {
1960         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1961         bool already = false;
1962
1963         if (!ieee80211_sdata_running(sdata))
1964                 return;
1965
1966         mutex_lock(&ifmgd->mtx);
1967
1968         if (!ifmgd->associated)
1969                 goto out;
1970
1971         mutex_lock(&sdata->local->mtx);
1972
1973         if (sdata->local->tmp_channel || sdata->local->scanning) {
1974                 mutex_unlock(&sdata->local->mtx);
1975                 goto out;
1976         }
1977
1978         if (beacon)
1979                 mlme_dbg_ratelimited(sdata,
1980                                      "detected beacon loss from AP - probing\n");
1981
1982         ieee80211_cqm_rssi_notify(&sdata->vif,
1983                 NL80211_CQM_RSSI_BEACON_LOSS_EVENT, GFP_KERNEL);
1984
1985         /*
1986          * The driver/our work has already reported this event or the
1987          * connection monitoring has kicked in and we have already sent
1988          * a probe request. Or maybe the AP died and the driver keeps
1989          * reporting until we disassociate...
1990          *
1991          * In either case we have to ignore the current call to this
1992          * function (except for setting the correct probe reason bit)
1993          * because otherwise we would reset the timer every time and
1994          * never check whether we received a probe response!
1995          */
1996         if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
1997                             IEEE80211_STA_CONNECTION_POLL))
1998                 already = true;
1999
2000         if (beacon)
2001                 ifmgd->flags |= IEEE80211_STA_BEACON_POLL;
2002         else
2003                 ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
2004
2005         mutex_unlock(&sdata->local->mtx);
2006
2007         if (already)
2008                 goto out;
2009
2010         mutex_lock(&sdata->local->iflist_mtx);
2011         ieee80211_recalc_ps(sdata->local, -1);
2012         mutex_unlock(&sdata->local->iflist_mtx);
2013
2014         ifmgd->probe_send_count = 0;
2015         ieee80211_mgd_probe_ap_send(sdata);
2016  out:
2017         mutex_unlock(&ifmgd->mtx);
2018 }
2019
2020 struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw,
2021                                           struct ieee80211_vif *vif)
2022 {
2023         struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2024         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2025         struct cfg80211_bss *cbss;
2026         struct sk_buff *skb;
2027         const u8 *ssid;
2028         int ssid_len;
2029
2030         if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
2031                 return NULL;
2032
2033         ASSERT_MGD_MTX(ifmgd);
2034
2035         if (ifmgd->associated)
2036                 cbss = ifmgd->associated;
2037         else if (ifmgd->auth_data)
2038                 cbss = ifmgd->auth_data->bss;
2039         else if (ifmgd->assoc_data)
2040                 cbss = ifmgd->assoc_data->bss;
2041         else
2042                 return NULL;
2043
2044         rcu_read_lock();
2045         ssid = ieee80211_bss_get_ie(cbss, WLAN_EID_SSID);
2046         if (WARN_ON_ONCE(ssid == NULL))
2047                 ssid_len = 0;
2048         else
2049                 ssid_len = ssid[1];
2050
2051         skb = ieee80211_build_probe_req(sdata, cbss->bssid,
2052                                         (u32) -1, cbss->channel,
2053                                         ssid + 2, ssid_len,
2054                                         NULL, 0, true);
2055         rcu_read_unlock();
2056
2057         return skb;
2058 }
2059 EXPORT_SYMBOL(ieee80211_ap_probereq_get);
2060
2061 static void __ieee80211_disconnect(struct ieee80211_sub_if_data *sdata)
2062 {
2063         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2064         u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
2065
2066         mutex_lock(&ifmgd->mtx);
2067         if (!ifmgd->associated) {
2068                 mutex_unlock(&ifmgd->mtx);
2069                 return;
2070         }
2071
2072         ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
2073                                WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
2074                                true, frame_buf);
2075         ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
2076         ieee80211_wake_queues_by_reason(&sdata->local->hw,
2077                                         IEEE80211_MAX_QUEUE_MAP,
2078                                         IEEE80211_QUEUE_STOP_REASON_CSA);
2079         mutex_unlock(&ifmgd->mtx);
2080
2081         /*
2082          * must be outside lock due to cfg80211,
2083          * but that's not a problem.
2084          */
2085         cfg80211_send_deauth(sdata->dev, frame_buf, IEEE80211_DEAUTH_FRAME_LEN);
2086 }
2087
2088 static void ieee80211_beacon_connection_loss_work(struct work_struct *work)
2089 {
2090         struct ieee80211_sub_if_data *sdata =
2091                 container_of(work, struct ieee80211_sub_if_data,
2092                              u.mgd.beacon_connection_loss_work);
2093         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2094         struct sta_info *sta;
2095
2096         if (ifmgd->associated) {
2097                 rcu_read_lock();
2098                 sta = sta_info_get(sdata, ifmgd->bssid);
2099                 if (sta)
2100                         sta->beacon_loss_count++;
2101                 rcu_read_unlock();
2102         }
2103
2104         if (ifmgd->connection_loss) {
2105                 sdata_info(sdata, "Connection to AP %pM lost\n",
2106                            ifmgd->bssid);
2107                 __ieee80211_disconnect(sdata);
2108         } else {
2109                 ieee80211_mgd_probe_ap(sdata, true);
2110         }
2111 }
2112
2113 static void ieee80211_csa_connection_drop_work(struct work_struct *work)
2114 {
2115         struct ieee80211_sub_if_data *sdata =
2116                 container_of(work, struct ieee80211_sub_if_data,
2117                              u.mgd.csa_connection_drop_work);
2118
2119         __ieee80211_disconnect(sdata);
2120 }
2121
2122 void ieee80211_beacon_loss(struct ieee80211_vif *vif)
2123 {
2124         struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2125         struct ieee80211_hw *hw = &sdata->local->hw;
2126
2127         trace_api_beacon_loss(sdata);
2128
2129         WARN_ON(hw->flags & IEEE80211_HW_CONNECTION_MONITOR);
2130         sdata->u.mgd.connection_loss = false;
2131         ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
2132 }
2133 EXPORT_SYMBOL(ieee80211_beacon_loss);
2134
2135 void ieee80211_connection_loss(struct ieee80211_vif *vif)
2136 {
2137         struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2138         struct ieee80211_hw *hw = &sdata->local->hw;
2139
2140         trace_api_connection_loss(sdata);
2141
2142         sdata->u.mgd.connection_loss = true;
2143         ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
2144 }
2145 EXPORT_SYMBOL(ieee80211_connection_loss);
2146
2147
2148 static void ieee80211_destroy_auth_data(struct ieee80211_sub_if_data *sdata,
2149                                         bool assoc)
2150 {
2151         struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
2152
2153         lockdep_assert_held(&sdata->u.mgd.mtx);
2154
2155         if (!assoc) {
2156                 sta_info_destroy_addr(sdata, auth_data->bss->bssid);
2157
2158                 memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
2159                 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
2160                 sdata->u.mgd.flags = 0;
2161                 ieee80211_vif_release_channel(sdata);
2162         }
2163
2164         cfg80211_put_bss(sdata->local->hw.wiphy, auth_data->bss);
2165         kfree(auth_data);
2166         sdata->u.mgd.auth_data = NULL;
2167 }
2168
2169 static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
2170                                      struct ieee80211_mgmt *mgmt, size_t len)
2171 {
2172         struct ieee80211_local *local = sdata->local;
2173         struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
2174         u8 *pos;
2175         struct ieee802_11_elems elems;
2176         u32 tx_flags = 0;
2177
2178         pos = mgmt->u.auth.variable;
2179         ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
2180         if (!elems.challenge)
2181                 return;
2182         auth_data->expected_transaction = 4;
2183         drv_mgd_prepare_tx(sdata->local, sdata);
2184         if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
2185                 tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
2186                            IEEE80211_TX_INTFL_MLME_CONN_TX;
2187         ieee80211_send_auth(sdata, 3, auth_data->algorithm, 0,
2188                             elems.challenge - 2, elems.challenge_len + 2,
2189                             auth_data->bss->bssid, auth_data->bss->bssid,
2190                             auth_data->key, auth_data->key_len,
2191                             auth_data->key_idx, tx_flags);
2192 }
2193
2194 static enum rx_mgmt_action __must_check
2195 ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
2196                        struct ieee80211_mgmt *mgmt, size_t len)
2197 {
2198         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2199         u8 bssid[ETH_ALEN];
2200         u16 auth_alg, auth_transaction, status_code;
2201         struct sta_info *sta;
2202
2203         lockdep_assert_held(&ifmgd->mtx);
2204
2205         if (len < 24 + 6)
2206                 return RX_MGMT_NONE;
2207
2208         if (!ifmgd->auth_data || ifmgd->auth_data->done)
2209                 return RX_MGMT_NONE;
2210
2211         memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
2212
2213         if (!ether_addr_equal(bssid, mgmt->bssid))
2214                 return RX_MGMT_NONE;
2215
2216         auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
2217         auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
2218         status_code = le16_to_cpu(mgmt->u.auth.status_code);
2219
2220         if (auth_alg != ifmgd->auth_data->algorithm ||
2221             auth_transaction != ifmgd->auth_data->expected_transaction) {
2222                 sdata_info(sdata, "%pM unexpected authentication state: alg %d (expected %d) transact %d (expected %d)\n",
2223                            mgmt->sa, auth_alg, ifmgd->auth_data->algorithm,
2224                            auth_transaction,
2225                            ifmgd->auth_data->expected_transaction);
2226                 return RX_MGMT_NONE;
2227         }
2228
2229         if (status_code != WLAN_STATUS_SUCCESS) {
2230                 sdata_info(sdata, "%pM denied authentication (status %d)\n",
2231                            mgmt->sa, status_code);
2232                 ieee80211_destroy_auth_data(sdata, false);
2233                 return RX_MGMT_CFG80211_RX_AUTH;
2234         }
2235
2236         switch (ifmgd->auth_data->algorithm) {
2237         case WLAN_AUTH_OPEN:
2238         case WLAN_AUTH_LEAP:
2239         case WLAN_AUTH_FT:
2240         case WLAN_AUTH_SAE:
2241                 break;
2242         case WLAN_AUTH_SHARED_KEY:
2243                 if (ifmgd->auth_data->expected_transaction != 4) {
2244                         ieee80211_auth_challenge(sdata, mgmt, len);
2245                         /* need another frame */
2246                         return RX_MGMT_NONE;
2247                 }
2248                 break;
2249         default:
2250                 WARN_ONCE(1, "invalid auth alg %d",
2251                           ifmgd->auth_data->algorithm);
2252                 return RX_MGMT_NONE;
2253         }
2254
2255         sdata_info(sdata, "authenticated\n");
2256         ifmgd->auth_data->done = true;
2257         ifmgd->auth_data->timeout = jiffies + IEEE80211_AUTH_WAIT_ASSOC;
2258         ifmgd->auth_data->timeout_started = true;
2259         run_again(ifmgd, ifmgd->auth_data->timeout);
2260
2261         if (ifmgd->auth_data->algorithm == WLAN_AUTH_SAE &&
2262             ifmgd->auth_data->expected_transaction != 2) {
2263                 /*
2264                  * Report auth frame to user space for processing since another
2265                  * round of Authentication frames is still needed.
2266                  */
2267                 return RX_MGMT_CFG80211_RX_AUTH;
2268         }
2269
2270         /* move station state to auth */
2271         mutex_lock(&sdata->local->sta_mtx);
2272         sta = sta_info_get(sdata, bssid);
2273         if (!sta) {
2274                 WARN_ONCE(1, "%s: STA %pM not found", sdata->name, bssid);
2275                 goto out_err;
2276         }
2277         if (sta_info_move_state(sta, IEEE80211_STA_AUTH)) {
2278                 sdata_info(sdata, "failed moving %pM to auth\n", bssid);
2279                 goto out_err;
2280         }
2281         mutex_unlock(&sdata->local->sta_mtx);
2282
2283         return RX_MGMT_CFG80211_RX_AUTH;
2284  out_err:
2285         mutex_unlock(&sdata->local->sta_mtx);
2286         /* ignore frame -- wait for timeout */
2287         return RX_MGMT_NONE;
2288 }
2289
2290
2291 static enum rx_mgmt_action __must_check
2292 ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
2293                          struct ieee80211_mgmt *mgmt, size_t len)
2294 {
2295         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2296         const u8 *bssid = NULL;
2297         u16 reason_code;
2298
2299         lockdep_assert_held(&ifmgd->mtx);
2300
2301         if (len < 24 + 2)
2302                 return RX_MGMT_NONE;
2303
2304         if (!ifmgd->associated ||
2305             !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
2306                 return RX_MGMT_NONE;
2307
2308         bssid = ifmgd->associated->bssid;
2309
2310         reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
2311
2312         sdata_info(sdata, "deauthenticated from %pM (Reason: %u)\n",
2313                    bssid, reason_code);
2314
2315         ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
2316
2317         return RX_MGMT_CFG80211_DEAUTH;
2318 }
2319
2320
2321 static enum rx_mgmt_action __must_check
2322 ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
2323                            struct ieee80211_mgmt *mgmt, size_t len)
2324 {
2325         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2326         u16 reason_code;
2327
2328         lockdep_assert_held(&ifmgd->mtx);
2329
2330         if (len < 24 + 2)
2331                 return RX_MGMT_NONE;
2332
2333         if (!ifmgd->associated ||
2334             !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
2335                 return RX_MGMT_NONE;
2336
2337         reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
2338
2339         sdata_info(sdata, "disassociated from %pM (Reason: %u)\n",
2340                    mgmt->sa, reason_code);
2341
2342         ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
2343
2344         return RX_MGMT_CFG80211_DISASSOC;
2345 }
2346
2347 static void ieee80211_get_rates(struct ieee80211_supported_band *sband,
2348                                 u8 *supp_rates, unsigned int supp_rates_len,
2349                                 u32 *rates, u32 *basic_rates,
2350                                 bool *have_higher_than_11mbit,
2351                                 int *min_rate, int *min_rate_index)
2352 {
2353         int i, j;
2354
2355         for (i = 0; i < supp_rates_len; i++) {
2356                 int rate = (supp_rates[i] & 0x7f) * 5;
2357                 bool is_basic = !!(supp_rates[i] & 0x80);
2358
2359                 if (rate > 110)
2360                         *have_higher_than_11mbit = true;
2361
2362                 /*
2363                  * BSS_MEMBERSHIP_SELECTOR_HT_PHY is defined in 802.11n-2009
2364                  * 7.3.2.2 as a magic value instead of a rate. Hence, skip it.
2365                  *
2366                  * Note: Even through the membership selector and the basic
2367                  *       rate flag share the same bit, they are not exactly
2368                  *       the same.
2369                  */
2370                 if (!!(supp_rates[i] & 0x80) &&
2371                     (supp_rates[i] & 0x7f) == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
2372                         continue;
2373
2374                 for (j = 0; j < sband->n_bitrates; j++) {
2375                         if (sband->bitrates[j].bitrate == rate) {
2376                                 *rates |= BIT(j);
2377                                 if (is_basic)
2378                                         *basic_rates |= BIT(j);
2379                                 if (rate < *min_rate) {
2380                                         *min_rate = rate;
2381                                         *min_rate_index = j;
2382                                 }
2383                                 break;
2384                         }
2385                 }
2386         }
2387 }
2388
2389 static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata,
2390                                          bool assoc)
2391 {
2392         struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
2393
2394         lockdep_assert_held(&sdata->u.mgd.mtx);
2395
2396         if (!assoc) {
2397                 sta_info_destroy_addr(sdata, assoc_data->bss->bssid);
2398
2399                 memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
2400                 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
2401                 sdata->u.mgd.flags = 0;
2402                 ieee80211_vif_release_channel(sdata);
2403         }
2404
2405         kfree(assoc_data);
2406         sdata->u.mgd.assoc_data = NULL;
2407 }
2408
2409 static bool ieee80211_assoc_success(struct ieee80211_sub_if_data *sdata,
2410                                     struct cfg80211_bss *cbss,
2411                                     struct ieee80211_mgmt *mgmt, size_t len)
2412 {
2413         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2414         struct ieee80211_local *local = sdata->local;
2415         struct ieee80211_supported_band *sband;
2416         struct sta_info *sta;
2417         u8 *pos;
2418         u16 capab_info, aid;
2419         struct ieee802_11_elems elems;
2420         struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
2421         u32 changed = 0;
2422         int err;
2423
2424         /* AssocResp and ReassocResp have identical structure */
2425
2426         aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
2427         capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
2428
2429         if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
2430                 sdata_info(sdata, "invalid AID value 0x%x; bits 15:14 not set\n",
2431                            aid);
2432         aid &= ~(BIT(15) | BIT(14));
2433
2434         ifmgd->broken_ap = false;
2435
2436         if (aid == 0 || aid > IEEE80211_MAX_AID) {
2437                 sdata_info(sdata, "invalid AID value %d (out of range), turn off PS\n",
2438                            aid);
2439                 aid = 0;
2440                 ifmgd->broken_ap = true;
2441         }
2442
2443         pos = mgmt->u.assoc_resp.variable;
2444         ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
2445
2446         if (!elems.supp_rates) {
2447                 sdata_info(sdata, "no SuppRates element in AssocResp\n");
2448                 return false;
2449         }
2450
2451         ifmgd->aid = aid;
2452
2453         /*
2454          * We previously checked these in the beacon/probe response, so
2455          * they should be present here. This is just a safety net.
2456          */
2457         if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
2458             (!elems.wmm_param || !elems.ht_cap_elem || !elems.ht_operation)) {
2459                 sdata_info(sdata,
2460                            "HT AP is missing WMM params or HT capability/operation in AssocResp\n");
2461                 return false;
2462         }
2463
2464         if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
2465             (!elems.vht_cap_elem || !elems.vht_operation)) {
2466                 sdata_info(sdata,
2467                            "VHT AP is missing VHT capability/operation in AssocResp\n");
2468                 return false;
2469         }
2470
2471         mutex_lock(&sdata->local->sta_mtx);
2472         /*
2473          * station info was already allocated and inserted before
2474          * the association and should be available to us
2475          */
2476         sta = sta_info_get(sdata, cbss->bssid);
2477         if (WARN_ON(!sta)) {
2478                 mutex_unlock(&sdata->local->sta_mtx);
2479                 return false;
2480         }
2481
2482         sband = local->hw.wiphy->bands[ieee80211_get_sdata_band(sdata)];
2483
2484         /* Set up internal HT/VHT capabilities */
2485         if (elems.ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
2486                 ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
2487                                                   elems.ht_cap_elem, sta);
2488
2489         if (elems.vht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
2490                 ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband,
2491                                                     elems.vht_cap_elem, sta);
2492
2493         /*
2494          * Some APs, e.g. Netgear WNDR3700, report invalid HT operation data
2495          * in their association response, so ignore that data for our own
2496          * configuration. If it changed since the last beacon, we'll get the
2497          * next beacon and update then.
2498          */
2499
2500         /*
2501          * If an operating mode notification IE is present, override the
2502          * NSS calculation (that would be done in rate_control_rate_init())
2503          * and use the # of streams from that element.
2504          */
2505         if (elems.opmode_notif &&
2506             !(*elems.opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_TYPE_BF)) {
2507                 u8 nss;
2508
2509                 nss = *elems.opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_MASK;
2510                 nss >>= IEEE80211_OPMODE_NOTIF_RX_NSS_SHIFT;
2511                 nss += 1;
2512                 sta->sta.rx_nss = nss;
2513         }
2514
2515         rate_control_rate_init(sta);
2516
2517         if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED)
2518                 set_sta_flag(sta, WLAN_STA_MFP);
2519
2520         if (elems.wmm_param)
2521                 set_sta_flag(sta, WLAN_STA_WME);
2522
2523         err = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
2524         if (!err && !(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
2525                 err = sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
2526         if (err) {
2527                 sdata_info(sdata,
2528                            "failed to move station %pM to desired state\n",
2529                            sta->sta.addr);
2530                 WARN_ON(__sta_info_destroy(sta));
2531                 mutex_unlock(&sdata->local->sta_mtx);
2532                 return false;
2533         }
2534
2535         mutex_unlock(&sdata->local->sta_mtx);
2536
2537         /*
2538          * Always handle WMM once after association regardless
2539          * of the first value the AP uses. Setting -1 here has
2540          * that effect because the AP values is an unsigned
2541          * 4-bit value.
2542          */
2543         ifmgd->wmm_last_param_set = -1;
2544
2545         if (elems.wmm_param)
2546                 ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
2547                                          elems.wmm_param_len);
2548         else
2549                 ieee80211_set_wmm_default(sdata, false);
2550         changed |= BSS_CHANGED_QOS;
2551
2552         /* set AID and assoc capability,
2553          * ieee80211_set_associated() will tell the driver */
2554         bss_conf->aid = aid;
2555         bss_conf->assoc_capability = capab_info;
2556         ieee80211_set_associated(sdata, cbss, changed);
2557
2558         /*
2559          * If we're using 4-addr mode, let the AP know that we're
2560          * doing so, so that it can create the STA VLAN on its side
2561          */
2562         if (ifmgd->use_4addr)
2563                 ieee80211_send_4addr_nullfunc(local, sdata);
2564
2565         /*
2566          * Start timer to probe the connection to the AP now.
2567          * Also start the timer that will detect beacon loss.
2568          */
2569         ieee80211_sta_rx_notify(sdata, (struct ieee80211_hdr *)mgmt);
2570         ieee80211_sta_reset_beacon_monitor(sdata);
2571
2572         return true;
2573 }
2574
2575 static enum rx_mgmt_action __must_check
2576 ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
2577                              struct ieee80211_mgmt *mgmt, size_t len,
2578                              struct cfg80211_bss **bss)
2579 {
2580         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2581         struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
2582         u16 capab_info, status_code, aid;
2583         struct ieee802_11_elems elems;
2584         u8 *pos;
2585         bool reassoc;
2586
2587         lockdep_assert_held(&ifmgd->mtx);
2588
2589         if (!assoc_data)
2590                 return RX_MGMT_NONE;
2591         if (!ether_addr_equal(assoc_data->bss->bssid, mgmt->bssid))
2592                 return RX_MGMT_NONE;
2593
2594         /*
2595          * AssocResp and ReassocResp have identical structure, so process both
2596          * of them in this function.
2597          */
2598
2599         if (len < 24 + 6)
2600                 return RX_MGMT_NONE;
2601
2602         reassoc = ieee80211_is_reassoc_req(mgmt->frame_control);
2603         capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
2604         status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
2605         aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
2606
2607         sdata_info(sdata,
2608                    "RX %sssocResp from %pM (capab=0x%x status=%d aid=%d)\n",
2609                    reassoc ? "Rea" : "A", mgmt->sa,
2610                    capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
2611
2612         pos = mgmt->u.assoc_resp.variable;
2613         ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
2614
2615         if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
2616             elems.timeout_int && elems.timeout_int_len == 5 &&
2617             elems.timeout_int[0] == WLAN_TIMEOUT_ASSOC_COMEBACK) {
2618                 u32 tu, ms;
2619                 tu = get_unaligned_le32(elems.timeout_int + 1);
2620                 ms = tu * 1024 / 1000;
2621                 sdata_info(sdata,
2622                            "%pM rejected association temporarily; comeback duration %u TU (%u ms)\n",
2623                            mgmt->sa, tu, ms);
2624                 assoc_data->timeout = jiffies + msecs_to_jiffies(ms);
2625                 assoc_data->timeout_started = true;
2626                 if (ms > IEEE80211_ASSOC_TIMEOUT)
2627                         run_again(ifmgd, assoc_data->timeout);
2628                 return RX_MGMT_NONE;
2629         }
2630
2631         *bss = assoc_data->bss;
2632
2633         if (status_code != WLAN_STATUS_SUCCESS) {
2634                 sdata_info(sdata, "%pM denied association (code=%d)\n",
2635                            mgmt->sa, status_code);
2636                 ieee80211_destroy_assoc_data(sdata, false);
2637         } else {
2638                 if (!ieee80211_assoc_success(sdata, *bss, mgmt, len)) {
2639                         /* oops -- internal error -- send timeout for now */
2640                         ieee80211_destroy_assoc_data(sdata, false);
2641                         cfg80211_put_bss(sdata->local->hw.wiphy, *bss);
2642                         return RX_MGMT_CFG80211_ASSOC_TIMEOUT;
2643                 }
2644                 sdata_info(sdata, "associated\n");
2645
2646                 /*
2647                  * destroy assoc_data afterwards, as otherwise an idle
2648                  * recalc after assoc_data is NULL but before associated
2649                  * is set can cause the interface to go idle
2650                  */
2651                 ieee80211_destroy_assoc_data(sdata, true);
2652         }
2653
2654         return RX_MGMT_CFG80211_RX_ASSOC;
2655 }
2656
2657 static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
2658                                   struct ieee80211_mgmt *mgmt, size_t len,
2659                                   struct ieee80211_rx_status *rx_status,
2660                                   struct ieee802_11_elems *elems)
2661 {
2662         struct ieee80211_local *local = sdata->local;
2663         int freq;
2664         struct ieee80211_bss *bss;
2665         struct ieee80211_channel *channel;
2666         bool need_ps = false;
2667
2668         if ((sdata->u.mgd.associated &&
2669              ether_addr_equal(mgmt->bssid, sdata->u.mgd.associated->bssid)) ||
2670             (sdata->u.mgd.assoc_data &&
2671              ether_addr_equal(mgmt->bssid,
2672                               sdata->u.mgd.assoc_data->bss->bssid))) {
2673                 /* not previously set so we may need to recalc */
2674                 need_ps = sdata->u.mgd.associated && !sdata->u.mgd.dtim_period;
2675
2676                 if (elems->tim && !elems->parse_error) {
2677                         const struct ieee80211_tim_ie *tim_ie = elems->tim;
2678                         sdata->u.mgd.dtim_period = tim_ie->dtim_period;
2679                 }
2680         }
2681
2682         if (elems->ds_params && elems->ds_params_len == 1)
2683                 freq = ieee80211_channel_to_frequency(elems->ds_params[0],
2684                                                       rx_status->band);
2685         else
2686                 freq = rx_status->freq;
2687
2688         channel = ieee80211_get_channel(local->hw.wiphy, freq);
2689
2690         if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
2691                 return;
2692
2693         bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
2694                                         channel);
2695         if (bss)
2696                 ieee80211_rx_bss_put(local, bss);
2697
2698         if (!sdata->u.mgd.associated)
2699                 return;
2700
2701         if (need_ps) {
2702                 mutex_lock(&local->iflist_mtx);
2703                 ieee80211_recalc_ps(local, -1);
2704                 mutex_unlock(&local->iflist_mtx);
2705         }
2706
2707         if (elems->ch_switch_ie &&
2708             memcmp(mgmt->bssid, sdata->u.mgd.associated->bssid, ETH_ALEN) == 0)
2709                 ieee80211_sta_process_chanswitch(sdata, elems->ch_switch_ie,
2710                                                  bss, rx_status->mactime);
2711 }
2712
2713
2714 static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
2715                                          struct sk_buff *skb)
2716 {
2717         struct ieee80211_mgmt *mgmt = (void *)skb->data;
2718         struct ieee80211_if_managed *ifmgd;
2719         struct ieee80211_rx_status *rx_status = (void *) skb->cb;
2720         size_t baselen, len = skb->len;
2721         struct ieee802_11_elems elems;
2722
2723         ifmgd = &sdata->u.mgd;
2724
2725         ASSERT_MGD_MTX(ifmgd);
2726
2727         if (!ether_addr_equal(mgmt->da, sdata->vif.addr))
2728                 return; /* ignore ProbeResp to foreign address */
2729
2730         baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
2731         if (baselen > len)
2732                 return;
2733
2734         ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
2735                                 &elems);
2736
2737         ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
2738
2739         if (ifmgd->associated &&
2740             ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
2741                 ieee80211_reset_ap_probe(sdata);
2742
2743         if (ifmgd->auth_data && !ifmgd->auth_data->bss->proberesp_ies &&
2744             ether_addr_equal(mgmt->bssid, ifmgd->auth_data->bss->bssid)) {
2745                 /* got probe response, continue with auth */
2746                 sdata_info(sdata, "direct probe responded\n");
2747                 ifmgd->auth_data->tries = 0;
2748                 ifmgd->auth_data->timeout = jiffies;
2749                 ifmgd->auth_data->timeout_started = true;
2750                 run_again(ifmgd, ifmgd->auth_data->timeout);
2751         }
2752 }
2753
2754 /*
2755  * This is the canonical list of information elements we care about,
2756  * the filter code also gives us all changes to the Microsoft OUI
2757  * (00:50:F2) vendor IE which is used for WMM which we need to track.
2758  *
2759  * We implement beacon filtering in software since that means we can
2760  * avoid processing the frame here and in cfg80211, and userspace
2761  * will not be able to tell whether the hardware supports it or not.
2762  *
2763  * XXX: This list needs to be dynamic -- userspace needs to be able to
2764  *      add items it requires. It also needs to be able to tell us to
2765  *      look out for other vendor IEs.
2766  */
2767 static const u64 care_about_ies =
2768         (1ULL << WLAN_EID_COUNTRY) |
2769         (1ULL << WLAN_EID_ERP_INFO) |
2770         (1ULL << WLAN_EID_CHANNEL_SWITCH) |
2771         (1ULL << WLAN_EID_PWR_CONSTRAINT) |
2772         (1ULL << WLAN_EID_HT_CAPABILITY) |
2773         (1ULL << WLAN_EID_HT_OPERATION);
2774
2775 static enum rx_mgmt_action
2776 ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
2777                          struct ieee80211_mgmt *mgmt, size_t len,
2778                          u8 *deauth_buf, struct ieee80211_rx_status *rx_status)
2779 {
2780         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2781         struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
2782         size_t baselen;
2783         struct ieee802_11_elems elems;
2784         struct ieee80211_local *local = sdata->local;
2785         struct ieee80211_chanctx_conf *chanctx_conf;
2786         struct ieee80211_channel *chan;
2787         struct sta_info *sta;
2788         u32 changed = 0;
2789         bool erp_valid;
2790         u8 erp_value = 0;
2791         u32 ncrc;
2792         u8 *bssid;
2793
2794         lockdep_assert_held(&ifmgd->mtx);
2795
2796         /* Process beacon from the current BSS */
2797         baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2798         if (baselen > len)
2799                 return RX_MGMT_NONE;
2800
2801         rcu_read_lock();
2802         chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2803         if (!chanctx_conf) {
2804                 rcu_read_unlock();
2805                 return RX_MGMT_NONE;
2806         }
2807
2808         if (rx_status->freq != chanctx_conf->def.chan->center_freq) {
2809                 rcu_read_unlock();
2810                 return RX_MGMT_NONE;
2811         }
2812         chan = chanctx_conf->def.chan;
2813         rcu_read_unlock();
2814
2815         if (ifmgd->assoc_data && ifmgd->assoc_data->need_beacon &&
2816             ether_addr_equal(mgmt->bssid, ifmgd->assoc_data->bss->bssid)) {
2817                 ieee802_11_parse_elems(mgmt->u.beacon.variable,
2818                                        len - baselen, &elems);
2819
2820                 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
2821                 ifmgd->assoc_data->have_beacon = true;
2822                 ifmgd->assoc_data->need_beacon = false;
2823                 if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
2824                         sdata->vif.bss_conf.sync_tsf =
2825                                 le64_to_cpu(mgmt->u.beacon.timestamp);
2826                         sdata->vif.bss_conf.sync_device_ts =
2827                                 rx_status->device_timestamp;
2828                         if (elems.tim)
2829                                 sdata->vif.bss_conf.sync_dtim_count =
2830                                         elems.tim->dtim_count;
2831                         else
2832                                 sdata->vif.bss_conf.sync_dtim_count = 0;
2833                 }
2834                 /* continue assoc process */
2835                 ifmgd->assoc_data->timeout = jiffies;
2836                 ifmgd->assoc_data->timeout_started = true;
2837                 run_again(ifmgd, ifmgd->assoc_data->timeout);
2838                 return RX_MGMT_NONE;
2839         }
2840
2841         if (!ifmgd->associated ||
2842             !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
2843                 return RX_MGMT_NONE;
2844         bssid = ifmgd->associated->bssid;
2845
2846         /* Track average RSSI from the Beacon frames of the current AP */
2847         ifmgd->last_beacon_signal = rx_status->signal;
2848         if (ifmgd->flags & IEEE80211_STA_RESET_SIGNAL_AVE) {
2849                 ifmgd->flags &= ~IEEE80211_STA_RESET_SIGNAL_AVE;
2850                 ifmgd->ave_beacon_signal = rx_status->signal * 16;
2851                 ifmgd->last_cqm_event_signal = 0;
2852                 ifmgd->count_beacon_signal = 1;
2853                 ifmgd->last_ave_beacon_signal = 0;
2854         } else {
2855                 ifmgd->ave_beacon_signal =
2856                         (IEEE80211_SIGNAL_AVE_WEIGHT * rx_status->signal * 16 +
2857                          (16 - IEEE80211_SIGNAL_AVE_WEIGHT) *
2858                          ifmgd->ave_beacon_signal) / 16;
2859                 ifmgd->count_beacon_signal++;
2860         }
2861
2862         if (ifmgd->rssi_min_thold != ifmgd->rssi_max_thold &&
2863             ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
2864                 int sig = ifmgd->ave_beacon_signal;
2865                 int last_sig = ifmgd->last_ave_beacon_signal;
2866
2867                 /*
2868                  * if signal crosses either of the boundaries, invoke callback
2869                  * with appropriate parameters
2870                  */
2871                 if (sig > ifmgd->rssi_max_thold &&
2872                     (last_sig <= ifmgd->rssi_min_thold || last_sig == 0)) {
2873                         ifmgd->last_ave_beacon_signal = sig;
2874                         drv_rssi_callback(local, sdata, RSSI_EVENT_HIGH);
2875                 } else if (sig < ifmgd->rssi_min_thold &&
2876                            (last_sig >= ifmgd->rssi_max_thold ||
2877                            last_sig == 0)) {
2878                         ifmgd->last_ave_beacon_signal = sig;
2879                         drv_rssi_callback(local, sdata, RSSI_EVENT_LOW);
2880                 }
2881         }
2882
2883         if (bss_conf->cqm_rssi_thold &&
2884             ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT &&
2885             !(sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI)) {
2886                 int sig = ifmgd->ave_beacon_signal / 16;
2887                 int last_event = ifmgd->last_cqm_event_signal;
2888                 int thold = bss_conf->cqm_rssi_thold;
2889                 int hyst = bss_conf->cqm_rssi_hyst;
2890                 if (sig < thold &&
2891                     (last_event == 0 || sig < last_event - hyst)) {
2892                         ifmgd->last_cqm_event_signal = sig;
2893                         ieee80211_cqm_rssi_notify(
2894                                 &sdata->vif,
2895                                 NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
2896                                 GFP_KERNEL);
2897                 } else if (sig > thold &&
2898                            (last_event == 0 || sig > last_event + hyst)) {
2899                         ifmgd->last_cqm_event_signal = sig;
2900                         ieee80211_cqm_rssi_notify(
2901                                 &sdata->vif,
2902                                 NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
2903                                 GFP_KERNEL);
2904                 }
2905         }
2906
2907         if (ifmgd->flags & IEEE80211_STA_BEACON_POLL) {
2908                 mlme_dbg_ratelimited(sdata,
2909                                      "cancelling AP probe due to a received beacon\n");
2910                 mutex_lock(&local->mtx);
2911                 ifmgd->flags &= ~IEEE80211_STA_BEACON_POLL;
2912                 ieee80211_run_deferred_scan(local);
2913                 mutex_unlock(&local->mtx);
2914
2915                 mutex_lock(&local->iflist_mtx);
2916                 ieee80211_recalc_ps(local, -1);
2917                 mutex_unlock(&local->iflist_mtx);
2918         }
2919
2920         /*
2921          * Push the beacon loss detection into the future since
2922          * we are processing a beacon from the AP just now.
2923          */
2924         ieee80211_sta_reset_beacon_monitor(sdata);
2925
2926         ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
2927         ncrc = ieee802_11_parse_elems_crc(mgmt->u.beacon.variable,
2928                                           len - baselen, &elems,
2929                                           care_about_ies, ncrc);
2930
2931         if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) {
2932                 bool directed_tim = ieee80211_check_tim(elems.tim,
2933                                                         elems.tim_len,
2934                                                         ifmgd->aid);
2935                 if (directed_tim) {
2936                         if (local->hw.conf.dynamic_ps_timeout > 0) {
2937                                 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
2938                                         local->hw.conf.flags &= ~IEEE80211_CONF_PS;
2939                                         ieee80211_hw_config(local,
2940                                                             IEEE80211_CONF_CHANGE_PS);
2941                                 }
2942                                 ieee80211_send_nullfunc(local, sdata, 0);
2943                         } else if (!local->pspolling && sdata->u.mgd.powersave) {
2944                                 local->pspolling = true;
2945
2946                                 /*
2947                                  * Here is assumed that the driver will be
2948                                  * able to send ps-poll frame and receive a
2949                                  * response even though power save mode is
2950                                  * enabled, but some drivers might require
2951                                  * to disable power save here. This needs
2952                                  * to be investigated.
2953                                  */
2954                                 ieee80211_send_pspoll(local, sdata);
2955                         }
2956                 }
2957         }
2958
2959         if (sdata->vif.p2p) {
2960                 u8 noa[2];
2961                 int ret;
2962
2963                 ret = cfg80211_get_p2p_attr(mgmt->u.beacon.variable,
2964                                             len - baselen,
2965                                             IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
2966                                             noa, sizeof(noa));
2967                 if (ret >= 2 && sdata->u.mgd.p2p_noa_index != noa[0]) {
2968                         bss_conf->p2p_oppps = noa[1] & 0x80;
2969                         bss_conf->p2p_ctwindow = noa[1] & 0x7f;
2970                         changed |= BSS_CHANGED_P2P_PS;
2971                         sdata->u.mgd.p2p_noa_index = noa[0];
2972                         /*
2973                          * make sure we update all information, the CRC
2974                          * mechanism doesn't look at P2P attributes.
2975                          */
2976                         ifmgd->beacon_crc_valid = false;
2977                 }
2978         }
2979
2980         if (ncrc == ifmgd->beacon_crc && ifmgd->beacon_crc_valid)
2981                 return RX_MGMT_NONE;
2982         ifmgd->beacon_crc = ncrc;
2983         ifmgd->beacon_crc_valid = true;
2984
2985         ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
2986
2987         if (ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
2988                                      elems.wmm_param_len))
2989                 changed |= BSS_CHANGED_QOS;
2990
2991         /*
2992          * If we haven't had a beacon before, tell the driver about the
2993          * DTIM period (and beacon timing if desired) now.
2994          */
2995         if (!bss_conf->dtim_period) {
2996                 /* a few bogus AP send dtim_period = 0 or no TIM IE */
2997                 if (elems.tim)
2998                         bss_conf->dtim_period = elems.tim->dtim_period ?: 1;
2999                 else
3000                         bss_conf->dtim_period = 1;
3001
3002                 if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
3003                         sdata->vif.bss_conf.sync_tsf =
3004                                 le64_to_cpu(mgmt->u.beacon.timestamp);
3005                         sdata->vif.bss_conf.sync_device_ts =
3006                                 rx_status->device_timestamp;
3007                         if (elems.tim)
3008                                 sdata->vif.bss_conf.sync_dtim_count =
3009                                         elems.tim->dtim_count;
3010                         else
3011                                 sdata->vif.bss_conf.sync_dtim_count = 0;
3012                 }
3013
3014                 changed |= BSS_CHANGED_DTIM_PERIOD;
3015         }
3016
3017         if (elems.erp_info && elems.erp_info_len >= 1) {
3018                 erp_valid = true;
3019                 erp_value = elems.erp_info[0];
3020         } else {
3021                 erp_valid = false;
3022         }
3023         changed |= ieee80211_handle_bss_capability(sdata,
3024                         le16_to_cpu(mgmt->u.beacon.capab_info),
3025                         erp_valid, erp_value);
3026
3027         mutex_lock(&local->sta_mtx);
3028         sta = sta_info_get(sdata, bssid);
3029
3030         if (ieee80211_config_bw(sdata, sta, elems.ht_operation,
3031                                 elems.vht_operation, bssid, &changed)) {
3032                 mutex_unlock(&local->sta_mtx);
3033                 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
3034                                        WLAN_REASON_DEAUTH_LEAVING,
3035                                        true, deauth_buf);
3036                 return RX_MGMT_CFG80211_TX_DEAUTH;
3037         }
3038
3039         if (sta && elems.opmode_notif)
3040                 ieee80211_vht_handle_opmode(sdata, sta, *elems.opmode_notif,
3041                                             rx_status->band, true);
3042         mutex_unlock(&local->sta_mtx);
3043
3044         if (elems.country_elem && elems.pwr_constr_elem &&
3045             mgmt->u.probe_resp.capab_info &
3046                                 cpu_to_le16(WLAN_CAPABILITY_SPECTRUM_MGMT))
3047                 changed |= ieee80211_handle_pwr_constr(sdata, chan,
3048                                                        elems.country_elem,
3049                                                        elems.country_elem_len,
3050                                                        elems.pwr_constr_elem);
3051
3052         ieee80211_bss_info_change_notify(sdata, changed);
3053
3054         return RX_MGMT_NONE;
3055 }
3056
3057 void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
3058                                   struct sk_buff *skb)
3059 {
3060         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3061         struct ieee80211_rx_status *rx_status;
3062         struct ieee80211_mgmt *mgmt;
3063         struct cfg80211_bss *bss = NULL;
3064         enum rx_mgmt_action rma = RX_MGMT_NONE;
3065         u8 deauth_buf[IEEE80211_DEAUTH_FRAME_LEN];
3066         u16 fc;
3067
3068         rx_status = (struct ieee80211_rx_status *) skb->cb;
3069         mgmt = (struct ieee80211_mgmt *) skb->data;
3070         fc = le16_to_cpu(mgmt->frame_control);
3071
3072         mutex_lock(&ifmgd->mtx);
3073
3074         switch (fc & IEEE80211_FCTL_STYPE) {
3075         case IEEE80211_STYPE_BEACON:
3076                 rma = ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len,
3077                                                deauth_buf, rx_status);
3078                 break;
3079         case IEEE80211_STYPE_PROBE_RESP:
3080                 ieee80211_rx_mgmt_probe_resp(sdata, skb);
3081                 break;
3082         case IEEE80211_STYPE_AUTH:
3083                 rma = ieee80211_rx_mgmt_auth(sdata, mgmt, skb->len);
3084                 break;
3085         case IEEE80211_STYPE_DEAUTH:
3086                 rma = ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
3087                 break;
3088         case IEEE80211_STYPE_DISASSOC:
3089                 rma = ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
3090                 break;
3091         case IEEE80211_STYPE_ASSOC_RESP:
3092         case IEEE80211_STYPE_REASSOC_RESP:
3093                 rma = ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len, &bss);
3094                 break;
3095         case IEEE80211_STYPE_ACTION:
3096                 switch (mgmt->u.action.category) {
3097                 case WLAN_CATEGORY_SPECTRUM_MGMT:
3098                         ieee80211_sta_process_chanswitch(sdata,
3099                                         &mgmt->u.action.u.chan_switch.sw_elem,
3100                                         (void *)ifmgd->associated->priv,
3101                                         rx_status->mactime);
3102                         break;
3103                 }
3104         }
3105         mutex_unlock(&ifmgd->mtx);
3106
3107         switch (rma) {
3108         case RX_MGMT_NONE:
3109                 /* no action */
3110                 break;
3111         case RX_MGMT_CFG80211_DEAUTH:
3112                 cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
3113                 break;
3114         case RX_MGMT_CFG80211_DISASSOC:
3115                 cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
3116                 break;
3117         case RX_MGMT_CFG80211_RX_AUTH:
3118                 cfg80211_send_rx_auth(sdata->dev, (u8 *)mgmt, skb->len);
3119                 break;
3120         case RX_MGMT_CFG80211_RX_ASSOC:
3121                 cfg80211_send_rx_assoc(sdata->dev, bss, (u8 *)mgmt, skb->len);
3122                 break;
3123         case RX_MGMT_CFG80211_ASSOC_TIMEOUT:
3124                 cfg80211_send_assoc_timeout(sdata->dev, mgmt->bssid);
3125                 break;
3126         case RX_MGMT_CFG80211_TX_DEAUTH:
3127                 cfg80211_send_deauth(sdata->dev, deauth_buf,
3128                                      sizeof(deauth_buf));
3129                 break;
3130         default:
3131                 WARN(1, "unexpected: %d", rma);
3132         }
3133 }
3134
3135 static void ieee80211_sta_timer(unsigned long data)
3136 {
3137         struct ieee80211_sub_if_data *sdata =
3138                 (struct ieee80211_sub_if_data *) data;
3139
3140         ieee80211_queue_work(&sdata->local->hw, &sdata->work);
3141 }
3142
3143 static void ieee80211_sta_connection_lost(struct ieee80211_sub_if_data *sdata,
3144                                           u8 *bssid, u8 reason, bool tx)
3145 {
3146         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3147         u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
3148
3149         ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH, reason,
3150                                tx, frame_buf);
3151         mutex_unlock(&ifmgd->mtx);
3152
3153         /*
3154          * must be outside lock due to cfg80211,
3155          * but that's not a problem.
3156          */
3157         cfg80211_send_deauth(sdata->dev, frame_buf, IEEE80211_DEAUTH_FRAME_LEN);
3158
3159         mutex_lock(&ifmgd->mtx);
3160 }
3161
3162 static int ieee80211_probe_auth(struct ieee80211_sub_if_data *sdata)
3163 {
3164         struct ieee80211_local *local = sdata->local;
3165         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3166         struct ieee80211_mgd_auth_data *auth_data = ifmgd->auth_data;
3167         u32 tx_flags = 0;
3168
3169         lockdep_assert_held(&ifmgd->mtx);
3170
3171         if (WARN_ON_ONCE(!auth_data))
3172                 return -EINVAL;
3173
3174         if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
3175                 tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
3176                            IEEE80211_TX_INTFL_MLME_CONN_TX;
3177
3178         auth_data->tries++;
3179
3180         if (auth_data->tries > IEEE80211_AUTH_MAX_TRIES) {
3181                 sdata_info(sdata, "authentication with %pM timed out\n",
3182                            auth_data->bss->bssid);
3183
3184                 /*
3185                  * Most likely AP is not in the range so remove the
3186                  * bss struct for that AP.
3187                  */
3188                 cfg80211_unlink_bss(local->hw.wiphy, auth_data->bss);
3189
3190                 return -ETIMEDOUT;
3191         }
3192
3193         drv_mgd_prepare_tx(local, sdata);
3194
3195         if (auth_data->bss->proberesp_ies) {
3196                 u16 trans = 1;
3197                 u16 status = 0;
3198
3199                 sdata_info(sdata, "send auth to %pM (try %d/%d)\n",
3200                            auth_data->bss->bssid, auth_data->tries,
3201                            IEEE80211_AUTH_MAX_TRIES);
3202
3203                 auth_data->expected_transaction = 2;
3204
3205                 if (auth_data->algorithm == WLAN_AUTH_SAE) {
3206                         trans = auth_data->sae_trans;
3207                         status = auth_data->sae_status;
3208                         auth_data->expected_transaction = trans;
3209                 }
3210
3211                 ieee80211_send_auth(sdata, trans, auth_data->algorithm, status,
3212                                     auth_data->data, auth_data->data_len,
3213                                     auth_data->bss->bssid,
3214                                     auth_data->bss->bssid, NULL, 0, 0,
3215                                     tx_flags);
3216         } else {
3217                 const u8 *ssidie;
3218
3219                 sdata_info(sdata, "direct probe to %pM (try %d/%i)\n",
3220                            auth_data->bss->bssid, auth_data->tries,
3221                            IEEE80211_AUTH_MAX_TRIES);
3222
3223                 rcu_read_lock();
3224                 ssidie = ieee80211_bss_get_ie(auth_data->bss, WLAN_EID_SSID);
3225                 if (!ssidie) {
3226                         rcu_read_unlock();
3227                         return -EINVAL;
3228                 }
3229                 /*
3230                  * Direct probe is sent to broadcast address as some APs
3231                  * will not answer to direct packet in unassociated state.
3232                  */
3233                 ieee80211_send_probe_req(sdata, NULL, ssidie + 2, ssidie[1],
3234                                          NULL, 0, (u32) -1, true, tx_flags,
3235                                          auth_data->bss->channel, false);
3236                 rcu_read_unlock();
3237         }
3238
3239         if (!(local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)) {
3240                 auth_data->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
3241                 ifmgd->auth_data->timeout_started = true;
3242                 run_again(ifmgd, auth_data->timeout);
3243         } else {
3244                 auth_data->timeout_started = false;
3245         }
3246
3247         return 0;
3248 }
3249
3250 static int ieee80211_do_assoc(struct ieee80211_sub_if_data *sdata)
3251 {
3252         struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
3253         struct ieee80211_local *local = sdata->local;
3254
3255         lockdep_assert_held(&sdata->u.mgd.mtx);
3256
3257         assoc_data->tries++;
3258         if (assoc_data->tries > IEEE80211_ASSOC_MAX_TRIES) {
3259                 sdata_info(sdata, "association with %pM timed out\n",
3260                            assoc_data->bss->bssid);
3261
3262                 /*
3263                  * Most likely AP is not in the range so remove the
3264                  * bss struct for that AP.
3265                  */
3266                 cfg80211_unlink_bss(local->hw.wiphy, assoc_data->bss);
3267
3268                 return -ETIMEDOUT;
3269         }
3270
3271         sdata_info(sdata, "associate with %pM (try %d/%d)\n",
3272                    assoc_data->bss->bssid, assoc_data->tries,
3273                    IEEE80211_ASSOC_MAX_TRIES);
3274         ieee80211_send_assoc(sdata);
3275
3276         if (!(local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)) {
3277                 assoc_data->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
3278                 assoc_data->timeout_started = true;
3279                 run_again(&sdata->u.mgd, assoc_data->timeout);
3280         } else {
3281                 assoc_data->timeout_started = false;
3282         }
3283
3284         return 0;
3285 }
3286
3287 void ieee80211_mgd_conn_tx_status(struct ieee80211_sub_if_data *sdata,
3288                                   __le16 fc, bool acked)
3289 {
3290         struct ieee80211_local *local = sdata->local;
3291
3292         sdata->u.mgd.status_fc = fc;
3293         sdata->u.mgd.status_acked = acked;
3294         sdata->u.mgd.status_received = true;
3295
3296         ieee80211_queue_work(&local->hw, &sdata->work);
3297 }
3298
3299 void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
3300 {
3301         struct ieee80211_local *local = sdata->local;
3302         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3303
3304         mutex_lock(&ifmgd->mtx);
3305
3306         if (ifmgd->status_received) {
3307                 __le16 fc = ifmgd->status_fc;
3308                 bool status_acked = ifmgd->status_acked;
3309
3310                 ifmgd->status_received = false;
3311                 if (ifmgd->auth_data &&
3312                     (ieee80211_is_probe_req(fc) || ieee80211_is_auth(fc))) {
3313                         if (status_acked) {
3314                                 ifmgd->auth_data->timeout =
3315                                         jiffies + IEEE80211_AUTH_TIMEOUT_SHORT;
3316                                 run_again(ifmgd, ifmgd->auth_data->timeout);
3317                         } else {
3318                                 ifmgd->auth_data->timeout = jiffies - 1;
3319                         }
3320                         ifmgd->auth_data->timeout_started = true;
3321                 } else if (ifmgd->assoc_data &&
3322                            (ieee80211_is_assoc_req(fc) ||
3323                             ieee80211_is_reassoc_req(fc))) {
3324                         if (status_acked) {
3325                                 ifmgd->assoc_data->timeout =
3326                                         jiffies + IEEE80211_ASSOC_TIMEOUT_SHORT;
3327                                 run_again(ifmgd, ifmgd->assoc_data->timeout);
3328                         } else {
3329                                 ifmgd->assoc_data->timeout = jiffies - 1;
3330                         }
3331                         ifmgd->assoc_data->timeout_started = true;
3332                 }
3333         }
3334
3335         if (ifmgd->auth_data && ifmgd->auth_data->timeout_started &&
3336             time_after(jiffies, ifmgd->auth_data->timeout)) {
3337                 if (ifmgd->auth_data->done) {
3338                         /*
3339                          * ok ... we waited for assoc but userspace didn't,
3340                          * so let's just kill the auth data
3341                          */
3342                         ieee80211_destroy_auth_data(sdata, false);
3343                 } else if (ieee80211_probe_auth(sdata)) {
3344                         u8 bssid[ETH_ALEN];
3345
3346                         memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
3347
3348                         ieee80211_destroy_auth_data(sdata, false);
3349
3350                         mutex_unlock(&ifmgd->mtx);
3351                         cfg80211_send_auth_timeout(sdata->dev, bssid);
3352                         mutex_lock(&ifmgd->mtx);
3353                 }
3354         } else if (ifmgd->auth_data && ifmgd->auth_data->timeout_started)
3355                 run_again(ifmgd, ifmgd->auth_data->timeout);
3356
3357         if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started &&
3358             time_after(jiffies, ifmgd->assoc_data->timeout)) {
3359                 if ((ifmgd->assoc_data->need_beacon &&
3360                      !ifmgd->assoc_data->have_beacon) ||
3361                     ieee80211_do_assoc(sdata)) {
3362                         u8 bssid[ETH_ALEN];
3363
3364                         memcpy(bssid, ifmgd->assoc_data->bss->bssid, ETH_ALEN);
3365
3366                         ieee80211_destroy_assoc_data(sdata, false);
3367
3368                         mutex_unlock(&ifmgd->mtx);
3369                         cfg80211_send_assoc_timeout(sdata->dev, bssid);
3370                         mutex_lock(&ifmgd->mtx);
3371                 }
3372         } else if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started)
3373                 run_again(ifmgd, ifmgd->assoc_data->timeout);
3374
3375         if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
3376                             IEEE80211_STA_CONNECTION_POLL) &&
3377             ifmgd->associated) {
3378                 u8 bssid[ETH_ALEN];
3379                 int max_tries;
3380
3381                 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
3382
3383                 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
3384                         max_tries = max_nullfunc_tries;
3385                 else
3386                         max_tries = max_probe_tries;
3387
3388                 /* ACK received for nullfunc probing frame */
3389                 if (!ifmgd->probe_send_count)
3390                         ieee80211_reset_ap_probe(sdata);
3391                 else if (ifmgd->nullfunc_failed) {
3392                         if (ifmgd->probe_send_count < max_tries) {
3393                                 mlme_dbg(sdata,
3394                                          "No ack for nullfunc frame to AP %pM, try %d/%i\n",
3395                                          bssid, ifmgd->probe_send_count,
3396                                          max_tries);
3397                                 ieee80211_mgd_probe_ap_send(sdata);
3398                         } else {
3399                                 mlme_dbg(sdata,
3400                                          "No ack for nullfunc frame to AP %pM, disconnecting.\n",
3401                                          bssid);
3402                                 ieee80211_sta_connection_lost(sdata, bssid,
3403                                         WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
3404                                         false);
3405                         }
3406                 } else if (time_is_after_jiffies(ifmgd->probe_timeout))
3407                         run_again(ifmgd, ifmgd->probe_timeout);
3408                 else if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
3409                         mlme_dbg(sdata,
3410                                  "Failed to send nullfunc to AP %pM after %dms, disconnecting\n",
3411                                  bssid, probe_wait_ms);
3412                         ieee80211_sta_connection_lost(sdata, bssid,
3413                                 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
3414                 } else if (ifmgd->probe_send_count < max_tries) {
3415                         mlme_dbg(sdata,
3416                                  "No probe response from AP %pM after %dms, try %d/%i\n",
3417                                  bssid, probe_wait_ms,
3418                                  ifmgd->probe_send_count, max_tries);
3419                         ieee80211_mgd_probe_ap_send(sdata);
3420                 } else {
3421                         /*
3422                          * We actually lost the connection ... or did we?
3423                          * Let's make sure!
3424                          */
3425                         wiphy_debug(local->hw.wiphy,
3426                                     "%s: No probe response from AP %pM"
3427                                     " after %dms, disconnecting.\n",
3428                                     sdata->name,
3429                                     bssid, probe_wait_ms);
3430
3431                         ieee80211_sta_connection_lost(sdata, bssid,
3432                                 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
3433                 }
3434         }
3435
3436         mutex_unlock(&ifmgd->mtx);
3437 }
3438
3439 static void ieee80211_sta_bcn_mon_timer(unsigned long data)
3440 {
3441         struct ieee80211_sub_if_data *sdata =
3442                 (struct ieee80211_sub_if_data *) data;
3443         struct ieee80211_local *local = sdata->local;
3444
3445         if (local->quiescing)
3446                 return;
3447
3448         sdata->u.mgd.connection_loss = false;
3449         ieee80211_queue_work(&sdata->local->hw,
3450                              &sdata->u.mgd.beacon_connection_loss_work);
3451 }
3452
3453 static void ieee80211_sta_conn_mon_timer(unsigned long data)
3454 {
3455         struct ieee80211_sub_if_data *sdata =
3456                 (struct ieee80211_sub_if_data *) data;
3457         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3458         struct ieee80211_local *local = sdata->local;
3459
3460         if (local->quiescing)
3461                 return;
3462
3463         ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
3464 }
3465
3466 static void ieee80211_sta_monitor_work(struct work_struct *work)
3467 {
3468         struct ieee80211_sub_if_data *sdata =
3469                 container_of(work, struct ieee80211_sub_if_data,
3470                              u.mgd.monitor_work);
3471
3472         ieee80211_mgd_probe_ap(sdata, false);
3473 }
3474
3475 static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
3476 {
3477         u32 flags;
3478
3479         if (sdata->vif.type == NL80211_IFTYPE_STATION) {
3480                 __ieee80211_stop_poll(sdata);
3481
3482                 /* let's probe the connection once */
3483                 flags = sdata->local->hw.flags;
3484                 if (!(flags & IEEE80211_HW_CONNECTION_MONITOR))
3485                         ieee80211_queue_work(&sdata->local->hw,
3486                                              &sdata->u.mgd.monitor_work);
3487                 /* and do all the other regular work too */
3488                 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
3489         }
3490 }
3491
3492 /* interface setup */
3493 void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
3494 {
3495         struct ieee80211_if_managed *ifmgd;
3496
3497         ifmgd = &sdata->u.mgd;
3498         INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
3499         INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
3500         INIT_WORK(&ifmgd->beacon_connection_loss_work,
3501                   ieee80211_beacon_connection_loss_work);
3502         INIT_WORK(&ifmgd->csa_connection_drop_work,
3503                   ieee80211_csa_connection_drop_work);
3504         INIT_WORK(&ifmgd->request_smps_work, ieee80211_request_smps_work);
3505         setup_timer(&ifmgd->timer, ieee80211_sta_timer,
3506                     (unsigned long) sdata);
3507         setup_timer(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer,
3508                     (unsigned long) sdata);
3509         setup_timer(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer,
3510                     (unsigned long) sdata);
3511         setup_timer(&ifmgd->chswitch_timer, ieee80211_chswitch_timer,
3512                     (unsigned long) sdata);
3513
3514         ifmgd->flags = 0;
3515         ifmgd->powersave = sdata->wdev.ps;
3516         ifmgd->uapsd_queues = IEEE80211_DEFAULT_UAPSD_QUEUES;
3517         ifmgd->uapsd_max_sp_len = IEEE80211_DEFAULT_MAX_SP_LEN;
3518
3519         mutex_init(&ifmgd->mtx);
3520
3521         if (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS)
3522                 ifmgd->req_smps = IEEE80211_SMPS_AUTOMATIC;
3523         else
3524                 ifmgd->req_smps = IEEE80211_SMPS_OFF;
3525 }
3526
3527 /* scan finished notification */
3528 void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
3529 {
3530         struct ieee80211_sub_if_data *sdata;
3531
3532         /* Restart STA timers */
3533         rcu_read_lock();
3534         list_for_each_entry_rcu(sdata, &local->interfaces, list) {
3535                 if (ieee80211_sdata_running(sdata))
3536                         ieee80211_restart_sta_timer(sdata);
3537         }
3538         rcu_read_unlock();
3539 }
3540
3541 int ieee80211_max_network_latency(struct notifier_block *nb,
3542                                   unsigned long data, void *dummy)
3543 {
3544         s32 latency_usec = (s32) data;
3545         struct ieee80211_local *local =
3546                 container_of(nb, struct ieee80211_local,
3547                              network_latency_notifier);
3548
3549         mutex_lock(&local->iflist_mtx);
3550         ieee80211_recalc_ps(local, latency_usec);
3551         mutex_unlock(&local->iflist_mtx);
3552
3553         return 0;
3554 }
3555
3556 static u8 ieee80211_ht_vht_rx_chains(struct ieee80211_sub_if_data *sdata,
3557                                      struct cfg80211_bss *cbss)
3558 {
3559         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3560         const u8 *ht_cap_ie, *vht_cap_ie;
3561         const struct ieee80211_ht_cap *ht_cap;
3562         const struct ieee80211_vht_cap *vht_cap;
3563         u8 chains = 1;
3564
3565         if (ifmgd->flags & IEEE80211_STA_DISABLE_HT)
3566                 return chains;
3567
3568         ht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
3569         if (ht_cap_ie && ht_cap_ie[1] >= sizeof(*ht_cap)) {
3570                 ht_cap = (void *)(ht_cap_ie + 2);
3571                 chains = ieee80211_mcs_to_chains(&ht_cap->mcs);
3572                 /*
3573                  * TODO: use "Tx Maximum Number Spatial Streams Supported" and
3574                  *       "Tx Unequal Modulation Supported" fields.
3575                  */
3576         }
3577
3578         if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
3579                 return chains;
3580
3581         vht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
3582         if (vht_cap_ie && vht_cap_ie[1] >= sizeof(*vht_cap)) {
3583                 u8 nss;
3584                 u16 tx_mcs_map;
3585
3586                 vht_cap = (void *)(vht_cap_ie + 2);
3587                 tx_mcs_map = le16_to_cpu(vht_cap->supp_mcs.tx_mcs_map);
3588                 for (nss = 8; nss > 0; nss--) {
3589                         if (((tx_mcs_map >> (2 * (nss - 1))) & 3) !=
3590                                         IEEE80211_VHT_MCS_NOT_SUPPORTED)
3591                                 break;
3592                 }
3593                 /* TODO: use "Tx Highest Supported Long GI Data Rate" field? */
3594                 chains = max(chains, nss);
3595         }
3596
3597         return chains;
3598 }
3599
3600 static int ieee80211_prep_channel(struct ieee80211_sub_if_data *sdata,
3601                                   struct cfg80211_bss *cbss)
3602 {
3603         struct ieee80211_local *local = sdata->local;
3604         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3605         const struct ieee80211_ht_operation *ht_oper = NULL;
3606         const struct ieee80211_vht_operation *vht_oper = NULL;
3607         struct ieee80211_supported_band *sband;
3608         struct cfg80211_chan_def chandef;
3609         int ret;
3610
3611         sband = local->hw.wiphy->bands[cbss->channel->band];
3612
3613         ifmgd->flags &= ~(IEEE80211_STA_DISABLE_40MHZ |
3614                           IEEE80211_STA_DISABLE_80P80MHZ |
3615                           IEEE80211_STA_DISABLE_160MHZ);
3616
3617         rcu_read_lock();
3618
3619         if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
3620             sband->ht_cap.ht_supported) {
3621                 const u8 *ht_oper_ie, *ht_cap;
3622
3623                 ht_oper_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_OPERATION);
3624                 if (ht_oper_ie && ht_oper_ie[1] >= sizeof(*ht_oper))
3625                         ht_oper = (void *)(ht_oper_ie + 2);
3626
3627                 ht_cap = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
3628                 if (!ht_cap || ht_cap[1] < sizeof(struct ieee80211_ht_cap)) {
3629                         ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
3630                         ht_oper = NULL;
3631                 }
3632         }
3633
3634         if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
3635             sband->vht_cap.vht_supported) {
3636                 const u8 *vht_oper_ie, *vht_cap;
3637
3638                 vht_oper_ie = ieee80211_bss_get_ie(cbss,
3639                                                    WLAN_EID_VHT_OPERATION);
3640                 if (vht_oper_ie && vht_oper_ie[1] >= sizeof(*vht_oper))
3641                         vht_oper = (void *)(vht_oper_ie + 2);
3642                 if (vht_oper && !ht_oper) {
3643                         vht_oper = NULL;
3644                         sdata_info(sdata,
3645                                    "AP advertised VHT without HT, disabling both\n");
3646                         ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
3647                         ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
3648                 }
3649
3650                 vht_cap = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
3651                 if (!vht_cap || vht_cap[1] < sizeof(struct ieee80211_vht_cap)) {
3652                         ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
3653                         vht_oper = NULL;
3654                 }
3655         }
3656
3657         ifmgd->flags |= ieee80211_determine_chantype(sdata, sband,
3658                                                      cbss->channel,
3659                                                      ht_oper, vht_oper,
3660                                                      &chandef, true);
3661
3662         sdata->needed_rx_chains = min(ieee80211_ht_vht_rx_chains(sdata, cbss),
3663                                       local->rx_chains);
3664
3665         rcu_read_unlock();
3666
3667         /* will change later if needed */
3668         sdata->smps_mode = IEEE80211_SMPS_OFF;
3669
3670         /*
3671          * If this fails (possibly due to channel context sharing
3672          * on incompatible channels, e.g. 80+80 and 160 sharing the
3673          * same control channel) try to use a smaller bandwidth.
3674          */
3675         ret = ieee80211_vif_use_channel(sdata, &chandef,
3676                                         IEEE80211_CHANCTX_SHARED);
3677         while (ret && chandef.width != NL80211_CHAN_WIDTH_20_NOHT) {
3678                 ifmgd->flags |= chandef_downgrade(&chandef);
3679                 ret = ieee80211_vif_use_channel(sdata, &chandef,
3680                                                 IEEE80211_CHANCTX_SHARED);
3681         }
3682         return ret;
3683 }
3684
3685 static int ieee80211_prep_connection(struct ieee80211_sub_if_data *sdata,
3686                                      struct cfg80211_bss *cbss, bool assoc)
3687 {
3688         struct ieee80211_local *local = sdata->local;
3689         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3690         struct ieee80211_bss *bss = (void *)cbss->priv;
3691         struct sta_info *new_sta = NULL;
3692         bool have_sta = false;
3693         int err;
3694
3695         if (WARN_ON(!ifmgd->auth_data && !ifmgd->assoc_data))
3696                 return -EINVAL;
3697
3698         if (assoc) {
3699                 rcu_read_lock();
3700                 have_sta = sta_info_get(sdata, cbss->bssid);
3701                 rcu_read_unlock();
3702         }
3703
3704         if (!have_sta) {
3705                 new_sta = sta_info_alloc(sdata, cbss->bssid, GFP_KERNEL);
3706                 if (!new_sta)
3707                         return -ENOMEM;
3708         }
3709
3710         if (new_sta) {
3711                 u32 rates = 0, basic_rates = 0;
3712                 bool have_higher_than_11mbit;
3713                 int min_rate = INT_MAX, min_rate_index = -1;
3714                 struct ieee80211_supported_band *sband;
3715                 const struct cfg80211_bss_ies *ies;
3716
3717                 sband = local->hw.wiphy->bands[cbss->channel->band];
3718
3719                 err = ieee80211_prep_channel(sdata, cbss);
3720                 if (err) {
3721                         sta_info_free(local, new_sta);
3722                         return err;
3723                 }
3724
3725                 ieee80211_get_rates(sband, bss->supp_rates,
3726                                     bss->supp_rates_len,
3727                                     &rates, &basic_rates,
3728                                     &have_higher_than_11mbit,
3729                                     &min_rate, &min_rate_index);
3730
3731                 /*
3732                  * This used to be a workaround for basic rates missing
3733                  * in the association response frame. Now that we no
3734                  * longer use the basic rates from there, it probably
3735                  * doesn't happen any more, but keep the workaround so
3736                  * in case some *other* APs are buggy in different ways
3737                  * we can connect -- with a warning.
3738                  */
3739                 if (!basic_rates && min_rate_index >= 0) {
3740                         sdata_info(sdata,
3741                                    "No basic rates, using min rate instead\n");
3742                         basic_rates = BIT(min_rate_index);
3743                 }
3744
3745                 new_sta->sta.supp_rates[cbss->channel->band] = rates;
3746                 sdata->vif.bss_conf.basic_rates = basic_rates;
3747
3748                 /* cf. IEEE 802.11 9.2.12 */
3749                 if (cbss->channel->band == IEEE80211_BAND_2GHZ &&
3750                     have_higher_than_11mbit)
3751                         sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
3752                 else
3753                         sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
3754
3755                 memcpy(ifmgd->bssid, cbss->bssid, ETH_ALEN);
3756
3757                 /* set timing information */
3758                 sdata->vif.bss_conf.beacon_int = cbss->beacon_interval;
3759                 rcu_read_lock();
3760                 ies = rcu_dereference(cbss->beacon_ies);
3761                 if (ies) {
3762                         const u8 *tim_ie;
3763
3764                         sdata->vif.bss_conf.sync_tsf = ies->tsf;
3765                         sdata->vif.bss_conf.sync_device_ts =
3766                                 bss->device_ts_beacon;
3767                         tim_ie = cfg80211_find_ie(WLAN_EID_TIM,
3768                                                   ies->data, ies->len);
3769                         if (tim_ie && tim_ie[1] >= 2)
3770                                 sdata->vif.bss_conf.sync_dtim_count = tim_ie[2];
3771                         else
3772                                 sdata->vif.bss_conf.sync_dtim_count = 0;
3773                 } else if (!(local->hw.flags &
3774                                         IEEE80211_HW_TIMING_BEACON_ONLY)) {
3775                         ies = rcu_dereference(cbss->proberesp_ies);
3776                         /* must be non-NULL since beacon IEs were NULL */
3777                         sdata->vif.bss_conf.sync_tsf = ies->tsf;
3778                         sdata->vif.bss_conf.sync_device_ts =
3779                                 bss->device_ts_presp;
3780                         sdata->vif.bss_conf.sync_dtim_count = 0;
3781                 } else {
3782                         sdata->vif.bss_conf.sync_tsf = 0;
3783                         sdata->vif.bss_conf.sync_device_ts = 0;
3784                         sdata->vif.bss_conf.sync_dtim_count = 0;
3785                 }
3786                 rcu_read_unlock();
3787
3788                 /* tell driver about BSSID, basic rates and timing */
3789                 ieee80211_bss_info_change_notify(sdata,
3790                         BSS_CHANGED_BSSID | BSS_CHANGED_BASIC_RATES |
3791                         BSS_CHANGED_BEACON_INT);
3792
3793                 if (assoc)
3794                         sta_info_pre_move_state(new_sta, IEEE80211_STA_AUTH);
3795
3796                 err = sta_info_insert(new_sta);
3797                 new_sta = NULL;
3798                 if (err) {
3799                         sdata_info(sdata,
3800                                    "failed to insert STA entry for the AP (error %d)\n",
3801                                    err);
3802                         return err;
3803                 }
3804         } else
3805                 WARN_ON_ONCE(!ether_addr_equal(ifmgd->bssid, cbss->bssid));
3806
3807         return 0;
3808 }
3809
3810 /* config hooks */
3811 int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
3812                        struct cfg80211_auth_request *req)
3813 {
3814         struct ieee80211_local *local = sdata->local;
3815         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3816         struct ieee80211_mgd_auth_data *auth_data;
3817         u16 auth_alg;
3818         int err;
3819
3820         /* prepare auth data structure */
3821
3822         switch (req->auth_type) {
3823         case NL80211_AUTHTYPE_OPEN_SYSTEM:
3824                 auth_alg = WLAN_AUTH_OPEN;
3825                 break;
3826         case NL80211_AUTHTYPE_SHARED_KEY:
3827                 if (IS_ERR(local->wep_tx_tfm))
3828                         return -EOPNOTSUPP;
3829                 auth_alg = WLAN_AUTH_SHARED_KEY;
3830                 break;
3831         case NL80211_AUTHTYPE_FT:
3832                 auth_alg = WLAN_AUTH_FT;
3833                 break;
3834         case NL80211_AUTHTYPE_NETWORK_EAP:
3835                 auth_alg = WLAN_AUTH_LEAP;
3836                 break;
3837         case NL80211_AUTHTYPE_SAE:
3838                 auth_alg = WLAN_AUTH_SAE;
3839                 break;
3840         default:
3841                 return -EOPNOTSUPP;
3842         }
3843
3844         auth_data = kzalloc(sizeof(*auth_data) + req->sae_data_len +
3845                             req->ie_len, GFP_KERNEL);
3846         if (!auth_data)
3847                 return -ENOMEM;
3848
3849         auth_data->bss = req->bss;
3850
3851         if (req->sae_data_len >= 4) {
3852                 __le16 *pos = (__le16 *) req->sae_data;
3853                 auth_data->sae_trans = le16_to_cpu(pos[0]);
3854                 auth_data->sae_status = le16_to_cpu(pos[1]);
3855                 memcpy(auth_data->data, req->sae_data + 4,
3856                        req->sae_data_len - 4);
3857                 auth_data->data_len += req->sae_data_len - 4;
3858         }
3859
3860         if (req->ie && req->ie_len) {
3861                 memcpy(&auth_data->data[auth_data->data_len],
3862                        req->ie, req->ie_len);
3863                 auth_data->data_len += req->ie_len;
3864         }
3865
3866         if (req->key && req->key_len) {
3867                 auth_data->key_len = req->key_len;
3868                 auth_data->key_idx = req->key_idx;
3869                 memcpy(auth_data->key, req->key, req->key_len);
3870         }
3871
3872         auth_data->algorithm = auth_alg;
3873
3874         /* try to authenticate/probe */
3875
3876         mutex_lock(&ifmgd->mtx);
3877
3878         if ((ifmgd->auth_data && !ifmgd->auth_data->done) ||
3879             ifmgd->assoc_data) {
3880                 err = -EBUSY;
3881                 goto err_free;
3882         }
3883
3884         if (ifmgd->auth_data)
3885                 ieee80211_destroy_auth_data(sdata, false);
3886
3887         /* prep auth_data so we don't go into idle on disassoc */
3888         ifmgd->auth_data = auth_data;
3889
3890         if (ifmgd->associated) {
3891                 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
3892
3893                 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
3894                                        WLAN_REASON_UNSPECIFIED,
3895                                        false, frame_buf);
3896
3897                 __cfg80211_send_deauth(sdata->dev, frame_buf,
3898                                        sizeof(frame_buf));
3899         }
3900
3901         sdata_info(sdata, "authenticate with %pM\n", req->bss->bssid);
3902
3903         err = ieee80211_prep_connection(sdata, req->bss, false);
3904         if (err)
3905                 goto err_clear;
3906
3907         err = ieee80211_probe_auth(sdata);
3908         if (err) {
3909                 sta_info_destroy_addr(sdata, req->bss->bssid);
3910                 goto err_clear;
3911         }
3912
3913         /* hold our own reference */
3914         cfg80211_ref_bss(local->hw.wiphy, auth_data->bss);
3915         err = 0;
3916         goto out_unlock;
3917
3918  err_clear:
3919         memset(ifmgd->bssid, 0, ETH_ALEN);
3920         ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
3921         ifmgd->auth_data = NULL;
3922  err_free:
3923         kfree(auth_data);
3924  out_unlock:
3925         mutex_unlock(&ifmgd->mtx);
3926
3927         return err;
3928 }
3929
3930 int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
3931                         struct cfg80211_assoc_request *req)
3932 {
3933         struct ieee80211_local *local = sdata->local;
3934         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3935         struct ieee80211_bss *bss = (void *)req->bss->priv;
3936         struct ieee80211_mgd_assoc_data *assoc_data;
3937         const struct cfg80211_bss_ies *beacon_ies;
3938         struct ieee80211_supported_band *sband;
3939         const u8 *ssidie, *ht_ie, *vht_ie;
3940         int i, err;
3941
3942         assoc_data = kzalloc(sizeof(*assoc_data) + req->ie_len, GFP_KERNEL);
3943         if (!assoc_data)
3944                 return -ENOMEM;
3945
3946         rcu_read_lock();
3947         ssidie = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
3948         if (!ssidie) {
3949                 rcu_read_unlock();
3950                 kfree(assoc_data);
3951                 return -EINVAL;
3952         }
3953         memcpy(assoc_data->ssid, ssidie + 2, ssidie[1]);
3954         assoc_data->ssid_len = ssidie[1];
3955         rcu_read_unlock();
3956
3957         mutex_lock(&ifmgd->mtx);
3958
3959         if (ifmgd->associated) {
3960                 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
3961
3962                 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
3963                                        WLAN_REASON_UNSPECIFIED,
3964                                        false, frame_buf);
3965
3966                 __cfg80211_send_deauth(sdata->dev, frame_buf,
3967                                        sizeof(frame_buf));
3968         }
3969
3970         if (ifmgd->auth_data && !ifmgd->auth_data->done) {
3971                 err = -EBUSY;
3972                 goto err_free;
3973         }
3974
3975         if (ifmgd->assoc_data) {
3976                 err = -EBUSY;
3977                 goto err_free;
3978         }
3979
3980         if (ifmgd->auth_data) {
3981                 bool match;
3982
3983                 /* keep sta info, bssid if matching */
3984                 match = ether_addr_equal(ifmgd->bssid, req->bss->bssid);
3985                 ieee80211_destroy_auth_data(sdata, match);
3986         }
3987
3988         /* prepare assoc data */
3989         
3990         ifmgd->beacon_crc_valid = false;
3991
3992         /*
3993          * IEEE802.11n does not allow TKIP/WEP as pairwise ciphers in HT mode.
3994          * We still associate in non-HT mode (11a/b/g) if any one of these
3995          * ciphers is configured as pairwise.
3996          * We can set this to true for non-11n hardware, that'll be checked
3997          * separately along with the peer capabilities.
3998          */
3999         for (i = 0; i < req->crypto.n_ciphers_pairwise; i++) {
4000                 if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
4001                     req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
4002                     req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104) {
4003                         ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
4004                         ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
4005                         netdev_info(sdata->dev,
4006                                     "disabling HT/VHT due to WEP/TKIP use\n");
4007                 }
4008         }
4009
4010         if (req->flags & ASSOC_REQ_DISABLE_HT) {
4011                 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
4012                 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
4013         }
4014
4015         if (req->flags & ASSOC_REQ_DISABLE_VHT)
4016                 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
4017
4018         /* Also disable HT if we don't support it or the AP doesn't use WMM */
4019         sband = local->hw.wiphy->bands[req->bss->channel->band];
4020         if (!sband->ht_cap.ht_supported ||
4021             local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used) {
4022                 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
4023                 if (!bss->wmm_used)
4024                         netdev_info(sdata->dev,
4025                                     "disabling HT as WMM/QoS is not supported by the AP\n");
4026         }
4027
4028         /* disable VHT if we don't support it or the AP doesn't use WMM */
4029         if (!sband->vht_cap.vht_supported ||
4030             local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used) {
4031                 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
4032                 if (!bss->wmm_used)
4033                         netdev_info(sdata->dev,
4034                                     "disabling VHT as WMM/QoS is not supported by the AP\n");
4035         }
4036
4037         memcpy(&ifmgd->ht_capa, &req->ht_capa, sizeof(ifmgd->ht_capa));
4038         memcpy(&ifmgd->ht_capa_mask, &req->ht_capa_mask,
4039                sizeof(ifmgd->ht_capa_mask));
4040
4041         memcpy(&ifmgd->vht_capa, &req->vht_capa, sizeof(ifmgd->vht_capa));
4042         memcpy(&ifmgd->vht_capa_mask, &req->vht_capa_mask,
4043                sizeof(ifmgd->vht_capa_mask));
4044
4045         if (req->ie && req->ie_len) {
4046                 memcpy(assoc_data->ie, req->ie, req->ie_len);
4047                 assoc_data->ie_len = req->ie_len;
4048         }
4049
4050         assoc_data->bss = req->bss;
4051
4052         if (ifmgd->req_smps == IEEE80211_SMPS_AUTOMATIC) {
4053                 if (ifmgd->powersave)
4054                         sdata->smps_mode = IEEE80211_SMPS_DYNAMIC;
4055                 else
4056                         sdata->smps_mode = IEEE80211_SMPS_OFF;
4057         } else
4058                 sdata->smps_mode = ifmgd->req_smps;
4059
4060         assoc_data->capability = req->bss->capability;
4061         assoc_data->wmm = bss->wmm_used &&
4062                           (local->hw.queues >= IEEE80211_NUM_ACS);
4063         assoc_data->supp_rates = bss->supp_rates;
4064         assoc_data->supp_rates_len = bss->supp_rates_len;
4065
4066         rcu_read_lock();
4067         ht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_HT_OPERATION);
4068         if (ht_ie && ht_ie[1] >= sizeof(struct ieee80211_ht_operation))
4069                 assoc_data->ap_ht_param =
4070                         ((struct ieee80211_ht_operation *)(ht_ie + 2))->ht_param;
4071         else
4072                 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
4073         vht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_VHT_CAPABILITY);
4074         if (vht_ie && vht_ie[1] >= sizeof(struct ieee80211_vht_cap))
4075                 memcpy(&assoc_data->ap_vht_cap, vht_ie + 2,
4076                        sizeof(struct ieee80211_vht_cap));
4077         else
4078                 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
4079         rcu_read_unlock();
4080
4081         if (bss->wmm_used && bss->uapsd_supported &&
4082             (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_UAPSD)) {
4083                 assoc_data->uapsd = true;
4084                 ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED;
4085         } else {
4086                 assoc_data->uapsd = false;
4087                 ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED;
4088         }
4089
4090         if (req->prev_bssid)
4091                 memcpy(assoc_data->prev_bssid, req->prev_bssid, ETH_ALEN);
4092
4093         if (req->use_mfp) {
4094                 ifmgd->mfp = IEEE80211_MFP_REQUIRED;
4095                 ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
4096         } else {
4097                 ifmgd->mfp = IEEE80211_MFP_DISABLED;
4098                 ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
4099         }
4100
4101         if (req->crypto.control_port)
4102                 ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
4103         else
4104                 ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
4105
4106         sdata->control_port_protocol = req->crypto.control_port_ethertype;
4107         sdata->control_port_no_encrypt = req->crypto.control_port_no_encrypt;
4108
4109         /* kick off associate process */
4110
4111         ifmgd->assoc_data = assoc_data;
4112         ifmgd->dtim_period = 0;
4113
4114         err = ieee80211_prep_connection(sdata, req->bss, true);
4115         if (err)
4116                 goto err_clear;
4117
4118         rcu_read_lock();
4119         beacon_ies = rcu_dereference(req->bss->beacon_ies);
4120
4121         if (sdata->local->hw.flags & IEEE80211_HW_NEED_DTIM_BEFORE_ASSOC &&
4122             !beacon_ies) {
4123                 /*
4124                  * Wait up to one beacon interval ...
4125                  * should this be more if we miss one?
4126                  */
4127                 sdata_info(sdata, "waiting for beacon from %pM\n",
4128                            ifmgd->bssid);
4129                 assoc_data->timeout = TU_TO_EXP_TIME(req->bss->beacon_interval);
4130                 assoc_data->timeout_started = true;
4131                 assoc_data->need_beacon = true;
4132         } else if (beacon_ies) {
4133                 const u8 *tim_ie = cfg80211_find_ie(WLAN_EID_TIM,
4134                                                     beacon_ies->data,
4135                                                     beacon_ies->len);
4136                 u8 dtim_count = 0;
4137
4138                 if (tim_ie && tim_ie[1] >= sizeof(struct ieee80211_tim_ie)) {
4139                         const struct ieee80211_tim_ie *tim;
4140                         tim = (void *)(tim_ie + 2);
4141                         ifmgd->dtim_period = tim->dtim_period;
4142                         dtim_count = tim->dtim_count;
4143                 }
4144                 assoc_data->have_beacon = true;
4145                 assoc_data->timeout = jiffies;
4146                 assoc_data->timeout_started = true;
4147
4148                 if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
4149                         sdata->vif.bss_conf.sync_tsf = beacon_ies->tsf;
4150                         sdata->vif.bss_conf.sync_device_ts =
4151                                 bss->device_ts_beacon;
4152                         sdata->vif.bss_conf.sync_dtim_count = dtim_count;
4153                 }
4154         } else {
4155                 assoc_data->timeout = jiffies;
4156                 assoc_data->timeout_started = true;
4157         }
4158         rcu_read_unlock();
4159
4160         run_again(ifmgd, assoc_data->timeout);
4161
4162         if (bss->corrupt_data) {
4163                 char *corrupt_type = "data";
4164                 if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_BEACON) {
4165                         if (bss->corrupt_data &
4166                                         IEEE80211_BSS_CORRUPT_PROBE_RESP)
4167                                 corrupt_type = "beacon and probe response";
4168                         else
4169                                 corrupt_type = "beacon";
4170                 } else if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_PROBE_RESP)
4171                         corrupt_type = "probe response";
4172                 sdata_info(sdata, "associating with AP with corrupt %s\n",
4173                            corrupt_type);
4174         }
4175
4176         err = 0;
4177         goto out;
4178  err_clear:
4179         memset(ifmgd->bssid, 0, ETH_ALEN);
4180         ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
4181         ifmgd->assoc_data = NULL;
4182  err_free:
4183         kfree(assoc_data);
4184  out:
4185         mutex_unlock(&ifmgd->mtx);
4186
4187         return err;
4188 }
4189
4190 int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
4191                          struct cfg80211_deauth_request *req)
4192 {
4193         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4194         u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
4195         bool tx = !req->local_state_change;
4196         bool sent_frame = false;
4197
4198         mutex_lock(&ifmgd->mtx);
4199
4200         sdata_info(sdata,
4201                    "deauthenticating from %pM by local choice (reason=%d)\n",
4202                    req->bssid, req->reason_code);
4203
4204         if (ifmgd->auth_data) {
4205                 drv_mgd_prepare_tx(sdata->local, sdata);
4206                 ieee80211_send_deauth_disassoc(sdata, req->bssid,
4207                                                IEEE80211_STYPE_DEAUTH,
4208                                                req->reason_code, tx,
4209                                                frame_buf);
4210                 ieee80211_destroy_auth_data(sdata, false);
4211                 mutex_unlock(&ifmgd->mtx);
4212
4213                 sent_frame = tx;
4214                 goto out;
4215         }
4216
4217         if (ifmgd->associated &&
4218             ether_addr_equal(ifmgd->associated->bssid, req->bssid)) {
4219                 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
4220                                        req->reason_code, tx, frame_buf);
4221                 sent_frame = tx;
4222         }
4223         mutex_unlock(&ifmgd->mtx);
4224
4225  out:
4226         if (sent_frame)
4227                 __cfg80211_send_deauth(sdata->dev, frame_buf,
4228                                        IEEE80211_DEAUTH_FRAME_LEN);
4229
4230         return 0;
4231 }
4232
4233 int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
4234                            struct cfg80211_disassoc_request *req)
4235 {
4236         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4237         u8 bssid[ETH_ALEN];
4238         u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
4239
4240         mutex_lock(&ifmgd->mtx);
4241
4242         /*
4243          * cfg80211 should catch this ... but it's racy since
4244          * we can receive a disassoc frame, process it, hand it
4245          * to cfg80211 while that's in a locked section already
4246          * trying to tell us that the user wants to disconnect.
4247          */
4248         if (ifmgd->associated != req->bss) {
4249                 mutex_unlock(&ifmgd->mtx);
4250                 return -ENOLINK;
4251         }
4252
4253         sdata_info(sdata,
4254                    "disassociating from %pM by local choice (reason=%d)\n",
4255                    req->bss->bssid, req->reason_code);
4256
4257         memcpy(bssid, req->bss->bssid, ETH_ALEN);
4258         ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DISASSOC,
4259                                req->reason_code, !req->local_state_change,
4260                                frame_buf);
4261         mutex_unlock(&ifmgd->mtx);
4262
4263         __cfg80211_send_disassoc(sdata->dev, frame_buf,
4264                                  IEEE80211_DEAUTH_FRAME_LEN);
4265
4266         return 0;
4267 }
4268
4269 void ieee80211_mgd_stop(struct ieee80211_sub_if_data *sdata)
4270 {
4271         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4272
4273         /*
4274          * Make sure some work items will not run after this,
4275          * they will not do anything but might not have been
4276          * cancelled when disconnecting.
4277          */
4278         cancel_work_sync(&ifmgd->monitor_work);
4279         cancel_work_sync(&ifmgd->beacon_connection_loss_work);
4280         cancel_work_sync(&ifmgd->request_smps_work);
4281         cancel_work_sync(&ifmgd->csa_connection_drop_work);
4282         cancel_work_sync(&ifmgd->chswitch_work);
4283
4284         mutex_lock(&ifmgd->mtx);
4285         if (ifmgd->assoc_data)
4286                 ieee80211_destroy_assoc_data(sdata, false);
4287         if (ifmgd->auth_data)
4288                 ieee80211_destroy_auth_data(sdata, false);
4289         del_timer_sync(&ifmgd->timer);
4290         mutex_unlock(&ifmgd->mtx);
4291 }
4292
4293 void ieee80211_cqm_rssi_notify(struct ieee80211_vif *vif,
4294                                enum nl80211_cqm_rssi_threshold_event rssi_event,
4295                                gfp_t gfp)
4296 {
4297         struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4298
4299         trace_api_cqm_rssi_notify(sdata, rssi_event);
4300
4301         cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, gfp);
4302 }
4303 EXPORT_SYMBOL(ieee80211_cqm_rssi_notify);