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