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