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