]> asedeno.scripts.mit.edu Git - linux.git/blob - net/mac80211/mlme.c
mac80211: notify driver on subsequent CSA beacons
[linux.git] / net / mac80211 / mlme.c
1 /*
2  * BSS client mode implementation
3  * Copyright 2003-2008, Jouni Malinen <j@w1.fi>
4  * Copyright 2004, Instant802 Networks, Inc.
5  * Copyright 2005, Devicescape Software, Inc.
6  * Copyright 2006-2007  Jiri Benc <jbenc@suse.cz>
7  * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
8  * Copyright 2013-2014  Intel Mobile Communications GmbH
9  * Copyright (C) 2015 - 2017 Intel Deutschland GmbH
10  * Copyright (C) 2018 - 2019 Intel Corporation
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License version 2 as
14  * published by the Free Software Foundation.
15  */
16
17 #include <linux/delay.h>
18 #include <linux/if_ether.h>
19 #include <linux/skbuff.h>
20 #include <linux/if_arp.h>
21 #include <linux/etherdevice.h>
22 #include <linux/moduleparam.h>
23 #include <linux/rtnetlink.h>
24 #include <linux/crc32.h>
25 #include <linux/slab.h>
26 #include <linux/export.h>
27 #include <net/mac80211.h>
28 #include <asm/unaligned.h>
29
30 #include "ieee80211_i.h"
31 #include "driver-ops.h"
32 #include "rate.h"
33 #include "led.h"
34 #include "fils_aead.h"
35
36 #define IEEE80211_AUTH_TIMEOUT          (HZ / 5)
37 #define IEEE80211_AUTH_TIMEOUT_LONG     (HZ / 2)
38 #define IEEE80211_AUTH_TIMEOUT_SHORT    (HZ / 10)
39 #define IEEE80211_AUTH_TIMEOUT_SAE      (HZ * 2)
40 #define IEEE80211_AUTH_MAX_TRIES        3
41 #define IEEE80211_AUTH_WAIT_ASSOC       (HZ * 5)
42 #define IEEE80211_ASSOC_TIMEOUT         (HZ / 5)
43 #define IEEE80211_ASSOC_TIMEOUT_LONG    (HZ / 2)
44 #define IEEE80211_ASSOC_TIMEOUT_SHORT   (HZ / 10)
45 #define IEEE80211_ASSOC_MAX_TRIES       3
46
47 static int max_nullfunc_tries = 2;
48 module_param(max_nullfunc_tries, int, 0644);
49 MODULE_PARM_DESC(max_nullfunc_tries,
50                  "Maximum nullfunc tx tries before disconnecting (reason 4).");
51
52 static int max_probe_tries = 5;
53 module_param(max_probe_tries, int, 0644);
54 MODULE_PARM_DESC(max_probe_tries,
55                  "Maximum probe tries before disconnecting (reason 4).");
56
57 /*
58  * Beacon loss timeout is calculated as N frames times the
59  * advertised beacon interval.  This may need to be somewhat
60  * higher than what hardware might detect to account for
61  * delays in the host processing frames. But since we also
62  * probe on beacon miss before declaring the connection lost
63  * default to what we want.
64  */
65 static int beacon_loss_count = 7;
66 module_param(beacon_loss_count, int, 0644);
67 MODULE_PARM_DESC(beacon_loss_count,
68                  "Number of beacon intervals before we decide beacon was lost.");
69
70 /*
71  * Time the connection can be idle before we probe
72  * it to see if we can still talk to the AP.
73  */
74 #define IEEE80211_CONNECTION_IDLE_TIME  (30 * HZ)
75 /*
76  * Time we wait for a probe response after sending
77  * a probe request because of beacon loss or for
78  * checking the connection still works.
79  */
80 static int probe_wait_ms = 500;
81 module_param(probe_wait_ms, int, 0644);
82 MODULE_PARM_DESC(probe_wait_ms,
83                  "Maximum time(ms) to wait for probe response"
84                  " before disconnecting (reason 4).");
85
86 /*
87  * How many Beacon frames need to have been used in average signal strength
88  * before starting to indicate signal change events.
89  */
90 #define IEEE80211_SIGNAL_AVE_MIN_COUNT  4
91
92 /*
93  * We can have multiple work items (and connection probing)
94  * scheduling this timer, but we need to take care to only
95  * reschedule it when it should fire _earlier_ than it was
96  * asked for before, or if it's not pending right now. This
97  * function ensures that. Note that it then is required to
98  * run this function for all timeouts after the first one
99  * has happened -- the work that runs from this timer will
100  * do that.
101  */
102 static void run_again(struct ieee80211_sub_if_data *sdata,
103                       unsigned long timeout)
104 {
105         sdata_assert_lock(sdata);
106
107         if (!timer_pending(&sdata->u.mgd.timer) ||
108             time_before(timeout, sdata->u.mgd.timer.expires))
109                 mod_timer(&sdata->u.mgd.timer, timeout);
110 }
111
112 void ieee80211_sta_reset_beacon_monitor(struct ieee80211_sub_if_data *sdata)
113 {
114         if (sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)
115                 return;
116
117         if (ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
118                 return;
119
120         mod_timer(&sdata->u.mgd.bcn_mon_timer,
121                   round_jiffies_up(jiffies + sdata->u.mgd.beacon_timeout));
122 }
123
124 void ieee80211_sta_reset_conn_monitor(struct ieee80211_sub_if_data *sdata)
125 {
126         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
127
128         if (unlikely(!ifmgd->associated))
129                 return;
130
131         if (ifmgd->probe_send_count)
132                 ifmgd->probe_send_count = 0;
133
134         if (ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
135                 return;
136
137         mod_timer(&ifmgd->conn_mon_timer,
138                   round_jiffies_up(jiffies + IEEE80211_CONNECTION_IDLE_TIME));
139 }
140
141 static int ecw2cw(int ecw)
142 {
143         return (1 << ecw) - 1;
144 }
145
146 static u32
147 ieee80211_determine_chantype(struct ieee80211_sub_if_data *sdata,
148                              struct ieee80211_supported_band *sband,
149                              struct ieee80211_channel *channel,
150                              const struct ieee80211_ht_operation *ht_oper,
151                              const struct ieee80211_vht_operation *vht_oper,
152                              const struct ieee80211_he_operation *he_oper,
153                              struct cfg80211_chan_def *chandef, bool tracking)
154 {
155         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
156         struct cfg80211_chan_def vht_chandef;
157         struct ieee80211_sta_ht_cap sta_ht_cap;
158         u32 ht_cfreq, ret;
159
160         memcpy(&sta_ht_cap, &sband->ht_cap, sizeof(sta_ht_cap));
161         ieee80211_apply_htcap_overrides(sdata, &sta_ht_cap);
162
163         chandef->chan = channel;
164         chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
165         chandef->center_freq1 = channel->center_freq;
166         chandef->center_freq2 = 0;
167
168         if (!ht_oper || !sta_ht_cap.ht_supported) {
169                 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
170                 goto out;
171         }
172
173         chandef->width = NL80211_CHAN_WIDTH_20;
174
175         ht_cfreq = ieee80211_channel_to_frequency(ht_oper->primary_chan,
176                                                   channel->band);
177         /* check that channel matches the right operating channel */
178         if (!tracking && channel->center_freq != ht_cfreq) {
179                 /*
180                  * It's possible that some APs are confused here;
181                  * Netgear WNDR3700 sometimes reports 4 higher than
182                  * the actual channel in association responses, but
183                  * since we look at probe response/beacon data here
184                  * it should be OK.
185                  */
186                 sdata_info(sdata,
187                            "Wrong control channel: center-freq: %d ht-cfreq: %d ht->primary_chan: %d band: %d - Disabling HT\n",
188                            channel->center_freq, ht_cfreq,
189                            ht_oper->primary_chan, channel->band);
190                 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
191                 goto out;
192         }
193
194         /* check 40 MHz support, if we have it */
195         if (sta_ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) {
196                 ieee80211_chandef_ht_oper(ht_oper, chandef);
197         } else {
198                 /* 40 MHz (and 80 MHz) must be supported for VHT */
199                 ret = IEEE80211_STA_DISABLE_VHT;
200                 /* also mark 40 MHz disabled */
201                 ret |= IEEE80211_STA_DISABLE_40MHZ;
202                 goto out;
203         }
204
205         if (!vht_oper || !sband->vht_cap.vht_supported) {
206                 ret = IEEE80211_STA_DISABLE_VHT;
207                 goto out;
208         }
209
210         vht_chandef = *chandef;
211         if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE) && he_oper &&
212             (le32_to_cpu(he_oper->he_oper_params) &
213              IEEE80211_HE_OPERATION_VHT_OPER_INFO)) {
214                 struct ieee80211_vht_operation he_oper_vht_cap;
215
216                 /*
217                  * Set only first 3 bytes (other 2 aren't used in
218                  * ieee80211_chandef_vht_oper() anyway)
219                  */
220                 memcpy(&he_oper_vht_cap, he_oper->optional, 3);
221                 he_oper_vht_cap.basic_mcs_set = cpu_to_le16(0);
222
223                 if (!ieee80211_chandef_vht_oper(&sdata->local->hw,
224                                                 &he_oper_vht_cap, ht_oper,
225                                                 &vht_chandef)) {
226                         if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE))
227                                 sdata_info(sdata,
228                                            "HE AP VHT information is invalid, disable HE\n");
229                         ret = IEEE80211_STA_DISABLE_HE;
230                         goto out;
231                 }
232         } else if (!ieee80211_chandef_vht_oper(&sdata->local->hw, vht_oper,
233                                                ht_oper, &vht_chandef)) {
234                 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
235                         sdata_info(sdata,
236                                    "AP VHT information is invalid, disable VHT\n");
237                 ret = IEEE80211_STA_DISABLE_VHT;
238                 goto out;
239         }
240
241         if (!cfg80211_chandef_valid(&vht_chandef)) {
242                 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
243                         sdata_info(sdata,
244                                    "AP VHT information is invalid, disable VHT\n");
245                 ret = IEEE80211_STA_DISABLE_VHT;
246                 goto out;
247         }
248
249         if (cfg80211_chandef_identical(chandef, &vht_chandef)) {
250                 ret = 0;
251                 goto out;
252         }
253
254         if (!cfg80211_chandef_compatible(chandef, &vht_chandef)) {
255                 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
256                         sdata_info(sdata,
257                                    "AP VHT information doesn't match HT, disable VHT\n");
258                 ret = IEEE80211_STA_DISABLE_VHT;
259                 goto out;
260         }
261
262         *chandef = vht_chandef;
263
264         ret = 0;
265
266 out:
267         /*
268          * When tracking the current AP, don't do any further checks if the
269          * new chandef is identical to the one we're currently using for the
270          * connection. This keeps us from playing ping-pong with regulatory,
271          * without it the following can happen (for example):
272          *  - connect to an AP with 80 MHz, world regdom allows 80 MHz
273          *  - AP advertises regdom US
274          *  - CRDA loads regdom US with 80 MHz prohibited (old database)
275          *  - the code below detects an unsupported channel, downgrades, and
276          *    we disconnect from the AP in the caller
277          *  - disconnect causes CRDA to reload world regdomain and the game
278          *    starts anew.
279          * (see https://bugzilla.kernel.org/show_bug.cgi?id=70881)
280          *
281          * It seems possible that there are still scenarios with CSA or real
282          * bandwidth changes where a this could happen, but those cases are
283          * less common and wouldn't completely prevent using the AP.
284          */
285         if (tracking &&
286             cfg80211_chandef_identical(chandef, &sdata->vif.bss_conf.chandef))
287                 return ret;
288
289         /* don't print the message below for VHT mismatch if VHT is disabled */
290         if (ret & IEEE80211_STA_DISABLE_VHT)
291                 vht_chandef = *chandef;
292
293         /*
294          * Ignore the DISABLED flag when we're already connected and only
295          * tracking the APs beacon for bandwidth changes - otherwise we
296          * might get disconnected here if we connect to an AP, update our
297          * regulatory information based on the AP's country IE and the
298          * information we have is wrong/outdated and disables the channel
299          * that we're actually using for the connection to the AP.
300          */
301         while (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
302                                         tracking ? 0 :
303                                                    IEEE80211_CHAN_DISABLED)) {
304                 if (WARN_ON(chandef->width == NL80211_CHAN_WIDTH_20_NOHT)) {
305                         ret = IEEE80211_STA_DISABLE_HT |
306                               IEEE80211_STA_DISABLE_VHT;
307                         break;
308                 }
309
310                 ret |= ieee80211_chandef_downgrade(chandef);
311         }
312
313         if (chandef->width != vht_chandef.width && !tracking)
314                 sdata_info(sdata,
315                            "capabilities/regulatory prevented using AP HT/VHT configuration, downgraded\n");
316
317         WARN_ON_ONCE(!cfg80211_chandef_valid(chandef));
318         return ret;
319 }
320
321 static int ieee80211_config_bw(struct ieee80211_sub_if_data *sdata,
322                                struct sta_info *sta,
323                                const struct ieee80211_ht_cap *ht_cap,
324                                const struct ieee80211_ht_operation *ht_oper,
325                                const struct ieee80211_vht_operation *vht_oper,
326                                const struct ieee80211_he_operation *he_oper,
327                                const u8 *bssid, u32 *changed)
328 {
329         struct ieee80211_local *local = sdata->local;
330         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
331         struct ieee80211_channel *chan = sdata->vif.bss_conf.chandef.chan;
332         struct ieee80211_supported_band *sband =
333                 local->hw.wiphy->bands[chan->band];
334         struct cfg80211_chan_def chandef;
335         u16 ht_opmode;
336         u32 flags;
337         enum ieee80211_sta_rx_bandwidth new_sta_bw;
338         int ret;
339
340         /* if HT was/is disabled, don't track any bandwidth changes */
341         if (ifmgd->flags & IEEE80211_STA_DISABLE_HT || !ht_oper)
342                 return 0;
343
344         /* don't check VHT if we associated as non-VHT station */
345         if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
346                 vht_oper = NULL;
347
348         /* don't check HE if we associated as non-HE station */
349         if (ifmgd->flags & IEEE80211_STA_DISABLE_HE ||
350             !ieee80211_get_he_sta_cap(sband))
351                 he_oper = NULL;
352
353         if (WARN_ON_ONCE(!sta))
354                 return -EINVAL;
355
356         /*
357          * if bss configuration changed store the new one -
358          * this may be applicable even if channel is identical
359          */
360         ht_opmode = le16_to_cpu(ht_oper->operation_mode);
361         if (sdata->vif.bss_conf.ht_operation_mode != ht_opmode) {
362                 *changed |= BSS_CHANGED_HT;
363                 sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
364         }
365
366         /* calculate new channel (type) based on HT/VHT/HE operation IEs */
367         flags = ieee80211_determine_chantype(sdata, sband, chan,
368                                              ht_oper, vht_oper, he_oper,
369                                              &chandef, true);
370
371         /*
372          * Downgrade the new channel if we associated with restricted
373          * capabilities. For example, if we associated as a 20 MHz STA
374          * to a 40 MHz AP (due to regulatory, capabilities or config
375          * reasons) then switching to a 40 MHz channel now won't do us
376          * any good -- we couldn't use it with the AP.
377          */
378         if (ifmgd->flags & IEEE80211_STA_DISABLE_80P80MHZ &&
379             chandef.width == NL80211_CHAN_WIDTH_80P80)
380                 flags |= ieee80211_chandef_downgrade(&chandef);
381         if (ifmgd->flags & IEEE80211_STA_DISABLE_160MHZ &&
382             chandef.width == NL80211_CHAN_WIDTH_160)
383                 flags |= ieee80211_chandef_downgrade(&chandef);
384         if (ifmgd->flags & IEEE80211_STA_DISABLE_40MHZ &&
385             chandef.width > NL80211_CHAN_WIDTH_20)
386                 flags |= ieee80211_chandef_downgrade(&chandef);
387
388         if (cfg80211_chandef_identical(&chandef, &sdata->vif.bss_conf.chandef))
389                 return 0;
390
391         sdata_info(sdata,
392                    "AP %pM changed bandwidth, new config is %d MHz, width %d (%d/%d MHz)\n",
393                    ifmgd->bssid, chandef.chan->center_freq, chandef.width,
394                    chandef.center_freq1, chandef.center_freq2);
395
396         if (flags != (ifmgd->flags & (IEEE80211_STA_DISABLE_HT |
397                                       IEEE80211_STA_DISABLE_VHT |
398                                       IEEE80211_STA_DISABLE_40MHZ |
399                                       IEEE80211_STA_DISABLE_80P80MHZ |
400                                       IEEE80211_STA_DISABLE_160MHZ)) ||
401             !cfg80211_chandef_valid(&chandef)) {
402                 sdata_info(sdata,
403                            "AP %pM changed bandwidth in a way we can't support - disconnect\n",
404                            ifmgd->bssid);
405                 return -EINVAL;
406         }
407
408         switch (chandef.width) {
409         case NL80211_CHAN_WIDTH_20_NOHT:
410         case NL80211_CHAN_WIDTH_20:
411                 new_sta_bw = IEEE80211_STA_RX_BW_20;
412                 break;
413         case NL80211_CHAN_WIDTH_40:
414                 new_sta_bw = IEEE80211_STA_RX_BW_40;
415                 break;
416         case NL80211_CHAN_WIDTH_80:
417                 new_sta_bw = IEEE80211_STA_RX_BW_80;
418                 break;
419         case NL80211_CHAN_WIDTH_80P80:
420         case NL80211_CHAN_WIDTH_160:
421                 new_sta_bw = IEEE80211_STA_RX_BW_160;
422                 break;
423         default:
424                 return -EINVAL;
425         }
426
427         if (new_sta_bw > sta->cur_max_bandwidth)
428                 new_sta_bw = sta->cur_max_bandwidth;
429
430         if (new_sta_bw < sta->sta.bandwidth) {
431                 sta->sta.bandwidth = new_sta_bw;
432                 rate_control_rate_update(local, sband, sta,
433                                          IEEE80211_RC_BW_CHANGED);
434         }
435
436         ret = ieee80211_vif_change_bandwidth(sdata, &chandef, changed);
437         if (ret) {
438                 sdata_info(sdata,
439                            "AP %pM changed bandwidth to incompatible one - disconnect\n",
440                            ifmgd->bssid);
441                 return ret;
442         }
443
444         if (new_sta_bw > sta->sta.bandwidth) {
445                 sta->sta.bandwidth = new_sta_bw;
446                 rate_control_rate_update(local, sband, sta,
447                                          IEEE80211_RC_BW_CHANGED);
448         }
449
450         return 0;
451 }
452
453 /* frame sending functions */
454
455 static void ieee80211_add_ht_ie(struct ieee80211_sub_if_data *sdata,
456                                 struct sk_buff *skb, u8 ap_ht_param,
457                                 struct ieee80211_supported_band *sband,
458                                 struct ieee80211_channel *channel,
459                                 enum ieee80211_smps_mode smps)
460 {
461         u8 *pos;
462         u32 flags = channel->flags;
463         u16 cap;
464         struct ieee80211_sta_ht_cap ht_cap;
465
466         BUILD_BUG_ON(sizeof(ht_cap) != sizeof(sband->ht_cap));
467
468         memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
469         ieee80211_apply_htcap_overrides(sdata, &ht_cap);
470
471         /* determine capability flags */
472         cap = ht_cap.cap;
473
474         switch (ap_ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
475         case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
476                 if (flags & IEEE80211_CHAN_NO_HT40PLUS) {
477                         cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
478                         cap &= ~IEEE80211_HT_CAP_SGI_40;
479                 }
480                 break;
481         case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
482                 if (flags & IEEE80211_CHAN_NO_HT40MINUS) {
483                         cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
484                         cap &= ~IEEE80211_HT_CAP_SGI_40;
485                 }
486                 break;
487         }
488
489         /*
490          * If 40 MHz was disabled associate as though we weren't
491          * capable of 40 MHz -- some broken APs will never fall
492          * back to trying to transmit in 20 MHz.
493          */
494         if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_40MHZ) {
495                 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
496                 cap &= ~IEEE80211_HT_CAP_SGI_40;
497         }
498
499         /* set SM PS mode properly */
500         cap &= ~IEEE80211_HT_CAP_SM_PS;
501         switch (smps) {
502         case IEEE80211_SMPS_AUTOMATIC:
503         case IEEE80211_SMPS_NUM_MODES:
504                 WARN_ON(1);
505                 /* fall through */
506         case IEEE80211_SMPS_OFF:
507                 cap |= WLAN_HT_CAP_SM_PS_DISABLED <<
508                         IEEE80211_HT_CAP_SM_PS_SHIFT;
509                 break;
510         case IEEE80211_SMPS_STATIC:
511                 cap |= WLAN_HT_CAP_SM_PS_STATIC <<
512                         IEEE80211_HT_CAP_SM_PS_SHIFT;
513                 break;
514         case IEEE80211_SMPS_DYNAMIC:
515                 cap |= WLAN_HT_CAP_SM_PS_DYNAMIC <<
516                         IEEE80211_HT_CAP_SM_PS_SHIFT;
517                 break;
518         }
519
520         /* reserve and fill IE */
521         pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
522         ieee80211_ie_build_ht_cap(pos, &ht_cap, cap);
523 }
524
525 /* This function determines vht capability flags for the association
526  * and builds the IE.
527  * Note - the function may set the owner of the MU-MIMO capability
528  */
529 static void ieee80211_add_vht_ie(struct ieee80211_sub_if_data *sdata,
530                                  struct sk_buff *skb,
531                                  struct ieee80211_supported_band *sband,
532                                  struct ieee80211_vht_cap *ap_vht_cap)
533 {
534         struct ieee80211_local *local = sdata->local;
535         u8 *pos;
536         u32 cap;
537         struct ieee80211_sta_vht_cap vht_cap;
538         u32 mask, ap_bf_sts, our_bf_sts;
539
540         BUILD_BUG_ON(sizeof(vht_cap) != sizeof(sband->vht_cap));
541
542         memcpy(&vht_cap, &sband->vht_cap, sizeof(vht_cap));
543         ieee80211_apply_vhtcap_overrides(sdata, &vht_cap);
544
545         /* determine capability flags */
546         cap = vht_cap.cap;
547
548         if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_80P80MHZ) {
549                 u32 bw = cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
550
551                 cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
552                 if (bw == IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ ||
553                     bw == IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ)
554                         cap |= IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
555         }
556
557         if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_160MHZ) {
558                 cap &= ~IEEE80211_VHT_CAP_SHORT_GI_160;
559                 cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
560         }
561
562         /*
563          * Some APs apparently get confused if our capabilities are better
564          * than theirs, so restrict what we advertise in the assoc request.
565          */
566         if (!(ap_vht_cap->vht_cap_info &
567                         cpu_to_le32(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)))
568                 cap &= ~(IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
569                          IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE);
570         else if (!(ap_vht_cap->vht_cap_info &
571                         cpu_to_le32(IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)))
572                 cap &= ~IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE;
573
574         /*
575          * If some other vif is using the MU-MIMO capablity we cannot associate
576          * using MU-MIMO - this will lead to contradictions in the group-id
577          * mechanism.
578          * Ownership is defined since association request, in order to avoid
579          * simultaneous associations with MU-MIMO.
580          */
581         if (cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE) {
582                 bool disable_mu_mimo = false;
583                 struct ieee80211_sub_if_data *other;
584
585                 list_for_each_entry_rcu(other, &local->interfaces, list) {
586                         if (other->vif.mu_mimo_owner) {
587                                 disable_mu_mimo = true;
588                                 break;
589                         }
590                 }
591                 if (disable_mu_mimo)
592                         cap &= ~IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE;
593                 else
594                         sdata->vif.mu_mimo_owner = true;
595         }
596
597         mask = IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
598
599         ap_bf_sts = le32_to_cpu(ap_vht_cap->vht_cap_info) & mask;
600         our_bf_sts = cap & mask;
601
602         if (ap_bf_sts < our_bf_sts) {
603                 cap &= ~mask;
604                 cap |= ap_bf_sts;
605         }
606
607         /* reserve and fill IE */
608         pos = skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2);
609         ieee80211_ie_build_vht_cap(pos, &vht_cap, cap);
610 }
611
612 /* This function determines HE capability flags for the association
613  * and builds the IE.
614  */
615 static void ieee80211_add_he_ie(struct ieee80211_sub_if_data *sdata,
616                                 struct sk_buff *skb,
617                                 struct ieee80211_supported_band *sband)
618 {
619         u8 *pos;
620         const struct ieee80211_sta_he_cap *he_cap = NULL;
621         u8 he_cap_size;
622
623         he_cap = ieee80211_get_he_sta_cap(sband);
624         if (!he_cap)
625                 return;
626
627         /*
628          * TODO: the 1 added is because this temporarily is under the EXTENSION
629          * IE. Get rid of it when it moves.
630          */
631         he_cap_size =
632                 2 + 1 + sizeof(he_cap->he_cap_elem) +
633                 ieee80211_he_mcs_nss_size(&he_cap->he_cap_elem) +
634                 ieee80211_he_ppe_size(he_cap->ppe_thres[0],
635                                       he_cap->he_cap_elem.phy_cap_info);
636         pos = skb_put(skb, he_cap_size);
637         ieee80211_ie_build_he_cap(pos, he_cap, pos + he_cap_size);
638 }
639
640 static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata)
641 {
642         struct ieee80211_local *local = sdata->local;
643         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
644         struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
645         struct sk_buff *skb;
646         struct ieee80211_mgmt *mgmt;
647         u8 *pos, qos_info, *ie_start;
648         size_t offset = 0, noffset;
649         int i, count, rates_len, supp_rates_len, shift;
650         u16 capab;
651         struct ieee80211_supported_band *sband;
652         struct ieee80211_chanctx_conf *chanctx_conf;
653         struct ieee80211_channel *chan;
654         u32 rates = 0;
655
656         sdata_assert_lock(sdata);
657
658         rcu_read_lock();
659         chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
660         if (WARN_ON(!chanctx_conf)) {
661                 rcu_read_unlock();
662                 return;
663         }
664         chan = chanctx_conf->def.chan;
665         rcu_read_unlock();
666         sband = local->hw.wiphy->bands[chan->band];
667         shift = ieee80211_vif_get_shift(&sdata->vif);
668
669         if (assoc_data->supp_rates_len) {
670                 /*
671                  * Get all rates supported by the device and the AP as
672                  * some APs don't like getting a superset of their rates
673                  * in the association request (e.g. D-Link DAP 1353 in
674                  * b-only mode)...
675                  */
676                 rates_len = ieee80211_parse_bitrates(&chanctx_conf->def, sband,
677                                                      assoc_data->supp_rates,
678                                                      assoc_data->supp_rates_len,
679                                                      &rates);
680         } else {
681                 /*
682                  * In case AP not provide any supported rates information
683                  * before association, we send information element(s) with
684                  * all rates that we support.
685                  */
686                 rates_len = 0;
687                 for (i = 0; i < sband->n_bitrates; i++) {
688                         rates |= BIT(i);
689                         rates_len++;
690                 }
691         }
692
693         skb = alloc_skb(local->hw.extra_tx_headroom +
694                         sizeof(*mgmt) + /* bit too much but doesn't matter */
695                         2 + assoc_data->ssid_len + /* SSID */
696                         4 + rates_len + /* (extended) rates */
697                         4 + /* power capability */
698                         2 + 2 * sband->n_channels + /* supported channels */
699                         2 + sizeof(struct ieee80211_ht_cap) + /* HT */
700                         2 + sizeof(struct ieee80211_vht_cap) + /* VHT */
701                         2 + 1 + sizeof(struct ieee80211_he_cap_elem) + /* HE */
702                                 sizeof(struct ieee80211_he_mcs_nss_supp) +
703                                 IEEE80211_HE_PPE_THRES_MAX_LEN +
704                         assoc_data->ie_len + /* extra IEs */
705                         (assoc_data->fils_kek_len ? 16 /* AES-SIV */ : 0) +
706                         9, /* WMM */
707                         GFP_KERNEL);
708         if (!skb)
709                 return;
710
711         skb_reserve(skb, local->hw.extra_tx_headroom);
712
713         capab = WLAN_CAPABILITY_ESS;
714
715         if (sband->band == NL80211_BAND_2GHZ) {
716                 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
717                 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
718         }
719
720         if (assoc_data->capability & WLAN_CAPABILITY_PRIVACY)
721                 capab |= WLAN_CAPABILITY_PRIVACY;
722
723         if ((assoc_data->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
724             ieee80211_hw_check(&local->hw, SPECTRUM_MGMT))
725                 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
726
727         if (ifmgd->flags & IEEE80211_STA_ENABLE_RRM)
728                 capab |= WLAN_CAPABILITY_RADIO_MEASURE;
729
730         mgmt = skb_put_zero(skb, 24);
731         memcpy(mgmt->da, assoc_data->bss->bssid, ETH_ALEN);
732         memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
733         memcpy(mgmt->bssid, assoc_data->bss->bssid, ETH_ALEN);
734
735         if (!is_zero_ether_addr(assoc_data->prev_bssid)) {
736                 skb_put(skb, 10);
737                 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
738                                                   IEEE80211_STYPE_REASSOC_REQ);
739                 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
740                 mgmt->u.reassoc_req.listen_interval =
741                                 cpu_to_le16(local->hw.conf.listen_interval);
742                 memcpy(mgmt->u.reassoc_req.current_ap, assoc_data->prev_bssid,
743                        ETH_ALEN);
744         } else {
745                 skb_put(skb, 4);
746                 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
747                                                   IEEE80211_STYPE_ASSOC_REQ);
748                 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
749                 mgmt->u.assoc_req.listen_interval =
750                                 cpu_to_le16(local->hw.conf.listen_interval);
751         }
752
753         /* SSID */
754         pos = skb_put(skb, 2 + assoc_data->ssid_len);
755         ie_start = pos;
756         *pos++ = WLAN_EID_SSID;
757         *pos++ = assoc_data->ssid_len;
758         memcpy(pos, assoc_data->ssid, assoc_data->ssid_len);
759
760         /* add all rates which were marked to be used above */
761         supp_rates_len = rates_len;
762         if (supp_rates_len > 8)
763                 supp_rates_len = 8;
764
765         pos = skb_put(skb, supp_rates_len + 2);
766         *pos++ = WLAN_EID_SUPP_RATES;
767         *pos++ = supp_rates_len;
768
769         count = 0;
770         for (i = 0; i < sband->n_bitrates; i++) {
771                 if (BIT(i) & rates) {
772                         int rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
773                                                 5 * (1 << shift));
774                         *pos++ = (u8) rate;
775                         if (++count == 8)
776                                 break;
777                 }
778         }
779
780         if (rates_len > count) {
781                 pos = skb_put(skb, rates_len - count + 2);
782                 *pos++ = WLAN_EID_EXT_SUPP_RATES;
783                 *pos++ = rates_len - count;
784
785                 for (i++; i < sband->n_bitrates; i++) {
786                         if (BIT(i) & rates) {
787                                 int rate;
788                                 rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
789                                                     5 * (1 << shift));
790                                 *pos++ = (u8) rate;
791                         }
792                 }
793         }
794
795         if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT ||
796             capab & WLAN_CAPABILITY_RADIO_MEASURE) {
797                 pos = skb_put(skb, 4);
798                 *pos++ = WLAN_EID_PWR_CAPABILITY;
799                 *pos++ = 2;
800                 *pos++ = 0; /* min tx power */
801                  /* max tx power */
802                 *pos++ = ieee80211_chandef_max_power(&chanctx_conf->def);
803         }
804
805         if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
806                 /* TODO: get this in reg domain format */
807                 pos = skb_put(skb, 2 * sband->n_channels + 2);
808                 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
809                 *pos++ = 2 * sband->n_channels;
810                 for (i = 0; i < sband->n_channels; i++) {
811                         *pos++ = ieee80211_frequency_to_channel(
812                                         sband->channels[i].center_freq);
813                         *pos++ = 1; /* one channel in the subband*/
814                 }
815         }
816
817         /* Set MBSSID support for HE AP if needed */
818         if (ieee80211_hw_check(&local->hw, SUPPORTS_ONLY_HE_MULTI_BSSID) &&
819             !(ifmgd->flags & IEEE80211_STA_DISABLE_HE) && assoc_data->ie_len) {
820                 struct element *elem;
821
822                 /* we know it's writable, cast away the const */
823                 elem = (void *)cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY,
824                                                   assoc_data->ie,
825                                                   assoc_data->ie_len);
826
827                 /* We can probably assume both always true */
828                 if (elem && elem->datalen >= 3)
829                         elem->data[2] |= WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT;
830         }
831
832         /* if present, add any custom IEs that go before HT */
833         if (assoc_data->ie_len) {
834                 static const u8 before_ht[] = {
835                         WLAN_EID_SSID,
836                         WLAN_EID_SUPP_RATES,
837                         WLAN_EID_EXT_SUPP_RATES,
838                         WLAN_EID_PWR_CAPABILITY,
839                         WLAN_EID_SUPPORTED_CHANNELS,
840                         WLAN_EID_RSN,
841                         WLAN_EID_QOS_CAPA,
842                         WLAN_EID_RRM_ENABLED_CAPABILITIES,
843                         WLAN_EID_MOBILITY_DOMAIN,
844                         WLAN_EID_FAST_BSS_TRANSITION,   /* reassoc only */
845                         WLAN_EID_RIC_DATA,              /* reassoc only */
846                         WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
847                 };
848                 static const u8 after_ric[] = {
849                         WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
850                         WLAN_EID_HT_CAPABILITY,
851                         WLAN_EID_BSS_COEX_2040,
852                         /* luckily this is almost always there */
853                         WLAN_EID_EXT_CAPABILITY,
854                         WLAN_EID_QOS_TRAFFIC_CAPA,
855                         WLAN_EID_TIM_BCAST_REQ,
856                         WLAN_EID_INTERWORKING,
857                         /* 60 GHz (Multi-band, DMG, MMS) can't happen */
858                         WLAN_EID_VHT_CAPABILITY,
859                         WLAN_EID_OPMODE_NOTIF,
860                 };
861
862                 noffset = ieee80211_ie_split_ric(assoc_data->ie,
863                                                  assoc_data->ie_len,
864                                                  before_ht,
865                                                  ARRAY_SIZE(before_ht),
866                                                  after_ric,
867                                                  ARRAY_SIZE(after_ric),
868                                                  offset);
869                 skb_put_data(skb, assoc_data->ie + offset, noffset - offset);
870                 offset = noffset;
871         }
872
873         if (WARN_ON_ONCE((ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
874                          !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)))
875                 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
876
877         if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
878                 ieee80211_add_ht_ie(sdata, skb, assoc_data->ap_ht_param,
879                                     sband, chan, sdata->smps_mode);
880
881         /* if present, add any custom IEs that go before VHT */
882         if (assoc_data->ie_len) {
883                 static const u8 before_vht[] = {
884                         /*
885                          * no need to list the ones split off before HT
886                          * or generated here
887                          */
888                         WLAN_EID_BSS_COEX_2040,
889                         WLAN_EID_EXT_CAPABILITY,
890                         WLAN_EID_QOS_TRAFFIC_CAPA,
891                         WLAN_EID_TIM_BCAST_REQ,
892                         WLAN_EID_INTERWORKING,
893                         /* 60 GHz (Multi-band, DMG, MMS) can't happen */
894                 };
895
896                 /* RIC already taken above, so no need to handle here anymore */
897                 noffset = ieee80211_ie_split(assoc_data->ie, assoc_data->ie_len,
898                                              before_vht, ARRAY_SIZE(before_vht),
899                                              offset);
900                 skb_put_data(skb, assoc_data->ie + offset, noffset - offset);
901                 offset = noffset;
902         }
903
904         /* if present, add any custom IEs that go before HE */
905         if (assoc_data->ie_len) {
906                 static const u8 before_he[] = {
907                         /*
908                          * no need to list the ones split off before VHT
909                          * or generated here
910                          */
911                         WLAN_EID_OPMODE_NOTIF,
912                         WLAN_EID_EXTENSION, WLAN_EID_EXT_FUTURE_CHAN_GUIDANCE,
913                         /* 11ai elements */
914                         WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_SESSION,
915                         WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_PUBLIC_KEY,
916                         WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_KEY_CONFIRM,
917                         WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_HLP_CONTAINER,
918                         WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_IP_ADDR_ASSIGN,
919                         /* TODO: add 11ah/11aj/11ak elements */
920                 };
921
922                 /* RIC already taken above, so no need to handle here anymore */
923                 noffset = ieee80211_ie_split(assoc_data->ie, assoc_data->ie_len,
924                                              before_he, ARRAY_SIZE(before_he),
925                                              offset);
926                 pos = skb_put(skb, noffset - offset);
927                 memcpy(pos, assoc_data->ie + offset, noffset - offset);
928                 offset = noffset;
929         }
930
931         if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
932                 ieee80211_add_vht_ie(sdata, skb, sband,
933                                      &assoc_data->ap_vht_cap);
934
935         /*
936          * If AP doesn't support HT, mark HE as disabled.
937          * If on the 5GHz band, make sure it supports VHT.
938          */
939         if (ifmgd->flags & IEEE80211_STA_DISABLE_HT ||
940             (sband->band == NL80211_BAND_5GHZ &&
941              ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
942                 ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
943
944         if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE))
945                 ieee80211_add_he_ie(sdata, skb, sband);
946
947         /* if present, add any custom non-vendor IEs that go after HE */
948         if (assoc_data->ie_len) {
949                 noffset = ieee80211_ie_split_vendor(assoc_data->ie,
950                                                     assoc_data->ie_len,
951                                                     offset);
952                 skb_put_data(skb, assoc_data->ie + offset, noffset - offset);
953                 offset = noffset;
954         }
955
956         if (assoc_data->wmm) {
957                 if (assoc_data->uapsd) {
958                         qos_info = ifmgd->uapsd_queues;
959                         qos_info |= (ifmgd->uapsd_max_sp_len <<
960                                      IEEE80211_WMM_IE_STA_QOSINFO_SP_SHIFT);
961                 } else {
962                         qos_info = 0;
963                 }
964
965                 pos = ieee80211_add_wmm_info_ie(skb_put(skb, 9), qos_info);
966         }
967
968         /* add any remaining custom (i.e. vendor specific here) IEs */
969         if (assoc_data->ie_len) {
970                 noffset = assoc_data->ie_len;
971                 skb_put_data(skb, assoc_data->ie + offset, noffset - offset);
972         }
973
974         if (assoc_data->fils_kek_len &&
975             fils_encrypt_assoc_req(skb, assoc_data) < 0) {
976                 dev_kfree_skb(skb);
977                 return;
978         }
979
980         pos = skb_tail_pointer(skb);
981         kfree(ifmgd->assoc_req_ies);
982         ifmgd->assoc_req_ies = kmemdup(ie_start, pos - ie_start, GFP_ATOMIC);
983         ifmgd->assoc_req_ies_len = pos - ie_start;
984
985         drv_mgd_prepare_tx(local, sdata, 0);
986
987         IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
988         if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
989                 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS |
990                                                 IEEE80211_TX_INTFL_MLME_CONN_TX;
991         ieee80211_tx_skb(sdata, skb);
992 }
993
994 void ieee80211_send_pspoll(struct ieee80211_local *local,
995                            struct ieee80211_sub_if_data *sdata)
996 {
997         struct ieee80211_pspoll *pspoll;
998         struct sk_buff *skb;
999
1000         skb = ieee80211_pspoll_get(&local->hw, &sdata->vif);
1001         if (!skb)
1002                 return;
1003
1004         pspoll = (struct ieee80211_pspoll *) skb->data;
1005         pspoll->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
1006
1007         IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
1008         ieee80211_tx_skb(sdata, skb);
1009 }
1010
1011 void ieee80211_send_nullfunc(struct ieee80211_local *local,
1012                              struct ieee80211_sub_if_data *sdata,
1013                              bool powersave)
1014 {
1015         struct sk_buff *skb;
1016         struct ieee80211_hdr_3addr *nullfunc;
1017         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1018
1019         /* Don't send NDPs when STA is connected HE */
1020         if (sdata->vif.type == NL80211_IFTYPE_STATION &&
1021             !(ifmgd->flags & IEEE80211_STA_DISABLE_HE))
1022                 return;
1023
1024         skb = ieee80211_nullfunc_get(&local->hw, &sdata->vif,
1025                 !ieee80211_hw_check(&local->hw, DOESNT_SUPPORT_QOS_NDP));
1026         if (!skb)
1027                 return;
1028
1029         nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
1030         if (powersave)
1031                 nullfunc->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
1032
1033         IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
1034                                         IEEE80211_TX_INTFL_OFFCHAN_TX_OK;
1035
1036         if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
1037                 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
1038
1039         if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL)
1040                 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_USE_MINRATE;
1041
1042         ieee80211_tx_skb(sdata, skb);
1043 }
1044
1045 static void ieee80211_send_4addr_nullfunc(struct ieee80211_local *local,
1046                                           struct ieee80211_sub_if_data *sdata)
1047 {
1048         struct sk_buff *skb;
1049         struct ieee80211_hdr *nullfunc;
1050         __le16 fc;
1051
1052         if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
1053                 return;
1054
1055         /* Don't send NDPs when connected HE */
1056         if (!(sdata->u.mgd.flags & IEEE80211_STA_DISABLE_HE))
1057                 return;
1058
1059         skb = dev_alloc_skb(local->hw.extra_tx_headroom + 30);
1060         if (!skb)
1061                 return;
1062
1063         skb_reserve(skb, local->hw.extra_tx_headroom);
1064
1065         nullfunc = skb_put_zero(skb, 30);
1066         fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
1067                          IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
1068         nullfunc->frame_control = fc;
1069         memcpy(nullfunc->addr1, sdata->u.mgd.bssid, ETH_ALEN);
1070         memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1071         memcpy(nullfunc->addr3, sdata->u.mgd.bssid, ETH_ALEN);
1072         memcpy(nullfunc->addr4, sdata->vif.addr, ETH_ALEN);
1073
1074         IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
1075         ieee80211_tx_skb(sdata, skb);
1076 }
1077
1078 /* spectrum management related things */
1079 static void ieee80211_chswitch_work(struct work_struct *work)
1080 {
1081         struct ieee80211_sub_if_data *sdata =
1082                 container_of(work, struct ieee80211_sub_if_data, u.mgd.chswitch_work);
1083         struct ieee80211_local *local = sdata->local;
1084         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1085         int ret;
1086
1087         if (!ieee80211_sdata_running(sdata))
1088                 return;
1089
1090         sdata_lock(sdata);
1091         mutex_lock(&local->mtx);
1092         mutex_lock(&local->chanctx_mtx);
1093
1094         if (!ifmgd->associated)
1095                 goto out;
1096
1097         if (!sdata->vif.csa_active)
1098                 goto out;
1099
1100         /*
1101          * using reservation isn't immediate as it may be deferred until later
1102          * with multi-vif. once reservation is complete it will re-schedule the
1103          * work with no reserved_chanctx so verify chandef to check if it
1104          * completed successfully
1105          */
1106
1107         if (sdata->reserved_chanctx) {
1108                 struct ieee80211_supported_band *sband = NULL;
1109                 struct sta_info *mgd_sta = NULL;
1110                 enum ieee80211_sta_rx_bandwidth bw = IEEE80211_STA_RX_BW_20;
1111
1112                 /*
1113                  * with multi-vif csa driver may call ieee80211_csa_finish()
1114                  * many times while waiting for other interfaces to use their
1115                  * reservations
1116                  */
1117                 if (sdata->reserved_ready)
1118                         goto out;
1119
1120                 if (sdata->vif.bss_conf.chandef.width !=
1121                     sdata->csa_chandef.width) {
1122                         /*
1123                          * For managed interface, we need to also update the AP
1124                          * station bandwidth and align the rate scale algorithm
1125                          * on the bandwidth change. Here we only consider the
1126                          * bandwidth of the new channel definition (as channel
1127                          * switch flow does not have the full HT/VHT/HE
1128                          * information), assuming that if additional changes are
1129                          * required they would be done as part of the processing
1130                          * of the next beacon from the AP.
1131                          */
1132                         switch (sdata->csa_chandef.width) {
1133                         case NL80211_CHAN_WIDTH_20_NOHT:
1134                         case NL80211_CHAN_WIDTH_20:
1135                         default:
1136                                 bw = IEEE80211_STA_RX_BW_20;
1137                                 break;
1138                         case NL80211_CHAN_WIDTH_40:
1139                                 bw = IEEE80211_STA_RX_BW_40;
1140                                 break;
1141                         case NL80211_CHAN_WIDTH_80:
1142                                 bw = IEEE80211_STA_RX_BW_80;
1143                                 break;
1144                         case NL80211_CHAN_WIDTH_80P80:
1145                         case NL80211_CHAN_WIDTH_160:
1146                                 bw = IEEE80211_STA_RX_BW_160;
1147                                 break;
1148                         }
1149
1150                         mgd_sta = sta_info_get(sdata, ifmgd->bssid);
1151                         sband =
1152                                 local->hw.wiphy->bands[sdata->csa_chandef.chan->band];
1153                 }
1154
1155                 if (sdata->vif.bss_conf.chandef.width >
1156                     sdata->csa_chandef.width) {
1157                         mgd_sta->sta.bandwidth = bw;
1158                         rate_control_rate_update(local, sband, mgd_sta,
1159                                                  IEEE80211_RC_BW_CHANGED);
1160                 }
1161
1162                 ret = ieee80211_vif_use_reserved_context(sdata);
1163                 if (ret) {
1164                         sdata_info(sdata,
1165                                    "failed to use reserved channel context, disconnecting (err=%d)\n",
1166                                    ret);
1167                         ieee80211_queue_work(&sdata->local->hw,
1168                                              &ifmgd->csa_connection_drop_work);
1169                         goto out;
1170                 }
1171
1172                 if (sdata->vif.bss_conf.chandef.width <
1173                     sdata->csa_chandef.width) {
1174                         mgd_sta->sta.bandwidth = bw;
1175                         rate_control_rate_update(local, sband, mgd_sta,
1176                                                  IEEE80211_RC_BW_CHANGED);
1177                 }
1178
1179                 goto out;
1180         }
1181
1182         if (!cfg80211_chandef_identical(&sdata->vif.bss_conf.chandef,
1183                                         &sdata->csa_chandef)) {
1184                 sdata_info(sdata,
1185                            "failed to finalize channel switch, disconnecting\n");
1186                 ieee80211_queue_work(&sdata->local->hw,
1187                                      &ifmgd->csa_connection_drop_work);
1188                 goto out;
1189         }
1190
1191         /* XXX: shouldn't really modify cfg80211-owned data! */
1192         ifmgd->associated->channel = sdata->csa_chandef.chan;
1193
1194         ifmgd->csa_waiting_bcn = true;
1195
1196         ieee80211_sta_reset_beacon_monitor(sdata);
1197         ieee80211_sta_reset_conn_monitor(sdata);
1198
1199 out:
1200         mutex_unlock(&local->chanctx_mtx);
1201         mutex_unlock(&local->mtx);
1202         sdata_unlock(sdata);
1203 }
1204
1205 static void ieee80211_chswitch_post_beacon(struct ieee80211_sub_if_data *sdata)
1206 {
1207         struct ieee80211_local *local = sdata->local;
1208         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1209         int ret;
1210
1211         sdata_assert_lock(sdata);
1212
1213         WARN_ON(!sdata->vif.csa_active);
1214
1215         if (sdata->csa_block_tx) {
1216                 ieee80211_wake_vif_queues(local, sdata,
1217                                           IEEE80211_QUEUE_STOP_REASON_CSA);
1218                 sdata->csa_block_tx = false;
1219         }
1220
1221         sdata->vif.csa_active = false;
1222         ifmgd->csa_waiting_bcn = false;
1223
1224         ret = drv_post_channel_switch(sdata);
1225         if (ret) {
1226                 sdata_info(sdata,
1227                            "driver post channel switch failed, disconnecting\n");
1228                 ieee80211_queue_work(&local->hw,
1229                                      &ifmgd->csa_connection_drop_work);
1230                 return;
1231         }
1232
1233         cfg80211_ch_switch_notify(sdata->dev, &sdata->reserved_chandef);
1234 }
1235
1236 void ieee80211_chswitch_done(struct ieee80211_vif *vif, bool success)
1237 {
1238         struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1239         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1240
1241         trace_api_chswitch_done(sdata, success);
1242         if (!success) {
1243                 sdata_info(sdata,
1244                            "driver channel switch failed, disconnecting\n");
1245                 ieee80211_queue_work(&sdata->local->hw,
1246                                      &ifmgd->csa_connection_drop_work);
1247         } else {
1248                 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
1249         }
1250 }
1251 EXPORT_SYMBOL(ieee80211_chswitch_done);
1252
1253 static void ieee80211_chswitch_timer(struct timer_list *t)
1254 {
1255         struct ieee80211_sub_if_data *sdata =
1256                 from_timer(sdata, t, u.mgd.chswitch_timer);
1257
1258         ieee80211_queue_work(&sdata->local->hw, &sdata->u.mgd.chswitch_work);
1259 }
1260
1261 static void
1262 ieee80211_sta_abort_chanswitch(struct ieee80211_sub_if_data *sdata)
1263 {
1264         struct ieee80211_local *local = sdata->local;
1265
1266         if (!local->ops->abort_channel_switch)
1267                 return;
1268
1269         mutex_lock(&local->mtx);
1270
1271         mutex_lock(&local->chanctx_mtx);
1272         ieee80211_vif_unreserve_chanctx(sdata);
1273         mutex_unlock(&local->chanctx_mtx);
1274
1275         if (sdata->csa_block_tx)
1276                 ieee80211_wake_vif_queues(local, sdata,
1277                                           IEEE80211_QUEUE_STOP_REASON_CSA);
1278
1279         sdata->csa_block_tx = false;
1280         sdata->vif.csa_active = false;
1281
1282         mutex_unlock(&local->mtx);
1283
1284         drv_abort_channel_switch(sdata);
1285 }
1286
1287 static void
1288 ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
1289                                  u64 timestamp, u32 device_timestamp,
1290                                  struct ieee802_11_elems *elems,
1291                                  bool beacon)
1292 {
1293         struct ieee80211_local *local = sdata->local;
1294         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1295         struct cfg80211_bss *cbss = ifmgd->associated;
1296         struct ieee80211_chanctx_conf *conf;
1297         struct ieee80211_chanctx *chanctx;
1298         enum nl80211_band current_band;
1299         struct ieee80211_csa_ie csa_ie;
1300         struct ieee80211_channel_switch ch_switch;
1301         int res;
1302
1303         sdata_assert_lock(sdata);
1304
1305         if (!cbss)
1306                 return;
1307
1308         if (local->scanning)
1309                 return;
1310
1311         current_band = cbss->channel->band;
1312         res = ieee80211_parse_ch_switch_ie(sdata, elems, current_band,
1313                                            ifmgd->flags,
1314                                            ifmgd->associated->bssid, &csa_ie);
1315
1316         if (!res) {
1317                 ch_switch.timestamp = timestamp;
1318                 ch_switch.device_timestamp = device_timestamp;
1319                 ch_switch.block_tx = csa_ie.mode;
1320                 ch_switch.chandef = csa_ie.chandef;
1321                 ch_switch.count = csa_ie.count;
1322                 ch_switch.delay = csa_ie.max_switch_time;
1323         }
1324
1325         if (res < 0) {
1326                 ieee80211_queue_work(&local->hw,
1327                                      &ifmgd->csa_connection_drop_work);
1328                 return;
1329         }
1330
1331         if (beacon && sdata->vif.csa_active && !ifmgd->csa_waiting_bcn) {
1332                 if (res)
1333                         ieee80211_sta_abort_chanswitch(sdata);
1334                 else
1335                         drv_channel_switch_rx_beacon(sdata, &ch_switch);
1336                 return;
1337         } else if (sdata->vif.csa_active || res) {
1338                 /* disregard subsequent announcements if already processing */
1339                 return;
1340         }
1341
1342         if (!cfg80211_chandef_usable(local->hw.wiphy, &csa_ie.chandef,
1343                                      IEEE80211_CHAN_DISABLED)) {
1344                 sdata_info(sdata,
1345                            "AP %pM switches to unsupported channel (%d MHz, width:%d, CF1/2: %d/%d MHz), disconnecting\n",
1346                            ifmgd->associated->bssid,
1347                            csa_ie.chandef.chan->center_freq,
1348                            csa_ie.chandef.width, csa_ie.chandef.center_freq1,
1349                            csa_ie.chandef.center_freq2);
1350                 ieee80211_queue_work(&local->hw,
1351                                      &ifmgd->csa_connection_drop_work);
1352                 return;
1353         }
1354
1355         if (cfg80211_chandef_identical(&csa_ie.chandef,
1356                                        &sdata->vif.bss_conf.chandef)) {
1357                 if (ifmgd->csa_ignored_same_chan)
1358                         return;
1359                 sdata_info(sdata,
1360                            "AP %pM tries to chanswitch to same channel, ignore\n",
1361                            ifmgd->associated->bssid);
1362                 ifmgd->csa_ignored_same_chan = true;
1363                 return;
1364         }
1365
1366         /*
1367          * Drop all TDLS peers - either we disconnect or move to a different
1368          * channel from this point on. There's no telling what our peer will do.
1369          * The TDLS WIDER_BW scenario is also problematic, as peers might now
1370          * have an incompatible wider chandef.
1371          */
1372         ieee80211_teardown_tdls_peers(sdata);
1373
1374         mutex_lock(&local->mtx);
1375         mutex_lock(&local->chanctx_mtx);
1376         conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
1377                                          lockdep_is_held(&local->chanctx_mtx));
1378         if (!conf) {
1379                 sdata_info(sdata,
1380                            "no channel context assigned to vif?, disconnecting\n");
1381                 goto drop_connection;
1382         }
1383
1384         chanctx = container_of(conf, struct ieee80211_chanctx, conf);
1385
1386         if (local->use_chanctx &&
1387             !ieee80211_hw_check(&local->hw, CHANCTX_STA_CSA)) {
1388                 sdata_info(sdata,
1389                            "driver doesn't support chan-switch with channel contexts\n");
1390                 goto drop_connection;
1391         }
1392
1393         if (drv_pre_channel_switch(sdata, &ch_switch)) {
1394                 sdata_info(sdata,
1395                            "preparing for channel switch failed, disconnecting\n");
1396                 goto drop_connection;
1397         }
1398
1399         res = ieee80211_vif_reserve_chanctx(sdata, &csa_ie.chandef,
1400                                             chanctx->mode, false);
1401         if (res) {
1402                 sdata_info(sdata,
1403                            "failed to reserve channel context for channel switch, disconnecting (err=%d)\n",
1404                            res);
1405                 goto drop_connection;
1406         }
1407         mutex_unlock(&local->chanctx_mtx);
1408
1409         sdata->vif.csa_active = true;
1410         sdata->csa_chandef = csa_ie.chandef;
1411         sdata->csa_block_tx = csa_ie.mode;
1412         ifmgd->csa_ignored_same_chan = false;
1413
1414         if (sdata->csa_block_tx)
1415                 ieee80211_stop_vif_queues(local, sdata,
1416                                           IEEE80211_QUEUE_STOP_REASON_CSA);
1417         mutex_unlock(&local->mtx);
1418
1419         cfg80211_ch_switch_started_notify(sdata->dev, &csa_ie.chandef,
1420                                           csa_ie.count);
1421
1422         if (local->ops->channel_switch) {
1423                 /* use driver's channel switch callback */
1424                 drv_channel_switch(local, sdata, &ch_switch);
1425                 return;
1426         }
1427
1428         /* channel switch handled in software */
1429         if (csa_ie.count <= 1)
1430                 ieee80211_queue_work(&local->hw, &ifmgd->chswitch_work);
1431         else
1432                 mod_timer(&ifmgd->chswitch_timer,
1433                           TU_TO_EXP_TIME((csa_ie.count - 1) *
1434                                          cbss->beacon_interval));
1435         return;
1436  drop_connection:
1437         /*
1438          * This is just so that the disconnect flow will know that
1439          * we were trying to switch channel and failed. In case the
1440          * mode is 1 (we are not allowed to Tx), we will know not to
1441          * send a deauthentication frame. Those two fields will be
1442          * reset when the disconnection worker runs.
1443          */
1444         sdata->vif.csa_active = true;
1445         sdata->csa_block_tx = csa_ie.mode;
1446
1447         ieee80211_queue_work(&local->hw, &ifmgd->csa_connection_drop_work);
1448         mutex_unlock(&local->chanctx_mtx);
1449         mutex_unlock(&local->mtx);
1450 }
1451
1452 static bool
1453 ieee80211_find_80211h_pwr_constr(struct ieee80211_sub_if_data *sdata,
1454                                  struct ieee80211_channel *channel,
1455                                  const u8 *country_ie, u8 country_ie_len,
1456                                  const u8 *pwr_constr_elem,
1457                                  int *chan_pwr, int *pwr_reduction)
1458 {
1459         struct ieee80211_country_ie_triplet *triplet;
1460         int chan = ieee80211_frequency_to_channel(channel->center_freq);
1461         int i, chan_increment;
1462         bool have_chan_pwr = false;
1463
1464         /* Invalid IE */
1465         if (country_ie_len % 2 || country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
1466                 return false;
1467
1468         triplet = (void *)(country_ie + 3);
1469         country_ie_len -= 3;
1470
1471         switch (channel->band) {
1472         default:
1473                 WARN_ON_ONCE(1);
1474                 /* fall through */
1475         case NL80211_BAND_2GHZ:
1476         case NL80211_BAND_60GHZ:
1477                 chan_increment = 1;
1478                 break;
1479         case NL80211_BAND_5GHZ:
1480                 chan_increment = 4;
1481                 break;
1482         }
1483
1484         /* find channel */
1485         while (country_ie_len >= 3) {
1486                 u8 first_channel = triplet->chans.first_channel;
1487
1488                 if (first_channel >= IEEE80211_COUNTRY_EXTENSION_ID)
1489                         goto next;
1490
1491                 for (i = 0; i < triplet->chans.num_channels; i++) {
1492                         if (first_channel + i * chan_increment == chan) {
1493                                 have_chan_pwr = true;
1494                                 *chan_pwr = triplet->chans.max_power;
1495                                 break;
1496                         }
1497                 }
1498                 if (have_chan_pwr)
1499                         break;
1500
1501  next:
1502                 triplet++;
1503                 country_ie_len -= 3;
1504         }
1505
1506         if (have_chan_pwr && pwr_constr_elem)
1507                 *pwr_reduction = *pwr_constr_elem;
1508         else
1509                 *pwr_reduction = 0;
1510
1511         return have_chan_pwr;
1512 }
1513
1514 static void ieee80211_find_cisco_dtpc(struct ieee80211_sub_if_data *sdata,
1515                                       struct ieee80211_channel *channel,
1516                                       const u8 *cisco_dtpc_ie,
1517                                       int *pwr_level)
1518 {
1519         /* From practical testing, the first data byte of the DTPC element
1520          * seems to contain the requested dBm level, and the CLI on Cisco
1521          * APs clearly state the range is -127 to 127 dBm, which indicates
1522          * a signed byte, although it seemingly never actually goes negative.
1523          * The other byte seems to always be zero.
1524          */
1525         *pwr_level = (__s8)cisco_dtpc_ie[4];
1526 }
1527
1528 static u32 ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data *sdata,
1529                                        struct ieee80211_channel *channel,
1530                                        struct ieee80211_mgmt *mgmt,
1531                                        const u8 *country_ie, u8 country_ie_len,
1532                                        const u8 *pwr_constr_ie,
1533                                        const u8 *cisco_dtpc_ie)
1534 {
1535         bool has_80211h_pwr = false, has_cisco_pwr = false;
1536         int chan_pwr = 0, pwr_reduction_80211h = 0;
1537         int pwr_level_cisco, pwr_level_80211h;
1538         int new_ap_level;
1539         __le16 capab = mgmt->u.probe_resp.capab_info;
1540
1541         if (country_ie &&
1542             (capab & cpu_to_le16(WLAN_CAPABILITY_SPECTRUM_MGMT) ||
1543              capab & cpu_to_le16(WLAN_CAPABILITY_RADIO_MEASURE))) {
1544                 has_80211h_pwr = ieee80211_find_80211h_pwr_constr(
1545                         sdata, channel, country_ie, country_ie_len,
1546                         pwr_constr_ie, &chan_pwr, &pwr_reduction_80211h);
1547                 pwr_level_80211h =
1548                         max_t(int, 0, chan_pwr - pwr_reduction_80211h);
1549         }
1550
1551         if (cisco_dtpc_ie) {
1552                 ieee80211_find_cisco_dtpc(
1553                         sdata, channel, cisco_dtpc_ie, &pwr_level_cisco);
1554                 has_cisco_pwr = true;
1555         }
1556
1557         if (!has_80211h_pwr && !has_cisco_pwr)
1558                 return 0;
1559
1560         /* If we have both 802.11h and Cisco DTPC, apply both limits
1561          * by picking the smallest of the two power levels advertised.
1562          */
1563         if (has_80211h_pwr &&
1564             (!has_cisco_pwr || pwr_level_80211h <= pwr_level_cisco)) {
1565                 new_ap_level = pwr_level_80211h;
1566
1567                 if (sdata->ap_power_level == new_ap_level)
1568                         return 0;
1569
1570                 sdata_dbg(sdata,
1571                           "Limiting TX power to %d (%d - %d) dBm as advertised by %pM\n",
1572                           pwr_level_80211h, chan_pwr, pwr_reduction_80211h,
1573                           sdata->u.mgd.bssid);
1574         } else {  /* has_cisco_pwr is always true here. */
1575                 new_ap_level = pwr_level_cisco;
1576
1577                 if (sdata->ap_power_level == new_ap_level)
1578                         return 0;
1579
1580                 sdata_dbg(sdata,
1581                           "Limiting TX power to %d dBm as advertised by %pM\n",
1582                           pwr_level_cisco, sdata->u.mgd.bssid);
1583         }
1584
1585         sdata->ap_power_level = new_ap_level;
1586         if (__ieee80211_recalc_txpower(sdata))
1587                 return BSS_CHANGED_TXPOWER;
1588         return 0;
1589 }
1590
1591 /* powersave */
1592 static void ieee80211_enable_ps(struct ieee80211_local *local,
1593                                 struct ieee80211_sub_if_data *sdata)
1594 {
1595         struct ieee80211_conf *conf = &local->hw.conf;
1596
1597         /*
1598          * If we are scanning right now then the parameters will
1599          * take effect when scan finishes.
1600          */
1601         if (local->scanning)
1602                 return;
1603
1604         if (conf->dynamic_ps_timeout > 0 &&
1605             !ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS)) {
1606                 mod_timer(&local->dynamic_ps_timer, jiffies +
1607                           msecs_to_jiffies(conf->dynamic_ps_timeout));
1608         } else {
1609                 if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK))
1610                         ieee80211_send_nullfunc(local, sdata, true);
1611
1612                 if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK) &&
1613                     ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
1614                         return;
1615
1616                 conf->flags |= IEEE80211_CONF_PS;
1617                 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1618         }
1619 }
1620
1621 static void ieee80211_change_ps(struct ieee80211_local *local)
1622 {
1623         struct ieee80211_conf *conf = &local->hw.conf;
1624
1625         if (local->ps_sdata) {
1626                 ieee80211_enable_ps(local, local->ps_sdata);
1627         } else if (conf->flags & IEEE80211_CONF_PS) {
1628                 conf->flags &= ~IEEE80211_CONF_PS;
1629                 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1630                 del_timer_sync(&local->dynamic_ps_timer);
1631                 cancel_work_sync(&local->dynamic_ps_enable_work);
1632         }
1633 }
1634
1635 static bool ieee80211_powersave_allowed(struct ieee80211_sub_if_data *sdata)
1636 {
1637         struct ieee80211_if_managed *mgd = &sdata->u.mgd;
1638         struct sta_info *sta = NULL;
1639         bool authorized = false;
1640
1641         if (!mgd->powersave)
1642                 return false;
1643
1644         if (mgd->broken_ap)
1645                 return false;
1646
1647         if (!mgd->associated)
1648                 return false;
1649
1650         if (mgd->flags & IEEE80211_STA_CONNECTION_POLL)
1651                 return false;
1652
1653         if (!mgd->have_beacon)
1654                 return false;
1655
1656         rcu_read_lock();
1657         sta = sta_info_get(sdata, mgd->bssid);
1658         if (sta)
1659                 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
1660         rcu_read_unlock();
1661
1662         return authorized;
1663 }
1664
1665 /* need to hold RTNL or interface lock */
1666 void ieee80211_recalc_ps(struct ieee80211_local *local)
1667 {
1668         struct ieee80211_sub_if_data *sdata, *found = NULL;
1669         int count = 0;
1670         int timeout;
1671
1672         if (!ieee80211_hw_check(&local->hw, SUPPORTS_PS)) {
1673                 local->ps_sdata = NULL;
1674                 return;
1675         }
1676
1677         list_for_each_entry(sdata, &local->interfaces, list) {
1678                 if (!ieee80211_sdata_running(sdata))
1679                         continue;
1680                 if (sdata->vif.type == NL80211_IFTYPE_AP) {
1681                         /* If an AP vif is found, then disable PS
1682                          * by setting the count to zero thereby setting
1683                          * ps_sdata to NULL.
1684                          */
1685                         count = 0;
1686                         break;
1687                 }
1688                 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1689                         continue;
1690                 found = sdata;
1691                 count++;
1692         }
1693
1694         if (count == 1 && ieee80211_powersave_allowed(found)) {
1695                 u8 dtimper = found->u.mgd.dtim_period;
1696
1697                 timeout = local->dynamic_ps_forced_timeout;
1698                 if (timeout < 0)
1699                         timeout = 100;
1700                 local->hw.conf.dynamic_ps_timeout = timeout;
1701
1702                 /* If the TIM IE is invalid, pretend the value is 1 */
1703                 if (!dtimper)
1704                         dtimper = 1;
1705
1706                 local->hw.conf.ps_dtim_period = dtimper;
1707                 local->ps_sdata = found;
1708         } else {
1709                 local->ps_sdata = NULL;
1710         }
1711
1712         ieee80211_change_ps(local);
1713 }
1714
1715 void ieee80211_recalc_ps_vif(struct ieee80211_sub_if_data *sdata)
1716 {
1717         bool ps_allowed = ieee80211_powersave_allowed(sdata);
1718
1719         if (sdata->vif.bss_conf.ps != ps_allowed) {
1720                 sdata->vif.bss_conf.ps = ps_allowed;
1721                 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_PS);
1722         }
1723 }
1724
1725 void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
1726 {
1727         struct ieee80211_local *local =
1728                 container_of(work, struct ieee80211_local,
1729                              dynamic_ps_disable_work);
1730
1731         if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1732                 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1733                 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1734         }
1735
1736         ieee80211_wake_queues_by_reason(&local->hw,
1737                                         IEEE80211_MAX_QUEUE_MAP,
1738                                         IEEE80211_QUEUE_STOP_REASON_PS,
1739                                         false);
1740 }
1741
1742 void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
1743 {
1744         struct ieee80211_local *local =
1745                 container_of(work, struct ieee80211_local,
1746                              dynamic_ps_enable_work);
1747         struct ieee80211_sub_if_data *sdata = local->ps_sdata;
1748         struct ieee80211_if_managed *ifmgd;
1749         unsigned long flags;
1750         int q;
1751
1752         /* can only happen when PS was just disabled anyway */
1753         if (!sdata)
1754                 return;
1755
1756         ifmgd = &sdata->u.mgd;
1757
1758         if (local->hw.conf.flags & IEEE80211_CONF_PS)
1759                 return;
1760
1761         if (local->hw.conf.dynamic_ps_timeout > 0) {
1762                 /* don't enter PS if TX frames are pending */
1763                 if (drv_tx_frames_pending(local)) {
1764                         mod_timer(&local->dynamic_ps_timer, jiffies +
1765                                   msecs_to_jiffies(
1766                                   local->hw.conf.dynamic_ps_timeout));
1767                         return;
1768                 }
1769
1770                 /*
1771                  * transmission can be stopped by others which leads to
1772                  * dynamic_ps_timer expiry. Postpone the ps timer if it
1773                  * is not the actual idle state.
1774                  */
1775                 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
1776                 for (q = 0; q < local->hw.queues; q++) {
1777                         if (local->queue_stop_reasons[q]) {
1778                                 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1779                                                        flags);
1780                                 mod_timer(&local->dynamic_ps_timer, jiffies +
1781                                           msecs_to_jiffies(
1782                                           local->hw.conf.dynamic_ps_timeout));
1783                                 return;
1784                         }
1785                 }
1786                 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
1787         }
1788
1789         if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK) &&
1790             !(ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
1791                 if (drv_tx_frames_pending(local)) {
1792                         mod_timer(&local->dynamic_ps_timer, jiffies +
1793                                   msecs_to_jiffies(
1794                                   local->hw.conf.dynamic_ps_timeout));
1795                 } else {
1796                         ieee80211_send_nullfunc(local, sdata, true);
1797                         /* Flush to get the tx status of nullfunc frame */
1798                         ieee80211_flush_queues(local, sdata, false);
1799                 }
1800         }
1801
1802         if (!(ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS) &&
1803               ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK)) ||
1804             (ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
1805                 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
1806                 local->hw.conf.flags |= IEEE80211_CONF_PS;
1807                 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1808         }
1809 }
1810
1811 void ieee80211_dynamic_ps_timer(struct timer_list *t)
1812 {
1813         struct ieee80211_local *local = from_timer(local, t, dynamic_ps_timer);
1814
1815         ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
1816 }
1817
1818 void ieee80211_dfs_cac_timer_work(struct work_struct *work)
1819 {
1820         struct delayed_work *delayed_work = to_delayed_work(work);
1821         struct ieee80211_sub_if_data *sdata =
1822                 container_of(delayed_work, struct ieee80211_sub_if_data,
1823                              dfs_cac_timer_work);
1824         struct cfg80211_chan_def chandef = sdata->vif.bss_conf.chandef;
1825
1826         mutex_lock(&sdata->local->mtx);
1827         if (sdata->wdev.cac_started) {
1828                 ieee80211_vif_release_channel(sdata);
1829                 cfg80211_cac_event(sdata->dev, &chandef,
1830                                    NL80211_RADAR_CAC_FINISHED,
1831                                    GFP_KERNEL);
1832         }
1833         mutex_unlock(&sdata->local->mtx);
1834 }
1835
1836 static bool
1837 __ieee80211_sta_handle_tspec_ac_params(struct ieee80211_sub_if_data *sdata)
1838 {
1839         struct ieee80211_local *local = sdata->local;
1840         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1841         bool ret = false;
1842         int ac;
1843
1844         if (local->hw.queues < IEEE80211_NUM_ACS)
1845                 return false;
1846
1847         for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1848                 struct ieee80211_sta_tx_tspec *tx_tspec = &ifmgd->tx_tspec[ac];
1849                 int non_acm_ac;
1850                 unsigned long now = jiffies;
1851
1852                 if (tx_tspec->action == TX_TSPEC_ACTION_NONE &&
1853                     tx_tspec->admitted_time &&
1854                     time_after(now, tx_tspec->time_slice_start + HZ)) {
1855                         tx_tspec->consumed_tx_time = 0;
1856                         tx_tspec->time_slice_start = now;
1857
1858                         if (tx_tspec->downgraded)
1859                                 tx_tspec->action =
1860                                         TX_TSPEC_ACTION_STOP_DOWNGRADE;
1861                 }
1862
1863                 switch (tx_tspec->action) {
1864                 case TX_TSPEC_ACTION_STOP_DOWNGRADE:
1865                         /* take the original parameters */
1866                         if (drv_conf_tx(local, sdata, ac, &sdata->tx_conf[ac]))
1867                                 sdata_err(sdata,
1868                                           "failed to set TX queue parameters for queue %d\n",
1869                                           ac);
1870                         tx_tspec->action = TX_TSPEC_ACTION_NONE;
1871                         tx_tspec->downgraded = false;
1872                         ret = true;
1873                         break;
1874                 case TX_TSPEC_ACTION_DOWNGRADE:
1875                         if (time_after(now, tx_tspec->time_slice_start + HZ)) {
1876                                 tx_tspec->action = TX_TSPEC_ACTION_NONE;
1877                                 ret = true;
1878                                 break;
1879                         }
1880                         /* downgrade next lower non-ACM AC */
1881                         for (non_acm_ac = ac + 1;
1882                              non_acm_ac < IEEE80211_NUM_ACS;
1883                              non_acm_ac++)
1884                                 if (!(sdata->wmm_acm & BIT(7 - 2 * non_acm_ac)))
1885                                         break;
1886                         /* Usually the loop will result in using BK even if it
1887                          * requires admission control, but such a configuration
1888                          * makes no sense and we have to transmit somehow - the
1889                          * AC selection does the same thing.
1890                          * If we started out trying to downgrade from BK, then
1891                          * the extra condition here might be needed.
1892                          */
1893                         if (non_acm_ac >= IEEE80211_NUM_ACS)
1894                                 non_acm_ac = IEEE80211_AC_BK;
1895                         if (drv_conf_tx(local, sdata, ac,
1896                                         &sdata->tx_conf[non_acm_ac]))
1897                                 sdata_err(sdata,
1898                                           "failed to set TX queue parameters for queue %d\n",
1899                                           ac);
1900                         tx_tspec->action = TX_TSPEC_ACTION_NONE;
1901                         ret = true;
1902                         schedule_delayed_work(&ifmgd->tx_tspec_wk,
1903                                 tx_tspec->time_slice_start + HZ - now + 1);
1904                         break;
1905                 case TX_TSPEC_ACTION_NONE:
1906                         /* nothing now */
1907                         break;
1908                 }
1909         }
1910
1911         return ret;
1912 }
1913
1914 void ieee80211_sta_handle_tspec_ac_params(struct ieee80211_sub_if_data *sdata)
1915 {
1916         if (__ieee80211_sta_handle_tspec_ac_params(sdata))
1917                 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_QOS);
1918 }
1919
1920 static void ieee80211_sta_handle_tspec_ac_params_wk(struct work_struct *work)
1921 {
1922         struct ieee80211_sub_if_data *sdata;
1923
1924         sdata = container_of(work, struct ieee80211_sub_if_data,
1925                              u.mgd.tx_tspec_wk.work);
1926         ieee80211_sta_handle_tspec_ac_params(sdata);
1927 }
1928
1929 /* MLME */
1930 static bool
1931 ieee80211_sta_wmm_params(struct ieee80211_local *local,
1932                          struct ieee80211_sub_if_data *sdata,
1933                          const u8 *wmm_param, size_t wmm_param_len,
1934                          const struct ieee80211_mu_edca_param_set *mu_edca)
1935 {
1936         struct ieee80211_tx_queue_params params[IEEE80211_NUM_ACS];
1937         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1938         size_t left;
1939         int count, mu_edca_count, ac;
1940         const u8 *pos;
1941         u8 uapsd_queues = 0;
1942
1943         if (!local->ops->conf_tx)
1944                 return false;
1945
1946         if (local->hw.queues < IEEE80211_NUM_ACS)
1947                 return false;
1948
1949         if (!wmm_param)
1950                 return false;
1951
1952         if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
1953                 return false;
1954
1955         if (ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED)
1956                 uapsd_queues = ifmgd->uapsd_queues;
1957
1958         count = wmm_param[6] & 0x0f;
1959         /* -1 is the initial value of ifmgd->mu_edca_last_param_set.
1960          * if mu_edca was preset before and now it disappeared tell
1961          * the driver about it.
1962          */
1963         mu_edca_count = mu_edca ? mu_edca->mu_qos_info & 0x0f : -1;
1964         if (count == ifmgd->wmm_last_param_set &&
1965             mu_edca_count == ifmgd->mu_edca_last_param_set)
1966                 return false;
1967         ifmgd->wmm_last_param_set = count;
1968         ifmgd->mu_edca_last_param_set = mu_edca_count;
1969
1970         pos = wmm_param + 8;
1971         left = wmm_param_len - 8;
1972
1973         memset(&params, 0, sizeof(params));
1974
1975         sdata->wmm_acm = 0;
1976         for (; left >= 4; left -= 4, pos += 4) {
1977                 int aci = (pos[0] >> 5) & 0x03;
1978                 int acm = (pos[0] >> 4) & 0x01;
1979                 bool uapsd = false;
1980
1981                 switch (aci) {
1982                 case 1: /* AC_BK */
1983                         ac = IEEE80211_AC_BK;
1984                         if (acm)
1985                                 sdata->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
1986                         if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
1987                                 uapsd = true;
1988                         params[ac].mu_edca = !!mu_edca;
1989                         if (mu_edca)
1990                                 params[ac].mu_edca_param_rec = mu_edca->ac_bk;
1991                         break;
1992                 case 2: /* AC_VI */
1993                         ac = IEEE80211_AC_VI;
1994                         if (acm)
1995                                 sdata->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
1996                         if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
1997                                 uapsd = true;
1998                         params[ac].mu_edca = !!mu_edca;
1999                         if (mu_edca)
2000                                 params[ac].mu_edca_param_rec = mu_edca->ac_vi;
2001                         break;
2002                 case 3: /* AC_VO */
2003                         ac = IEEE80211_AC_VO;
2004                         if (acm)
2005                                 sdata->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
2006                         if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
2007                                 uapsd = true;
2008                         params[ac].mu_edca = !!mu_edca;
2009                         if (mu_edca)
2010                                 params[ac].mu_edca_param_rec = mu_edca->ac_vo;
2011                         break;
2012                 case 0: /* AC_BE */
2013                 default:
2014                         ac = IEEE80211_AC_BE;
2015                         if (acm)
2016                                 sdata->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
2017                         if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
2018                                 uapsd = true;
2019                         params[ac].mu_edca = !!mu_edca;
2020                         if (mu_edca)
2021                                 params[ac].mu_edca_param_rec = mu_edca->ac_be;
2022                         break;
2023                 }
2024
2025                 params[ac].aifs = pos[0] & 0x0f;
2026
2027                 if (params[ac].aifs < 2) {
2028                         sdata_info(sdata,
2029                                    "AP has invalid WMM params (AIFSN=%d for ACI %d), will use 2\n",
2030                                    params[ac].aifs, aci);
2031                         params[ac].aifs = 2;
2032                 }
2033                 params[ac].cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
2034                 params[ac].cw_min = ecw2cw(pos[1] & 0x0f);
2035                 params[ac].txop = get_unaligned_le16(pos + 2);
2036                 params[ac].acm = acm;
2037                 params[ac].uapsd = uapsd;
2038
2039                 if (params[ac].cw_min == 0 ||
2040                     params[ac].cw_min > params[ac].cw_max) {
2041                         sdata_info(sdata,
2042                                    "AP has invalid WMM params (CWmin/max=%d/%d for ACI %d), using defaults\n",
2043                                    params[ac].cw_min, params[ac].cw_max, aci);
2044                         return false;
2045                 }
2046                 ieee80211_regulatory_limit_wmm_params(sdata, &params[ac], ac);
2047         }
2048
2049         for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
2050                 mlme_dbg(sdata,
2051                          "WMM AC=%d acm=%d aifs=%d cWmin=%d cWmax=%d txop=%d uapsd=%d, downgraded=%d\n",
2052                          ac, params[ac].acm,
2053                          params[ac].aifs, params[ac].cw_min, params[ac].cw_max,
2054                          params[ac].txop, params[ac].uapsd,
2055                          ifmgd->tx_tspec[ac].downgraded);
2056                 sdata->tx_conf[ac] = params[ac];
2057                 if (!ifmgd->tx_tspec[ac].downgraded &&
2058                     drv_conf_tx(local, sdata, ac, &params[ac]))
2059                         sdata_err(sdata,
2060                                   "failed to set TX queue parameters for AC %d\n",
2061                                   ac);
2062         }
2063
2064         /* enable WMM or activate new settings */
2065         sdata->vif.bss_conf.qos = true;
2066         return true;
2067 }
2068
2069 static void __ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
2070 {
2071         lockdep_assert_held(&sdata->local->mtx);
2072
2073         sdata->u.mgd.flags &= ~IEEE80211_STA_CONNECTION_POLL;
2074         ieee80211_run_deferred_scan(sdata->local);
2075 }
2076
2077 static void ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
2078 {
2079         mutex_lock(&sdata->local->mtx);
2080         __ieee80211_stop_poll(sdata);
2081         mutex_unlock(&sdata->local->mtx);
2082 }
2083
2084 static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
2085                                            u16 capab, bool erp_valid, u8 erp)
2086 {
2087         struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
2088         struct ieee80211_supported_band *sband;
2089         u32 changed = 0;
2090         bool use_protection;
2091         bool use_short_preamble;
2092         bool use_short_slot;
2093
2094         sband = ieee80211_get_sband(sdata);
2095         if (!sband)
2096                 return changed;
2097
2098         if (erp_valid) {
2099                 use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
2100                 use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
2101         } else {
2102                 use_protection = false;
2103                 use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
2104         }
2105
2106         use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
2107         if (sband->band == NL80211_BAND_5GHZ)
2108                 use_short_slot = true;
2109
2110         if (use_protection != bss_conf->use_cts_prot) {
2111                 bss_conf->use_cts_prot = use_protection;
2112                 changed |= BSS_CHANGED_ERP_CTS_PROT;
2113         }
2114
2115         if (use_short_preamble != bss_conf->use_short_preamble) {
2116                 bss_conf->use_short_preamble = use_short_preamble;
2117                 changed |= BSS_CHANGED_ERP_PREAMBLE;
2118         }
2119
2120         if (use_short_slot != bss_conf->use_short_slot) {
2121                 bss_conf->use_short_slot = use_short_slot;
2122                 changed |= BSS_CHANGED_ERP_SLOT;
2123         }
2124
2125         return changed;
2126 }
2127
2128 static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
2129                                      struct cfg80211_bss *cbss,
2130                                      u32 bss_info_changed)
2131 {
2132         struct ieee80211_bss *bss = (void *)cbss->priv;
2133         struct ieee80211_local *local = sdata->local;
2134         struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
2135
2136         bss_info_changed |= BSS_CHANGED_ASSOC;
2137         bss_info_changed |= ieee80211_handle_bss_capability(sdata,
2138                 bss_conf->assoc_capability, bss->has_erp_value, bss->erp_value);
2139
2140         sdata->u.mgd.beacon_timeout = usecs_to_jiffies(ieee80211_tu_to_usec(
2141                 beacon_loss_count * bss_conf->beacon_int));
2142
2143         sdata->u.mgd.associated = cbss;
2144         memcpy(sdata->u.mgd.bssid, cbss->bssid, ETH_ALEN);
2145
2146         ieee80211_check_rate_mask(sdata);
2147
2148         sdata->u.mgd.flags |= IEEE80211_STA_RESET_SIGNAL_AVE;
2149
2150         if (sdata->vif.p2p ||
2151             sdata->vif.driver_flags & IEEE80211_VIF_GET_NOA_UPDATE) {
2152                 const struct cfg80211_bss_ies *ies;
2153
2154                 rcu_read_lock();
2155                 ies = rcu_dereference(cbss->ies);
2156                 if (ies) {
2157                         int ret;
2158
2159                         ret = cfg80211_get_p2p_attr(
2160                                         ies->data, ies->len,
2161                                         IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
2162                                         (u8 *) &bss_conf->p2p_noa_attr,
2163                                         sizeof(bss_conf->p2p_noa_attr));
2164                         if (ret >= 2) {
2165                                 sdata->u.mgd.p2p_noa_index =
2166                                         bss_conf->p2p_noa_attr.index;
2167                                 bss_info_changed |= BSS_CHANGED_P2P_PS;
2168                         }
2169                 }
2170                 rcu_read_unlock();
2171         }
2172
2173         /* just to be sure */
2174         ieee80211_stop_poll(sdata);
2175
2176         ieee80211_led_assoc(local, 1);
2177
2178         if (sdata->u.mgd.have_beacon) {
2179                 /*
2180                  * If the AP is buggy we may get here with no DTIM period
2181                  * known, so assume it's 1 which is the only safe assumption
2182                  * in that case, although if the TIM IE is broken powersave
2183                  * probably just won't work at all.
2184                  */
2185                 bss_conf->dtim_period = sdata->u.mgd.dtim_period ?: 1;
2186                 bss_conf->beacon_rate = bss->beacon_rate;
2187                 bss_info_changed |= BSS_CHANGED_BEACON_INFO;
2188         } else {
2189                 bss_conf->beacon_rate = NULL;
2190                 bss_conf->dtim_period = 0;
2191         }
2192
2193         bss_conf->assoc = 1;
2194
2195         /* Tell the driver to monitor connection quality (if supported) */
2196         if (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI &&
2197             bss_conf->cqm_rssi_thold)
2198                 bss_info_changed |= BSS_CHANGED_CQM;
2199
2200         /* Enable ARP filtering */
2201         if (bss_conf->arp_addr_cnt)
2202                 bss_info_changed |= BSS_CHANGED_ARP_FILTER;
2203
2204         ieee80211_bss_info_change_notify(sdata, bss_info_changed);
2205
2206         mutex_lock(&local->iflist_mtx);
2207         ieee80211_recalc_ps(local);
2208         mutex_unlock(&local->iflist_mtx);
2209
2210         ieee80211_recalc_smps(sdata);
2211         ieee80211_recalc_ps_vif(sdata);
2212
2213         netif_carrier_on(sdata->dev);
2214 }
2215
2216 static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
2217                                    u16 stype, u16 reason, bool tx,
2218                                    u8 *frame_buf)
2219 {
2220         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2221         struct ieee80211_local *local = sdata->local;
2222         u32 changed = 0;
2223
2224         sdata_assert_lock(sdata);
2225
2226         if (WARN_ON_ONCE(tx && !frame_buf))
2227                 return;
2228
2229         if (WARN_ON(!ifmgd->associated))
2230                 return;
2231
2232         ieee80211_stop_poll(sdata);
2233
2234         ifmgd->associated = NULL;
2235         netif_carrier_off(sdata->dev);
2236
2237         /*
2238          * if we want to get out of ps before disassoc (why?) we have
2239          * to do it before sending disassoc, as otherwise the null-packet
2240          * won't be valid.
2241          */
2242         if (local->hw.conf.flags & IEEE80211_CONF_PS) {
2243                 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
2244                 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
2245         }
2246         local->ps_sdata = NULL;
2247
2248         /* disable per-vif ps */
2249         ieee80211_recalc_ps_vif(sdata);
2250
2251         /* make sure ongoing transmission finishes */
2252         synchronize_net();
2253
2254         /*
2255          * drop any frame before deauth/disassoc, this can be data or
2256          * management frame. Since we are disconnecting, we should not
2257          * insist sending these frames which can take time and delay
2258          * the disconnection and possible the roaming.
2259          */
2260         if (tx)
2261                 ieee80211_flush_queues(local, sdata, true);
2262
2263         /* deauthenticate/disassociate now */
2264         if (tx || frame_buf) {
2265                 /*
2266                  * In multi channel scenarios guarantee that the virtual
2267                  * interface is granted immediate airtime to transmit the
2268                  * deauthentication frame by calling mgd_prepare_tx, if the
2269                  * driver requested so.
2270                  */
2271                 if (ieee80211_hw_check(&local->hw, DEAUTH_NEED_MGD_TX_PREP) &&
2272                     !ifmgd->have_beacon)
2273                         drv_mgd_prepare_tx(sdata->local, sdata, 0);
2274
2275                 ieee80211_send_deauth_disassoc(sdata, ifmgd->bssid, stype,
2276                                                reason, tx, frame_buf);
2277         }
2278
2279         /* flush out frame - make sure the deauth was actually sent */
2280         if (tx)
2281                 ieee80211_flush_queues(local, sdata, false);
2282
2283         /* clear bssid only after building the needed mgmt frames */
2284         eth_zero_addr(ifmgd->bssid);
2285
2286         /* remove AP and TDLS peers */
2287         sta_info_flush(sdata);
2288
2289         /* finally reset all BSS / config parameters */
2290         changed |= ieee80211_reset_erp_info(sdata);
2291
2292         ieee80211_led_assoc(local, 0);
2293         changed |= BSS_CHANGED_ASSOC;
2294         sdata->vif.bss_conf.assoc = false;
2295
2296         ifmgd->p2p_noa_index = -1;
2297         memset(&sdata->vif.bss_conf.p2p_noa_attr, 0,
2298                sizeof(sdata->vif.bss_conf.p2p_noa_attr));
2299
2300         /* on the next assoc, re-program HT/VHT parameters */
2301         memset(&ifmgd->ht_capa, 0, sizeof(ifmgd->ht_capa));
2302         memset(&ifmgd->ht_capa_mask, 0, sizeof(ifmgd->ht_capa_mask));
2303         memset(&ifmgd->vht_capa, 0, sizeof(ifmgd->vht_capa));
2304         memset(&ifmgd->vht_capa_mask, 0, sizeof(ifmgd->vht_capa_mask));
2305
2306         /* reset MU-MIMO ownership and group data */
2307         memset(sdata->vif.bss_conf.mu_group.membership, 0,
2308                sizeof(sdata->vif.bss_conf.mu_group.membership));
2309         memset(sdata->vif.bss_conf.mu_group.position, 0,
2310                sizeof(sdata->vif.bss_conf.mu_group.position));
2311         changed |= BSS_CHANGED_MU_GROUPS;
2312         sdata->vif.mu_mimo_owner = false;
2313
2314         sdata->ap_power_level = IEEE80211_UNSET_POWER_LEVEL;
2315
2316         del_timer_sync(&local->dynamic_ps_timer);
2317         cancel_work_sync(&local->dynamic_ps_enable_work);
2318
2319         /* Disable ARP filtering */
2320         if (sdata->vif.bss_conf.arp_addr_cnt)
2321                 changed |= BSS_CHANGED_ARP_FILTER;
2322
2323         sdata->vif.bss_conf.qos = false;
2324         changed |= BSS_CHANGED_QOS;
2325
2326         /* The BSSID (not really interesting) and HT changed */
2327         changed |= BSS_CHANGED_BSSID | BSS_CHANGED_HT;
2328         ieee80211_bss_info_change_notify(sdata, changed);
2329
2330         /* disassociated - set to defaults now */
2331         ieee80211_set_wmm_default(sdata, false, false);
2332
2333         del_timer_sync(&sdata->u.mgd.conn_mon_timer);
2334         del_timer_sync(&sdata->u.mgd.bcn_mon_timer);
2335         del_timer_sync(&sdata->u.mgd.timer);
2336         del_timer_sync(&sdata->u.mgd.chswitch_timer);
2337
2338         sdata->vif.bss_conf.dtim_period = 0;
2339         sdata->vif.bss_conf.beacon_rate = NULL;
2340
2341         ifmgd->have_beacon = false;
2342
2343         ifmgd->flags = 0;
2344         mutex_lock(&local->mtx);
2345         ieee80211_vif_release_channel(sdata);
2346
2347         sdata->vif.csa_active = false;
2348         ifmgd->csa_waiting_bcn = false;
2349         ifmgd->csa_ignored_same_chan = false;
2350         if (sdata->csa_block_tx) {
2351                 ieee80211_wake_vif_queues(local, sdata,
2352                                           IEEE80211_QUEUE_STOP_REASON_CSA);
2353                 sdata->csa_block_tx = false;
2354         }
2355         mutex_unlock(&local->mtx);
2356
2357         /* existing TX TSPEC sessions no longer exist */
2358         memset(ifmgd->tx_tspec, 0, sizeof(ifmgd->tx_tspec));
2359         cancel_delayed_work_sync(&ifmgd->tx_tspec_wk);
2360
2361         sdata->encrypt_headroom = IEEE80211_ENCRYPT_HEADROOM;
2362 }
2363
2364 void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
2365                              struct ieee80211_hdr *hdr)
2366 {
2367         /*
2368          * We can postpone the mgd.timer whenever receiving unicast frames
2369          * from AP because we know that the connection is working both ways
2370          * at that time. But multicast frames (and hence also beacons) must
2371          * be ignored here, because we need to trigger the timer during
2372          * data idle periods for sending the periodic probe request to the
2373          * AP we're connected to.
2374          */
2375         if (is_multicast_ether_addr(hdr->addr1))
2376                 return;
2377
2378         ieee80211_sta_reset_conn_monitor(sdata);
2379 }
2380
2381 static void ieee80211_reset_ap_probe(struct ieee80211_sub_if_data *sdata)
2382 {
2383         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2384         struct ieee80211_local *local = sdata->local;
2385
2386         mutex_lock(&local->mtx);
2387         if (!(ifmgd->flags & IEEE80211_STA_CONNECTION_POLL))
2388                 goto out;
2389
2390         __ieee80211_stop_poll(sdata);
2391
2392         mutex_lock(&local->iflist_mtx);
2393         ieee80211_recalc_ps(local);
2394         mutex_unlock(&local->iflist_mtx);
2395
2396         if (ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
2397                 goto out;
2398
2399         /*
2400          * We've received a probe response, but are not sure whether
2401          * we have or will be receiving any beacons or data, so let's
2402          * schedule the timers again, just in case.
2403          */
2404         ieee80211_sta_reset_beacon_monitor(sdata);
2405
2406         mod_timer(&ifmgd->conn_mon_timer,
2407                   round_jiffies_up(jiffies +
2408                                    IEEE80211_CONNECTION_IDLE_TIME));
2409 out:
2410         mutex_unlock(&local->mtx);
2411 }
2412
2413 static void ieee80211_sta_tx_wmm_ac_notify(struct ieee80211_sub_if_data *sdata,
2414                                            struct ieee80211_hdr *hdr,
2415                                            u16 tx_time)
2416 {
2417         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2418         u16 tid = ieee80211_get_tid(hdr);
2419         int ac = ieee80211_ac_from_tid(tid);
2420         struct ieee80211_sta_tx_tspec *tx_tspec = &ifmgd->tx_tspec[ac];
2421         unsigned long now = jiffies;
2422
2423         if (likely(!tx_tspec->admitted_time))
2424                 return;
2425
2426         if (time_after(now, tx_tspec->time_slice_start + HZ)) {
2427                 tx_tspec->consumed_tx_time = 0;
2428                 tx_tspec->time_slice_start = now;
2429
2430                 if (tx_tspec->downgraded) {
2431                         tx_tspec->action = TX_TSPEC_ACTION_STOP_DOWNGRADE;
2432                         schedule_delayed_work(&ifmgd->tx_tspec_wk, 0);
2433                 }
2434         }
2435
2436         if (tx_tspec->downgraded)
2437                 return;
2438
2439         tx_tspec->consumed_tx_time += tx_time;
2440
2441         if (tx_tspec->consumed_tx_time >= tx_tspec->admitted_time) {
2442                 tx_tspec->downgraded = true;
2443                 tx_tspec->action = TX_TSPEC_ACTION_DOWNGRADE;
2444                 schedule_delayed_work(&ifmgd->tx_tspec_wk, 0);
2445         }
2446 }
2447
2448 void ieee80211_sta_tx_notify(struct ieee80211_sub_if_data *sdata,
2449                              struct ieee80211_hdr *hdr, bool ack, u16 tx_time)
2450 {
2451         ieee80211_sta_tx_wmm_ac_notify(sdata, hdr, tx_time);
2452
2453         if (!ieee80211_is_data(hdr->frame_control))
2454             return;
2455
2456         if (ieee80211_is_nullfunc(hdr->frame_control) &&
2457             sdata->u.mgd.probe_send_count > 0) {
2458                 if (ack)
2459                         ieee80211_sta_reset_conn_monitor(sdata);
2460                 else
2461                         sdata->u.mgd.nullfunc_failed = true;
2462                 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
2463                 return;
2464         }
2465
2466         if (ack)
2467                 ieee80211_sta_reset_conn_monitor(sdata);
2468 }
2469
2470 static void ieee80211_mlme_send_probe_req(struct ieee80211_sub_if_data *sdata,
2471                                           const u8 *src, const u8 *dst,
2472                                           const u8 *ssid, size_t ssid_len,
2473                                           struct ieee80211_channel *channel)
2474 {
2475         struct sk_buff *skb;
2476
2477         skb = ieee80211_build_probe_req(sdata, src, dst, (u32)-1, channel,
2478                                         ssid, ssid_len, NULL, 0,
2479                                         IEEE80211_PROBE_FLAG_DIRECTED);
2480         if (skb)
2481                 ieee80211_tx_skb(sdata, skb);
2482 }
2483
2484 static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
2485 {
2486         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2487         const u8 *ssid;
2488         u8 *dst = ifmgd->associated->bssid;
2489         u8 unicast_limit = max(1, max_probe_tries - 3);
2490         struct sta_info *sta;
2491
2492         /*
2493          * Try sending broadcast probe requests for the last three
2494          * probe requests after the first ones failed since some
2495          * buggy APs only support broadcast probe requests.
2496          */
2497         if (ifmgd->probe_send_count >= unicast_limit)
2498                 dst = NULL;
2499
2500         /*
2501          * When the hardware reports an accurate Tx ACK status, it's
2502          * better to send a nullfunc frame instead of a probe request,
2503          * as it will kick us off the AP quickly if we aren't associated
2504          * anymore. The timeout will be reset if the frame is ACKed by
2505          * the AP.
2506          */
2507         ifmgd->probe_send_count++;
2508
2509         if (dst) {
2510                 mutex_lock(&sdata->local->sta_mtx);
2511                 sta = sta_info_get(sdata, dst);
2512                 if (!WARN_ON(!sta))
2513                         ieee80211_check_fast_rx(sta);
2514                 mutex_unlock(&sdata->local->sta_mtx);
2515         }
2516
2517         if (ieee80211_hw_check(&sdata->local->hw, REPORTS_TX_ACK_STATUS)) {
2518                 ifmgd->nullfunc_failed = false;
2519                 ieee80211_send_nullfunc(sdata->local, sdata, false);
2520         } else {
2521                 int ssid_len;
2522
2523                 rcu_read_lock();
2524                 ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
2525                 if (WARN_ON_ONCE(ssid == NULL))
2526                         ssid_len = 0;
2527                 else
2528                         ssid_len = ssid[1];
2529
2530                 ieee80211_mlme_send_probe_req(sdata, sdata->vif.addr, dst,
2531                                               ssid + 2, ssid_len,
2532                                               ifmgd->associated->channel);
2533                 rcu_read_unlock();
2534         }
2535
2536         ifmgd->probe_timeout = jiffies + msecs_to_jiffies(probe_wait_ms);
2537         run_again(sdata, ifmgd->probe_timeout);
2538 }
2539
2540 static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
2541                                    bool beacon)
2542 {
2543         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2544         bool already = false;
2545
2546         if (!ieee80211_sdata_running(sdata))
2547                 return;
2548
2549         sdata_lock(sdata);
2550
2551         if (!ifmgd->associated)
2552                 goto out;
2553
2554         mutex_lock(&sdata->local->mtx);
2555
2556         if (sdata->local->tmp_channel || sdata->local->scanning) {
2557                 mutex_unlock(&sdata->local->mtx);
2558                 goto out;
2559         }
2560
2561         if (beacon) {
2562                 mlme_dbg_ratelimited(sdata,
2563                                      "detected beacon loss from AP (missed %d beacons) - probing\n",
2564                                      beacon_loss_count);
2565
2566                 ieee80211_cqm_beacon_loss_notify(&sdata->vif, GFP_KERNEL);
2567         }
2568
2569         /*
2570          * The driver/our work has already reported this event or the
2571          * connection monitoring has kicked in and we have already sent
2572          * a probe request. Or maybe the AP died and the driver keeps
2573          * reporting until we disassociate...
2574          *
2575          * In either case we have to ignore the current call to this
2576          * function (except for setting the correct probe reason bit)
2577          * because otherwise we would reset the timer every time and
2578          * never check whether we received a probe response!
2579          */
2580         if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL)
2581                 already = true;
2582
2583         ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
2584
2585         mutex_unlock(&sdata->local->mtx);
2586
2587         if (already)
2588                 goto out;
2589
2590         mutex_lock(&sdata->local->iflist_mtx);
2591         ieee80211_recalc_ps(sdata->local);
2592         mutex_unlock(&sdata->local->iflist_mtx);
2593
2594         ifmgd->probe_send_count = 0;
2595         ieee80211_mgd_probe_ap_send(sdata);
2596  out:
2597         sdata_unlock(sdata);
2598 }
2599
2600 struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw,
2601                                           struct ieee80211_vif *vif)
2602 {
2603         struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2604         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2605         struct cfg80211_bss *cbss;
2606         struct sk_buff *skb;
2607         const u8 *ssid;
2608         int ssid_len;
2609
2610         if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
2611                 return NULL;
2612
2613         sdata_assert_lock(sdata);
2614
2615         if (ifmgd->associated)
2616                 cbss = ifmgd->associated;
2617         else if (ifmgd->auth_data)
2618                 cbss = ifmgd->auth_data->bss;
2619         else if (ifmgd->assoc_data)
2620                 cbss = ifmgd->assoc_data->bss;
2621         else
2622                 return NULL;
2623
2624         rcu_read_lock();
2625         ssid = ieee80211_bss_get_ie(cbss, WLAN_EID_SSID);
2626         if (WARN_ON_ONCE(ssid == NULL))
2627                 ssid_len = 0;
2628         else
2629                 ssid_len = ssid[1];
2630
2631         skb = ieee80211_build_probe_req(sdata, sdata->vif.addr, cbss->bssid,
2632                                         (u32) -1, cbss->channel,
2633                                         ssid + 2, ssid_len,
2634                                         NULL, 0, IEEE80211_PROBE_FLAG_DIRECTED);
2635         rcu_read_unlock();
2636
2637         return skb;
2638 }
2639 EXPORT_SYMBOL(ieee80211_ap_probereq_get);
2640
2641 static void ieee80211_report_disconnect(struct ieee80211_sub_if_data *sdata,
2642                                         const u8 *buf, size_t len, bool tx,
2643                                         u16 reason)
2644 {
2645         struct ieee80211_event event = {
2646                 .type = MLME_EVENT,
2647                 .u.mlme.data = tx ? DEAUTH_TX_EVENT : DEAUTH_RX_EVENT,
2648                 .u.mlme.reason = reason,
2649         };
2650
2651         if (tx)
2652                 cfg80211_tx_mlme_mgmt(sdata->dev, buf, len);
2653         else
2654                 cfg80211_rx_mlme_mgmt(sdata->dev, buf, len);
2655
2656         drv_event_callback(sdata->local, sdata, &event);
2657 }
2658
2659 static void __ieee80211_disconnect(struct ieee80211_sub_if_data *sdata)
2660 {
2661         struct ieee80211_local *local = sdata->local;
2662         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2663         u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
2664         bool tx;
2665
2666         sdata_lock(sdata);
2667         if (!ifmgd->associated) {
2668                 sdata_unlock(sdata);
2669                 return;
2670         }
2671
2672         tx = !sdata->csa_block_tx;
2673
2674         /* AP is probably out of range (or not reachable for another reason) so
2675          * remove the bss struct for that AP.
2676          */
2677         cfg80211_unlink_bss(local->hw.wiphy, ifmgd->associated);
2678
2679         ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
2680                                WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
2681                                tx, frame_buf);
2682         mutex_lock(&local->mtx);
2683         sdata->vif.csa_active = false;
2684         ifmgd->csa_waiting_bcn = false;
2685         if (sdata->csa_block_tx) {
2686                 ieee80211_wake_vif_queues(local, sdata,
2687                                           IEEE80211_QUEUE_STOP_REASON_CSA);
2688                 sdata->csa_block_tx = false;
2689         }
2690         mutex_unlock(&local->mtx);
2691
2692         ieee80211_report_disconnect(sdata, frame_buf, sizeof(frame_buf), tx,
2693                                     WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
2694
2695         sdata_unlock(sdata);
2696 }
2697
2698 static void ieee80211_beacon_connection_loss_work(struct work_struct *work)
2699 {
2700         struct ieee80211_sub_if_data *sdata =
2701                 container_of(work, struct ieee80211_sub_if_data,
2702                              u.mgd.beacon_connection_loss_work);
2703         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2704
2705         if (ifmgd->associated)
2706                 ifmgd->beacon_loss_count++;
2707
2708         if (ifmgd->connection_loss) {
2709                 sdata_info(sdata, "Connection to AP %pM lost\n",
2710                            ifmgd->bssid);
2711                 __ieee80211_disconnect(sdata);
2712         } else {
2713                 ieee80211_mgd_probe_ap(sdata, true);
2714         }
2715 }
2716
2717 static void ieee80211_csa_connection_drop_work(struct work_struct *work)
2718 {
2719         struct ieee80211_sub_if_data *sdata =
2720                 container_of(work, struct ieee80211_sub_if_data,
2721                              u.mgd.csa_connection_drop_work);
2722
2723         __ieee80211_disconnect(sdata);
2724 }
2725
2726 void ieee80211_beacon_loss(struct ieee80211_vif *vif)
2727 {
2728         struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2729         struct ieee80211_hw *hw = &sdata->local->hw;
2730
2731         trace_api_beacon_loss(sdata);
2732
2733         sdata->u.mgd.connection_loss = false;
2734         ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
2735 }
2736 EXPORT_SYMBOL(ieee80211_beacon_loss);
2737
2738 void ieee80211_connection_loss(struct ieee80211_vif *vif)
2739 {
2740         struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2741         struct ieee80211_hw *hw = &sdata->local->hw;
2742
2743         trace_api_connection_loss(sdata);
2744
2745         sdata->u.mgd.connection_loss = true;
2746         ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
2747 }
2748 EXPORT_SYMBOL(ieee80211_connection_loss);
2749
2750
2751 static void ieee80211_destroy_auth_data(struct ieee80211_sub_if_data *sdata,
2752                                         bool assoc)
2753 {
2754         struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
2755
2756         sdata_assert_lock(sdata);
2757
2758         if (!assoc) {
2759                 /*
2760                  * we are not authenticated yet, the only timer that could be
2761                  * running is the timeout for the authentication response which
2762                  * which is not relevant anymore.
2763                  */
2764                 del_timer_sync(&sdata->u.mgd.timer);
2765                 sta_info_destroy_addr(sdata, auth_data->bss->bssid);
2766
2767                 eth_zero_addr(sdata->u.mgd.bssid);
2768                 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
2769                 sdata->u.mgd.flags = 0;
2770                 mutex_lock(&sdata->local->mtx);
2771                 ieee80211_vif_release_channel(sdata);
2772                 mutex_unlock(&sdata->local->mtx);
2773         }
2774
2775         cfg80211_put_bss(sdata->local->hw.wiphy, auth_data->bss);
2776         kfree(auth_data);
2777         sdata->u.mgd.auth_data = NULL;
2778 }
2779
2780 static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata,
2781                                          bool assoc, bool abandon)
2782 {
2783         struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
2784
2785         sdata_assert_lock(sdata);
2786
2787         if (!assoc) {
2788                 /*
2789                  * we are not associated yet, the only timer that could be
2790                  * running is the timeout for the association response which
2791                  * which is not relevant anymore.
2792                  */
2793                 del_timer_sync(&sdata->u.mgd.timer);
2794                 sta_info_destroy_addr(sdata, assoc_data->bss->bssid);
2795
2796                 eth_zero_addr(sdata->u.mgd.bssid);
2797                 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
2798                 sdata->u.mgd.flags = 0;
2799                 sdata->vif.mu_mimo_owner = false;
2800
2801                 mutex_lock(&sdata->local->mtx);
2802                 ieee80211_vif_release_channel(sdata);
2803                 mutex_unlock(&sdata->local->mtx);
2804
2805                 if (abandon)
2806                         cfg80211_abandon_assoc(sdata->dev, assoc_data->bss);
2807         }
2808
2809         kfree(assoc_data);
2810         sdata->u.mgd.assoc_data = NULL;
2811 }
2812
2813 static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
2814                                      struct ieee80211_mgmt *mgmt, size_t len)
2815 {
2816         struct ieee80211_local *local = sdata->local;
2817         struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
2818         u8 *pos;
2819         struct ieee802_11_elems elems;
2820         u32 tx_flags = 0;
2821
2822         pos = mgmt->u.auth.variable;
2823         ieee802_11_parse_elems(pos, len - (pos - (u8 *)mgmt), false, &elems,
2824                                mgmt->bssid, auth_data->bss->bssid);
2825         if (!elems.challenge)
2826                 return;
2827         auth_data->expected_transaction = 4;
2828         drv_mgd_prepare_tx(sdata->local, sdata, 0);
2829         if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
2830                 tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
2831                            IEEE80211_TX_INTFL_MLME_CONN_TX;
2832         ieee80211_send_auth(sdata, 3, auth_data->algorithm, 0,
2833                             elems.challenge - 2, elems.challenge_len + 2,
2834                             auth_data->bss->bssid, auth_data->bss->bssid,
2835                             auth_data->key, auth_data->key_len,
2836                             auth_data->key_idx, tx_flags);
2837 }
2838
2839 static bool ieee80211_mark_sta_auth(struct ieee80211_sub_if_data *sdata,
2840                                     const u8 *bssid)
2841 {
2842         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2843         struct sta_info *sta;
2844         bool result = true;
2845
2846         sdata_info(sdata, "authenticated\n");
2847         ifmgd->auth_data->done = true;
2848         ifmgd->auth_data->timeout = jiffies + IEEE80211_AUTH_WAIT_ASSOC;
2849         ifmgd->auth_data->timeout_started = true;
2850         run_again(sdata, ifmgd->auth_data->timeout);
2851
2852         /* move station state to auth */
2853         mutex_lock(&sdata->local->sta_mtx);
2854         sta = sta_info_get(sdata, bssid);
2855         if (!sta) {
2856                 WARN_ONCE(1, "%s: STA %pM not found", sdata->name, bssid);
2857                 result = false;
2858                 goto out;
2859         }
2860         if (sta_info_move_state(sta, IEEE80211_STA_AUTH)) {
2861                 sdata_info(sdata, "failed moving %pM to auth\n", bssid);
2862                 result = false;
2863                 goto out;
2864         }
2865
2866 out:
2867         mutex_unlock(&sdata->local->sta_mtx);
2868         return result;
2869 }
2870
2871 static void ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
2872                                    struct ieee80211_mgmt *mgmt, size_t len)
2873 {
2874         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2875         u8 bssid[ETH_ALEN];
2876         u16 auth_alg, auth_transaction, status_code;
2877         struct ieee80211_event event = {
2878                 .type = MLME_EVENT,
2879                 .u.mlme.data = AUTH_EVENT,
2880         };
2881
2882         sdata_assert_lock(sdata);
2883
2884         if (len < 24 + 6)
2885                 return;
2886
2887         if (!ifmgd->auth_data || ifmgd->auth_data->done)
2888                 return;
2889
2890         memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
2891
2892         if (!ether_addr_equal(bssid, mgmt->bssid))
2893                 return;
2894
2895         auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
2896         auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
2897         status_code = le16_to_cpu(mgmt->u.auth.status_code);
2898
2899         if (auth_alg != ifmgd->auth_data->algorithm ||
2900             (auth_alg != WLAN_AUTH_SAE &&
2901              auth_transaction != ifmgd->auth_data->expected_transaction) ||
2902             (auth_alg == WLAN_AUTH_SAE &&
2903              (auth_transaction < ifmgd->auth_data->expected_transaction ||
2904               auth_transaction > 2))) {
2905                 sdata_info(sdata, "%pM unexpected authentication state: alg %d (expected %d) transact %d (expected %d)\n",
2906                            mgmt->sa, auth_alg, ifmgd->auth_data->algorithm,
2907                            auth_transaction,
2908                            ifmgd->auth_data->expected_transaction);
2909                 return;
2910         }
2911
2912         if (status_code != WLAN_STATUS_SUCCESS) {
2913                 sdata_info(sdata, "%pM denied authentication (status %d)\n",
2914                            mgmt->sa, status_code);
2915                 ieee80211_destroy_auth_data(sdata, false);
2916                 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
2917                 event.u.mlme.status = MLME_DENIED;
2918                 event.u.mlme.reason = status_code;
2919                 drv_event_callback(sdata->local, sdata, &event);
2920                 return;
2921         }
2922
2923         switch (ifmgd->auth_data->algorithm) {
2924         case WLAN_AUTH_OPEN:
2925         case WLAN_AUTH_LEAP:
2926         case WLAN_AUTH_FT:
2927         case WLAN_AUTH_SAE:
2928         case WLAN_AUTH_FILS_SK:
2929         case WLAN_AUTH_FILS_SK_PFS:
2930         case WLAN_AUTH_FILS_PK:
2931                 break;
2932         case WLAN_AUTH_SHARED_KEY:
2933                 if (ifmgd->auth_data->expected_transaction != 4) {
2934                         ieee80211_auth_challenge(sdata, mgmt, len);
2935                         /* need another frame */
2936                         return;
2937                 }
2938                 break;
2939         default:
2940                 WARN_ONCE(1, "invalid auth alg %d",
2941                           ifmgd->auth_data->algorithm);
2942                 return;
2943         }
2944
2945         event.u.mlme.status = MLME_SUCCESS;
2946         drv_event_callback(sdata->local, sdata, &event);
2947         if (ifmgd->auth_data->algorithm != WLAN_AUTH_SAE ||
2948             (auth_transaction == 2 &&
2949              ifmgd->auth_data->expected_transaction == 2)) {
2950                 if (!ieee80211_mark_sta_auth(sdata, bssid))
2951                         goto out_err;
2952         } else if (ifmgd->auth_data->algorithm == WLAN_AUTH_SAE &&
2953                    auth_transaction == 2) {
2954                 sdata_info(sdata, "SAE peer confirmed\n");
2955                 ifmgd->auth_data->peer_confirmed = true;
2956         }
2957
2958         cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
2959         return;
2960  out_err:
2961         mutex_unlock(&sdata->local->sta_mtx);
2962         /* ignore frame -- wait for timeout */
2963 }
2964
2965 #define case_WLAN(type) \
2966         case WLAN_REASON_##type: return #type
2967
2968 static const char *ieee80211_get_reason_code_string(u16 reason_code)
2969 {
2970         switch (reason_code) {
2971         case_WLAN(UNSPECIFIED);
2972         case_WLAN(PREV_AUTH_NOT_VALID);
2973         case_WLAN(DEAUTH_LEAVING);
2974         case_WLAN(DISASSOC_DUE_TO_INACTIVITY);
2975         case_WLAN(DISASSOC_AP_BUSY);
2976         case_WLAN(CLASS2_FRAME_FROM_NONAUTH_STA);
2977         case_WLAN(CLASS3_FRAME_FROM_NONASSOC_STA);
2978         case_WLAN(DISASSOC_STA_HAS_LEFT);
2979         case_WLAN(STA_REQ_ASSOC_WITHOUT_AUTH);
2980         case_WLAN(DISASSOC_BAD_POWER);
2981         case_WLAN(DISASSOC_BAD_SUPP_CHAN);
2982         case_WLAN(INVALID_IE);
2983         case_WLAN(MIC_FAILURE);
2984         case_WLAN(4WAY_HANDSHAKE_TIMEOUT);
2985         case_WLAN(GROUP_KEY_HANDSHAKE_TIMEOUT);
2986         case_WLAN(IE_DIFFERENT);
2987         case_WLAN(INVALID_GROUP_CIPHER);
2988         case_WLAN(INVALID_PAIRWISE_CIPHER);
2989         case_WLAN(INVALID_AKMP);
2990         case_WLAN(UNSUPP_RSN_VERSION);
2991         case_WLAN(INVALID_RSN_IE_CAP);
2992         case_WLAN(IEEE8021X_FAILED);
2993         case_WLAN(CIPHER_SUITE_REJECTED);
2994         case_WLAN(DISASSOC_UNSPECIFIED_QOS);
2995         case_WLAN(DISASSOC_QAP_NO_BANDWIDTH);
2996         case_WLAN(DISASSOC_LOW_ACK);
2997         case_WLAN(DISASSOC_QAP_EXCEED_TXOP);
2998         case_WLAN(QSTA_LEAVE_QBSS);
2999         case_WLAN(QSTA_NOT_USE);
3000         case_WLAN(QSTA_REQUIRE_SETUP);
3001         case_WLAN(QSTA_TIMEOUT);
3002         case_WLAN(QSTA_CIPHER_NOT_SUPP);
3003         case_WLAN(MESH_PEER_CANCELED);
3004         case_WLAN(MESH_MAX_PEERS);
3005         case_WLAN(MESH_CONFIG);
3006         case_WLAN(MESH_CLOSE);
3007         case_WLAN(MESH_MAX_RETRIES);
3008         case_WLAN(MESH_CONFIRM_TIMEOUT);
3009         case_WLAN(MESH_INVALID_GTK);
3010         case_WLAN(MESH_INCONSISTENT_PARAM);
3011         case_WLAN(MESH_INVALID_SECURITY);
3012         case_WLAN(MESH_PATH_ERROR);
3013         case_WLAN(MESH_PATH_NOFORWARD);
3014         case_WLAN(MESH_PATH_DEST_UNREACHABLE);
3015         case_WLAN(MAC_EXISTS_IN_MBSS);
3016         case_WLAN(MESH_CHAN_REGULATORY);
3017         case_WLAN(MESH_CHAN);
3018         default: return "<unknown>";
3019         }
3020 }
3021
3022 static void ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
3023                                      struct ieee80211_mgmt *mgmt, size_t len)
3024 {
3025         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3026         u16 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
3027
3028         sdata_assert_lock(sdata);
3029
3030         if (len < 24 + 2)
3031                 return;
3032
3033         if (ifmgd->associated &&
3034             ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid)) {
3035                 const u8 *bssid = ifmgd->associated->bssid;
3036
3037                 sdata_info(sdata, "deauthenticated from %pM (Reason: %u=%s)\n",
3038                            bssid, reason_code,
3039                            ieee80211_get_reason_code_string(reason_code));
3040
3041                 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
3042
3043                 ieee80211_report_disconnect(sdata, (u8 *)mgmt, len, false,
3044                                             reason_code);
3045                 return;
3046         }
3047
3048         if (ifmgd->assoc_data &&
3049             ether_addr_equal(mgmt->bssid, ifmgd->assoc_data->bss->bssid)) {
3050                 const u8 *bssid = ifmgd->assoc_data->bss->bssid;
3051
3052                 sdata_info(sdata,
3053                            "deauthenticated from %pM while associating (Reason: %u=%s)\n",
3054                            bssid, reason_code,
3055                            ieee80211_get_reason_code_string(reason_code));
3056
3057                 ieee80211_destroy_assoc_data(sdata, false, true);
3058
3059                 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
3060                 return;
3061         }
3062 }
3063
3064
3065 static void ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
3066                                        struct ieee80211_mgmt *mgmt, size_t len)
3067 {
3068         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3069         u16 reason_code;
3070
3071         sdata_assert_lock(sdata);
3072
3073         if (len < 24 + 2)
3074                 return;
3075
3076         if (!ifmgd->associated ||
3077             !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
3078                 return;
3079
3080         reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
3081
3082         sdata_info(sdata, "disassociated from %pM (Reason: %u=%s)\n",
3083                    mgmt->sa, reason_code,
3084                    ieee80211_get_reason_code_string(reason_code));
3085
3086         ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
3087
3088         ieee80211_report_disconnect(sdata, (u8 *)mgmt, len, false, reason_code);
3089 }
3090
3091 static void ieee80211_get_rates(struct ieee80211_supported_band *sband,
3092                                 u8 *supp_rates, unsigned int supp_rates_len,
3093                                 u32 *rates, u32 *basic_rates,
3094                                 bool *have_higher_than_11mbit,
3095                                 int *min_rate, int *min_rate_index,
3096                                 int shift)
3097 {
3098         int i, j;
3099
3100         for (i = 0; i < supp_rates_len; i++) {
3101                 int rate = supp_rates[i] & 0x7f;
3102                 bool is_basic = !!(supp_rates[i] & 0x80);
3103
3104                 if ((rate * 5 * (1 << shift)) > 110)
3105                         *have_higher_than_11mbit = true;
3106
3107                 /*
3108                  * Skip HT and VHT BSS membership selectors since they're not
3109                  * rates.
3110                  *
3111                  * Note: Even though the membership selector and the basic
3112                  *       rate flag share the same bit, they are not exactly
3113                  *       the same.
3114                  */
3115                 if (supp_rates[i] == (0x80 | BSS_MEMBERSHIP_SELECTOR_HT_PHY) ||
3116                     supp_rates[i] == (0x80 | BSS_MEMBERSHIP_SELECTOR_VHT_PHY))
3117                         continue;
3118
3119                 for (j = 0; j < sband->n_bitrates; j++) {
3120                         struct ieee80211_rate *br;
3121                         int brate;
3122
3123                         br = &sband->bitrates[j];
3124
3125                         brate = DIV_ROUND_UP(br->bitrate, (1 << shift) * 5);
3126                         if (brate == rate) {
3127                                 *rates |= BIT(j);
3128                                 if (is_basic)
3129                                         *basic_rates |= BIT(j);
3130                                 if ((rate * 5) < *min_rate) {
3131                                         *min_rate = rate * 5;
3132                                         *min_rate_index = j;
3133                                 }
3134                                 break;
3135                         }
3136                 }
3137         }
3138 }
3139
3140 static bool ieee80211_twt_req_supported(const struct sta_info *sta,
3141                                         const struct ieee802_11_elems *elems)
3142 {
3143         if (elems->ext_capab_len < 10)
3144                 return false;
3145
3146         if (!(elems->ext_capab[9] & WLAN_EXT_CAPA10_TWT_RESPONDER_SUPPORT))
3147                 return false;
3148
3149         return sta->sta.he_cap.he_cap_elem.mac_cap_info[0] &
3150                 IEEE80211_HE_MAC_CAP0_TWT_RES;
3151 }
3152
3153 static bool ieee80211_assoc_success(struct ieee80211_sub_if_data *sdata,
3154                                     struct cfg80211_bss *cbss,
3155                                     struct ieee80211_mgmt *mgmt, size_t len)
3156 {
3157         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3158         struct ieee80211_local *local = sdata->local;
3159         struct ieee80211_supported_band *sband;
3160         struct sta_info *sta;
3161         u8 *pos;
3162         u16 capab_info, aid;
3163         struct ieee802_11_elems elems;
3164         struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
3165         const struct cfg80211_bss_ies *bss_ies = NULL;
3166         struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
3167         u32 changed = 0;
3168         int err;
3169         bool ret;
3170
3171         /* AssocResp and ReassocResp have identical structure */
3172
3173         aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
3174         capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
3175
3176         /*
3177          * The 5 MSB of the AID field are reserved
3178          * (802.11-2016 9.4.1.8 AID field)
3179          */
3180         aid &= 0x7ff;
3181
3182         ifmgd->broken_ap = false;
3183
3184         if (aid == 0 || aid > IEEE80211_MAX_AID) {
3185                 sdata_info(sdata, "invalid AID value %d (out of range), turn off PS\n",
3186                            aid);
3187                 aid = 0;
3188                 ifmgd->broken_ap = true;
3189         }
3190
3191         pos = mgmt->u.assoc_resp.variable;
3192         ieee802_11_parse_elems(pos, len - (pos - (u8 *)mgmt), false, &elems,
3193                                mgmt->bssid, assoc_data->bss->bssid);
3194
3195         if (!elems.supp_rates) {
3196                 sdata_info(sdata, "no SuppRates element in AssocResp\n");
3197                 return false;
3198         }
3199
3200         ifmgd->aid = aid;
3201         ifmgd->tdls_chan_switch_prohibited =
3202                 elems.ext_capab && elems.ext_capab_len >= 5 &&
3203                 (elems.ext_capab[4] & WLAN_EXT_CAPA5_TDLS_CH_SW_PROHIBITED);
3204
3205         /*
3206          * Some APs are erroneously not including some information in their
3207          * (re)association response frames. Try to recover by using the data
3208          * from the beacon or probe response. This seems to afflict mobile
3209          * 2G/3G/4G wifi routers, reported models include the "Onda PN51T",
3210          * "Vodafone PocketWiFi 2", "ZTE MF60" and a similar T-Mobile device.
3211          */
3212         if ((assoc_data->wmm && !elems.wmm_param) ||
3213             (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
3214              (!elems.ht_cap_elem || !elems.ht_operation)) ||
3215             (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
3216              (!elems.vht_cap_elem || !elems.vht_operation))) {
3217                 const struct cfg80211_bss_ies *ies;
3218                 struct ieee802_11_elems bss_elems;
3219
3220                 rcu_read_lock();
3221                 ies = rcu_dereference(cbss->ies);
3222                 if (ies)
3223                         bss_ies = kmemdup(ies, sizeof(*ies) + ies->len,
3224                                           GFP_ATOMIC);
3225                 rcu_read_unlock();
3226                 if (!bss_ies)
3227                         return false;
3228
3229                 ieee802_11_parse_elems(bss_ies->data, bss_ies->len,
3230                                        false, &bss_elems,
3231                                        mgmt->bssid,
3232                                        assoc_data->bss->bssid);
3233                 if (assoc_data->wmm &&
3234                     !elems.wmm_param && bss_elems.wmm_param) {
3235                         elems.wmm_param = bss_elems.wmm_param;
3236                         sdata_info(sdata,
3237                                    "AP bug: WMM param missing from AssocResp\n");
3238                 }
3239
3240                 /*
3241                  * Also check if we requested HT/VHT, otherwise the AP doesn't
3242                  * have to include the IEs in the (re)association response.
3243                  */
3244                 if (!elems.ht_cap_elem && bss_elems.ht_cap_elem &&
3245                     !(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
3246                         elems.ht_cap_elem = bss_elems.ht_cap_elem;
3247                         sdata_info(sdata,
3248                                    "AP bug: HT capability missing from AssocResp\n");
3249                 }
3250                 if (!elems.ht_operation && bss_elems.ht_operation &&
3251                     !(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
3252                         elems.ht_operation = bss_elems.ht_operation;
3253                         sdata_info(sdata,
3254                                    "AP bug: HT operation missing from AssocResp\n");
3255                 }
3256                 if (!elems.vht_cap_elem && bss_elems.vht_cap_elem &&
3257                     !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)) {
3258                         elems.vht_cap_elem = bss_elems.vht_cap_elem;
3259                         sdata_info(sdata,
3260                                    "AP bug: VHT capa missing from AssocResp\n");
3261                 }
3262                 if (!elems.vht_operation && bss_elems.vht_operation &&
3263                     !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)) {
3264                         elems.vht_operation = bss_elems.vht_operation;
3265                         sdata_info(sdata,
3266                                    "AP bug: VHT operation missing from AssocResp\n");
3267                 }
3268         }
3269
3270         /*
3271          * We previously checked these in the beacon/probe response, so
3272          * they should be present here. This is just a safety net.
3273          */
3274         if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
3275             (!elems.wmm_param || !elems.ht_cap_elem || !elems.ht_operation)) {
3276                 sdata_info(sdata,
3277                            "HT AP is missing WMM params or HT capability/operation\n");
3278                 ret = false;
3279                 goto out;
3280         }
3281
3282         if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
3283             (!elems.vht_cap_elem || !elems.vht_operation)) {
3284                 sdata_info(sdata,
3285                            "VHT AP is missing VHT capability/operation\n");
3286                 ret = false;
3287                 goto out;
3288         }
3289
3290         mutex_lock(&sdata->local->sta_mtx);
3291         /*
3292          * station info was already allocated and inserted before
3293          * the association and should be available to us
3294          */
3295         sta = sta_info_get(sdata, cbss->bssid);
3296         if (WARN_ON(!sta)) {
3297                 mutex_unlock(&sdata->local->sta_mtx);
3298                 ret = false;
3299                 goto out;
3300         }
3301
3302         sband = ieee80211_get_sband(sdata);
3303         if (!sband) {
3304                 mutex_unlock(&sdata->local->sta_mtx);
3305                 ret = false;
3306                 goto out;
3307         }
3308
3309         if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE) &&
3310             (!elems.he_cap || !elems.he_operation)) {
3311                 mutex_unlock(&sdata->local->sta_mtx);
3312                 sdata_info(sdata,
3313                            "HE AP is missing HE capability/operation\n");
3314                 ret = false;
3315                 goto out;
3316         }
3317
3318         /* Set up internal HT/VHT capabilities */
3319         if (elems.ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
3320                 ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
3321                                                   elems.ht_cap_elem, sta);
3322
3323         if (elems.vht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
3324                 ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband,
3325                                                     elems.vht_cap_elem, sta);
3326
3327         if (elems.he_operation && !(ifmgd->flags & IEEE80211_STA_DISABLE_HE) &&
3328             elems.he_cap) {
3329                 ieee80211_he_cap_ie_to_sta_he_cap(sdata, sband,
3330                                                   elems.he_cap,
3331                                                   elems.he_cap_len,
3332                                                   sta);
3333
3334                 bss_conf->he_support = sta->sta.he_cap.has_he;
3335                 bss_conf->twt_requester =
3336                         ieee80211_twt_req_supported(sta, &elems);
3337         } else {
3338                 bss_conf->he_support = false;
3339                 bss_conf->twt_requester = false;
3340         }
3341
3342         if (bss_conf->he_support) {
3343                 bss_conf->bss_color =
3344                         le32_get_bits(elems.he_operation->he_oper_params,
3345                                       IEEE80211_HE_OPERATION_BSS_COLOR_MASK);
3346
3347                 bss_conf->htc_trig_based_pkt_ext =
3348                         le32_get_bits(elems.he_operation->he_oper_params,
3349                               IEEE80211_HE_OPERATION_DFLT_PE_DURATION_MASK);
3350                 bss_conf->frame_time_rts_th =
3351                         le32_get_bits(elems.he_operation->he_oper_params,
3352                               IEEE80211_HE_OPERATION_RTS_THRESHOLD_MASK);
3353
3354                 bss_conf->multi_sta_back_32bit =
3355                         sta->sta.he_cap.he_cap_elem.mac_cap_info[2] &
3356                         IEEE80211_HE_MAC_CAP2_32BIT_BA_BITMAP;
3357
3358                 bss_conf->ack_enabled =
3359                         sta->sta.he_cap.he_cap_elem.mac_cap_info[2] &
3360                         IEEE80211_HE_MAC_CAP2_ACK_EN;
3361
3362                 bss_conf->uora_exists = !!elems.uora_element;
3363                 if (elems.uora_element)
3364                         bss_conf->uora_ocw_range = elems.uora_element[0];
3365
3366                 /* TODO: OPEN: what happens if BSS color disable is set? */
3367         }
3368
3369         if (cbss->transmitted_bss) {
3370                 bss_conf->nontransmitted = true;
3371                 ether_addr_copy(bss_conf->transmitter_bssid,
3372                                 cbss->transmitted_bss->bssid);
3373                 bss_conf->bssid_indicator = cbss->max_bssid_indicator;
3374                 bss_conf->bssid_index = cbss->bssid_index;
3375         }
3376
3377         /*
3378          * Some APs, e.g. Netgear WNDR3700, report invalid HT operation data
3379          * in their association response, so ignore that data for our own
3380          * configuration. If it changed since the last beacon, we'll get the
3381          * next beacon and update then.
3382          */
3383
3384         /*
3385          * If an operating mode notification IE is present, override the
3386          * NSS calculation (that would be done in rate_control_rate_init())
3387          * and use the # of streams from that element.
3388          */
3389         if (elems.opmode_notif &&
3390             !(*elems.opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_TYPE_BF)) {
3391                 u8 nss;
3392
3393                 nss = *elems.opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_MASK;
3394                 nss >>= IEEE80211_OPMODE_NOTIF_RX_NSS_SHIFT;
3395                 nss += 1;
3396                 sta->sta.rx_nss = nss;
3397         }
3398
3399         rate_control_rate_init(sta);
3400
3401         if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED) {
3402                 set_sta_flag(sta, WLAN_STA_MFP);
3403                 sta->sta.mfp = true;
3404         } else {
3405                 sta->sta.mfp = false;
3406         }
3407
3408         sta->sta.wme = elems.wmm_param && local->hw.queues >= IEEE80211_NUM_ACS;
3409
3410         err = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
3411         if (!err && !(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
3412                 err = sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
3413         if (err) {
3414                 sdata_info(sdata,
3415                            "failed to move station %pM to desired state\n",
3416                            sta->sta.addr);
3417                 WARN_ON(__sta_info_destroy(sta));
3418                 mutex_unlock(&sdata->local->sta_mtx);
3419                 ret = false;
3420                 goto out;
3421         }
3422
3423         mutex_unlock(&sdata->local->sta_mtx);
3424
3425         /*
3426          * Always handle WMM once after association regardless
3427          * of the first value the AP uses. Setting -1 here has
3428          * that effect because the AP values is an unsigned
3429          * 4-bit value.
3430          */
3431         ifmgd->wmm_last_param_set = -1;
3432         ifmgd->mu_edca_last_param_set = -1;
3433
3434         if (ifmgd->flags & IEEE80211_STA_DISABLE_WMM) {
3435                 ieee80211_set_wmm_default(sdata, false, false);
3436         } else if (!ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
3437                                              elems.wmm_param_len,
3438                                              elems.mu_edca_param_set)) {
3439                 /* still enable QoS since we might have HT/VHT */
3440                 ieee80211_set_wmm_default(sdata, false, true);
3441                 /* set the disable-WMM flag in this case to disable
3442                  * tracking WMM parameter changes in the beacon if
3443                  * the parameters weren't actually valid. Doing so
3444                  * avoids changing parameters very strangely when
3445                  * the AP is going back and forth between valid and
3446                  * invalid parameters.
3447                  */
3448                 ifmgd->flags |= IEEE80211_STA_DISABLE_WMM;
3449         }
3450         changed |= BSS_CHANGED_QOS;
3451
3452         if (elems.max_idle_period_ie) {
3453                 bss_conf->max_idle_period =
3454                         le16_to_cpu(elems.max_idle_period_ie->max_idle_period);
3455                 bss_conf->protected_keep_alive =
3456                         !!(elems.max_idle_period_ie->idle_options &
3457                            WLAN_IDLE_OPTIONS_PROTECTED_KEEP_ALIVE);
3458                 changed |= BSS_CHANGED_KEEP_ALIVE;
3459         } else {
3460                 bss_conf->max_idle_period = 0;
3461                 bss_conf->protected_keep_alive = false;
3462         }
3463
3464         /* set AID and assoc capability,
3465          * ieee80211_set_associated() will tell the driver */
3466         bss_conf->aid = aid;
3467         bss_conf->assoc_capability = capab_info;
3468         ieee80211_set_associated(sdata, cbss, changed);
3469
3470         /*
3471          * If we're using 4-addr mode, let the AP know that we're
3472          * doing so, so that it can create the STA VLAN on its side
3473          */
3474         if (ifmgd->use_4addr)
3475                 ieee80211_send_4addr_nullfunc(local, sdata);
3476
3477         /*
3478          * Start timer to probe the connection to the AP now.
3479          * Also start the timer that will detect beacon loss.
3480          */
3481         ieee80211_sta_rx_notify(sdata, (struct ieee80211_hdr *)mgmt);
3482         ieee80211_sta_reset_beacon_monitor(sdata);
3483
3484         ret = true;
3485  out:
3486         kfree(bss_ies);
3487         return ret;
3488 }
3489
3490 static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
3491                                          struct ieee80211_mgmt *mgmt,
3492                                          size_t len)
3493 {
3494         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3495         struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
3496         u16 capab_info, status_code, aid;
3497         struct ieee802_11_elems elems;
3498         int ac, uapsd_queues = -1;
3499         u8 *pos;
3500         bool reassoc;
3501         struct cfg80211_bss *bss;
3502         struct ieee80211_event event = {
3503                 .type = MLME_EVENT,
3504                 .u.mlme.data = ASSOC_EVENT,
3505         };
3506
3507         sdata_assert_lock(sdata);
3508
3509         if (!assoc_data)
3510                 return;
3511         if (!ether_addr_equal(assoc_data->bss->bssid, mgmt->bssid))
3512                 return;
3513
3514         /*
3515          * AssocResp and ReassocResp have identical structure, so process both
3516          * of them in this function.
3517          */
3518
3519         if (len < 24 + 6)
3520                 return;
3521
3522         reassoc = ieee80211_is_reassoc_resp(mgmt->frame_control);
3523         capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
3524         status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
3525         aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
3526
3527         sdata_info(sdata,
3528                    "RX %sssocResp from %pM (capab=0x%x status=%d aid=%d)\n",
3529                    reassoc ? "Rea" : "A", mgmt->sa,
3530                    capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
3531
3532         if (assoc_data->fils_kek_len &&
3533             fils_decrypt_assoc_resp(sdata, (u8 *)mgmt, &len, assoc_data) < 0)
3534                 return;
3535
3536         pos = mgmt->u.assoc_resp.variable;
3537         ieee802_11_parse_elems(pos, len - (pos - (u8 *)mgmt), false, &elems,
3538                                mgmt->bssid, assoc_data->bss->bssid);
3539
3540         if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
3541             elems.timeout_int &&
3542             elems.timeout_int->type == WLAN_TIMEOUT_ASSOC_COMEBACK) {
3543                 u32 tu, ms;
3544                 tu = le32_to_cpu(elems.timeout_int->value);
3545                 ms = tu * 1024 / 1000;
3546                 sdata_info(sdata,
3547                            "%pM rejected association temporarily; comeback duration %u TU (%u ms)\n",
3548                            mgmt->sa, tu, ms);
3549                 assoc_data->timeout = jiffies + msecs_to_jiffies(ms);
3550                 assoc_data->timeout_started = true;
3551                 if (ms > IEEE80211_ASSOC_TIMEOUT)
3552                         run_again(sdata, assoc_data->timeout);
3553                 return;
3554         }
3555
3556         bss = assoc_data->bss;
3557
3558         if (status_code != WLAN_STATUS_SUCCESS) {
3559                 sdata_info(sdata, "%pM denied association (code=%d)\n",
3560                            mgmt->sa, status_code);
3561                 ieee80211_destroy_assoc_data(sdata, false, false);
3562                 event.u.mlme.status = MLME_DENIED;
3563                 event.u.mlme.reason = status_code;
3564                 drv_event_callback(sdata->local, sdata, &event);
3565         } else {
3566                 if (!ieee80211_assoc_success(sdata, bss, mgmt, len)) {
3567                         /* oops -- internal error -- send timeout for now */
3568                         ieee80211_destroy_assoc_data(sdata, false, false);
3569                         cfg80211_assoc_timeout(sdata->dev, bss);
3570                         return;
3571                 }
3572                 event.u.mlme.status = MLME_SUCCESS;
3573                 drv_event_callback(sdata->local, sdata, &event);
3574                 sdata_info(sdata, "associated\n");
3575
3576                 /*
3577                  * destroy assoc_data afterwards, as otherwise an idle
3578                  * recalc after assoc_data is NULL but before associated
3579                  * is set can cause the interface to go idle
3580                  */
3581                 ieee80211_destroy_assoc_data(sdata, true, false);
3582
3583                 /* get uapsd queues configuration */
3584                 uapsd_queues = 0;
3585                 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
3586                         if (sdata->tx_conf[ac].uapsd)
3587                                 uapsd_queues |= ieee80211_ac_to_qos_mask[ac];
3588         }
3589
3590         cfg80211_rx_assoc_resp(sdata->dev, bss, (u8 *)mgmt, len, uapsd_queues,
3591                                ifmgd->assoc_req_ies, ifmgd->assoc_req_ies_len);
3592 }
3593
3594 static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
3595                                   struct ieee80211_mgmt *mgmt, size_t len,
3596                                   struct ieee80211_rx_status *rx_status)
3597 {
3598         struct ieee80211_local *local = sdata->local;
3599         struct ieee80211_bss *bss;
3600         struct ieee80211_channel *channel;
3601
3602         sdata_assert_lock(sdata);
3603
3604         channel = ieee80211_get_channel(local->hw.wiphy, rx_status->freq);
3605         if (!channel)
3606                 return;
3607
3608         bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, channel);
3609         if (bss) {
3610                 sdata->vif.bss_conf.beacon_rate = bss->beacon_rate;
3611                 ieee80211_rx_bss_put(local, bss);
3612         }
3613 }
3614
3615
3616 static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
3617                                          struct sk_buff *skb)
3618 {
3619         struct ieee80211_mgmt *mgmt = (void *)skb->data;
3620         struct ieee80211_if_managed *ifmgd;
3621         struct ieee80211_rx_status *rx_status = (void *) skb->cb;
3622         size_t baselen, len = skb->len;
3623
3624         ifmgd = &sdata->u.mgd;
3625
3626         sdata_assert_lock(sdata);
3627
3628         if (!ether_addr_equal(mgmt->da, sdata->vif.addr))
3629                 return; /* ignore ProbeResp to foreign address */
3630
3631         baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
3632         if (baselen > len)
3633                 return;
3634
3635         ieee80211_rx_bss_info(sdata, mgmt, len, rx_status);
3636
3637         if (ifmgd->associated &&
3638             ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
3639                 ieee80211_reset_ap_probe(sdata);
3640 }
3641
3642 /*
3643  * This is the canonical list of information elements we care about,
3644  * the filter code also gives us all changes to the Microsoft OUI
3645  * (00:50:F2) vendor IE which is used for WMM which we need to track,
3646  * as well as the DTPC IE (part of the Cisco OUI) used for signaling
3647  * changes to requested client power.
3648  *
3649  * We implement beacon filtering in software since that means we can
3650  * avoid processing the frame here and in cfg80211, and userspace
3651  * will not be able to tell whether the hardware supports it or not.
3652  *
3653  * XXX: This list needs to be dynamic -- userspace needs to be able to
3654  *      add items it requires. It also needs to be able to tell us to
3655  *      look out for other vendor IEs.
3656  */
3657 static const u64 care_about_ies =
3658         (1ULL << WLAN_EID_COUNTRY) |
3659         (1ULL << WLAN_EID_ERP_INFO) |
3660         (1ULL << WLAN_EID_CHANNEL_SWITCH) |
3661         (1ULL << WLAN_EID_PWR_CONSTRAINT) |
3662         (1ULL << WLAN_EID_HT_CAPABILITY) |
3663         (1ULL << WLAN_EID_HT_OPERATION) |
3664         (1ULL << WLAN_EID_EXT_CHANSWITCH_ANN);
3665
3666 static void ieee80211_handle_beacon_sig(struct ieee80211_sub_if_data *sdata,
3667                                         struct ieee80211_if_managed *ifmgd,
3668                                         struct ieee80211_bss_conf *bss_conf,
3669                                         struct ieee80211_local *local,
3670                                         struct ieee80211_rx_status *rx_status)
3671 {
3672         /* Track average RSSI from the Beacon frames of the current AP */
3673
3674         if (ifmgd->flags & IEEE80211_STA_RESET_SIGNAL_AVE) {
3675                 ifmgd->flags &= ~IEEE80211_STA_RESET_SIGNAL_AVE;
3676                 ewma_beacon_signal_init(&ifmgd->ave_beacon_signal);
3677                 ifmgd->last_cqm_event_signal = 0;
3678                 ifmgd->count_beacon_signal = 1;
3679                 ifmgd->last_ave_beacon_signal = 0;
3680         } else {
3681                 ifmgd->count_beacon_signal++;
3682         }
3683
3684         ewma_beacon_signal_add(&ifmgd->ave_beacon_signal, -rx_status->signal);
3685
3686         if (ifmgd->rssi_min_thold != ifmgd->rssi_max_thold &&
3687             ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
3688                 int sig = -ewma_beacon_signal_read(&ifmgd->ave_beacon_signal);
3689                 int last_sig = ifmgd->last_ave_beacon_signal;
3690                 struct ieee80211_event event = {
3691                         .type = RSSI_EVENT,
3692                 };
3693
3694                 /*
3695                  * if signal crosses either of the boundaries, invoke callback
3696                  * with appropriate parameters
3697                  */
3698                 if (sig > ifmgd->rssi_max_thold &&
3699                     (last_sig <= ifmgd->rssi_min_thold || last_sig == 0)) {
3700                         ifmgd->last_ave_beacon_signal = sig;
3701                         event.u.rssi.data = RSSI_EVENT_HIGH;
3702                         drv_event_callback(local, sdata, &event);
3703                 } else if (sig < ifmgd->rssi_min_thold &&
3704                            (last_sig >= ifmgd->rssi_max_thold ||
3705                            last_sig == 0)) {
3706                         ifmgd->last_ave_beacon_signal = sig;
3707                         event.u.rssi.data = RSSI_EVENT_LOW;
3708                         drv_event_callback(local, sdata, &event);
3709                 }
3710         }
3711
3712         if (bss_conf->cqm_rssi_thold &&
3713             ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT &&
3714             !(sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI)) {
3715                 int sig = -ewma_beacon_signal_read(&ifmgd->ave_beacon_signal);
3716                 int last_event = ifmgd->last_cqm_event_signal;
3717                 int thold = bss_conf->cqm_rssi_thold;
3718                 int hyst = bss_conf->cqm_rssi_hyst;
3719
3720                 if (sig < thold &&
3721                     (last_event == 0 || sig < last_event - hyst)) {
3722                         ifmgd->last_cqm_event_signal = sig;
3723                         ieee80211_cqm_rssi_notify(
3724                                 &sdata->vif,
3725                                 NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
3726                                 sig, GFP_KERNEL);
3727                 } else if (sig > thold &&
3728                            (last_event == 0 || sig > last_event + hyst)) {
3729                         ifmgd->last_cqm_event_signal = sig;
3730                         ieee80211_cqm_rssi_notify(
3731                                 &sdata->vif,
3732                                 NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
3733                                 sig, GFP_KERNEL);
3734                 }
3735         }
3736
3737         if (bss_conf->cqm_rssi_low &&
3738             ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
3739                 int sig = -ewma_beacon_signal_read(&ifmgd->ave_beacon_signal);
3740                 int last_event = ifmgd->last_cqm_event_signal;
3741                 int low = bss_conf->cqm_rssi_low;
3742                 int high = bss_conf->cqm_rssi_high;
3743
3744                 if (sig < low &&
3745                     (last_event == 0 || last_event >= low)) {
3746                         ifmgd->last_cqm_event_signal = sig;
3747                         ieee80211_cqm_rssi_notify(
3748                                 &sdata->vif,
3749                                 NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
3750                                 sig, GFP_KERNEL);
3751                 } else if (sig > high &&
3752                            (last_event == 0 || last_event <= high)) {
3753                         ifmgd->last_cqm_event_signal = sig;
3754                         ieee80211_cqm_rssi_notify(
3755                                 &sdata->vif,
3756                                 NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
3757                                 sig, GFP_KERNEL);
3758                 }
3759         }
3760 }
3761
3762 static bool ieee80211_rx_our_beacon(const u8 *tx_bssid,
3763                                     struct cfg80211_bss *bss)
3764 {
3765         if (ether_addr_equal(tx_bssid, bss->bssid))
3766                 return true;
3767         if (!bss->transmitted_bss)
3768                 return false;
3769         return ether_addr_equal(tx_bssid, bss->transmitted_bss->bssid);
3770 }
3771
3772 static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
3773                                      struct ieee80211_mgmt *mgmt, size_t len,
3774                                      struct ieee80211_rx_status *rx_status)
3775 {
3776         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3777         struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
3778         size_t baselen;
3779         struct ieee802_11_elems elems;
3780         struct ieee80211_local *local = sdata->local;
3781         struct ieee80211_chanctx_conf *chanctx_conf;
3782         struct ieee80211_channel *chan;
3783         struct sta_info *sta;
3784         u32 changed = 0;
3785         bool erp_valid;
3786         u8 erp_value = 0;
3787         u32 ncrc;
3788         u8 *bssid;
3789         u8 deauth_buf[IEEE80211_DEAUTH_FRAME_LEN];
3790
3791         sdata_assert_lock(sdata);
3792
3793         /* Process beacon from the current BSS */
3794         baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
3795         if (baselen > len)
3796                 return;
3797
3798         rcu_read_lock();
3799         chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
3800         if (!chanctx_conf) {
3801                 rcu_read_unlock();
3802                 return;
3803         }
3804
3805         if (rx_status->freq != chanctx_conf->def.chan->center_freq) {
3806                 rcu_read_unlock();
3807                 return;
3808         }
3809         chan = chanctx_conf->def.chan;
3810         rcu_read_unlock();
3811
3812         if (ifmgd->assoc_data && ifmgd->assoc_data->need_beacon &&
3813             ieee80211_rx_our_beacon(mgmt->bssid, ifmgd->assoc_data->bss)) {
3814                 ieee802_11_parse_elems(mgmt->u.beacon.variable,
3815                                        len - baselen, false, &elems,
3816                                        mgmt->bssid,
3817                                        ifmgd->assoc_data->bss->bssid);
3818
3819                 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status);
3820
3821                 if (elems.dtim_period)
3822                         ifmgd->dtim_period = elems.dtim_period;
3823                 ifmgd->have_beacon = true;
3824                 ifmgd->assoc_data->need_beacon = false;
3825                 if (ieee80211_hw_check(&local->hw, TIMING_BEACON_ONLY)) {
3826                         sdata->vif.bss_conf.sync_tsf =
3827                                 le64_to_cpu(mgmt->u.beacon.timestamp);
3828                         sdata->vif.bss_conf.sync_device_ts =
3829                                 rx_status->device_timestamp;
3830                         sdata->vif.bss_conf.sync_dtim_count = elems.dtim_count;
3831                 }
3832
3833                 if (elems.mbssid_config_ie)
3834                         bss_conf->profile_periodicity =
3835                                 elems.mbssid_config_ie->profile_periodicity;
3836
3837                 if (elems.ext_capab_len >= 11 &&
3838                     (elems.ext_capab[10] & WLAN_EXT_CAPA11_EMA_SUPPORT))
3839                         bss_conf->ema_ap = true;
3840
3841                 /* continue assoc process */
3842                 ifmgd->assoc_data->timeout = jiffies;
3843                 ifmgd->assoc_data->timeout_started = true;
3844                 run_again(sdata, ifmgd->assoc_data->timeout);
3845                 return;
3846         }
3847
3848         if (!ifmgd->associated ||
3849             !ieee80211_rx_our_beacon(mgmt->bssid,  ifmgd->associated))
3850                 return;
3851         bssid = ifmgd->associated->bssid;
3852
3853         if (!(rx_status->flag & RX_FLAG_NO_SIGNAL_VAL))
3854                 ieee80211_handle_beacon_sig(sdata, ifmgd, bss_conf,
3855                                             local, rx_status);
3856
3857         if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL) {
3858                 mlme_dbg_ratelimited(sdata,
3859                                      "cancelling AP probe due to a received beacon\n");
3860                 ieee80211_reset_ap_probe(sdata);
3861         }
3862
3863         /*
3864          * Push the beacon loss detection into the future since
3865          * we are processing a beacon from the AP just now.
3866          */
3867         ieee80211_sta_reset_beacon_monitor(sdata);
3868
3869         ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
3870         ncrc = ieee802_11_parse_elems_crc(mgmt->u.beacon.variable,
3871                                           len - baselen, false, &elems,
3872                                           care_about_ies, ncrc,
3873                                           mgmt->bssid, bssid);
3874
3875         if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK) &&
3876             ieee80211_check_tim(elems.tim, elems.tim_len, ifmgd->aid)) {
3877                 if (local->hw.conf.dynamic_ps_timeout > 0) {
3878                         if (local->hw.conf.flags & IEEE80211_CONF_PS) {
3879                                 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
3880                                 ieee80211_hw_config(local,
3881                                                     IEEE80211_CONF_CHANGE_PS);
3882                         }
3883                         ieee80211_send_nullfunc(local, sdata, false);
3884                 } else if (!local->pspolling && sdata->u.mgd.powersave) {
3885                         local->pspolling = true;
3886
3887                         /*
3888                          * Here is assumed that the driver will be
3889                          * able to send ps-poll frame and receive a
3890                          * response even though power save mode is
3891                          * enabled, but some drivers might require
3892                          * to disable power save here. This needs
3893                          * to be investigated.
3894                          */
3895                         ieee80211_send_pspoll(local, sdata);
3896                 }
3897         }
3898
3899         if (sdata->vif.p2p ||
3900             sdata->vif.driver_flags & IEEE80211_VIF_GET_NOA_UPDATE) {
3901                 struct ieee80211_p2p_noa_attr noa = {};
3902                 int ret;
3903
3904                 ret = cfg80211_get_p2p_attr(mgmt->u.beacon.variable,
3905                                             len - baselen,
3906                                             IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
3907                                             (u8 *) &noa, sizeof(noa));
3908                 if (ret >= 2) {
3909                         if (sdata->u.mgd.p2p_noa_index != noa.index) {
3910                                 /* valid noa_attr and index changed */
3911                                 sdata->u.mgd.p2p_noa_index = noa.index;
3912                                 memcpy(&bss_conf->p2p_noa_attr, &noa, sizeof(noa));
3913                                 changed |= BSS_CHANGED_P2P_PS;
3914                                 /*
3915                                  * make sure we update all information, the CRC
3916                                  * mechanism doesn't look at P2P attributes.
3917                                  */
3918                                 ifmgd->beacon_crc_valid = false;
3919                         }
3920                 } else if (sdata->u.mgd.p2p_noa_index != -1) {
3921                         /* noa_attr not found and we had valid noa_attr before */
3922                         sdata->u.mgd.p2p_noa_index = -1;
3923                         memset(&bss_conf->p2p_noa_attr, 0, sizeof(bss_conf->p2p_noa_attr));
3924                         changed |= BSS_CHANGED_P2P_PS;
3925                         ifmgd->beacon_crc_valid = false;
3926                 }
3927         }
3928
3929         if (ifmgd->csa_waiting_bcn)
3930                 ieee80211_chswitch_post_beacon(sdata);
3931
3932         /*
3933          * Update beacon timing and dtim count on every beacon appearance. This
3934          * will allow the driver to use the most updated values. Do it before
3935          * comparing this one with last received beacon.
3936          * IMPORTANT: These parameters would possibly be out of sync by the time
3937          * the driver will use them. The synchronized view is currently
3938          * guaranteed only in certain callbacks.
3939          */
3940         if (ieee80211_hw_check(&local->hw, TIMING_BEACON_ONLY)) {
3941                 sdata->vif.bss_conf.sync_tsf =
3942                         le64_to_cpu(mgmt->u.beacon.timestamp);
3943                 sdata->vif.bss_conf.sync_device_ts =
3944                         rx_status->device_timestamp;
3945                 sdata->vif.bss_conf.sync_dtim_count = elems.dtim_count;
3946         }
3947
3948         if (ncrc == ifmgd->beacon_crc && ifmgd->beacon_crc_valid)
3949                 return;
3950         ifmgd->beacon_crc = ncrc;
3951         ifmgd->beacon_crc_valid = true;
3952
3953         ieee80211_rx_bss_info(sdata, mgmt, len, rx_status);
3954
3955         ieee80211_sta_process_chanswitch(sdata, rx_status->mactime,
3956                                          rx_status->device_timestamp,
3957                                          &elems, true);
3958
3959         if (!(ifmgd->flags & IEEE80211_STA_DISABLE_WMM) &&
3960             ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
3961                                      elems.wmm_param_len,
3962                                      elems.mu_edca_param_set))
3963                 changed |= BSS_CHANGED_QOS;
3964
3965         /*
3966          * If we haven't had a beacon before, tell the driver about the
3967          * DTIM period (and beacon timing if desired) now.
3968          */
3969         if (!ifmgd->have_beacon) {
3970                 /* a few bogus AP send dtim_period = 0 or no TIM IE */
3971                 bss_conf->dtim_period = elems.dtim_period ?: 1;
3972
3973                 changed |= BSS_CHANGED_BEACON_INFO;
3974                 ifmgd->have_beacon = true;
3975
3976                 mutex_lock(&local->iflist_mtx);
3977                 ieee80211_recalc_ps(local);
3978                 mutex_unlock(&local->iflist_mtx);
3979
3980                 ieee80211_recalc_ps_vif(sdata);
3981         }
3982
3983         if (elems.erp_info) {
3984                 erp_valid = true;
3985                 erp_value = elems.erp_info[0];
3986         } else {
3987                 erp_valid = false;
3988         }
3989         changed |= ieee80211_handle_bss_capability(sdata,
3990                         le16_to_cpu(mgmt->u.beacon.capab_info),
3991                         erp_valid, erp_value);
3992
3993         mutex_lock(&local->sta_mtx);
3994         sta = sta_info_get(sdata, bssid);
3995
3996         if (ieee80211_config_bw(sdata, sta,
3997                                 elems.ht_cap_elem, elems.ht_operation,
3998                                 elems.vht_operation, elems.he_operation,
3999                                 bssid, &changed)) {
4000                 mutex_unlock(&local->sta_mtx);
4001                 sdata_info(sdata,
4002                            "failed to follow AP %pM bandwidth change, disconnect\n",
4003                            bssid);
4004                 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
4005                                        WLAN_REASON_DEAUTH_LEAVING,
4006                                        true, deauth_buf);
4007                 ieee80211_report_disconnect(sdata, deauth_buf,
4008                                             sizeof(deauth_buf), true,
4009                                             WLAN_REASON_DEAUTH_LEAVING);
4010                 return;
4011         }
4012
4013         if (sta && elems.opmode_notif)
4014                 ieee80211_vht_handle_opmode(sdata, sta, *elems.opmode_notif,
4015                                             rx_status->band);
4016         mutex_unlock(&local->sta_mtx);
4017
4018         changed |= ieee80211_handle_pwr_constr(sdata, chan, mgmt,
4019                                                elems.country_elem,
4020                                                elems.country_elem_len,
4021                                                elems.pwr_constr_elem,
4022                                                elems.cisco_dtpc_elem);
4023
4024         ieee80211_bss_info_change_notify(sdata, changed);
4025 }
4026
4027 void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
4028                                   struct sk_buff *skb)
4029 {
4030         struct ieee80211_rx_status *rx_status;
4031         struct ieee80211_mgmt *mgmt;
4032         u16 fc;
4033         struct ieee802_11_elems elems;
4034         int ies_len;
4035
4036         rx_status = (struct ieee80211_rx_status *) skb->cb;
4037         mgmt = (struct ieee80211_mgmt *) skb->data;
4038         fc = le16_to_cpu(mgmt->frame_control);
4039
4040         sdata_lock(sdata);
4041
4042         switch (fc & IEEE80211_FCTL_STYPE) {
4043         case IEEE80211_STYPE_BEACON:
4044                 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len, rx_status);
4045                 break;
4046         case IEEE80211_STYPE_PROBE_RESP:
4047                 ieee80211_rx_mgmt_probe_resp(sdata, skb);
4048                 break;
4049         case IEEE80211_STYPE_AUTH:
4050                 ieee80211_rx_mgmt_auth(sdata, mgmt, skb->len);
4051                 break;
4052         case IEEE80211_STYPE_DEAUTH:
4053                 ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
4054                 break;
4055         case IEEE80211_STYPE_DISASSOC:
4056                 ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
4057                 break;
4058         case IEEE80211_STYPE_ASSOC_RESP:
4059         case IEEE80211_STYPE_REASSOC_RESP:
4060                 ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len);
4061                 break;
4062         case IEEE80211_STYPE_ACTION:
4063                 if (mgmt->u.action.category == WLAN_CATEGORY_SPECTRUM_MGMT) {
4064                         ies_len = skb->len -
4065                                   offsetof(struct ieee80211_mgmt,
4066                                            u.action.u.chan_switch.variable);
4067
4068                         if (ies_len < 0)
4069                                 break;
4070
4071                         /* CSA IE cannot be overridden, no need for BSSID */
4072                         ieee802_11_parse_elems(
4073                                 mgmt->u.action.u.chan_switch.variable,
4074                                 ies_len, true, &elems, mgmt->bssid, NULL);
4075
4076                         if (elems.parse_error)
4077                                 break;
4078
4079                         ieee80211_sta_process_chanswitch(sdata,
4080                                                  rx_status->mactime,
4081                                                  rx_status->device_timestamp,
4082                                                  &elems, false);
4083                 } else if (mgmt->u.action.category == WLAN_CATEGORY_PUBLIC) {
4084                         ies_len = skb->len -
4085                                   offsetof(struct ieee80211_mgmt,
4086                                            u.action.u.ext_chan_switch.variable);
4087
4088                         if (ies_len < 0)
4089                                 break;
4090
4091                         /*
4092                          * extended CSA IE can't be overridden, no need for
4093                          * BSSID
4094                          */
4095                         ieee802_11_parse_elems(
4096                                 mgmt->u.action.u.ext_chan_switch.variable,
4097                                 ies_len, true, &elems, mgmt->bssid, NULL);
4098
4099                         if (elems.parse_error)
4100                                 break;
4101
4102                         /* for the handling code pretend this was also an IE */
4103                         elems.ext_chansw_ie =
4104                                 &mgmt->u.action.u.ext_chan_switch.data;
4105
4106                         ieee80211_sta_process_chanswitch(sdata,
4107                                                  rx_status->mactime,
4108                                                  rx_status->device_timestamp,
4109                                                  &elems, false);
4110                 }
4111                 break;
4112         }
4113         sdata_unlock(sdata);
4114 }
4115
4116 static void ieee80211_sta_timer(struct timer_list *t)
4117 {
4118         struct ieee80211_sub_if_data *sdata =
4119                 from_timer(sdata, t, u.mgd.timer);
4120
4121         ieee80211_queue_work(&sdata->local->hw, &sdata->work);
4122 }
4123
4124 static void ieee80211_sta_connection_lost(struct ieee80211_sub_if_data *sdata,
4125                                           u8 *bssid, u8 reason, bool tx)
4126 {
4127         u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
4128
4129         ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH, reason,
4130                                tx, frame_buf);
4131
4132         ieee80211_report_disconnect(sdata, frame_buf, sizeof(frame_buf), true,
4133                                     reason);
4134 }
4135
4136 static int ieee80211_auth(struct ieee80211_sub_if_data *sdata)
4137 {
4138         struct ieee80211_local *local = sdata->local;
4139         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4140         struct ieee80211_mgd_auth_data *auth_data = ifmgd->auth_data;
4141         u32 tx_flags = 0;
4142         u16 trans = 1;
4143         u16 status = 0;
4144         u16 prepare_tx_duration = 0;
4145
4146         sdata_assert_lock(sdata);
4147
4148         if (WARN_ON_ONCE(!auth_data))
4149                 return -EINVAL;
4150
4151         auth_data->tries++;
4152
4153         if (auth_data->tries > IEEE80211_AUTH_MAX_TRIES) {
4154                 sdata_info(sdata, "authentication with %pM timed out\n",
4155                            auth_data->bss->bssid);
4156
4157                 /*
4158                  * Most likely AP is not in the range so remove the
4159                  * bss struct for that AP.
4160                  */
4161                 cfg80211_unlink_bss(local->hw.wiphy, auth_data->bss);
4162
4163                 return -ETIMEDOUT;
4164         }
4165
4166         if (auth_data->algorithm == WLAN_AUTH_SAE)
4167                 prepare_tx_duration =
4168                         jiffies_to_msecs(IEEE80211_AUTH_TIMEOUT_SAE);
4169
4170         drv_mgd_prepare_tx(local, sdata, prepare_tx_duration);
4171
4172         sdata_info(sdata, "send auth to %pM (try %d/%d)\n",
4173                    auth_data->bss->bssid, auth_data->tries,
4174                    IEEE80211_AUTH_MAX_TRIES);
4175
4176         auth_data->expected_transaction = 2;
4177
4178         if (auth_data->algorithm == WLAN_AUTH_SAE) {
4179                 trans = auth_data->sae_trans;
4180                 status = auth_data->sae_status;
4181                 auth_data->expected_transaction = trans;
4182         }
4183
4184         if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
4185                 tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
4186                            IEEE80211_TX_INTFL_MLME_CONN_TX;
4187
4188         ieee80211_send_auth(sdata, trans, auth_data->algorithm, status,
4189                             auth_data->data, auth_data->data_len,
4190                             auth_data->bss->bssid,
4191                             auth_data->bss->bssid, NULL, 0, 0,
4192                             tx_flags);
4193
4194         if (tx_flags == 0) {
4195                 if (auth_data->algorithm == WLAN_AUTH_SAE)
4196                         auth_data->timeout = jiffies +
4197                                 IEEE80211_AUTH_TIMEOUT_SAE;
4198                 else
4199                         auth_data->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
4200         } else {
4201                 auth_data->timeout =
4202                         round_jiffies_up(jiffies + IEEE80211_AUTH_TIMEOUT_LONG);
4203         }
4204
4205         auth_data->timeout_started = true;
4206         run_again(sdata, auth_data->timeout);
4207
4208         return 0;
4209 }
4210
4211 static int ieee80211_do_assoc(struct ieee80211_sub_if_data *sdata)
4212 {
4213         struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
4214         struct ieee80211_local *local = sdata->local;
4215
4216         sdata_assert_lock(sdata);
4217
4218         assoc_data->tries++;
4219         if (assoc_data->tries > IEEE80211_ASSOC_MAX_TRIES) {
4220                 sdata_info(sdata, "association with %pM timed out\n",
4221                            assoc_data->bss->bssid);
4222
4223                 /*
4224                  * Most likely AP is not in the range so remove the
4225                  * bss struct for that AP.
4226                  */
4227                 cfg80211_unlink_bss(local->hw.wiphy, assoc_data->bss);
4228
4229                 return -ETIMEDOUT;
4230         }
4231
4232         sdata_info(sdata, "associate with %pM (try %d/%d)\n",
4233                    assoc_data->bss->bssid, assoc_data->tries,
4234                    IEEE80211_ASSOC_MAX_TRIES);
4235         ieee80211_send_assoc(sdata);
4236
4237         if (!ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
4238                 assoc_data->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
4239                 assoc_data->timeout_started = true;
4240                 run_again(sdata, assoc_data->timeout);
4241         } else {
4242                 assoc_data->timeout =
4243                         round_jiffies_up(jiffies +
4244                                          IEEE80211_ASSOC_TIMEOUT_LONG);
4245                 assoc_data->timeout_started = true;
4246                 run_again(sdata, assoc_data->timeout);
4247         }
4248
4249         return 0;
4250 }
4251
4252 void ieee80211_mgd_conn_tx_status(struct ieee80211_sub_if_data *sdata,
4253                                   __le16 fc, bool acked)
4254 {
4255         struct ieee80211_local *local = sdata->local;
4256
4257         sdata->u.mgd.status_fc = fc;
4258         sdata->u.mgd.status_acked = acked;
4259         sdata->u.mgd.status_received = true;
4260
4261         ieee80211_queue_work(&local->hw, &sdata->work);
4262 }
4263
4264 void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
4265 {
4266         struct ieee80211_local *local = sdata->local;
4267         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4268
4269         sdata_lock(sdata);
4270
4271         if (ifmgd->status_received) {
4272                 __le16 fc = ifmgd->status_fc;
4273                 bool status_acked = ifmgd->status_acked;
4274
4275                 ifmgd->status_received = false;
4276                 if (ifmgd->auth_data && ieee80211_is_auth(fc)) {
4277                         if (status_acked) {
4278                                 if (ifmgd->auth_data->algorithm ==
4279                                     WLAN_AUTH_SAE)
4280                                         ifmgd->auth_data->timeout =
4281                                                 jiffies +
4282                                                 IEEE80211_AUTH_TIMEOUT_SAE;
4283                                 else
4284                                         ifmgd->auth_data->timeout =
4285                                                 jiffies +
4286                                                 IEEE80211_AUTH_TIMEOUT_SHORT;
4287                                 run_again(sdata, ifmgd->auth_data->timeout);
4288                         } else {
4289                                 ifmgd->auth_data->timeout = jiffies - 1;
4290                         }
4291                         ifmgd->auth_data->timeout_started = true;
4292                 } else if (ifmgd->assoc_data &&
4293                            (ieee80211_is_assoc_req(fc) ||
4294                             ieee80211_is_reassoc_req(fc))) {
4295                         if (status_acked) {
4296                                 ifmgd->assoc_data->timeout =
4297                                         jiffies + IEEE80211_ASSOC_TIMEOUT_SHORT;
4298                                 run_again(sdata, ifmgd->assoc_data->timeout);
4299                         } else {
4300                                 ifmgd->assoc_data->timeout = jiffies - 1;
4301                         }
4302                         ifmgd->assoc_data->timeout_started = true;
4303                 }
4304         }
4305
4306         if (ifmgd->auth_data && ifmgd->auth_data->timeout_started &&
4307             time_after(jiffies, ifmgd->auth_data->timeout)) {
4308                 if (ifmgd->auth_data->done) {
4309                         /*
4310                          * ok ... we waited for assoc but userspace didn't,
4311                          * so let's just kill the auth data
4312                          */
4313                         ieee80211_destroy_auth_data(sdata, false);
4314                 } else if (ieee80211_auth(sdata)) {
4315                         u8 bssid[ETH_ALEN];
4316                         struct ieee80211_event event = {
4317                                 .type = MLME_EVENT,
4318                                 .u.mlme.data = AUTH_EVENT,
4319                                 .u.mlme.status = MLME_TIMEOUT,
4320                         };
4321
4322                         memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
4323
4324                         ieee80211_destroy_auth_data(sdata, false);
4325
4326                         cfg80211_auth_timeout(sdata->dev, bssid);
4327                         drv_event_callback(sdata->local, sdata, &event);
4328                 }
4329         } else if (ifmgd->auth_data && ifmgd->auth_data->timeout_started)
4330                 run_again(sdata, ifmgd->auth_data->timeout);
4331
4332         if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started &&
4333             time_after(jiffies, ifmgd->assoc_data->timeout)) {
4334                 if ((ifmgd->assoc_data->need_beacon && !ifmgd->have_beacon) ||
4335                     ieee80211_do_assoc(sdata)) {
4336                         struct cfg80211_bss *bss = ifmgd->assoc_data->bss;
4337                         struct ieee80211_event event = {
4338                                 .type = MLME_EVENT,
4339                                 .u.mlme.data = ASSOC_EVENT,
4340                                 .u.mlme.status = MLME_TIMEOUT,
4341                         };
4342
4343                         ieee80211_destroy_assoc_data(sdata, false, false);
4344                         cfg80211_assoc_timeout(sdata->dev, bss);
4345                         drv_event_callback(sdata->local, sdata, &event);
4346                 }
4347         } else if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started)
4348                 run_again(sdata, ifmgd->assoc_data->timeout);
4349
4350         if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL &&
4351             ifmgd->associated) {
4352                 u8 bssid[ETH_ALEN];
4353                 int max_tries;
4354
4355                 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
4356
4357                 if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
4358                         max_tries = max_nullfunc_tries;
4359                 else
4360                         max_tries = max_probe_tries;
4361
4362                 /* ACK received for nullfunc probing frame */
4363                 if (!ifmgd->probe_send_count)
4364                         ieee80211_reset_ap_probe(sdata);
4365                 else if (ifmgd->nullfunc_failed) {
4366                         if (ifmgd->probe_send_count < max_tries) {
4367                                 mlme_dbg(sdata,
4368                                          "No ack for nullfunc frame to AP %pM, try %d/%i\n",
4369                                          bssid, ifmgd->probe_send_count,
4370                                          max_tries);
4371                                 ieee80211_mgd_probe_ap_send(sdata);
4372                         } else {
4373                                 mlme_dbg(sdata,
4374                                          "No ack for nullfunc frame to AP %pM, disconnecting.\n",
4375                                          bssid);
4376                                 ieee80211_sta_connection_lost(sdata, bssid,
4377                                         WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
4378                                         false);
4379                         }
4380                 } else if (time_is_after_jiffies(ifmgd->probe_timeout))
4381                         run_again(sdata, ifmgd->probe_timeout);
4382                 else if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
4383                         mlme_dbg(sdata,
4384                                  "Failed to send nullfunc to AP %pM after %dms, disconnecting\n",
4385                                  bssid, probe_wait_ms);
4386                         ieee80211_sta_connection_lost(sdata, bssid,
4387                                 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
4388                 } else if (ifmgd->probe_send_count < max_tries) {
4389                         mlme_dbg(sdata,
4390                                  "No probe response from AP %pM after %dms, try %d/%i\n",
4391                                  bssid, probe_wait_ms,
4392                                  ifmgd->probe_send_count, max_tries);
4393                         ieee80211_mgd_probe_ap_send(sdata);
4394                 } else {
4395                         /*
4396                          * We actually lost the connection ... or did we?
4397                          * Let's make sure!
4398                          */
4399                         mlme_dbg(sdata,
4400                                  "No probe response from AP %pM after %dms, disconnecting.\n",
4401                                  bssid, probe_wait_ms);
4402
4403                         ieee80211_sta_connection_lost(sdata, bssid,
4404                                 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
4405                 }
4406         }
4407
4408         sdata_unlock(sdata);
4409 }
4410
4411 static void ieee80211_sta_bcn_mon_timer(struct timer_list *t)
4412 {
4413         struct ieee80211_sub_if_data *sdata =
4414                 from_timer(sdata, t, u.mgd.bcn_mon_timer);
4415         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4416
4417         if (sdata->vif.csa_active && !ifmgd->csa_waiting_bcn)
4418                 return;
4419
4420         sdata->u.mgd.connection_loss = false;
4421         ieee80211_queue_work(&sdata->local->hw,
4422                              &sdata->u.mgd.beacon_connection_loss_work);
4423 }
4424
4425 static void ieee80211_sta_conn_mon_timer(struct timer_list *t)
4426 {
4427         struct ieee80211_sub_if_data *sdata =
4428                 from_timer(sdata, t, u.mgd.conn_mon_timer);
4429         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4430         struct ieee80211_local *local = sdata->local;
4431
4432         if (sdata->vif.csa_active && !ifmgd->csa_waiting_bcn)
4433                 return;
4434
4435         ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
4436 }
4437
4438 static void ieee80211_sta_monitor_work(struct work_struct *work)
4439 {
4440         struct ieee80211_sub_if_data *sdata =
4441                 container_of(work, struct ieee80211_sub_if_data,
4442                              u.mgd.monitor_work);
4443
4444         ieee80211_mgd_probe_ap(sdata, false);
4445 }
4446
4447 static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
4448 {
4449         if (sdata->vif.type == NL80211_IFTYPE_STATION) {
4450                 __ieee80211_stop_poll(sdata);
4451
4452                 /* let's probe the connection once */
4453                 if (!ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
4454                         ieee80211_queue_work(&sdata->local->hw,
4455                                              &sdata->u.mgd.monitor_work);
4456         }
4457 }
4458
4459 #ifdef CONFIG_PM
4460 void ieee80211_mgd_quiesce(struct ieee80211_sub_if_data *sdata)
4461 {
4462         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4463         u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
4464
4465         sdata_lock(sdata);
4466
4467         if (ifmgd->auth_data || ifmgd->assoc_data) {
4468                 const u8 *bssid = ifmgd->auth_data ?
4469                                 ifmgd->auth_data->bss->bssid :
4470                                 ifmgd->assoc_data->bss->bssid;
4471
4472                 /*
4473                  * If we are trying to authenticate / associate while suspending,
4474                  * cfg80211 won't know and won't actually abort those attempts,
4475                  * thus we need to do that ourselves.
4476                  */
4477                 ieee80211_send_deauth_disassoc(sdata, bssid,
4478                                                IEEE80211_STYPE_DEAUTH,
4479                                                WLAN_REASON_DEAUTH_LEAVING,
4480                                                false, frame_buf);
4481                 if (ifmgd->assoc_data)
4482                         ieee80211_destroy_assoc_data(sdata, false, true);
4483                 if (ifmgd->auth_data)
4484                         ieee80211_destroy_auth_data(sdata, false);
4485                 cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
4486                                       IEEE80211_DEAUTH_FRAME_LEN);
4487         }
4488
4489         /* This is a bit of a hack - we should find a better and more generic
4490          * solution to this. Normally when suspending, cfg80211 will in fact
4491          * deauthenticate. However, it doesn't (and cannot) stop an ongoing
4492          * auth (not so important) or assoc (this is the problem) process.
4493          *
4494          * As a consequence, it can happen that we are in the process of both
4495          * associating and suspending, and receive an association response
4496          * after cfg80211 has checked if it needs to disconnect, but before
4497          * we actually set the flag to drop incoming frames. This will then
4498          * cause the workqueue flush to process the association response in
4499          * the suspend, resulting in a successful association just before it
4500          * tries to remove the interface from the driver, which now though
4501          * has a channel context assigned ... this results in issues.
4502          *
4503          * To work around this (for now) simply deauth here again if we're
4504          * now connected.
4505          */
4506         if (ifmgd->associated && !sdata->local->wowlan) {
4507                 u8 bssid[ETH_ALEN];
4508                 struct cfg80211_deauth_request req = {
4509                         .reason_code = WLAN_REASON_DEAUTH_LEAVING,
4510                         .bssid = bssid,
4511                 };
4512
4513                 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
4514                 ieee80211_mgd_deauth(sdata, &req);
4515         }
4516
4517         sdata_unlock(sdata);
4518 }
4519
4520 void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
4521 {
4522         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4523
4524         sdata_lock(sdata);
4525         if (!ifmgd->associated) {
4526                 sdata_unlock(sdata);
4527                 return;
4528         }
4529
4530         if (sdata->flags & IEEE80211_SDATA_DISCONNECT_RESUME) {
4531                 sdata->flags &= ~IEEE80211_SDATA_DISCONNECT_RESUME;
4532                 mlme_dbg(sdata, "driver requested disconnect after resume\n");
4533                 ieee80211_sta_connection_lost(sdata,
4534                                               ifmgd->associated->bssid,
4535                                               WLAN_REASON_UNSPECIFIED,
4536                                               true);
4537                 sdata_unlock(sdata);
4538                 return;
4539         }
4540         sdata_unlock(sdata);
4541 }
4542 #endif
4543
4544 /* interface setup */
4545 void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
4546 {
4547         struct ieee80211_if_managed *ifmgd;
4548
4549         ifmgd = &sdata->u.mgd;
4550         INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
4551         INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
4552         INIT_WORK(&ifmgd->beacon_connection_loss_work,
4553                   ieee80211_beacon_connection_loss_work);
4554         INIT_WORK(&ifmgd->csa_connection_drop_work,
4555                   ieee80211_csa_connection_drop_work);
4556         INIT_WORK(&ifmgd->request_smps_work, ieee80211_request_smps_mgd_work);
4557         INIT_DELAYED_WORK(&ifmgd->tdls_peer_del_work,
4558                           ieee80211_tdls_peer_del_work);
4559         timer_setup(&ifmgd->timer, ieee80211_sta_timer, 0);
4560         timer_setup(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer, 0);
4561         timer_setup(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer, 0);
4562         timer_setup(&ifmgd->chswitch_timer, ieee80211_chswitch_timer, 0);
4563         INIT_DELAYED_WORK(&ifmgd->tx_tspec_wk,
4564                           ieee80211_sta_handle_tspec_ac_params_wk);
4565
4566         ifmgd->flags = 0;
4567         ifmgd->powersave = sdata->wdev.ps;
4568         ifmgd->uapsd_queues = sdata->local->hw.uapsd_queues;
4569         ifmgd->uapsd_max_sp_len = sdata->local->hw.uapsd_max_sp_len;
4570         ifmgd->p2p_noa_index = -1;
4571
4572         if (sdata->local->hw.wiphy->features & NL80211_FEATURE_DYNAMIC_SMPS)
4573                 ifmgd->req_smps = IEEE80211_SMPS_AUTOMATIC;
4574         else
4575                 ifmgd->req_smps = IEEE80211_SMPS_OFF;
4576
4577         /* Setup TDLS data */
4578         spin_lock_init(&ifmgd->teardown_lock);
4579         ifmgd->teardown_skb = NULL;
4580         ifmgd->orig_teardown_skb = NULL;
4581 }
4582
4583 /* scan finished notification */
4584 void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
4585 {
4586         struct ieee80211_sub_if_data *sdata;
4587
4588         /* Restart STA timers */
4589         rcu_read_lock();
4590         list_for_each_entry_rcu(sdata, &local->interfaces, list) {
4591                 if (ieee80211_sdata_running(sdata))
4592                         ieee80211_restart_sta_timer(sdata);
4593         }
4594         rcu_read_unlock();
4595 }
4596
4597 static u8 ieee80211_ht_vht_rx_chains(struct ieee80211_sub_if_data *sdata,
4598                                      struct cfg80211_bss *cbss)
4599 {
4600         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4601         const u8 *ht_cap_ie, *vht_cap_ie;
4602         const struct ieee80211_ht_cap *ht_cap;
4603         const struct ieee80211_vht_cap *vht_cap;
4604         u8 chains = 1;
4605
4606         if (ifmgd->flags & IEEE80211_STA_DISABLE_HT)
4607                 return chains;
4608
4609         ht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
4610         if (ht_cap_ie && ht_cap_ie[1] >= sizeof(*ht_cap)) {
4611                 ht_cap = (void *)(ht_cap_ie + 2);
4612                 chains = ieee80211_mcs_to_chains(&ht_cap->mcs);
4613                 /*
4614                  * TODO: use "Tx Maximum Number Spatial Streams Supported" and
4615                  *       "Tx Unequal Modulation Supported" fields.
4616                  */
4617         }
4618
4619         if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
4620                 return chains;
4621
4622         vht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
4623         if (vht_cap_ie && vht_cap_ie[1] >= sizeof(*vht_cap)) {
4624                 u8 nss;
4625                 u16 tx_mcs_map;
4626
4627                 vht_cap = (void *)(vht_cap_ie + 2);
4628                 tx_mcs_map = le16_to_cpu(vht_cap->supp_mcs.tx_mcs_map);
4629                 for (nss = 8; nss > 0; nss--) {
4630                         if (((tx_mcs_map >> (2 * (nss - 1))) & 3) !=
4631                                         IEEE80211_VHT_MCS_NOT_SUPPORTED)
4632                                 break;
4633                 }
4634                 /* TODO: use "Tx Highest Supported Long GI Data Rate" field? */
4635                 chains = max(chains, nss);
4636         }
4637
4638         return chains;
4639 }
4640
4641 static bool
4642 ieee80211_verify_sta_he_mcs_support(struct ieee80211_supported_band *sband,
4643                                     const struct ieee80211_he_operation *he_op)
4644 {
4645         const struct ieee80211_sta_he_cap *sta_he_cap =
4646                 ieee80211_get_he_sta_cap(sband);
4647         u16 ap_min_req_set;
4648         int i;
4649
4650         if (!sta_he_cap || !he_op)
4651                 return false;
4652
4653         ap_min_req_set = le16_to_cpu(he_op->he_mcs_nss_set);
4654
4655         /* Need to go over for 80MHz, 160MHz and for 80+80 */
4656         for (i = 0; i < 3; i++) {
4657                 const struct ieee80211_he_mcs_nss_supp *sta_mcs_nss_supp =
4658                         &sta_he_cap->he_mcs_nss_supp;
4659                 u16 sta_mcs_map_rx =
4660                         le16_to_cpu(((__le16 *)sta_mcs_nss_supp)[2 * i]);
4661                 u16 sta_mcs_map_tx =
4662                         le16_to_cpu(((__le16 *)sta_mcs_nss_supp)[2 * i + 1]);
4663                 u8 nss;
4664                 bool verified = true;
4665
4666                 /*
4667                  * For each band there is a maximum of 8 spatial streams
4668                  * possible. Each of the sta_mcs_map_* is a 16-bit struct built
4669                  * of 2 bits per NSS (1-8), with the values defined in enum
4670                  * ieee80211_he_mcs_support. Need to make sure STA TX and RX
4671                  * capabilities aren't less than the AP's minimum requirements
4672                  * for this HE BSS per SS.
4673                  * It is enough to find one such band that meets the reqs.
4674                  */
4675                 for (nss = 8; nss > 0; nss--) {
4676                         u8 sta_rx_val = (sta_mcs_map_rx >> (2 * (nss - 1))) & 3;
4677                         u8 sta_tx_val = (sta_mcs_map_tx >> (2 * (nss - 1))) & 3;
4678                         u8 ap_val = (ap_min_req_set >> (2 * (nss - 1))) & 3;
4679
4680                         if (ap_val == IEEE80211_HE_MCS_NOT_SUPPORTED)
4681                                 continue;
4682
4683                         /*
4684                          * Make sure the HE AP doesn't require MCSs that aren't
4685                          * supported by the client
4686                          */
4687                         if (sta_rx_val == IEEE80211_HE_MCS_NOT_SUPPORTED ||
4688                             sta_tx_val == IEEE80211_HE_MCS_NOT_SUPPORTED ||
4689                             (ap_val > sta_rx_val) || (ap_val > sta_tx_val)) {
4690                                 verified = false;
4691                                 break;
4692                         }
4693                 }
4694
4695                 if (verified)
4696                         return true;
4697         }
4698
4699         /* If here, STA doesn't meet AP's HE min requirements */
4700         return false;
4701 }
4702
4703 static int ieee80211_prep_channel(struct ieee80211_sub_if_data *sdata,
4704                                   struct cfg80211_bss *cbss)
4705 {
4706         struct ieee80211_local *local = sdata->local;
4707         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4708         const struct ieee80211_ht_cap *ht_cap = NULL;
4709         const struct ieee80211_ht_operation *ht_oper = NULL;
4710         const struct ieee80211_vht_operation *vht_oper = NULL;
4711         const struct ieee80211_he_operation *he_oper = NULL;
4712         struct ieee80211_supported_band *sband;
4713         struct cfg80211_chan_def chandef;
4714         int ret;
4715         u32 i;
4716         bool have_80mhz;
4717
4718         sband = local->hw.wiphy->bands[cbss->channel->band];
4719
4720         ifmgd->flags &= ~(IEEE80211_STA_DISABLE_40MHZ |
4721                           IEEE80211_STA_DISABLE_80P80MHZ |
4722                           IEEE80211_STA_DISABLE_160MHZ);
4723
4724         rcu_read_lock();
4725
4726         if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
4727             sband->ht_cap.ht_supported) {
4728                 const u8 *ht_oper_ie, *ht_cap_ie;
4729
4730                 ht_oper_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_OPERATION);
4731                 if (ht_oper_ie && ht_oper_ie[1] >= sizeof(*ht_oper))
4732                         ht_oper = (void *)(ht_oper_ie + 2);
4733
4734                 ht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
4735                 if (ht_cap_ie && ht_cap_ie[1] >= sizeof(*ht_cap))
4736                         ht_cap = (void *)(ht_cap_ie + 2);
4737
4738                 if (!ht_cap) {
4739                         ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
4740                         ht_oper = NULL;
4741                 }
4742         }
4743
4744         if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
4745             sband->vht_cap.vht_supported) {
4746                 const u8 *vht_oper_ie, *vht_cap;
4747
4748                 vht_oper_ie = ieee80211_bss_get_ie(cbss,
4749                                                    WLAN_EID_VHT_OPERATION);
4750                 if (vht_oper_ie && vht_oper_ie[1] >= sizeof(*vht_oper))
4751                         vht_oper = (void *)(vht_oper_ie + 2);
4752                 if (vht_oper && !ht_oper) {
4753                         vht_oper = NULL;
4754                         sdata_info(sdata,
4755                                    "AP advertised VHT without HT, disabling both\n");
4756                         ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
4757                         ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
4758                 }
4759
4760                 vht_cap = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
4761                 if (!vht_cap || vht_cap[1] < sizeof(struct ieee80211_vht_cap)) {
4762                         ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
4763                         vht_oper = NULL;
4764                 }
4765         }
4766
4767         if (!ieee80211_get_he_sta_cap(sband))
4768                 ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
4769
4770         if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE)) {
4771                 const struct cfg80211_bss_ies *ies;
4772                 const u8 *he_oper_ie;
4773
4774                 ies = rcu_dereference(cbss->ies);
4775                 he_oper_ie = cfg80211_find_ext_ie(WLAN_EID_EXT_HE_OPERATION,
4776                                                   ies->data, ies->len);
4777                 if (he_oper_ie &&
4778                     he_oper_ie[1] == ieee80211_he_oper_size(&he_oper_ie[3]))
4779                         he_oper = (void *)(he_oper_ie + 3);
4780                 else
4781                         he_oper = NULL;
4782
4783                 if (!ieee80211_verify_sta_he_mcs_support(sband, he_oper))
4784                         ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
4785         }
4786
4787         /* Allow VHT if at least one channel on the sband supports 80 MHz */
4788         have_80mhz = false;
4789         for (i = 0; i < sband->n_channels; i++) {
4790                 if (sband->channels[i].flags & (IEEE80211_CHAN_DISABLED |
4791                                                 IEEE80211_CHAN_NO_80MHZ))
4792                         continue;
4793
4794                 have_80mhz = true;
4795                 break;
4796         }
4797
4798         if (!have_80mhz)
4799                 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
4800
4801         ifmgd->flags |= ieee80211_determine_chantype(sdata, sband,
4802                                                      cbss->channel,
4803                                                      ht_oper, vht_oper, he_oper,
4804                                                      &chandef, false);
4805
4806         sdata->needed_rx_chains = min(ieee80211_ht_vht_rx_chains(sdata, cbss),
4807                                       local->rx_chains);
4808
4809         rcu_read_unlock();
4810
4811         /* will change later if needed */
4812         sdata->smps_mode = IEEE80211_SMPS_OFF;
4813
4814         mutex_lock(&local->mtx);
4815         /*
4816          * If this fails (possibly due to channel context sharing
4817          * on incompatible channels, e.g. 80+80 and 160 sharing the
4818          * same control channel) try to use a smaller bandwidth.
4819          */
4820         ret = ieee80211_vif_use_channel(sdata, &chandef,
4821                                         IEEE80211_CHANCTX_SHARED);
4822
4823         /* don't downgrade for 5 and 10 MHz channels, though. */
4824         if (chandef.width == NL80211_CHAN_WIDTH_5 ||
4825             chandef.width == NL80211_CHAN_WIDTH_10)
4826                 goto out;
4827
4828         while (ret && chandef.width != NL80211_CHAN_WIDTH_20_NOHT) {
4829                 ifmgd->flags |= ieee80211_chandef_downgrade(&chandef);
4830                 ret = ieee80211_vif_use_channel(sdata, &chandef,
4831                                                 IEEE80211_CHANCTX_SHARED);
4832         }
4833  out:
4834         mutex_unlock(&local->mtx);
4835         return ret;
4836 }
4837
4838 static bool ieee80211_get_dtim(const struct cfg80211_bss_ies *ies,
4839                                u8 *dtim_count, u8 *dtim_period)
4840 {
4841         const u8 *tim_ie = cfg80211_find_ie(WLAN_EID_TIM, ies->data, ies->len);
4842         const u8 *idx_ie = cfg80211_find_ie(WLAN_EID_MULTI_BSSID_IDX, ies->data,
4843                                          ies->len);
4844         const struct ieee80211_tim_ie *tim = NULL;
4845         const struct ieee80211_bssid_index *idx;
4846         bool valid = tim_ie && tim_ie[1] >= 2;
4847
4848         if (valid)
4849                 tim = (void *)(tim_ie + 2);
4850
4851         if (dtim_count)
4852                 *dtim_count = valid ? tim->dtim_count : 0;
4853
4854         if (dtim_period)
4855                 *dtim_period = valid ? tim->dtim_period : 0;
4856
4857         /* Check if value is overridden by non-transmitted profile */
4858         if (!idx_ie || idx_ie[1] < 3)
4859                 return valid;
4860
4861         idx = (void *)(idx_ie + 2);
4862
4863         if (dtim_count)
4864                 *dtim_count = idx->dtim_count;
4865
4866         if (dtim_period)
4867                 *dtim_period = idx->dtim_period;
4868
4869         return true;
4870 }
4871
4872 static int ieee80211_prep_connection(struct ieee80211_sub_if_data *sdata,
4873                                      struct cfg80211_bss *cbss, bool assoc,
4874                                      bool override)
4875 {
4876         struct ieee80211_local *local = sdata->local;
4877         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4878         struct ieee80211_bss *bss = (void *)cbss->priv;
4879         struct sta_info *new_sta = NULL;
4880         struct ieee80211_supported_band *sband;
4881         bool have_sta = false;
4882         int err;
4883
4884         sband = local->hw.wiphy->bands[cbss->channel->band];
4885
4886         if (WARN_ON(!ifmgd->auth_data && !ifmgd->assoc_data))
4887                 return -EINVAL;
4888
4889         /* If a reconfig is happening, bail out */
4890         if (local->in_reconfig)
4891                 return -EBUSY;
4892
4893         if (assoc) {
4894                 rcu_read_lock();
4895                 have_sta = sta_info_get(sdata, cbss->bssid);
4896                 rcu_read_unlock();
4897         }
4898
4899         if (!have_sta) {
4900                 new_sta = sta_info_alloc(sdata, cbss->bssid, GFP_KERNEL);
4901                 if (!new_sta)
4902                         return -ENOMEM;
4903         }
4904
4905         /*
4906          * Set up the information for the new channel before setting the
4907          * new channel. We can't - completely race-free - change the basic
4908          * rates bitmap and the channel (sband) that it refers to, but if
4909          * we set it up before we at least avoid calling into the driver's
4910          * bss_info_changed() method with invalid information (since we do
4911          * call that from changing the channel - only for IDLE and perhaps
4912          * some others, but ...).
4913          *
4914          * So to avoid that, just set up all the new information before the
4915          * channel, but tell the driver to apply it only afterwards, since
4916          * it might need the new channel for that.
4917          */
4918         if (new_sta) {
4919                 u32 rates = 0, basic_rates = 0;
4920                 bool have_higher_than_11mbit;
4921                 int min_rate = INT_MAX, min_rate_index = -1;
4922                 const struct cfg80211_bss_ies *ies;
4923                 int shift = ieee80211_vif_get_shift(&sdata->vif);
4924
4925                 ieee80211_get_rates(sband, bss->supp_rates,
4926                                     bss->supp_rates_len,
4927                                     &rates, &basic_rates,
4928                                     &have_higher_than_11mbit,
4929                                     &min_rate, &min_rate_index,
4930                                     shift);
4931
4932                 /*
4933                  * This used to be a workaround for basic rates missing
4934                  * in the association response frame. Now that we no
4935                  * longer use the basic rates from there, it probably
4936                  * doesn't happen any more, but keep the workaround so
4937                  * in case some *other* APs are buggy in different ways
4938                  * we can connect -- with a warning.
4939                  */
4940                 if (!basic_rates && min_rate_index >= 0) {
4941                         sdata_info(sdata,
4942                                    "No basic rates, using min rate instead\n");
4943                         basic_rates = BIT(min_rate_index);
4944                 }
4945
4946                 new_sta->sta.supp_rates[cbss->channel->band] = rates;
4947                 sdata->vif.bss_conf.basic_rates = basic_rates;
4948
4949                 /* cf. IEEE 802.11 9.2.12 */
4950                 if (cbss->channel->band == NL80211_BAND_2GHZ &&
4951                     have_higher_than_11mbit)
4952                         sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
4953                 else
4954                         sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
4955
4956                 memcpy(ifmgd->bssid, cbss->bssid, ETH_ALEN);
4957
4958                 /* set timing information */
4959                 sdata->vif.bss_conf.beacon_int = cbss->beacon_interval;
4960                 rcu_read_lock();
4961                 ies = rcu_dereference(cbss->beacon_ies);
4962                 if (ies) {
4963                         sdata->vif.bss_conf.sync_tsf = ies->tsf;
4964                         sdata->vif.bss_conf.sync_device_ts =
4965                                 bss->device_ts_beacon;
4966
4967                         ieee80211_get_dtim(ies,
4968                                            &sdata->vif.bss_conf.sync_dtim_count,
4969                                            NULL);
4970                 } else if (!ieee80211_hw_check(&sdata->local->hw,
4971                                                TIMING_BEACON_ONLY)) {
4972                         ies = rcu_dereference(cbss->proberesp_ies);
4973                         /* must be non-NULL since beacon IEs were NULL */
4974                         sdata->vif.bss_conf.sync_tsf = ies->tsf;
4975                         sdata->vif.bss_conf.sync_device_ts =
4976                                 bss->device_ts_presp;
4977                         sdata->vif.bss_conf.sync_dtim_count = 0;
4978                 } else {
4979                         sdata->vif.bss_conf.sync_tsf = 0;
4980                         sdata->vif.bss_conf.sync_device_ts = 0;
4981                         sdata->vif.bss_conf.sync_dtim_count = 0;
4982                 }
4983                 rcu_read_unlock();
4984         }
4985
4986         if (new_sta || override) {
4987                 err = ieee80211_prep_channel(sdata, cbss);
4988                 if (err) {
4989                         if (new_sta)
4990                                 sta_info_free(local, new_sta);
4991                         return -EINVAL;
4992                 }
4993         }
4994
4995         if (new_sta) {
4996                 /*
4997                  * tell driver about BSSID, basic rates and timing
4998                  * this was set up above, before setting the channel
4999                  */
5000                 ieee80211_bss_info_change_notify(sdata,
5001                         BSS_CHANGED_BSSID | BSS_CHANGED_BASIC_RATES |
5002                         BSS_CHANGED_BEACON_INT);
5003
5004                 if (assoc)
5005                         sta_info_pre_move_state(new_sta, IEEE80211_STA_AUTH);
5006
5007                 err = sta_info_insert(new_sta);
5008                 new_sta = NULL;
5009                 if (err) {
5010                         sdata_info(sdata,
5011                                    "failed to insert STA entry for the AP (error %d)\n",
5012                                    err);
5013                         return err;
5014                 }
5015         } else
5016                 WARN_ON_ONCE(!ether_addr_equal(ifmgd->bssid, cbss->bssid));
5017
5018         /* Cancel scan to ensure that nothing interferes with connection */
5019         if (local->scanning)
5020                 ieee80211_scan_cancel(local);
5021
5022         return 0;
5023 }
5024
5025 /* config hooks */
5026 int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
5027                        struct cfg80211_auth_request *req)
5028 {
5029         struct ieee80211_local *local = sdata->local;
5030         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
5031         struct ieee80211_mgd_auth_data *auth_data;
5032         u16 auth_alg;
5033         int err;
5034         bool cont_auth;
5035
5036         /* prepare auth data structure */
5037
5038         switch (req->auth_type) {
5039         case NL80211_AUTHTYPE_OPEN_SYSTEM:
5040                 auth_alg = WLAN_AUTH_OPEN;
5041                 break;
5042         case NL80211_AUTHTYPE_SHARED_KEY:
5043                 if (IS_ERR(local->wep_tx_tfm))
5044                         return -EOPNOTSUPP;
5045                 auth_alg = WLAN_AUTH_SHARED_KEY;
5046                 break;
5047         case NL80211_AUTHTYPE_FT:
5048                 auth_alg = WLAN_AUTH_FT;
5049                 break;
5050         case NL80211_AUTHTYPE_NETWORK_EAP:
5051                 auth_alg = WLAN_AUTH_LEAP;
5052                 break;
5053         case NL80211_AUTHTYPE_SAE:
5054                 auth_alg = WLAN_AUTH_SAE;
5055                 break;
5056         case NL80211_AUTHTYPE_FILS_SK:
5057                 auth_alg = WLAN_AUTH_FILS_SK;
5058                 break;
5059         case NL80211_AUTHTYPE_FILS_SK_PFS:
5060                 auth_alg = WLAN_AUTH_FILS_SK_PFS;
5061                 break;
5062         case NL80211_AUTHTYPE_FILS_PK:
5063                 auth_alg = WLAN_AUTH_FILS_PK;
5064                 break;
5065         default:
5066                 return -EOPNOTSUPP;
5067         }
5068
5069         if (ifmgd->assoc_data)
5070                 return -EBUSY;
5071
5072         auth_data = kzalloc(sizeof(*auth_data) + req->auth_data_len +
5073                             req->ie_len, GFP_KERNEL);
5074         if (!auth_data)
5075                 return -ENOMEM;
5076
5077         auth_data->bss = req->bss;
5078
5079         if (req->auth_data_len >= 4) {
5080                 if (req->auth_type == NL80211_AUTHTYPE_SAE) {
5081                         __le16 *pos = (__le16 *) req->auth_data;
5082
5083                         auth_data->sae_trans = le16_to_cpu(pos[0]);
5084                         auth_data->sae_status = le16_to_cpu(pos[1]);
5085                 }
5086                 memcpy(auth_data->data, req->auth_data + 4,
5087                        req->auth_data_len - 4);
5088                 auth_data->data_len += req->auth_data_len - 4;
5089         }
5090
5091         /* Check if continuing authentication or trying to authenticate with the
5092          * same BSS that we were in the process of authenticating with and avoid
5093          * removal and re-addition of the STA entry in
5094          * ieee80211_prep_connection().
5095          */
5096         cont_auth = ifmgd->auth_data && req->bss == ifmgd->auth_data->bss;
5097
5098         if (req->ie && req->ie_len) {
5099                 memcpy(&auth_data->data[auth_data->data_len],
5100                        req->ie, req->ie_len);
5101                 auth_data->data_len += req->ie_len;
5102         }
5103
5104         if (req->key && req->key_len) {
5105                 auth_data->key_len = req->key_len;
5106                 auth_data->key_idx = req->key_idx;
5107                 memcpy(auth_data->key, req->key, req->key_len);
5108         }
5109
5110         auth_data->algorithm = auth_alg;
5111
5112         /* try to authenticate/probe */
5113
5114         if (ifmgd->auth_data) {
5115                 if (cont_auth && req->auth_type == NL80211_AUTHTYPE_SAE) {
5116                         auth_data->peer_confirmed =
5117                                 ifmgd->auth_data->peer_confirmed;
5118                 }
5119                 ieee80211_destroy_auth_data(sdata, cont_auth);
5120         }
5121
5122         /* prep auth_data so we don't go into idle on disassoc */
5123         ifmgd->auth_data = auth_data;
5124
5125         /* If this is continuation of an ongoing SAE authentication exchange
5126          * (i.e., request to send SAE Confirm) and the peer has already
5127          * confirmed, mark authentication completed since we are about to send
5128          * out SAE Confirm.
5129          */
5130         if (cont_auth && req->auth_type == NL80211_AUTHTYPE_SAE &&
5131             auth_data->peer_confirmed && auth_data->sae_trans == 2)
5132                 ieee80211_mark_sta_auth(sdata, req->bss->bssid);
5133
5134         if (ifmgd->associated) {
5135                 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
5136
5137                 sdata_info(sdata,
5138                            "disconnect from AP %pM for new auth to %pM\n",
5139                            ifmgd->associated->bssid, req->bss->bssid);
5140                 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
5141                                        WLAN_REASON_UNSPECIFIED,
5142                                        false, frame_buf);
5143
5144                 ieee80211_report_disconnect(sdata, frame_buf,
5145                                             sizeof(frame_buf), true,
5146                                             WLAN_REASON_UNSPECIFIED);
5147         }
5148
5149         sdata_info(sdata, "authenticate with %pM\n", req->bss->bssid);
5150
5151         err = ieee80211_prep_connection(sdata, req->bss, cont_auth, false);
5152         if (err)
5153                 goto err_clear;
5154
5155         err = ieee80211_auth(sdata);
5156         if (err) {
5157                 sta_info_destroy_addr(sdata, req->bss->bssid);
5158                 goto err_clear;
5159         }
5160
5161         /* hold our own reference */
5162         cfg80211_ref_bss(local->hw.wiphy, auth_data->bss);
5163         return 0;
5164
5165  err_clear:
5166         eth_zero_addr(ifmgd->bssid);
5167         ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
5168         ifmgd->auth_data = NULL;
5169         mutex_lock(&sdata->local->mtx);
5170         ieee80211_vif_release_channel(sdata);
5171         mutex_unlock(&sdata->local->mtx);
5172         kfree(auth_data);
5173         return err;
5174 }
5175
5176 int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
5177                         struct cfg80211_assoc_request *req)
5178 {
5179         struct ieee80211_local *local = sdata->local;
5180         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
5181         struct ieee80211_bss *bss = (void *)req->bss->priv;
5182         struct ieee80211_mgd_assoc_data *assoc_data;
5183         const struct cfg80211_bss_ies *beacon_ies;
5184         struct ieee80211_supported_band *sband;
5185         const u8 *ssidie, *ht_ie, *vht_ie;
5186         int i, err;
5187         bool override = false;
5188
5189         assoc_data = kzalloc(sizeof(*assoc_data) + req->ie_len, GFP_KERNEL);
5190         if (!assoc_data)
5191                 return -ENOMEM;
5192
5193         rcu_read_lock();
5194         ssidie = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
5195         if (!ssidie) {
5196                 rcu_read_unlock();
5197                 kfree(assoc_data);
5198                 return -EINVAL;
5199         }
5200         memcpy(assoc_data->ssid, ssidie + 2, ssidie[1]);
5201         assoc_data->ssid_len = ssidie[1];
5202         rcu_read_unlock();
5203
5204         if (ifmgd->associated) {
5205                 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
5206
5207                 sdata_info(sdata,
5208                            "disconnect from AP %pM for new assoc to %pM\n",
5209                            ifmgd->associated->bssid, req->bss->bssid);
5210                 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
5211                                        WLAN_REASON_UNSPECIFIED,
5212                                        false, frame_buf);
5213
5214                 ieee80211_report_disconnect(sdata, frame_buf,
5215                                             sizeof(frame_buf), true,
5216                                             WLAN_REASON_UNSPECIFIED);
5217         }
5218
5219         if (ifmgd->auth_data && !ifmgd->auth_data->done) {
5220                 err = -EBUSY;
5221                 goto err_free;
5222         }
5223
5224         if (ifmgd->assoc_data) {
5225                 err = -EBUSY;
5226                 goto err_free;
5227         }
5228
5229         if (ifmgd->auth_data) {
5230                 bool match;
5231
5232                 /* keep sta info, bssid if matching */
5233                 match = ether_addr_equal(ifmgd->bssid, req->bss->bssid);
5234                 ieee80211_destroy_auth_data(sdata, match);
5235         }
5236
5237         /* prepare assoc data */
5238
5239         ifmgd->beacon_crc_valid = false;
5240
5241         assoc_data->wmm = bss->wmm_used &&
5242                           (local->hw.queues >= IEEE80211_NUM_ACS);
5243
5244         /*
5245          * IEEE802.11n does not allow TKIP/WEP as pairwise ciphers in HT mode.
5246          * We still associate in non-HT mode (11a/b/g) if any one of these
5247          * ciphers is configured as pairwise.
5248          * We can set this to true for non-11n hardware, that'll be checked
5249          * separately along with the peer capabilities.
5250          */
5251         for (i = 0; i < req->crypto.n_ciphers_pairwise; i++) {
5252                 if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
5253                     req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
5254                     req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104) {
5255                         ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
5256                         ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
5257                         ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
5258                         netdev_info(sdata->dev,
5259                                     "disabling HE/HT/VHT due to WEP/TKIP use\n");
5260                 }
5261         }
5262
5263         /* Also disable HT if we don't support it or the AP doesn't use WMM */
5264         sband = local->hw.wiphy->bands[req->bss->channel->band];
5265         if (!sband->ht_cap.ht_supported ||
5266             local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used ||
5267             ifmgd->flags & IEEE80211_STA_DISABLE_WMM) {
5268                 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
5269                 if (!bss->wmm_used &&
5270                     !(ifmgd->flags & IEEE80211_STA_DISABLE_WMM))
5271                         netdev_info(sdata->dev,
5272                                     "disabling HT as WMM/QoS is not supported by the AP\n");
5273         }
5274
5275         /* disable VHT if we don't support it or the AP doesn't use WMM */
5276         if (!sband->vht_cap.vht_supported ||
5277             local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used ||
5278             ifmgd->flags & IEEE80211_STA_DISABLE_WMM) {
5279                 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
5280                 if (!bss->wmm_used &&
5281                     !(ifmgd->flags & IEEE80211_STA_DISABLE_WMM))
5282                         netdev_info(sdata->dev,
5283                                     "disabling VHT as WMM/QoS is not supported by the AP\n");
5284         }
5285
5286         memcpy(&ifmgd->ht_capa, &req->ht_capa, sizeof(ifmgd->ht_capa));
5287         memcpy(&ifmgd->ht_capa_mask, &req->ht_capa_mask,
5288                sizeof(ifmgd->ht_capa_mask));
5289
5290         memcpy(&ifmgd->vht_capa, &req->vht_capa, sizeof(ifmgd->vht_capa));
5291         memcpy(&ifmgd->vht_capa_mask, &req->vht_capa_mask,
5292                sizeof(ifmgd->vht_capa_mask));
5293
5294         if (req->ie && req->ie_len) {
5295                 memcpy(assoc_data->ie, req->ie, req->ie_len);
5296                 assoc_data->ie_len = req->ie_len;
5297         }
5298
5299         if (req->fils_kek) {
5300                 /* should already be checked in cfg80211 - so warn */
5301                 if (WARN_ON(req->fils_kek_len > FILS_MAX_KEK_LEN)) {
5302                         err = -EINVAL;
5303                         goto err_free;
5304                 }
5305                 memcpy(assoc_data->fils_kek, req->fils_kek,
5306                        req->fils_kek_len);
5307                 assoc_data->fils_kek_len = req->fils_kek_len;
5308         }
5309
5310         if (req->fils_nonces)
5311                 memcpy(assoc_data->fils_nonces, req->fils_nonces,
5312                        2 * FILS_NONCE_LEN);
5313
5314         assoc_data->bss = req->bss;
5315
5316         if (ifmgd->req_smps == IEEE80211_SMPS_AUTOMATIC) {
5317                 if (ifmgd->powersave)
5318                         sdata->smps_mode = IEEE80211_SMPS_DYNAMIC;
5319                 else
5320                         sdata->smps_mode = IEEE80211_SMPS_OFF;
5321         } else
5322                 sdata->smps_mode = ifmgd->req_smps;
5323
5324         assoc_data->capability = req->bss->capability;
5325         assoc_data->supp_rates = bss->supp_rates;
5326         assoc_data->supp_rates_len = bss->supp_rates_len;
5327
5328         rcu_read_lock();
5329         ht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_HT_OPERATION);
5330         if (ht_ie && ht_ie[1] >= sizeof(struct ieee80211_ht_operation))
5331                 assoc_data->ap_ht_param =
5332                         ((struct ieee80211_ht_operation *)(ht_ie + 2))->ht_param;
5333         else
5334                 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
5335         vht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_VHT_CAPABILITY);
5336         if (vht_ie && vht_ie[1] >= sizeof(struct ieee80211_vht_cap))
5337                 memcpy(&assoc_data->ap_vht_cap, vht_ie + 2,
5338                        sizeof(struct ieee80211_vht_cap));
5339         else
5340                 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
5341         rcu_read_unlock();
5342
5343         if (WARN((sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_UAPSD) &&
5344                  ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK),
5345              "U-APSD not supported with HW_PS_NULLFUNC_STACK\n"))
5346                 sdata->vif.driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
5347
5348         if (bss->wmm_used && bss->uapsd_supported &&
5349             (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_UAPSD)) {
5350                 assoc_data->uapsd = true;
5351                 ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED;
5352         } else {
5353                 assoc_data->uapsd = false;
5354                 ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED;
5355         }
5356
5357         if (req->prev_bssid)
5358                 memcpy(assoc_data->prev_bssid, req->prev_bssid, ETH_ALEN);
5359
5360         if (req->use_mfp) {
5361                 ifmgd->mfp = IEEE80211_MFP_REQUIRED;
5362                 ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
5363         } else {
5364                 ifmgd->mfp = IEEE80211_MFP_DISABLED;
5365                 ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
5366         }
5367
5368         if (req->flags & ASSOC_REQ_USE_RRM)
5369                 ifmgd->flags |= IEEE80211_STA_ENABLE_RRM;
5370         else
5371                 ifmgd->flags &= ~IEEE80211_STA_ENABLE_RRM;
5372
5373         if (req->crypto.control_port)
5374                 ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
5375         else
5376                 ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
5377
5378         sdata->control_port_protocol = req->crypto.control_port_ethertype;
5379         sdata->control_port_no_encrypt = req->crypto.control_port_no_encrypt;
5380         sdata->control_port_over_nl80211 =
5381                                         req->crypto.control_port_over_nl80211;
5382         sdata->encrypt_headroom = ieee80211_cs_headroom(local, &req->crypto,
5383                                                         sdata->vif.type);
5384
5385         /* kick off associate process */
5386
5387         ifmgd->assoc_data = assoc_data;
5388         ifmgd->dtim_period = 0;
5389         ifmgd->have_beacon = false;
5390
5391         /* override HT/VHT configuration only if the AP and we support it */
5392         if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
5393                 struct ieee80211_sta_ht_cap sta_ht_cap;
5394
5395                 if (req->flags & ASSOC_REQ_DISABLE_HT)
5396                         override = true;
5397
5398                 memcpy(&sta_ht_cap, &sband->ht_cap, sizeof(sta_ht_cap));
5399                 ieee80211_apply_htcap_overrides(sdata, &sta_ht_cap);
5400
5401                 /* check for 40 MHz disable override */
5402                 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_40MHZ) &&
5403                     sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40 &&
5404                     !(sta_ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40))
5405                         override = true;
5406
5407                 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
5408                     req->flags & ASSOC_REQ_DISABLE_VHT)
5409                         override = true;
5410         }
5411
5412         if (req->flags & ASSOC_REQ_DISABLE_HT) {
5413                 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
5414                 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
5415         }
5416
5417         if (req->flags & ASSOC_REQ_DISABLE_VHT)
5418                 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
5419
5420         err = ieee80211_prep_connection(sdata, req->bss, true, override);
5421         if (err)
5422                 goto err_clear;
5423
5424         rcu_read_lock();
5425         beacon_ies = rcu_dereference(req->bss->beacon_ies);
5426
5427         if (ieee80211_hw_check(&sdata->local->hw, NEED_DTIM_BEFORE_ASSOC) &&
5428             !beacon_ies) {
5429                 /*
5430                  * Wait up to one beacon interval ...
5431                  * should this be more if we miss one?
5432                  */
5433                 sdata_info(sdata, "waiting for beacon from %pM\n",
5434                            ifmgd->bssid);
5435                 assoc_data->timeout = TU_TO_EXP_TIME(req->bss->beacon_interval);
5436                 assoc_data->timeout_started = true;
5437                 assoc_data->need_beacon = true;
5438         } else if (beacon_ies) {
5439                 const u8 *ie;
5440                 u8 dtim_count = 0;
5441
5442                 ieee80211_get_dtim(beacon_ies, &dtim_count,
5443                                    &ifmgd->dtim_period);
5444
5445                 ifmgd->have_beacon = true;
5446                 assoc_data->timeout = jiffies;
5447                 assoc_data->timeout_started = true;
5448
5449                 if (ieee80211_hw_check(&local->hw, TIMING_BEACON_ONLY)) {
5450                         sdata->vif.bss_conf.sync_tsf = beacon_ies->tsf;
5451                         sdata->vif.bss_conf.sync_device_ts =
5452                                 bss->device_ts_beacon;
5453                         sdata->vif.bss_conf.sync_dtim_count = dtim_count;
5454                 }
5455
5456                 ie = cfg80211_find_ext_ie(WLAN_EID_EXT_MULTIPLE_BSSID_CONFIGURATION,
5457                                           beacon_ies->data, beacon_ies->len);
5458                 if (ie && ie[1] >= 3)
5459                         sdata->vif.bss_conf.profile_periodicity = ie[4];
5460
5461                 ie = cfg80211_find_ie(WLAN_EID_EXT_CAPABILITY,
5462                                       beacon_ies->data, beacon_ies->len);
5463                 if (ie && ie[1] >= 11 &&
5464                     (ie[10] & WLAN_EXT_CAPA11_EMA_SUPPORT))
5465                         sdata->vif.bss_conf.ema_ap = true;
5466         } else {
5467                 assoc_data->timeout = jiffies;
5468                 assoc_data->timeout_started = true;
5469         }
5470         rcu_read_unlock();
5471
5472         run_again(sdata, assoc_data->timeout);
5473
5474         if (bss->corrupt_data) {
5475                 char *corrupt_type = "data";
5476                 if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_BEACON) {
5477                         if (bss->corrupt_data &
5478                                         IEEE80211_BSS_CORRUPT_PROBE_RESP)
5479                                 corrupt_type = "beacon and probe response";
5480                         else
5481                                 corrupt_type = "beacon";
5482                 } else if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_PROBE_RESP)
5483                         corrupt_type = "probe response";
5484                 sdata_info(sdata, "associating with AP with corrupt %s\n",
5485                            corrupt_type);
5486         }
5487
5488         return 0;
5489  err_clear:
5490         eth_zero_addr(ifmgd->bssid);
5491         ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
5492         ifmgd->assoc_data = NULL;
5493  err_free:
5494         kfree(assoc_data);
5495         return err;
5496 }
5497
5498 int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
5499                          struct cfg80211_deauth_request *req)
5500 {
5501         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
5502         u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
5503         bool tx = !req->local_state_change;
5504
5505         if (ifmgd->auth_data &&
5506             ether_addr_equal(ifmgd->auth_data->bss->bssid, req->bssid)) {
5507                 sdata_info(sdata,
5508                            "aborting authentication with %pM by local choice (Reason: %u=%s)\n",
5509                            req->bssid, req->reason_code,
5510                            ieee80211_get_reason_code_string(req->reason_code));
5511
5512                 drv_mgd_prepare_tx(sdata->local, sdata, 0);
5513                 ieee80211_send_deauth_disassoc(sdata, req->bssid,
5514                                                IEEE80211_STYPE_DEAUTH,
5515                                                req->reason_code, tx,
5516                                                frame_buf);
5517                 ieee80211_destroy_auth_data(sdata, false);
5518                 ieee80211_report_disconnect(sdata, frame_buf,
5519                                             sizeof(frame_buf), true,
5520                                             req->reason_code);
5521
5522                 return 0;
5523         }
5524
5525         if (ifmgd->assoc_data &&
5526             ether_addr_equal(ifmgd->assoc_data->bss->bssid, req->bssid)) {
5527                 sdata_info(sdata,
5528                            "aborting association with %pM by local choice (Reason: %u=%s)\n",
5529                            req->bssid, req->reason_code,
5530                            ieee80211_get_reason_code_string(req->reason_code));
5531
5532                 drv_mgd_prepare_tx(sdata->local, sdata, 0);
5533                 ieee80211_send_deauth_disassoc(sdata, req->bssid,
5534                                                IEEE80211_STYPE_DEAUTH,
5535                                                req->reason_code, tx,
5536                                                frame_buf);
5537                 ieee80211_destroy_assoc_data(sdata, false, true);
5538                 ieee80211_report_disconnect(sdata, frame_buf,
5539                                             sizeof(frame_buf), true,
5540                                             req->reason_code);
5541                 return 0;
5542         }
5543
5544         if (ifmgd->associated &&
5545             ether_addr_equal(ifmgd->associated->bssid, req->bssid)) {
5546                 sdata_info(sdata,
5547                            "deauthenticating from %pM by local choice (Reason: %u=%s)\n",
5548                            req->bssid, req->reason_code,
5549                            ieee80211_get_reason_code_string(req->reason_code));
5550
5551                 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
5552                                        req->reason_code, tx, frame_buf);
5553                 ieee80211_report_disconnect(sdata, frame_buf,
5554                                             sizeof(frame_buf), true,
5555                                             req->reason_code);
5556                 return 0;
5557         }
5558
5559         return -ENOTCONN;
5560 }
5561
5562 int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
5563                            struct cfg80211_disassoc_request *req)
5564 {
5565         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
5566         u8 bssid[ETH_ALEN];
5567         u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
5568
5569         /*
5570          * cfg80211 should catch this ... but it's racy since
5571          * we can receive a disassoc frame, process it, hand it
5572          * to cfg80211 while that's in a locked section already
5573          * trying to tell us that the user wants to disconnect.
5574          */
5575         if (ifmgd->associated != req->bss)
5576                 return -ENOLINK;
5577
5578         sdata_info(sdata,
5579                    "disassociating from %pM by local choice (Reason: %u=%s)\n",
5580                    req->bss->bssid, req->reason_code, ieee80211_get_reason_code_string(req->reason_code));
5581
5582         memcpy(bssid, req->bss->bssid, ETH_ALEN);
5583         ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DISASSOC,
5584                                req->reason_code, !req->local_state_change,
5585                                frame_buf);
5586
5587         ieee80211_report_disconnect(sdata, frame_buf, sizeof(frame_buf), true,
5588                                     req->reason_code);
5589
5590         return 0;
5591 }
5592
5593 void ieee80211_mgd_stop(struct ieee80211_sub_if_data *sdata)
5594 {
5595         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
5596
5597         /*
5598          * Make sure some work items will not run after this,
5599          * they will not do anything but might not have been
5600          * cancelled when disconnecting.
5601          */
5602         cancel_work_sync(&ifmgd->monitor_work);
5603         cancel_work_sync(&ifmgd->beacon_connection_loss_work);
5604         cancel_work_sync(&ifmgd->request_smps_work);
5605         cancel_work_sync(&ifmgd->csa_connection_drop_work);
5606         cancel_work_sync(&ifmgd->chswitch_work);
5607         cancel_delayed_work_sync(&ifmgd->tdls_peer_del_work);
5608
5609         sdata_lock(sdata);
5610         if (ifmgd->assoc_data) {
5611                 struct cfg80211_bss *bss = ifmgd->assoc_data->bss;
5612                 ieee80211_destroy_assoc_data(sdata, false, false);
5613                 cfg80211_assoc_timeout(sdata->dev, bss);
5614         }
5615         if (ifmgd->auth_data)
5616                 ieee80211_destroy_auth_data(sdata, false);
5617         spin_lock_bh(&ifmgd->teardown_lock);
5618         if (ifmgd->teardown_skb) {
5619                 kfree_skb(ifmgd->teardown_skb);
5620                 ifmgd->teardown_skb = NULL;
5621                 ifmgd->orig_teardown_skb = NULL;
5622         }
5623         kfree(ifmgd->assoc_req_ies);
5624         ifmgd->assoc_req_ies = NULL;
5625         ifmgd->assoc_req_ies_len = 0;
5626         spin_unlock_bh(&ifmgd->teardown_lock);
5627         del_timer_sync(&ifmgd->timer);
5628         sdata_unlock(sdata);
5629 }
5630
5631 void ieee80211_cqm_rssi_notify(struct ieee80211_vif *vif,
5632                                enum nl80211_cqm_rssi_threshold_event rssi_event,
5633                                s32 rssi_level,
5634                                gfp_t gfp)
5635 {
5636         struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
5637
5638         trace_api_cqm_rssi_notify(sdata, rssi_event, rssi_level);
5639
5640         cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, rssi_level, gfp);
5641 }
5642 EXPORT_SYMBOL(ieee80211_cqm_rssi_notify);
5643
5644 void ieee80211_cqm_beacon_loss_notify(struct ieee80211_vif *vif, gfp_t gfp)
5645 {
5646         struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
5647
5648         trace_api_cqm_beacon_loss_notify(sdata->local, sdata);
5649
5650         cfg80211_cqm_beacon_loss_notify(sdata->dev, gfp);
5651 }
5652 EXPORT_SYMBOL(ieee80211_cqm_beacon_loss_notify);