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