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