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