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