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