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