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