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