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