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