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