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