root/net/mac80211/mlme.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. run_again
  2. ieee80211_sta_reset_beacon_monitor
  3. ieee80211_sta_reset_conn_monitor
  4. ecw2cw
  5. ieee80211_determine_chantype
  6. ieee80211_config_bw
  7. ieee80211_add_ht_ie
  8. ieee80211_add_vht_ie
  9. ieee80211_add_he_ie
  10. ieee80211_send_assoc
  11. ieee80211_send_pspoll
  12. ieee80211_send_nullfunc
  13. ieee80211_send_4addr_nullfunc
  14. ieee80211_chswitch_work
  15. ieee80211_chswitch_post_beacon
  16. ieee80211_chswitch_done
  17. ieee80211_chswitch_timer
  18. ieee80211_sta_abort_chanswitch
  19. ieee80211_sta_process_chanswitch
  20. ieee80211_find_80211h_pwr_constr
  21. ieee80211_find_cisco_dtpc
  22. ieee80211_handle_pwr_constr
  23. ieee80211_enable_ps
  24. ieee80211_change_ps
  25. ieee80211_powersave_allowed
  26. ieee80211_recalc_ps
  27. ieee80211_recalc_ps_vif
  28. ieee80211_dynamic_ps_disable_work
  29. ieee80211_dynamic_ps_enable_work
  30. ieee80211_dynamic_ps_timer
  31. ieee80211_dfs_cac_timer_work
  32. __ieee80211_sta_handle_tspec_ac_params
  33. ieee80211_sta_handle_tspec_ac_params
  34. ieee80211_sta_handle_tspec_ac_params_wk
  35. ieee80211_sta_wmm_params
  36. __ieee80211_stop_poll
  37. ieee80211_stop_poll
  38. ieee80211_handle_bss_capability
  39. ieee80211_set_associated
  40. ieee80211_set_disassoc
  41. ieee80211_sta_rx_notify
  42. ieee80211_reset_ap_probe
  43. ieee80211_sta_tx_wmm_ac_notify
  44. ieee80211_sta_tx_notify
  45. ieee80211_mlme_send_probe_req
  46. ieee80211_mgd_probe_ap_send
  47. ieee80211_mgd_probe_ap
  48. ieee80211_ap_probereq_get
  49. ieee80211_report_disconnect
  50. __ieee80211_disconnect
  51. ieee80211_beacon_connection_loss_work
  52. ieee80211_csa_connection_drop_work
  53. ieee80211_beacon_loss
  54. ieee80211_connection_loss
  55. ieee80211_destroy_auth_data
  56. ieee80211_destroy_assoc_data
  57. ieee80211_auth_challenge
  58. ieee80211_mark_sta_auth
  59. ieee80211_rx_mgmt_auth
  60. ieee80211_get_reason_code_string
  61. ieee80211_rx_mgmt_deauth
  62. ieee80211_rx_mgmt_disassoc
  63. ieee80211_get_rates
  64. ieee80211_twt_req_supported
  65. ieee80211_recalc_twt_req
  66. ieee80211_assoc_success
  67. ieee80211_rx_mgmt_assoc_resp
  68. ieee80211_rx_bss_info
  69. ieee80211_rx_mgmt_probe_resp
  70. ieee80211_handle_beacon_sig
  71. ieee80211_rx_our_beacon
  72. ieee80211_rx_mgmt_beacon
  73. ieee80211_sta_rx_queued_mgmt
  74. ieee80211_sta_timer
  75. ieee80211_sta_connection_lost
  76. ieee80211_auth
  77. ieee80211_do_assoc
  78. ieee80211_mgd_conn_tx_status
  79. ieee80211_sta_work
  80. ieee80211_sta_bcn_mon_timer
  81. ieee80211_sta_conn_mon_timer
  82. ieee80211_sta_monitor_work
  83. ieee80211_restart_sta_timer
  84. ieee80211_mgd_quiesce
  85. ieee80211_sta_restart
  86. ieee80211_sta_setup_sdata
  87. ieee80211_mlme_notify_scan_completed
  88. ieee80211_ht_vht_rx_chains
  89. ieee80211_verify_sta_he_mcs_support
  90. ieee80211_prep_channel
  91. ieee80211_get_dtim
  92. ieee80211_prep_connection
  93. ieee80211_mgd_auth
  94. ieee80211_mgd_assoc
  95. ieee80211_mgd_deauth
  96. ieee80211_mgd_disassoc
  97. ieee80211_mgd_stop
  98. ieee80211_cqm_rssi_notify
  99. ieee80211_cqm_beacon_loss_notify

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

/* [<][>][^][v][top][bottom][index][help] */