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