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