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