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