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