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