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