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