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