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