~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/mac80211/mlme.c

Version: ~ [ linux-5.6-rc1 ] ~ [ linux-5.5.2 ] ~ [ linux-5.4.17 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.102 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.170 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.213 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.213 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.81 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp