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