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