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