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