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