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