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