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