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