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