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