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