OSDN Git Service

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