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