]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - net/mac80211/mlme.c
mac80211: update driver when MU EDCA params change
[mirror_ubuntu-jammy-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>
d98ad83e 8 * Copyright 2013-2014 Intel Mobile Communications GmbH
e38a017b 9 * Copyright (C) 2015 - 2017 Intel Deutschland GmbH
94ba9271 10 * Copyright (C) 2018 Intel Corporation
f0706e82
JB
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
15 */
16
5b323edb 17#include <linux/delay.h>
f0706e82
JB
18#include <linux/if_ether.h>
19#include <linux/skbuff.h>
f0706e82 20#include <linux/if_arp.h>
f0706e82 21#include <linux/etherdevice.h>
d9b93842 22#include <linux/moduleparam.h>
d0709a65 23#include <linux/rtnetlink.h>
d91f36db 24#include <linux/crc32.h>
5a0e3ad6 25#include <linux/slab.h>
bc3b2d7f 26#include <linux/export.h>
f0706e82 27#include <net/mac80211.h>
472dbc45 28#include <asm/unaligned.h>
60f8b39c 29
f0706e82 30#include "ieee80211_i.h"
24487981 31#include "driver-ops.h"
2c8dccc7
JB
32#include "rate.h"
33#include "led.h"
39404fee 34#include "fils_aead.h"
f0706e82 35
1672c0e3 36#define IEEE80211_AUTH_TIMEOUT (HZ / 5)
cb236d2d 37#define IEEE80211_AUTH_TIMEOUT_LONG (HZ / 2)
1672c0e3 38#define IEEE80211_AUTH_TIMEOUT_SHORT (HZ / 10)
407879b6 39#define IEEE80211_AUTH_TIMEOUT_SAE (HZ * 2)
1672c0e3
JB
40#define IEEE80211_AUTH_MAX_TRIES 3
41#define IEEE80211_AUTH_WAIT_ASSOC (HZ * 5)
42#define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
cb236d2d 43#define IEEE80211_ASSOC_TIMEOUT_LONG (HZ / 2)
1672c0e3
JB
44#define IEEE80211_ASSOC_TIMEOUT_SHORT (HZ / 10)
45#define IEEE80211_ASSOC_MAX_TRIES 3
66e67e41 46
180205bd
BG
47static int max_nullfunc_tries = 2;
48module_param(max_nullfunc_tries, int, 0644);
49MODULE_PARM_DESC(max_nullfunc_tries,
50 "Maximum nullfunc tx tries before disconnecting (reason 4).");
51
52static int max_probe_tries = 5;
53module_param(max_probe_tries, int, 0644);
54MODULE_PARM_DESC(max_probe_tries,
55 "Maximum probe tries before disconnecting (reason 4).");
b291ba11
JB
56
57/*
7ccc8bd7
FF
58 * Beacon loss timeout is calculated as N frames times the
59 * advertised beacon interval. This may need to be somewhat
60 * higher than what hardware might detect to account for
61 * delays in the host processing frames. But since we also
62 * probe on beacon miss before declaring the connection lost
63 * default to what we want.
b291ba11 64 */
59c1ec2b
BG
65static int beacon_loss_count = 7;
66module_param(beacon_loss_count, int, 0644);
67MODULE_PARM_DESC(beacon_loss_count,
68 "Number of beacon intervals before we decide beacon was lost.");
7ccc8bd7 69
b291ba11
JB
70/*
71 * Time the connection can be idle before we probe
72 * it to see if we can still talk to the AP.
73 */
d1c5091f 74#define IEEE80211_CONNECTION_IDLE_TIME (30 * HZ)
b291ba11
JB
75/*
76 * Time we wait for a probe response after sending
77 * a probe request because of beacon loss or for
78 * checking the connection still works.
79 */
180205bd
BG
80static int probe_wait_ms = 500;
81module_param(probe_wait_ms, int, 0644);
82MODULE_PARM_DESC(probe_wait_ms,
83 "Maximum time(ms) to wait for probe response"
84 " before disconnecting (reason 4).");
f0706e82 85
391a200a
JM
86/*
87 * How many Beacon frames need to have been used in average signal strength
88 * before starting to indicate signal change events.
89 */
90#define IEEE80211_SIGNAL_AVE_MIN_COUNT 4
91
ca386f31
JB
92/*
93 * We can have multiple work items (and connection probing)
94 * scheduling this timer, but we need to take care to only
95 * reschedule it when it should fire _earlier_ than it was
96 * asked for before, or if it's not pending right now. This
97 * function ensures that. Note that it then is required to
98 * run this function for all timeouts after the first one
99 * has happened -- the work that runs from this timer will
100 * do that.
101 */
8d61ffa5
JB
102static void run_again(struct ieee80211_sub_if_data *sdata,
103 unsigned long timeout)
ca386f31 104{
8d61ffa5 105 sdata_assert_lock(sdata);
ca386f31 106
8d61ffa5
JB
107 if (!timer_pending(&sdata->u.mgd.timer) ||
108 time_before(timeout, sdata->u.mgd.timer.expires))
109 mod_timer(&sdata->u.mgd.timer, timeout);
ca386f31
JB
110}
111
d3a910a8 112void ieee80211_sta_reset_beacon_monitor(struct ieee80211_sub_if_data *sdata)
b291ba11 113{
c1288b12 114 if (sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)
b291ba11
JB
115 return;
116
30686bf7 117 if (ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
7eeff74c
JB
118 return;
119
b291ba11 120 mod_timer(&sdata->u.mgd.bcn_mon_timer,
7ccc8bd7 121 round_jiffies_up(jiffies + sdata->u.mgd.beacon_timeout));
b291ba11
JB
122}
123
be099e82
LR
124void ieee80211_sta_reset_conn_monitor(struct ieee80211_sub_if_data *sdata)
125{
0c699c3a
LR
126 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
127
b100e5d6 128 if (unlikely(!ifmgd->associated))
8628172f
SG
129 return;
130
b100e5d6
JB
131 if (ifmgd->probe_send_count)
132 ifmgd->probe_send_count = 0;
448cd2e2 133
30686bf7 134 if (ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
be099e82
LR
135 return;
136
b100e5d6 137 mod_timer(&ifmgd->conn_mon_timer,
be099e82
LR
138 round_jiffies_up(jiffies + IEEE80211_CONNECTION_IDLE_TIME));
139}
140
5484e237 141static int ecw2cw(int ecw)
60f8b39c 142{
5484e237 143 return (1 << ecw) - 1;
60f8b39c
JB
144}
145
6565ec9b
JB
146static u32
147ieee80211_determine_chantype(struct ieee80211_sub_if_data *sdata,
148 struct ieee80211_supported_band *sband,
149 struct ieee80211_channel *channel,
150 const struct ieee80211_ht_operation *ht_oper,
151 const struct ieee80211_vht_operation *vht_oper,
41cbb0f5 152 const struct ieee80211_he_operation *he_oper,
5cdaed1e 153 struct cfg80211_chan_def *chandef, bool tracking)
6565ec9b 154{
5cdaed1e 155 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
6565ec9b 156 struct cfg80211_chan_def vht_chandef;
179b8fc7 157 struct ieee80211_sta_ht_cap sta_ht_cap;
6565ec9b
JB
158 u32 ht_cfreq, ret;
159
179b8fc7
CRI
160 memcpy(&sta_ht_cap, &sband->ht_cap, sizeof(sta_ht_cap));
161 ieee80211_apply_htcap_overrides(sdata, &sta_ht_cap);
162
6565ec9b
JB
163 chandef->chan = channel;
164 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
165 chandef->center_freq1 = channel->center_freq;
166 chandef->center_freq2 = 0;
167
b1b1ae2c 168 if (!ht_oper || !sta_ht_cap.ht_supported) {
6565ec9b
JB
169 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
170 goto out;
171 }
172
173 chandef->width = NL80211_CHAN_WIDTH_20;
174
175 ht_cfreq = ieee80211_channel_to_frequency(ht_oper->primary_chan,
176 channel->band);
177 /* check that channel matches the right operating channel */
5cdaed1e 178 if (!tracking && channel->center_freq != ht_cfreq) {
6565ec9b
JB
179 /*
180 * It's possible that some APs are confused here;
181 * Netgear WNDR3700 sometimes reports 4 higher than
182 * the actual channel in association responses, but
183 * since we look at probe response/beacon data here
184 * it should be OK.
185 */
5cdaed1e
JB
186 sdata_info(sdata,
187 "Wrong control channel: center-freq: %d ht-cfreq: %d ht->primary_chan: %d band: %d - Disabling HT\n",
188 channel->center_freq, ht_cfreq,
189 ht_oper->primary_chan, channel->band);
6565ec9b
JB
190 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
191 goto out;
192 }
193
194 /* check 40 MHz support, if we have it */
179b8fc7 195 if (sta_ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) {
8ac3c704 196 ieee80211_chandef_ht_oper(ht_oper, chandef);
6565ec9b
JB
197 } else {
198 /* 40 MHz (and 80 MHz) must be supported for VHT */
199 ret = IEEE80211_STA_DISABLE_VHT;
85220d71
JB
200 /* also mark 40 MHz disabled */
201 ret |= IEEE80211_STA_DISABLE_40MHZ;
6565ec9b
JB
202 goto out;
203 }
204
205 if (!vht_oper || !sband->vht_cap.vht_supported) {
206 ret = IEEE80211_STA_DISABLE_VHT;
207 goto out;
208 }
209
8ac3c704 210 vht_chandef = *chandef;
41cbb0f5
LC
211 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE) && he_oper &&
212 (le32_to_cpu(he_oper->he_oper_params) &
213 IEEE80211_HE_OPERATION_VHT_OPER_INFO)) {
214 struct ieee80211_vht_operation he_oper_vht_cap;
215
216 /*
217 * Set only first 3 bytes (other 2 aren't used in
218 * ieee80211_chandef_vht_oper() anyway)
219 */
220 memcpy(&he_oper_vht_cap, he_oper->optional, 3);
221 he_oper_vht_cap.basic_mcs_set = cpu_to_le16(0);
222
7eb26df2
JB
223 if (!ieee80211_chandef_vht_oper(&sdata->local->hw,
224 &he_oper_vht_cap, ht_oper,
41cbb0f5
LC
225 &vht_chandef)) {
226 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE))
227 sdata_info(sdata,
228 "HE AP VHT information is invalid, disable HE\n");
229 ret = IEEE80211_STA_DISABLE_HE;
230 goto out;
231 }
7eb26df2
JB
232 } else if (!ieee80211_chandef_vht_oper(&sdata->local->hw, vht_oper,
233 ht_oper, &vht_chandef)) {
5cdaed1e 234 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
30eb1dc2 235 sdata_info(sdata,
8ac3c704 236 "AP VHT information is invalid, disable VHT\n");
6565ec9b
JB
237 ret = IEEE80211_STA_DISABLE_VHT;
238 goto out;
239 }
240
241 if (!cfg80211_chandef_valid(&vht_chandef)) {
5cdaed1e 242 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
30eb1dc2
JB
243 sdata_info(sdata,
244 "AP VHT information is invalid, disable VHT\n");
6565ec9b
JB
245 ret = IEEE80211_STA_DISABLE_VHT;
246 goto out;
247 }
248
249 if (cfg80211_chandef_identical(chandef, &vht_chandef)) {
250 ret = 0;
251 goto out;
252 }
253
254 if (!cfg80211_chandef_compatible(chandef, &vht_chandef)) {
5cdaed1e 255 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
30eb1dc2
JB
256 sdata_info(sdata,
257 "AP VHT information doesn't match HT, disable VHT\n");
6565ec9b
JB
258 ret = IEEE80211_STA_DISABLE_VHT;
259 goto out;
260 }
261
262 *chandef = vht_chandef;
263
264 ret = 0;
265
266out:
963a1852
JB
267 /*
268 * When tracking the current AP, don't do any further checks if the
269 * new chandef is identical to the one we're currently using for the
270 * connection. This keeps us from playing ping-pong with regulatory,
271 * without it the following can happen (for example):
272 * - connect to an AP with 80 MHz, world regdom allows 80 MHz
273 * - AP advertises regdom US
274 * - CRDA loads regdom US with 80 MHz prohibited (old database)
275 * - the code below detects an unsupported channel, downgrades, and
276 * we disconnect from the AP in the caller
277 * - disconnect causes CRDA to reload world regdomain and the game
278 * starts anew.
279 * (see https://bugzilla.kernel.org/show_bug.cgi?id=70881)
280 *
281 * It seems possible that there are still scenarios with CSA or real
282 * bandwidth changes where a this could happen, but those cases are
283 * less common and wouldn't completely prevent using the AP.
284 */
285 if (tracking &&
286 cfg80211_chandef_identical(chandef, &sdata->vif.bss_conf.chandef))
287 return ret;
288
586e01ed
JB
289 /* don't print the message below for VHT mismatch if VHT is disabled */
290 if (ret & IEEE80211_STA_DISABLE_VHT)
291 vht_chandef = *chandef;
292
ddfe49b4
JB
293 /*
294 * Ignore the DISABLED flag when we're already connected and only
295 * tracking the APs beacon for bandwidth changes - otherwise we
296 * might get disconnected here if we connect to an AP, update our
297 * regulatory information based on the AP's country IE and the
298 * information we have is wrong/outdated and disables the channel
299 * that we're actually using for the connection to the AP.
300 */
6565ec9b 301 while (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
ddfe49b4
JB
302 tracking ? 0 :
303 IEEE80211_CHAN_DISABLED)) {
6565ec9b
JB
304 if (WARN_ON(chandef->width == NL80211_CHAN_WIDTH_20_NOHT)) {
305 ret = IEEE80211_STA_DISABLE_HT |
306 IEEE80211_STA_DISABLE_VHT;
b56e4b85 307 break;
6565ec9b
JB
308 }
309
e6b7cde4 310 ret |= ieee80211_chandef_downgrade(chandef);
6565ec9b
JB
311 }
312
5cdaed1e 313 if (chandef->width != vht_chandef.width && !tracking)
6565ec9b
JB
314 sdata_info(sdata,
315 "capabilities/regulatory prevented using AP HT/VHT configuration, downgraded\n");
316
317 WARN_ON_ONCE(!cfg80211_chandef_valid(chandef));
318 return ret;
319}
320
30eb1dc2
JB
321static int ieee80211_config_bw(struct ieee80211_sub_if_data *sdata,
322 struct sta_info *sta,
53b954ee 323 const struct ieee80211_ht_cap *ht_cap,
30eb1dc2
JB
324 const struct ieee80211_ht_operation *ht_oper,
325 const struct ieee80211_vht_operation *vht_oper,
41cbb0f5 326 const struct ieee80211_he_operation *he_oper,
30eb1dc2 327 const u8 *bssid, u32 *changed)
d5522e03
JB
328{
329 struct ieee80211_local *local = sdata->local;
30eb1dc2 330 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
41cbb0f5
LC
331 struct ieee80211_channel *chan = sdata->vif.bss_conf.chandef.chan;
332 struct ieee80211_supported_band *sband =
333 local->hw.wiphy->bands[chan->band];
30eb1dc2 334 struct cfg80211_chan_def chandef;
9ed6bcce 335 u16 ht_opmode;
30eb1dc2
JB
336 u32 flags;
337 enum ieee80211_sta_rx_bandwidth new_sta_bw;
338 int ret;
d5522e03 339
30eb1dc2
JB
340 /* if HT was/is disabled, don't track any bandwidth changes */
341 if (ifmgd->flags & IEEE80211_STA_DISABLE_HT || !ht_oper)
1128958d
JB
342 return 0;
343
30eb1dc2
JB
344 /* don't check VHT if we associated as non-VHT station */
345 if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
346 vht_oper = NULL;
347
41cbb0f5
LC
348 /* don't check HE if we associated as non-HE station */
349 if (ifmgd->flags & IEEE80211_STA_DISABLE_HE ||
350 !ieee80211_get_he_sta_cap(sband))
351 he_oper = NULL;
352
30eb1dc2
JB
353 if (WARN_ON_ONCE(!sta))
354 return -EINVAL;
355
017b45bb
AA
356 /*
357 * if bss configuration changed store the new one -
358 * this may be applicable even if channel is identical
359 */
360 ht_opmode = le16_to_cpu(ht_oper->operation_mode);
361 if (sdata->vif.bss_conf.ht_operation_mode != ht_opmode) {
362 *changed |= BSS_CHANGED_HT;
363 sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
364 }
365
41cbb0f5 366 /* calculate new channel (type) based on HT/VHT/HE operation IEs */
53b954ee 367 flags = ieee80211_determine_chantype(sdata, sband, chan,
41cbb0f5 368 ht_oper, vht_oper, he_oper,
53b954ee 369 &chandef, true);
30eb1dc2
JB
370
371 /*
372 * Downgrade the new channel if we associated with restricted
373 * capabilities. For example, if we associated as a 20 MHz STA
374 * to a 40 MHz AP (due to regulatory, capabilities or config
375 * reasons) then switching to a 40 MHz channel now won't do us
376 * any good -- we couldn't use it with the AP.
377 */
378 if (ifmgd->flags & IEEE80211_STA_DISABLE_80P80MHZ &&
379 chandef.width == NL80211_CHAN_WIDTH_80P80)
e6b7cde4 380 flags |= ieee80211_chandef_downgrade(&chandef);
30eb1dc2
JB
381 if (ifmgd->flags & IEEE80211_STA_DISABLE_160MHZ &&
382 chandef.width == NL80211_CHAN_WIDTH_160)
e6b7cde4 383 flags |= ieee80211_chandef_downgrade(&chandef);
30eb1dc2
JB
384 if (ifmgd->flags & IEEE80211_STA_DISABLE_40MHZ &&
385 chandef.width > NL80211_CHAN_WIDTH_20)
e6b7cde4 386 flags |= ieee80211_chandef_downgrade(&chandef);
30eb1dc2
JB
387
388 if (cfg80211_chandef_identical(&chandef, &sdata->vif.bss_conf.chandef))
389 return 0;
390
391 sdata_info(sdata,
392 "AP %pM changed bandwidth, new config is %d MHz, width %d (%d/%d MHz)\n",
393 ifmgd->bssid, chandef.chan->center_freq, chandef.width,
394 chandef.center_freq1, chandef.center_freq2);
395
396 if (flags != (ifmgd->flags & (IEEE80211_STA_DISABLE_HT |
397 IEEE80211_STA_DISABLE_VHT |
398 IEEE80211_STA_DISABLE_40MHZ |
399 IEEE80211_STA_DISABLE_80P80MHZ |
400 IEEE80211_STA_DISABLE_160MHZ)) ||
401 !cfg80211_chandef_valid(&chandef)) {
402 sdata_info(sdata,
403 "AP %pM changed bandwidth in a way we can't support - disconnect\n",
404 ifmgd->bssid);
405 return -EINVAL;
406 }
407
408 switch (chandef.width) {
409 case NL80211_CHAN_WIDTH_20_NOHT:
410 case NL80211_CHAN_WIDTH_20:
411 new_sta_bw = IEEE80211_STA_RX_BW_20;
412 break;
4bf88530 413 case NL80211_CHAN_WIDTH_40:
30eb1dc2 414 new_sta_bw = IEEE80211_STA_RX_BW_40;
64f68e5d 415 break;
30eb1dc2
JB
416 case NL80211_CHAN_WIDTH_80:
417 new_sta_bw = IEEE80211_STA_RX_BW_80;
418 break;
419 case NL80211_CHAN_WIDTH_80P80:
420 case NL80211_CHAN_WIDTH_160:
421 new_sta_bw = IEEE80211_STA_RX_BW_160;
64f68e5d 422 break;
30eb1dc2
JB
423 default:
424 return -EINVAL;
64f68e5d 425 }
d5522e03 426
30eb1dc2
JB
427 if (new_sta_bw > sta->cur_max_bandwidth)
428 new_sta_bw = sta->cur_max_bandwidth;
64f68e5d 429
30eb1dc2
JB
430 if (new_sta_bw < sta->sta.bandwidth) {
431 sta->sta.bandwidth = new_sta_bw;
432 rate_control_rate_update(local, sband, sta,
433 IEEE80211_RC_BW_CHANGED);
434 }
64f68e5d 435
30eb1dc2
JB
436 ret = ieee80211_vif_change_bandwidth(sdata, &chandef, changed);
437 if (ret) {
438 sdata_info(sdata,
439 "AP %pM changed bandwidth to incompatible one - disconnect\n",
440 ifmgd->bssid);
441 return ret;
442 }
7cc44ed4 443
30eb1dc2
JB
444 if (new_sta_bw > sta->sta.bandwidth) {
445 sta->sta.bandwidth = new_sta_bw;
446 rate_control_rate_update(local, sband, sta,
447 IEEE80211_RC_BW_CHANGED);
0aaffa9b 448 }
d5522e03 449
30eb1dc2 450 return 0;
d5522e03
JB
451}
452
9c6bd790
JB
453/* frame sending functions */
454
66e67e41 455static void ieee80211_add_ht_ie(struct ieee80211_sub_if_data *sdata,
9dde6423 456 struct sk_buff *skb, u8 ap_ht_param,
66e67e41
JB
457 struct ieee80211_supported_band *sband,
458 struct ieee80211_channel *channel,
459 enum ieee80211_smps_mode smps)
460{
66e67e41
JB
461 u8 *pos;
462 u32 flags = channel->flags;
463 u16 cap;
464 struct ieee80211_sta_ht_cap ht_cap;
465
466 BUILD_BUG_ON(sizeof(ht_cap) != sizeof(sband->ht_cap));
467
66e67e41
JB
468 memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
469 ieee80211_apply_htcap_overrides(sdata, &ht_cap);
470
66e67e41
JB
471 /* determine capability flags */
472 cap = ht_cap.cap;
473
9dde6423 474 switch (ap_ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
66e67e41
JB
475 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
476 if (flags & IEEE80211_CHAN_NO_HT40PLUS) {
477 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
478 cap &= ~IEEE80211_HT_CAP_SGI_40;
479 }
480 break;
481 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
482 if (flags & IEEE80211_CHAN_NO_HT40MINUS) {
483 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
484 cap &= ~IEEE80211_HT_CAP_SGI_40;
485 }
486 break;
487 }
488
24398e39
JB
489 /*
490 * If 40 MHz was disabled associate as though we weren't
491 * capable of 40 MHz -- some broken APs will never fall
492 * back to trying to transmit in 20 MHz.
493 */
494 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_40MHZ) {
495 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
496 cap &= ~IEEE80211_HT_CAP_SGI_40;
497 }
498
66e67e41
JB
499 /* set SM PS mode properly */
500 cap &= ~IEEE80211_HT_CAP_SM_PS;
501 switch (smps) {
502 case IEEE80211_SMPS_AUTOMATIC:
503 case IEEE80211_SMPS_NUM_MODES:
504 WARN_ON(1);
02049ce2 505 /* fall through */
66e67e41
JB
506 case IEEE80211_SMPS_OFF:
507 cap |= WLAN_HT_CAP_SM_PS_DISABLED <<
508 IEEE80211_HT_CAP_SM_PS_SHIFT;
509 break;
510 case IEEE80211_SMPS_STATIC:
511 cap |= WLAN_HT_CAP_SM_PS_STATIC <<
512 IEEE80211_HT_CAP_SM_PS_SHIFT;
513 break;
514 case IEEE80211_SMPS_DYNAMIC:
515 cap |= WLAN_HT_CAP_SM_PS_DYNAMIC <<
516 IEEE80211_HT_CAP_SM_PS_SHIFT;
517 break;
518 }
519
520 /* reserve and fill IE */
521 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
522 ieee80211_ie_build_ht_cap(pos, &ht_cap, cap);
523}
524
322cd406
SS
525/* This function determines vht capability flags for the association
526 * and builds the IE.
527 * Note - the function may set the owner of the MU-MIMO capability
528 */
d545daba
MP
529static void ieee80211_add_vht_ie(struct ieee80211_sub_if_data *sdata,
530 struct sk_buff *skb,
b08fbbd8
JB
531 struct ieee80211_supported_band *sband,
532 struct ieee80211_vht_cap *ap_vht_cap)
d545daba 533{
322cd406 534 struct ieee80211_local *local = sdata->local;
d545daba
MP
535 u8 *pos;
536 u32 cap;
537 struct ieee80211_sta_vht_cap vht_cap;
c1cf6d4e 538 u32 mask, ap_bf_sts, our_bf_sts;
d545daba
MP
539
540 BUILD_BUG_ON(sizeof(vht_cap) != sizeof(sband->vht_cap));
541
542 memcpy(&vht_cap, &sband->vht_cap, sizeof(vht_cap));
dd5ecfea 543 ieee80211_apply_vhtcap_overrides(sdata, &vht_cap);
d545daba
MP
544
545 /* determine capability flags */
546 cap = vht_cap.cap;
547
f2d9d270 548 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_80P80MHZ) {
575f0530
JB
549 u32 bw = cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
550
551 cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
552 if (bw == IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ ||
553 bw == IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ)
554 cap |= IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
f2d9d270
JB
555 }
556
557 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_160MHZ) {
558 cap &= ~IEEE80211_VHT_CAP_SHORT_GI_160;
575f0530 559 cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
f2d9d270
JB
560 }
561
b08fbbd8
JB
562 /*
563 * Some APs apparently get confused if our capabilities are better
564 * than theirs, so restrict what we advertise in the assoc request.
565 */
566 if (!(ap_vht_cap->vht_cap_info &
567 cpu_to_le32(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)))
322cd406
SS
568 cap &= ~(IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
569 IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE);
570 else if (!(ap_vht_cap->vht_cap_info &
571 cpu_to_le32(IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)))
572 cap &= ~IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE;
573
574 /*
575 * If some other vif is using the MU-MIMO capablity we cannot associate
576 * using MU-MIMO - this will lead to contradictions in the group-id
577 * mechanism.
578 * Ownership is defined since association request, in order to avoid
579 * simultaneous associations with MU-MIMO.
580 */
581 if (cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE) {
582 bool disable_mu_mimo = false;
583 struct ieee80211_sub_if_data *other;
584
585 list_for_each_entry_rcu(other, &local->interfaces, list) {
b5a33d52 586 if (other->vif.mu_mimo_owner) {
322cd406
SS
587 disable_mu_mimo = true;
588 break;
589 }
590 }
591 if (disable_mu_mimo)
592 cap &= ~IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE;
593 else
b5a33d52 594 sdata->vif.mu_mimo_owner = true;
322cd406 595 }
b08fbbd8 596
c1cf6d4e
ES
597 mask = IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
598
599 ap_bf_sts = le32_to_cpu(ap_vht_cap->vht_cap_info) & mask;
600 our_bf_sts = cap & mask;
601
602 if (ap_bf_sts < our_bf_sts) {
603 cap &= ~mask;
604 cap |= ap_bf_sts;
605 }
606
d545daba 607 /* reserve and fill IE */
d4950281 608 pos = skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2);
d545daba
MP
609 ieee80211_ie_build_vht_cap(pos, &vht_cap, cap);
610}
611
41cbb0f5
LC
612/* This function determines HE capability flags for the association
613 * and builds the IE.
614 */
615static void ieee80211_add_he_ie(struct ieee80211_sub_if_data *sdata,
616 struct sk_buff *skb,
617 struct ieee80211_supported_band *sband)
618{
619 u8 *pos;
620 const struct ieee80211_sta_he_cap *he_cap = NULL;
621 u8 he_cap_size;
622
623 he_cap = ieee80211_get_he_sta_cap(sband);
624 if (!he_cap)
625 return;
626
627 /*
628 * TODO: the 1 added is because this temporarily is under the EXTENSION
629 * IE. Get rid of it when it moves.
630 */
631 he_cap_size =
632 2 + 1 + sizeof(he_cap->he_cap_elem) +
633 ieee80211_he_mcs_nss_size(&he_cap->he_cap_elem) +
634 ieee80211_he_ppe_size(he_cap->ppe_thres[0],
635 he_cap->he_cap_elem.phy_cap_info);
636 pos = skb_put(skb, he_cap_size);
637 ieee80211_ie_build_he_cap(pos, he_cap, pos + he_cap_size);
638}
639
66e67e41
JB
640static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata)
641{
642 struct ieee80211_local *local = sdata->local;
643 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
644 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
645 struct sk_buff *skb;
646 struct ieee80211_mgmt *mgmt;
647 u8 *pos, qos_info;
648 size_t offset = 0, noffset;
2103dec1 649 int i, count, rates_len, supp_rates_len, shift;
66e67e41
JB
650 u16 capab;
651 struct ieee80211_supported_band *sband;
55de908a
JB
652 struct ieee80211_chanctx_conf *chanctx_conf;
653 struct ieee80211_channel *chan;
44f6d42c 654 u32 rates = 0;
66e67e41 655
8d61ffa5 656 sdata_assert_lock(sdata);
66e67e41 657
55de908a
JB
658 rcu_read_lock();
659 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
660 if (WARN_ON(!chanctx_conf)) {
661 rcu_read_unlock();
662 return;
663 }
4bf88530 664 chan = chanctx_conf->def.chan;
55de908a
JB
665 rcu_read_unlock();
666 sband = local->hw.wiphy->bands[chan->band];
2103dec1 667 shift = ieee80211_vif_get_shift(&sdata->vif);
66e67e41
JB
668
669 if (assoc_data->supp_rates_len) {
670 /*
671 * Get all rates supported by the device and the AP as
672 * some APs don't like getting a superset of their rates
673 * in the association request (e.g. D-Link DAP 1353 in
674 * b-only mode)...
675 */
2103dec1
SW
676 rates_len = ieee80211_parse_bitrates(&chanctx_conf->def, sband,
677 assoc_data->supp_rates,
678 assoc_data->supp_rates_len,
679 &rates);
66e67e41
JB
680 } else {
681 /*
682 * In case AP not provide any supported rates information
683 * before association, we send information element(s) with
684 * all rates that we support.
685 */
2103dec1
SW
686 rates_len = 0;
687 for (i = 0; i < sband->n_bitrates; i++) {
2103dec1
SW
688 rates |= BIT(i);
689 rates_len++;
690 }
66e67e41
JB
691 }
692
693 skb = alloc_skb(local->hw.extra_tx_headroom +
694 sizeof(*mgmt) + /* bit too much but doesn't matter */
695 2 + assoc_data->ssid_len + /* SSID */
696 4 + rates_len + /* (extended) rates */
697 4 + /* power capability */
698 2 + 2 * sband->n_channels + /* supported channels */
699 2 + sizeof(struct ieee80211_ht_cap) + /* HT */
d4950281 700 2 + sizeof(struct ieee80211_vht_cap) + /* VHT */
41cbb0f5
LC
701 2 + 1 + sizeof(struct ieee80211_he_cap_elem) + /* HE */
702 sizeof(struct ieee80211_he_mcs_nss_supp) +
703 IEEE80211_HE_PPE_THRES_MAX_LEN +
66e67e41 704 assoc_data->ie_len + /* extra IEs */
39404fee 705 (assoc_data->fils_kek_len ? 16 /* AES-SIV */ : 0) +
66e67e41
JB
706 9, /* WMM */
707 GFP_KERNEL);
708 if (!skb)
709 return;
710
711 skb_reserve(skb, local->hw.extra_tx_headroom);
712
713 capab = WLAN_CAPABILITY_ESS;
714
57fbcce3 715 if (sband->band == NL80211_BAND_2GHZ) {
ea1b2b45
JB
716 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
717 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
66e67e41
JB
718 }
719
720 if (assoc_data->capability & WLAN_CAPABILITY_PRIVACY)
721 capab |= WLAN_CAPABILITY_PRIVACY;
722
723 if ((assoc_data->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
30686bf7 724 ieee80211_hw_check(&local->hw, SPECTRUM_MGMT))
66e67e41
JB
725 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
726
cd2f5dd7
AK
727 if (ifmgd->flags & IEEE80211_STA_ENABLE_RRM)
728 capab |= WLAN_CAPABILITY_RADIO_MEASURE;
729
b080db58 730 mgmt = skb_put_zero(skb, 24);
66e67e41
JB
731 memcpy(mgmt->da, assoc_data->bss->bssid, ETH_ALEN);
732 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
733 memcpy(mgmt->bssid, assoc_data->bss->bssid, ETH_ALEN);
734
735 if (!is_zero_ether_addr(assoc_data->prev_bssid)) {
736 skb_put(skb, 10);
737 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
738 IEEE80211_STYPE_REASSOC_REQ);
739 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
740 mgmt->u.reassoc_req.listen_interval =
741 cpu_to_le16(local->hw.conf.listen_interval);
742 memcpy(mgmt->u.reassoc_req.current_ap, assoc_data->prev_bssid,
743 ETH_ALEN);
744 } else {
745 skb_put(skb, 4);
746 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
747 IEEE80211_STYPE_ASSOC_REQ);
748 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
749 mgmt->u.assoc_req.listen_interval =
750 cpu_to_le16(local->hw.conf.listen_interval);
751 }
752
753 /* SSID */
754 pos = skb_put(skb, 2 + assoc_data->ssid_len);
755 *pos++ = WLAN_EID_SSID;
756 *pos++ = assoc_data->ssid_len;
757 memcpy(pos, assoc_data->ssid, assoc_data->ssid_len);
758
759 /* add all rates which were marked to be used above */
760 supp_rates_len = rates_len;
761 if (supp_rates_len > 8)
762 supp_rates_len = 8;
763
764 pos = skb_put(skb, supp_rates_len + 2);
765 *pos++ = WLAN_EID_SUPP_RATES;
766 *pos++ = supp_rates_len;
767
768 count = 0;
769 for (i = 0; i < sband->n_bitrates; i++) {
770 if (BIT(i) & rates) {
2103dec1
SW
771 int rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
772 5 * (1 << shift));
773 *pos++ = (u8) rate;
66e67e41
JB
774 if (++count == 8)
775 break;
776 }
777 }
778
779 if (rates_len > count) {
780 pos = skb_put(skb, rates_len - count + 2);
781 *pos++ = WLAN_EID_EXT_SUPP_RATES;
782 *pos++ = rates_len - count;
783
784 for (i++; i < sband->n_bitrates; i++) {
785 if (BIT(i) & rates) {
2103dec1
SW
786 int rate;
787 rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
788 5 * (1 << shift));
789 *pos++ = (u8) rate;
66e67e41
JB
790 }
791 }
792 }
793
cd2f5dd7
AK
794 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT ||
795 capab & WLAN_CAPABILITY_RADIO_MEASURE) {
66e67e41
JB
796 pos = skb_put(skb, 4);
797 *pos++ = WLAN_EID_PWR_CAPABILITY;
798 *pos++ = 2;
799 *pos++ = 0; /* min tx power */
0430c883
SW
800 /* max tx power */
801 *pos++ = ieee80211_chandef_max_power(&chanctx_conf->def);
cd2f5dd7 802 }
66e67e41 803
cd2f5dd7 804 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
66e67e41
JB
805 /* TODO: get this in reg domain format */
806 pos = skb_put(skb, 2 * sband->n_channels + 2);
807 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
808 *pos++ = 2 * sband->n_channels;
809 for (i = 0; i < sband->n_channels; i++) {
810 *pos++ = ieee80211_frequency_to_channel(
811 sband->channels[i].center_freq);
812 *pos++ = 1; /* one channel in the subband*/
813 }
814 }
815
816 /* if present, add any custom IEs that go before HT */
b3f51e94 817 if (assoc_data->ie_len) {
66e67e41
JB
818 static const u8 before_ht[] = {
819 WLAN_EID_SSID,
820 WLAN_EID_SUPP_RATES,
821 WLAN_EID_EXT_SUPP_RATES,
822 WLAN_EID_PWR_CAPABILITY,
823 WLAN_EID_SUPPORTED_CHANNELS,
824 WLAN_EID_RSN,
825 WLAN_EID_QOS_CAPA,
826 WLAN_EID_RRM_ENABLED_CAPABILITIES,
827 WLAN_EID_MOBILITY_DOMAIN,
8ed28747
JB
828 WLAN_EID_FAST_BSS_TRANSITION, /* reassoc only */
829 WLAN_EID_RIC_DATA, /* reassoc only */
66e67e41
JB
830 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
831 };
8ed28747
JB
832 static const u8 after_ric[] = {
833 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
834 WLAN_EID_HT_CAPABILITY,
835 WLAN_EID_BSS_COEX_2040,
a7f26d80 836 /* luckily this is almost always there */
8ed28747
JB
837 WLAN_EID_EXT_CAPABILITY,
838 WLAN_EID_QOS_TRAFFIC_CAPA,
839 WLAN_EID_TIM_BCAST_REQ,
840 WLAN_EID_INTERWORKING,
a7f26d80 841 /* 60 GHz (Multi-band, DMG, MMS) can't happen */
8ed28747
JB
842 WLAN_EID_VHT_CAPABILITY,
843 WLAN_EID_OPMODE_NOTIF,
844 };
845
846 noffset = ieee80211_ie_split_ric(assoc_data->ie,
847 assoc_data->ie_len,
848 before_ht,
849 ARRAY_SIZE(before_ht),
850 after_ric,
851 ARRAY_SIZE(after_ric),
852 offset);
b952f4df 853 skb_put_data(skb, assoc_data->ie + offset, noffset - offset);
66e67e41
JB
854 offset = noffset;
855 }
856
f2d9d270
JB
857 if (WARN_ON_ONCE((ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
858 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)))
859 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
860
a8243b72 861 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
9dde6423 862 ieee80211_add_ht_ie(sdata, skb, assoc_data->ap_ht_param,
04ecd257 863 sband, chan, sdata->smps_mode);
66e67e41 864
3de3802c
JB
865 /* if present, add any custom IEs that go before VHT */
866 if (assoc_data->ie_len) {
867 static const u8 before_vht[] = {
a7f26d80
JB
868 /*
869 * no need to list the ones split off before HT
870 * or generated here
871 */
3de3802c
JB
872 WLAN_EID_BSS_COEX_2040,
873 WLAN_EID_EXT_CAPABILITY,
874 WLAN_EID_QOS_TRAFFIC_CAPA,
875 WLAN_EID_TIM_BCAST_REQ,
876 WLAN_EID_INTERWORKING,
a7f26d80 877 /* 60 GHz (Multi-band, DMG, MMS) can't happen */
3de3802c 878 };
8ed28747
JB
879
880 /* RIC already taken above, so no need to handle here anymore */
3de3802c
JB
881 noffset = ieee80211_ie_split(assoc_data->ie, assoc_data->ie_len,
882 before_vht, ARRAY_SIZE(before_vht),
883 offset);
b952f4df 884 skb_put_data(skb, assoc_data->ie + offset, noffset - offset);
3de3802c
JB
885 offset = noffset;
886 }
887
41cbb0f5
LC
888 /* if present, add any custom IEs that go before HE */
889 if (assoc_data->ie_len) {
890 static const u8 before_he[] = {
891 /*
892 * no need to list the ones split off before VHT
893 * or generated here
894 */
895 WLAN_EID_OPMODE_NOTIF,
896 WLAN_EID_EXTENSION, WLAN_EID_EXT_FUTURE_CHAN_GUIDANCE,
897 /* 11ai elements */
898 WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_SESSION,
899 WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_PUBLIC_KEY,
900 WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_KEY_CONFIRM,
901 WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_HLP_CONTAINER,
902 WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_IP_ADDR_ASSIGN,
903 /* TODO: add 11ah/11aj/11ak elements */
904 };
905
906 /* RIC already taken above, so no need to handle here anymore */
907 noffset = ieee80211_ie_split(assoc_data->ie, assoc_data->ie_len,
908 before_he, ARRAY_SIZE(before_he),
909 offset);
910 pos = skb_put(skb, noffset - offset);
911 memcpy(pos, assoc_data->ie + offset, noffset - offset);
912 offset = noffset;
913 }
914
d545daba 915 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
b08fbbd8
JB
916 ieee80211_add_vht_ie(sdata, skb, sband,
917 &assoc_data->ap_vht_cap);
d545daba 918
41cbb0f5
LC
919 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE))
920 ieee80211_add_he_ie(sdata, skb, sband);
921
922 /* if present, add any custom non-vendor IEs that go after HE */
b3f51e94 923 if (assoc_data->ie_len) {
66e67e41
JB
924 noffset = ieee80211_ie_split_vendor(assoc_data->ie,
925 assoc_data->ie_len,
926 offset);
b952f4df 927 skb_put_data(skb, assoc_data->ie + offset, noffset - offset);
66e67e41
JB
928 offset = noffset;
929 }
930
76f0303d
JB
931 if (assoc_data->wmm) {
932 if (assoc_data->uapsd) {
dc41e4d4
EP
933 qos_info = ifmgd->uapsd_queues;
934 qos_info |= (ifmgd->uapsd_max_sp_len <<
66e67e41
JB
935 IEEE80211_WMM_IE_STA_QOSINFO_SP_SHIFT);
936 } else {
937 qos_info = 0;
938 }
939
40b861a0 940 pos = ieee80211_add_wmm_info_ie(skb_put(skb, 9), qos_info);
66e67e41
JB
941 }
942
943 /* add any remaining custom (i.e. vendor specific here) IEs */
b3f51e94 944 if (assoc_data->ie_len) {
66e67e41 945 noffset = assoc_data->ie_len;
b952f4df 946 skb_put_data(skb, assoc_data->ie + offset, noffset - offset);
66e67e41
JB
947 }
948
39404fee
JM
949 if (assoc_data->fils_kek_len &&
950 fils_encrypt_assoc_req(skb, assoc_data) < 0) {
951 dev_kfree_skb(skb);
952 return;
953 }
954
d4e36e55 955 drv_mgd_prepare_tx(local, sdata, 0);
a1845fc7 956
66e67e41 957 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
30686bf7 958 if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
1672c0e3
JB
959 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS |
960 IEEE80211_TX_INTFL_MLME_CONN_TX;
66e67e41
JB
961 ieee80211_tx_skb(sdata, skb);
962}
963
572e0012
KV
964void ieee80211_send_pspoll(struct ieee80211_local *local,
965 struct ieee80211_sub_if_data *sdata)
966{
572e0012
KV
967 struct ieee80211_pspoll *pspoll;
968 struct sk_buff *skb;
572e0012 969
d8cd189e
KV
970 skb = ieee80211_pspoll_get(&local->hw, &sdata->vif);
971 if (!skb)
572e0012 972 return;
572e0012 973
d8cd189e
KV
974 pspoll = (struct ieee80211_pspoll *) skb->data;
975 pspoll->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
572e0012 976
62ae67be
JB
977 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
978 ieee80211_tx_skb(sdata, skb);
572e0012
KV
979}
980
965bedad
JB
981void ieee80211_send_nullfunc(struct ieee80211_local *local,
982 struct ieee80211_sub_if_data *sdata,
076cdcb1 983 bool powersave)
965bedad
JB
984{
985 struct sk_buff *skb;
d8cd189e 986 struct ieee80211_hdr_3addr *nullfunc;
b6f35301 987 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
965bedad 988
41cbb0f5
LC
989 /* Don't send NDPs when STA is connected HE */
990 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
991 !(ifmgd->flags & IEEE80211_STA_DISABLE_HE))
992 return;
993
7c181f4f
BCD
994 skb = ieee80211_nullfunc_get(&local->hw, &sdata->vif,
995 !ieee80211_hw_check(&local->hw, DOESNT_SUPPORT_QOS_NDP));
d8cd189e 996 if (!skb)
965bedad 997 return;
965bedad 998
d8cd189e 999 nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
965bedad 1000 if (powersave)
d8cd189e 1001 nullfunc->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
965bedad 1002
6c17b77b
SF
1003 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
1004 IEEE80211_TX_INTFL_OFFCHAN_TX_OK;
ff40b425 1005
30686bf7 1006 if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
ff40b425
PF
1007 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
1008
392b9ffb 1009 if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL)
b6f35301
RM
1010 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_USE_MINRATE;
1011
62ae67be 1012 ieee80211_tx_skb(sdata, skb);
965bedad
JB
1013}
1014
d524215f
FF
1015static void ieee80211_send_4addr_nullfunc(struct ieee80211_local *local,
1016 struct ieee80211_sub_if_data *sdata)
1017{
1018 struct sk_buff *skb;
1019 struct ieee80211_hdr *nullfunc;
1020 __le16 fc;
1021
1022 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
1023 return;
1024
41cbb0f5
LC
1025 /* Don't send NDPs when connected HE */
1026 if (!(sdata->u.mgd.flags & IEEE80211_STA_DISABLE_HE))
1027 return;
1028
d524215f 1029 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 30);
d15b8459 1030 if (!skb)
d524215f 1031 return;
d15b8459 1032
d524215f
FF
1033 skb_reserve(skb, local->hw.extra_tx_headroom);
1034
b080db58 1035 nullfunc = skb_put_zero(skb, 30);
d524215f
FF
1036 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
1037 IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
1038 nullfunc->frame_control = fc;
1039 memcpy(nullfunc->addr1, sdata->u.mgd.bssid, ETH_ALEN);
1040 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1041 memcpy(nullfunc->addr3, sdata->u.mgd.bssid, ETH_ALEN);
1042 memcpy(nullfunc->addr4, sdata->vif.addr, ETH_ALEN);
1043
1044 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
1045 ieee80211_tx_skb(sdata, skb);
1046}
1047
cc32abd4
JB
1048/* spectrum management related things */
1049static void ieee80211_chswitch_work(struct work_struct *work)
1050{
1051 struct ieee80211_sub_if_data *sdata =
1052 container_of(work, struct ieee80211_sub_if_data, u.mgd.chswitch_work);
675a0b04 1053 struct ieee80211_local *local = sdata->local;
cc32abd4 1054 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
7578d575 1055 int ret;
cc32abd4 1056
9607e6b6 1057 if (!ieee80211_sdata_running(sdata))
cc32abd4
JB
1058 return;
1059
8d61ffa5 1060 sdata_lock(sdata);
4c3ebc56
MK
1061 mutex_lock(&local->mtx);
1062 mutex_lock(&local->chanctx_mtx);
1063
77fdaa12
JB
1064 if (!ifmgd->associated)
1065 goto out;
cc32abd4 1066
4c3ebc56
MK
1067 if (!sdata->vif.csa_active)
1068 goto out;
1069
1070 /*
1071 * using reservation isn't immediate as it may be deferred until later
1072 * with multi-vif. once reservation is complete it will re-schedule the
1073 * work with no reserved_chanctx so verify chandef to check if it
1074 * completed successfully
1075 */
1076
1077 if (sdata->reserved_chanctx) {
0007e943
IP
1078 struct ieee80211_supported_band *sband = NULL;
1079 struct sta_info *mgd_sta = NULL;
1080 enum ieee80211_sta_rx_bandwidth bw = IEEE80211_STA_RX_BW_20;
1081
4c3ebc56
MK
1082 /*
1083 * with multi-vif csa driver may call ieee80211_csa_finish()
1084 * many times while waiting for other interfaces to use their
1085 * reservations
1086 */
1087 if (sdata->reserved_ready)
1088 goto out;
1089
0007e943
IP
1090 if (sdata->vif.bss_conf.chandef.width !=
1091 sdata->csa_chandef.width) {
1092 /*
1093 * For managed interface, we need to also update the AP
1094 * station bandwidth and align the rate scale algorithm
1095 * on the bandwidth change. Here we only consider the
1096 * bandwidth of the new channel definition (as channel
1097 * switch flow does not have the full HT/VHT/HE
1098 * information), assuming that if additional changes are
1099 * required they would be done as part of the processing
1100 * of the next beacon from the AP.
1101 */
1102 switch (sdata->csa_chandef.width) {
1103 case NL80211_CHAN_WIDTH_20_NOHT:
1104 case NL80211_CHAN_WIDTH_20:
1105 default:
1106 bw = IEEE80211_STA_RX_BW_20;
1107 break;
1108 case NL80211_CHAN_WIDTH_40:
1109 bw = IEEE80211_STA_RX_BW_40;
1110 break;
1111 case NL80211_CHAN_WIDTH_80:
1112 bw = IEEE80211_STA_RX_BW_80;
1113 break;
1114 case NL80211_CHAN_WIDTH_80P80:
1115 case NL80211_CHAN_WIDTH_160:
1116 bw = IEEE80211_STA_RX_BW_160;
1117 break;
1118 }
1119
1120 mgd_sta = sta_info_get(sdata, ifmgd->bssid);
1121 sband =
1122 local->hw.wiphy->bands[sdata->csa_chandef.chan->band];
1123 }
1124
1125 if (sdata->vif.bss_conf.chandef.width >
1126 sdata->csa_chandef.width) {
1127 mgd_sta->sta.bandwidth = bw;
1128 rate_control_rate_update(local, sband, mgd_sta,
1129 IEEE80211_RC_BW_CHANGED);
1130 }
1131
4c3ebc56
MK
1132 ret = ieee80211_vif_use_reserved_context(sdata);
1133 if (ret) {
1134 sdata_info(sdata,
1135 "failed to use reserved channel context, disconnecting (err=%d)\n",
1136 ret);
1137 ieee80211_queue_work(&sdata->local->hw,
1138 &ifmgd->csa_connection_drop_work);
1139 goto out;
1140 }
1141
0007e943
IP
1142 if (sdata->vif.bss_conf.chandef.width <
1143 sdata->csa_chandef.width) {
1144 mgd_sta->sta.bandwidth = bw;
1145 rate_control_rate_update(local, sband, mgd_sta,
1146 IEEE80211_RC_BW_CHANGED);
1147 }
1148
4c3ebc56
MK
1149 goto out;
1150 }
1151
1152 if (!cfg80211_chandef_identical(&sdata->vif.bss_conf.chandef,
1153 &sdata->csa_chandef)) {
7578d575 1154 sdata_info(sdata,
4c3ebc56 1155 "failed to finalize channel switch, disconnecting\n");
7578d575
AN
1156 ieee80211_queue_work(&sdata->local->hw,
1157 &ifmgd->csa_connection_drop_work);
1158 goto out;
1159 }
675a0b04 1160
cc32abd4 1161 /* XXX: shouldn't really modify cfg80211-owned data! */
33787fc4 1162 ifmgd->associated->channel = sdata->csa_chandef.chan;
cc32abd4 1163
0c21e632
LC
1164 ifmgd->csa_waiting_bcn = true;
1165
1166 ieee80211_sta_reset_beacon_monitor(sdata);
1167 ieee80211_sta_reset_conn_monitor(sdata);
1168
1169out:
1170 mutex_unlock(&local->chanctx_mtx);
1171 mutex_unlock(&local->mtx);
1172 sdata_unlock(sdata);
1173}
1174
1175static void ieee80211_chswitch_post_beacon(struct ieee80211_sub_if_data *sdata)
1176{
1177 struct ieee80211_local *local = sdata->local;
1178 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1179 int ret;
1180
1181 sdata_assert_lock(sdata);
1182
1183 WARN_ON(!sdata->vif.csa_active);
4c3ebc56 1184
a46992b4
LC
1185 if (sdata->csa_block_tx) {
1186 ieee80211_wake_vif_queues(local, sdata,
1187 IEEE80211_QUEUE_STOP_REASON_CSA);
1188 sdata->csa_block_tx = false;
1189 }
7578d575 1190
0c21e632
LC
1191 sdata->vif.csa_active = false;
1192 ifmgd->csa_waiting_bcn = false;
1193
f1d65583
LC
1194 ret = drv_post_channel_switch(sdata);
1195 if (ret) {
1196 sdata_info(sdata,
1197 "driver post channel switch failed, disconnecting\n");
1198 ieee80211_queue_work(&local->hw,
1199 &ifmgd->csa_connection_drop_work);
0c21e632 1200 return;
f1d65583 1201 }
688b1ecf
LC
1202
1203 cfg80211_ch_switch_notify(sdata->dev, &sdata->reserved_chandef);
cc32abd4
JB
1204}
1205
5ce6e438
JB
1206void ieee80211_chswitch_done(struct ieee80211_vif *vif, bool success)
1207{
55de908a
JB
1208 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1209 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
5ce6e438
JB
1210
1211 trace_api_chswitch_done(sdata, success);
1212 if (!success) {
882a7c69
JB
1213 sdata_info(sdata,
1214 "driver channel switch failed, disconnecting\n");
1215 ieee80211_queue_work(&sdata->local->hw,
1216 &ifmgd->csa_connection_drop_work);
1217 } else {
1218 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
5ce6e438 1219 }
5ce6e438
JB
1220}
1221EXPORT_SYMBOL(ieee80211_chswitch_done);
1222
34f11cd3 1223static void ieee80211_chswitch_timer(struct timer_list *t)
cc32abd4
JB
1224{
1225 struct ieee80211_sub_if_data *sdata =
34f11cd3 1226 from_timer(sdata, t, u.mgd.chswitch_timer);
5bb644a0 1227
9b7d72c1 1228 ieee80211_queue_work(&sdata->local->hw, &sdata->u.mgd.chswitch_work);
cc32abd4
JB
1229}
1230
37799e52 1231static void
4a3cb702 1232ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
2ba45384
LC
1233 u64 timestamp, u32 device_timestamp,
1234 struct ieee802_11_elems *elems,
04a161f4 1235 bool beacon)
cc32abd4 1236{
b4f286a1 1237 struct ieee80211_local *local = sdata->local;
cc32abd4 1238 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
37799e52 1239 struct cfg80211_bss *cbss = ifmgd->associated;
4c3ebc56 1240 struct ieee80211_chanctx_conf *conf;
55de908a 1241 struct ieee80211_chanctx *chanctx;
57fbcce3 1242 enum nl80211_band current_band;
c0f17eb9 1243 struct ieee80211_csa_ie csa_ie;
6d027bcc 1244 struct ieee80211_channel_switch ch_switch;
e6b7cde4 1245 int res;
cc32abd4 1246
8d61ffa5 1247 sdata_assert_lock(sdata);
77fdaa12 1248
37799e52 1249 if (!cbss)
cc32abd4
JB
1250 return;
1251
b4f286a1 1252 if (local->scanning)
cc32abd4
JB
1253 return;
1254
37799e52 1255 /* disregard subsequent announcements if we are already processing */
3e2a0226 1256 if (sdata->vif.csa_active)
cc32abd4
JB
1257 return;
1258
e6b7cde4 1259 current_band = cbss->channel->band;
84469a45 1260 res = ieee80211_parse_ch_switch_ie(sdata, elems, current_band,
e6b7cde4 1261 ifmgd->flags,
c0f17eb9 1262 ifmgd->associated->bssid, &csa_ie);
e6b7cde4 1263 if (res < 0)
b4f286a1 1264 ieee80211_queue_work(&local->hw,
882a7c69 1265 &ifmgd->csa_connection_drop_work);
e6b7cde4 1266 if (res)
cc32abd4 1267 return;
cd64f2a9 1268
c0f17eb9 1269 if (!cfg80211_chandef_usable(local->hw.wiphy, &csa_ie.chandef,
85220d71
JB
1270 IEEE80211_CHAN_DISABLED)) {
1271 sdata_info(sdata,
1272 "AP %pM switches to unsupported channel (%d MHz, width:%d, CF1/2: %d/%d MHz), disconnecting\n",
e6b7cde4 1273 ifmgd->associated->bssid,
c0f17eb9
CYY
1274 csa_ie.chandef.chan->center_freq,
1275 csa_ie.chandef.width, csa_ie.chandef.center_freq1,
1276 csa_ie.chandef.center_freq2);
85220d71
JB
1277 ieee80211_queue_work(&local->hw,
1278 &ifmgd->csa_connection_drop_work);
1279 return;
1280 }
1281
f84eaa10
JB
1282 if (cfg80211_chandef_identical(&csa_ie.chandef,
1283 &sdata->vif.bss_conf.chandef)) {
1284 if (ifmgd->csa_ignored_same_chan)
1285 return;
1286 sdata_info(sdata,
1287 "AP %pM tries to chanswitch to same channel, ignore\n",
1288 ifmgd->associated->bssid);
1289 ifmgd->csa_ignored_same_chan = true;
1290 return;
1291 }
1292
c5a71688
AN
1293 /*
1294 * Drop all TDLS peers - either we disconnect or move to a different
1295 * channel from this point on. There's no telling what our peer will do.
1296 * The TDLS WIDER_BW scenario is also problematic, as peers might now
1297 * have an incompatible wider chandef.
1298 */
1299 ieee80211_teardown_tdls_peers(sdata);
1300
4c3ebc56 1301 mutex_lock(&local->mtx);
7578d575 1302 mutex_lock(&local->chanctx_mtx);
4c3ebc56
MK
1303 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
1304 lockdep_is_held(&local->chanctx_mtx));
1305 if (!conf) {
1306 sdata_info(sdata,
1307 "no channel context assigned to vif?, disconnecting\n");
f3b0bbb3 1308 goto drop_connection;
4c3ebc56
MK
1309 }
1310
1311 chanctx = container_of(conf, struct ieee80211_chanctx, conf);
1312
0f791eb4 1313 if (local->use_chanctx &&
30686bf7 1314 !ieee80211_hw_check(&local->hw, CHANCTX_STA_CSA)) {
0f791eb4
LC
1315 sdata_info(sdata,
1316 "driver doesn't support chan-switch with channel contexts\n");
f3b0bbb3 1317 goto drop_connection;
55de908a
JB
1318 }
1319
6d027bcc
LC
1320 ch_switch.timestamp = timestamp;
1321 ch_switch.device_timestamp = device_timestamp;
1322 ch_switch.block_tx = csa_ie.mode;
1323 ch_switch.chandef = csa_ie.chandef;
1324 ch_switch.count = csa_ie.count;
1325
1326 if (drv_pre_channel_switch(sdata, &ch_switch)) {
1327 sdata_info(sdata,
1328 "preparing for channel switch failed, disconnecting\n");
f3b0bbb3 1329 goto drop_connection;
6d027bcc
LC
1330 }
1331
4c3ebc56
MK
1332 res = ieee80211_vif_reserve_chanctx(sdata, &csa_ie.chandef,
1333 chanctx->mode, false);
1334 if (res) {
55de908a 1335 sdata_info(sdata,
4c3ebc56
MK
1336 "failed to reserve channel context for channel switch, disconnecting (err=%d)\n",
1337 res);
f3b0bbb3 1338 goto drop_connection;
55de908a 1339 }
b4f286a1 1340 mutex_unlock(&local->chanctx_mtx);
55de908a 1341
c46a73f3 1342 sdata->vif.csa_active = true;
4c3ebc56 1343 sdata->csa_chandef = csa_ie.chandef;
59af6928 1344 sdata->csa_block_tx = csa_ie.mode;
f84eaa10 1345 ifmgd->csa_ignored_same_chan = false;
55de908a 1346
59af6928 1347 if (sdata->csa_block_tx)
a46992b4
LC
1348 ieee80211_stop_vif_queues(local, sdata,
1349 IEEE80211_QUEUE_STOP_REASON_CSA);
59af6928 1350 mutex_unlock(&local->mtx);
57eebdf3 1351
2f457293
LC
1352 cfg80211_ch_switch_started_notify(sdata->dev, &csa_ie.chandef,
1353 csa_ie.count);
1354
b4f286a1 1355 if (local->ops->channel_switch) {
5ce6e438 1356 /* use driver's channel switch callback */
0f791eb4 1357 drv_channel_switch(local, sdata, &ch_switch);
5ce6e438
JB
1358 return;
1359 }
1360
1361 /* channel switch handled in software */
c0f17eb9 1362 if (csa_ie.count <= 1)
b4f286a1 1363 ieee80211_queue_work(&local->hw, &ifmgd->chswitch_work);
57eebdf3 1364 else
cc32abd4 1365 mod_timer(&ifmgd->chswitch_timer,
ff1e417c
LC
1366 TU_TO_EXP_TIME((csa_ie.count - 1) *
1367 cbss->beacon_interval));
f3b0bbb3
JB
1368 return;
1369 drop_connection:
6c18b27d
EG
1370 /*
1371 * This is just so that the disconnect flow will know that
1372 * we were trying to switch channel and failed. In case the
1373 * mode is 1 (we are not allowed to Tx), we will know not to
1374 * send a deauthentication frame. Those two fields will be
1375 * reset when the disconnection worker runs.
1376 */
1377 sdata->vif.csa_active = true;
1378 sdata->csa_block_tx = csa_ie.mode;
1379
f3b0bbb3
JB
1380 ieee80211_queue_work(&local->hw, &ifmgd->csa_connection_drop_work);
1381 mutex_unlock(&local->chanctx_mtx);
1382 mutex_unlock(&local->mtx);
cc32abd4
JB
1383}
1384
24a4e400
SG
1385static bool
1386ieee80211_find_80211h_pwr_constr(struct ieee80211_sub_if_data *sdata,
1387 struct ieee80211_channel *channel,
1388 const u8 *country_ie, u8 country_ie_len,
1389 const u8 *pwr_constr_elem,
1390 int *chan_pwr, int *pwr_reduction)
cc32abd4 1391{
04b7b2ff
JB
1392 struct ieee80211_country_ie_triplet *triplet;
1393 int chan = ieee80211_frequency_to_channel(channel->center_freq);
24a4e400 1394 int i, chan_increment;
04b7b2ff 1395 bool have_chan_pwr = false;
cc32abd4 1396
04b7b2ff
JB
1397 /* Invalid IE */
1398 if (country_ie_len % 2 || country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
24a4e400 1399 return false;
cc32abd4 1400
04b7b2ff
JB
1401 triplet = (void *)(country_ie + 3);
1402 country_ie_len -= 3;
1403
1404 switch (channel->band) {
1405 default:
1406 WARN_ON_ONCE(1);
1407 /* fall through */
57fbcce3
JB
1408 case NL80211_BAND_2GHZ:
1409 case NL80211_BAND_60GHZ:
04b7b2ff
JB
1410 chan_increment = 1;
1411 break;
57fbcce3 1412 case NL80211_BAND_5GHZ:
04b7b2ff
JB
1413 chan_increment = 4;
1414 break;
cc32abd4 1415 }
04b7b2ff
JB
1416
1417 /* find channel */
1418 while (country_ie_len >= 3) {
1419 u8 first_channel = triplet->chans.first_channel;
1420
1421 if (first_channel >= IEEE80211_COUNTRY_EXTENSION_ID)
1422 goto next;
1423
1424 for (i = 0; i < triplet->chans.num_channels; i++) {
1425 if (first_channel + i * chan_increment == chan) {
1426 have_chan_pwr = true;
24a4e400 1427 *chan_pwr = triplet->chans.max_power;
04b7b2ff
JB
1428 break;
1429 }
1430 }
1431 if (have_chan_pwr)
1432 break;
1433
1434 next:
1435 triplet++;
1436 country_ie_len -= 3;
1437 }
1438
a5fee9cb 1439 if (have_chan_pwr && pwr_constr_elem)
24a4e400 1440 *pwr_reduction = *pwr_constr_elem;
a5fee9cb
MB
1441 else
1442 *pwr_reduction = 0;
1443
24a4e400
SG
1444 return have_chan_pwr;
1445}
1446
c8d65917
SG
1447static void ieee80211_find_cisco_dtpc(struct ieee80211_sub_if_data *sdata,
1448 struct ieee80211_channel *channel,
1449 const u8 *cisco_dtpc_ie,
1450 int *pwr_level)
1451{
1452 /* From practical testing, the first data byte of the DTPC element
1453 * seems to contain the requested dBm level, and the CLI on Cisco
1454 * APs clearly state the range is -127 to 127 dBm, which indicates
1455 * a signed byte, although it seemingly never actually goes negative.
1456 * The other byte seems to always be zero.
1457 */
1458 *pwr_level = (__s8)cisco_dtpc_ie[4];
1459}
1460
24a4e400
SG
1461static u32 ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data *sdata,
1462 struct ieee80211_channel *channel,
1463 struct ieee80211_mgmt *mgmt,
1464 const u8 *country_ie, u8 country_ie_len,
c8d65917
SG
1465 const u8 *pwr_constr_ie,
1466 const u8 *cisco_dtpc_ie)
24a4e400 1467{
c8d65917
SG
1468 bool has_80211h_pwr = false, has_cisco_pwr = false;
1469 int chan_pwr = 0, pwr_reduction_80211h = 0;
1470 int pwr_level_cisco, pwr_level_80211h;
24a4e400 1471 int new_ap_level;
a5fee9cb 1472 __le16 capab = mgmt->u.probe_resp.capab_info;
04b7b2ff 1473
a5fee9cb
MB
1474 if (country_ie &&
1475 (capab & cpu_to_le16(WLAN_CAPABILITY_SPECTRUM_MGMT) ||
1476 capab & cpu_to_le16(WLAN_CAPABILITY_RADIO_MEASURE))) {
24a4e400
SG
1477 has_80211h_pwr = ieee80211_find_80211h_pwr_constr(
1478 sdata, channel, country_ie, country_ie_len,
1479 pwr_constr_ie, &chan_pwr, &pwr_reduction_80211h);
c8d65917
SG
1480 pwr_level_80211h =
1481 max_t(int, 0, chan_pwr - pwr_reduction_80211h);
1482 }
1483
1484 if (cisco_dtpc_ie) {
1485 ieee80211_find_cisco_dtpc(
1486 sdata, channel, cisco_dtpc_ie, &pwr_level_cisco);
1487 has_cisco_pwr = true;
24a4e400 1488 }
04b7b2ff 1489
c8d65917 1490 if (!has_80211h_pwr && !has_cisco_pwr)
1ea6f9c0 1491 return 0;
04b7b2ff 1492
c8d65917
SG
1493 /* If we have both 802.11h and Cisco DTPC, apply both limits
1494 * by picking the smallest of the two power levels advertised.
1495 */
1496 if (has_80211h_pwr &&
1497 (!has_cisco_pwr || pwr_level_80211h <= pwr_level_cisco)) {
a87da0cb
JB
1498 new_ap_level = pwr_level_80211h;
1499
1500 if (sdata->ap_power_level == new_ap_level)
1501 return 0;
1502
cef2fc1c
JL
1503 sdata_dbg(sdata,
1504 "Limiting TX power to %d (%d - %d) dBm as advertised by %pM\n",
1505 pwr_level_80211h, chan_pwr, pwr_reduction_80211h,
1506 sdata->u.mgd.bssid);
c8d65917 1507 } else { /* has_cisco_pwr is always true here. */
a87da0cb
JB
1508 new_ap_level = pwr_level_cisco;
1509
1510 if (sdata->ap_power_level == new_ap_level)
1511 return 0;
1512
cef2fc1c
JL
1513 sdata_dbg(sdata,
1514 "Limiting TX power to %d dBm as advertised by %pM\n",
1515 pwr_level_cisco, sdata->u.mgd.bssid);
c8d65917 1516 }
04b7b2ff 1517
1ea6f9c0
JB
1518 sdata->ap_power_level = new_ap_level;
1519 if (__ieee80211_recalc_txpower(sdata))
1520 return BSS_CHANGED_TXPOWER;
1521 return 0;
cc32abd4
JB
1522}
1523
965bedad
JB
1524/* powersave */
1525static void ieee80211_enable_ps(struct ieee80211_local *local,
1526 struct ieee80211_sub_if_data *sdata)
1527{
1528 struct ieee80211_conf *conf = &local->hw.conf;
1529
d5edaedc
JB
1530 /*
1531 * If we are scanning right now then the parameters will
1532 * take effect when scan finishes.
1533 */
fbe9c429 1534 if (local->scanning)
d5edaedc
JB
1535 return;
1536
965bedad 1537 if (conf->dynamic_ps_timeout > 0 &&
30686bf7 1538 !ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS)) {
965bedad
JB
1539 mod_timer(&local->dynamic_ps_timer, jiffies +
1540 msecs_to_jiffies(conf->dynamic_ps_timeout));
1541 } else {
30686bf7 1542 if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK))
076cdcb1 1543 ieee80211_send_nullfunc(local, sdata, true);
375177bf 1544
30686bf7
JB
1545 if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK) &&
1546 ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
2a13052f
JO
1547 return;
1548
1549 conf->flags |= IEEE80211_CONF_PS;
1550 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
965bedad
JB
1551 }
1552}
1553
1554static void ieee80211_change_ps(struct ieee80211_local *local)
1555{
1556 struct ieee80211_conf *conf = &local->hw.conf;
1557
1558 if (local->ps_sdata) {
965bedad
JB
1559 ieee80211_enable_ps(local, local->ps_sdata);
1560 } else if (conf->flags & IEEE80211_CONF_PS) {
1561 conf->flags &= ~IEEE80211_CONF_PS;
1562 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1563 del_timer_sync(&local->dynamic_ps_timer);
1564 cancel_work_sync(&local->dynamic_ps_enable_work);
1565 }
1566}
1567
808118cb
JY
1568static bool ieee80211_powersave_allowed(struct ieee80211_sub_if_data *sdata)
1569{
1570 struct ieee80211_if_managed *mgd = &sdata->u.mgd;
1571 struct sta_info *sta = NULL;
c2c98fde 1572 bool authorized = false;
808118cb
JY
1573
1574 if (!mgd->powersave)
1575 return false;
1576
05cb9108
JB
1577 if (mgd->broken_ap)
1578 return false;
1579
808118cb
JY
1580 if (!mgd->associated)
1581 return false;
1582
392b9ffb 1583 if (mgd->flags & IEEE80211_STA_CONNECTION_POLL)
808118cb
JY
1584 return false;
1585
989c6505 1586 if (!mgd->have_beacon)
ce857888
AB
1587 return false;
1588
808118cb
JY
1589 rcu_read_lock();
1590 sta = sta_info_get(sdata, mgd->bssid);
1591 if (sta)
c2c98fde 1592 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
808118cb
JY
1593 rcu_read_unlock();
1594
c2c98fde 1595 return authorized;
808118cb
JY
1596}
1597
965bedad 1598/* need to hold RTNL or interface lock */
4a733ef1 1599void ieee80211_recalc_ps(struct ieee80211_local *local)
965bedad
JB
1600{
1601 struct ieee80211_sub_if_data *sdata, *found = NULL;
1602 int count = 0;
195e294d 1603 int timeout;
965bedad 1604
30686bf7 1605 if (!ieee80211_hw_check(&local->hw, SUPPORTS_PS)) {
965bedad
JB
1606 local->ps_sdata = NULL;
1607 return;
1608 }
1609
1610 list_for_each_entry(sdata, &local->interfaces, list) {
9607e6b6 1611 if (!ieee80211_sdata_running(sdata))
965bedad 1612 continue;
8c7914de
RM
1613 if (sdata->vif.type == NL80211_IFTYPE_AP) {
1614 /* If an AP vif is found, then disable PS
1615 * by setting the count to zero thereby setting
1616 * ps_sdata to NULL.
1617 */
1618 count = 0;
1619 break;
1620 }
965bedad
JB
1621 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1622 continue;
1623 found = sdata;
1624 count++;
1625 }
1626
808118cb 1627 if (count == 1 && ieee80211_powersave_allowed(found)) {
4a733ef1 1628 u8 dtimper = found->u.mgd.dtim_period;
10f644a4 1629
ff616381 1630 timeout = local->dynamic_ps_forced_timeout;
4a733ef1
JB
1631 if (timeout < 0)
1632 timeout = 100;
09b85568 1633 local->hw.conf.dynamic_ps_timeout = timeout;
195e294d 1634
4a733ef1
JB
1635 /* If the TIM IE is invalid, pretend the value is 1 */
1636 if (!dtimper)
1637 dtimper = 1;
1638
1639 local->hw.conf.ps_dtim_period = dtimper;
1640 local->ps_sdata = found;
10f644a4 1641 } else {
965bedad 1642 local->ps_sdata = NULL;
10f644a4 1643 }
965bedad
JB
1644
1645 ieee80211_change_ps(local);
1646}
1647
ab095877
EP
1648void ieee80211_recalc_ps_vif(struct ieee80211_sub_if_data *sdata)
1649{
1650 bool ps_allowed = ieee80211_powersave_allowed(sdata);
1651
1652 if (sdata->vif.bss_conf.ps != ps_allowed) {
1653 sdata->vif.bss_conf.ps = ps_allowed;
1654 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_PS);
1655 }
1656}
1657
965bedad
JB
1658void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
1659{
1660 struct ieee80211_local *local =
1661 container_of(work, struct ieee80211_local,
1662 dynamic_ps_disable_work);
1663
1664 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1665 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1666 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1667 }
1668
1669 ieee80211_wake_queues_by_reason(&local->hw,
445ea4e8 1670 IEEE80211_MAX_QUEUE_MAP,
cca07b00
LC
1671 IEEE80211_QUEUE_STOP_REASON_PS,
1672 false);
965bedad
JB
1673}
1674
1675void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
1676{
1677 struct ieee80211_local *local =
1678 container_of(work, struct ieee80211_local,
1679 dynamic_ps_enable_work);
1680 struct ieee80211_sub_if_data *sdata = local->ps_sdata;
5e34069c 1681 struct ieee80211_if_managed *ifmgd;
1ddc2867
RM
1682 unsigned long flags;
1683 int q;
965bedad
JB
1684
1685 /* can only happen when PS was just disabled anyway */
1686 if (!sdata)
1687 return;
1688
5e34069c
CL
1689 ifmgd = &sdata->u.mgd;
1690
965bedad
JB
1691 if (local->hw.conf.flags & IEEE80211_CONF_PS)
1692 return;
1693
09b85568 1694 if (local->hw.conf.dynamic_ps_timeout > 0) {
77b7023a
AN
1695 /* don't enter PS if TX frames are pending */
1696 if (drv_tx_frames_pending(local)) {
1ddc2867
RM
1697 mod_timer(&local->dynamic_ps_timer, jiffies +
1698 msecs_to_jiffies(
1699 local->hw.conf.dynamic_ps_timeout));
1700 return;
1701 }
77b7023a
AN
1702
1703 /*
1704 * transmission can be stopped by others which leads to
1705 * dynamic_ps_timer expiry. Postpone the ps timer if it
1706 * is not the actual idle state.
1707 */
1708 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
1709 for (q = 0; q < local->hw.queues; q++) {
1710 if (local->queue_stop_reasons[q]) {
1711 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1712 flags);
1713 mod_timer(&local->dynamic_ps_timer, jiffies +
1714 msecs_to_jiffies(
1715 local->hw.conf.dynamic_ps_timeout));
1716 return;
1717 }
1718 }
1719 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
1ddc2867 1720 }
1ddc2867 1721
30686bf7 1722 if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK) &&
9c38a8b4 1723 !(ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
a1598383 1724 if (drv_tx_frames_pending(local)) {
e8306f98
VN
1725 mod_timer(&local->dynamic_ps_timer, jiffies +
1726 msecs_to_jiffies(
1727 local->hw.conf.dynamic_ps_timeout));
a1598383 1728 } else {
076cdcb1 1729 ieee80211_send_nullfunc(local, sdata, true);
e8306f98 1730 /* Flush to get the tx status of nullfunc frame */
3b24f4c6 1731 ieee80211_flush_queues(local, sdata, false);
e8306f98 1732 }
f3e85b9e
VN
1733 }
1734
30686bf7
JB
1735 if (!(ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS) &&
1736 ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK)) ||
375177bf
VN
1737 (ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
1738 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
1739 local->hw.conf.flags |= IEEE80211_CONF_PS;
1740 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1741 }
965bedad
JB
1742}
1743
34f11cd3 1744void ieee80211_dynamic_ps_timer(struct timer_list *t)
965bedad 1745{
34f11cd3 1746 struct ieee80211_local *local = from_timer(local, t, dynamic_ps_timer);
965bedad 1747
42935eca 1748 ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
965bedad
JB
1749}
1750
164eb02d
SW
1751void ieee80211_dfs_cac_timer_work(struct work_struct *work)
1752{
a85a7e28 1753 struct delayed_work *delayed_work = to_delayed_work(work);
164eb02d
SW
1754 struct ieee80211_sub_if_data *sdata =
1755 container_of(delayed_work, struct ieee80211_sub_if_data,
1756 dfs_cac_timer_work);
d2859df5 1757 struct cfg80211_chan_def chandef = sdata->vif.bss_conf.chandef;
164eb02d 1758
34a3740d
JB
1759 mutex_lock(&sdata->local->mtx);
1760 if (sdata->wdev.cac_started) {
1761 ieee80211_vif_release_channel(sdata);
1762 cfg80211_cac_event(sdata->dev, &chandef,
1763 NL80211_RADAR_CAC_FINISHED,
1764 GFP_KERNEL);
1765 }
1766 mutex_unlock(&sdata->local->mtx);
164eb02d
SW
1767}
1768
02219b3a
JB
1769static bool
1770__ieee80211_sta_handle_tspec_ac_params(struct ieee80211_sub_if_data *sdata)
1771{
1772 struct ieee80211_local *local = sdata->local;
1773 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
cc72f6e2 1774 bool ret = false;
02219b3a
JB
1775 int ac;
1776
1777 if (local->hw.queues < IEEE80211_NUM_ACS)
1778 return false;
1779
1780 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1781 struct ieee80211_sta_tx_tspec *tx_tspec = &ifmgd->tx_tspec[ac];
1782 int non_acm_ac;
1783 unsigned long now = jiffies;
1784
1785 if (tx_tspec->action == TX_TSPEC_ACTION_NONE &&
1786 tx_tspec->admitted_time &&
1787 time_after(now, tx_tspec->time_slice_start + HZ)) {
1788 tx_tspec->consumed_tx_time = 0;
1789 tx_tspec->time_slice_start = now;
1790
1791 if (tx_tspec->downgraded)
1792 tx_tspec->action =
1793 TX_TSPEC_ACTION_STOP_DOWNGRADE;
1794 }
1795
1796 switch (tx_tspec->action) {
1797 case TX_TSPEC_ACTION_STOP_DOWNGRADE:
1798 /* take the original parameters */
1799 if (drv_conf_tx(local, sdata, ac, &sdata->tx_conf[ac]))
1800 sdata_err(sdata,
1801 "failed to set TX queue parameters for queue %d\n",
1802 ac);
1803 tx_tspec->action = TX_TSPEC_ACTION_NONE;
1804 tx_tspec->downgraded = false;
1805 ret = true;
1806 break;
1807 case TX_TSPEC_ACTION_DOWNGRADE:
1808 if (time_after(now, tx_tspec->time_slice_start + HZ)) {
1809 tx_tspec->action = TX_TSPEC_ACTION_NONE;
1810 ret = true;
1811 break;
1812 }
1813 /* downgrade next lower non-ACM AC */
1814 for (non_acm_ac = ac + 1;
1815 non_acm_ac < IEEE80211_NUM_ACS;
1816 non_acm_ac++)
1817 if (!(sdata->wmm_acm & BIT(7 - 2 * non_acm_ac)))
1818 break;
93db1d9e
JB
1819 /* Usually the loop will result in using BK even if it
1820 * requires admission control, but such a configuration
1821 * makes no sense and we have to transmit somehow - the
1822 * AC selection does the same thing.
1823 * If we started out trying to downgrade from BK, then
1824 * the extra condition here might be needed.
02219b3a 1825 */
93db1d9e
JB
1826 if (non_acm_ac >= IEEE80211_NUM_ACS)
1827 non_acm_ac = IEEE80211_AC_BK;
02219b3a
JB
1828 if (drv_conf_tx(local, sdata, ac,
1829 &sdata->tx_conf[non_acm_ac]))
1830 sdata_err(sdata,
1831 "failed to set TX queue parameters for queue %d\n",
1832 ac);
1833 tx_tspec->action = TX_TSPEC_ACTION_NONE;
1834 ret = true;
1835 schedule_delayed_work(&ifmgd->tx_tspec_wk,
1836 tx_tspec->time_slice_start + HZ - now + 1);
1837 break;
1838 case TX_TSPEC_ACTION_NONE:
1839 /* nothing now */
1840 break;
1841 }
1842 }
1843
1844 return ret;
1845}
1846
1847void ieee80211_sta_handle_tspec_ac_params(struct ieee80211_sub_if_data *sdata)
1848{
1849 if (__ieee80211_sta_handle_tspec_ac_params(sdata))
1850 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_QOS);
1851}
1852
1853static void ieee80211_sta_handle_tspec_ac_params_wk(struct work_struct *work)
1854{
1855 struct ieee80211_sub_if_data *sdata;
1856
1857 sdata = container_of(work, struct ieee80211_sub_if_data,
1858 u.mgd.tx_tspec_wk.work);
1859 ieee80211_sta_handle_tspec_ac_params(sdata);
1860}
1861
60f8b39c 1862/* MLME */
41cbb0f5
LC
1863static bool
1864ieee80211_sta_wmm_params(struct ieee80211_local *local,
1865 struct ieee80211_sub_if_data *sdata,
1866 const u8 *wmm_param, size_t wmm_param_len,
1867 const struct ieee80211_mu_edca_param_set *mu_edca)
f0706e82 1868{
2ed77ea6 1869 struct ieee80211_tx_queue_params params[IEEE80211_NUM_ACS];
4ced3f74 1870 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
f0706e82 1871 size_t left;
2e249fc3 1872 int count, mu_edca_count, ac;
4a3cb702
JB
1873 const u8 *pos;
1874 u8 uapsd_queues = 0;
f0706e82 1875
e1b3ec1a 1876 if (!local->ops->conf_tx)
7d25745d 1877 return false;
e1b3ec1a 1878
32c5057b 1879 if (local->hw.queues < IEEE80211_NUM_ACS)
7d25745d 1880 return false;
3434fbd3
JB
1881
1882 if (!wmm_param)
7d25745d 1883 return false;
3434fbd3 1884
f0706e82 1885 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
7d25745d 1886 return false;
ab13315a
KV
1887
1888 if (ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED)
dc41e4d4 1889 uapsd_queues = ifmgd->uapsd_queues;
ab13315a 1890
f0706e82 1891 count = wmm_param[6] & 0x0f;
2e249fc3
ST
1892 /* -1 is the initial value of ifmgd->mu_edca_last_param_set.
1893 * if mu_edca was preset before and now it disappeared tell
1894 * the driver about it.
1895 */
1896 mu_edca_count = mu_edca ? mu_edca->mu_qos_info & 0x0f : -1;
1897 if (count == ifmgd->wmm_last_param_set &&
1898 mu_edca_count == ifmgd->mu_edca_last_param_set)
7d25745d 1899 return false;
46900298 1900 ifmgd->wmm_last_param_set = count;
2e249fc3 1901 ifmgd->mu_edca_last_param_set = mu_edca_count;
f0706e82
JB
1902
1903 pos = wmm_param + 8;
1904 left = wmm_param_len - 8;
1905
1906 memset(&params, 0, sizeof(params));
1907
00e96dec 1908 sdata->wmm_acm = 0;
f0706e82
JB
1909 for (; left >= 4; left -= 4, pos += 4) {
1910 int aci = (pos[0] >> 5) & 0x03;
1911 int acm = (pos[0] >> 4) & 0x01;
ab13315a 1912 bool uapsd = false;
f0706e82
JB
1913
1914 switch (aci) {
0eeb59fe 1915 case 1: /* AC_BK */
2ed77ea6 1916 ac = IEEE80211_AC_BK;
988c0f72 1917 if (acm)
00e96dec 1918 sdata->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
ab13315a
KV
1919 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
1920 uapsd = true;
41cbb0f5
LC
1921 params[ac].mu_edca = !!mu_edca;
1922 if (mu_edca)
1923 params[ac].mu_edca_param_rec = mu_edca->ac_bk;
f0706e82 1924 break;
0eeb59fe 1925 case 2: /* AC_VI */
2ed77ea6 1926 ac = IEEE80211_AC_VI;
988c0f72 1927 if (acm)
00e96dec 1928 sdata->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
ab13315a
KV
1929 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
1930 uapsd = true;
41cbb0f5
LC
1931 params[ac].mu_edca = !!mu_edca;
1932 if (mu_edca)
1933 params[ac].mu_edca_param_rec = mu_edca->ac_vi;
f0706e82 1934 break;
0eeb59fe 1935 case 3: /* AC_VO */
2ed77ea6 1936 ac = IEEE80211_AC_VO;
988c0f72 1937 if (acm)
00e96dec 1938 sdata->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
ab13315a
KV
1939 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
1940 uapsd = true;
41cbb0f5
LC
1941 params[ac].mu_edca = !!mu_edca;
1942 if (mu_edca)
1943 params[ac].mu_edca_param_rec = mu_edca->ac_vo;
f0706e82 1944 break;
0eeb59fe 1945 case 0: /* AC_BE */
f0706e82 1946 default:
2ed77ea6 1947 ac = IEEE80211_AC_BE;
988c0f72 1948 if (acm)
00e96dec 1949 sdata->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
ab13315a
KV
1950 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
1951 uapsd = true;
41cbb0f5
LC
1952 params[ac].mu_edca = !!mu_edca;
1953 if (mu_edca)
1954 params[ac].mu_edca_param_rec = mu_edca->ac_be;
f0706e82
JB
1955 break;
1956 }
1957
2ed77ea6 1958 params[ac].aifs = pos[0] & 0x0f;
730a7550 1959
2ed77ea6 1960 if (params[ac].aifs < 2) {
730a7550
EG
1961 sdata_info(sdata,
1962 "AP has invalid WMM params (AIFSN=%d for ACI %d), will use 2\n",
2ed77ea6
JB
1963 params[ac].aifs, aci);
1964 params[ac].aifs = 2;
730a7550 1965 }
2ed77ea6
JB
1966 params[ac].cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
1967 params[ac].cw_min = ecw2cw(pos[1] & 0x0f);
1968 params[ac].txop = get_unaligned_le16(pos + 2);
1969 params[ac].acm = acm;
1970 params[ac].uapsd = uapsd;
ab13315a 1971
911a2648 1972 if (params[ac].cw_min == 0 ||
c470bdc1 1973 params[ac].cw_min > params[ac].cw_max) {
2ed77ea6
JB
1974 sdata_info(sdata,
1975 "AP has invalid WMM params (CWmin/max=%d/%d for ACI %d), using defaults\n",
1976 params[ac].cw_min, params[ac].cw_max, aci);
1977 return false;
1978 }
e552af05 1979 ieee80211_regulatory_limit_wmm_params(sdata, &params[ac], ac);
2ed77ea6
JB
1980 }
1981
1982 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
bdcbd8e0 1983 mlme_dbg(sdata,
2ed77ea6
JB
1984 "WMM AC=%d acm=%d aifs=%d cWmin=%d cWmax=%d txop=%d uapsd=%d, downgraded=%d\n",
1985 ac, params[ac].acm,
1986 params[ac].aifs, params[ac].cw_min, params[ac].cw_max,
1987 params[ac].txop, params[ac].uapsd,
1988 ifmgd->tx_tspec[ac].downgraded);
1989 sdata->tx_conf[ac] = params[ac];
1990 if (!ifmgd->tx_tspec[ac].downgraded &&
1991 drv_conf_tx(local, sdata, ac, &params[ac]))
bdcbd8e0 1992 sdata_err(sdata,
2ed77ea6
JB
1993 "failed to set TX queue parameters for AC %d\n",
1994 ac);
f0706e82 1995 }
e1b3ec1a
SG
1996
1997 /* enable WMM or activate new settings */
4ced3f74 1998 sdata->vif.bss_conf.qos = true;
7d25745d 1999 return true;
f0706e82
JB
2000}
2001
925e64c3
SG
2002static void __ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
2003{
2004 lockdep_assert_held(&sdata->local->mtx);
2005
392b9ffb 2006 sdata->u.mgd.flags &= ~IEEE80211_STA_CONNECTION_POLL;
925e64c3
SG
2007 ieee80211_run_deferred_scan(sdata->local);
2008}
2009
2010static void ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
2011{
2012 mutex_lock(&sdata->local->mtx);
2013 __ieee80211_stop_poll(sdata);
2014 mutex_unlock(&sdata->local->mtx);
2015}
2016
7a5158ef
JB
2017static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
2018 u16 capab, bool erp_valid, u8 erp)
5628221c 2019{
bda3933a 2020 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
21a8e9dd 2021 struct ieee80211_supported_band *sband;
471b3efd 2022 u32 changed = 0;
7a5158ef
JB
2023 bool use_protection;
2024 bool use_short_preamble;
2025 bool use_short_slot;
2026
21a8e9dd
MSS
2027 sband = ieee80211_get_sband(sdata);
2028 if (!sband)
2029 return changed;
2030
7a5158ef
JB
2031 if (erp_valid) {
2032 use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
2033 use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
2034 } else {
2035 use_protection = false;
2036 use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
2037 }
2038
2039 use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
21a8e9dd 2040 if (sband->band == NL80211_BAND_5GHZ)
43d35343 2041 use_short_slot = true;
5628221c 2042
471b3efd 2043 if (use_protection != bss_conf->use_cts_prot) {
471b3efd
JB
2044 bss_conf->use_cts_prot = use_protection;
2045 changed |= BSS_CHANGED_ERP_CTS_PROT;
5628221c 2046 }
7e9ed188 2047
d43c7b37 2048 if (use_short_preamble != bss_conf->use_short_preamble) {
d43c7b37 2049 bss_conf->use_short_preamble = use_short_preamble;
471b3efd 2050 changed |= BSS_CHANGED_ERP_PREAMBLE;
7e9ed188 2051 }
d9430a32 2052
7a5158ef 2053 if (use_short_slot != bss_conf->use_short_slot) {
7a5158ef
JB
2054 bss_conf->use_short_slot = use_short_slot;
2055 changed |= BSS_CHANGED_ERP_SLOT;
50c4afb9
JL
2056 }
2057
2058 return changed;
2059}
2060
f698d856 2061static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
0c1ad2ca 2062 struct cfg80211_bss *cbss,
ae5eb026 2063 u32 bss_info_changed)
f0706e82 2064{
0c1ad2ca 2065 struct ieee80211_bss *bss = (void *)cbss->priv;
471b3efd 2066 struct ieee80211_local *local = sdata->local;
68542962 2067 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
d6f2da5b 2068
ae5eb026 2069 bss_info_changed |= BSS_CHANGED_ASSOC;
77fdaa12 2070 bss_info_changed |= ieee80211_handle_bss_capability(sdata,
50ae34a2 2071 bss_conf->assoc_capability, bss->has_erp_value, bss->erp_value);
21c0cbe7 2072
7ccc8bd7 2073 sdata->u.mgd.beacon_timeout = usecs_to_jiffies(ieee80211_tu_to_usec(
59c1ec2b 2074 beacon_loss_count * bss_conf->beacon_int));
7ccc8bd7 2075
0c1ad2ca
JB
2076 sdata->u.mgd.associated = cbss;
2077 memcpy(sdata->u.mgd.bssid, cbss->bssid, ETH_ALEN);
f0706e82 2078
e8e4f528
JB
2079 ieee80211_check_rate_mask(sdata);
2080
17e4ec14
JM
2081 sdata->u.mgd.flags |= IEEE80211_STA_RESET_SIGNAL_AVE;
2082
b115b972
JD
2083 if (sdata->vif.p2p ||
2084 sdata->vif.driver_flags & IEEE80211_VIF_GET_NOA_UPDATE) {
9caf0364 2085 const struct cfg80211_bss_ies *ies;
488dd7b5 2086
9caf0364
JB
2087 rcu_read_lock();
2088 ies = rcu_dereference(cbss->ies);
2089 if (ies) {
9caf0364
JB
2090 int ret;
2091
2092 ret = cfg80211_get_p2p_attr(
2093 ies->data, ies->len,
2094 IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
67baf663
JD
2095 (u8 *) &bss_conf->p2p_noa_attr,
2096 sizeof(bss_conf->p2p_noa_attr));
9caf0364 2097 if (ret >= 2) {
67baf663
JD
2098 sdata->u.mgd.p2p_noa_index =
2099 bss_conf->p2p_noa_attr.index;
9caf0364 2100 bss_info_changed |= BSS_CHANGED_P2P_PS;
9caf0364 2101 }
488dd7b5 2102 }
9caf0364 2103 rcu_read_unlock();
488dd7b5
JB
2104 }
2105
b291ba11 2106 /* just to be sure */
925e64c3 2107 ieee80211_stop_poll(sdata);
b291ba11 2108
9ac19a90 2109 ieee80211_led_assoc(local, 1);
9306102e 2110
989c6505 2111 if (sdata->u.mgd.have_beacon) {
826262c3
JB
2112 /*
2113 * If the AP is buggy we may get here with no DTIM period
2114 * known, so assume it's 1 which is the only safe assumption
2115 * in that case, although if the TIM IE is broken powersave
2116 * probably just won't work at all.
2117 */
2118 bss_conf->dtim_period = sdata->u.mgd.dtim_period ?: 1;
817cee76 2119 bss_conf->beacon_rate = bss->beacon_rate;
989c6505 2120 bss_info_changed |= BSS_CHANGED_BEACON_INFO;
826262c3 2121 } else {
817cee76 2122 bss_conf->beacon_rate = NULL;
e5b900d2 2123 bss_conf->dtim_period = 0;
826262c3 2124 }
e5b900d2 2125
68542962 2126 bss_conf->assoc = 1;
9cef8737 2127
a97c13c3 2128 /* Tell the driver to monitor connection quality (if supported) */
ea086359 2129 if (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI &&
68542962 2130 bss_conf->cqm_rssi_thold)
a97c13c3
JO
2131 bss_info_changed |= BSS_CHANGED_CQM;
2132
68542962 2133 /* Enable ARP filtering */
0f19b41e 2134 if (bss_conf->arp_addr_cnt)
68542962 2135 bss_info_changed |= BSS_CHANGED_ARP_FILTER;
68542962 2136
ae5eb026 2137 ieee80211_bss_info_change_notify(sdata, bss_info_changed);
f0706e82 2138
056508dc 2139 mutex_lock(&local->iflist_mtx);
4a733ef1 2140 ieee80211_recalc_ps(local);
056508dc 2141 mutex_unlock(&local->iflist_mtx);
e0cb686f 2142
04ecd257 2143 ieee80211_recalc_smps(sdata);
ab095877
EP
2144 ieee80211_recalc_ps_vif(sdata);
2145
9ac19a90 2146 netif_carrier_on(sdata->dev);
f0706e82
JB
2147}
2148
e69e95db 2149static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
37ad3888
JB
2150 u16 stype, u16 reason, bool tx,
2151 u8 *frame_buf)
aa458d17 2152{
46900298 2153 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
aa458d17 2154 struct ieee80211_local *local = sdata->local;
3cc5240b 2155 u32 changed = 0;
aa458d17 2156
8d61ffa5 2157 sdata_assert_lock(sdata);
77fdaa12 2158
37ad3888
JB
2159 if (WARN_ON_ONCE(tx && !frame_buf))
2160 return;
2161
b291ba11
JB
2162 if (WARN_ON(!ifmgd->associated))
2163 return;
2164
79543d8e
DS
2165 ieee80211_stop_poll(sdata);
2166
77fdaa12 2167 ifmgd->associated = NULL;
aa458d17
TW
2168 netif_carrier_off(sdata->dev);
2169
88bc40e8
EP
2170 /*
2171 * if we want to get out of ps before disassoc (why?) we have
2172 * to do it before sending disassoc, as otherwise the null-packet
2173 * won't be valid.
2174 */
2175 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
2176 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
2177 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
2178 }
2179 local->ps_sdata = NULL;
2180
ab095877
EP
2181 /* disable per-vif ps */
2182 ieee80211_recalc_ps_vif(sdata);
2183
14f2ae83
EG
2184 /* make sure ongoing transmission finishes */
2185 synchronize_net();
2186
3b24f4c6
EG
2187 /*
2188 * drop any frame before deauth/disassoc, this can be data or
2189 * management frame. Since we are disconnecting, we should not
2190 * insist sending these frames which can take time and delay
2191 * the disconnection and possible the roaming.
2192 */
f823981e 2193 if (tx)
3b24f4c6 2194 ieee80211_flush_queues(local, sdata, true);
f823981e 2195
37ad3888 2196 /* deauthenticate/disassociate now */
94ba9271 2197 if (tx || frame_buf) {
94ba9271
IP
2198 /*
2199 * In multi channel scenarios guarantee that the virtual
2200 * interface is granted immediate airtime to transmit the
2201 * deauthentication frame by calling mgd_prepare_tx, if the
2202 * driver requested so.
2203 */
2204 if (ieee80211_hw_check(&local->hw, DEAUTH_NEED_MGD_TX_PREP) &&
2205 !ifmgd->have_beacon)
d4e36e55 2206 drv_mgd_prepare_tx(sdata->local, sdata, 0);
94ba9271 2207
88a9e31c
EP
2208 ieee80211_send_deauth_disassoc(sdata, ifmgd->bssid, stype,
2209 reason, tx, frame_buf);
94ba9271 2210 }
37ad3888 2211
3b24f4c6 2212 /* flush out frame - make sure the deauth was actually sent */
37ad3888 2213 if (tx)
3b24f4c6 2214 ieee80211_flush_queues(local, sdata, false);
37ad3888 2215
88a9e31c 2216 /* clear bssid only after building the needed mgmt frames */
c84a67a2 2217 eth_zero_addr(ifmgd->bssid);
88a9e31c 2218
37ad3888 2219 /* remove AP and TDLS peers */
d34ba216 2220 sta_info_flush(sdata);
37ad3888
JB
2221
2222 /* finally reset all BSS / config parameters */
f5e5bf25
TW
2223 changed |= ieee80211_reset_erp_info(sdata);
2224
f5e5bf25 2225 ieee80211_led_assoc(local, 0);
ae5eb026
JB
2226 changed |= BSS_CHANGED_ASSOC;
2227 sdata->vif.bss_conf.assoc = false;
f5e5bf25 2228
67baf663
JD
2229 ifmgd->p2p_noa_index = -1;
2230 memset(&sdata->vif.bss_conf.p2p_noa_attr, 0,
2231 sizeof(sdata->vif.bss_conf.p2p_noa_attr));
488dd7b5 2232
dd5ecfea 2233 /* on the next assoc, re-program HT/VHT parameters */
ef96a842
BG
2234 memset(&ifmgd->ht_capa, 0, sizeof(ifmgd->ht_capa));
2235 memset(&ifmgd->ht_capa_mask, 0, sizeof(ifmgd->ht_capa_mask));
dd5ecfea
JB
2236 memset(&ifmgd->vht_capa, 0, sizeof(ifmgd->vht_capa));
2237 memset(&ifmgd->vht_capa_mask, 0, sizeof(ifmgd->vht_capa_mask));
23a1f8d4
SS
2238
2239 /* reset MU-MIMO ownership and group data */
2240 memset(sdata->vif.bss_conf.mu_group.membership, 0,
2241 sizeof(sdata->vif.bss_conf.mu_group.membership));
2242 memset(sdata->vif.bss_conf.mu_group.position, 0,
2243 sizeof(sdata->vif.bss_conf.mu_group.position));
2244 changed |= BSS_CHANGED_MU_GROUPS;
b5a33d52 2245 sdata->vif.mu_mimo_owner = false;
413ad50a 2246
1ea6f9c0 2247 sdata->ap_power_level = IEEE80211_UNSET_POWER_LEVEL;
a8302de9 2248
520eb820
KV
2249 del_timer_sync(&local->dynamic_ps_timer);
2250 cancel_work_sync(&local->dynamic_ps_enable_work);
2251
68542962 2252 /* Disable ARP filtering */
0f19b41e 2253 if (sdata->vif.bss_conf.arp_addr_cnt)
68542962 2254 changed |= BSS_CHANGED_ARP_FILTER;
68542962 2255
3abead59
JB
2256 sdata->vif.bss_conf.qos = false;
2257 changed |= BSS_CHANGED_QOS;
2258
0aaffa9b
JB
2259 /* The BSSID (not really interesting) and HT changed */
2260 changed |= BSS_CHANGED_BSSID | BSS_CHANGED_HT;
ae5eb026 2261 ieee80211_bss_info_change_notify(sdata, changed);
8e268e47 2262
3abead59 2263 /* disassociated - set to defaults now */
cec66283 2264 ieee80211_set_wmm_default(sdata, false, false);
3abead59 2265
b9dcf712
JB
2266 del_timer_sync(&sdata->u.mgd.conn_mon_timer);
2267 del_timer_sync(&sdata->u.mgd.bcn_mon_timer);
2268 del_timer_sync(&sdata->u.mgd.timer);
2269 del_timer_sync(&sdata->u.mgd.chswitch_timer);
2d9957cc 2270
826262c3 2271 sdata->vif.bss_conf.dtim_period = 0;
817cee76
AB
2272 sdata->vif.bss_conf.beacon_rate = NULL;
2273
989c6505 2274 ifmgd->have_beacon = false;
826262c3 2275
028e8da0 2276 ifmgd->flags = 0;
34a3740d 2277 mutex_lock(&local->mtx);
028e8da0 2278 ieee80211_vif_release_channel(sdata);
59af6928
MK
2279
2280 sdata->vif.csa_active = false;
0c21e632 2281 ifmgd->csa_waiting_bcn = false;
f84eaa10 2282 ifmgd->csa_ignored_same_chan = false;
a46992b4
LC
2283 if (sdata->csa_block_tx) {
2284 ieee80211_wake_vif_queues(local, sdata,
2285 IEEE80211_QUEUE_STOP_REASON_CSA);
2286 sdata->csa_block_tx = false;
2287 }
34a3740d 2288 mutex_unlock(&local->mtx);
2475b1cc 2289
02219b3a
JB
2290 /* existing TX TSPEC sessions no longer exist */
2291 memset(ifmgd->tx_tspec, 0, sizeof(ifmgd->tx_tspec));
2292 cancel_delayed_work_sync(&ifmgd->tx_tspec_wk);
2293
2475b1cc 2294 sdata->encrypt_headroom = IEEE80211_ENCRYPT_HEADROOM;
aa458d17 2295}
f0706e82 2296
3cf335d5
KV
2297void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
2298 struct ieee80211_hdr *hdr)
2299{
2300 /*
2301 * We can postpone the mgd.timer whenever receiving unicast frames
2302 * from AP because we know that the connection is working both ways
2303 * at that time. But multicast frames (and hence also beacons) must
2304 * be ignored here, because we need to trigger the timer during
b291ba11
JB
2305 * data idle periods for sending the periodic probe request to the
2306 * AP we're connected to.
3cf335d5 2307 */
b291ba11
JB
2308 if (is_multicast_ether_addr(hdr->addr1))
2309 return;
2310
be099e82 2311 ieee80211_sta_reset_conn_monitor(sdata);
3cf335d5 2312}
f0706e82 2313
4e5ff376
FF
2314static void ieee80211_reset_ap_probe(struct ieee80211_sub_if_data *sdata)
2315{
2316 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
133d40f9 2317 struct ieee80211_local *local = sdata->local;
4e5ff376 2318
133d40f9 2319 mutex_lock(&local->mtx);
392b9ffb
SG
2320 if (!(ifmgd->flags & IEEE80211_STA_CONNECTION_POLL))
2321 goto out;
4e5ff376 2322
925e64c3 2323 __ieee80211_stop_poll(sdata);
133d40f9
SG
2324
2325 mutex_lock(&local->iflist_mtx);
4a733ef1 2326 ieee80211_recalc_ps(local);
133d40f9 2327 mutex_unlock(&local->iflist_mtx);
4e5ff376 2328
30686bf7 2329 if (ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
133d40f9 2330 goto out;
4e5ff376
FF
2331
2332 /*
2333 * We've received a probe response, but are not sure whether
2334 * we have or will be receiving any beacons or data, so let's
2335 * schedule the timers again, just in case.
2336 */
2337 ieee80211_sta_reset_beacon_monitor(sdata);
2338
2339 mod_timer(&ifmgd->conn_mon_timer,
2340 round_jiffies_up(jiffies +
2341 IEEE80211_CONNECTION_IDLE_TIME));
133d40f9 2342out:
133d40f9 2343 mutex_unlock(&local->mtx);
4e5ff376
FF
2344}
2345
02219b3a
JB
2346static void ieee80211_sta_tx_wmm_ac_notify(struct ieee80211_sub_if_data *sdata,
2347 struct ieee80211_hdr *hdr,
2348 u16 tx_time)
2349{
2350 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
a1f2ba04 2351 u16 tid = ieee80211_get_tid(hdr);
02219b3a
JB
2352 int ac = ieee80211_ac_from_tid(tid);
2353 struct ieee80211_sta_tx_tspec *tx_tspec = &ifmgd->tx_tspec[ac];
2354 unsigned long now = jiffies;
2355
2356 if (likely(!tx_tspec->admitted_time))
2357 return;
2358
2359 if (time_after(now, tx_tspec->time_slice_start + HZ)) {
2360 tx_tspec->consumed_tx_time = 0;
2361 tx_tspec->time_slice_start = now;
2362
2363 if (tx_tspec->downgraded) {
2364 tx_tspec->action = TX_TSPEC_ACTION_STOP_DOWNGRADE;
2365 schedule_delayed_work(&ifmgd->tx_tspec_wk, 0);
2366 }
2367 }
2368
2369 if (tx_tspec->downgraded)
2370 return;
2371
2372 tx_tspec->consumed_tx_time += tx_time;
2373
2374 if (tx_tspec->consumed_tx_time >= tx_tspec->admitted_time) {
2375 tx_tspec->downgraded = true;
2376 tx_tspec->action = TX_TSPEC_ACTION_DOWNGRADE;
2377 schedule_delayed_work(&ifmgd->tx_tspec_wk, 0);
2378 }
2379}
2380
4e5ff376 2381void ieee80211_sta_tx_notify(struct ieee80211_sub_if_data *sdata,
02219b3a 2382 struct ieee80211_hdr *hdr, bool ack, u16 tx_time)
4e5ff376 2383{
02219b3a
JB
2384 ieee80211_sta_tx_wmm_ac_notify(sdata, hdr, tx_time);
2385
75706d0e 2386 if (!ieee80211_is_data(hdr->frame_control))
4e5ff376
FF
2387 return;
2388
4e5ff376
FF
2389 if (ieee80211_is_nullfunc(hdr->frame_control) &&
2390 sdata->u.mgd.probe_send_count > 0) {
04ac3c0e 2391 if (ack)
cab1c7fd 2392 ieee80211_sta_reset_conn_monitor(sdata);
04ac3c0e
FF
2393 else
2394 sdata->u.mgd.nullfunc_failed = true;
4e5ff376 2395 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
cab1c7fd 2396 return;
4e5ff376 2397 }
cab1c7fd
WD
2398
2399 if (ack)
2400 ieee80211_sta_reset_conn_monitor(sdata);
4e5ff376
FF
2401}
2402
45ad6834
JB
2403static void ieee80211_mlme_send_probe_req(struct ieee80211_sub_if_data *sdata,
2404 const u8 *src, const u8 *dst,
2405 const u8 *ssid, size_t ssid_len,
2406 struct ieee80211_channel *channel)
2407{
2408 struct sk_buff *skb;
2409
2410 skb = ieee80211_build_probe_req(sdata, src, dst, (u32)-1, channel,
2411 ssid, ssid_len, NULL, 0,
2412 IEEE80211_PROBE_FLAG_DIRECTED);
2413 if (skb)
2414 ieee80211_tx_skb(sdata, skb);
2415}
2416
a43abf29
ML
2417static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
2418{
2419 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2420 const u8 *ssid;
f01a067d 2421 u8 *dst = ifmgd->associated->bssid;
180205bd 2422 u8 unicast_limit = max(1, max_probe_tries - 3);
49ddf8e6 2423 struct sta_info *sta;
f01a067d
LR
2424
2425 /*
2426 * Try sending broadcast probe requests for the last three
2427 * probe requests after the first ones failed since some
2428 * buggy APs only support broadcast probe requests.
2429 */
2430 if (ifmgd->probe_send_count >= unicast_limit)
2431 dst = NULL;
a43abf29 2432
4e5ff376
FF
2433 /*
2434 * When the hardware reports an accurate Tx ACK status, it's
2435 * better to send a nullfunc frame instead of a probe request,
2436 * as it will kick us off the AP quickly if we aren't associated
2437 * anymore. The timeout will be reset if the frame is ACKed by
2438 * the AP.
2439 */
992e68bf
SD
2440 ifmgd->probe_send_count++;
2441
49ddf8e6
JB
2442 if (dst) {
2443 mutex_lock(&sdata->local->sta_mtx);
2444 sta = sta_info_get(sdata, dst);
2445 if (!WARN_ON(!sta))
2446 ieee80211_check_fast_rx(sta);
2447 mutex_unlock(&sdata->local->sta_mtx);
2448 }
2449
30686bf7 2450 if (ieee80211_hw_check(&sdata->local->hw, REPORTS_TX_ACK_STATUS)) {
04ac3c0e 2451 ifmgd->nullfunc_failed = false;
076cdcb1 2452 ieee80211_send_nullfunc(sdata->local, sdata, false);
04ac3c0e 2453 } else {
88c868c4
SG
2454 int ssid_len;
2455
9caf0364 2456 rcu_read_lock();
4e5ff376 2457 ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
88c868c4
SG
2458 if (WARN_ON_ONCE(ssid == NULL))
2459 ssid_len = 0;
2460 else
2461 ssid_len = ssid[1];
2462
45ad6834
JB
2463 ieee80211_mlme_send_probe_req(sdata, sdata->vif.addr, dst,
2464 ssid + 2, ssid_len,
2465 ifmgd->associated->channel);
9caf0364 2466 rcu_read_unlock();
4e5ff376 2467 }
a43abf29 2468
180205bd 2469 ifmgd->probe_timeout = jiffies + msecs_to_jiffies(probe_wait_ms);
8d61ffa5 2470 run_again(sdata, ifmgd->probe_timeout);
a43abf29
ML
2471}
2472
b291ba11
JB
2473static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
2474 bool beacon)
04de8381 2475{
04de8381 2476 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
b291ba11 2477 bool already = false;
34bfc411 2478
9607e6b6 2479 if (!ieee80211_sdata_running(sdata))
0e2b6286
JB
2480 return;
2481
8d61ffa5 2482 sdata_lock(sdata);
77fdaa12
JB
2483
2484 if (!ifmgd->associated)
2485 goto out;
2486
133d40f9
SG
2487 mutex_lock(&sdata->local->mtx);
2488
2489 if (sdata->local->tmp_channel || sdata->local->scanning) {
2490 mutex_unlock(&sdata->local->mtx);
2491 goto out;
2492 }
2493
a13fbe54 2494 if (beacon) {
bdcbd8e0 2495 mlme_dbg_ratelimited(sdata,
59c1ec2b
BG
2496 "detected beacon loss from AP (missed %d beacons) - probing\n",
2497 beacon_loss_count);
bdcbd8e0 2498
98f03342 2499 ieee80211_cqm_beacon_loss_notify(&sdata->vif, GFP_KERNEL);
a13fbe54 2500 }
04de8381 2501
b291ba11
JB
2502 /*
2503 * The driver/our work has already reported this event or the
2504 * connection monitoring has kicked in and we have already sent
2505 * a probe request. Or maybe the AP died and the driver keeps
2506 * reporting until we disassociate...
2507 *
2508 * In either case we have to ignore the current call to this
2509 * function (except for setting the correct probe reason bit)
2510 * because otherwise we would reset the timer every time and
2511 * never check whether we received a probe response!
2512 */
392b9ffb 2513 if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL)
b291ba11
JB
2514 already = true;
2515
12b5f34d
EP
2516 ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
2517
133d40f9
SG
2518 mutex_unlock(&sdata->local->mtx);
2519
b291ba11
JB
2520 if (already)
2521 goto out;
2522
4e751843 2523 mutex_lock(&sdata->local->iflist_mtx);
4a733ef1 2524 ieee80211_recalc_ps(sdata->local);
4e751843
JB
2525 mutex_unlock(&sdata->local->iflist_mtx);
2526
a43abf29
ML
2527 ifmgd->probe_send_count = 0;
2528 ieee80211_mgd_probe_ap_send(sdata);
77fdaa12 2529 out:
8d61ffa5 2530 sdata_unlock(sdata);
04de8381
KV
2531}
2532
a619a4c0
JO
2533struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw,
2534 struct ieee80211_vif *vif)
2535{
2536 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2537 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
d9b3b28b 2538 struct cfg80211_bss *cbss;
a619a4c0
JO
2539 struct sk_buff *skb;
2540 const u8 *ssid;
88c868c4 2541 int ssid_len;
a619a4c0
JO
2542
2543 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
2544 return NULL;
2545
8d61ffa5 2546 sdata_assert_lock(sdata);
a619a4c0 2547
d9b3b28b
EP
2548 if (ifmgd->associated)
2549 cbss = ifmgd->associated;
2550 else if (ifmgd->auth_data)
2551 cbss = ifmgd->auth_data->bss;
2552 else if (ifmgd->assoc_data)
2553 cbss = ifmgd->assoc_data->bss;
2554 else
a619a4c0
JO
2555 return NULL;
2556
9caf0364 2557 rcu_read_lock();
d9b3b28b 2558 ssid = ieee80211_bss_get_ie(cbss, WLAN_EID_SSID);
88c868c4
SG
2559 if (WARN_ON_ONCE(ssid == NULL))
2560 ssid_len = 0;
2561 else
2562 ssid_len = ssid[1];
2563
a344d677 2564 skb = ieee80211_build_probe_req(sdata, sdata->vif.addr, cbss->bssid,
55de908a 2565 (u32) -1, cbss->channel,
6b77863b 2566 ssid + 2, ssid_len,
00387f32 2567 NULL, 0, IEEE80211_PROBE_FLAG_DIRECTED);
9caf0364 2568 rcu_read_unlock();
a619a4c0
JO
2569
2570 return skb;
2571}
2572EXPORT_SYMBOL(ieee80211_ap_probereq_get);
2573
a90faa9d
EG
2574static void ieee80211_report_disconnect(struct ieee80211_sub_if_data *sdata,
2575 const u8 *buf, size_t len, bool tx,
2576 u16 reason)
2577{
2578 struct ieee80211_event event = {
2579 .type = MLME_EVENT,
2580 .u.mlme.data = tx ? DEAUTH_TX_EVENT : DEAUTH_RX_EVENT,
2581 .u.mlme.reason = reason,
2582 };
2583
2584 if (tx)
2585 cfg80211_tx_mlme_mgmt(sdata->dev, buf, len);
2586 else
2587 cfg80211_rx_mlme_mgmt(sdata->dev, buf, len);
2588
2589 drv_event_callback(sdata->local, sdata, &event);
2590}
2591
eef9e54c 2592static void __ieee80211_disconnect(struct ieee80211_sub_if_data *sdata)
1e4dcd01 2593{
59af6928 2594 struct ieee80211_local *local = sdata->local;
1e4dcd01 2595 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
6ae16775 2596 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
6c18b27d 2597 bool tx;
1e4dcd01 2598
8d61ffa5 2599 sdata_lock(sdata);
1e4dcd01 2600 if (!ifmgd->associated) {
8d61ffa5 2601 sdata_unlock(sdata);
1e4dcd01
JO
2602 return;
2603 }
2604
6c18b27d
EG
2605 tx = !sdata->csa_block_tx;
2606
20eb7ea9
DS
2607 /* AP is probably out of range (or not reachable for another reason) so
2608 * remove the bss struct for that AP.
2609 */
2610 cfg80211_unlink_bss(local->hw.wiphy, ifmgd->associated);
2611
37ad3888
JB
2612 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
2613 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
6c18b27d 2614 tx, frame_buf);
59af6928 2615 mutex_lock(&local->mtx);
7578d575 2616 sdata->vif.csa_active = false;
0c21e632 2617 ifmgd->csa_waiting_bcn = false;
a46992b4
LC
2618 if (sdata->csa_block_tx) {
2619 ieee80211_wake_vif_queues(local, sdata,
2620 IEEE80211_QUEUE_STOP_REASON_CSA);
2621 sdata->csa_block_tx = false;
2622 }
59af6928 2623 mutex_unlock(&local->mtx);
7da7cc1d 2624
6c18b27d 2625 ieee80211_report_disconnect(sdata, frame_buf, sizeof(frame_buf), tx,
a90faa9d
EG
2626 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
2627
8d61ffa5 2628 sdata_unlock(sdata);
1e4dcd01
JO
2629}
2630
cc74c0c7 2631static void ieee80211_beacon_connection_loss_work(struct work_struct *work)
b291ba11
JB
2632{
2633 struct ieee80211_sub_if_data *sdata =
2634 container_of(work, struct ieee80211_sub_if_data,
1e4dcd01 2635 u.mgd.beacon_connection_loss_work);
a85e1d55 2636 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
a85e1d55 2637
976bd9ef
JB
2638 if (ifmgd->associated)
2639 ifmgd->beacon_loss_count++;
b291ba11 2640
682bd38b 2641 if (ifmgd->connection_loss) {
882a7c69
JB
2642 sdata_info(sdata, "Connection to AP %pM lost\n",
2643 ifmgd->bssid);
eef9e54c 2644 __ieee80211_disconnect(sdata);
882a7c69 2645 } else {
1e4dcd01 2646 ieee80211_mgd_probe_ap(sdata, true);
882a7c69
JB
2647 }
2648}
2649
2650static void ieee80211_csa_connection_drop_work(struct work_struct *work)
2651{
2652 struct ieee80211_sub_if_data *sdata =
2653 container_of(work, struct ieee80211_sub_if_data,
2654 u.mgd.csa_connection_drop_work);
2655
eef9e54c 2656 __ieee80211_disconnect(sdata);
b291ba11
JB
2657}
2658
04de8381
KV
2659void ieee80211_beacon_loss(struct ieee80211_vif *vif)
2660{
2661 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1e4dcd01 2662 struct ieee80211_hw *hw = &sdata->local->hw;
04de8381 2663
b5878a2d
JB
2664 trace_api_beacon_loss(sdata);
2665
682bd38b 2666 sdata->u.mgd.connection_loss = false;
1e4dcd01 2667 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
04de8381
KV
2668}
2669EXPORT_SYMBOL(ieee80211_beacon_loss);
2670
1e4dcd01
JO
2671void ieee80211_connection_loss(struct ieee80211_vif *vif)
2672{
2673 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2674 struct ieee80211_hw *hw = &sdata->local->hw;
2675
b5878a2d
JB
2676 trace_api_connection_loss(sdata);
2677
682bd38b 2678 sdata->u.mgd.connection_loss = true;
1e4dcd01
JO
2679 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
2680}
2681EXPORT_SYMBOL(ieee80211_connection_loss);
2682
2683
66e67e41
JB
2684static void ieee80211_destroy_auth_data(struct ieee80211_sub_if_data *sdata,
2685 bool assoc)
2686{
2687 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
2688
8d61ffa5 2689 sdata_assert_lock(sdata);
66e67e41 2690
66e67e41 2691 if (!assoc) {
c1e140bf
EG
2692 /*
2693 * we are not authenticated yet, the only timer that could be
2694 * running is the timeout for the authentication response which
2695 * which is not relevant anymore.
2696 */
2697 del_timer_sync(&sdata->u.mgd.timer);
66e67e41
JB
2698 sta_info_destroy_addr(sdata, auth_data->bss->bssid);
2699
c84a67a2 2700 eth_zero_addr(sdata->u.mgd.bssid);
66e67e41 2701 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
028e8da0 2702 sdata->u.mgd.flags = 0;
34a3740d 2703 mutex_lock(&sdata->local->mtx);
55de908a 2704 ieee80211_vif_release_channel(sdata);
34a3740d 2705 mutex_unlock(&sdata->local->mtx);
66e67e41
JB
2706 }
2707
5b112d3d 2708 cfg80211_put_bss(sdata->local->hw.wiphy, auth_data->bss);
66e67e41
JB
2709 kfree(auth_data);
2710 sdata->u.mgd.auth_data = NULL;
2711}
2712
c9c99f89 2713static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata,
e6f462df 2714 bool assoc, bool abandon)
c9c99f89
JB
2715{
2716 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
2717
2718 sdata_assert_lock(sdata);
2719
2720 if (!assoc) {
2721 /*
2722 * we are not associated yet, the only timer that could be
2723 * running is the timeout for the association response which
2724 * which is not relevant anymore.
2725 */
2726 del_timer_sync(&sdata->u.mgd.timer);
2727 sta_info_destroy_addr(sdata, assoc_data->bss->bssid);
2728
2729 eth_zero_addr(sdata->u.mgd.bssid);
2730 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
2731 sdata->u.mgd.flags = 0;
b5a33d52
SS
2732 sdata->vif.mu_mimo_owner = false;
2733
c9c99f89
JB
2734 mutex_lock(&sdata->local->mtx);
2735 ieee80211_vif_release_channel(sdata);
2736 mutex_unlock(&sdata->local->mtx);
e6f462df
JB
2737
2738 if (abandon)
2739 cfg80211_abandon_assoc(sdata->dev, assoc_data->bss);
c9c99f89
JB
2740 }
2741
2742 kfree(assoc_data);
2743 sdata->u.mgd.assoc_data = NULL;
2744}
2745
66e67e41
JB
2746static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
2747 struct ieee80211_mgmt *mgmt, size_t len)
2748{
1672c0e3 2749 struct ieee80211_local *local = sdata->local;
66e67e41
JB
2750 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
2751 u8 *pos;
2752 struct ieee802_11_elems elems;
1672c0e3 2753 u32 tx_flags = 0;
66e67e41
JB
2754
2755 pos = mgmt->u.auth.variable;
b2e506bf 2756 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), false, &elems);
66e67e41
JB
2757 if (!elems.challenge)
2758 return;
2759 auth_data->expected_transaction = 4;
d4e36e55 2760 drv_mgd_prepare_tx(sdata->local, sdata, 0);
30686bf7 2761 if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
1672c0e3
JB
2762 tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
2763 IEEE80211_TX_INTFL_MLME_CONN_TX;
700e8ea6 2764 ieee80211_send_auth(sdata, 3, auth_data->algorithm, 0,
66e67e41
JB
2765 elems.challenge - 2, elems.challenge_len + 2,
2766 auth_data->bss->bssid, auth_data->bss->bssid,
2767 auth_data->key, auth_data->key_len,
1672c0e3 2768 auth_data->key_idx, tx_flags);
66e67e41
JB
2769}
2770
fc107a93
JM
2771static bool ieee80211_mark_sta_auth(struct ieee80211_sub_if_data *sdata,
2772 const u8 *bssid)
2773{
efb543e6 2774 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
fc107a93
JM
2775 struct sta_info *sta;
2776
efb543e6
JM
2777 sdata_info(sdata, "authenticated\n");
2778 ifmgd->auth_data->done = true;
2779 ifmgd->auth_data->timeout = jiffies + IEEE80211_AUTH_WAIT_ASSOC;
2780 ifmgd->auth_data->timeout_started = true;
2781 run_again(sdata, ifmgd->auth_data->timeout);
2782
fc107a93
JM
2783 /* move station state to auth */
2784 mutex_lock(&sdata->local->sta_mtx);
2785 sta = sta_info_get(sdata, bssid);
2786 if (!sta) {
2787 WARN_ONCE(1, "%s: STA %pM not found", sdata->name, bssid);
2788 return false;
2789 }
2790 if (sta_info_move_state(sta, IEEE80211_STA_AUTH)) {
2791 sdata_info(sdata, "failed moving %pM to auth\n", bssid);
2792 return false;
2793 }
2794 mutex_unlock(&sdata->local->sta_mtx);
2795
2796 return true;
2797}
2798
8d61ffa5
JB
2799static void ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
2800 struct ieee80211_mgmt *mgmt, size_t len)
66e67e41
JB
2801{
2802 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2803 u8 bssid[ETH_ALEN];
2804 u16 auth_alg, auth_transaction, status_code;
a9409093
EG
2805 struct ieee80211_event event = {
2806 .type = MLME_EVENT,
2807 .u.mlme.data = AUTH_EVENT,
2808 };
66e67e41 2809
8d61ffa5 2810 sdata_assert_lock(sdata);
66e67e41
JB
2811
2812 if (len < 24 + 6)
8d61ffa5 2813 return;
66e67e41
JB
2814
2815 if (!ifmgd->auth_data || ifmgd->auth_data->done)
8d61ffa5 2816 return;
66e67e41
JB
2817
2818 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
2819
b203ca39 2820 if (!ether_addr_equal(bssid, mgmt->bssid))
8d61ffa5 2821 return;
66e67e41
JB
2822
2823 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
2824 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
2825 status_code = le16_to_cpu(mgmt->u.auth.status_code);
2826
2827 if (auth_alg != ifmgd->auth_data->algorithm ||
efb543e6
JM
2828 (auth_alg != WLAN_AUTH_SAE &&
2829 auth_transaction != ifmgd->auth_data->expected_transaction) ||
2830 (auth_alg == WLAN_AUTH_SAE &&
2831 (auth_transaction < ifmgd->auth_data->expected_transaction ||
2832 auth_transaction > 2))) {
0f4126e8
JM
2833 sdata_info(sdata, "%pM unexpected authentication state: alg %d (expected %d) transact %d (expected %d)\n",
2834 mgmt->sa, auth_alg, ifmgd->auth_data->algorithm,
2835 auth_transaction,
2836 ifmgd->auth_data->expected_transaction);
8d61ffa5 2837 return;
0f4126e8 2838 }
66e67e41
JB
2839
2840 if (status_code != WLAN_STATUS_SUCCESS) {
bdcbd8e0
JB
2841 sdata_info(sdata, "%pM denied authentication (status %d)\n",
2842 mgmt->sa, status_code);
dac211ec 2843 ieee80211_destroy_auth_data(sdata, false);
6ff57cf8 2844 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
a9409093
EG
2845 event.u.mlme.status = MLME_DENIED;
2846 event.u.mlme.reason = status_code;
2847 drv_event_callback(sdata->local, sdata, &event);
8d61ffa5 2848 return;
66e67e41
JB
2849 }
2850
2851 switch (ifmgd->auth_data->algorithm) {
2852 case WLAN_AUTH_OPEN:
2853 case WLAN_AUTH_LEAP:
2854 case WLAN_AUTH_FT:
6b8ece3a 2855 case WLAN_AUTH_SAE:
dbc0c2cb
JM
2856 case WLAN_AUTH_FILS_SK:
2857 case WLAN_AUTH_FILS_SK_PFS:
2858 case WLAN_AUTH_FILS_PK:
66e67e41
JB
2859 break;
2860 case WLAN_AUTH_SHARED_KEY:
2861 if (ifmgd->auth_data->expected_transaction != 4) {
2862 ieee80211_auth_challenge(sdata, mgmt, len);
2863 /* need another frame */
8d61ffa5 2864 return;
66e67e41
JB
2865 }
2866 break;
2867 default:
2868 WARN_ONCE(1, "invalid auth alg %d",
2869 ifmgd->auth_data->algorithm);
8d61ffa5 2870 return;
66e67e41
JB
2871 }
2872
a9409093
EG
2873 event.u.mlme.status = MLME_SUCCESS;
2874 drv_event_callback(sdata->local, sdata, &event);
efb543e6
JM
2875 if (ifmgd->auth_data->algorithm != WLAN_AUTH_SAE ||
2876 (auth_transaction == 2 &&
2877 ifmgd->auth_data->expected_transaction == 2)) {
2878 if (!ieee80211_mark_sta_auth(sdata, bssid))
2879 goto out_err;
2880 } else if (ifmgd->auth_data->algorithm == WLAN_AUTH_SAE &&
2881 auth_transaction == 2) {
2882 sdata_info(sdata, "SAE peer confirmed\n");
2883 ifmgd->auth_data->peer_confirmed = true;
6b8ece3a
JM
2884 }
2885
6ff57cf8 2886 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
8d61ffa5 2887 return;
66e67e41
JB
2888 out_err:
2889 mutex_unlock(&sdata->local->sta_mtx);
2890 /* ignore frame -- wait for timeout */
66e67e41
JB
2891}
2892
dfa1ad29
CO
2893#define case_WLAN(type) \
2894 case WLAN_REASON_##type: return #type
2895
2896static const char *ieee80211_get_reason_code_string(u16 reason_code)
2897{
2898 switch (reason_code) {
2899 case_WLAN(UNSPECIFIED);
2900 case_WLAN(PREV_AUTH_NOT_VALID);
2901 case_WLAN(DEAUTH_LEAVING);
2902 case_WLAN(DISASSOC_DUE_TO_INACTIVITY);
2903 case_WLAN(DISASSOC_AP_BUSY);
2904 case_WLAN(CLASS2_FRAME_FROM_NONAUTH_STA);
2905 case_WLAN(CLASS3_FRAME_FROM_NONASSOC_STA);
2906 case_WLAN(DISASSOC_STA_HAS_LEFT);
2907 case_WLAN(STA_REQ_ASSOC_WITHOUT_AUTH);
2908 case_WLAN(DISASSOC_BAD_POWER);
2909 case_WLAN(DISASSOC_BAD_SUPP_CHAN);
2910 case_WLAN(INVALID_IE);
2911 case_WLAN(MIC_FAILURE);
2912 case_WLAN(4WAY_HANDSHAKE_TIMEOUT);
2913 case_WLAN(GROUP_KEY_HANDSHAKE_TIMEOUT);
2914 case_WLAN(IE_DIFFERENT);
2915 case_WLAN(INVALID_GROUP_CIPHER);
2916 case_WLAN(INVALID_PAIRWISE_CIPHER);
2917 case_WLAN(INVALID_AKMP);
2918 case_WLAN(UNSUPP_RSN_VERSION);
2919 case_WLAN(INVALID_RSN_IE_CAP);
2920 case_WLAN(IEEE8021X_FAILED);
2921 case_WLAN(CIPHER_SUITE_REJECTED);
2922 case_WLAN(DISASSOC_UNSPECIFIED_QOS);
2923 case_WLAN(DISASSOC_QAP_NO_BANDWIDTH);
2924 case_WLAN(DISASSOC_LOW_ACK);
2925 case_WLAN(DISASSOC_QAP_EXCEED_TXOP);
2926 case_WLAN(QSTA_LEAVE_QBSS);
2927 case_WLAN(QSTA_NOT_USE);
2928 case_WLAN(QSTA_REQUIRE_SETUP);
2929 case_WLAN(QSTA_TIMEOUT);
2930 case_WLAN(QSTA_CIPHER_NOT_SUPP);
2931 case_WLAN(MESH_PEER_CANCELED);
2932 case_WLAN(MESH_MAX_PEERS);
2933 case_WLAN(MESH_CONFIG);
2934 case_WLAN(MESH_CLOSE);
2935 case_WLAN(MESH_MAX_RETRIES);
2936 case_WLAN(MESH_CONFIRM_TIMEOUT);
2937 case_WLAN(MESH_INVALID_GTK);
2938 case_WLAN(MESH_INCONSISTENT_PARAM);
2939 case_WLAN(MESH_INVALID_SECURITY);
2940 case_WLAN(MESH_PATH_ERROR);
2941 case_WLAN(MESH_PATH_NOFORWARD);
2942 case_WLAN(MESH_PATH_DEST_UNREACHABLE);
2943 case_WLAN(MAC_EXISTS_IN_MBSS);
2944 case_WLAN(MESH_CHAN_REGULATORY);
2945 case_WLAN(MESH_CHAN);
2946 default: return "<unknown>";
2947 }
2948}
66e67e41 2949
8d61ffa5
JB
2950static void ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
2951 struct ieee80211_mgmt *mgmt, size_t len)
f0706e82 2952{
46900298 2953 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
c9c99f89 2954 u16 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
f0706e82 2955
8d61ffa5 2956 sdata_assert_lock(sdata);
66e67e41 2957
f4ea83dd 2958 if (len < 24 + 2)
8d61ffa5 2959 return;
f0706e82 2960
c9c99f89
JB
2961 if (ifmgd->associated &&
2962 ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid)) {
2963 const u8 *bssid = ifmgd->associated->bssid;
77fdaa12 2964
c9c99f89
JB
2965 sdata_info(sdata, "deauthenticated from %pM (Reason: %u=%s)\n",
2966 bssid, reason_code,
2967 ieee80211_get_reason_code_string(reason_code));
f0706e82 2968
c9c99f89 2969 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
f0706e82 2970
c9c99f89
JB
2971 ieee80211_report_disconnect(sdata, (u8 *)mgmt, len, false,
2972 reason_code);
2973 return;
2974 }
77fdaa12 2975
c9c99f89
JB
2976 if (ifmgd->assoc_data &&
2977 ether_addr_equal(mgmt->bssid, ifmgd->assoc_data->bss->bssid)) {
2978 const u8 *bssid = ifmgd->assoc_data->bss->bssid;
37ad3888 2979
c9c99f89
JB
2980 sdata_info(sdata,
2981 "deauthenticated from %pM while associating (Reason: %u=%s)\n",
2982 bssid, reason_code,
2983 ieee80211_get_reason_code_string(reason_code));
2984
e6f462df 2985 ieee80211_destroy_assoc_data(sdata, false, true);
c9c99f89
JB
2986
2987 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
2988 return;
2989 }
f0706e82
JB
2990}
2991
2992
8d61ffa5
JB
2993static void ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
2994 struct ieee80211_mgmt *mgmt, size_t len)
f0706e82 2995{
46900298 2996 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
f0706e82
JB
2997 u16 reason_code;
2998
8d61ffa5 2999 sdata_assert_lock(sdata);
77fdaa12 3000
66e67e41 3001 if (len < 24 + 2)
8d61ffa5 3002 return;
77fdaa12 3003
66e67e41 3004 if (!ifmgd->associated ||
b203ca39 3005 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
8d61ffa5 3006 return;
f0706e82
JB
3007
3008 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
3009
68506e9a
AM
3010 sdata_info(sdata, "disassociated from %pM (Reason: %u=%s)\n",
3011 mgmt->sa, reason_code,
3012 ieee80211_get_reason_code_string(reason_code));
f0706e82 3013
37ad3888
JB
3014 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
3015
a90faa9d 3016 ieee80211_report_disconnect(sdata, (u8 *)mgmt, len, false, reason_code);
f0706e82
JB
3017}
3018
c74d084f
CL
3019static void ieee80211_get_rates(struct ieee80211_supported_band *sband,
3020 u8 *supp_rates, unsigned int supp_rates_len,
3021 u32 *rates, u32 *basic_rates,
3022 bool *have_higher_than_11mbit,
2103dec1 3023 int *min_rate, int *min_rate_index,
44f6d42c 3024 int shift)
c74d084f
CL
3025{
3026 int i, j;
3027
3028 for (i = 0; i < supp_rates_len; i++) {
2103dec1 3029 int rate = supp_rates[i] & 0x7f;
c74d084f
CL
3030 bool is_basic = !!(supp_rates[i] & 0x80);
3031
2103dec1 3032 if ((rate * 5 * (1 << shift)) > 110)
c74d084f
CL
3033 *have_higher_than_11mbit = true;
3034
3035 /*
a6289d3f
JB
3036 * Skip HT and VHT BSS membership selectors since they're not
3037 * rates.
c74d084f 3038 *
a6289d3f 3039 * Note: Even though the membership selector and the basic
c74d084f
CL
3040 * rate flag share the same bit, they are not exactly
3041 * the same.
3042 */
a6289d3f
JB
3043 if (supp_rates[i] == (0x80 | BSS_MEMBERSHIP_SELECTOR_HT_PHY) ||
3044 supp_rates[i] == (0x80 | BSS_MEMBERSHIP_SELECTOR_VHT_PHY))
c74d084f
CL
3045 continue;
3046
3047 for (j = 0; j < sband->n_bitrates; j++) {
2103dec1
SW
3048 struct ieee80211_rate *br;
3049 int brate;
3050
3051 br = &sband->bitrates[j];
2103dec1
SW
3052
3053 brate = DIV_ROUND_UP(br->bitrate, (1 << shift) * 5);
3054 if (brate == rate) {
c74d084f
CL
3055 *rates |= BIT(j);
3056 if (is_basic)
3057 *basic_rates |= BIT(j);
2103dec1
SW
3058 if ((rate * 5) < *min_rate) {
3059 *min_rate = rate * 5;
c74d084f
CL
3060 *min_rate_index = j;
3061 }
3062 break;
3063 }
3064 }
3065 }
3066}
f0706e82 3067
55ebd6e6
EG
3068static bool ieee80211_twt_req_supported(const struct sta_info *sta,
3069 const struct ieee802_11_elems *elems)
3070{
3071 if (elems->ext_capab_len < 10)
3072 return false;
3073
3074 if (!(elems->ext_capab[9] & WLAN_EXT_CAPA10_TWT_RESPONDER_SUPPORT))
3075 return false;
3076
3077 return sta->sta.he_cap.he_cap_elem.mac_cap_info[0] &
3078 IEEE80211_HE_MAC_CAP0_TWT_RES;
3079}
3080
66e67e41
JB
3081static bool ieee80211_assoc_success(struct ieee80211_sub_if_data *sdata,
3082 struct cfg80211_bss *cbss,
af6b6374 3083 struct ieee80211_mgmt *mgmt, size_t len)
f0706e82 3084{
46900298 3085 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
471b3efd 3086 struct ieee80211_local *local = sdata->local;
8318d78a 3087 struct ieee80211_supported_band *sband;
f0706e82 3088 struct sta_info *sta;
af6b6374 3089 u8 *pos;
af6b6374 3090 u16 capab_info, aid;
f0706e82 3091 struct ieee802_11_elems elems;
bda3933a 3092 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
35d865af
JB
3093 const struct cfg80211_bss_ies *bss_ies = NULL;
3094 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
ae5eb026 3095 u32 changed = 0;
c74d084f 3096 int err;
35d865af 3097 bool ret;
f0706e82 3098
af6b6374 3099 /* AssocResp and ReassocResp have identical structure */
f0706e82 3100
f0706e82 3101 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
af6b6374 3102 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
f0706e82 3103
c7c477b5
JB
3104 /*
3105 * The 5 MSB of the AID field are reserved
3106 * (802.11-2016 9.4.1.8 AID field)
3107 */
3108 aid &= 0x7ff;
1dd84aa2 3109
05cb9108
JB
3110 ifmgd->broken_ap = false;
3111
3112 if (aid == 0 || aid > IEEE80211_MAX_AID) {
bdcbd8e0
JB
3113 sdata_info(sdata, "invalid AID value %d (out of range), turn off PS\n",
3114 aid);
05cb9108
JB
3115 aid = 0;
3116 ifmgd->broken_ap = true;
3117 }
3118
af6b6374 3119 pos = mgmt->u.assoc_resp.variable;
b2e506bf 3120 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), false, &elems);
af6b6374 3121
f0706e82 3122 if (!elems.supp_rates) {
bdcbd8e0 3123 sdata_info(sdata, "no SuppRates element in AssocResp\n");
af6b6374 3124 return false;
f0706e82
JB
3125 }
3126
46900298 3127 ifmgd->aid = aid;
9041c1fa
AN
3128 ifmgd->tdls_chan_switch_prohibited =
3129 elems.ext_capab && elems.ext_capab_len >= 5 &&
3130 (elems.ext_capab[4] & WLAN_EXT_CAPA5_TDLS_CH_SW_PROHIBITED);
f0706e82 3131
35d865af
JB
3132 /*
3133 * Some APs are erroneously not including some information in their
3134 * (re)association response frames. Try to recover by using the data
3135 * from the beacon or probe response. This seems to afflict mobile
3136 * 2G/3G/4G wifi routers, reported models include the "Onda PN51T",
3137 * "Vodafone PocketWiFi 2", "ZTE MF60" and a similar T-Mobile device.
3138 */
3139 if ((assoc_data->wmm && !elems.wmm_param) ||
3140 (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
3141 (!elems.ht_cap_elem || !elems.ht_operation)) ||
3142 (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
3143 (!elems.vht_cap_elem || !elems.vht_operation))) {
3144 const struct cfg80211_bss_ies *ies;
3145 struct ieee802_11_elems bss_elems;
3146
3147 rcu_read_lock();
3148 ies = rcu_dereference(cbss->ies);
3149 if (ies)
3150 bss_ies = kmemdup(ies, sizeof(*ies) + ies->len,
3151 GFP_ATOMIC);
3152 rcu_read_unlock();
3153 if (!bss_ies)
3154 return false;
3155
3156 ieee802_11_parse_elems(bss_ies->data, bss_ies->len,
3157 false, &bss_elems);
3158 if (assoc_data->wmm &&
3159 !elems.wmm_param && bss_elems.wmm_param) {
3160 elems.wmm_param = bss_elems.wmm_param;
3161 sdata_info(sdata,
3162 "AP bug: WMM param missing from AssocResp\n");
3163 }
3164
3165 /*
3166 * Also check if we requested HT/VHT, otherwise the AP doesn't
3167 * have to include the IEs in the (re)association response.
3168 */
3169 if (!elems.ht_cap_elem && bss_elems.ht_cap_elem &&
3170 !(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
3171 elems.ht_cap_elem = bss_elems.ht_cap_elem;
3172 sdata_info(sdata,
3173 "AP bug: HT capability missing from AssocResp\n");
3174 }
3175 if (!elems.ht_operation && bss_elems.ht_operation &&
3176 !(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
3177 elems.ht_operation = bss_elems.ht_operation;
3178 sdata_info(sdata,
3179 "AP bug: HT operation missing from AssocResp\n");
3180 }
3181 if (!elems.vht_cap_elem && bss_elems.vht_cap_elem &&
3182 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)) {
3183 elems.vht_cap_elem = bss_elems.vht_cap_elem;
3184 sdata_info(sdata,
3185 "AP bug: VHT capa missing from AssocResp\n");
3186 }
3187 if (!elems.vht_operation && bss_elems.vht_operation &&
3188 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)) {
3189 elems.vht_operation = bss_elems.vht_operation;
3190 sdata_info(sdata,
3191 "AP bug: VHT operation missing from AssocResp\n");
3192 }
3193 }
3194
30eb1dc2
JB
3195 /*
3196 * We previously checked these in the beacon/probe response, so
3197 * they should be present here. This is just a safety net.
3198 */
3199 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
3200 (!elems.wmm_param || !elems.ht_cap_elem || !elems.ht_operation)) {
3201 sdata_info(sdata,
35d865af
JB
3202 "HT AP is missing WMM params or HT capability/operation\n");
3203 ret = false;
3204 goto out;
30eb1dc2
JB
3205 }
3206
3207 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
3208 (!elems.vht_cap_elem || !elems.vht_operation)) {
3209 sdata_info(sdata,
35d865af
JB
3210 "VHT AP is missing VHT capability/operation\n");
3211 ret = false;
3212 goto out;
30eb1dc2
JB
3213 }
3214
2a33bee2
GE
3215 mutex_lock(&sdata->local->sta_mtx);
3216 /*
3217 * station info was already allocated and inserted before
3218 * the association and should be available to us
3219 */
7852e361 3220 sta = sta_info_get(sdata, cbss->bssid);
2a33bee2
GE
3221 if (WARN_ON(!sta)) {
3222 mutex_unlock(&sdata->local->sta_mtx);
35d865af
JB
3223 ret = false;
3224 goto out;
77fdaa12 3225 }
05e5e883 3226
21a8e9dd
MSS
3227 sband = ieee80211_get_sband(sdata);
3228 if (!sband) {
3229 mutex_unlock(&sdata->local->sta_mtx);
3230 ret = false;
3231 goto out;
3232 }
f709fc69 3233
41cbb0f5
LC
3234 /*
3235 * If AP doesn't support HT, or it doesn't have HE mandatory IEs, mark
3236 * HE as disabled. If on the 5GHz band, make sure it supports VHT.
3237 */
3238 if (ifmgd->flags & IEEE80211_STA_DISABLE_HT ||
3239 (sband->band == NL80211_BAND_5GHZ &&
3240 ifmgd->flags & IEEE80211_STA_DISABLE_VHT) ||
3241 (!elems.he_cap && !elems.he_operation))
3242 ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
3243
3244 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE) &&
3245 (!elems.he_cap || !elems.he_operation)) {
3246 mutex_unlock(&sdata->local->sta_mtx);
3247 sdata_info(sdata,
3248 "HE AP is missing HE capability/operation\n");
3249 ret = false;
3250 goto out;
3251 }
3252
30eb1dc2 3253 /* Set up internal HT/VHT capabilities */
a8243b72 3254 if (elems.ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
ef96a842 3255 ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
e1a0c6b3 3256 elems.ht_cap_elem, sta);
64f68e5d 3257
818255ea
MP
3258 if (elems.vht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
3259 ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband,
4a34215e 3260 elems.vht_cap_elem, sta);
818255ea 3261
41cbb0f5
LC
3262 if (elems.he_operation && !(ifmgd->flags & IEEE80211_STA_DISABLE_HE) &&
3263 elems.he_cap) {
3264 ieee80211_he_cap_ie_to_sta_he_cap(sdata, sband,
3265 elems.he_cap,
3266 elems.he_cap_len,
3267 sta);
3268
3269 bss_conf->he_support = sta->sta.he_cap.has_he;
55ebd6e6
EG
3270 bss_conf->twt_requester =
3271 ieee80211_twt_req_supported(sta, &elems);
41cbb0f5
LC
3272 } else {
3273 bss_conf->he_support = false;
55ebd6e6 3274 bss_conf->twt_requester = false;
41cbb0f5
LC
3275 }
3276
3277 if (bss_conf->he_support) {
77cbbc35
NG
3278 bss_conf->bss_color =
3279 le32_get_bits(elems.he_operation->he_oper_params,
3280 IEEE80211_HE_OPERATION_BSS_COLOR_MASK);
41cbb0f5 3281
41cbb0f5 3282 bss_conf->htc_trig_based_pkt_ext =
77cbbc35
NG
3283 le32_get_bits(elems.he_operation->he_oper_params,
3284 IEEE80211_HE_OPERATION_DFLT_PE_DURATION_MASK);
41cbb0f5 3285 bss_conf->frame_time_rts_th =
77cbbc35
NG
3286 le32_get_bits(elems.he_operation->he_oper_params,
3287 IEEE80211_HE_OPERATION_RTS_THRESHOLD_MASK);
41cbb0f5
LC
3288
3289 bss_conf->multi_sta_back_32bit =
3290 sta->sta.he_cap.he_cap_elem.mac_cap_info[2] &
3291 IEEE80211_HE_MAC_CAP2_32BIT_BA_BITMAP;
3292
3293 bss_conf->ack_enabled =
3294 sta->sta.he_cap.he_cap_elem.mac_cap_info[2] &
3295 IEEE80211_HE_MAC_CAP2_ACK_EN;
3296
3297 bss_conf->uora_exists = !!elems.uora_element;
3298 if (elems.uora_element)
3299 bss_conf->uora_ocw_range = elems.uora_element[0];
3300
3301 /* TODO: OPEN: what happens if BSS color disable is set? */
3302 }
3303
30eb1dc2
JB
3304 /*
3305 * Some APs, e.g. Netgear WNDR3700, report invalid HT operation data
3306 * in their association response, so ignore that data for our own
3307 * configuration. If it changed since the last beacon, we'll get the
3308 * next beacon and update then.
3309 */
1128958d 3310
bee7f586
JB
3311 /*
3312 * If an operating mode notification IE is present, override the
3313 * NSS calculation (that would be done in rate_control_rate_init())
3314 * and use the # of streams from that element.
3315 */
3316 if (elems.opmode_notif &&
3317 !(*elems.opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_TYPE_BF)) {
3318 u8 nss;
3319
3320 nss = *elems.opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_MASK;
3321 nss >>= IEEE80211_OPMODE_NOTIF_RX_NSS_SHIFT;
3322 nss += 1;
3323 sta->sta.rx_nss = nss;
3324 }
3325
4b7679a5 3326 rate_control_rate_init(sta);
f0706e82 3327
93f0490e 3328 if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED) {
c2c98fde 3329 set_sta_flag(sta, WLAN_STA_MFP);
93f0490e
T
3330 sta->sta.mfp = true;
3331 } else {
3332 sta->sta.mfp = false;
3333 }
5394af4d 3334
527871d7 3335 sta->sta.wme = elems.wmm_param && local->hw.queues >= IEEE80211_NUM_ACS;
ddf4ac53 3336
3e4d40fa 3337 err = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
c8987876
JB
3338 if (!err && !(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
3339 err = sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
3340 if (err) {
bdcbd8e0
JB
3341 sdata_info(sdata,
3342 "failed to move station %pM to desired state\n",
3343 sta->sta.addr);
c8987876
JB
3344 WARN_ON(__sta_info_destroy(sta));
3345 mutex_unlock(&sdata->local->sta_mtx);
35d865af
JB
3346 ret = false;
3347 goto out;
c8987876
JB
3348 }
3349
7852e361 3350 mutex_unlock(&sdata->local->sta_mtx);
ddf4ac53 3351
f2176d72
JO
3352 /*
3353 * Always handle WMM once after association regardless
3354 * of the first value the AP uses. Setting -1 here has
3355 * that effect because the AP values is an unsigned
3356 * 4-bit value.
3357 */
3358 ifmgd->wmm_last_param_set = -1;
2e249fc3 3359 ifmgd->mu_edca_last_param_set = -1;
f2176d72 3360
2ed77ea6 3361 if (ifmgd->flags & IEEE80211_STA_DISABLE_WMM) {
cec66283 3362 ieee80211_set_wmm_default(sdata, false, false);
2ed77ea6 3363 } else if (!ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
41cbb0f5
LC
3364 elems.wmm_param_len,
3365 elems.mu_edca_param_set)) {
2ed77ea6
JB
3366 /* still enable QoS since we might have HT/VHT */
3367 ieee80211_set_wmm_default(sdata, false, true);
3368 /* set the disable-WMM flag in this case to disable
3369 * tracking WMM parameter changes in the beacon if
3370 * the parameters weren't actually valid. Doing so
3371 * avoids changing parameters very strangely when
3372 * the AP is going back and forth between valid and
3373 * invalid parameters.
3374 */
3375 ifmgd->flags |= IEEE80211_STA_DISABLE_WMM;
3376 }
3abead59 3377 changed |= BSS_CHANGED_QOS;
f0706e82 3378
e38a017b
AS
3379 if (elems.max_idle_period_ie) {
3380 bss_conf->max_idle_period =
3381 le16_to_cpu(elems.max_idle_period_ie->max_idle_period);
3382 bss_conf->protected_keep_alive =
3383 !!(elems.max_idle_period_ie->idle_options &
3384 WLAN_IDLE_OPTIONS_PROTECTED_KEEP_ALIVE);
3385 changed |= BSS_CHANGED_KEEP_ALIVE;
3386 } else {
3387 bss_conf->max_idle_period = 0;
3388 bss_conf->protected_keep_alive = false;
3389 }
3390
60f8b39c
JB
3391 /* set AID and assoc capability,
3392 * ieee80211_set_associated() will tell the driver */
3393 bss_conf->aid = aid;
3394 bss_conf->assoc_capability = capab_info;
0c1ad2ca 3395 ieee80211_set_associated(sdata, cbss, changed);
f0706e82 3396
d524215f
FF
3397 /*
3398 * If we're using 4-addr mode, let the AP know that we're
3399 * doing so, so that it can create the STA VLAN on its side
3400 */
3401 if (ifmgd->use_4addr)
3402 ieee80211_send_4addr_nullfunc(local, sdata);
3403
15b7b062 3404 /*
b291ba11
JB
3405 * Start timer to probe the connection to the AP now.
3406 * Also start the timer that will detect beacon loss.
15b7b062 3407 */
b291ba11 3408 ieee80211_sta_rx_notify(sdata, (struct ieee80211_hdr *)mgmt);
d3a910a8 3409 ieee80211_sta_reset_beacon_monitor(sdata);
15b7b062 3410
35d865af
JB
3411 ret = true;
3412 out:
3413 kfree(bss_ies);
3414 return ret;
f0706e82
JB
3415}
3416
8d61ffa5
JB
3417static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
3418 struct ieee80211_mgmt *mgmt,
3419 size_t len)
66e67e41
JB
3420{
3421 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3422 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
3423 u16 capab_info, status_code, aid;
3424 struct ieee802_11_elems elems;
b0b6aa2c 3425 int ac, uapsd_queues = -1;
66e67e41
JB
3426 u8 *pos;
3427 bool reassoc;
8d61ffa5 3428 struct cfg80211_bss *bss;
d0d1a12f
EG
3429 struct ieee80211_event event = {
3430 .type = MLME_EVENT,
3431 .u.mlme.data = ASSOC_EVENT,
3432 };
66e67e41 3433
8d61ffa5 3434 sdata_assert_lock(sdata);
66e67e41
JB
3435
3436 if (!assoc_data)
8d61ffa5 3437 return;
b203ca39 3438 if (!ether_addr_equal(assoc_data->bss->bssid, mgmt->bssid))
8d61ffa5 3439 return;
66e67e41
JB
3440
3441 /*
3442 * AssocResp and ReassocResp have identical structure, so process both
3443 * of them in this function.
3444 */
3445
3446 if (len < 24 + 6)
8d61ffa5 3447 return;
66e67e41 3448
7a7d3e4c 3449 reassoc = ieee80211_is_reassoc_resp(mgmt->frame_control);
66e67e41
JB
3450 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
3451 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
3452 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
3453
bdcbd8e0
JB
3454 sdata_info(sdata,
3455 "RX %sssocResp from %pM (capab=0x%x status=%d aid=%d)\n",
3456 reassoc ? "Rea" : "A", mgmt->sa,
3457 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
66e67e41 3458
39404fee
JM
3459 if (assoc_data->fils_kek_len &&
3460 fils_decrypt_assoc_resp(sdata, (u8 *)mgmt, &len, assoc_data) < 0)
3461 return;
3462
66e67e41 3463 pos = mgmt->u.assoc_resp.variable;
b2e506bf 3464 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), false, &elems);
66e67e41
JB
3465
3466 if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
79ba1d89
JB
3467 elems.timeout_int &&
3468 elems.timeout_int->type == WLAN_TIMEOUT_ASSOC_COMEBACK) {
66e67e41 3469 u32 tu, ms;
79ba1d89 3470 tu = le32_to_cpu(elems.timeout_int->value);
66e67e41 3471 ms = tu * 1024 / 1000;
bdcbd8e0
JB
3472 sdata_info(sdata,
3473 "%pM rejected association temporarily; comeback duration %u TU (%u ms)\n",
3474 mgmt->sa, tu, ms);
66e67e41 3475 assoc_data->timeout = jiffies + msecs_to_jiffies(ms);
89afe614 3476 assoc_data->timeout_started = true;
66e67e41 3477 if (ms > IEEE80211_ASSOC_TIMEOUT)
8d61ffa5
JB
3478 run_again(sdata, assoc_data->timeout);
3479 return;
66e67e41
JB
3480 }
3481
8d61ffa5 3482 bss = assoc_data->bss;
66e67e41
JB
3483
3484 if (status_code != WLAN_STATUS_SUCCESS) {
bdcbd8e0
JB
3485 sdata_info(sdata, "%pM denied association (code=%d)\n",
3486 mgmt->sa, status_code);
e6f462df 3487 ieee80211_destroy_assoc_data(sdata, false, false);
d0d1a12f
EG
3488 event.u.mlme.status = MLME_DENIED;
3489 event.u.mlme.reason = status_code;
3490 drv_event_callback(sdata->local, sdata, &event);
66e67e41 3491 } else {
8d61ffa5 3492 if (!ieee80211_assoc_success(sdata, bss, mgmt, len)) {
66e67e41 3493 /* oops -- internal error -- send timeout for now */
e6f462df 3494 ieee80211_destroy_assoc_data(sdata, false, false);
959867fa 3495 cfg80211_assoc_timeout(sdata->dev, bss);
8d61ffa5 3496 return;
66e67e41 3497 }
d0d1a12f
EG
3498 event.u.mlme.status = MLME_SUCCESS;
3499 drv_event_callback(sdata->local, sdata, &event);
635d999f 3500 sdata_info(sdata, "associated\n");
79ebfb85
JB
3501
3502 /*
3503 * destroy assoc_data afterwards, as otherwise an idle
3504 * recalc after assoc_data is NULL but before associated
3505 * is set can cause the interface to go idle
3506 */
e6f462df 3507 ieee80211_destroy_assoc_data(sdata, true, false);
b0b6aa2c
EP
3508
3509 /* get uapsd queues configuration */
3510 uapsd_queues = 0;
3511 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
3512 if (sdata->tx_conf[ac].uapsd)
f438ceb8 3513 uapsd_queues |= ieee80211_ac_to_qos_mask[ac];
66e67e41
JB
3514 }
3515
b0b6aa2c 3516 cfg80211_rx_assoc_resp(sdata->dev, bss, (u8 *)mgmt, len, uapsd_queues);
66e67e41 3517}
8e3c1b77 3518
98c8fccf 3519static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
8e3c1b77 3520 struct ieee80211_mgmt *mgmt, size_t len,
98c8fccf 3521 struct ieee80211_rx_status *rx_status,
d45c4172 3522 struct ieee802_11_elems *elems)
98c8fccf
JB
3523{
3524 struct ieee80211_local *local = sdata->local;
c2b13452 3525 struct ieee80211_bss *bss;
98c8fccf 3526 struct ieee80211_channel *channel;
56007a02 3527
8d61ffa5 3528 sdata_assert_lock(sdata);
b4f286a1 3529
3afc2167
EG
3530 channel = ieee80211_get_channel(local->hw.wiphy, rx_status->freq);
3531 if (!channel)
98c8fccf
JB
3532 return;
3533
98c8fccf 3534 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
d45c4172 3535 channel);
817cee76 3536 if (bss) {
817cee76 3537 sdata->vif.bss_conf.beacon_rate = bss->beacon_rate;
d2722f8b 3538 ieee80211_rx_bss_put(local, bss);
817cee76 3539 }
f0706e82
JB
3540}
3541
3542
f698d856 3543static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
af6b6374 3544 struct sk_buff *skb)
f0706e82 3545{
af6b6374 3546 struct ieee80211_mgmt *mgmt = (void *)skb->data;
15b7b062 3547 struct ieee80211_if_managed *ifmgd;
af6b6374
JB
3548 struct ieee80211_rx_status *rx_status = (void *) skb->cb;
3549 size_t baselen, len = skb->len;
ae6a44e3
EK
3550 struct ieee802_11_elems elems;
3551
15b7b062
KV
3552 ifmgd = &sdata->u.mgd;
3553
8d61ffa5 3554 sdata_assert_lock(sdata);
77fdaa12 3555
b203ca39 3556 if (!ether_addr_equal(mgmt->da, sdata->vif.addr))
8e7cdbb6
TW
3557 return; /* ignore ProbeResp to foreign address */
3558
ae6a44e3
EK
3559 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
3560 if (baselen > len)
3561 return;
3562
3563 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
b2e506bf 3564 false, &elems);
ae6a44e3 3565
d45c4172 3566 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
9859b81e 3567
77fdaa12 3568 if (ifmgd->associated &&
b203ca39 3569 ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
4e5ff376 3570 ieee80211_reset_ap_probe(sdata);
f0706e82
JB
3571}
3572
d91f36db
JB
3573/*
3574 * This is the canonical list of information elements we care about,
3575 * the filter code also gives us all changes to the Microsoft OUI
c8d65917
SG
3576 * (00:50:F2) vendor IE which is used for WMM which we need to track,
3577 * as well as the DTPC IE (part of the Cisco OUI) used for signaling
3578 * changes to requested client power.
d91f36db
JB
3579 *
3580 * We implement beacon filtering in software since that means we can
3581 * avoid processing the frame here and in cfg80211, and userspace
3582 * will not be able to tell whether the hardware supports it or not.
3583 *
3584 * XXX: This list needs to be dynamic -- userspace needs to be able to
3585 * add items it requires. It also needs to be able to tell us to
3586 * look out for other vendor IEs.
3587 */
3588static const u64 care_about_ies =
1d4df3a5
JB
3589 (1ULL << WLAN_EID_COUNTRY) |
3590 (1ULL << WLAN_EID_ERP_INFO) |
3591 (1ULL << WLAN_EID_CHANNEL_SWITCH) |
3592 (1ULL << WLAN_EID_PWR_CONSTRAINT) |
3593 (1ULL << WLAN_EID_HT_CAPABILITY) |
70a3fd6c
JB
3594 (1ULL << WLAN_EID_HT_OPERATION) |
3595 (1ULL << WLAN_EID_EXT_CHANSWITCH_ANN);
d91f36db 3596
1ad22fb5
T
3597static void ieee80211_handle_beacon_sig(struct ieee80211_sub_if_data *sdata,
3598 struct ieee80211_if_managed *ifmgd,
3599 struct ieee80211_bss_conf *bss_conf,
3600 struct ieee80211_local *local,
3601 struct ieee80211_rx_status *rx_status)
f0706e82 3602{
17e4ec14 3603 /* Track average RSSI from the Beacon frames of the current AP */
1ad22fb5 3604
17e4ec14
JM
3605 if (ifmgd->flags & IEEE80211_STA_RESET_SIGNAL_AVE) {
3606 ifmgd->flags &= ~IEEE80211_STA_RESET_SIGNAL_AVE;
338c17ae 3607 ewma_beacon_signal_init(&ifmgd->ave_beacon_signal);
17e4ec14 3608 ifmgd->last_cqm_event_signal = 0;
391a200a 3609 ifmgd->count_beacon_signal = 1;
615f7b9b 3610 ifmgd->last_ave_beacon_signal = 0;
17e4ec14 3611 } else {
391a200a 3612 ifmgd->count_beacon_signal++;
17e4ec14 3613 }
615f7b9b 3614
338c17ae
JB
3615 ewma_beacon_signal_add(&ifmgd->ave_beacon_signal, -rx_status->signal);
3616
615f7b9b
MV
3617 if (ifmgd->rssi_min_thold != ifmgd->rssi_max_thold &&
3618 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
338c17ae 3619 int sig = -ewma_beacon_signal_read(&ifmgd->ave_beacon_signal);
615f7b9b 3620 int last_sig = ifmgd->last_ave_beacon_signal;
a8182929
EG
3621 struct ieee80211_event event = {
3622 .type = RSSI_EVENT,
3623 };
615f7b9b
MV
3624
3625 /*
3626 * if signal crosses either of the boundaries, invoke callback
3627 * with appropriate parameters
3628 */
3629 if (sig > ifmgd->rssi_max_thold &&
3630 (last_sig <= ifmgd->rssi_min_thold || last_sig == 0)) {
3631 ifmgd->last_ave_beacon_signal = sig;
a8182929
EG
3632 event.u.rssi.data = RSSI_EVENT_HIGH;
3633 drv_event_callback(local, sdata, &event);
615f7b9b
MV
3634 } else if (sig < ifmgd->rssi_min_thold &&
3635 (last_sig >= ifmgd->rssi_max_thold ||
3636 last_sig == 0)) {
3637 ifmgd->last_ave_beacon_signal = sig;
a8182929
EG
3638 event.u.rssi.data = RSSI_EVENT_LOW;
3639 drv_event_callback(local, sdata, &event);
615f7b9b
MV
3640 }
3641 }
3642
17e4ec14 3643 if (bss_conf->cqm_rssi_thold &&
391a200a 3644 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT &&
ea086359 3645 !(sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI)) {
338c17ae 3646 int sig = -ewma_beacon_signal_read(&ifmgd->ave_beacon_signal);
17e4ec14
JM
3647 int last_event = ifmgd->last_cqm_event_signal;
3648 int thold = bss_conf->cqm_rssi_thold;
3649 int hyst = bss_conf->cqm_rssi_hyst;
338c17ae 3650
17e4ec14
JM
3651 if (sig < thold &&
3652 (last_event == 0 || sig < last_event - hyst)) {
3653 ifmgd->last_cqm_event_signal = sig;
3654 ieee80211_cqm_rssi_notify(
3655 &sdata->vif,
3656 NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
769f07d8 3657 sig, GFP_KERNEL);
17e4ec14
JM
3658 } else if (sig > thold &&
3659 (last_event == 0 || sig > last_event + hyst)) {
3660 ifmgd->last_cqm_event_signal = sig;
3661 ieee80211_cqm_rssi_notify(
3662 &sdata->vif,
3663 NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
769f07d8 3664 sig, GFP_KERNEL);
17e4ec14
JM
3665 }
3666 }
3667
2c3c5f8c
AZ
3668 if (bss_conf->cqm_rssi_low &&
3669 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
3670 int sig = -ewma_beacon_signal_read(&ifmgd->ave_beacon_signal);
3671 int last_event = ifmgd->last_cqm_event_signal;
3672 int low = bss_conf->cqm_rssi_low;
3673 int high = bss_conf->cqm_rssi_high;
3674
3675 if (sig < low &&
3676 (last_event == 0 || last_event >= low)) {
3677 ifmgd->last_cqm_event_signal = sig;
3678 ieee80211_cqm_rssi_notify(
3679 &sdata->vif,
3680 NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
3681 sig, GFP_KERNEL);
3682 } else if (sig > high &&
3683 (last_event == 0 || last_event <= high)) {
3684 ifmgd->last_cqm_event_signal = sig;
3685 ieee80211_cqm_rssi_notify(
3686 &sdata->vif,
3687 NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
3688 sig, GFP_KERNEL);
3689 }
3690 }
1ad22fb5
T
3691}
3692
3693static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
3694 struct ieee80211_mgmt *mgmt, size_t len,
3695 struct ieee80211_rx_status *rx_status)
3696{
3697 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3698 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
3699 size_t baselen;
3700 struct ieee802_11_elems elems;
3701 struct ieee80211_local *local = sdata->local;
3702 struct ieee80211_chanctx_conf *chanctx_conf;
3703 struct ieee80211_channel *chan;
3704 struct sta_info *sta;
3705 u32 changed = 0;
3706 bool erp_valid;
3707 u8 erp_value = 0;
3708 u32 ncrc;
3709 u8 *bssid;
3710 u8 deauth_buf[IEEE80211_DEAUTH_FRAME_LEN];
3711
3712 sdata_assert_lock(sdata);
3713
3714 /* Process beacon from the current BSS */
3715 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
3716 if (baselen > len)
3717 return;
3718
3719 rcu_read_lock();
3720 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
3721 if (!chanctx_conf) {
3722 rcu_read_unlock();
3723 return;
3724 }
3725
3726 if (rx_status->freq != chanctx_conf->def.chan->center_freq) {
3727 rcu_read_unlock();
3728 return;
3729 }
3730 chan = chanctx_conf->def.chan;
3731 rcu_read_unlock();
3732
3733 if (ifmgd->assoc_data && ifmgd->assoc_data->need_beacon &&
3734 ether_addr_equal(mgmt->bssid, ifmgd->assoc_data->bss->bssid)) {
3735 ieee802_11_parse_elems(mgmt->u.beacon.variable,
3736 len - baselen, false, &elems);
3737
3738 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
3739 if (elems.tim && !elems.parse_error) {
3740 const struct ieee80211_tim_ie *tim_ie = elems.tim;
3741 ifmgd->dtim_period = tim_ie->dtim_period;
3742 }
3743 ifmgd->have_beacon = true;
3744 ifmgd->assoc_data->need_beacon = false;
3745 if (ieee80211_hw_check(&local->hw, TIMING_BEACON_ONLY)) {
3746 sdata->vif.bss_conf.sync_tsf =
3747 le64_to_cpu(mgmt->u.beacon.timestamp);
3748 sdata->vif.bss_conf.sync_device_ts =
3749 rx_status->device_timestamp;
3750 if (elems.tim)
3751 sdata->vif.bss_conf.sync_dtim_count =
3752 elems.tim->dtim_count;
3753 else
3754 sdata->vif.bss_conf.sync_dtim_count = 0;
3755 }
3756 /* continue assoc process */
3757 ifmgd->assoc_data->timeout = jiffies;
3758 ifmgd->assoc_data->timeout_started = true;
3759 run_again(sdata, ifmgd->assoc_data->timeout);
3760 return;
3761 }
3762
3763 if (!ifmgd->associated ||
3764 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
3765 return;
3766 bssid = ifmgd->associated->bssid;
3767
3768 if (!(rx_status->flag & RX_FLAG_NO_SIGNAL_VAL))
3769 ieee80211_handle_beacon_sig(sdata, ifmgd, bss_conf,
3770 local, rx_status);
2c3c5f8c 3771
392b9ffb 3772 if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL) {
bdcbd8e0 3773 mlme_dbg_ratelimited(sdata,
112c31f0 3774 "cancelling AP probe due to a received beacon\n");
392b9ffb 3775 ieee80211_reset_ap_probe(sdata);
92778180
JM
3776 }
3777
b291ba11
JB
3778 /*
3779 * Push the beacon loss detection into the future since
3780 * we are processing a beacon from the AP just now.
3781 */
d3a910a8 3782 ieee80211_sta_reset_beacon_monitor(sdata);
b291ba11 3783
d91f36db
JB
3784 ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
3785 ncrc = ieee802_11_parse_elems_crc(mgmt->u.beacon.variable,
b2e506bf 3786 len - baselen, false, &elems,
d91f36db
JB
3787 care_about_ies, ncrc);
3788
90d13e8f
JB
3789 if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK) &&
3790 ieee80211_check_tim(elems.tim, elems.tim_len, ifmgd->aid)) {
3791 if (local->hw.conf.dynamic_ps_timeout > 0) {
3792 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
3793 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
3794 ieee80211_hw_config(local,
3795 IEEE80211_CONF_CHANGE_PS);
572e0012 3796 }
076cdcb1 3797 ieee80211_send_nullfunc(local, sdata, false);
90d13e8f
JB
3798 } else if (!local->pspolling && sdata->u.mgd.powersave) {
3799 local->pspolling = true;
3800
3801 /*
3802 * Here is assumed that the driver will be
3803 * able to send ps-poll frame and receive a
3804 * response even though power save mode is
3805 * enabled, but some drivers might require
3806 * to disable power save here. This needs
3807 * to be investigated.
3808 */
3809 ieee80211_send_pspoll(local, sdata);
a97b77b9
VN
3810 }
3811 }
7a5158ef 3812
b115b972
JD
3813 if (sdata->vif.p2p ||
3814 sdata->vif.driver_flags & IEEE80211_VIF_GET_NOA_UPDATE) {
67baf663 3815 struct ieee80211_p2p_noa_attr noa = {};
488dd7b5
JB
3816 int ret;
3817
3818 ret = cfg80211_get_p2p_attr(mgmt->u.beacon.variable,
3819 len - baselen,
3820 IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
934457ee 3821 (u8 *) &noa, sizeof(noa));
67baf663
JD
3822 if (ret >= 2) {
3823 if (sdata->u.mgd.p2p_noa_index != noa.index) {
3824 /* valid noa_attr and index changed */
3825 sdata->u.mgd.p2p_noa_index = noa.index;
3826 memcpy(&bss_conf->p2p_noa_attr, &noa, sizeof(noa));
3827 changed |= BSS_CHANGED_P2P_PS;
3828 /*
3829 * make sure we update all information, the CRC
3830 * mechanism doesn't look at P2P attributes.
3831 */
3832 ifmgd->beacon_crc_valid = false;
3833 }
3834 } else if (sdata->u.mgd.p2p_noa_index != -1) {
3835 /* noa_attr not found and we had valid noa_attr before */
3836 sdata->u.mgd.p2p_noa_index = -1;
3837 memset(&bss_conf->p2p_noa_attr, 0, sizeof(bss_conf->p2p_noa_attr));
488dd7b5 3838 changed |= BSS_CHANGED_P2P_PS;
488dd7b5
JB
3839 ifmgd->beacon_crc_valid = false;
3840 }
3841 }
3842
0c21e632
LC
3843 if (ifmgd->csa_waiting_bcn)
3844 ieee80211_chswitch_post_beacon(sdata);
3845
2ecc3905
AB
3846 /*
3847 * Update beacon timing and dtim count on every beacon appearance. This
3848 * will allow the driver to use the most updated values. Do it before
3849 * comparing this one with last received beacon.
3850 * IMPORTANT: These parameters would possibly be out of sync by the time
3851 * the driver will use them. The synchronized view is currently
3852 * guaranteed only in certain callbacks.
3853 */
30686bf7 3854 if (ieee80211_hw_check(&local->hw, TIMING_BEACON_ONLY)) {
2ecc3905
AB
3855 sdata->vif.bss_conf.sync_tsf =
3856 le64_to_cpu(mgmt->u.beacon.timestamp);
3857 sdata->vif.bss_conf.sync_device_ts =
3858 rx_status->device_timestamp;
3859 if (elems.tim)
3860 sdata->vif.bss_conf.sync_dtim_count =
3861 elems.tim->dtim_count;
3862 else
3863 sdata->vif.bss_conf.sync_dtim_count = 0;
3864 }
3865
d8ec4433 3866 if (ncrc == ifmgd->beacon_crc && ifmgd->beacon_crc_valid)
8d61ffa5 3867 return;
30196673 3868 ifmgd->beacon_crc = ncrc;
d8ec4433 3869 ifmgd->beacon_crc_valid = true;
30196673 3870
d45c4172 3871 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
7d25745d 3872
d70b7616 3873 ieee80211_sta_process_chanswitch(sdata, rx_status->mactime,
2ba45384 3874 rx_status->device_timestamp,
d70b7616
JB
3875 &elems, true);
3876
095d81ce
JB
3877 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_WMM) &&
3878 ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
41cbb0f5
LC
3879 elems.wmm_param_len,
3880 elems.mu_edca_param_set))
7d25745d
JB
3881 changed |= BSS_CHANGED_QOS;
3882
c65dd147
EG
3883 /*
3884 * If we haven't had a beacon before, tell the driver about the
ef429dad 3885 * DTIM period (and beacon timing if desired) now.
c65dd147 3886 */
989c6505 3887 if (!ifmgd->have_beacon) {
c65dd147
EG
3888 /* a few bogus AP send dtim_period = 0 or no TIM IE */
3889 if (elems.tim)
3890 bss_conf->dtim_period = elems.tim->dtim_period ?: 1;
3891 else
3892 bss_conf->dtim_period = 1;
ef429dad 3893
989c6505
AB
3894 changed |= BSS_CHANGED_BEACON_INFO;
3895 ifmgd->have_beacon = true;
482a9c74
AB
3896
3897 mutex_lock(&local->iflist_mtx);
4a733ef1 3898 ieee80211_recalc_ps(local);
482a9c74
AB
3899 mutex_unlock(&local->iflist_mtx);
3900
ce857888 3901 ieee80211_recalc_ps_vif(sdata);
c65dd147
EG
3902 }
3903
1946bed9 3904 if (elems.erp_info) {
7a5158ef
JB
3905 erp_valid = true;
3906 erp_value = elems.erp_info[0];
3907 } else {
3908 erp_valid = false;
50c4afb9 3909 }
7a5158ef
JB
3910 changed |= ieee80211_handle_bss_capability(sdata,
3911 le16_to_cpu(mgmt->u.beacon.capab_info),
3912 erp_valid, erp_value);
f0706e82 3913
1128958d 3914 mutex_lock(&local->sta_mtx);
bee7f586
JB
3915 sta = sta_info_get(sdata, bssid);
3916
53b954ee
EP
3917 if (ieee80211_config_bw(sdata, sta,
3918 elems.ht_cap_elem, elems.ht_operation,
41cbb0f5
LC
3919 elems.vht_operation, elems.he_operation,
3920 bssid, &changed)) {
30eb1dc2 3921 mutex_unlock(&local->sta_mtx);
89f774e6
JB
3922 sdata_info(sdata,
3923 "failed to follow AP %pM bandwidth change, disconnect\n",
3924 bssid);
30eb1dc2
JB
3925 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
3926 WLAN_REASON_DEAUTH_LEAVING,
3927 true, deauth_buf);
a90faa9d
EG
3928 ieee80211_report_disconnect(sdata, deauth_buf,
3929 sizeof(deauth_buf), true,
3930 WLAN_REASON_DEAUTH_LEAVING);
8d61ffa5 3931 return;
30eb1dc2 3932 }
bee7f586
JB
3933
3934 if (sta && elems.opmode_notif)
3935 ieee80211_vht_handle_opmode(sdata, sta, *elems.opmode_notif,
cf1e05c6 3936 rx_status->band);
1128958d 3937 mutex_unlock(&local->sta_mtx);
d3c990fb 3938
24a4e400
SG
3939 changed |= ieee80211_handle_pwr_constr(sdata, chan, mgmt,
3940 elems.country_elem,
3941 elems.country_elem_len,
c8d65917
SG
3942 elems.pwr_constr_elem,
3943 elems.cisco_dtpc_elem);
3f2355cb 3944
471b3efd 3945 ieee80211_bss_info_change_notify(sdata, changed);
f0706e82
JB
3946}
3947
1fa57d01
JB
3948void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
3949 struct sk_buff *skb)
f0706e82
JB
3950{
3951 struct ieee80211_rx_status *rx_status;
f0706e82
JB
3952 struct ieee80211_mgmt *mgmt;
3953 u16 fc;
1b3a2e49
JB
3954 struct ieee802_11_elems elems;
3955 int ies_len;
f0706e82 3956
f0706e82
JB
3957 rx_status = (struct ieee80211_rx_status *) skb->cb;
3958 mgmt = (struct ieee80211_mgmt *) skb->data;
3959 fc = le16_to_cpu(mgmt->frame_control);
3960
8d61ffa5 3961 sdata_lock(sdata);
77fdaa12 3962
66e67e41
JB
3963 switch (fc & IEEE80211_FCTL_STYPE) {
3964 case IEEE80211_STYPE_BEACON:
8d61ffa5 3965 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len, rx_status);
66e67e41
JB
3966 break;
3967 case IEEE80211_STYPE_PROBE_RESP:
3968 ieee80211_rx_mgmt_probe_resp(sdata, skb);
3969 break;
3970 case IEEE80211_STYPE_AUTH:
8d61ffa5 3971 ieee80211_rx_mgmt_auth(sdata, mgmt, skb->len);
66e67e41
JB
3972 break;
3973 case IEEE80211_STYPE_DEAUTH:
8d61ffa5 3974 ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
66e67e41
JB
3975 break;
3976 case IEEE80211_STYPE_DISASSOC:
8d61ffa5 3977 ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
66e67e41
JB
3978 break;
3979 case IEEE80211_STYPE_ASSOC_RESP:
3980 case IEEE80211_STYPE_REASSOC_RESP:
8d61ffa5 3981 ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len);
66e67e41
JB
3982 break;
3983 case IEEE80211_STYPE_ACTION:
37799e52 3984 if (mgmt->u.action.category == WLAN_CATEGORY_SPECTRUM_MGMT) {
1b3a2e49
JB
3985 ies_len = skb->len -
3986 offsetof(struct ieee80211_mgmt,
3987 u.action.u.chan_switch.variable);
37799e52
JB
3988
3989 if (ies_len < 0)
3990 break;
3991
3992 ieee802_11_parse_elems(
3993 mgmt->u.action.u.chan_switch.variable,
b2e506bf 3994 ies_len, true, &elems);
37799e52
JB
3995
3996 if (elems.parse_error)
3997 break;
3998
1b3a2e49 3999 ieee80211_sta_process_chanswitch(sdata,
2ba45384
LC
4000 rx_status->mactime,
4001 rx_status->device_timestamp,
4002 &elems, false);
1b3a2e49
JB
4003 } else if (mgmt->u.action.category == WLAN_CATEGORY_PUBLIC) {
4004 ies_len = skb->len -
4005 offsetof(struct ieee80211_mgmt,
4006 u.action.u.ext_chan_switch.variable);
4007
4008 if (ies_len < 0)
4009 break;
4010
4011 ieee802_11_parse_elems(
4012 mgmt->u.action.u.ext_chan_switch.variable,
b2e506bf 4013 ies_len, true, &elems);
1b3a2e49
JB
4014
4015 if (elems.parse_error)
4016 break;
4017
4018 /* for the handling code pretend this was also an IE */
4019 elems.ext_chansw_ie =
4020 &mgmt->u.action.u.ext_chan_switch.data;
4021
66e67e41 4022 ieee80211_sta_process_chanswitch(sdata,
2ba45384
LC
4023 rx_status->mactime,
4024 rx_status->device_timestamp,
4025 &elems, false);
77fdaa12 4026 }
37799e52 4027 break;
77fdaa12 4028 }
8d61ffa5 4029 sdata_unlock(sdata);
f0706e82
JB
4030}
4031
34f11cd3 4032static void ieee80211_sta_timer(struct timer_list *t)
f0706e82 4033{
60f8b39c 4034 struct ieee80211_sub_if_data *sdata =
34f11cd3 4035 from_timer(sdata, t, u.mgd.timer);
5bb644a0 4036
9b7d72c1 4037 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
f0706e82
JB
4038}
4039
04ac3c0e 4040static void ieee80211_sta_connection_lost(struct ieee80211_sub_if_data *sdata,
6b684db1 4041 u8 *bssid, u8 reason, bool tx)
04ac3c0e 4042{
6ae16775 4043 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
04ac3c0e 4044
37ad3888 4045 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH, reason,
6b684db1 4046 tx, frame_buf);
37ad3888 4047
a90faa9d
EG
4048 ieee80211_report_disconnect(sdata, frame_buf, sizeof(frame_buf), true,
4049 reason);
04ac3c0e
FF
4050}
4051
46cad4b7 4052static int ieee80211_auth(struct ieee80211_sub_if_data *sdata)
66e67e41
JB
4053{
4054 struct ieee80211_local *local = sdata->local;
4055 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4056 struct ieee80211_mgd_auth_data *auth_data = ifmgd->auth_data;
1672c0e3 4057 u32 tx_flags = 0;
46cad4b7
JB
4058 u16 trans = 1;
4059 u16 status = 0;
d4e36e55 4060 u16 prepare_tx_duration = 0;
66e67e41 4061
8d61ffa5 4062 sdata_assert_lock(sdata);
66e67e41
JB
4063
4064 if (WARN_ON_ONCE(!auth_data))
4065 return -EINVAL;
4066
66e67e41
JB
4067 auth_data->tries++;
4068
4069 if (auth_data->tries > IEEE80211_AUTH_MAX_TRIES) {
bdcbd8e0
JB
4070 sdata_info(sdata, "authentication with %pM timed out\n",
4071 auth_data->bss->bssid);
66e67e41
JB
4072
4073 /*
4074 * Most likely AP is not in the range so remove the
4075 * bss struct for that AP.
4076 */
4077 cfg80211_unlink_bss(local->hw.wiphy, auth_data->bss);
4078
4079 return -ETIMEDOUT;
4080 }
4081
d4e36e55
IP
4082 if (auth_data->algorithm == WLAN_AUTH_SAE)
4083 prepare_tx_duration =
4084 jiffies_to_msecs(IEEE80211_AUTH_TIMEOUT_SAE);
4085
4086 drv_mgd_prepare_tx(local, sdata, prepare_tx_duration);
a1845fc7 4087
46cad4b7
JB
4088 sdata_info(sdata, "send auth to %pM (try %d/%d)\n",
4089 auth_data->bss->bssid, auth_data->tries,
4090 IEEE80211_AUTH_MAX_TRIES);
66e67e41 4091
46cad4b7 4092 auth_data->expected_transaction = 2;
6b8ece3a 4093
46cad4b7
JB
4094 if (auth_data->algorithm == WLAN_AUTH_SAE) {
4095 trans = auth_data->sae_trans;
4096 status = auth_data->sae_status;
4097 auth_data->expected_transaction = trans;
4098 }
66e67e41 4099
46cad4b7
JB
4100 if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
4101 tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
4102 IEEE80211_TX_INTFL_MLME_CONN_TX;
66e67e41 4103
46cad4b7
JB
4104 ieee80211_send_auth(sdata, trans, auth_data->algorithm, status,
4105 auth_data->data, auth_data->data_len,
4106 auth_data->bss->bssid,
4107 auth_data->bss->bssid, NULL, 0, 0,
4108 tx_flags);
66e67e41 4109
6211dd12 4110 if (tx_flags == 0) {
407879b6
IP
4111 if (auth_data->algorithm == WLAN_AUTH_SAE)
4112 auth_data->timeout = jiffies +
4113 IEEE80211_AUTH_TIMEOUT_SAE;
4114 else
4115 auth_data->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
89afe614 4116 } else {
cb236d2d
JB
4117 auth_data->timeout =
4118 round_jiffies_up(jiffies + IEEE80211_AUTH_TIMEOUT_LONG);
1672c0e3 4119 }
66e67e41 4120
407879b6
IP
4121 auth_data->timeout_started = true;
4122 run_again(sdata, auth_data->timeout);
4123
66e67e41
JB
4124 return 0;
4125}
4126
4127static int ieee80211_do_assoc(struct ieee80211_sub_if_data *sdata)
4128{
4129 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
4130 struct ieee80211_local *local = sdata->local;
4131
8d61ffa5 4132 sdata_assert_lock(sdata);
66e67e41 4133
66e67e41
JB
4134 assoc_data->tries++;
4135 if (assoc_data->tries > IEEE80211_ASSOC_MAX_TRIES) {
bdcbd8e0
JB
4136 sdata_info(sdata, "association with %pM timed out\n",
4137 assoc_data->bss->bssid);
66e67e41
JB
4138
4139 /*
4140 * Most likely AP is not in the range so remove the
4141 * bss struct for that AP.
4142 */
4143 cfg80211_unlink_bss(local->hw.wiphy, assoc_data->bss);
4144
4145 return -ETIMEDOUT;
4146 }
4147
bdcbd8e0
JB
4148 sdata_info(sdata, "associate with %pM (try %d/%d)\n",
4149 assoc_data->bss->bssid, assoc_data->tries,
4150 IEEE80211_ASSOC_MAX_TRIES);
66e67e41
JB
4151 ieee80211_send_assoc(sdata);
4152
30686bf7 4153 if (!ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
1672c0e3 4154 assoc_data->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
89afe614 4155 assoc_data->timeout_started = true;
8d61ffa5 4156 run_again(sdata, assoc_data->timeout);
89afe614 4157 } else {
cb236d2d
JB
4158 assoc_data->timeout =
4159 round_jiffies_up(jiffies +
4160 IEEE80211_ASSOC_TIMEOUT_LONG);
4161 assoc_data->timeout_started = true;
4162 run_again(sdata, assoc_data->timeout);
1672c0e3 4163 }
66e67e41
JB
4164
4165 return 0;
4166}
4167
1672c0e3
JB
4168void ieee80211_mgd_conn_tx_status(struct ieee80211_sub_if_data *sdata,
4169 __le16 fc, bool acked)
4170{
4171 struct ieee80211_local *local = sdata->local;
4172
4173 sdata->u.mgd.status_fc = fc;
4174 sdata->u.mgd.status_acked = acked;
4175 sdata->u.mgd.status_received = true;
4176
4177 ieee80211_queue_work(&local->hw, &sdata->work);
4178}
4179
1fa57d01 4180void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
9c6bd790 4181{
9c6bd790 4182 struct ieee80211_local *local = sdata->local;
1fa57d01 4183 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
9c6bd790 4184
8d61ffa5 4185 sdata_lock(sdata);
77fdaa12 4186
1672c0e3
JB
4187 if (ifmgd->status_received) {
4188 __le16 fc = ifmgd->status_fc;
4189 bool status_acked = ifmgd->status_acked;
4190
4191 ifmgd->status_received = false;
46cad4b7 4192 if (ifmgd->auth_data && ieee80211_is_auth(fc)) {
1672c0e3 4193 if (status_acked) {
407879b6
IP
4194 if (ifmgd->auth_data->algorithm ==
4195 WLAN_AUTH_SAE)
4196 ifmgd->auth_data->timeout =
4197 jiffies +
4198 IEEE80211_AUTH_TIMEOUT_SAE;
4199 else
4200 ifmgd->auth_data->timeout =
4201 jiffies +
4202 IEEE80211_AUTH_TIMEOUT_SHORT;
8d61ffa5 4203 run_again(sdata, ifmgd->auth_data->timeout);
1672c0e3
JB
4204 } else {
4205 ifmgd->auth_data->timeout = jiffies - 1;
4206 }
89afe614 4207 ifmgd->auth_data->timeout_started = true;
1672c0e3
JB
4208 } else if (ifmgd->assoc_data &&
4209 (ieee80211_is_assoc_req(fc) ||
4210 ieee80211_is_reassoc_req(fc))) {
4211 if (status_acked) {
4212 ifmgd->assoc_data->timeout =
4213 jiffies + IEEE80211_ASSOC_TIMEOUT_SHORT;
8d61ffa5 4214 run_again(sdata, ifmgd->assoc_data->timeout);
1672c0e3
JB
4215 } else {
4216 ifmgd->assoc_data->timeout = jiffies - 1;
4217 }
89afe614 4218 ifmgd->assoc_data->timeout_started = true;
1672c0e3
JB
4219 }
4220 }
4221
89afe614 4222 if (ifmgd->auth_data && ifmgd->auth_data->timeout_started &&
66e67e41
JB
4223 time_after(jiffies, ifmgd->auth_data->timeout)) {
4224 if (ifmgd->auth_data->done) {
4225 /*
4226 * ok ... we waited for assoc but userspace didn't,
4227 * so let's just kill the auth data
4228 */
4229 ieee80211_destroy_auth_data(sdata, false);
46cad4b7 4230 } else if (ieee80211_auth(sdata)) {
66e67e41 4231 u8 bssid[ETH_ALEN];
a9409093
EG
4232 struct ieee80211_event event = {
4233 .type = MLME_EVENT,
4234 .u.mlme.data = AUTH_EVENT,
4235 .u.mlme.status = MLME_TIMEOUT,
4236 };
66e67e41
JB
4237
4238 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
4239
4240 ieee80211_destroy_auth_data(sdata, false);
4241
6ff57cf8 4242 cfg80211_auth_timeout(sdata->dev, bssid);
a9409093 4243 drv_event_callback(sdata->local, sdata, &event);
66e67e41 4244 }
89afe614 4245 } else if (ifmgd->auth_data && ifmgd->auth_data->timeout_started)
8d61ffa5 4246 run_again(sdata, ifmgd->auth_data->timeout);
66e67e41 4247
89afe614 4248 if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started &&
66e67e41 4249 time_after(jiffies, ifmgd->assoc_data->timeout)) {
989c6505 4250 if ((ifmgd->assoc_data->need_beacon && !ifmgd->have_beacon) ||
66e67e41 4251 ieee80211_do_assoc(sdata)) {
959867fa 4252 struct cfg80211_bss *bss = ifmgd->assoc_data->bss;
d0d1a12f
EG
4253 struct ieee80211_event event = {
4254 .type = MLME_EVENT,
4255 .u.mlme.data = ASSOC_EVENT,
4256 .u.mlme.status = MLME_TIMEOUT,
4257 };
66e67e41 4258
e6f462df 4259 ieee80211_destroy_assoc_data(sdata, false, false);
959867fa 4260 cfg80211_assoc_timeout(sdata->dev, bss);
d0d1a12f 4261 drv_event_callback(sdata->local, sdata, &event);
66e67e41 4262 }
89afe614 4263 } else if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started)
8d61ffa5 4264 run_again(sdata, ifmgd->assoc_data->timeout);
66e67e41 4265
392b9ffb 4266 if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL &&
b291ba11 4267 ifmgd->associated) {
a43abf29 4268 u8 bssid[ETH_ALEN];
72a8a3ed 4269 int max_tries;
a43abf29 4270
0c1ad2ca 4271 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
4e5ff376 4272
30686bf7 4273 if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
180205bd 4274 max_tries = max_nullfunc_tries;
72a8a3ed 4275 else
180205bd 4276 max_tries = max_probe_tries;
72a8a3ed 4277
4e5ff376
FF
4278 /* ACK received for nullfunc probing frame */
4279 if (!ifmgd->probe_send_count)
4280 ieee80211_reset_ap_probe(sdata);
04ac3c0e
FF
4281 else if (ifmgd->nullfunc_failed) {
4282 if (ifmgd->probe_send_count < max_tries) {
bdcbd8e0
JB
4283 mlme_dbg(sdata,
4284 "No ack for nullfunc frame to AP %pM, try %d/%i\n",
4285 bssid, ifmgd->probe_send_count,
4286 max_tries);
04ac3c0e
FF
4287 ieee80211_mgd_probe_ap_send(sdata);
4288 } else {
bdcbd8e0
JB
4289 mlme_dbg(sdata,
4290 "No ack for nullfunc frame to AP %pM, disconnecting.\n",
4291 bssid);
95acac61 4292 ieee80211_sta_connection_lost(sdata, bssid,
6b684db1
JB
4293 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
4294 false);
04ac3c0e
FF
4295 }
4296 } else if (time_is_after_jiffies(ifmgd->probe_timeout))
8d61ffa5 4297 run_again(sdata, ifmgd->probe_timeout);
30686bf7 4298 else if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
bdcbd8e0
JB
4299 mlme_dbg(sdata,
4300 "Failed to send nullfunc to AP %pM after %dms, disconnecting\n",
4301 bssid, probe_wait_ms);
95acac61 4302 ieee80211_sta_connection_lost(sdata, bssid,
6b684db1 4303 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
04ac3c0e 4304 } else if (ifmgd->probe_send_count < max_tries) {
bdcbd8e0
JB
4305 mlme_dbg(sdata,
4306 "No probe response from AP %pM after %dms, try %d/%i\n",
4307 bssid, probe_wait_ms,
4308 ifmgd->probe_send_count, max_tries);
a43abf29
ML
4309 ieee80211_mgd_probe_ap_send(sdata);
4310 } else {
b291ba11
JB
4311 /*
4312 * We actually lost the connection ... or did we?
4313 * Let's make sure!
4314 */
89f774e6
JB
4315 mlme_dbg(sdata,
4316 "No probe response from AP %pM after %dms, disconnecting.\n",
4317 bssid, probe_wait_ms);
04ac3c0e 4318
95acac61 4319 ieee80211_sta_connection_lost(sdata, bssid,
6b684db1 4320 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
b291ba11
JB
4321 }
4322 }
4323
8d61ffa5 4324 sdata_unlock(sdata);
f0706e82
JB
4325}
4326
34f11cd3 4327static void ieee80211_sta_bcn_mon_timer(struct timer_list *t)
b291ba11
JB
4328{
4329 struct ieee80211_sub_if_data *sdata =
34f11cd3 4330 from_timer(sdata, t, u.mgd.bcn_mon_timer);
0c21e632 4331 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
b291ba11 4332
0c21e632 4333 if (sdata->vif.csa_active && !ifmgd->csa_waiting_bcn)
e5593f56
MK
4334 return;
4335
682bd38b 4336 sdata->u.mgd.connection_loss = false;
1e4dcd01
JO
4337 ieee80211_queue_work(&sdata->local->hw,
4338 &sdata->u.mgd.beacon_connection_loss_work);
b291ba11
JB
4339}
4340
34f11cd3 4341static void ieee80211_sta_conn_mon_timer(struct timer_list *t)
b291ba11
JB
4342{
4343 struct ieee80211_sub_if_data *sdata =
34f11cd3 4344 from_timer(sdata, t, u.mgd.conn_mon_timer);
b291ba11
JB
4345 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4346 struct ieee80211_local *local = sdata->local;
4347
0c21e632 4348 if (sdata->vif.csa_active && !ifmgd->csa_waiting_bcn)
e5593f56
MK
4349 return;
4350
42935eca 4351 ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
b291ba11
JB
4352}
4353
4354static void ieee80211_sta_monitor_work(struct work_struct *work)
4355{
4356 struct ieee80211_sub_if_data *sdata =
4357 container_of(work, struct ieee80211_sub_if_data,
4358 u.mgd.monitor_work);
4359
4360 ieee80211_mgd_probe_ap(sdata, false);
4361}
4362
9c6bd790
JB
4363static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
4364{
ad935687 4365 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
925e64c3 4366 __ieee80211_stop_poll(sdata);
ad935687 4367
b291ba11 4368 /* let's probe the connection once */
30686bf7 4369 if (!ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
494f1fe5
EP
4370 ieee80211_queue_work(&sdata->local->hw,
4371 &sdata->u.mgd.monitor_work);
ad935687 4372 }
9c6bd790 4373}
f0706e82 4374
b8360ab8 4375#ifdef CONFIG_PM
1a1cb744
JB
4376void ieee80211_mgd_quiesce(struct ieee80211_sub_if_data *sdata)
4377{
4378 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4379 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
4380
4381 sdata_lock(sdata);
4382
c52666ae
EG
4383 if (ifmgd->auth_data || ifmgd->assoc_data) {
4384 const u8 *bssid = ifmgd->auth_data ?
4385 ifmgd->auth_data->bss->bssid :
4386 ifmgd->assoc_data->bss->bssid;
4387
1a1cb744 4388 /*
c52666ae
EG
4389 * If we are trying to authenticate / associate while suspending,
4390 * cfg80211 won't know and won't actually abort those attempts,
4391 * thus we need to do that ourselves.
1a1cb744 4392 */
c52666ae 4393 ieee80211_send_deauth_disassoc(sdata, bssid,
1a1cb744
JB
4394 IEEE80211_STYPE_DEAUTH,
4395 WLAN_REASON_DEAUTH_LEAVING,
4396 false, frame_buf);
c52666ae 4397 if (ifmgd->assoc_data)
e6f462df 4398 ieee80211_destroy_assoc_data(sdata, false, true);
c52666ae
EG
4399 if (ifmgd->auth_data)
4400 ieee80211_destroy_auth_data(sdata, false);
1a1cb744
JB
4401 cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
4402 IEEE80211_DEAUTH_FRAME_LEN);
4403 }
4404
be72afe0
JB
4405 /* This is a bit of a hack - we should find a better and more generic
4406 * solution to this. Normally when suspending, cfg80211 will in fact
4407 * deauthenticate. However, it doesn't (and cannot) stop an ongoing
4408 * auth (not so important) or assoc (this is the problem) process.
4409 *
4410 * As a consequence, it can happen that we are in the process of both
4411 * associating and suspending, and receive an association response
4412 * after cfg80211 has checked if it needs to disconnect, but before
4413 * we actually set the flag to drop incoming frames. This will then
4414 * cause the workqueue flush to process the association response in
4415 * the suspend, resulting in a successful association just before it
4416 * tries to remove the interface from the driver, which now though
4417 * has a channel context assigned ... this results in issues.
4418 *
4419 * To work around this (for now) simply deauth here again if we're
4420 * now connected.
4421 */
4422 if (ifmgd->associated && !sdata->local->wowlan) {
4423 u8 bssid[ETH_ALEN];
4424 struct cfg80211_deauth_request req = {
4425 .reason_code = WLAN_REASON_DEAUTH_LEAVING,
4426 .bssid = bssid,
4427 };
4428
4429 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
4430 ieee80211_mgd_deauth(sdata, &req);
4431 }
4432
1a1cb744
JB
4433 sdata_unlock(sdata);
4434}
4435
b8360ab8
JB
4436void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
4437{
4438 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4439
8d61ffa5 4440 sdata_lock(sdata);
b8360ab8 4441 if (!ifmgd->associated) {
8d61ffa5 4442 sdata_unlock(sdata);
b8360ab8
JB
4443 return;
4444 }
4445
4446 if (sdata->flags & IEEE80211_SDATA_DISCONNECT_RESUME) {
4447 sdata->flags &= ~IEEE80211_SDATA_DISCONNECT_RESUME;
4448 mlme_dbg(sdata, "driver requested disconnect after resume\n");
4449 ieee80211_sta_connection_lost(sdata,
4450 ifmgd->associated->bssid,
4451 WLAN_REASON_UNSPECIFIED,
4452 true);
8d61ffa5 4453 sdata_unlock(sdata);
b8360ab8
JB
4454 return;
4455 }
8d61ffa5 4456 sdata_unlock(sdata);
b8360ab8
JB
4457}
4458#endif
4459
9c6bd790
JB
4460/* interface setup */
4461void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
f0706e82 4462{
46900298 4463 struct ieee80211_if_managed *ifmgd;
988c0f72 4464
46900298 4465 ifmgd = &sdata->u.mgd;
b291ba11 4466 INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
46900298 4467 INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
1e4dcd01
JO
4468 INIT_WORK(&ifmgd->beacon_connection_loss_work,
4469 ieee80211_beacon_connection_loss_work);
882a7c69
JB
4470 INIT_WORK(&ifmgd->csa_connection_drop_work,
4471 ieee80211_csa_connection_drop_work);
687da132 4472 INIT_WORK(&ifmgd->request_smps_work, ieee80211_request_smps_mgd_work);
81dd2b88
AN
4473 INIT_DELAYED_WORK(&ifmgd->tdls_peer_del_work,
4474 ieee80211_tdls_peer_del_work);
34f11cd3
KC
4475 timer_setup(&ifmgd->timer, ieee80211_sta_timer, 0);
4476 timer_setup(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer, 0);
4477 timer_setup(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer, 0);
4478 timer_setup(&ifmgd->chswitch_timer, ieee80211_chswitch_timer, 0);
02219b3a
JB
4479 INIT_DELAYED_WORK(&ifmgd->tx_tspec_wk,
4480 ieee80211_sta_handle_tspec_ac_params_wk);
9c6bd790 4481
ab1faead 4482 ifmgd->flags = 0;
1478acb3 4483 ifmgd->powersave = sdata->wdev.ps;
219c3867
AB
4484 ifmgd->uapsd_queues = sdata->local->hw.uapsd_queues;
4485 ifmgd->uapsd_max_sp_len = sdata->local->hw.uapsd_max_sp_len;
67baf663 4486 ifmgd->p2p_noa_index = -1;
bbbdff9e 4487
0d8614b4 4488 if (sdata->local->hw.wiphy->features & NL80211_FEATURE_DYNAMIC_SMPS)
0f78231b
JB
4489 ifmgd->req_smps = IEEE80211_SMPS_AUTOMATIC;
4490 else
4491 ifmgd->req_smps = IEEE80211_SMPS_OFF;
1277b4a9
LK
4492
4493 /* Setup TDLS data */
4494 spin_lock_init(&ifmgd->teardown_lock);
4495 ifmgd->teardown_skb = NULL;
4496 ifmgd->orig_teardown_skb = NULL;
f0706e82
JB
4497}
4498
77fdaa12
JB
4499/* scan finished notification */
4500void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
60f8b39c 4501{
31ee67a1 4502 struct ieee80211_sub_if_data *sdata;
60f8b39c 4503
77fdaa12
JB
4504 /* Restart STA timers */
4505 rcu_read_lock();
370bd005
BG
4506 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
4507 if (ieee80211_sdata_running(sdata))
4508 ieee80211_restart_sta_timer(sdata);
4509 }
77fdaa12
JB
4510 rcu_read_unlock();
4511}
60f8b39c 4512
f2d9d270
JB
4513static u8 ieee80211_ht_vht_rx_chains(struct ieee80211_sub_if_data *sdata,
4514 struct cfg80211_bss *cbss)
4515{
4516 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4517 const u8 *ht_cap_ie, *vht_cap_ie;
4518 const struct ieee80211_ht_cap *ht_cap;
4519 const struct ieee80211_vht_cap *vht_cap;
4520 u8 chains = 1;
4521
4522 if (ifmgd->flags & IEEE80211_STA_DISABLE_HT)
4523 return chains;
4524
9caf0364 4525 ht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
f2d9d270
JB
4526 if (ht_cap_ie && ht_cap_ie[1] >= sizeof(*ht_cap)) {
4527 ht_cap = (void *)(ht_cap_ie + 2);
4528 chains = ieee80211_mcs_to_chains(&ht_cap->mcs);
4529 /*
4530 * TODO: use "Tx Maximum Number Spatial Streams Supported" and
4531 * "Tx Unequal Modulation Supported" fields.
4532 */
4533 }
4534
4535 if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
4536 return chains;
4537
9caf0364 4538 vht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
f2d9d270
JB
4539 if (vht_cap_ie && vht_cap_ie[1] >= sizeof(*vht_cap)) {
4540 u8 nss;
4541 u16 tx_mcs_map;
4542
4543 vht_cap = (void *)(vht_cap_ie + 2);
4544 tx_mcs_map = le16_to_cpu(vht_cap->supp_mcs.tx_mcs_map);
4545 for (nss = 8; nss > 0; nss--) {
4546 if (((tx_mcs_map >> (2 * (nss - 1))) & 3) !=
4547 IEEE80211_VHT_MCS_NOT_SUPPORTED)
4548 break;
4549 }
4550 /* TODO: use "Tx Highest Supported Long GI Data Rate" field? */
4551 chains = max(chains, nss);
4552 }
4553
4554 return chains;
4555}
4556
41cbb0f5
LC
4557static bool
4558ieee80211_verify_sta_he_mcs_support(struct ieee80211_supported_band *sband,
4559 const struct ieee80211_he_operation *he_op)
4560{
4561 const struct ieee80211_sta_he_cap *sta_he_cap =
4562 ieee80211_get_he_sta_cap(sband);
47aa7861 4563 u16 ap_min_req_set;
41cbb0f5
LC
4564 int i;
4565
4566 if (!sta_he_cap || !he_op)
4567 return false;
4568
47aa7861
GS
4569 ap_min_req_set = le16_to_cpu(he_op->he_mcs_nss_set);
4570
41cbb0f5
LC
4571 /* Need to go over for 80MHz, 160MHz and for 80+80 */
4572 for (i = 0; i < 3; i++) {
4573 const struct ieee80211_he_mcs_nss_supp *sta_mcs_nss_supp =
4574 &sta_he_cap->he_mcs_nss_supp;
4575 u16 sta_mcs_map_rx =
4576 le16_to_cpu(((__le16 *)sta_mcs_nss_supp)[2 * i]);
4577 u16 sta_mcs_map_tx =
4578 le16_to_cpu(((__le16 *)sta_mcs_nss_supp)[2 * i + 1]);
4579 u8 nss;
4580 bool verified = true;
4581
4582 /*
4583 * For each band there is a maximum of 8 spatial streams
4584 * possible. Each of the sta_mcs_map_* is a 16-bit struct built
4585 * of 2 bits per NSS (1-8), with the values defined in enum
4586 * ieee80211_he_mcs_support. Need to make sure STA TX and RX
4587 * capabilities aren't less than the AP's minimum requirements
4588 * for this HE BSS per SS.
4589 * It is enough to find one such band that meets the reqs.
4590 */
4591 for (nss = 8; nss > 0; nss--) {
4592 u8 sta_rx_val = (sta_mcs_map_rx >> (2 * (nss - 1))) & 3;
4593 u8 sta_tx_val = (sta_mcs_map_tx >> (2 * (nss - 1))) & 3;
4594 u8 ap_val = (ap_min_req_set >> (2 * (nss - 1))) & 3;
4595
4596 if (ap_val == IEEE80211_HE_MCS_NOT_SUPPORTED)
4597 continue;
4598
4599 /*
4600 * Make sure the HE AP doesn't require MCSs that aren't
4601 * supported by the client
4602 */
4603 if (sta_rx_val == IEEE80211_HE_MCS_NOT_SUPPORTED ||
4604 sta_tx_val == IEEE80211_HE_MCS_NOT_SUPPORTED ||
4605 (ap_val > sta_rx_val) || (ap_val > sta_tx_val)) {
4606 verified = false;
4607 break;
4608 }
4609 }
4610
4611 if (verified)
4612 return true;
4613 }
4614
4615 /* If here, STA doesn't meet AP's HE min requirements */
4616 return false;
4617}
4618
b17166a7
JB
4619static int ieee80211_prep_channel(struct ieee80211_sub_if_data *sdata,
4620 struct cfg80211_bss *cbss)
a1cf775d
JB
4621{
4622 struct ieee80211_local *local = sdata->local;
4623 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
53b954ee 4624 const struct ieee80211_ht_cap *ht_cap = NULL;
24398e39 4625 const struct ieee80211_ht_operation *ht_oper = NULL;
f2d9d270 4626 const struct ieee80211_vht_operation *vht_oper = NULL;
41cbb0f5 4627 const struct ieee80211_he_operation *he_oper = NULL;
24398e39 4628 struct ieee80211_supported_band *sband;
4bf88530 4629 struct cfg80211_chan_def chandef;
f2d9d270 4630 int ret;
52a45f38
AN
4631 u32 i;
4632 bool have_80mhz;
a1cf775d 4633
24398e39
JB
4634 sband = local->hw.wiphy->bands[cbss->channel->band];
4635
f2d9d270
JB
4636 ifmgd->flags &= ~(IEEE80211_STA_DISABLE_40MHZ |
4637 IEEE80211_STA_DISABLE_80P80MHZ |
4638 IEEE80211_STA_DISABLE_160MHZ);
4639
9caf0364
JB
4640 rcu_read_lock();
4641
f2d9d270
JB
4642 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
4643 sband->ht_cap.ht_supported) {
53b954ee 4644 const u8 *ht_oper_ie, *ht_cap_ie;
24398e39 4645
9caf0364 4646 ht_oper_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_OPERATION);
24398e39
JB
4647 if (ht_oper_ie && ht_oper_ie[1] >= sizeof(*ht_oper))
4648 ht_oper = (void *)(ht_oper_ie + 2);
08e6effa 4649
53b954ee
EP
4650 ht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
4651 if (ht_cap_ie && ht_cap_ie[1] >= sizeof(*ht_cap))
4652 ht_cap = (void *)(ht_cap_ie + 2);
4653
4654 if (!ht_cap) {
08e6effa
JB
4655 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
4656 ht_oper = NULL;
4657 }
24398e39
JB
4658 }
4659
f2d9d270
JB
4660 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
4661 sband->vht_cap.vht_supported) {
08e6effa 4662 const u8 *vht_oper_ie, *vht_cap;
f2d9d270 4663
9caf0364
JB
4664 vht_oper_ie = ieee80211_bss_get_ie(cbss,
4665 WLAN_EID_VHT_OPERATION);
f2d9d270
JB
4666 if (vht_oper_ie && vht_oper_ie[1] >= sizeof(*vht_oper))
4667 vht_oper = (void *)(vht_oper_ie + 2);
4668 if (vht_oper && !ht_oper) {
4669 vht_oper = NULL;
bdcbd8e0 4670 sdata_info(sdata,
f2d9d270 4671 "AP advertised VHT without HT, disabling both\n");
cb145022
JB
4672 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
4673 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
24398e39 4674 }
08e6effa
JB
4675
4676 vht_cap = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
4677 if (!vht_cap || vht_cap[1] < sizeof(struct ieee80211_vht_cap)) {
4678 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
4679 vht_oper = NULL;
4680 }
24398e39
JB
4681 }
4682
41cbb0f5
LC
4683 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE) &&
4684 ieee80211_get_he_sta_cap(sband)) {
4685 const struct cfg80211_bss_ies *ies;
4686 const u8 *he_oper_ie;
4687
4688 ies = rcu_dereference(cbss->ies);
4689 he_oper_ie = cfg80211_find_ext_ie(WLAN_EID_EXT_HE_OPERATION,
4690 ies->data, ies->len);
4691 if (he_oper_ie &&
4692 he_oper_ie[1] == ieee80211_he_oper_size(&he_oper_ie[3]))
4693 he_oper = (void *)(he_oper_ie + 3);
4694 else
4695 he_oper = NULL;
4696
f0c0407d 4697 if (!ieee80211_verify_sta_he_mcs_support(sband, he_oper))
41cbb0f5
LC
4698 ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
4699 }
4700
52a45f38
AN
4701 /* Allow VHT if at least one channel on the sband supports 80 MHz */
4702 have_80mhz = false;
4703 for (i = 0; i < sband->n_channels; i++) {
4704 if (sband->channels[i].flags & (IEEE80211_CHAN_DISABLED |
4705 IEEE80211_CHAN_NO_80MHZ))
4706 continue;
4707
4708 have_80mhz = true;
4709 break;
4710 }
4711
4712 if (!have_80mhz)
4713 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
4714
f2d9d270
JB
4715 ifmgd->flags |= ieee80211_determine_chantype(sdata, sband,
4716 cbss->channel,
41cbb0f5 4717 ht_oper, vht_oper, he_oper,
5cdaed1e 4718 &chandef, false);
04ecd257 4719
f2d9d270
JB
4720 sdata->needed_rx_chains = min(ieee80211_ht_vht_rx_chains(sdata, cbss),
4721 local->rx_chains);
24398e39 4722
9caf0364
JB
4723 rcu_read_unlock();
4724
04ecd257
JB
4725 /* will change later if needed */
4726 sdata->smps_mode = IEEE80211_SMPS_OFF;
4727
34a3740d 4728 mutex_lock(&local->mtx);
f2d9d270
JB
4729 /*
4730 * If this fails (possibly due to channel context sharing
4731 * on incompatible channels, e.g. 80+80 and 160 sharing the
4732 * same control channel) try to use a smaller bandwidth.
4733 */
4734 ret = ieee80211_vif_use_channel(sdata, &chandef,
4735 IEEE80211_CHANCTX_SHARED);
0418a445
SW
4736
4737 /* don't downgrade for 5 and 10 MHz channels, though. */
4738 if (chandef.width == NL80211_CHAN_WIDTH_5 ||
4739 chandef.width == NL80211_CHAN_WIDTH_10)
34a3740d 4740 goto out;
0418a445 4741
d601cd8d 4742 while (ret && chandef.width != NL80211_CHAN_WIDTH_20_NOHT) {
e6b7cde4 4743 ifmgd->flags |= ieee80211_chandef_downgrade(&chandef);
d601cd8d
JB
4744 ret = ieee80211_vif_use_channel(sdata, &chandef,
4745 IEEE80211_CHANCTX_SHARED);
4746 }
34a3740d
JB
4747 out:
4748 mutex_unlock(&local->mtx);
f2d9d270 4749 return ret;
b17166a7
JB
4750}
4751
4752static int ieee80211_prep_connection(struct ieee80211_sub_if_data *sdata,
c1041f10
CRI
4753 struct cfg80211_bss *cbss, bool assoc,
4754 bool override)
b17166a7
JB
4755{
4756 struct ieee80211_local *local = sdata->local;
4757 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4758 struct ieee80211_bss *bss = (void *)cbss->priv;
4759 struct sta_info *new_sta = NULL;
179b8fc7 4760 struct ieee80211_supported_band *sband;
c1041f10 4761 bool have_sta = false;
b17166a7
JB
4762 int err;
4763
179b8fc7
CRI
4764 sband = local->hw.wiphy->bands[cbss->channel->band];
4765
b17166a7
JB
4766 if (WARN_ON(!ifmgd->auth_data && !ifmgd->assoc_data))
4767 return -EINVAL;
4768
f8860ce8
LC
4769 /* If a reconfig is happening, bail out */
4770 if (local->in_reconfig)
4771 return -EBUSY;
4772
b17166a7
JB
4773 if (assoc) {
4774 rcu_read_lock();
4775 have_sta = sta_info_get(sdata, cbss->bssid);
4776 rcu_read_unlock();
4777 }
4778
4779 if (!have_sta) {
4780 new_sta = sta_info_alloc(sdata, cbss->bssid, GFP_KERNEL);
4781 if (!new_sta)
4782 return -ENOMEM;
4783 }
179b8fc7 4784
c87905be
JB
4785 /*
4786 * Set up the information for the new channel before setting the
4787 * new channel. We can't - completely race-free - change the basic
4788 * rates bitmap and the channel (sband) that it refers to, but if
4789 * we set it up before we at least avoid calling into the driver's
4790 * bss_info_changed() method with invalid information (since we do
4791 * call that from changing the channel - only for IDLE and perhaps
4792 * some others, but ...).
4793 *
4794 * So to avoid that, just set up all the new information before the
4795 * channel, but tell the driver to apply it only afterwards, since
4796 * it might need the new channel for that.
4797 */
13e0c8e3 4798 if (new_sta) {
5d6a1b06
JB
4799 u32 rates = 0, basic_rates = 0;
4800 bool have_higher_than_11mbit;
4801 int min_rate = INT_MAX, min_rate_index = -1;
8cef2c9d 4802 const struct cfg80211_bss_ies *ies;
179b8fc7 4803 int shift = ieee80211_vif_get_shift(&sdata->vif);
5d6a1b06 4804
5d6a1b06
JB
4805 ieee80211_get_rates(sband, bss->supp_rates,
4806 bss->supp_rates_len,
4807 &rates, &basic_rates,
4808 &have_higher_than_11mbit,
2103dec1 4809 &min_rate, &min_rate_index,
44f6d42c 4810 shift);
5d6a1b06
JB
4811
4812 /*
4813 * This used to be a workaround for basic rates missing
4814 * in the association response frame. Now that we no
4815 * longer use the basic rates from there, it probably
4816 * doesn't happen any more, but keep the workaround so
4817 * in case some *other* APs are buggy in different ways
4818 * we can connect -- with a warning.
4819 */
4820 if (!basic_rates && min_rate_index >= 0) {
bdcbd8e0
JB
4821 sdata_info(sdata,
4822 "No basic rates, using min rate instead\n");
5d6a1b06
JB
4823 basic_rates = BIT(min_rate_index);
4824 }
4825
13e0c8e3 4826 new_sta->sta.supp_rates[cbss->channel->band] = rates;
5d6a1b06
JB
4827 sdata->vif.bss_conf.basic_rates = basic_rates;
4828
4829 /* cf. IEEE 802.11 9.2.12 */
57fbcce3 4830 if (cbss->channel->band == NL80211_BAND_2GHZ &&
5d6a1b06
JB
4831 have_higher_than_11mbit)
4832 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
4833 else
4834 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
4835
4836 memcpy(ifmgd->bssid, cbss->bssid, ETH_ALEN);
4837
8c358bcd
JB
4838 /* set timing information */
4839 sdata->vif.bss_conf.beacon_int = cbss->beacon_interval;
8cef2c9d 4840 rcu_read_lock();
ef429dad
JB
4841 ies = rcu_dereference(cbss->beacon_ies);
4842 if (ies) {
4843 const u8 *tim_ie;
4844
4845 sdata->vif.bss_conf.sync_tsf = ies->tsf;
4846 sdata->vif.bss_conf.sync_device_ts =
4847 bss->device_ts_beacon;
4848 tim_ie = cfg80211_find_ie(WLAN_EID_TIM,
4849 ies->data, ies->len);
4850 if (tim_ie && tim_ie[1] >= 2)
4851 sdata->vif.bss_conf.sync_dtim_count = tim_ie[2];
4852 else
4853 sdata->vif.bss_conf.sync_dtim_count = 0;
30686bf7
JB
4854 } else if (!ieee80211_hw_check(&sdata->local->hw,
4855 TIMING_BEACON_ONLY)) {
ef429dad
JB
4856 ies = rcu_dereference(cbss->proberesp_ies);
4857 /* must be non-NULL since beacon IEs were NULL */
4858 sdata->vif.bss_conf.sync_tsf = ies->tsf;
4859 sdata->vif.bss_conf.sync_device_ts =
4860 bss->device_ts_presp;
4861 sdata->vif.bss_conf.sync_dtim_count = 0;
4862 } else {
4863 sdata->vif.bss_conf.sync_tsf = 0;
4864 sdata->vif.bss_conf.sync_device_ts = 0;
4865 sdata->vif.bss_conf.sync_dtim_count = 0;
4866 }
8cef2c9d 4867 rcu_read_unlock();
c87905be 4868 }
8c358bcd 4869
c87905be
JB
4870 if (new_sta || override) {
4871 err = ieee80211_prep_channel(sdata, cbss);
4872 if (err) {
4873 if (new_sta)
4874 sta_info_free(local, new_sta);
4875 return -EINVAL;
4876 }
4877 }
4878
4879 if (new_sta) {
4880 /*
4881 * tell driver about BSSID, basic rates and timing
4882 * this was set up above, before setting the channel
4883 */
5d6a1b06 4884 ieee80211_bss_info_change_notify(sdata,
8c358bcd
JB
4885 BSS_CHANGED_BSSID | BSS_CHANGED_BASIC_RATES |
4886 BSS_CHANGED_BEACON_INT);
a1cf775d
JB
4887
4888 if (assoc)
13e0c8e3 4889 sta_info_pre_move_state(new_sta, IEEE80211_STA_AUTH);
a1cf775d 4890
13e0c8e3
JB
4891 err = sta_info_insert(new_sta);
4892 new_sta = NULL;
a1cf775d 4893 if (err) {
bdcbd8e0
JB
4894 sdata_info(sdata,
4895 "failed to insert STA entry for the AP (error %d)\n",
4896 err);
a1cf775d
JB
4897 return err;
4898 }
4899 } else
b203ca39 4900 WARN_ON_ONCE(!ether_addr_equal(ifmgd->bssid, cbss->bssid));
a1cf775d 4901
7d830a19
DS
4902 /* Cancel scan to ensure that nothing interferes with connection */
4903 if (local->scanning)
4904 ieee80211_scan_cancel(local);
4905
a1cf775d
JB
4906 return 0;
4907}
4908
77fdaa12
JB
4909/* config hooks */
4910int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
4911 struct cfg80211_auth_request *req)
9c6bd790 4912{
66e67e41
JB
4913 struct ieee80211_local *local = sdata->local;
4914 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4915 struct ieee80211_mgd_auth_data *auth_data;
77fdaa12 4916 u16 auth_alg;
66e67e41 4917 int err;
efb543e6 4918 bool cont_auth;
66e67e41
JB
4919
4920 /* prepare auth data structure */
9c6bd790 4921
77fdaa12
JB
4922 switch (req->auth_type) {
4923 case NL80211_AUTHTYPE_OPEN_SYSTEM:
4924 auth_alg = WLAN_AUTH_OPEN;
4925 break;
4926 case NL80211_AUTHTYPE_SHARED_KEY:
66e67e41 4927 if (IS_ERR(local->wep_tx_tfm))
9dca9c49 4928 return -EOPNOTSUPP;
77fdaa12
JB
4929 auth_alg = WLAN_AUTH_SHARED_KEY;
4930 break;
4931 case NL80211_AUTHTYPE_FT:
4932 auth_alg = WLAN_AUTH_FT;
4933 break;
4934 case NL80211_AUTHTYPE_NETWORK_EAP:
4935 auth_alg = WLAN_AUTH_LEAP;
4936 break;
6b8ece3a
JM
4937 case NL80211_AUTHTYPE_SAE:
4938 auth_alg = WLAN_AUTH_SAE;
4939 break;
dbc0c2cb
JM
4940 case NL80211_AUTHTYPE_FILS_SK:
4941 auth_alg = WLAN_AUTH_FILS_SK;
4942 break;
4943 case NL80211_AUTHTYPE_FILS_SK_PFS:
4944 auth_alg = WLAN_AUTH_FILS_SK_PFS;
4945 break;
4946 case NL80211_AUTHTYPE_FILS_PK:
4947 auth_alg = WLAN_AUTH_FILS_PK;
4948 break;
77fdaa12
JB
4949 default:
4950 return -EOPNOTSUPP;
60f8b39c 4951 }
77fdaa12 4952
efb543e6 4953 if (ifmgd->assoc_data)
8d7432a2
JM
4954 return -EBUSY;
4955
11b6b5a4 4956 auth_data = kzalloc(sizeof(*auth_data) + req->auth_data_len +
6b8ece3a 4957 req->ie_len, GFP_KERNEL);
66e67e41 4958 if (!auth_data)
9c6bd790 4959 return -ENOMEM;
77fdaa12 4960
66e67e41 4961 auth_data->bss = req->bss;
77fdaa12 4962
11b6b5a4 4963 if (req->auth_data_len >= 4) {
6ec63612
JM
4964 if (req->auth_type == NL80211_AUTHTYPE_SAE) {
4965 __le16 *pos = (__le16 *) req->auth_data;
4966
4967 auth_data->sae_trans = le16_to_cpu(pos[0]);
4968 auth_data->sae_status = le16_to_cpu(pos[1]);
4969 }
11b6b5a4
JM
4970 memcpy(auth_data->data, req->auth_data + 4,
4971 req->auth_data_len - 4);
4972 auth_data->data_len += req->auth_data_len - 4;
6b8ece3a
JM
4973 }
4974
efb543e6
JM
4975 /* Check if continuing authentication or trying to authenticate with the
4976 * same BSS that we were in the process of authenticating with and avoid
4977 * removal and re-addition of the STA entry in
4978 * ieee80211_prep_connection().
4979 */
4980 cont_auth = ifmgd->auth_data && req->bss == ifmgd->auth_data->bss;
4981
77fdaa12 4982 if (req->ie && req->ie_len) {
6b8ece3a
JM
4983 memcpy(&auth_data->data[auth_data->data_len],
4984 req->ie, req->ie_len);
4985 auth_data->data_len += req->ie_len;
9c6bd790 4986 }
77fdaa12 4987
fffd0934 4988 if (req->key && req->key_len) {
66e67e41
JB
4989 auth_data->key_len = req->key_len;
4990 auth_data->key_idx = req->key_idx;
4991 memcpy(auth_data->key, req->key, req->key_len);
fffd0934
JB
4992 }
4993
66e67e41 4994 auth_data->algorithm = auth_alg;
60f8b39c 4995
66e67e41 4996 /* try to authenticate/probe */
2a33bee2 4997
efb543e6
JM
4998 if (ifmgd->auth_data) {
4999 if (cont_auth && req->auth_type == NL80211_AUTHTYPE_SAE) {
5000 auth_data->peer_confirmed =
5001 ifmgd->auth_data->peer_confirmed;
5002 }
5003 ieee80211_destroy_auth_data(sdata, cont_auth);
5004 }
2a33bee2 5005
66e67e41
JB
5006 /* prep auth_data so we don't go into idle on disassoc */
5007 ifmgd->auth_data = auth_data;
af6b6374 5008
efb543e6
JM
5009 /* If this is continuation of an ongoing SAE authentication exchange
5010 * (i.e., request to send SAE Confirm) and the peer has already
5011 * confirmed, mark authentication completed since we are about to send
5012 * out SAE Confirm.
5013 */
5014 if (cont_auth && req->auth_type == NL80211_AUTHTYPE_SAE &&
5015 auth_data->peer_confirmed && auth_data->sae_trans == 2)
5016 ieee80211_mark_sta_auth(sdata, req->bss->bssid);
5017
7b119dc0
JB
5018 if (ifmgd->associated) {
5019 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
5020
89f774e6
JB
5021 sdata_info(sdata,
5022 "disconnect from AP %pM for new auth to %pM\n",
5023 ifmgd->associated->bssid, req->bss->bssid);
7b119dc0
JB
5024 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
5025 WLAN_REASON_UNSPECIFIED,
5026 false, frame_buf);
5027
a90faa9d
EG
5028 ieee80211_report_disconnect(sdata, frame_buf,
5029 sizeof(frame_buf), true,
5030 WLAN_REASON_UNSPECIFIED);
7b119dc0 5031 }
af6b6374 5032
bdcbd8e0 5033 sdata_info(sdata, "authenticate with %pM\n", req->bss->bssid);
e5b900d2 5034
efb543e6 5035 err = ieee80211_prep_connection(sdata, req->bss, cont_auth, false);
a1cf775d 5036 if (err)
66e67e41 5037 goto err_clear;
af6b6374 5038
46cad4b7 5039 err = ieee80211_auth(sdata);
66e67e41 5040 if (err) {
66e67e41
JB
5041 sta_info_destroy_addr(sdata, req->bss->bssid);
5042 goto err_clear;
5043 }
5044
5045 /* hold our own reference */
5b112d3d 5046 cfg80211_ref_bss(local->hw.wiphy, auth_data->bss);
8d61ffa5 5047 return 0;
66e67e41
JB
5048
5049 err_clear:
c84a67a2 5050 eth_zero_addr(ifmgd->bssid);
3d2abdfd 5051 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
66e67e41 5052 ifmgd->auth_data = NULL;
d01f858c
MK
5053 mutex_lock(&sdata->local->mtx);
5054 ieee80211_vif_release_channel(sdata);
5055 mutex_unlock(&sdata->local->mtx);
66e67e41 5056 kfree(auth_data);
66e67e41 5057 return err;
af6b6374
JB
5058}
5059
77fdaa12
JB
5060int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
5061 struct cfg80211_assoc_request *req)
f0706e82 5062{
66e67e41 5063 struct ieee80211_local *local = sdata->local;
77fdaa12 5064 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
0c1ad2ca 5065 struct ieee80211_bss *bss = (void *)req->bss->priv;
66e67e41 5066 struct ieee80211_mgd_assoc_data *assoc_data;
c65dd147 5067 const struct cfg80211_bss_ies *beacon_ies;
4e74bfdb 5068 struct ieee80211_supported_band *sband;
b08fbbd8 5069 const u8 *ssidie, *ht_ie, *vht_ie;
2a33bee2 5070 int i, err;
c1041f10 5071 bool override = false;
f0706e82 5072
66e67e41
JB
5073 assoc_data = kzalloc(sizeof(*assoc_data) + req->ie_len, GFP_KERNEL);
5074 if (!assoc_data)
5075 return -ENOMEM;
5076
9caf0364
JB
5077 rcu_read_lock();
5078 ssidie = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
5079 if (!ssidie) {
5080 rcu_read_unlock();
5081 kfree(assoc_data);
5082 return -EINVAL;
5083 }
5084 memcpy(assoc_data->ssid, ssidie + 2, ssidie[1]);
5085 assoc_data->ssid_len = ssidie[1];
5086 rcu_read_unlock();
5087
7b119dc0
JB
5088 if (ifmgd->associated) {
5089 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
5090
89f774e6
JB
5091 sdata_info(sdata,
5092 "disconnect from AP %pM for new assoc to %pM\n",
5093 ifmgd->associated->bssid, req->bss->bssid);
7b119dc0
JB
5094 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
5095 WLAN_REASON_UNSPECIFIED,
5096 false, frame_buf);
5097
a90faa9d
EG
5098 ieee80211_report_disconnect(sdata, frame_buf,
5099 sizeof(frame_buf), true,
5100 WLAN_REASON_UNSPECIFIED);
7b119dc0 5101 }
66e67e41
JB
5102
5103 if (ifmgd->auth_data && !ifmgd->auth_data->done) {
5104 err = -EBUSY;
5105 goto err_free;
af6b6374 5106 }
77fdaa12 5107
66e67e41
JB
5108 if (ifmgd->assoc_data) {
5109 err = -EBUSY;
5110 goto err_free;
5111 }
77fdaa12 5112
66e67e41
JB
5113 if (ifmgd->auth_data) {
5114 bool match;
5115
5116 /* keep sta info, bssid if matching */
b203ca39 5117 match = ether_addr_equal(ifmgd->bssid, req->bss->bssid);
66e67e41 5118 ieee80211_destroy_auth_data(sdata, match);
2a33bee2
GE
5119 }
5120
66e67e41 5121 /* prepare assoc data */
095d81ce 5122
d8ec4433
JO
5123 ifmgd->beacon_crc_valid = false;
5124
095d81ce
JB
5125 assoc_data->wmm = bss->wmm_used &&
5126 (local->hw.queues >= IEEE80211_NUM_ACS);
095d81ce 5127
de5036aa
JB
5128 /*
5129 * IEEE802.11n does not allow TKIP/WEP as pairwise ciphers in HT mode.
5130 * We still associate in non-HT mode (11a/b/g) if any one of these
5131 * ciphers is configured as pairwise.
5132 * We can set this to true for non-11n hardware, that'll be checked
5133 * separately along with the peer capabilities.
5134 */
1c4cb928 5135 for (i = 0; i < req->crypto.n_ciphers_pairwise; i++) {
77fdaa12
JB
5136 if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
5137 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
1c4cb928 5138 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104) {
a8243b72 5139 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
d545daba 5140 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
41cbb0f5 5141 ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
1c4cb928 5142 netdev_info(sdata->dev,
41cbb0f5 5143 "disabling HE/HT/VHT due to WEP/TKIP use\n");
1c4cb928
JB
5144 }
5145 }
77fdaa12 5146
4e74bfdb
JB
5147 /* Also disable HT if we don't support it or the AP doesn't use WMM */
5148 sband = local->hw.wiphy->bands[req->bss->channel->band];
5149 if (!sband->ht_cap.ht_supported ||
095d81ce
JB
5150 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used ||
5151 ifmgd->flags & IEEE80211_STA_DISABLE_WMM) {
a8243b72 5152 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
095d81ce
JB
5153 if (!bss->wmm_used &&
5154 !(ifmgd->flags & IEEE80211_STA_DISABLE_WMM))
1b49de26
JB
5155 netdev_info(sdata->dev,
5156 "disabling HT as WMM/QoS is not supported by the AP\n");
1c4cb928 5157 }
4e74bfdb 5158
d545daba
MP
5159 /* disable VHT if we don't support it or the AP doesn't use WMM */
5160 if (!sband->vht_cap.vht_supported ||
095d81ce
JB
5161 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used ||
5162 ifmgd->flags & IEEE80211_STA_DISABLE_WMM) {
d545daba 5163 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
095d81ce
JB
5164 if (!bss->wmm_used &&
5165 !(ifmgd->flags & IEEE80211_STA_DISABLE_WMM))
1b49de26
JB
5166 netdev_info(sdata->dev,
5167 "disabling VHT as WMM/QoS is not supported by the AP\n");
d545daba
MP
5168 }
5169
ef96a842
BG
5170 memcpy(&ifmgd->ht_capa, &req->ht_capa, sizeof(ifmgd->ht_capa));
5171 memcpy(&ifmgd->ht_capa_mask, &req->ht_capa_mask,
5172 sizeof(ifmgd->ht_capa_mask));
5173
dd5ecfea
JB
5174 memcpy(&ifmgd->vht_capa, &req->vht_capa, sizeof(ifmgd->vht_capa));
5175 memcpy(&ifmgd->vht_capa_mask, &req->vht_capa_mask,
5176 sizeof(ifmgd->vht_capa_mask));
5177
77fdaa12 5178 if (req->ie && req->ie_len) {
66e67e41
JB
5179 memcpy(assoc_data->ie, req->ie, req->ie_len);
5180 assoc_data->ie_len = req->ie_len;
5181 }
f679f65d 5182
39404fee
JM
5183 if (req->fils_kek) {
5184 /* should already be checked in cfg80211 - so warn */
5185 if (WARN_ON(req->fils_kek_len > FILS_MAX_KEK_LEN)) {
5186 err = -EINVAL;
5187 goto err_free;
5188 }
5189 memcpy(assoc_data->fils_kek, req->fils_kek,
5190 req->fils_kek_len);
5191 assoc_data->fils_kek_len = req->fils_kek_len;
5192 }
5193
5194 if (req->fils_nonces)
5195 memcpy(assoc_data->fils_nonces, req->fils_nonces,
5196 2 * FILS_NONCE_LEN);
5197
66e67e41 5198 assoc_data->bss = req->bss;
f679f65d 5199
af6b6374
JB
5200 if (ifmgd->req_smps == IEEE80211_SMPS_AUTOMATIC) {
5201 if (ifmgd->powersave)
04ecd257 5202 sdata->smps_mode = IEEE80211_SMPS_DYNAMIC;
af6b6374 5203 else
04ecd257 5204 sdata->smps_mode = IEEE80211_SMPS_OFF;
af6b6374 5205 } else
04ecd257 5206 sdata->smps_mode = ifmgd->req_smps;
af6b6374 5207
66e67e41 5208 assoc_data->capability = req->bss->capability;
66e67e41
JB
5209 assoc_data->supp_rates = bss->supp_rates;
5210 assoc_data->supp_rates_len = bss->supp_rates_len;
9dde6423 5211
9caf0364 5212 rcu_read_lock();
9dde6423
JB
5213 ht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_HT_OPERATION);
5214 if (ht_ie && ht_ie[1] >= sizeof(struct ieee80211_ht_operation))
5215 assoc_data->ap_ht_param =
5216 ((struct ieee80211_ht_operation *)(ht_ie + 2))->ht_param;
5217 else
a8243b72 5218 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
b08fbbd8
JB
5219 vht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_VHT_CAPABILITY);
5220 if (vht_ie && vht_ie[1] >= sizeof(struct ieee80211_vht_cap))
5221 memcpy(&assoc_data->ap_vht_cap, vht_ie + 2,
5222 sizeof(struct ieee80211_vht_cap));
5223 else
5224 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
9caf0364 5225 rcu_read_unlock();
63f170e0 5226
848955cc 5227 if (WARN((sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_UAPSD) &&
30686bf7 5228 ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK),
848955cc
JB
5229 "U-APSD not supported with HW_PS_NULLFUNC_STACK\n"))
5230 sdata->vif.driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
5231
ab13315a 5232 if (bss->wmm_used && bss->uapsd_supported &&
848955cc 5233 (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_UAPSD)) {
76f0303d 5234 assoc_data->uapsd = true;
ab13315a
KV
5235 ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED;
5236 } else {
76f0303d 5237 assoc_data->uapsd = false;
ab13315a
KV
5238 ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED;
5239 }
5240
77fdaa12 5241 if (req->prev_bssid)
66e67e41 5242 memcpy(assoc_data->prev_bssid, req->prev_bssid, ETH_ALEN);
77fdaa12
JB
5243
5244 if (req->use_mfp) {
5245 ifmgd->mfp = IEEE80211_MFP_REQUIRED;
5246 ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
5247 } else {
5248 ifmgd->mfp = IEEE80211_MFP_DISABLED;
5249 ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
5250 }
5251
cd2f5dd7
AK
5252 if (req->flags & ASSOC_REQ_USE_RRM)
5253 ifmgd->flags |= IEEE80211_STA_ENABLE_RRM;
5254 else
5255 ifmgd->flags &= ~IEEE80211_STA_ENABLE_RRM;
5256
77fdaa12
JB
5257 if (req->crypto.control_port)
5258 ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
5259 else
5260 ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
5261
a621fa4d
JB
5262 sdata->control_port_protocol = req->crypto.control_port_ethertype;
5263 sdata->control_port_no_encrypt = req->crypto.control_port_no_encrypt;
018f6fbf
DK
5264 sdata->control_port_over_nl80211 =
5265 req->crypto.control_port_over_nl80211;
2475b1cc
MS
5266 sdata->encrypt_headroom = ieee80211_cs_headroom(local, &req->crypto,
5267 sdata->vif.type);
a621fa4d 5268
66e67e41
JB
5269 /* kick off associate process */
5270
5271 ifmgd->assoc_data = assoc_data;
826262c3 5272 ifmgd->dtim_period = 0;
989c6505 5273 ifmgd->have_beacon = false;
66e67e41 5274
c1041f10
CRI
5275 /* override HT/VHT configuration only if the AP and we support it */
5276 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
5277 struct ieee80211_sta_ht_cap sta_ht_cap;
5278
5279 if (req->flags & ASSOC_REQ_DISABLE_HT)
5280 override = true;
5281
5282 memcpy(&sta_ht_cap, &sband->ht_cap, sizeof(sta_ht_cap));
5283 ieee80211_apply_htcap_overrides(sdata, &sta_ht_cap);
5284
5285 /* check for 40 MHz disable override */
5286 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_40MHZ) &&
5287 sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40 &&
5288 !(sta_ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40))
5289 override = true;
5290
5291 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
5292 req->flags & ASSOC_REQ_DISABLE_VHT)
5293 override = true;
5294 }
5295
5296 if (req->flags & ASSOC_REQ_DISABLE_HT) {
5297 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
5298 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
5299 }
5300
5301 if (req->flags & ASSOC_REQ_DISABLE_VHT)
5302 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
5303
5304 err = ieee80211_prep_connection(sdata, req->bss, true, override);
a1cf775d
JB
5305 if (err)
5306 goto err_clear;
66e67e41 5307
c65dd147
EG
5308 rcu_read_lock();
5309 beacon_ies = rcu_dereference(req->bss->beacon_ies);
826262c3 5310
30686bf7 5311 if (ieee80211_hw_check(&sdata->local->hw, NEED_DTIM_BEFORE_ASSOC) &&
c65dd147
EG
5312 !beacon_ies) {
5313 /*
5314 * Wait up to one beacon interval ...
5315 * should this be more if we miss one?
5316 */
5317 sdata_info(sdata, "waiting for beacon from %pM\n",
5318 ifmgd->bssid);
5319 assoc_data->timeout = TU_TO_EXP_TIME(req->bss->beacon_interval);
89afe614 5320 assoc_data->timeout_started = true;
c65dd147
EG
5321 assoc_data->need_beacon = true;
5322 } else if (beacon_ies) {
5323 const u8 *tim_ie = cfg80211_find_ie(WLAN_EID_TIM,
5324 beacon_ies->data,
5325 beacon_ies->len);
ef429dad
JB
5326 u8 dtim_count = 0;
5327
c65dd147
EG
5328 if (tim_ie && tim_ie[1] >= sizeof(struct ieee80211_tim_ie)) {
5329 const struct ieee80211_tim_ie *tim;
5330 tim = (void *)(tim_ie + 2);
5331 ifmgd->dtim_period = tim->dtim_period;
ef429dad 5332 dtim_count = tim->dtim_count;
826262c3 5333 }
989c6505 5334 ifmgd->have_beacon = true;
66e67e41 5335 assoc_data->timeout = jiffies;
89afe614 5336 assoc_data->timeout_started = true;
ef429dad 5337
30686bf7 5338 if (ieee80211_hw_check(&local->hw, TIMING_BEACON_ONLY)) {
ef429dad
JB
5339 sdata->vif.bss_conf.sync_tsf = beacon_ies->tsf;
5340 sdata->vif.bss_conf.sync_device_ts =
5341 bss->device_ts_beacon;
5342 sdata->vif.bss_conf.sync_dtim_count = dtim_count;
5343 }
c65dd147
EG
5344 } else {
5345 assoc_data->timeout = jiffies;
89afe614 5346 assoc_data->timeout_started = true;
66e67e41 5347 }
c65dd147
EG
5348 rcu_read_unlock();
5349
8d61ffa5 5350 run_again(sdata, assoc_data->timeout);
66e67e41 5351
fcff4f10
PS
5352 if (bss->corrupt_data) {
5353 char *corrupt_type = "data";
5354 if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_BEACON) {
5355 if (bss->corrupt_data &
5356 IEEE80211_BSS_CORRUPT_PROBE_RESP)
5357 corrupt_type = "beacon and probe response";
5358 else
5359 corrupt_type = "beacon";
5360 } else if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_PROBE_RESP)
5361 corrupt_type = "probe response";
bdcbd8e0
JB
5362 sdata_info(sdata, "associating with AP with corrupt %s\n",
5363 corrupt_type);
fcff4f10
PS
5364 }
5365
8d61ffa5 5366 return 0;
66e67e41 5367 err_clear:
c84a67a2 5368 eth_zero_addr(ifmgd->bssid);
3d2abdfd 5369 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
66e67e41
JB
5370 ifmgd->assoc_data = NULL;
5371 err_free:
5372 kfree(assoc_data);
66e67e41 5373 return err;
f0706e82
JB
5374}
5375
77fdaa12 5376int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
63c9c5e7 5377 struct cfg80211_deauth_request *req)
f0706e82 5378{
46900298 5379 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
6ae16775 5380 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
6863255b 5381 bool tx = !req->local_state_change;
f0706e82 5382
c9c3a060
JB
5383 if (ifmgd->auth_data &&
5384 ether_addr_equal(ifmgd->auth_data->bss->bssid, req->bssid)) {
5385 sdata_info(sdata,
5386 "aborting authentication with %pM by local choice (Reason: %u=%s)\n",
5387 req->bssid, req->reason_code,
5388 ieee80211_get_reason_code_string(req->reason_code));
0ff71613 5389
d4e36e55 5390 drv_mgd_prepare_tx(sdata->local, sdata, 0);
37ad3888
JB
5391 ieee80211_send_deauth_disassoc(sdata, req->bssid,
5392 IEEE80211_STYPE_DEAUTH,
6863255b 5393 req->reason_code, tx,
37ad3888 5394 frame_buf);
86552017 5395 ieee80211_destroy_auth_data(sdata, false);
a90faa9d
EG
5396 ieee80211_report_disconnect(sdata, frame_buf,
5397 sizeof(frame_buf), true,
5398 req->reason_code);
86552017 5399
c9c3a060 5400 return 0;
8c7d857c
EG
5401 }
5402
a64cba3c
AO
5403 if (ifmgd->assoc_data &&
5404 ether_addr_equal(ifmgd->assoc_data->bss->bssid, req->bssid)) {
5405 sdata_info(sdata,
5406 "aborting association with %pM by local choice (Reason: %u=%s)\n",
5407 req->bssid, req->reason_code,
5408 ieee80211_get_reason_code_string(req->reason_code));
5409
d4e36e55 5410 drv_mgd_prepare_tx(sdata->local, sdata, 0);
a64cba3c
AO
5411 ieee80211_send_deauth_disassoc(sdata, req->bssid,
5412 IEEE80211_STYPE_DEAUTH,
5413 req->reason_code, tx,
5414 frame_buf);
e6f462df 5415 ieee80211_destroy_assoc_data(sdata, false, true);
a64cba3c
AO
5416 ieee80211_report_disconnect(sdata, frame_buf,
5417 sizeof(frame_buf), true,
5418 req->reason_code);
5419 return 0;
5420 }
5421
86552017
JB
5422 if (ifmgd->associated &&
5423 ether_addr_equal(ifmgd->associated->bssid, req->bssid)) {
c9c3a060
JB
5424 sdata_info(sdata,
5425 "deauthenticating from %pM by local choice (Reason: %u=%s)\n",
5426 req->bssid, req->reason_code,
5427 ieee80211_get_reason_code_string(req->reason_code));
5428
86552017
JB
5429 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
5430 req->reason_code, tx, frame_buf);
a90faa9d
EG
5431 ieee80211_report_disconnect(sdata, frame_buf,
5432 sizeof(frame_buf), true,
5433 req->reason_code);
c9c3a060
JB
5434 return 0;
5435 }
86552017 5436
c9c3a060 5437 return -ENOTCONN;
f0706e82 5438}
84363e6e 5439
77fdaa12 5440int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
63c9c5e7 5441 struct cfg80211_disassoc_request *req)
9c6bd790 5442{
77fdaa12 5443 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
e69e95db 5444 u8 bssid[ETH_ALEN];
6ae16775 5445 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
9c6bd790 5446
8d8b261a
JB
5447 /*
5448 * cfg80211 should catch this ... but it's racy since
5449 * we can receive a disassoc frame, process it, hand it
5450 * to cfg80211 while that's in a locked section already
5451 * trying to tell us that the user wants to disconnect.
5452 */
8d61ffa5 5453 if (ifmgd->associated != req->bss)
77fdaa12 5454 return -ENOLINK;
77fdaa12 5455
bdcbd8e0 5456 sdata_info(sdata,
dfa1ad29
CO
5457 "disassociating from %pM by local choice (Reason: %u=%s)\n",
5458 req->bss->bssid, req->reason_code, ieee80211_get_reason_code_string(req->reason_code));
0ff71613 5459
e69e95db 5460 memcpy(bssid, req->bss->bssid, ETH_ALEN);
37ad3888
JB
5461 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DISASSOC,
5462 req->reason_code, !req->local_state_change,
5463 frame_buf);
10f644a4 5464
a90faa9d
EG
5465 ieee80211_report_disconnect(sdata, frame_buf, sizeof(frame_buf), true,
5466 req->reason_code);
bc83b681 5467
10f644a4
JB
5468 return 0;
5469}
026331c4 5470
afa762f6 5471void ieee80211_mgd_stop(struct ieee80211_sub_if_data *sdata)
54e4ffb2
JB
5472{
5473 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
5474
49921859
BG
5475 /*
5476 * Make sure some work items will not run after this,
5477 * they will not do anything but might not have been
5478 * cancelled when disconnecting.
5479 */
5480 cancel_work_sync(&ifmgd->monitor_work);
5481 cancel_work_sync(&ifmgd->beacon_connection_loss_work);
5482 cancel_work_sync(&ifmgd->request_smps_work);
5483 cancel_work_sync(&ifmgd->csa_connection_drop_work);
5484 cancel_work_sync(&ifmgd->chswitch_work);
81dd2b88 5485 cancel_delayed_work_sync(&ifmgd->tdls_peer_del_work);
49921859 5486
8d61ffa5 5487 sdata_lock(sdata);
959867fa
JB
5488 if (ifmgd->assoc_data) {
5489 struct cfg80211_bss *bss = ifmgd->assoc_data->bss;
e6f462df 5490 ieee80211_destroy_assoc_data(sdata, false, false);
959867fa
JB
5491 cfg80211_assoc_timeout(sdata->dev, bss);
5492 }
54e4ffb2
JB
5493 if (ifmgd->auth_data)
5494 ieee80211_destroy_auth_data(sdata, false);
1277b4a9
LK
5495 spin_lock_bh(&ifmgd->teardown_lock);
5496 if (ifmgd->teardown_skb) {
5497 kfree_skb(ifmgd->teardown_skb);
5498 ifmgd->teardown_skb = NULL;
5499 ifmgd->orig_teardown_skb = NULL;
5500 }
5501 spin_unlock_bh(&ifmgd->teardown_lock);
54e4ffb2 5502 del_timer_sync(&ifmgd->timer);
8d61ffa5 5503 sdata_unlock(sdata);
54e4ffb2
JB
5504}
5505
a97c13c3
JO
5506void ieee80211_cqm_rssi_notify(struct ieee80211_vif *vif,
5507 enum nl80211_cqm_rssi_threshold_event rssi_event,
769f07d8 5508 s32 rssi_level,
a97c13c3
JO
5509 gfp_t gfp)
5510{
5511 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
5512
769f07d8 5513 trace_api_cqm_rssi_notify(sdata, rssi_event, rssi_level);
b5878a2d 5514
bee427b8 5515 cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, rssi_level, gfp);
a97c13c3
JO
5516}
5517EXPORT_SYMBOL(ieee80211_cqm_rssi_notify);
98f03342
JB
5518
5519void ieee80211_cqm_beacon_loss_notify(struct ieee80211_vif *vif, gfp_t gfp)
5520{
5521 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
5522
5523 trace_api_cqm_beacon_loss_notify(sdata->local, sdata);
5524
5525 cfg80211_cqm_beacon_loss_notify(sdata->dev, gfp);
5526}
5527EXPORT_SYMBOL(ieee80211_cqm_beacon_loss_notify);