blob: 0dba353d3f8fe16c786f7cc383778e6a0b8a60f7 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-only
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * BSS client mode implementation
4 * Copyright 2003-2008, Jouni Malinen <j@w1.fi>
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>
9 * Copyright 2013-2014 Intel Mobile Communications GmbH
10 * Copyright (C) 2015 - 2017 Intel Deutschland GmbH
Olivier Deprez0e641232021-09-23 10:07:05 +020011 * Copyright (C) 2018 - 2020 Intel Corporation
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000012 */
13
14#include <linux/delay.h>
David Brazdil0f672f62019-12-10 10:32:29 +000015#include <linux/fips.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000016#include <linux/if_ether.h>
17#include <linux/skbuff.h>
18#include <linux/if_arp.h>
19#include <linux/etherdevice.h>
20#include <linux/moduleparam.h>
21#include <linux/rtnetlink.h>
22#include <linux/crc32.h>
23#include <linux/slab.h>
24#include <linux/export.h>
25#include <net/mac80211.h>
26#include <asm/unaligned.h>
27
28#include "ieee80211_i.h"
29#include "driver-ops.h"
30#include "rate.h"
31#include "led.h"
32#include "fils_aead.h"
33
34#define IEEE80211_AUTH_TIMEOUT (HZ / 5)
35#define IEEE80211_AUTH_TIMEOUT_LONG (HZ / 2)
36#define IEEE80211_AUTH_TIMEOUT_SHORT (HZ / 10)
37#define IEEE80211_AUTH_TIMEOUT_SAE (HZ * 2)
38#define IEEE80211_AUTH_MAX_TRIES 3
39#define IEEE80211_AUTH_WAIT_ASSOC (HZ * 5)
Olivier Deprez157378f2022-04-04 15:47:50 +020040#define IEEE80211_AUTH_WAIT_SAE_RETRY (HZ * 2)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000041#define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
42#define IEEE80211_ASSOC_TIMEOUT_LONG (HZ / 2)
43#define IEEE80211_ASSOC_TIMEOUT_SHORT (HZ / 10)
44#define IEEE80211_ASSOC_MAX_TRIES 3
45
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).");
55
56/*
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.
63 */
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.");
68
69/*
70 * Time the connection can be idle before we probe
71 * it to see if we can still talk to the AP.
72 */
73#define IEEE80211_CONNECTION_IDLE_TIME (30 * HZ)
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 */
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).");
84
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
91/*
92 * We can have multiple work items (and connection probing)
93 * scheduling this timer, but we need to take care to only
94 * reschedule it when it should fire _earlier_ than it was
95 * asked for before, or if it's not pending right now. This
96 * function ensures that. Note that it then is required to
97 * run this function for all timeouts after the first one
98 * has happened -- the work that runs from this timer will
99 * do that.
100 */
101static void run_again(struct ieee80211_sub_if_data *sdata,
102 unsigned long timeout)
103{
104 sdata_assert_lock(sdata);
105
106 if (!timer_pending(&sdata->u.mgd.timer) ||
107 time_before(timeout, sdata->u.mgd.timer.expires))
108 mod_timer(&sdata->u.mgd.timer, timeout);
109}
110
111void ieee80211_sta_reset_beacon_monitor(struct ieee80211_sub_if_data *sdata)
112{
113 if (sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)
114 return;
115
116 if (ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
117 return;
118
119 mod_timer(&sdata->u.mgd.bcn_mon_timer,
120 round_jiffies_up(jiffies + sdata->u.mgd.beacon_timeout));
121}
122
123void ieee80211_sta_reset_conn_monitor(struct ieee80211_sub_if_data *sdata)
124{
125 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
126
127 if (unlikely(!ifmgd->associated))
128 return;
129
130 if (ifmgd->probe_send_count)
131 ifmgd->probe_send_count = 0;
132
133 if (ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
134 return;
135
136 mod_timer(&ifmgd->conn_mon_timer,
137 round_jiffies_up(jiffies + IEEE80211_CONNECTION_IDLE_TIME));
138}
139
140static int ecw2cw(int ecw)
141{
142 return (1 << ecw) - 1;
143}
144
145static u32
146ieee80211_determine_chantype(struct ieee80211_sub_if_data *sdata,
147 struct ieee80211_supported_band *sband,
148 struct ieee80211_channel *channel,
Olivier Deprez157378f2022-04-04 15:47:50 +0200149 u32 vht_cap_info,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000150 const struct ieee80211_ht_operation *ht_oper,
151 const struct ieee80211_vht_operation *vht_oper,
152 const struct ieee80211_he_operation *he_oper,
Olivier Deprez157378f2022-04-04 15:47:50 +0200153 const struct ieee80211_s1g_oper_ie *s1g_oper,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000154 struct cfg80211_chan_def *chandef, bool tracking)
155{
156 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
157 struct cfg80211_chan_def vht_chandef;
158 struct ieee80211_sta_ht_cap sta_ht_cap;
159 u32 ht_cfreq, ret;
160
David Brazdil0f672f62019-12-10 10:32:29 +0000161 memset(chandef, 0, sizeof(struct cfg80211_chan_def));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000162 chandef->chan = channel;
163 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
164 chandef->center_freq1 = channel->center_freq;
Olivier Deprez157378f2022-04-04 15:47:50 +0200165 chandef->freq1_offset = channel->freq_offset;
166
167 if (channel->band == NL80211_BAND_6GHZ) {
168 if (!ieee80211_chandef_he_6ghz_oper(sdata, he_oper, chandef))
169 ret = IEEE80211_STA_DISABLE_HT |
170 IEEE80211_STA_DISABLE_VHT |
171 IEEE80211_STA_DISABLE_HE;
172 else
173 ret = 0;
174 vht_chandef = *chandef;
175 goto out;
176 } else if (sband->band == NL80211_BAND_S1GHZ) {
177 if (!ieee80211_chandef_s1g_oper(s1g_oper, chandef)) {
178 sdata_info(sdata,
179 "Missing S1G Operation Element? Trying operating == primary\n");
180 chandef->width = ieee80211_s1g_channel_width(channel);
181 }
182
183 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_40MHZ |
184 IEEE80211_STA_DISABLE_VHT |
185 IEEE80211_STA_DISABLE_80P80MHZ |
186 IEEE80211_STA_DISABLE_160MHZ;
187 goto out;
188 }
189
190 memcpy(&sta_ht_cap, &sband->ht_cap, sizeof(sta_ht_cap));
191 ieee80211_apply_htcap_overrides(sdata, &sta_ht_cap);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000192
193 if (!ht_oper || !sta_ht_cap.ht_supported) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200194 ret = IEEE80211_STA_DISABLE_HT |
195 IEEE80211_STA_DISABLE_VHT |
196 IEEE80211_STA_DISABLE_HE;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000197 goto out;
198 }
199
200 chandef->width = NL80211_CHAN_WIDTH_20;
201
202 ht_cfreq = ieee80211_channel_to_frequency(ht_oper->primary_chan,
203 channel->band);
204 /* check that channel matches the right operating channel */
205 if (!tracking && channel->center_freq != ht_cfreq) {
206 /*
207 * It's possible that some APs are confused here;
208 * Netgear WNDR3700 sometimes reports 4 higher than
209 * the actual channel in association responses, but
210 * since we look at probe response/beacon data here
211 * it should be OK.
212 */
213 sdata_info(sdata,
214 "Wrong control channel: center-freq: %d ht-cfreq: %d ht->primary_chan: %d band: %d - Disabling HT\n",
215 channel->center_freq, ht_cfreq,
216 ht_oper->primary_chan, channel->band);
Olivier Deprez157378f2022-04-04 15:47:50 +0200217 ret = IEEE80211_STA_DISABLE_HT |
218 IEEE80211_STA_DISABLE_VHT |
219 IEEE80211_STA_DISABLE_HE;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000220 goto out;
221 }
222
223 /* check 40 MHz support, if we have it */
224 if (sta_ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) {
225 ieee80211_chandef_ht_oper(ht_oper, chandef);
226 } else {
227 /* 40 MHz (and 80 MHz) must be supported for VHT */
228 ret = IEEE80211_STA_DISABLE_VHT;
229 /* also mark 40 MHz disabled */
230 ret |= IEEE80211_STA_DISABLE_40MHZ;
231 goto out;
232 }
233
234 if (!vht_oper || !sband->vht_cap.vht_supported) {
235 ret = IEEE80211_STA_DISABLE_VHT;
236 goto out;
237 }
238
239 vht_chandef = *chandef;
240 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE) && he_oper &&
241 (le32_to_cpu(he_oper->he_oper_params) &
242 IEEE80211_HE_OPERATION_VHT_OPER_INFO)) {
243 struct ieee80211_vht_operation he_oper_vht_cap;
244
245 /*
246 * Set only first 3 bytes (other 2 aren't used in
247 * ieee80211_chandef_vht_oper() anyway)
248 */
249 memcpy(&he_oper_vht_cap, he_oper->optional, 3);
250 he_oper_vht_cap.basic_mcs_set = cpu_to_le16(0);
251
Olivier Deprez157378f2022-04-04 15:47:50 +0200252 if (!ieee80211_chandef_vht_oper(&sdata->local->hw, vht_cap_info,
David Brazdil0f672f62019-12-10 10:32:29 +0000253 &he_oper_vht_cap, ht_oper,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000254 &vht_chandef)) {
255 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE))
256 sdata_info(sdata,
257 "HE AP VHT information is invalid, disable HE\n");
258 ret = IEEE80211_STA_DISABLE_HE;
259 goto out;
260 }
Olivier Deprez157378f2022-04-04 15:47:50 +0200261 } else if (!ieee80211_chandef_vht_oper(&sdata->local->hw,
262 vht_cap_info,
263 vht_oper, ht_oper,
264 &vht_chandef)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000265 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
266 sdata_info(sdata,
267 "AP VHT information is invalid, disable VHT\n");
268 ret = IEEE80211_STA_DISABLE_VHT;
269 goto out;
270 }
271
272 if (!cfg80211_chandef_valid(&vht_chandef)) {
273 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
274 sdata_info(sdata,
275 "AP VHT information is invalid, disable VHT\n");
276 ret = IEEE80211_STA_DISABLE_VHT;
277 goto out;
278 }
279
280 if (cfg80211_chandef_identical(chandef, &vht_chandef)) {
281 ret = 0;
282 goto out;
283 }
284
285 if (!cfg80211_chandef_compatible(chandef, &vht_chandef)) {
286 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
287 sdata_info(sdata,
288 "AP VHT information doesn't match HT, disable VHT\n");
289 ret = IEEE80211_STA_DISABLE_VHT;
290 goto out;
291 }
292
293 *chandef = vht_chandef;
294
295 ret = 0;
296
297out:
298 /*
299 * When tracking the current AP, don't do any further checks if the
300 * new chandef is identical to the one we're currently using for the
301 * connection. This keeps us from playing ping-pong with regulatory,
302 * without it the following can happen (for example):
303 * - connect to an AP with 80 MHz, world regdom allows 80 MHz
304 * - AP advertises regdom US
305 * - CRDA loads regdom US with 80 MHz prohibited (old database)
306 * - the code below detects an unsupported channel, downgrades, and
307 * we disconnect from the AP in the caller
308 * - disconnect causes CRDA to reload world regdomain and the game
309 * starts anew.
310 * (see https://bugzilla.kernel.org/show_bug.cgi?id=70881)
311 *
312 * It seems possible that there are still scenarios with CSA or real
313 * bandwidth changes where a this could happen, but those cases are
314 * less common and wouldn't completely prevent using the AP.
315 */
316 if (tracking &&
317 cfg80211_chandef_identical(chandef, &sdata->vif.bss_conf.chandef))
318 return ret;
319
320 /* don't print the message below for VHT mismatch if VHT is disabled */
321 if (ret & IEEE80211_STA_DISABLE_VHT)
322 vht_chandef = *chandef;
323
324 /*
325 * Ignore the DISABLED flag when we're already connected and only
326 * tracking the APs beacon for bandwidth changes - otherwise we
327 * might get disconnected here if we connect to an AP, update our
328 * regulatory information based on the AP's country IE and the
329 * information we have is wrong/outdated and disables the channel
330 * that we're actually using for the connection to the AP.
331 */
332 while (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
333 tracking ? 0 :
334 IEEE80211_CHAN_DISABLED)) {
335 if (WARN_ON(chandef->width == NL80211_CHAN_WIDTH_20_NOHT)) {
336 ret = IEEE80211_STA_DISABLE_HT |
Olivier Deprez157378f2022-04-04 15:47:50 +0200337 IEEE80211_STA_DISABLE_VHT |
338 IEEE80211_STA_DISABLE_HE;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000339 break;
340 }
341
342 ret |= ieee80211_chandef_downgrade(chandef);
343 }
344
Olivier Deprez157378f2022-04-04 15:47:50 +0200345 if (!he_oper || !cfg80211_chandef_usable(sdata->wdev.wiphy, chandef,
346 IEEE80211_CHAN_NO_HE))
347 ret |= IEEE80211_STA_DISABLE_HE;
348
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000349 if (chandef->width != vht_chandef.width && !tracking)
350 sdata_info(sdata,
351 "capabilities/regulatory prevented using AP HT/VHT configuration, downgraded\n");
352
353 WARN_ON_ONCE(!cfg80211_chandef_valid(chandef));
354 return ret;
355}
356
357static int ieee80211_config_bw(struct ieee80211_sub_if_data *sdata,
358 struct sta_info *sta,
359 const struct ieee80211_ht_cap *ht_cap,
Olivier Deprez157378f2022-04-04 15:47:50 +0200360 const struct ieee80211_vht_cap *vht_cap,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000361 const struct ieee80211_ht_operation *ht_oper,
362 const struct ieee80211_vht_operation *vht_oper,
363 const struct ieee80211_he_operation *he_oper,
Olivier Deprez157378f2022-04-04 15:47:50 +0200364 const struct ieee80211_s1g_oper_ie *s1g_oper,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000365 const u8 *bssid, u32 *changed)
366{
367 struct ieee80211_local *local = sdata->local;
368 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
369 struct ieee80211_channel *chan = sdata->vif.bss_conf.chandef.chan;
370 struct ieee80211_supported_band *sband =
371 local->hw.wiphy->bands[chan->band];
372 struct cfg80211_chan_def chandef;
373 u16 ht_opmode;
374 u32 flags;
375 enum ieee80211_sta_rx_bandwidth new_sta_bw;
Olivier Deprez157378f2022-04-04 15:47:50 +0200376 u32 vht_cap_info = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000377 int ret;
378
379 /* if HT was/is disabled, don't track any bandwidth changes */
380 if (ifmgd->flags & IEEE80211_STA_DISABLE_HT || !ht_oper)
381 return 0;
382
383 /* don't check VHT if we associated as non-VHT station */
384 if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
385 vht_oper = NULL;
386
387 /* don't check HE if we associated as non-HE station */
388 if (ifmgd->flags & IEEE80211_STA_DISABLE_HE ||
389 !ieee80211_get_he_sta_cap(sband))
390 he_oper = NULL;
391
392 if (WARN_ON_ONCE(!sta))
393 return -EINVAL;
394
395 /*
396 * if bss configuration changed store the new one -
397 * this may be applicable even if channel is identical
398 */
399 ht_opmode = le16_to_cpu(ht_oper->operation_mode);
400 if (sdata->vif.bss_conf.ht_operation_mode != ht_opmode) {
401 *changed |= BSS_CHANGED_HT;
402 sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
403 }
404
Olivier Deprez157378f2022-04-04 15:47:50 +0200405 if (vht_cap)
406 vht_cap_info = le32_to_cpu(vht_cap->vht_cap_info);
407
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000408 /* calculate new channel (type) based on HT/VHT/HE operation IEs */
Olivier Deprez157378f2022-04-04 15:47:50 +0200409 flags = ieee80211_determine_chantype(sdata, sband, chan, vht_cap_info,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000410 ht_oper, vht_oper, he_oper,
Olivier Deprez157378f2022-04-04 15:47:50 +0200411 s1g_oper, &chandef, true);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000412
413 /*
414 * Downgrade the new channel if we associated with restricted
415 * capabilities. For example, if we associated as a 20 MHz STA
416 * to a 40 MHz AP (due to regulatory, capabilities or config
417 * reasons) then switching to a 40 MHz channel now won't do us
418 * any good -- we couldn't use it with the AP.
419 */
420 if (ifmgd->flags & IEEE80211_STA_DISABLE_80P80MHZ &&
421 chandef.width == NL80211_CHAN_WIDTH_80P80)
422 flags |= ieee80211_chandef_downgrade(&chandef);
423 if (ifmgd->flags & IEEE80211_STA_DISABLE_160MHZ &&
424 chandef.width == NL80211_CHAN_WIDTH_160)
425 flags |= ieee80211_chandef_downgrade(&chandef);
426 if (ifmgd->flags & IEEE80211_STA_DISABLE_40MHZ &&
427 chandef.width > NL80211_CHAN_WIDTH_20)
428 flags |= ieee80211_chandef_downgrade(&chandef);
429
430 if (cfg80211_chandef_identical(&chandef, &sdata->vif.bss_conf.chandef))
431 return 0;
432
433 sdata_info(sdata,
Olivier Deprez157378f2022-04-04 15:47:50 +0200434 "AP %pM changed bandwidth, new config is %d.%03d MHz, "
435 "width %d (%d.%03d/%d MHz)\n",
436 ifmgd->bssid, chandef.chan->center_freq,
437 chandef.chan->freq_offset, chandef.width,
438 chandef.center_freq1, chandef.freq1_offset,
439 chandef.center_freq2);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000440
441 if (flags != (ifmgd->flags & (IEEE80211_STA_DISABLE_HT |
442 IEEE80211_STA_DISABLE_VHT |
Olivier Deprez157378f2022-04-04 15:47:50 +0200443 IEEE80211_STA_DISABLE_HE |
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000444 IEEE80211_STA_DISABLE_40MHZ |
445 IEEE80211_STA_DISABLE_80P80MHZ |
446 IEEE80211_STA_DISABLE_160MHZ)) ||
447 !cfg80211_chandef_valid(&chandef)) {
448 sdata_info(sdata,
449 "AP %pM changed bandwidth in a way we can't support - disconnect\n",
450 ifmgd->bssid);
451 return -EINVAL;
452 }
453
454 switch (chandef.width) {
455 case NL80211_CHAN_WIDTH_20_NOHT:
456 case NL80211_CHAN_WIDTH_20:
457 new_sta_bw = IEEE80211_STA_RX_BW_20;
458 break;
459 case NL80211_CHAN_WIDTH_40:
460 new_sta_bw = IEEE80211_STA_RX_BW_40;
461 break;
462 case NL80211_CHAN_WIDTH_80:
463 new_sta_bw = IEEE80211_STA_RX_BW_80;
464 break;
465 case NL80211_CHAN_WIDTH_80P80:
466 case NL80211_CHAN_WIDTH_160:
467 new_sta_bw = IEEE80211_STA_RX_BW_160;
468 break;
469 default:
470 return -EINVAL;
471 }
472
473 if (new_sta_bw > sta->cur_max_bandwidth)
474 new_sta_bw = sta->cur_max_bandwidth;
475
476 if (new_sta_bw < sta->sta.bandwidth) {
477 sta->sta.bandwidth = new_sta_bw;
478 rate_control_rate_update(local, sband, sta,
479 IEEE80211_RC_BW_CHANGED);
480 }
481
482 ret = ieee80211_vif_change_bandwidth(sdata, &chandef, changed);
483 if (ret) {
484 sdata_info(sdata,
485 "AP %pM changed bandwidth to incompatible one - disconnect\n",
486 ifmgd->bssid);
487 return ret;
488 }
489
490 if (new_sta_bw > sta->sta.bandwidth) {
491 sta->sta.bandwidth = new_sta_bw;
492 rate_control_rate_update(local, sband, sta,
493 IEEE80211_RC_BW_CHANGED);
494 }
495
496 return 0;
497}
498
499/* frame sending functions */
500
501static void ieee80211_add_ht_ie(struct ieee80211_sub_if_data *sdata,
502 struct sk_buff *skb, u8 ap_ht_param,
503 struct ieee80211_supported_band *sband,
504 struct ieee80211_channel *channel,
505 enum ieee80211_smps_mode smps)
506{
507 u8 *pos;
508 u32 flags = channel->flags;
509 u16 cap;
510 struct ieee80211_sta_ht_cap ht_cap;
511
512 BUILD_BUG_ON(sizeof(ht_cap) != sizeof(sband->ht_cap));
513
514 memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
515 ieee80211_apply_htcap_overrides(sdata, &ht_cap);
516
517 /* determine capability flags */
518 cap = ht_cap.cap;
519
520 switch (ap_ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
521 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
522 if (flags & IEEE80211_CHAN_NO_HT40PLUS) {
523 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
524 cap &= ~IEEE80211_HT_CAP_SGI_40;
525 }
526 break;
527 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
528 if (flags & IEEE80211_CHAN_NO_HT40MINUS) {
529 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
530 cap &= ~IEEE80211_HT_CAP_SGI_40;
531 }
532 break;
533 }
534
535 /*
536 * If 40 MHz was disabled associate as though we weren't
537 * capable of 40 MHz -- some broken APs will never fall
538 * back to trying to transmit in 20 MHz.
539 */
540 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_40MHZ) {
541 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
542 cap &= ~IEEE80211_HT_CAP_SGI_40;
543 }
544
545 /* set SM PS mode properly */
546 cap &= ~IEEE80211_HT_CAP_SM_PS;
547 switch (smps) {
548 case IEEE80211_SMPS_AUTOMATIC:
549 case IEEE80211_SMPS_NUM_MODES:
550 WARN_ON(1);
Olivier Deprez157378f2022-04-04 15:47:50 +0200551 fallthrough;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000552 case IEEE80211_SMPS_OFF:
553 cap |= WLAN_HT_CAP_SM_PS_DISABLED <<
554 IEEE80211_HT_CAP_SM_PS_SHIFT;
555 break;
556 case IEEE80211_SMPS_STATIC:
557 cap |= WLAN_HT_CAP_SM_PS_STATIC <<
558 IEEE80211_HT_CAP_SM_PS_SHIFT;
559 break;
560 case IEEE80211_SMPS_DYNAMIC:
561 cap |= WLAN_HT_CAP_SM_PS_DYNAMIC <<
562 IEEE80211_HT_CAP_SM_PS_SHIFT;
563 break;
564 }
565
566 /* reserve and fill IE */
567 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
568 ieee80211_ie_build_ht_cap(pos, &ht_cap, cap);
569}
570
571/* This function determines vht capability flags for the association
572 * and builds the IE.
573 * Note - the function may set the owner of the MU-MIMO capability
574 */
575static void ieee80211_add_vht_ie(struct ieee80211_sub_if_data *sdata,
576 struct sk_buff *skb,
577 struct ieee80211_supported_band *sband,
578 struct ieee80211_vht_cap *ap_vht_cap)
579{
580 struct ieee80211_local *local = sdata->local;
581 u8 *pos;
582 u32 cap;
583 struct ieee80211_sta_vht_cap vht_cap;
584 u32 mask, ap_bf_sts, our_bf_sts;
585
586 BUILD_BUG_ON(sizeof(vht_cap) != sizeof(sband->vht_cap));
587
588 memcpy(&vht_cap, &sband->vht_cap, sizeof(vht_cap));
589 ieee80211_apply_vhtcap_overrides(sdata, &vht_cap);
590
591 /* determine capability flags */
592 cap = vht_cap.cap;
593
594 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_80P80MHZ) {
595 u32 bw = cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
596
597 cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
598 if (bw == IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ ||
599 bw == IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ)
600 cap |= IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
601 }
602
603 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_160MHZ) {
604 cap &= ~IEEE80211_VHT_CAP_SHORT_GI_160;
605 cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
606 }
607
608 /*
609 * Some APs apparently get confused if our capabilities are better
610 * than theirs, so restrict what we advertise in the assoc request.
611 */
612 if (!(ap_vht_cap->vht_cap_info &
613 cpu_to_le32(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)))
614 cap &= ~(IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
615 IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE);
616 else if (!(ap_vht_cap->vht_cap_info &
617 cpu_to_le32(IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)))
618 cap &= ~IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE;
619
620 /*
621 * If some other vif is using the MU-MIMO capablity we cannot associate
622 * using MU-MIMO - this will lead to contradictions in the group-id
623 * mechanism.
624 * Ownership is defined since association request, in order to avoid
625 * simultaneous associations with MU-MIMO.
626 */
627 if (cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE) {
628 bool disable_mu_mimo = false;
629 struct ieee80211_sub_if_data *other;
630
631 list_for_each_entry_rcu(other, &local->interfaces, list) {
632 if (other->vif.mu_mimo_owner) {
633 disable_mu_mimo = true;
634 break;
635 }
636 }
637 if (disable_mu_mimo)
638 cap &= ~IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE;
639 else
640 sdata->vif.mu_mimo_owner = true;
641 }
642
643 mask = IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
644
645 ap_bf_sts = le32_to_cpu(ap_vht_cap->vht_cap_info) & mask;
646 our_bf_sts = cap & mask;
647
648 if (ap_bf_sts < our_bf_sts) {
649 cap &= ~mask;
650 cap |= ap_bf_sts;
651 }
652
653 /* reserve and fill IE */
654 pos = skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2);
655 ieee80211_ie_build_vht_cap(pos, &vht_cap, cap);
656}
657
658/* This function determines HE capability flags for the association
659 * and builds the IE.
660 */
661static void ieee80211_add_he_ie(struct ieee80211_sub_if_data *sdata,
662 struct sk_buff *skb,
663 struct ieee80211_supported_band *sband)
664{
665 u8 *pos;
666 const struct ieee80211_sta_he_cap *he_cap = NULL;
Olivier Deprez157378f2022-04-04 15:47:50 +0200667 struct ieee80211_chanctx_conf *chanctx_conf;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000668 u8 he_cap_size;
Olivier Deprez157378f2022-04-04 15:47:50 +0200669 bool reg_cap = false;
670
671 rcu_read_lock();
672 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
673 if (!WARN_ON_ONCE(!chanctx_conf))
674 reg_cap = cfg80211_chandef_usable(sdata->wdev.wiphy,
675 &chanctx_conf->def,
676 IEEE80211_CHAN_NO_HE);
677
678 rcu_read_unlock();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000679
680 he_cap = ieee80211_get_he_sta_cap(sband);
Olivier Deprez157378f2022-04-04 15:47:50 +0200681 if (!he_cap || !reg_cap)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000682 return;
683
684 /*
685 * TODO: the 1 added is because this temporarily is under the EXTENSION
686 * IE. Get rid of it when it moves.
687 */
688 he_cap_size =
689 2 + 1 + sizeof(he_cap->he_cap_elem) +
690 ieee80211_he_mcs_nss_size(&he_cap->he_cap_elem) +
691 ieee80211_he_ppe_size(he_cap->ppe_thres[0],
692 he_cap->he_cap_elem.phy_cap_info);
693 pos = skb_put(skb, he_cap_size);
694 ieee80211_ie_build_he_cap(pos, he_cap, pos + he_cap_size);
Olivier Deprez157378f2022-04-04 15:47:50 +0200695
696 ieee80211_ie_build_he_6ghz_cap(sdata, skb);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000697}
698
699static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata)
700{
701 struct ieee80211_local *local = sdata->local;
702 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
703 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
704 struct sk_buff *skb;
705 struct ieee80211_mgmt *mgmt;
David Brazdil0f672f62019-12-10 10:32:29 +0000706 u8 *pos, qos_info, *ie_start;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000707 size_t offset = 0, noffset;
708 int i, count, rates_len, supp_rates_len, shift;
709 u16 capab;
710 struct ieee80211_supported_band *sband;
711 struct ieee80211_chanctx_conf *chanctx_conf;
712 struct ieee80211_channel *chan;
713 u32 rates = 0;
Olivier Deprez157378f2022-04-04 15:47:50 +0200714 __le16 listen_int;
715 struct element *ext_capa = NULL;
716
717 /* we know it's writable, cast away the const */
718 if (assoc_data->ie_len)
719 ext_capa = (void *)cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY,
720 assoc_data->ie,
721 assoc_data->ie_len);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000722
723 sdata_assert_lock(sdata);
724
725 rcu_read_lock();
726 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
727 if (WARN_ON(!chanctx_conf)) {
728 rcu_read_unlock();
729 return;
730 }
731 chan = chanctx_conf->def.chan;
732 rcu_read_unlock();
733 sband = local->hw.wiphy->bands[chan->band];
734 shift = ieee80211_vif_get_shift(&sdata->vif);
735
736 if (assoc_data->supp_rates_len) {
737 /*
738 * Get all rates supported by the device and the AP as
739 * some APs don't like getting a superset of their rates
740 * in the association request (e.g. D-Link DAP 1353 in
741 * b-only mode)...
742 */
743 rates_len = ieee80211_parse_bitrates(&chanctx_conf->def, sband,
744 assoc_data->supp_rates,
745 assoc_data->supp_rates_len,
746 &rates);
747 } else {
748 /*
749 * In case AP not provide any supported rates information
750 * before association, we send information element(s) with
751 * all rates that we support.
752 */
753 rates_len = 0;
754 for (i = 0; i < sband->n_bitrates; i++) {
755 rates |= BIT(i);
756 rates_len++;
757 }
758 }
759
760 skb = alloc_skb(local->hw.extra_tx_headroom +
761 sizeof(*mgmt) + /* bit too much but doesn't matter */
762 2 + assoc_data->ssid_len + /* SSID */
763 4 + rates_len + /* (extended) rates */
764 4 + /* power capability */
765 2 + 2 * sband->n_channels + /* supported channels */
766 2 + sizeof(struct ieee80211_ht_cap) + /* HT */
767 2 + sizeof(struct ieee80211_vht_cap) + /* VHT */
768 2 + 1 + sizeof(struct ieee80211_he_cap_elem) + /* HE */
769 sizeof(struct ieee80211_he_mcs_nss_supp) +
770 IEEE80211_HE_PPE_THRES_MAX_LEN +
Olivier Deprez157378f2022-04-04 15:47:50 +0200771 2 + 1 + sizeof(struct ieee80211_he_6ghz_capa) +
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000772 assoc_data->ie_len + /* extra IEs */
773 (assoc_data->fils_kek_len ? 16 /* AES-SIV */ : 0) +
774 9, /* WMM */
775 GFP_KERNEL);
776 if (!skb)
777 return;
778
779 skb_reserve(skb, local->hw.extra_tx_headroom);
780
781 capab = WLAN_CAPABILITY_ESS;
782
783 if (sband->band == NL80211_BAND_2GHZ) {
784 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
785 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
786 }
787
788 if (assoc_data->capability & WLAN_CAPABILITY_PRIVACY)
789 capab |= WLAN_CAPABILITY_PRIVACY;
790
791 if ((assoc_data->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
792 ieee80211_hw_check(&local->hw, SPECTRUM_MGMT))
793 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
794
795 if (ifmgd->flags & IEEE80211_STA_ENABLE_RRM)
796 capab |= WLAN_CAPABILITY_RADIO_MEASURE;
797
798 mgmt = skb_put_zero(skb, 24);
799 memcpy(mgmt->da, assoc_data->bss->bssid, ETH_ALEN);
800 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
801 memcpy(mgmt->bssid, assoc_data->bss->bssid, ETH_ALEN);
802
Olivier Deprez157378f2022-04-04 15:47:50 +0200803 listen_int = cpu_to_le16(sband->band == NL80211_BAND_S1GHZ ?
804 ieee80211_encode_usf(local->hw.conf.listen_interval) :
805 local->hw.conf.listen_interval);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000806 if (!is_zero_ether_addr(assoc_data->prev_bssid)) {
807 skb_put(skb, 10);
808 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
809 IEEE80211_STYPE_REASSOC_REQ);
810 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
Olivier Deprez157378f2022-04-04 15:47:50 +0200811 mgmt->u.reassoc_req.listen_interval = listen_int;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000812 memcpy(mgmt->u.reassoc_req.current_ap, assoc_data->prev_bssid,
813 ETH_ALEN);
814 } else {
815 skb_put(skb, 4);
816 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
817 IEEE80211_STYPE_ASSOC_REQ);
818 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
Olivier Deprez157378f2022-04-04 15:47:50 +0200819 mgmt->u.assoc_req.listen_interval = listen_int;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000820 }
821
822 /* SSID */
823 pos = skb_put(skb, 2 + assoc_data->ssid_len);
David Brazdil0f672f62019-12-10 10:32:29 +0000824 ie_start = pos;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000825 *pos++ = WLAN_EID_SSID;
826 *pos++ = assoc_data->ssid_len;
827 memcpy(pos, assoc_data->ssid, assoc_data->ssid_len);
828
Olivier Deprez157378f2022-04-04 15:47:50 +0200829 if (sband->band == NL80211_BAND_S1GHZ)
830 goto skip_rates;
831
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000832 /* add all rates which were marked to be used above */
833 supp_rates_len = rates_len;
834 if (supp_rates_len > 8)
835 supp_rates_len = 8;
836
837 pos = skb_put(skb, supp_rates_len + 2);
838 *pos++ = WLAN_EID_SUPP_RATES;
839 *pos++ = supp_rates_len;
840
841 count = 0;
842 for (i = 0; i < sband->n_bitrates; i++) {
843 if (BIT(i) & rates) {
844 int rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
845 5 * (1 << shift));
846 *pos++ = (u8) rate;
847 if (++count == 8)
848 break;
849 }
850 }
851
852 if (rates_len > count) {
853 pos = skb_put(skb, rates_len - count + 2);
854 *pos++ = WLAN_EID_EXT_SUPP_RATES;
855 *pos++ = rates_len - count;
856
857 for (i++; i < sband->n_bitrates; i++) {
858 if (BIT(i) & rates) {
859 int rate;
860 rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
861 5 * (1 << shift));
862 *pos++ = (u8) rate;
863 }
864 }
865 }
866
Olivier Deprez157378f2022-04-04 15:47:50 +0200867skip_rates:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000868 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT ||
869 capab & WLAN_CAPABILITY_RADIO_MEASURE) {
870 pos = skb_put(skb, 4);
871 *pos++ = WLAN_EID_PWR_CAPABILITY;
872 *pos++ = 2;
873 *pos++ = 0; /* min tx power */
874 /* max tx power */
875 *pos++ = ieee80211_chandef_max_power(&chanctx_conf->def);
876 }
877
Olivier Deprez157378f2022-04-04 15:47:50 +0200878 /*
879 * Per spec, we shouldn't include the list of channels if we advertise
880 * support for extended channel switching, but we've always done that;
881 * (for now?) apply this restriction only on the (new) 6 GHz band.
882 */
883 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT &&
884 (sband->band != NL80211_BAND_6GHZ ||
885 !ext_capa || ext_capa->datalen < 1 ||
886 !(ext_capa->data[0] & WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING))) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000887 /* TODO: get this in reg domain format */
888 pos = skb_put(skb, 2 * sband->n_channels + 2);
889 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
890 *pos++ = 2 * sband->n_channels;
891 for (i = 0; i < sband->n_channels; i++) {
892 *pos++ = ieee80211_frequency_to_channel(
893 sband->channels[i].center_freq);
894 *pos++ = 1; /* one channel in the subband*/
895 }
896 }
897
David Brazdil0f672f62019-12-10 10:32:29 +0000898 /* Set MBSSID support for HE AP if needed */
899 if (ieee80211_hw_check(&local->hw, SUPPORTS_ONLY_HE_MULTI_BSSID) &&
Olivier Deprez157378f2022-04-04 15:47:50 +0200900 !(ifmgd->flags & IEEE80211_STA_DISABLE_HE) && assoc_data->ie_len &&
901 ext_capa && ext_capa->datalen >= 3)
902 ext_capa->data[2] |= WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT;
David Brazdil0f672f62019-12-10 10:32:29 +0000903
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000904 /* if present, add any custom IEs that go before HT */
905 if (assoc_data->ie_len) {
906 static const u8 before_ht[] = {
907 WLAN_EID_SSID,
908 WLAN_EID_SUPP_RATES,
909 WLAN_EID_EXT_SUPP_RATES,
910 WLAN_EID_PWR_CAPABILITY,
911 WLAN_EID_SUPPORTED_CHANNELS,
912 WLAN_EID_RSN,
913 WLAN_EID_QOS_CAPA,
914 WLAN_EID_RRM_ENABLED_CAPABILITIES,
915 WLAN_EID_MOBILITY_DOMAIN,
916 WLAN_EID_FAST_BSS_TRANSITION, /* reassoc only */
917 WLAN_EID_RIC_DATA, /* reassoc only */
918 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
919 };
920 static const u8 after_ric[] = {
921 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
922 WLAN_EID_HT_CAPABILITY,
923 WLAN_EID_BSS_COEX_2040,
924 /* luckily this is almost always there */
925 WLAN_EID_EXT_CAPABILITY,
926 WLAN_EID_QOS_TRAFFIC_CAPA,
927 WLAN_EID_TIM_BCAST_REQ,
928 WLAN_EID_INTERWORKING,
929 /* 60 GHz (Multi-band, DMG, MMS) can't happen */
930 WLAN_EID_VHT_CAPABILITY,
931 WLAN_EID_OPMODE_NOTIF,
932 };
933
934 noffset = ieee80211_ie_split_ric(assoc_data->ie,
935 assoc_data->ie_len,
936 before_ht,
937 ARRAY_SIZE(before_ht),
938 after_ric,
939 ARRAY_SIZE(after_ric),
940 offset);
941 skb_put_data(skb, assoc_data->ie + offset, noffset - offset);
942 offset = noffset;
943 }
944
945 if (WARN_ON_ONCE((ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
946 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)))
947 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
948
Olivier Deprez157378f2022-04-04 15:47:50 +0200949 if (sband->band != NL80211_BAND_6GHZ &&
950 !(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000951 ieee80211_add_ht_ie(sdata, skb, assoc_data->ap_ht_param,
952 sband, chan, sdata->smps_mode);
953
954 /* if present, add any custom IEs that go before VHT */
955 if (assoc_data->ie_len) {
956 static const u8 before_vht[] = {
957 /*
958 * no need to list the ones split off before HT
959 * or generated here
960 */
961 WLAN_EID_BSS_COEX_2040,
962 WLAN_EID_EXT_CAPABILITY,
963 WLAN_EID_QOS_TRAFFIC_CAPA,
964 WLAN_EID_TIM_BCAST_REQ,
965 WLAN_EID_INTERWORKING,
966 /* 60 GHz (Multi-band, DMG, MMS) can't happen */
967 };
968
969 /* RIC already taken above, so no need to handle here anymore */
970 noffset = ieee80211_ie_split(assoc_data->ie, assoc_data->ie_len,
971 before_vht, ARRAY_SIZE(before_vht),
972 offset);
973 skb_put_data(skb, assoc_data->ie + offset, noffset - offset);
974 offset = noffset;
975 }
976
977 /* if present, add any custom IEs that go before HE */
978 if (assoc_data->ie_len) {
979 static const u8 before_he[] = {
980 /*
981 * no need to list the ones split off before VHT
982 * or generated here
983 */
984 WLAN_EID_OPMODE_NOTIF,
985 WLAN_EID_EXTENSION, WLAN_EID_EXT_FUTURE_CHAN_GUIDANCE,
986 /* 11ai elements */
987 WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_SESSION,
988 WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_PUBLIC_KEY,
989 WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_KEY_CONFIRM,
990 WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_HLP_CONTAINER,
991 WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_IP_ADDR_ASSIGN,
992 /* TODO: add 11ah/11aj/11ak elements */
993 };
994
995 /* RIC already taken above, so no need to handle here anymore */
996 noffset = ieee80211_ie_split(assoc_data->ie, assoc_data->ie_len,
997 before_he, ARRAY_SIZE(before_he),
998 offset);
999 pos = skb_put(skb, noffset - offset);
1000 memcpy(pos, assoc_data->ie + offset, noffset - offset);
1001 offset = noffset;
1002 }
1003
Olivier Deprez157378f2022-04-04 15:47:50 +02001004 if (sband->band != NL80211_BAND_6GHZ &&
1005 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001006 ieee80211_add_vht_ie(sdata, skb, sband,
1007 &assoc_data->ap_vht_cap);
1008
David Brazdil0f672f62019-12-10 10:32:29 +00001009 /*
1010 * If AP doesn't support HT, mark HE as disabled.
1011 * If on the 5GHz band, make sure it supports VHT.
1012 */
1013 if (ifmgd->flags & IEEE80211_STA_DISABLE_HT ||
1014 (sband->band == NL80211_BAND_5GHZ &&
1015 ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
1016 ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
1017
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001018 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE))
1019 ieee80211_add_he_ie(sdata, skb, sband);
1020
1021 /* if present, add any custom non-vendor IEs that go after HE */
1022 if (assoc_data->ie_len) {
1023 noffset = ieee80211_ie_split_vendor(assoc_data->ie,
1024 assoc_data->ie_len,
1025 offset);
1026 skb_put_data(skb, assoc_data->ie + offset, noffset - offset);
1027 offset = noffset;
1028 }
1029
1030 if (assoc_data->wmm) {
1031 if (assoc_data->uapsd) {
1032 qos_info = ifmgd->uapsd_queues;
1033 qos_info |= (ifmgd->uapsd_max_sp_len <<
1034 IEEE80211_WMM_IE_STA_QOSINFO_SP_SHIFT);
1035 } else {
1036 qos_info = 0;
1037 }
1038
1039 pos = ieee80211_add_wmm_info_ie(skb_put(skb, 9), qos_info);
1040 }
1041
Olivier Deprez157378f2022-04-04 15:47:50 +02001042 if (sband->band == NL80211_BAND_S1GHZ) {
1043 ieee80211_add_aid_request_ie(sdata, skb);
1044 ieee80211_add_s1g_capab_ie(sdata, &sband->s1g_cap, skb);
1045 }
1046
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001047 /* add any remaining custom (i.e. vendor specific here) IEs */
1048 if (assoc_data->ie_len) {
1049 noffset = assoc_data->ie_len;
1050 skb_put_data(skb, assoc_data->ie + offset, noffset - offset);
1051 }
1052
1053 if (assoc_data->fils_kek_len &&
1054 fils_encrypt_assoc_req(skb, assoc_data) < 0) {
1055 dev_kfree_skb(skb);
1056 return;
1057 }
1058
David Brazdil0f672f62019-12-10 10:32:29 +00001059 pos = skb_tail_pointer(skb);
1060 kfree(ifmgd->assoc_req_ies);
1061 ifmgd->assoc_req_ies = kmemdup(ie_start, pos - ie_start, GFP_ATOMIC);
1062 ifmgd->assoc_req_ies_len = pos - ie_start;
1063
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001064 drv_mgd_prepare_tx(local, sdata, 0);
1065
1066 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
1067 if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
1068 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS |
1069 IEEE80211_TX_INTFL_MLME_CONN_TX;
1070 ieee80211_tx_skb(sdata, skb);
1071}
1072
1073void ieee80211_send_pspoll(struct ieee80211_local *local,
1074 struct ieee80211_sub_if_data *sdata)
1075{
1076 struct ieee80211_pspoll *pspoll;
1077 struct sk_buff *skb;
1078
1079 skb = ieee80211_pspoll_get(&local->hw, &sdata->vif);
1080 if (!skb)
1081 return;
1082
1083 pspoll = (struct ieee80211_pspoll *) skb->data;
1084 pspoll->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
1085
1086 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
1087 ieee80211_tx_skb(sdata, skb);
1088}
1089
1090void ieee80211_send_nullfunc(struct ieee80211_local *local,
1091 struct ieee80211_sub_if_data *sdata,
1092 bool powersave)
1093{
1094 struct sk_buff *skb;
1095 struct ieee80211_hdr_3addr *nullfunc;
1096 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1097
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001098 skb = ieee80211_nullfunc_get(&local->hw, &sdata->vif,
1099 !ieee80211_hw_check(&local->hw, DOESNT_SUPPORT_QOS_NDP));
1100 if (!skb)
1101 return;
1102
1103 nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
1104 if (powersave)
1105 nullfunc->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
1106
1107 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
1108 IEEE80211_TX_INTFL_OFFCHAN_TX_OK;
1109
1110 if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
1111 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
1112
1113 if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL)
1114 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_USE_MINRATE;
1115
1116 ieee80211_tx_skb(sdata, skb);
1117}
1118
Olivier Deprez157378f2022-04-04 15:47:50 +02001119void ieee80211_send_4addr_nullfunc(struct ieee80211_local *local,
1120 struct ieee80211_sub_if_data *sdata)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001121{
1122 struct sk_buff *skb;
1123 struct ieee80211_hdr *nullfunc;
1124 __le16 fc;
1125
1126 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
1127 return;
1128
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001129 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 30);
1130 if (!skb)
1131 return;
1132
1133 skb_reserve(skb, local->hw.extra_tx_headroom);
1134
1135 nullfunc = skb_put_zero(skb, 30);
1136 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
1137 IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
1138 nullfunc->frame_control = fc;
1139 memcpy(nullfunc->addr1, sdata->u.mgd.bssid, ETH_ALEN);
1140 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1141 memcpy(nullfunc->addr3, sdata->u.mgd.bssid, ETH_ALEN);
1142 memcpy(nullfunc->addr4, sdata->vif.addr, ETH_ALEN);
1143
1144 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
1145 ieee80211_tx_skb(sdata, skb);
1146}
1147
1148/* spectrum management related things */
1149static void ieee80211_chswitch_work(struct work_struct *work)
1150{
1151 struct ieee80211_sub_if_data *sdata =
1152 container_of(work, struct ieee80211_sub_if_data, u.mgd.chswitch_work);
1153 struct ieee80211_local *local = sdata->local;
1154 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1155 int ret;
1156
1157 if (!ieee80211_sdata_running(sdata))
1158 return;
1159
1160 sdata_lock(sdata);
1161 mutex_lock(&local->mtx);
1162 mutex_lock(&local->chanctx_mtx);
1163
1164 if (!ifmgd->associated)
1165 goto out;
1166
1167 if (!sdata->vif.csa_active)
1168 goto out;
1169
1170 /*
1171 * using reservation isn't immediate as it may be deferred until later
1172 * with multi-vif. once reservation is complete it will re-schedule the
1173 * work with no reserved_chanctx so verify chandef to check if it
1174 * completed successfully
1175 */
1176
1177 if (sdata->reserved_chanctx) {
1178 struct ieee80211_supported_band *sband = NULL;
1179 struct sta_info *mgd_sta = NULL;
1180 enum ieee80211_sta_rx_bandwidth bw = IEEE80211_STA_RX_BW_20;
1181
1182 /*
1183 * with multi-vif csa driver may call ieee80211_csa_finish()
1184 * many times while waiting for other interfaces to use their
1185 * reservations
1186 */
1187 if (sdata->reserved_ready)
1188 goto out;
1189
1190 if (sdata->vif.bss_conf.chandef.width !=
1191 sdata->csa_chandef.width) {
1192 /*
1193 * For managed interface, we need to also update the AP
1194 * station bandwidth and align the rate scale algorithm
1195 * on the bandwidth change. Here we only consider the
1196 * bandwidth of the new channel definition (as channel
1197 * switch flow does not have the full HT/VHT/HE
1198 * information), assuming that if additional changes are
1199 * required they would be done as part of the processing
1200 * of the next beacon from the AP.
1201 */
1202 switch (sdata->csa_chandef.width) {
1203 case NL80211_CHAN_WIDTH_20_NOHT:
1204 case NL80211_CHAN_WIDTH_20:
1205 default:
1206 bw = IEEE80211_STA_RX_BW_20;
1207 break;
1208 case NL80211_CHAN_WIDTH_40:
1209 bw = IEEE80211_STA_RX_BW_40;
1210 break;
1211 case NL80211_CHAN_WIDTH_80:
1212 bw = IEEE80211_STA_RX_BW_80;
1213 break;
1214 case NL80211_CHAN_WIDTH_80P80:
1215 case NL80211_CHAN_WIDTH_160:
1216 bw = IEEE80211_STA_RX_BW_160;
1217 break;
1218 }
1219
1220 mgd_sta = sta_info_get(sdata, ifmgd->bssid);
1221 sband =
1222 local->hw.wiphy->bands[sdata->csa_chandef.chan->band];
1223 }
1224
1225 if (sdata->vif.bss_conf.chandef.width >
1226 sdata->csa_chandef.width) {
1227 mgd_sta->sta.bandwidth = bw;
1228 rate_control_rate_update(local, sband, mgd_sta,
1229 IEEE80211_RC_BW_CHANGED);
1230 }
1231
1232 ret = ieee80211_vif_use_reserved_context(sdata);
1233 if (ret) {
1234 sdata_info(sdata,
1235 "failed to use reserved channel context, disconnecting (err=%d)\n",
1236 ret);
1237 ieee80211_queue_work(&sdata->local->hw,
1238 &ifmgd->csa_connection_drop_work);
1239 goto out;
1240 }
1241
1242 if (sdata->vif.bss_conf.chandef.width <
1243 sdata->csa_chandef.width) {
1244 mgd_sta->sta.bandwidth = bw;
1245 rate_control_rate_update(local, sband, mgd_sta,
1246 IEEE80211_RC_BW_CHANGED);
1247 }
1248
1249 goto out;
1250 }
1251
1252 if (!cfg80211_chandef_identical(&sdata->vif.bss_conf.chandef,
1253 &sdata->csa_chandef)) {
1254 sdata_info(sdata,
1255 "failed to finalize channel switch, disconnecting\n");
1256 ieee80211_queue_work(&sdata->local->hw,
1257 &ifmgd->csa_connection_drop_work);
1258 goto out;
1259 }
1260
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001261 ifmgd->csa_waiting_bcn = true;
1262
1263 ieee80211_sta_reset_beacon_monitor(sdata);
1264 ieee80211_sta_reset_conn_monitor(sdata);
1265
1266out:
1267 mutex_unlock(&local->chanctx_mtx);
1268 mutex_unlock(&local->mtx);
1269 sdata_unlock(sdata);
1270}
1271
1272static void ieee80211_chswitch_post_beacon(struct ieee80211_sub_if_data *sdata)
1273{
1274 struct ieee80211_local *local = sdata->local;
1275 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1276 int ret;
1277
1278 sdata_assert_lock(sdata);
1279
1280 WARN_ON(!sdata->vif.csa_active);
1281
1282 if (sdata->csa_block_tx) {
1283 ieee80211_wake_vif_queues(local, sdata,
1284 IEEE80211_QUEUE_STOP_REASON_CSA);
1285 sdata->csa_block_tx = false;
1286 }
1287
1288 sdata->vif.csa_active = false;
1289 ifmgd->csa_waiting_bcn = false;
Olivier Deprez0e641232021-09-23 10:07:05 +02001290 /*
1291 * If the CSA IE is still present on the beacon after the switch,
1292 * we need to consider it as a new CSA (possibly to self).
1293 */
1294 ifmgd->beacon_crc_valid = false;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001295
1296 ret = drv_post_channel_switch(sdata);
1297 if (ret) {
1298 sdata_info(sdata,
1299 "driver post channel switch failed, disconnecting\n");
1300 ieee80211_queue_work(&local->hw,
1301 &ifmgd->csa_connection_drop_work);
1302 return;
1303 }
1304
1305 cfg80211_ch_switch_notify(sdata->dev, &sdata->reserved_chandef);
1306}
1307
1308void ieee80211_chswitch_done(struct ieee80211_vif *vif, bool success)
1309{
1310 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1311 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1312
1313 trace_api_chswitch_done(sdata, success);
1314 if (!success) {
1315 sdata_info(sdata,
1316 "driver channel switch failed, disconnecting\n");
1317 ieee80211_queue_work(&sdata->local->hw,
1318 &ifmgd->csa_connection_drop_work);
1319 } else {
1320 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
1321 }
1322}
1323EXPORT_SYMBOL(ieee80211_chswitch_done);
1324
1325static void ieee80211_chswitch_timer(struct timer_list *t)
1326{
1327 struct ieee80211_sub_if_data *sdata =
1328 from_timer(sdata, t, u.mgd.chswitch_timer);
1329
1330 ieee80211_queue_work(&sdata->local->hw, &sdata->u.mgd.chswitch_work);
1331}
1332
1333static void
David Brazdil0f672f62019-12-10 10:32:29 +00001334ieee80211_sta_abort_chanswitch(struct ieee80211_sub_if_data *sdata)
1335{
1336 struct ieee80211_local *local = sdata->local;
1337
1338 if (!local->ops->abort_channel_switch)
1339 return;
1340
1341 mutex_lock(&local->mtx);
1342
1343 mutex_lock(&local->chanctx_mtx);
1344 ieee80211_vif_unreserve_chanctx(sdata);
1345 mutex_unlock(&local->chanctx_mtx);
1346
1347 if (sdata->csa_block_tx)
1348 ieee80211_wake_vif_queues(local, sdata,
1349 IEEE80211_QUEUE_STOP_REASON_CSA);
1350
1351 sdata->csa_block_tx = false;
1352 sdata->vif.csa_active = false;
1353
1354 mutex_unlock(&local->mtx);
1355
1356 drv_abort_channel_switch(sdata);
1357}
1358
1359static void
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001360ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
1361 u64 timestamp, u32 device_timestamp,
1362 struct ieee802_11_elems *elems,
1363 bool beacon)
1364{
1365 struct ieee80211_local *local = sdata->local;
1366 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1367 struct cfg80211_bss *cbss = ifmgd->associated;
1368 struct ieee80211_chanctx_conf *conf;
1369 struct ieee80211_chanctx *chanctx;
1370 enum nl80211_band current_band;
1371 struct ieee80211_csa_ie csa_ie;
1372 struct ieee80211_channel_switch ch_switch;
Olivier Deprez157378f2022-04-04 15:47:50 +02001373 struct ieee80211_bss *bss;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001374 int res;
1375
1376 sdata_assert_lock(sdata);
1377
1378 if (!cbss)
1379 return;
1380
1381 if (local->scanning)
1382 return;
1383
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001384 current_band = cbss->channel->band;
Olivier Deprez157378f2022-04-04 15:47:50 +02001385 bss = (void *)cbss->priv;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001386 res = ieee80211_parse_ch_switch_ie(sdata, elems, current_band,
Olivier Deprez157378f2022-04-04 15:47:50 +02001387 bss->vht_cap_info,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001388 ifmgd->flags,
1389 ifmgd->associated->bssid, &csa_ie);
David Brazdil0f672f62019-12-10 10:32:29 +00001390
1391 if (!res) {
1392 ch_switch.timestamp = timestamp;
1393 ch_switch.device_timestamp = device_timestamp;
Olivier Deprez0e641232021-09-23 10:07:05 +02001394 ch_switch.block_tx = csa_ie.mode;
David Brazdil0f672f62019-12-10 10:32:29 +00001395 ch_switch.chandef = csa_ie.chandef;
1396 ch_switch.count = csa_ie.count;
1397 ch_switch.delay = csa_ie.max_switch_time;
1398 }
1399
1400 if (res < 0) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001401 ieee80211_queue_work(&local->hw,
1402 &ifmgd->csa_connection_drop_work);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001403 return;
David Brazdil0f672f62019-12-10 10:32:29 +00001404 }
1405
1406 if (beacon && sdata->vif.csa_active && !ifmgd->csa_waiting_bcn) {
1407 if (res)
1408 ieee80211_sta_abort_chanswitch(sdata);
1409 else
1410 drv_channel_switch_rx_beacon(sdata, &ch_switch);
1411 return;
1412 } else if (sdata->vif.csa_active || res) {
1413 /* disregard subsequent announcements if already processing */
1414 return;
1415 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001416
1417 if (!cfg80211_chandef_usable(local->hw.wiphy, &csa_ie.chandef,
1418 IEEE80211_CHAN_DISABLED)) {
1419 sdata_info(sdata,
Olivier Deprez157378f2022-04-04 15:47:50 +02001420 "AP %pM switches to unsupported channel "
1421 "(%d.%03d MHz, width:%d, CF1/2: %d.%03d/%d MHz), "
1422 "disconnecting\n",
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001423 ifmgd->associated->bssid,
1424 csa_ie.chandef.chan->center_freq,
Olivier Deprez157378f2022-04-04 15:47:50 +02001425 csa_ie.chandef.chan->freq_offset,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001426 csa_ie.chandef.width, csa_ie.chandef.center_freq1,
Olivier Deprez157378f2022-04-04 15:47:50 +02001427 csa_ie.chandef.freq1_offset,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001428 csa_ie.chandef.center_freq2);
1429 ieee80211_queue_work(&local->hw,
1430 &ifmgd->csa_connection_drop_work);
1431 return;
1432 }
1433
1434 if (cfg80211_chandef_identical(&csa_ie.chandef,
David Brazdil0f672f62019-12-10 10:32:29 +00001435 &sdata->vif.bss_conf.chandef) &&
1436 (!csa_ie.mode || !beacon)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001437 if (ifmgd->csa_ignored_same_chan)
1438 return;
1439 sdata_info(sdata,
1440 "AP %pM tries to chanswitch to same channel, ignore\n",
1441 ifmgd->associated->bssid);
1442 ifmgd->csa_ignored_same_chan = true;
1443 return;
1444 }
1445
1446 /*
1447 * Drop all TDLS peers - either we disconnect or move to a different
1448 * channel from this point on. There's no telling what our peer will do.
1449 * The TDLS WIDER_BW scenario is also problematic, as peers might now
1450 * have an incompatible wider chandef.
1451 */
1452 ieee80211_teardown_tdls_peers(sdata);
1453
1454 mutex_lock(&local->mtx);
1455 mutex_lock(&local->chanctx_mtx);
1456 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
1457 lockdep_is_held(&local->chanctx_mtx));
1458 if (!conf) {
1459 sdata_info(sdata,
1460 "no channel context assigned to vif?, disconnecting\n");
1461 goto drop_connection;
1462 }
1463
1464 chanctx = container_of(conf, struct ieee80211_chanctx, conf);
1465
1466 if (local->use_chanctx &&
1467 !ieee80211_hw_check(&local->hw, CHANCTX_STA_CSA)) {
1468 sdata_info(sdata,
1469 "driver doesn't support chan-switch with channel contexts\n");
1470 goto drop_connection;
1471 }
1472
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001473 if (drv_pre_channel_switch(sdata, &ch_switch)) {
1474 sdata_info(sdata,
1475 "preparing for channel switch failed, disconnecting\n");
1476 goto drop_connection;
1477 }
1478
1479 res = ieee80211_vif_reserve_chanctx(sdata, &csa_ie.chandef,
1480 chanctx->mode, false);
1481 if (res) {
1482 sdata_info(sdata,
1483 "failed to reserve channel context for channel switch, disconnecting (err=%d)\n",
1484 res);
1485 goto drop_connection;
1486 }
1487 mutex_unlock(&local->chanctx_mtx);
1488
1489 sdata->vif.csa_active = true;
1490 sdata->csa_chandef = csa_ie.chandef;
Olivier Deprez0e641232021-09-23 10:07:05 +02001491 sdata->csa_block_tx = csa_ie.mode;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001492 ifmgd->csa_ignored_same_chan = false;
1493
1494 if (sdata->csa_block_tx)
1495 ieee80211_stop_vif_queues(local, sdata,
1496 IEEE80211_QUEUE_STOP_REASON_CSA);
1497 mutex_unlock(&local->mtx);
1498
1499 cfg80211_ch_switch_started_notify(sdata->dev, &csa_ie.chandef,
1500 csa_ie.count);
1501
1502 if (local->ops->channel_switch) {
1503 /* use driver's channel switch callback */
1504 drv_channel_switch(local, sdata, &ch_switch);
1505 return;
1506 }
1507
1508 /* channel switch handled in software */
1509 if (csa_ie.count <= 1)
1510 ieee80211_queue_work(&local->hw, &ifmgd->chswitch_work);
1511 else
1512 mod_timer(&ifmgd->chswitch_timer,
1513 TU_TO_EXP_TIME((csa_ie.count - 1) *
1514 cbss->beacon_interval));
1515 return;
1516 drop_connection:
1517 /*
1518 * This is just so that the disconnect flow will know that
1519 * we were trying to switch channel and failed. In case the
1520 * mode is 1 (we are not allowed to Tx), we will know not to
1521 * send a deauthentication frame. Those two fields will be
1522 * reset when the disconnection worker runs.
1523 */
1524 sdata->vif.csa_active = true;
Olivier Deprez0e641232021-09-23 10:07:05 +02001525 sdata->csa_block_tx = csa_ie.mode;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001526
1527 ieee80211_queue_work(&local->hw, &ifmgd->csa_connection_drop_work);
1528 mutex_unlock(&local->chanctx_mtx);
1529 mutex_unlock(&local->mtx);
1530}
1531
1532static bool
1533ieee80211_find_80211h_pwr_constr(struct ieee80211_sub_if_data *sdata,
1534 struct ieee80211_channel *channel,
1535 const u8 *country_ie, u8 country_ie_len,
1536 const u8 *pwr_constr_elem,
1537 int *chan_pwr, int *pwr_reduction)
1538{
1539 struct ieee80211_country_ie_triplet *triplet;
1540 int chan = ieee80211_frequency_to_channel(channel->center_freq);
1541 int i, chan_increment;
1542 bool have_chan_pwr = false;
1543
1544 /* Invalid IE */
1545 if (country_ie_len % 2 || country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
1546 return false;
1547
1548 triplet = (void *)(country_ie + 3);
1549 country_ie_len -= 3;
1550
1551 switch (channel->band) {
1552 default:
1553 WARN_ON_ONCE(1);
Olivier Deprez157378f2022-04-04 15:47:50 +02001554 fallthrough;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001555 case NL80211_BAND_2GHZ:
1556 case NL80211_BAND_60GHZ:
1557 chan_increment = 1;
1558 break;
1559 case NL80211_BAND_5GHZ:
Olivier Deprez157378f2022-04-04 15:47:50 +02001560 case NL80211_BAND_6GHZ:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001561 chan_increment = 4;
1562 break;
1563 }
1564
1565 /* find channel */
1566 while (country_ie_len >= 3) {
1567 u8 first_channel = triplet->chans.first_channel;
1568
1569 if (first_channel >= IEEE80211_COUNTRY_EXTENSION_ID)
1570 goto next;
1571
1572 for (i = 0; i < triplet->chans.num_channels; i++) {
1573 if (first_channel + i * chan_increment == chan) {
1574 have_chan_pwr = true;
1575 *chan_pwr = triplet->chans.max_power;
1576 break;
1577 }
1578 }
1579 if (have_chan_pwr)
1580 break;
1581
1582 next:
1583 triplet++;
1584 country_ie_len -= 3;
1585 }
1586
1587 if (have_chan_pwr && pwr_constr_elem)
1588 *pwr_reduction = *pwr_constr_elem;
1589 else
1590 *pwr_reduction = 0;
1591
1592 return have_chan_pwr;
1593}
1594
1595static void ieee80211_find_cisco_dtpc(struct ieee80211_sub_if_data *sdata,
1596 struct ieee80211_channel *channel,
1597 const u8 *cisco_dtpc_ie,
1598 int *pwr_level)
1599{
1600 /* From practical testing, the first data byte of the DTPC element
1601 * seems to contain the requested dBm level, and the CLI on Cisco
1602 * APs clearly state the range is -127 to 127 dBm, which indicates
1603 * a signed byte, although it seemingly never actually goes negative.
1604 * The other byte seems to always be zero.
1605 */
1606 *pwr_level = (__s8)cisco_dtpc_ie[4];
1607}
1608
1609static u32 ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data *sdata,
1610 struct ieee80211_channel *channel,
1611 struct ieee80211_mgmt *mgmt,
1612 const u8 *country_ie, u8 country_ie_len,
1613 const u8 *pwr_constr_ie,
1614 const u8 *cisco_dtpc_ie)
1615{
1616 bool has_80211h_pwr = false, has_cisco_pwr = false;
1617 int chan_pwr = 0, pwr_reduction_80211h = 0;
1618 int pwr_level_cisco, pwr_level_80211h;
1619 int new_ap_level;
1620 __le16 capab = mgmt->u.probe_resp.capab_info;
1621
Olivier Deprez157378f2022-04-04 15:47:50 +02001622 if (ieee80211_is_s1g_beacon(mgmt->frame_control))
1623 return 0; /* TODO */
1624
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001625 if (country_ie &&
1626 (capab & cpu_to_le16(WLAN_CAPABILITY_SPECTRUM_MGMT) ||
1627 capab & cpu_to_le16(WLAN_CAPABILITY_RADIO_MEASURE))) {
1628 has_80211h_pwr = ieee80211_find_80211h_pwr_constr(
1629 sdata, channel, country_ie, country_ie_len,
1630 pwr_constr_ie, &chan_pwr, &pwr_reduction_80211h);
1631 pwr_level_80211h =
1632 max_t(int, 0, chan_pwr - pwr_reduction_80211h);
1633 }
1634
1635 if (cisco_dtpc_ie) {
1636 ieee80211_find_cisco_dtpc(
1637 sdata, channel, cisco_dtpc_ie, &pwr_level_cisco);
1638 has_cisco_pwr = true;
1639 }
1640
1641 if (!has_80211h_pwr && !has_cisco_pwr)
1642 return 0;
1643
1644 /* If we have both 802.11h and Cisco DTPC, apply both limits
1645 * by picking the smallest of the two power levels advertised.
1646 */
1647 if (has_80211h_pwr &&
1648 (!has_cisco_pwr || pwr_level_80211h <= pwr_level_cisco)) {
1649 new_ap_level = pwr_level_80211h;
1650
1651 if (sdata->ap_power_level == new_ap_level)
1652 return 0;
1653
1654 sdata_dbg(sdata,
1655 "Limiting TX power to %d (%d - %d) dBm as advertised by %pM\n",
1656 pwr_level_80211h, chan_pwr, pwr_reduction_80211h,
1657 sdata->u.mgd.bssid);
1658 } else { /* has_cisco_pwr is always true here. */
1659 new_ap_level = pwr_level_cisco;
1660
1661 if (sdata->ap_power_level == new_ap_level)
1662 return 0;
1663
1664 sdata_dbg(sdata,
1665 "Limiting TX power to %d dBm as advertised by %pM\n",
1666 pwr_level_cisco, sdata->u.mgd.bssid);
1667 }
1668
1669 sdata->ap_power_level = new_ap_level;
1670 if (__ieee80211_recalc_txpower(sdata))
1671 return BSS_CHANGED_TXPOWER;
1672 return 0;
1673}
1674
1675/* powersave */
1676static void ieee80211_enable_ps(struct ieee80211_local *local,
1677 struct ieee80211_sub_if_data *sdata)
1678{
1679 struct ieee80211_conf *conf = &local->hw.conf;
1680
1681 /*
1682 * If we are scanning right now then the parameters will
1683 * take effect when scan finishes.
1684 */
1685 if (local->scanning)
1686 return;
1687
1688 if (conf->dynamic_ps_timeout > 0 &&
1689 !ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS)) {
1690 mod_timer(&local->dynamic_ps_timer, jiffies +
1691 msecs_to_jiffies(conf->dynamic_ps_timeout));
1692 } else {
1693 if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK))
1694 ieee80211_send_nullfunc(local, sdata, true);
1695
1696 if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK) &&
1697 ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
1698 return;
1699
1700 conf->flags |= IEEE80211_CONF_PS;
1701 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1702 }
1703}
1704
1705static void ieee80211_change_ps(struct ieee80211_local *local)
1706{
1707 struct ieee80211_conf *conf = &local->hw.conf;
1708
1709 if (local->ps_sdata) {
1710 ieee80211_enable_ps(local, local->ps_sdata);
1711 } else if (conf->flags & IEEE80211_CONF_PS) {
1712 conf->flags &= ~IEEE80211_CONF_PS;
1713 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1714 del_timer_sync(&local->dynamic_ps_timer);
1715 cancel_work_sync(&local->dynamic_ps_enable_work);
1716 }
1717}
1718
1719static bool ieee80211_powersave_allowed(struct ieee80211_sub_if_data *sdata)
1720{
1721 struct ieee80211_if_managed *mgd = &sdata->u.mgd;
1722 struct sta_info *sta = NULL;
1723 bool authorized = false;
1724
1725 if (!mgd->powersave)
1726 return false;
1727
1728 if (mgd->broken_ap)
1729 return false;
1730
1731 if (!mgd->associated)
1732 return false;
1733
1734 if (mgd->flags & IEEE80211_STA_CONNECTION_POLL)
1735 return false;
1736
1737 if (!mgd->have_beacon)
1738 return false;
1739
1740 rcu_read_lock();
1741 sta = sta_info_get(sdata, mgd->bssid);
1742 if (sta)
1743 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
1744 rcu_read_unlock();
1745
1746 return authorized;
1747}
1748
1749/* need to hold RTNL or interface lock */
1750void ieee80211_recalc_ps(struct ieee80211_local *local)
1751{
1752 struct ieee80211_sub_if_data *sdata, *found = NULL;
1753 int count = 0;
1754 int timeout;
1755
1756 if (!ieee80211_hw_check(&local->hw, SUPPORTS_PS)) {
1757 local->ps_sdata = NULL;
1758 return;
1759 }
1760
1761 list_for_each_entry(sdata, &local->interfaces, list) {
1762 if (!ieee80211_sdata_running(sdata))
1763 continue;
1764 if (sdata->vif.type == NL80211_IFTYPE_AP) {
1765 /* If an AP vif is found, then disable PS
1766 * by setting the count to zero thereby setting
1767 * ps_sdata to NULL.
1768 */
1769 count = 0;
1770 break;
1771 }
1772 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1773 continue;
1774 found = sdata;
1775 count++;
1776 }
1777
1778 if (count == 1 && ieee80211_powersave_allowed(found)) {
1779 u8 dtimper = found->u.mgd.dtim_period;
1780
1781 timeout = local->dynamic_ps_forced_timeout;
1782 if (timeout < 0)
1783 timeout = 100;
1784 local->hw.conf.dynamic_ps_timeout = timeout;
1785
1786 /* If the TIM IE is invalid, pretend the value is 1 */
1787 if (!dtimper)
1788 dtimper = 1;
1789
1790 local->hw.conf.ps_dtim_period = dtimper;
1791 local->ps_sdata = found;
1792 } else {
1793 local->ps_sdata = NULL;
1794 }
1795
1796 ieee80211_change_ps(local);
1797}
1798
1799void ieee80211_recalc_ps_vif(struct ieee80211_sub_if_data *sdata)
1800{
1801 bool ps_allowed = ieee80211_powersave_allowed(sdata);
1802
1803 if (sdata->vif.bss_conf.ps != ps_allowed) {
1804 sdata->vif.bss_conf.ps = ps_allowed;
1805 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_PS);
1806 }
1807}
1808
1809void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
1810{
1811 struct ieee80211_local *local =
1812 container_of(work, struct ieee80211_local,
1813 dynamic_ps_disable_work);
1814
1815 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1816 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1817 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1818 }
1819
1820 ieee80211_wake_queues_by_reason(&local->hw,
1821 IEEE80211_MAX_QUEUE_MAP,
1822 IEEE80211_QUEUE_STOP_REASON_PS,
1823 false);
1824}
1825
1826void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
1827{
1828 struct ieee80211_local *local =
1829 container_of(work, struct ieee80211_local,
1830 dynamic_ps_enable_work);
1831 struct ieee80211_sub_if_data *sdata = local->ps_sdata;
1832 struct ieee80211_if_managed *ifmgd;
1833 unsigned long flags;
1834 int q;
1835
1836 /* can only happen when PS was just disabled anyway */
1837 if (!sdata)
1838 return;
1839
1840 ifmgd = &sdata->u.mgd;
1841
1842 if (local->hw.conf.flags & IEEE80211_CONF_PS)
1843 return;
1844
1845 if (local->hw.conf.dynamic_ps_timeout > 0) {
1846 /* don't enter PS if TX frames are pending */
1847 if (drv_tx_frames_pending(local)) {
1848 mod_timer(&local->dynamic_ps_timer, jiffies +
1849 msecs_to_jiffies(
1850 local->hw.conf.dynamic_ps_timeout));
1851 return;
1852 }
1853
1854 /*
1855 * transmission can be stopped by others which leads to
1856 * dynamic_ps_timer expiry. Postpone the ps timer if it
1857 * is not the actual idle state.
1858 */
1859 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
1860 for (q = 0; q < local->hw.queues; q++) {
1861 if (local->queue_stop_reasons[q]) {
1862 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1863 flags);
1864 mod_timer(&local->dynamic_ps_timer, jiffies +
1865 msecs_to_jiffies(
1866 local->hw.conf.dynamic_ps_timeout));
1867 return;
1868 }
1869 }
1870 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
1871 }
1872
1873 if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK) &&
1874 !(ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
1875 if (drv_tx_frames_pending(local)) {
1876 mod_timer(&local->dynamic_ps_timer, jiffies +
1877 msecs_to_jiffies(
1878 local->hw.conf.dynamic_ps_timeout));
1879 } else {
1880 ieee80211_send_nullfunc(local, sdata, true);
1881 /* Flush to get the tx status of nullfunc frame */
1882 ieee80211_flush_queues(local, sdata, false);
1883 }
1884 }
1885
1886 if (!(ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS) &&
1887 ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK)) ||
1888 (ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
1889 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
1890 local->hw.conf.flags |= IEEE80211_CONF_PS;
1891 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1892 }
1893}
1894
1895void ieee80211_dynamic_ps_timer(struct timer_list *t)
1896{
1897 struct ieee80211_local *local = from_timer(local, t, dynamic_ps_timer);
1898
1899 ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
1900}
1901
1902void ieee80211_dfs_cac_timer_work(struct work_struct *work)
1903{
1904 struct delayed_work *delayed_work = to_delayed_work(work);
1905 struct ieee80211_sub_if_data *sdata =
1906 container_of(delayed_work, struct ieee80211_sub_if_data,
1907 dfs_cac_timer_work);
1908 struct cfg80211_chan_def chandef = sdata->vif.bss_conf.chandef;
1909
1910 mutex_lock(&sdata->local->mtx);
1911 if (sdata->wdev.cac_started) {
1912 ieee80211_vif_release_channel(sdata);
1913 cfg80211_cac_event(sdata->dev, &chandef,
1914 NL80211_RADAR_CAC_FINISHED,
1915 GFP_KERNEL);
1916 }
1917 mutex_unlock(&sdata->local->mtx);
1918}
1919
1920static bool
1921__ieee80211_sta_handle_tspec_ac_params(struct ieee80211_sub_if_data *sdata)
1922{
1923 struct ieee80211_local *local = sdata->local;
1924 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1925 bool ret = false;
1926 int ac;
1927
1928 if (local->hw.queues < IEEE80211_NUM_ACS)
1929 return false;
1930
1931 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1932 struct ieee80211_sta_tx_tspec *tx_tspec = &ifmgd->tx_tspec[ac];
1933 int non_acm_ac;
1934 unsigned long now = jiffies;
1935
1936 if (tx_tspec->action == TX_TSPEC_ACTION_NONE &&
1937 tx_tspec->admitted_time &&
1938 time_after(now, tx_tspec->time_slice_start + HZ)) {
1939 tx_tspec->consumed_tx_time = 0;
1940 tx_tspec->time_slice_start = now;
1941
1942 if (tx_tspec->downgraded)
1943 tx_tspec->action =
1944 TX_TSPEC_ACTION_STOP_DOWNGRADE;
1945 }
1946
1947 switch (tx_tspec->action) {
1948 case TX_TSPEC_ACTION_STOP_DOWNGRADE:
1949 /* take the original parameters */
1950 if (drv_conf_tx(local, sdata, ac, &sdata->tx_conf[ac]))
1951 sdata_err(sdata,
1952 "failed to set TX queue parameters for queue %d\n",
1953 ac);
1954 tx_tspec->action = TX_TSPEC_ACTION_NONE;
1955 tx_tspec->downgraded = false;
1956 ret = true;
1957 break;
1958 case TX_TSPEC_ACTION_DOWNGRADE:
1959 if (time_after(now, tx_tspec->time_slice_start + HZ)) {
1960 tx_tspec->action = TX_TSPEC_ACTION_NONE;
1961 ret = true;
1962 break;
1963 }
1964 /* downgrade next lower non-ACM AC */
1965 for (non_acm_ac = ac + 1;
1966 non_acm_ac < IEEE80211_NUM_ACS;
1967 non_acm_ac++)
1968 if (!(sdata->wmm_acm & BIT(7 - 2 * non_acm_ac)))
1969 break;
1970 /* Usually the loop will result in using BK even if it
1971 * requires admission control, but such a configuration
1972 * makes no sense and we have to transmit somehow - the
1973 * AC selection does the same thing.
1974 * If we started out trying to downgrade from BK, then
1975 * the extra condition here might be needed.
1976 */
1977 if (non_acm_ac >= IEEE80211_NUM_ACS)
1978 non_acm_ac = IEEE80211_AC_BK;
1979 if (drv_conf_tx(local, sdata, ac,
1980 &sdata->tx_conf[non_acm_ac]))
1981 sdata_err(sdata,
1982 "failed to set TX queue parameters for queue %d\n",
1983 ac);
1984 tx_tspec->action = TX_TSPEC_ACTION_NONE;
1985 ret = true;
1986 schedule_delayed_work(&ifmgd->tx_tspec_wk,
1987 tx_tspec->time_slice_start + HZ - now + 1);
1988 break;
1989 case TX_TSPEC_ACTION_NONE:
1990 /* nothing now */
1991 break;
1992 }
1993 }
1994
1995 return ret;
1996}
1997
1998void ieee80211_sta_handle_tspec_ac_params(struct ieee80211_sub_if_data *sdata)
1999{
2000 if (__ieee80211_sta_handle_tspec_ac_params(sdata))
2001 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_QOS);
2002}
2003
2004static void ieee80211_sta_handle_tspec_ac_params_wk(struct work_struct *work)
2005{
2006 struct ieee80211_sub_if_data *sdata;
2007
2008 sdata = container_of(work, struct ieee80211_sub_if_data,
2009 u.mgd.tx_tspec_wk.work);
2010 ieee80211_sta_handle_tspec_ac_params(sdata);
2011}
2012
2013/* MLME */
2014static bool
2015ieee80211_sta_wmm_params(struct ieee80211_local *local,
2016 struct ieee80211_sub_if_data *sdata,
2017 const u8 *wmm_param, size_t wmm_param_len,
2018 const struct ieee80211_mu_edca_param_set *mu_edca)
2019{
2020 struct ieee80211_tx_queue_params params[IEEE80211_NUM_ACS];
2021 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2022 size_t left;
David Brazdil0f672f62019-12-10 10:32:29 +00002023 int count, mu_edca_count, ac;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002024 const u8 *pos;
2025 u8 uapsd_queues = 0;
2026
2027 if (!local->ops->conf_tx)
2028 return false;
2029
2030 if (local->hw.queues < IEEE80211_NUM_ACS)
2031 return false;
2032
2033 if (!wmm_param)
2034 return false;
2035
2036 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
2037 return false;
2038
2039 if (ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED)
2040 uapsd_queues = ifmgd->uapsd_queues;
2041
2042 count = wmm_param[6] & 0x0f;
David Brazdil0f672f62019-12-10 10:32:29 +00002043 /* -1 is the initial value of ifmgd->mu_edca_last_param_set.
2044 * if mu_edca was preset before and now it disappeared tell
2045 * the driver about it.
2046 */
2047 mu_edca_count = mu_edca ? mu_edca->mu_qos_info & 0x0f : -1;
2048 if (count == ifmgd->wmm_last_param_set &&
2049 mu_edca_count == ifmgd->mu_edca_last_param_set)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002050 return false;
2051 ifmgd->wmm_last_param_set = count;
David Brazdil0f672f62019-12-10 10:32:29 +00002052 ifmgd->mu_edca_last_param_set = mu_edca_count;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002053
2054 pos = wmm_param + 8;
2055 left = wmm_param_len - 8;
2056
2057 memset(&params, 0, sizeof(params));
2058
2059 sdata->wmm_acm = 0;
2060 for (; left >= 4; left -= 4, pos += 4) {
2061 int aci = (pos[0] >> 5) & 0x03;
2062 int acm = (pos[0] >> 4) & 0x01;
2063 bool uapsd = false;
2064
2065 switch (aci) {
2066 case 1: /* AC_BK */
2067 ac = IEEE80211_AC_BK;
2068 if (acm)
2069 sdata->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
2070 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
2071 uapsd = true;
2072 params[ac].mu_edca = !!mu_edca;
2073 if (mu_edca)
2074 params[ac].mu_edca_param_rec = mu_edca->ac_bk;
2075 break;
2076 case 2: /* AC_VI */
2077 ac = IEEE80211_AC_VI;
2078 if (acm)
2079 sdata->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
2080 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
2081 uapsd = true;
2082 params[ac].mu_edca = !!mu_edca;
2083 if (mu_edca)
2084 params[ac].mu_edca_param_rec = mu_edca->ac_vi;
2085 break;
2086 case 3: /* AC_VO */
2087 ac = IEEE80211_AC_VO;
2088 if (acm)
2089 sdata->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
2090 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
2091 uapsd = true;
2092 params[ac].mu_edca = !!mu_edca;
2093 if (mu_edca)
2094 params[ac].mu_edca_param_rec = mu_edca->ac_vo;
2095 break;
2096 case 0: /* AC_BE */
2097 default:
2098 ac = IEEE80211_AC_BE;
2099 if (acm)
2100 sdata->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
2101 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
2102 uapsd = true;
2103 params[ac].mu_edca = !!mu_edca;
2104 if (mu_edca)
2105 params[ac].mu_edca_param_rec = mu_edca->ac_be;
2106 break;
2107 }
2108
2109 params[ac].aifs = pos[0] & 0x0f;
2110
2111 if (params[ac].aifs < 2) {
2112 sdata_info(sdata,
2113 "AP has invalid WMM params (AIFSN=%d for ACI %d), will use 2\n",
2114 params[ac].aifs, aci);
2115 params[ac].aifs = 2;
2116 }
2117 params[ac].cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
2118 params[ac].cw_min = ecw2cw(pos[1] & 0x0f);
2119 params[ac].txop = get_unaligned_le16(pos + 2);
2120 params[ac].acm = acm;
2121 params[ac].uapsd = uapsd;
2122
2123 if (params[ac].cw_min == 0 ||
2124 params[ac].cw_min > params[ac].cw_max) {
2125 sdata_info(sdata,
2126 "AP has invalid WMM params (CWmin/max=%d/%d for ACI %d), using defaults\n",
2127 params[ac].cw_min, params[ac].cw_max, aci);
2128 return false;
2129 }
2130 ieee80211_regulatory_limit_wmm_params(sdata, &params[ac], ac);
2131 }
2132
David Brazdil0f672f62019-12-10 10:32:29 +00002133 /* WMM specification requires all 4 ACIs. */
2134 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
2135 if (params[ac].cw_min == 0) {
2136 sdata_info(sdata,
2137 "AP has invalid WMM params (missing AC %d), using defaults\n",
2138 ac);
2139 return false;
2140 }
2141 }
2142
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002143 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
2144 mlme_dbg(sdata,
2145 "WMM AC=%d acm=%d aifs=%d cWmin=%d cWmax=%d txop=%d uapsd=%d, downgraded=%d\n",
2146 ac, params[ac].acm,
2147 params[ac].aifs, params[ac].cw_min, params[ac].cw_max,
2148 params[ac].txop, params[ac].uapsd,
2149 ifmgd->tx_tspec[ac].downgraded);
2150 sdata->tx_conf[ac] = params[ac];
2151 if (!ifmgd->tx_tspec[ac].downgraded &&
2152 drv_conf_tx(local, sdata, ac, &params[ac]))
2153 sdata_err(sdata,
2154 "failed to set TX queue parameters for AC %d\n",
2155 ac);
2156 }
2157
2158 /* enable WMM or activate new settings */
2159 sdata->vif.bss_conf.qos = true;
2160 return true;
2161}
2162
2163static void __ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
2164{
2165 lockdep_assert_held(&sdata->local->mtx);
2166
2167 sdata->u.mgd.flags &= ~IEEE80211_STA_CONNECTION_POLL;
2168 ieee80211_run_deferred_scan(sdata->local);
2169}
2170
2171static void ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
2172{
2173 mutex_lock(&sdata->local->mtx);
2174 __ieee80211_stop_poll(sdata);
2175 mutex_unlock(&sdata->local->mtx);
2176}
2177
2178static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
2179 u16 capab, bool erp_valid, u8 erp)
2180{
2181 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
2182 struct ieee80211_supported_band *sband;
2183 u32 changed = 0;
2184 bool use_protection;
2185 bool use_short_preamble;
2186 bool use_short_slot;
2187
2188 sband = ieee80211_get_sband(sdata);
2189 if (!sband)
2190 return changed;
2191
2192 if (erp_valid) {
2193 use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
2194 use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
2195 } else {
2196 use_protection = false;
2197 use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
2198 }
2199
2200 use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
Olivier Deprez157378f2022-04-04 15:47:50 +02002201 if (sband->band == NL80211_BAND_5GHZ ||
2202 sband->band == NL80211_BAND_6GHZ)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002203 use_short_slot = true;
2204
2205 if (use_protection != bss_conf->use_cts_prot) {
2206 bss_conf->use_cts_prot = use_protection;
2207 changed |= BSS_CHANGED_ERP_CTS_PROT;
2208 }
2209
2210 if (use_short_preamble != bss_conf->use_short_preamble) {
2211 bss_conf->use_short_preamble = use_short_preamble;
2212 changed |= BSS_CHANGED_ERP_PREAMBLE;
2213 }
2214
2215 if (use_short_slot != bss_conf->use_short_slot) {
2216 bss_conf->use_short_slot = use_short_slot;
2217 changed |= BSS_CHANGED_ERP_SLOT;
2218 }
2219
2220 return changed;
2221}
2222
2223static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
2224 struct cfg80211_bss *cbss,
2225 u32 bss_info_changed)
2226{
2227 struct ieee80211_bss *bss = (void *)cbss->priv;
2228 struct ieee80211_local *local = sdata->local;
2229 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
2230
2231 bss_info_changed |= BSS_CHANGED_ASSOC;
2232 bss_info_changed |= ieee80211_handle_bss_capability(sdata,
2233 bss_conf->assoc_capability, bss->has_erp_value, bss->erp_value);
2234
2235 sdata->u.mgd.beacon_timeout = usecs_to_jiffies(ieee80211_tu_to_usec(
2236 beacon_loss_count * bss_conf->beacon_int));
2237
2238 sdata->u.mgd.associated = cbss;
2239 memcpy(sdata->u.mgd.bssid, cbss->bssid, ETH_ALEN);
2240
2241 ieee80211_check_rate_mask(sdata);
2242
2243 sdata->u.mgd.flags |= IEEE80211_STA_RESET_SIGNAL_AVE;
2244
2245 if (sdata->vif.p2p ||
2246 sdata->vif.driver_flags & IEEE80211_VIF_GET_NOA_UPDATE) {
2247 const struct cfg80211_bss_ies *ies;
2248
2249 rcu_read_lock();
2250 ies = rcu_dereference(cbss->ies);
2251 if (ies) {
2252 int ret;
2253
2254 ret = cfg80211_get_p2p_attr(
2255 ies->data, ies->len,
2256 IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
2257 (u8 *) &bss_conf->p2p_noa_attr,
2258 sizeof(bss_conf->p2p_noa_attr));
2259 if (ret >= 2) {
2260 sdata->u.mgd.p2p_noa_index =
2261 bss_conf->p2p_noa_attr.index;
2262 bss_info_changed |= BSS_CHANGED_P2P_PS;
2263 }
2264 }
2265 rcu_read_unlock();
2266 }
2267
2268 /* just to be sure */
2269 ieee80211_stop_poll(sdata);
2270
2271 ieee80211_led_assoc(local, 1);
2272
2273 if (sdata->u.mgd.have_beacon) {
2274 /*
2275 * If the AP is buggy we may get here with no DTIM period
2276 * known, so assume it's 1 which is the only safe assumption
2277 * in that case, although if the TIM IE is broken powersave
2278 * probably just won't work at all.
2279 */
2280 bss_conf->dtim_period = sdata->u.mgd.dtim_period ?: 1;
2281 bss_conf->beacon_rate = bss->beacon_rate;
2282 bss_info_changed |= BSS_CHANGED_BEACON_INFO;
2283 } else {
2284 bss_conf->beacon_rate = NULL;
2285 bss_conf->dtim_period = 0;
2286 }
2287
2288 bss_conf->assoc = 1;
2289
2290 /* Tell the driver to monitor connection quality (if supported) */
2291 if (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI &&
2292 bss_conf->cqm_rssi_thold)
2293 bss_info_changed |= BSS_CHANGED_CQM;
2294
2295 /* Enable ARP filtering */
2296 if (bss_conf->arp_addr_cnt)
2297 bss_info_changed |= BSS_CHANGED_ARP_FILTER;
2298
2299 ieee80211_bss_info_change_notify(sdata, bss_info_changed);
2300
2301 mutex_lock(&local->iflist_mtx);
2302 ieee80211_recalc_ps(local);
2303 mutex_unlock(&local->iflist_mtx);
2304
2305 ieee80211_recalc_smps(sdata);
2306 ieee80211_recalc_ps_vif(sdata);
2307
2308 netif_carrier_on(sdata->dev);
2309}
2310
2311static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
2312 u16 stype, u16 reason, bool tx,
2313 u8 *frame_buf)
2314{
2315 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2316 struct ieee80211_local *local = sdata->local;
2317 u32 changed = 0;
2318
2319 sdata_assert_lock(sdata);
2320
2321 if (WARN_ON_ONCE(tx && !frame_buf))
2322 return;
2323
2324 if (WARN_ON(!ifmgd->associated))
2325 return;
2326
2327 ieee80211_stop_poll(sdata);
2328
2329 ifmgd->associated = NULL;
2330 netif_carrier_off(sdata->dev);
2331
2332 /*
2333 * if we want to get out of ps before disassoc (why?) we have
2334 * to do it before sending disassoc, as otherwise the null-packet
2335 * won't be valid.
2336 */
2337 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
2338 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
2339 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
2340 }
2341 local->ps_sdata = NULL;
2342
2343 /* disable per-vif ps */
2344 ieee80211_recalc_ps_vif(sdata);
2345
2346 /* make sure ongoing transmission finishes */
2347 synchronize_net();
2348
2349 /*
2350 * drop any frame before deauth/disassoc, this can be data or
2351 * management frame. Since we are disconnecting, we should not
2352 * insist sending these frames which can take time and delay
2353 * the disconnection and possible the roaming.
2354 */
2355 if (tx)
2356 ieee80211_flush_queues(local, sdata, true);
2357
2358 /* deauthenticate/disassociate now */
2359 if (tx || frame_buf) {
2360 /*
2361 * In multi channel scenarios guarantee that the virtual
2362 * interface is granted immediate airtime to transmit the
2363 * deauthentication frame by calling mgd_prepare_tx, if the
2364 * driver requested so.
2365 */
2366 if (ieee80211_hw_check(&local->hw, DEAUTH_NEED_MGD_TX_PREP) &&
2367 !ifmgd->have_beacon)
2368 drv_mgd_prepare_tx(sdata->local, sdata, 0);
2369
David Brazdil0f672f62019-12-10 10:32:29 +00002370 ieee80211_send_deauth_disassoc(sdata, ifmgd->bssid,
2371 ifmgd->bssid, stype, reason,
2372 tx, frame_buf);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002373 }
2374
2375 /* flush out frame - make sure the deauth was actually sent */
2376 if (tx)
2377 ieee80211_flush_queues(local, sdata, false);
2378
2379 /* clear bssid only after building the needed mgmt frames */
2380 eth_zero_addr(ifmgd->bssid);
2381
2382 /* remove AP and TDLS peers */
2383 sta_info_flush(sdata);
2384
2385 /* finally reset all BSS / config parameters */
2386 changed |= ieee80211_reset_erp_info(sdata);
2387
2388 ieee80211_led_assoc(local, 0);
2389 changed |= BSS_CHANGED_ASSOC;
2390 sdata->vif.bss_conf.assoc = false;
2391
2392 ifmgd->p2p_noa_index = -1;
2393 memset(&sdata->vif.bss_conf.p2p_noa_attr, 0,
2394 sizeof(sdata->vif.bss_conf.p2p_noa_attr));
2395
2396 /* on the next assoc, re-program HT/VHT parameters */
2397 memset(&ifmgd->ht_capa, 0, sizeof(ifmgd->ht_capa));
2398 memset(&ifmgd->ht_capa_mask, 0, sizeof(ifmgd->ht_capa_mask));
2399 memset(&ifmgd->vht_capa, 0, sizeof(ifmgd->vht_capa));
2400 memset(&ifmgd->vht_capa_mask, 0, sizeof(ifmgd->vht_capa_mask));
2401
2402 /* reset MU-MIMO ownership and group data */
2403 memset(sdata->vif.bss_conf.mu_group.membership, 0,
2404 sizeof(sdata->vif.bss_conf.mu_group.membership));
2405 memset(sdata->vif.bss_conf.mu_group.position, 0,
2406 sizeof(sdata->vif.bss_conf.mu_group.position));
2407 changed |= BSS_CHANGED_MU_GROUPS;
2408 sdata->vif.mu_mimo_owner = false;
2409
2410 sdata->ap_power_level = IEEE80211_UNSET_POWER_LEVEL;
2411
2412 del_timer_sync(&local->dynamic_ps_timer);
2413 cancel_work_sync(&local->dynamic_ps_enable_work);
2414
2415 /* Disable ARP filtering */
2416 if (sdata->vif.bss_conf.arp_addr_cnt)
2417 changed |= BSS_CHANGED_ARP_FILTER;
2418
2419 sdata->vif.bss_conf.qos = false;
2420 changed |= BSS_CHANGED_QOS;
2421
2422 /* The BSSID (not really interesting) and HT changed */
2423 changed |= BSS_CHANGED_BSSID | BSS_CHANGED_HT;
2424 ieee80211_bss_info_change_notify(sdata, changed);
2425
2426 /* disassociated - set to defaults now */
2427 ieee80211_set_wmm_default(sdata, false, false);
2428
2429 del_timer_sync(&sdata->u.mgd.conn_mon_timer);
2430 del_timer_sync(&sdata->u.mgd.bcn_mon_timer);
2431 del_timer_sync(&sdata->u.mgd.timer);
2432 del_timer_sync(&sdata->u.mgd.chswitch_timer);
2433
2434 sdata->vif.bss_conf.dtim_period = 0;
2435 sdata->vif.bss_conf.beacon_rate = NULL;
2436
2437 ifmgd->have_beacon = false;
2438
2439 ifmgd->flags = 0;
2440 mutex_lock(&local->mtx);
2441 ieee80211_vif_release_channel(sdata);
2442
2443 sdata->vif.csa_active = false;
2444 ifmgd->csa_waiting_bcn = false;
2445 ifmgd->csa_ignored_same_chan = false;
2446 if (sdata->csa_block_tx) {
2447 ieee80211_wake_vif_queues(local, sdata,
2448 IEEE80211_QUEUE_STOP_REASON_CSA);
2449 sdata->csa_block_tx = false;
2450 }
2451 mutex_unlock(&local->mtx);
2452
2453 /* existing TX TSPEC sessions no longer exist */
2454 memset(ifmgd->tx_tspec, 0, sizeof(ifmgd->tx_tspec));
2455 cancel_delayed_work_sync(&ifmgd->tx_tspec_wk);
2456
2457 sdata->encrypt_headroom = IEEE80211_ENCRYPT_HEADROOM;
2458}
2459
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002460static void ieee80211_reset_ap_probe(struct ieee80211_sub_if_data *sdata)
2461{
2462 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2463 struct ieee80211_local *local = sdata->local;
2464
2465 mutex_lock(&local->mtx);
2466 if (!(ifmgd->flags & IEEE80211_STA_CONNECTION_POLL))
2467 goto out;
2468
2469 __ieee80211_stop_poll(sdata);
2470
2471 mutex_lock(&local->iflist_mtx);
2472 ieee80211_recalc_ps(local);
2473 mutex_unlock(&local->iflist_mtx);
2474
2475 if (ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
2476 goto out;
2477
2478 /*
2479 * We've received a probe response, but are not sure whether
2480 * we have or will be receiving any beacons or data, so let's
2481 * schedule the timers again, just in case.
2482 */
2483 ieee80211_sta_reset_beacon_monitor(sdata);
2484
2485 mod_timer(&ifmgd->conn_mon_timer,
2486 round_jiffies_up(jiffies +
2487 IEEE80211_CONNECTION_IDLE_TIME));
2488out:
2489 mutex_unlock(&local->mtx);
2490}
2491
2492static void ieee80211_sta_tx_wmm_ac_notify(struct ieee80211_sub_if_data *sdata,
2493 struct ieee80211_hdr *hdr,
2494 u16 tx_time)
2495{
2496 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Olivier Deprez157378f2022-04-04 15:47:50 +02002497 u16 tid;
2498 int ac;
2499 struct ieee80211_sta_tx_tspec *tx_tspec;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002500 unsigned long now = jiffies;
2501
Olivier Deprez157378f2022-04-04 15:47:50 +02002502 if (!ieee80211_is_data_qos(hdr->frame_control))
2503 return;
2504
2505 tid = ieee80211_get_tid(hdr);
2506 ac = ieee80211_ac_from_tid(tid);
2507 tx_tspec = &ifmgd->tx_tspec[ac];
2508
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002509 if (likely(!tx_tspec->admitted_time))
2510 return;
2511
2512 if (time_after(now, tx_tspec->time_slice_start + HZ)) {
2513 tx_tspec->consumed_tx_time = 0;
2514 tx_tspec->time_slice_start = now;
2515
2516 if (tx_tspec->downgraded) {
2517 tx_tspec->action = TX_TSPEC_ACTION_STOP_DOWNGRADE;
2518 schedule_delayed_work(&ifmgd->tx_tspec_wk, 0);
2519 }
2520 }
2521
2522 if (tx_tspec->downgraded)
2523 return;
2524
2525 tx_tspec->consumed_tx_time += tx_time;
2526
2527 if (tx_tspec->consumed_tx_time >= tx_tspec->admitted_time) {
2528 tx_tspec->downgraded = true;
2529 tx_tspec->action = TX_TSPEC_ACTION_DOWNGRADE;
2530 schedule_delayed_work(&ifmgd->tx_tspec_wk, 0);
2531 }
2532}
2533
2534void ieee80211_sta_tx_notify(struct ieee80211_sub_if_data *sdata,
2535 struct ieee80211_hdr *hdr, bool ack, u16 tx_time)
2536{
2537 ieee80211_sta_tx_wmm_ac_notify(sdata, hdr, tx_time);
2538
Olivier Deprez157378f2022-04-04 15:47:50 +02002539 if (!ieee80211_is_any_nullfunc(hdr->frame_control) ||
2540 !sdata->u.mgd.probe_send_count)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002541 return;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002542
2543 if (ack)
Olivier Deprez157378f2022-04-04 15:47:50 +02002544 sdata->u.mgd.probe_send_count = 0;
2545 else
2546 sdata->u.mgd.nullfunc_failed = true;
2547 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002548}
2549
2550static void ieee80211_mlme_send_probe_req(struct ieee80211_sub_if_data *sdata,
2551 const u8 *src, const u8 *dst,
2552 const u8 *ssid, size_t ssid_len,
2553 struct ieee80211_channel *channel)
2554{
2555 struct sk_buff *skb;
2556
2557 skb = ieee80211_build_probe_req(sdata, src, dst, (u32)-1, channel,
2558 ssid, ssid_len, NULL, 0,
2559 IEEE80211_PROBE_FLAG_DIRECTED);
2560 if (skb)
2561 ieee80211_tx_skb(sdata, skb);
2562}
2563
2564static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
2565{
2566 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2567 const u8 *ssid;
2568 u8 *dst = ifmgd->associated->bssid;
2569 u8 unicast_limit = max(1, max_probe_tries - 3);
2570 struct sta_info *sta;
2571
2572 /*
2573 * Try sending broadcast probe requests for the last three
2574 * probe requests after the first ones failed since some
2575 * buggy APs only support broadcast probe requests.
2576 */
2577 if (ifmgd->probe_send_count >= unicast_limit)
2578 dst = NULL;
2579
2580 /*
2581 * When the hardware reports an accurate Tx ACK status, it's
2582 * better to send a nullfunc frame instead of a probe request,
2583 * as it will kick us off the AP quickly if we aren't associated
2584 * anymore. The timeout will be reset if the frame is ACKed by
2585 * the AP.
2586 */
2587 ifmgd->probe_send_count++;
2588
2589 if (dst) {
2590 mutex_lock(&sdata->local->sta_mtx);
2591 sta = sta_info_get(sdata, dst);
2592 if (!WARN_ON(!sta))
2593 ieee80211_check_fast_rx(sta);
2594 mutex_unlock(&sdata->local->sta_mtx);
2595 }
2596
2597 if (ieee80211_hw_check(&sdata->local->hw, REPORTS_TX_ACK_STATUS)) {
2598 ifmgd->nullfunc_failed = false;
David Brazdil0f672f62019-12-10 10:32:29 +00002599 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE))
2600 ifmgd->probe_send_count--;
2601 else
2602 ieee80211_send_nullfunc(sdata->local, sdata, false);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002603 } else {
2604 int ssid_len;
2605
2606 rcu_read_lock();
2607 ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
2608 if (WARN_ON_ONCE(ssid == NULL))
2609 ssid_len = 0;
2610 else
2611 ssid_len = ssid[1];
2612
2613 ieee80211_mlme_send_probe_req(sdata, sdata->vif.addr, dst,
2614 ssid + 2, ssid_len,
2615 ifmgd->associated->channel);
2616 rcu_read_unlock();
2617 }
2618
2619 ifmgd->probe_timeout = jiffies + msecs_to_jiffies(probe_wait_ms);
2620 run_again(sdata, ifmgd->probe_timeout);
2621}
2622
2623static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
2624 bool beacon)
2625{
2626 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2627 bool already = false;
2628
2629 if (!ieee80211_sdata_running(sdata))
2630 return;
2631
2632 sdata_lock(sdata);
2633
2634 if (!ifmgd->associated)
2635 goto out;
2636
2637 mutex_lock(&sdata->local->mtx);
2638
2639 if (sdata->local->tmp_channel || sdata->local->scanning) {
2640 mutex_unlock(&sdata->local->mtx);
2641 goto out;
2642 }
2643
2644 if (beacon) {
2645 mlme_dbg_ratelimited(sdata,
2646 "detected beacon loss from AP (missed %d beacons) - probing\n",
2647 beacon_loss_count);
2648
2649 ieee80211_cqm_beacon_loss_notify(&sdata->vif, GFP_KERNEL);
2650 }
2651
2652 /*
2653 * The driver/our work has already reported this event or the
2654 * connection monitoring has kicked in and we have already sent
2655 * a probe request. Or maybe the AP died and the driver keeps
2656 * reporting until we disassociate...
2657 *
2658 * In either case we have to ignore the current call to this
2659 * function (except for setting the correct probe reason bit)
2660 * because otherwise we would reset the timer every time and
2661 * never check whether we received a probe response!
2662 */
2663 if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL)
2664 already = true;
2665
2666 ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
2667
2668 mutex_unlock(&sdata->local->mtx);
2669
2670 if (already)
2671 goto out;
2672
2673 mutex_lock(&sdata->local->iflist_mtx);
2674 ieee80211_recalc_ps(sdata->local);
2675 mutex_unlock(&sdata->local->iflist_mtx);
2676
2677 ifmgd->probe_send_count = 0;
2678 ieee80211_mgd_probe_ap_send(sdata);
2679 out:
2680 sdata_unlock(sdata);
2681}
2682
2683struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw,
2684 struct ieee80211_vif *vif)
2685{
2686 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2687 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2688 struct cfg80211_bss *cbss;
2689 struct sk_buff *skb;
2690 const u8 *ssid;
2691 int ssid_len;
2692
2693 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
2694 return NULL;
2695
2696 sdata_assert_lock(sdata);
2697
2698 if (ifmgd->associated)
2699 cbss = ifmgd->associated;
2700 else if (ifmgd->auth_data)
2701 cbss = ifmgd->auth_data->bss;
2702 else if (ifmgd->assoc_data)
2703 cbss = ifmgd->assoc_data->bss;
2704 else
2705 return NULL;
2706
2707 rcu_read_lock();
2708 ssid = ieee80211_bss_get_ie(cbss, WLAN_EID_SSID);
David Brazdil0f672f62019-12-10 10:32:29 +00002709 if (WARN_ONCE(!ssid || ssid[1] > IEEE80211_MAX_SSID_LEN,
2710 "invalid SSID element (len=%d)", ssid ? ssid[1] : -1))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002711 ssid_len = 0;
2712 else
2713 ssid_len = ssid[1];
2714
2715 skb = ieee80211_build_probe_req(sdata, sdata->vif.addr, cbss->bssid,
2716 (u32) -1, cbss->channel,
2717 ssid + 2, ssid_len,
2718 NULL, 0, IEEE80211_PROBE_FLAG_DIRECTED);
2719 rcu_read_unlock();
2720
2721 return skb;
2722}
2723EXPORT_SYMBOL(ieee80211_ap_probereq_get);
2724
2725static void ieee80211_report_disconnect(struct ieee80211_sub_if_data *sdata,
2726 const u8 *buf, size_t len, bool tx,
2727 u16 reason)
2728{
2729 struct ieee80211_event event = {
2730 .type = MLME_EVENT,
2731 .u.mlme.data = tx ? DEAUTH_TX_EVENT : DEAUTH_RX_EVENT,
2732 .u.mlme.reason = reason,
2733 };
2734
2735 if (tx)
2736 cfg80211_tx_mlme_mgmt(sdata->dev, buf, len);
2737 else
2738 cfg80211_rx_mlme_mgmt(sdata->dev, buf, len);
2739
2740 drv_event_callback(sdata->local, sdata, &event);
2741}
2742
2743static void __ieee80211_disconnect(struct ieee80211_sub_if_data *sdata)
2744{
2745 struct ieee80211_local *local = sdata->local;
2746 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2747 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
2748 bool tx;
2749
2750 sdata_lock(sdata);
2751 if (!ifmgd->associated) {
2752 sdata_unlock(sdata);
2753 return;
2754 }
2755
2756 tx = !sdata->csa_block_tx;
2757
2758 /* AP is probably out of range (or not reachable for another reason) so
2759 * remove the bss struct for that AP.
2760 */
2761 cfg80211_unlink_bss(local->hw.wiphy, ifmgd->associated);
2762
2763 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
2764 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
2765 tx, frame_buf);
2766 mutex_lock(&local->mtx);
2767 sdata->vif.csa_active = false;
2768 ifmgd->csa_waiting_bcn = false;
2769 if (sdata->csa_block_tx) {
2770 ieee80211_wake_vif_queues(local, sdata,
2771 IEEE80211_QUEUE_STOP_REASON_CSA);
2772 sdata->csa_block_tx = false;
2773 }
2774 mutex_unlock(&local->mtx);
2775
2776 ieee80211_report_disconnect(sdata, frame_buf, sizeof(frame_buf), tx,
2777 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
2778
2779 sdata_unlock(sdata);
2780}
2781
2782static void ieee80211_beacon_connection_loss_work(struct work_struct *work)
2783{
2784 struct ieee80211_sub_if_data *sdata =
2785 container_of(work, struct ieee80211_sub_if_data,
2786 u.mgd.beacon_connection_loss_work);
2787 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2788
2789 if (ifmgd->associated)
2790 ifmgd->beacon_loss_count++;
2791
2792 if (ifmgd->connection_loss) {
2793 sdata_info(sdata, "Connection to AP %pM lost\n",
2794 ifmgd->bssid);
2795 __ieee80211_disconnect(sdata);
2796 } else {
2797 ieee80211_mgd_probe_ap(sdata, true);
2798 }
2799}
2800
2801static void ieee80211_csa_connection_drop_work(struct work_struct *work)
2802{
2803 struct ieee80211_sub_if_data *sdata =
2804 container_of(work, struct ieee80211_sub_if_data,
2805 u.mgd.csa_connection_drop_work);
2806
2807 __ieee80211_disconnect(sdata);
2808}
2809
2810void ieee80211_beacon_loss(struct ieee80211_vif *vif)
2811{
2812 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2813 struct ieee80211_hw *hw = &sdata->local->hw;
2814
2815 trace_api_beacon_loss(sdata);
2816
2817 sdata->u.mgd.connection_loss = false;
2818 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
2819}
2820EXPORT_SYMBOL(ieee80211_beacon_loss);
2821
2822void ieee80211_connection_loss(struct ieee80211_vif *vif)
2823{
2824 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2825 struct ieee80211_hw *hw = &sdata->local->hw;
2826
2827 trace_api_connection_loss(sdata);
2828
2829 sdata->u.mgd.connection_loss = true;
2830 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
2831}
2832EXPORT_SYMBOL(ieee80211_connection_loss);
2833
2834
2835static void ieee80211_destroy_auth_data(struct ieee80211_sub_if_data *sdata,
2836 bool assoc)
2837{
2838 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
2839
2840 sdata_assert_lock(sdata);
2841
2842 if (!assoc) {
2843 /*
2844 * we are not authenticated yet, the only timer that could be
2845 * running is the timeout for the authentication response which
2846 * which is not relevant anymore.
2847 */
2848 del_timer_sync(&sdata->u.mgd.timer);
2849 sta_info_destroy_addr(sdata, auth_data->bss->bssid);
2850
2851 eth_zero_addr(sdata->u.mgd.bssid);
2852 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
2853 sdata->u.mgd.flags = 0;
2854 mutex_lock(&sdata->local->mtx);
2855 ieee80211_vif_release_channel(sdata);
2856 mutex_unlock(&sdata->local->mtx);
2857 }
2858
2859 cfg80211_put_bss(sdata->local->hw.wiphy, auth_data->bss);
2860 kfree(auth_data);
2861 sdata->u.mgd.auth_data = NULL;
2862}
2863
2864static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata,
2865 bool assoc, bool abandon)
2866{
2867 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
2868
2869 sdata_assert_lock(sdata);
2870
2871 if (!assoc) {
2872 /*
2873 * we are not associated yet, the only timer that could be
2874 * running is the timeout for the association response which
2875 * which is not relevant anymore.
2876 */
2877 del_timer_sync(&sdata->u.mgd.timer);
2878 sta_info_destroy_addr(sdata, assoc_data->bss->bssid);
2879
2880 eth_zero_addr(sdata->u.mgd.bssid);
2881 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
2882 sdata->u.mgd.flags = 0;
2883 sdata->vif.mu_mimo_owner = false;
2884
2885 mutex_lock(&sdata->local->mtx);
2886 ieee80211_vif_release_channel(sdata);
2887 mutex_unlock(&sdata->local->mtx);
2888
2889 if (abandon)
2890 cfg80211_abandon_assoc(sdata->dev, assoc_data->bss);
2891 }
2892
2893 kfree(assoc_data);
2894 sdata->u.mgd.assoc_data = NULL;
2895}
2896
2897static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
2898 struct ieee80211_mgmt *mgmt, size_t len)
2899{
2900 struct ieee80211_local *local = sdata->local;
2901 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
2902 u8 *pos;
2903 struct ieee802_11_elems elems;
2904 u32 tx_flags = 0;
2905
2906 pos = mgmt->u.auth.variable;
David Brazdil0f672f62019-12-10 10:32:29 +00002907 ieee802_11_parse_elems(pos, len - (pos - (u8 *)mgmt), false, &elems,
2908 mgmt->bssid, auth_data->bss->bssid);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002909 if (!elems.challenge)
2910 return;
2911 auth_data->expected_transaction = 4;
2912 drv_mgd_prepare_tx(sdata->local, sdata, 0);
2913 if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
2914 tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
2915 IEEE80211_TX_INTFL_MLME_CONN_TX;
2916 ieee80211_send_auth(sdata, 3, auth_data->algorithm, 0,
2917 elems.challenge - 2, elems.challenge_len + 2,
2918 auth_data->bss->bssid, auth_data->bss->bssid,
2919 auth_data->key, auth_data->key_len,
2920 auth_data->key_idx, tx_flags);
2921}
2922
David Brazdil0f672f62019-12-10 10:32:29 +00002923static bool ieee80211_mark_sta_auth(struct ieee80211_sub_if_data *sdata,
2924 const u8 *bssid)
2925{
2926 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2927 struct sta_info *sta;
2928 bool result = true;
2929
2930 sdata_info(sdata, "authenticated\n");
2931 ifmgd->auth_data->done = true;
2932 ifmgd->auth_data->timeout = jiffies + IEEE80211_AUTH_WAIT_ASSOC;
2933 ifmgd->auth_data->timeout_started = true;
2934 run_again(sdata, ifmgd->auth_data->timeout);
2935
2936 /* move station state to auth */
2937 mutex_lock(&sdata->local->sta_mtx);
2938 sta = sta_info_get(sdata, bssid);
2939 if (!sta) {
2940 WARN_ONCE(1, "%s: STA %pM not found", sdata->name, bssid);
2941 result = false;
2942 goto out;
2943 }
2944 if (sta_info_move_state(sta, IEEE80211_STA_AUTH)) {
2945 sdata_info(sdata, "failed moving %pM to auth\n", bssid);
2946 result = false;
2947 goto out;
2948 }
2949
2950out:
2951 mutex_unlock(&sdata->local->sta_mtx);
2952 return result;
2953}
2954
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002955static void ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
2956 struct ieee80211_mgmt *mgmt, size_t len)
2957{
2958 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2959 u8 bssid[ETH_ALEN];
2960 u16 auth_alg, auth_transaction, status_code;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002961 struct ieee80211_event event = {
2962 .type = MLME_EVENT,
2963 .u.mlme.data = AUTH_EVENT,
2964 };
2965
2966 sdata_assert_lock(sdata);
2967
2968 if (len < 24 + 6)
2969 return;
2970
2971 if (!ifmgd->auth_data || ifmgd->auth_data->done)
2972 return;
2973
2974 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
2975
2976 if (!ether_addr_equal(bssid, mgmt->bssid))
2977 return;
2978
2979 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
2980 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
2981 status_code = le16_to_cpu(mgmt->u.auth.status_code);
2982
2983 if (auth_alg != ifmgd->auth_data->algorithm ||
David Brazdil0f672f62019-12-10 10:32:29 +00002984 (auth_alg != WLAN_AUTH_SAE &&
2985 auth_transaction != ifmgd->auth_data->expected_transaction) ||
2986 (auth_alg == WLAN_AUTH_SAE &&
2987 (auth_transaction < ifmgd->auth_data->expected_transaction ||
2988 auth_transaction > 2))) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002989 sdata_info(sdata, "%pM unexpected authentication state: alg %d (expected %d) transact %d (expected %d)\n",
2990 mgmt->sa, auth_alg, ifmgd->auth_data->algorithm,
2991 auth_transaction,
2992 ifmgd->auth_data->expected_transaction);
2993 return;
2994 }
2995
2996 if (status_code != WLAN_STATUS_SUCCESS) {
Olivier Deprez157378f2022-04-04 15:47:50 +02002997 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
2998
2999 if (auth_alg == WLAN_AUTH_SAE &&
3000 (status_code == WLAN_STATUS_ANTI_CLOG_REQUIRED ||
3001 (auth_transaction == 1 &&
3002 (status_code == WLAN_STATUS_SAE_HASH_TO_ELEMENT ||
3003 status_code == WLAN_STATUS_SAE_PK)))) {
3004 /* waiting for userspace now */
3005 ifmgd->auth_data->waiting = true;
3006 ifmgd->auth_data->timeout =
3007 jiffies + IEEE80211_AUTH_WAIT_SAE_RETRY;
3008 ifmgd->auth_data->timeout_started = true;
3009 run_again(sdata, ifmgd->auth_data->timeout);
3010 return;
3011 }
3012
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003013 sdata_info(sdata, "%pM denied authentication (status %d)\n",
3014 mgmt->sa, status_code);
3015 ieee80211_destroy_auth_data(sdata, false);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003016 event.u.mlme.status = MLME_DENIED;
3017 event.u.mlme.reason = status_code;
3018 drv_event_callback(sdata->local, sdata, &event);
3019 return;
3020 }
3021
3022 switch (ifmgd->auth_data->algorithm) {
3023 case WLAN_AUTH_OPEN:
3024 case WLAN_AUTH_LEAP:
3025 case WLAN_AUTH_FT:
3026 case WLAN_AUTH_SAE:
3027 case WLAN_AUTH_FILS_SK:
3028 case WLAN_AUTH_FILS_SK_PFS:
3029 case WLAN_AUTH_FILS_PK:
3030 break;
3031 case WLAN_AUTH_SHARED_KEY:
3032 if (ifmgd->auth_data->expected_transaction != 4) {
3033 ieee80211_auth_challenge(sdata, mgmt, len);
3034 /* need another frame */
3035 return;
3036 }
3037 break;
3038 default:
3039 WARN_ONCE(1, "invalid auth alg %d",
3040 ifmgd->auth_data->algorithm);
3041 return;
3042 }
3043
3044 event.u.mlme.status = MLME_SUCCESS;
3045 drv_event_callback(sdata->local, sdata, &event);
David Brazdil0f672f62019-12-10 10:32:29 +00003046 if (ifmgd->auth_data->algorithm != WLAN_AUTH_SAE ||
3047 (auth_transaction == 2 &&
3048 ifmgd->auth_data->expected_transaction == 2)) {
3049 if (!ieee80211_mark_sta_auth(sdata, bssid))
Olivier Deprez0e641232021-09-23 10:07:05 +02003050 return; /* ignore frame -- wait for timeout */
David Brazdil0f672f62019-12-10 10:32:29 +00003051 } else if (ifmgd->auth_data->algorithm == WLAN_AUTH_SAE &&
3052 auth_transaction == 2) {
3053 sdata_info(sdata, "SAE peer confirmed\n");
3054 ifmgd->auth_data->peer_confirmed = true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003055 }
3056
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003057 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003058}
3059
3060#define case_WLAN(type) \
3061 case WLAN_REASON_##type: return #type
3062
David Brazdil0f672f62019-12-10 10:32:29 +00003063const char *ieee80211_get_reason_code_string(u16 reason_code)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003064{
3065 switch (reason_code) {
3066 case_WLAN(UNSPECIFIED);
3067 case_WLAN(PREV_AUTH_NOT_VALID);
3068 case_WLAN(DEAUTH_LEAVING);
3069 case_WLAN(DISASSOC_DUE_TO_INACTIVITY);
3070 case_WLAN(DISASSOC_AP_BUSY);
3071 case_WLAN(CLASS2_FRAME_FROM_NONAUTH_STA);
3072 case_WLAN(CLASS3_FRAME_FROM_NONASSOC_STA);
3073 case_WLAN(DISASSOC_STA_HAS_LEFT);
3074 case_WLAN(STA_REQ_ASSOC_WITHOUT_AUTH);
3075 case_WLAN(DISASSOC_BAD_POWER);
3076 case_WLAN(DISASSOC_BAD_SUPP_CHAN);
3077 case_WLAN(INVALID_IE);
3078 case_WLAN(MIC_FAILURE);
3079 case_WLAN(4WAY_HANDSHAKE_TIMEOUT);
3080 case_WLAN(GROUP_KEY_HANDSHAKE_TIMEOUT);
3081 case_WLAN(IE_DIFFERENT);
3082 case_WLAN(INVALID_GROUP_CIPHER);
3083 case_WLAN(INVALID_PAIRWISE_CIPHER);
3084 case_WLAN(INVALID_AKMP);
3085 case_WLAN(UNSUPP_RSN_VERSION);
3086 case_WLAN(INVALID_RSN_IE_CAP);
3087 case_WLAN(IEEE8021X_FAILED);
3088 case_WLAN(CIPHER_SUITE_REJECTED);
3089 case_WLAN(DISASSOC_UNSPECIFIED_QOS);
3090 case_WLAN(DISASSOC_QAP_NO_BANDWIDTH);
3091 case_WLAN(DISASSOC_LOW_ACK);
3092 case_WLAN(DISASSOC_QAP_EXCEED_TXOP);
3093 case_WLAN(QSTA_LEAVE_QBSS);
3094 case_WLAN(QSTA_NOT_USE);
3095 case_WLAN(QSTA_REQUIRE_SETUP);
3096 case_WLAN(QSTA_TIMEOUT);
3097 case_WLAN(QSTA_CIPHER_NOT_SUPP);
3098 case_WLAN(MESH_PEER_CANCELED);
3099 case_WLAN(MESH_MAX_PEERS);
3100 case_WLAN(MESH_CONFIG);
3101 case_WLAN(MESH_CLOSE);
3102 case_WLAN(MESH_MAX_RETRIES);
3103 case_WLAN(MESH_CONFIRM_TIMEOUT);
3104 case_WLAN(MESH_INVALID_GTK);
3105 case_WLAN(MESH_INCONSISTENT_PARAM);
3106 case_WLAN(MESH_INVALID_SECURITY);
3107 case_WLAN(MESH_PATH_ERROR);
3108 case_WLAN(MESH_PATH_NOFORWARD);
3109 case_WLAN(MESH_PATH_DEST_UNREACHABLE);
3110 case_WLAN(MAC_EXISTS_IN_MBSS);
3111 case_WLAN(MESH_CHAN_REGULATORY);
3112 case_WLAN(MESH_CHAN);
3113 default: return "<unknown>";
3114 }
3115}
3116
3117static void ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
3118 struct ieee80211_mgmt *mgmt, size_t len)
3119{
3120 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3121 u16 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
3122
3123 sdata_assert_lock(sdata);
3124
3125 if (len < 24 + 2)
3126 return;
3127
David Brazdil0f672f62019-12-10 10:32:29 +00003128 if (!ether_addr_equal(mgmt->bssid, mgmt->sa)) {
3129 ieee80211_tdls_handle_disconnect(sdata, mgmt->sa, reason_code);
3130 return;
3131 }
3132
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003133 if (ifmgd->associated &&
3134 ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid)) {
3135 const u8 *bssid = ifmgd->associated->bssid;
3136
3137 sdata_info(sdata, "deauthenticated from %pM (Reason: %u=%s)\n",
3138 bssid, reason_code,
3139 ieee80211_get_reason_code_string(reason_code));
3140
3141 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
3142
3143 ieee80211_report_disconnect(sdata, (u8 *)mgmt, len, false,
3144 reason_code);
3145 return;
3146 }
3147
3148 if (ifmgd->assoc_data &&
3149 ether_addr_equal(mgmt->bssid, ifmgd->assoc_data->bss->bssid)) {
3150 const u8 *bssid = ifmgd->assoc_data->bss->bssid;
3151
3152 sdata_info(sdata,
3153 "deauthenticated from %pM while associating (Reason: %u=%s)\n",
3154 bssid, reason_code,
3155 ieee80211_get_reason_code_string(reason_code));
3156
3157 ieee80211_destroy_assoc_data(sdata, false, true);
3158
3159 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
3160 return;
3161 }
3162}
3163
3164
3165static void ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
3166 struct ieee80211_mgmt *mgmt, size_t len)
3167{
3168 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3169 u16 reason_code;
3170
3171 sdata_assert_lock(sdata);
3172
3173 if (len < 24 + 2)
3174 return;
3175
3176 if (!ifmgd->associated ||
3177 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
3178 return;
3179
3180 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
3181
David Brazdil0f672f62019-12-10 10:32:29 +00003182 if (!ether_addr_equal(mgmt->bssid, mgmt->sa)) {
3183 ieee80211_tdls_handle_disconnect(sdata, mgmt->sa, reason_code);
3184 return;
3185 }
3186
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003187 sdata_info(sdata, "disassociated from %pM (Reason: %u=%s)\n",
3188 mgmt->sa, reason_code,
3189 ieee80211_get_reason_code_string(reason_code));
3190
3191 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
3192
3193 ieee80211_report_disconnect(sdata, (u8 *)mgmt, len, false, reason_code);
3194}
3195
3196static void ieee80211_get_rates(struct ieee80211_supported_band *sband,
3197 u8 *supp_rates, unsigned int supp_rates_len,
3198 u32 *rates, u32 *basic_rates,
3199 bool *have_higher_than_11mbit,
3200 int *min_rate, int *min_rate_index,
3201 int shift)
3202{
3203 int i, j;
3204
3205 for (i = 0; i < supp_rates_len; i++) {
3206 int rate = supp_rates[i] & 0x7f;
3207 bool is_basic = !!(supp_rates[i] & 0x80);
3208
3209 if ((rate * 5 * (1 << shift)) > 110)
3210 *have_higher_than_11mbit = true;
3211
3212 /*
Olivier Deprez157378f2022-04-04 15:47:50 +02003213 * Skip HT, VHT and HE BSS membership selectors since they're
3214 * not rates.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003215 *
3216 * Note: Even though the membership selector and the basic
3217 * rate flag share the same bit, they are not exactly
3218 * the same.
3219 */
3220 if (supp_rates[i] == (0x80 | BSS_MEMBERSHIP_SELECTOR_HT_PHY) ||
Olivier Deprez157378f2022-04-04 15:47:50 +02003221 supp_rates[i] == (0x80 | BSS_MEMBERSHIP_SELECTOR_VHT_PHY) ||
3222 supp_rates[i] == (0x80 | BSS_MEMBERSHIP_SELECTOR_HE_PHY))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003223 continue;
3224
3225 for (j = 0; j < sband->n_bitrates; j++) {
3226 struct ieee80211_rate *br;
3227 int brate;
3228
3229 br = &sband->bitrates[j];
3230
3231 brate = DIV_ROUND_UP(br->bitrate, (1 << shift) * 5);
3232 if (brate == rate) {
3233 *rates |= BIT(j);
3234 if (is_basic)
3235 *basic_rates |= BIT(j);
3236 if ((rate * 5) < *min_rate) {
3237 *min_rate = rate * 5;
3238 *min_rate_index = j;
3239 }
3240 break;
3241 }
3242 }
3243 }
3244}
3245
David Brazdil0f672f62019-12-10 10:32:29 +00003246static bool ieee80211_twt_req_supported(const struct sta_info *sta,
3247 const struct ieee802_11_elems *elems)
3248{
3249 if (elems->ext_capab_len < 10)
3250 return false;
3251
3252 if (!(elems->ext_capab[9] & WLAN_EXT_CAPA10_TWT_RESPONDER_SUPPORT))
3253 return false;
3254
3255 return sta->sta.he_cap.he_cap_elem.mac_cap_info[0] &
3256 IEEE80211_HE_MAC_CAP0_TWT_RES;
3257}
3258
3259static int ieee80211_recalc_twt_req(struct ieee80211_sub_if_data *sdata,
3260 struct sta_info *sta,
3261 struct ieee802_11_elems *elems)
3262{
3263 bool twt = ieee80211_twt_req_supported(sta, elems);
3264
3265 if (sdata->vif.bss_conf.twt_requester != twt) {
3266 sdata->vif.bss_conf.twt_requester = twt;
3267 return BSS_CHANGED_TWT;
3268 }
3269 return 0;
3270}
3271
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003272static bool ieee80211_assoc_success(struct ieee80211_sub_if_data *sdata,
3273 struct cfg80211_bss *cbss,
Olivier Deprez157378f2022-04-04 15:47:50 +02003274 struct ieee80211_mgmt *mgmt, size_t len,
3275 struct ieee802_11_elems *elems)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003276{
3277 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3278 struct ieee80211_local *local = sdata->local;
3279 struct ieee80211_supported_band *sband;
3280 struct sta_info *sta;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003281 u16 capab_info, aid;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003282 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
3283 const struct cfg80211_bss_ies *bss_ies = NULL;
3284 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
Olivier Deprez157378f2022-04-04 15:47:50 +02003285 bool is_6ghz = cbss->channel->band == NL80211_BAND_6GHZ;
3286 bool is_s1g = cbss->channel->band == NL80211_BAND_S1GHZ;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003287 u32 changed = 0;
Olivier Deprez157378f2022-04-04 15:47:50 +02003288 u8 *pos;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003289 int err;
3290 bool ret;
3291
3292 /* AssocResp and ReassocResp have identical structure */
3293
Olivier Deprez157378f2022-04-04 15:47:50 +02003294 pos = mgmt->u.assoc_resp.variable;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003295 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
Olivier Deprez157378f2022-04-04 15:47:50 +02003296 if (is_s1g) {
3297 pos = (u8 *) mgmt->u.s1g_assoc_resp.variable;
3298 aid = 0; /* TODO */
3299 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003300 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
Olivier Deprez157378f2022-04-04 15:47:50 +02003301 ieee802_11_parse_elems(pos, len - (pos - (u8 *)mgmt), false, elems,
3302 mgmt->bssid, assoc_data->bss->bssid);
3303
3304 if (elems->aid_resp)
3305 aid = le16_to_cpu(elems->aid_resp->aid);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003306
3307 /*
3308 * The 5 MSB of the AID field are reserved
3309 * (802.11-2016 9.4.1.8 AID field)
3310 */
3311 aid &= 0x7ff;
3312
3313 ifmgd->broken_ap = false;
3314
3315 if (aid == 0 || aid > IEEE80211_MAX_AID) {
3316 sdata_info(sdata, "invalid AID value %d (out of range), turn off PS\n",
3317 aid);
3318 aid = 0;
3319 ifmgd->broken_ap = true;
3320 }
3321
Olivier Deprez157378f2022-04-04 15:47:50 +02003322 if (!is_s1g && !elems->supp_rates) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003323 sdata_info(sdata, "no SuppRates element in AssocResp\n");
3324 return false;
3325 }
3326
Olivier Deprez157378f2022-04-04 15:47:50 +02003327 sdata->vif.bss_conf.aid = aid;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003328 ifmgd->tdls_chan_switch_prohibited =
Olivier Deprez157378f2022-04-04 15:47:50 +02003329 elems->ext_capab && elems->ext_capab_len >= 5 &&
3330 (elems->ext_capab[4] & WLAN_EXT_CAPA5_TDLS_CH_SW_PROHIBITED);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003331
3332 /*
3333 * Some APs are erroneously not including some information in their
3334 * (re)association response frames. Try to recover by using the data
3335 * from the beacon or probe response. This seems to afflict mobile
3336 * 2G/3G/4G wifi routers, reported models include the "Onda PN51T",
3337 * "Vodafone PocketWiFi 2", "ZTE MF60" and a similar T-Mobile device.
3338 */
Olivier Deprez157378f2022-04-04 15:47:50 +02003339 if (!is_6ghz &&
3340 ((assoc_data->wmm && !elems->wmm_param) ||
3341 (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
3342 (!elems->ht_cap_elem || !elems->ht_operation)) ||
3343 (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
3344 (!elems->vht_cap_elem || !elems->vht_operation)))) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003345 const struct cfg80211_bss_ies *ies;
3346 struct ieee802_11_elems bss_elems;
3347
3348 rcu_read_lock();
3349 ies = rcu_dereference(cbss->ies);
3350 if (ies)
3351 bss_ies = kmemdup(ies, sizeof(*ies) + ies->len,
3352 GFP_ATOMIC);
3353 rcu_read_unlock();
3354 if (!bss_ies)
3355 return false;
3356
3357 ieee802_11_parse_elems(bss_ies->data, bss_ies->len,
David Brazdil0f672f62019-12-10 10:32:29 +00003358 false, &bss_elems,
3359 mgmt->bssid,
3360 assoc_data->bss->bssid);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003361 if (assoc_data->wmm &&
Olivier Deprez157378f2022-04-04 15:47:50 +02003362 !elems->wmm_param && bss_elems.wmm_param) {
3363 elems->wmm_param = bss_elems.wmm_param;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003364 sdata_info(sdata,
3365 "AP bug: WMM param missing from AssocResp\n");
3366 }
3367
3368 /*
3369 * Also check if we requested HT/VHT, otherwise the AP doesn't
3370 * have to include the IEs in the (re)association response.
3371 */
Olivier Deprez157378f2022-04-04 15:47:50 +02003372 if (!elems->ht_cap_elem && bss_elems.ht_cap_elem &&
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003373 !(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
Olivier Deprez157378f2022-04-04 15:47:50 +02003374 elems->ht_cap_elem = bss_elems.ht_cap_elem;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003375 sdata_info(sdata,
3376 "AP bug: HT capability missing from AssocResp\n");
3377 }
Olivier Deprez157378f2022-04-04 15:47:50 +02003378 if (!elems->ht_operation && bss_elems.ht_operation &&
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003379 !(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
Olivier Deprez157378f2022-04-04 15:47:50 +02003380 elems->ht_operation = bss_elems.ht_operation;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003381 sdata_info(sdata,
3382 "AP bug: HT operation missing from AssocResp\n");
3383 }
Olivier Deprez157378f2022-04-04 15:47:50 +02003384 if (!elems->vht_cap_elem && bss_elems.vht_cap_elem &&
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003385 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)) {
Olivier Deprez157378f2022-04-04 15:47:50 +02003386 elems->vht_cap_elem = bss_elems.vht_cap_elem;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003387 sdata_info(sdata,
3388 "AP bug: VHT capa missing from AssocResp\n");
3389 }
Olivier Deprez157378f2022-04-04 15:47:50 +02003390 if (!elems->vht_operation && bss_elems.vht_operation &&
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003391 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)) {
Olivier Deprez157378f2022-04-04 15:47:50 +02003392 elems->vht_operation = bss_elems.vht_operation;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003393 sdata_info(sdata,
3394 "AP bug: VHT operation missing from AssocResp\n");
3395 }
3396 }
3397
3398 /*
3399 * We previously checked these in the beacon/probe response, so
3400 * they should be present here. This is just a safety net.
3401 */
Olivier Deprez157378f2022-04-04 15:47:50 +02003402 if (!is_6ghz && !(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
3403 (!elems->wmm_param || !elems->ht_cap_elem || !elems->ht_operation)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003404 sdata_info(sdata,
3405 "HT AP is missing WMM params or HT capability/operation\n");
3406 ret = false;
3407 goto out;
3408 }
3409
Olivier Deprez157378f2022-04-04 15:47:50 +02003410 if (!is_6ghz && !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
3411 (!elems->vht_cap_elem || !elems->vht_operation)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003412 sdata_info(sdata,
3413 "VHT AP is missing VHT capability/operation\n");
3414 ret = false;
3415 goto out;
3416 }
3417
Olivier Deprez157378f2022-04-04 15:47:50 +02003418 if (is_6ghz && !(ifmgd->flags & IEEE80211_STA_DISABLE_HE) &&
3419 !elems->he_6ghz_capa) {
3420 sdata_info(sdata,
3421 "HE 6 GHz AP is missing HE 6 GHz band capability\n");
3422 ret = false;
3423 goto out;
3424 }
3425
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003426 mutex_lock(&sdata->local->sta_mtx);
3427 /*
3428 * station info was already allocated and inserted before
3429 * the association and should be available to us
3430 */
3431 sta = sta_info_get(sdata, cbss->bssid);
3432 if (WARN_ON(!sta)) {
3433 mutex_unlock(&sdata->local->sta_mtx);
3434 ret = false;
3435 goto out;
3436 }
3437
3438 sband = ieee80211_get_sband(sdata);
3439 if (!sband) {
3440 mutex_unlock(&sdata->local->sta_mtx);
3441 ret = false;
3442 goto out;
3443 }
3444
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003445 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE) &&
Olivier Deprez157378f2022-04-04 15:47:50 +02003446 (!elems->he_cap || !elems->he_operation)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003447 mutex_unlock(&sdata->local->sta_mtx);
3448 sdata_info(sdata,
3449 "HE AP is missing HE capability/operation\n");
3450 ret = false;
3451 goto out;
3452 }
3453
3454 /* Set up internal HT/VHT capabilities */
Olivier Deprez157378f2022-04-04 15:47:50 +02003455 if (elems->ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003456 ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
Olivier Deprez157378f2022-04-04 15:47:50 +02003457 elems->ht_cap_elem, sta);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003458
Olivier Deprez157378f2022-04-04 15:47:50 +02003459 if (elems->vht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003460 ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband,
Olivier Deprez157378f2022-04-04 15:47:50 +02003461 elems->vht_cap_elem, sta);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003462
Olivier Deprez157378f2022-04-04 15:47:50 +02003463 if (elems->he_operation && !(ifmgd->flags & IEEE80211_STA_DISABLE_HE) &&
3464 elems->he_cap) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003465 ieee80211_he_cap_ie_to_sta_he_cap(sdata, sband,
Olivier Deprez157378f2022-04-04 15:47:50 +02003466 elems->he_cap,
3467 elems->he_cap_len,
3468 elems->he_6ghz_capa,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003469 sta);
3470
3471 bss_conf->he_support = sta->sta.he_cap.has_he;
Olivier Deprez157378f2022-04-04 15:47:50 +02003472 if (elems->rsnx && elems->rsnx_len &&
3473 (elems->rsnx[0] & WLAN_RSNX_CAPA_PROTECTED_TWT) &&
3474 wiphy_ext_feature_isset(local->hw.wiphy,
3475 NL80211_EXT_FEATURE_PROTECTED_TWT))
3476 bss_conf->twt_protected = true;
3477 else
3478 bss_conf->twt_protected = false;
3479
3480 changed |= ieee80211_recalc_twt_req(sdata, sta, elems);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003481 } else {
3482 bss_conf->he_support = false;
David Brazdil0f672f62019-12-10 10:32:29 +00003483 bss_conf->twt_requester = false;
Olivier Deprez157378f2022-04-04 15:47:50 +02003484 bss_conf->twt_protected = false;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003485 }
3486
3487 if (bss_conf->he_support) {
Olivier Deprez157378f2022-04-04 15:47:50 +02003488 bss_conf->he_bss_color.color =
3489 le32_get_bits(elems->he_operation->he_oper_params,
David Brazdil0f672f62019-12-10 10:32:29 +00003490 IEEE80211_HE_OPERATION_BSS_COLOR_MASK);
Olivier Deprez157378f2022-04-04 15:47:50 +02003491 bss_conf->he_bss_color.partial =
3492 le32_get_bits(elems->he_operation->he_oper_params,
3493 IEEE80211_HE_OPERATION_PARTIAL_BSS_COLOR);
3494 bss_conf->he_bss_color.enabled =
3495 !le32_get_bits(elems->he_operation->he_oper_params,
3496 IEEE80211_HE_OPERATION_BSS_COLOR_DISABLED);
3497
3498 if (bss_conf->he_bss_color.enabled)
3499 changed |= BSS_CHANGED_HE_BSS_COLOR;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003500
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003501 bss_conf->htc_trig_based_pkt_ext =
Olivier Deprez157378f2022-04-04 15:47:50 +02003502 le32_get_bits(elems->he_operation->he_oper_params,
David Brazdil0f672f62019-12-10 10:32:29 +00003503 IEEE80211_HE_OPERATION_DFLT_PE_DURATION_MASK);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003504 bss_conf->frame_time_rts_th =
Olivier Deprez157378f2022-04-04 15:47:50 +02003505 le32_get_bits(elems->he_operation->he_oper_params,
David Brazdil0f672f62019-12-10 10:32:29 +00003506 IEEE80211_HE_OPERATION_RTS_THRESHOLD_MASK);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003507
3508 bss_conf->multi_sta_back_32bit =
3509 sta->sta.he_cap.he_cap_elem.mac_cap_info[2] &
3510 IEEE80211_HE_MAC_CAP2_32BIT_BA_BITMAP;
3511
3512 bss_conf->ack_enabled =
3513 sta->sta.he_cap.he_cap_elem.mac_cap_info[2] &
3514 IEEE80211_HE_MAC_CAP2_ACK_EN;
3515
Olivier Deprez157378f2022-04-04 15:47:50 +02003516 bss_conf->uora_exists = !!elems->uora_element;
3517 if (elems->uora_element)
3518 bss_conf->uora_ocw_range = elems->uora_element[0];
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003519
Olivier Deprez157378f2022-04-04 15:47:50 +02003520 ieee80211_he_op_ie_to_bss_conf(&sdata->vif, elems->he_operation);
3521 ieee80211_he_spr_ie_to_bss_conf(&sdata->vif, elems->he_spr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003522 /* TODO: OPEN: what happens if BSS color disable is set? */
3523 }
3524
David Brazdil0f672f62019-12-10 10:32:29 +00003525 if (cbss->transmitted_bss) {
3526 bss_conf->nontransmitted = true;
3527 ether_addr_copy(bss_conf->transmitter_bssid,
3528 cbss->transmitted_bss->bssid);
3529 bss_conf->bssid_indicator = cbss->max_bssid_indicator;
3530 bss_conf->bssid_index = cbss->bssid_index;
3531 }
3532
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003533 /*
3534 * Some APs, e.g. Netgear WNDR3700, report invalid HT operation data
3535 * in their association response, so ignore that data for our own
3536 * configuration. If it changed since the last beacon, we'll get the
3537 * next beacon and update then.
3538 */
3539
3540 /*
3541 * If an operating mode notification IE is present, override the
3542 * NSS calculation (that would be done in rate_control_rate_init())
3543 * and use the # of streams from that element.
3544 */
Olivier Deprez157378f2022-04-04 15:47:50 +02003545 if (elems->opmode_notif &&
3546 !(*elems->opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_TYPE_BF)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003547 u8 nss;
3548
Olivier Deprez157378f2022-04-04 15:47:50 +02003549 nss = *elems->opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_MASK;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003550 nss >>= IEEE80211_OPMODE_NOTIF_RX_NSS_SHIFT;
3551 nss += 1;
3552 sta->sta.rx_nss = nss;
3553 }
3554
3555 rate_control_rate_init(sta);
3556
3557 if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED) {
3558 set_sta_flag(sta, WLAN_STA_MFP);
3559 sta->sta.mfp = true;
3560 } else {
3561 sta->sta.mfp = false;
3562 }
3563
Olivier Deprez157378f2022-04-04 15:47:50 +02003564 sta->sta.wme = (elems->wmm_param || elems->s1g_capab) &&
3565 local->hw.queues >= IEEE80211_NUM_ACS;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003566
3567 err = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
3568 if (!err && !(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
3569 err = sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
3570 if (err) {
3571 sdata_info(sdata,
3572 "failed to move station %pM to desired state\n",
3573 sta->sta.addr);
3574 WARN_ON(__sta_info_destroy(sta));
3575 mutex_unlock(&sdata->local->sta_mtx);
3576 ret = false;
3577 goto out;
3578 }
3579
Olivier Deprez157378f2022-04-04 15:47:50 +02003580 if (sdata->wdev.use_4addr)
3581 drv_sta_set_4addr(local, sdata, &sta->sta, true);
3582
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003583 mutex_unlock(&sdata->local->sta_mtx);
3584
3585 /*
3586 * Always handle WMM once after association regardless
3587 * of the first value the AP uses. Setting -1 here has
3588 * that effect because the AP values is an unsigned
3589 * 4-bit value.
3590 */
3591 ifmgd->wmm_last_param_set = -1;
David Brazdil0f672f62019-12-10 10:32:29 +00003592 ifmgd->mu_edca_last_param_set = -1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003593
3594 if (ifmgd->flags & IEEE80211_STA_DISABLE_WMM) {
3595 ieee80211_set_wmm_default(sdata, false, false);
Olivier Deprez157378f2022-04-04 15:47:50 +02003596 } else if (!ieee80211_sta_wmm_params(local, sdata, elems->wmm_param,
3597 elems->wmm_param_len,
3598 elems->mu_edca_param_set)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003599 /* still enable QoS since we might have HT/VHT */
3600 ieee80211_set_wmm_default(sdata, false, true);
3601 /* set the disable-WMM flag in this case to disable
3602 * tracking WMM parameter changes in the beacon if
3603 * the parameters weren't actually valid. Doing so
3604 * avoids changing parameters very strangely when
3605 * the AP is going back and forth between valid and
3606 * invalid parameters.
3607 */
3608 ifmgd->flags |= IEEE80211_STA_DISABLE_WMM;
3609 }
3610 changed |= BSS_CHANGED_QOS;
3611
Olivier Deprez157378f2022-04-04 15:47:50 +02003612 if (elems->max_idle_period_ie) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003613 bss_conf->max_idle_period =
Olivier Deprez157378f2022-04-04 15:47:50 +02003614 le16_to_cpu(elems->max_idle_period_ie->max_idle_period);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003615 bss_conf->protected_keep_alive =
Olivier Deprez157378f2022-04-04 15:47:50 +02003616 !!(elems->max_idle_period_ie->idle_options &
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003617 WLAN_IDLE_OPTIONS_PROTECTED_KEEP_ALIVE);
3618 changed |= BSS_CHANGED_KEEP_ALIVE;
3619 } else {
3620 bss_conf->max_idle_period = 0;
3621 bss_conf->protected_keep_alive = false;
3622 }
3623
Olivier Deprez157378f2022-04-04 15:47:50 +02003624 /* set assoc capability (AID was already set earlier),
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003625 * ieee80211_set_associated() will tell the driver */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003626 bss_conf->assoc_capability = capab_info;
3627 ieee80211_set_associated(sdata, cbss, changed);
3628
3629 /*
3630 * If we're using 4-addr mode, let the AP know that we're
3631 * doing so, so that it can create the STA VLAN on its side
3632 */
3633 if (ifmgd->use_4addr)
3634 ieee80211_send_4addr_nullfunc(local, sdata);
3635
3636 /*
3637 * Start timer to probe the connection to the AP now.
3638 * Also start the timer that will detect beacon loss.
3639 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003640 ieee80211_sta_reset_beacon_monitor(sdata);
Olivier Deprez157378f2022-04-04 15:47:50 +02003641 ieee80211_sta_reset_conn_monitor(sdata);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003642
3643 ret = true;
3644 out:
3645 kfree(bss_ies);
3646 return ret;
3647}
3648
3649static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
3650 struct ieee80211_mgmt *mgmt,
3651 size_t len)
3652{
3653 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3654 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
3655 u16 capab_info, status_code, aid;
3656 struct ieee802_11_elems elems;
3657 int ac, uapsd_queues = -1;
3658 u8 *pos;
3659 bool reassoc;
Olivier Deprez157378f2022-04-04 15:47:50 +02003660 struct cfg80211_bss *cbss;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003661 struct ieee80211_event event = {
3662 .type = MLME_EVENT,
3663 .u.mlme.data = ASSOC_EVENT,
3664 };
3665
3666 sdata_assert_lock(sdata);
3667
3668 if (!assoc_data)
3669 return;
Olivier Deprez157378f2022-04-04 15:47:50 +02003670
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003671 if (!ether_addr_equal(assoc_data->bss->bssid, mgmt->bssid))
3672 return;
3673
Olivier Deprez157378f2022-04-04 15:47:50 +02003674 cbss = assoc_data->bss;
3675
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003676 /*
3677 * AssocResp and ReassocResp have identical structure, so process both
3678 * of them in this function.
3679 */
3680
3681 if (len < 24 + 6)
3682 return;
3683
3684 reassoc = ieee80211_is_reassoc_resp(mgmt->frame_control);
3685 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
3686 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
Olivier Deprez157378f2022-04-04 15:47:50 +02003687 pos = mgmt->u.assoc_resp.variable;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003688 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
Olivier Deprez157378f2022-04-04 15:47:50 +02003689 if (cbss->channel->band == NL80211_BAND_S1GHZ) {
3690 pos = (u8 *) mgmt->u.s1g_assoc_resp.variable;
3691 aid = 0; /* TODO */
3692 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003693
3694 sdata_info(sdata,
3695 "RX %sssocResp from %pM (capab=0x%x status=%d aid=%d)\n",
3696 reassoc ? "Rea" : "A", mgmt->sa,
3697 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
3698
3699 if (assoc_data->fils_kek_len &&
3700 fils_decrypt_assoc_resp(sdata, (u8 *)mgmt, &len, assoc_data) < 0)
3701 return;
3702
David Brazdil0f672f62019-12-10 10:32:29 +00003703 ieee802_11_parse_elems(pos, len - (pos - (u8 *)mgmt), false, &elems,
3704 mgmt->bssid, assoc_data->bss->bssid);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003705
3706 if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
3707 elems.timeout_int &&
3708 elems.timeout_int->type == WLAN_TIMEOUT_ASSOC_COMEBACK) {
3709 u32 tu, ms;
3710 tu = le32_to_cpu(elems.timeout_int->value);
3711 ms = tu * 1024 / 1000;
3712 sdata_info(sdata,
3713 "%pM rejected association temporarily; comeback duration %u TU (%u ms)\n",
3714 mgmt->sa, tu, ms);
3715 assoc_data->timeout = jiffies + msecs_to_jiffies(ms);
3716 assoc_data->timeout_started = true;
3717 if (ms > IEEE80211_ASSOC_TIMEOUT)
3718 run_again(sdata, assoc_data->timeout);
3719 return;
3720 }
3721
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003722 if (status_code != WLAN_STATUS_SUCCESS) {
3723 sdata_info(sdata, "%pM denied association (code=%d)\n",
3724 mgmt->sa, status_code);
3725 ieee80211_destroy_assoc_data(sdata, false, false);
3726 event.u.mlme.status = MLME_DENIED;
3727 event.u.mlme.reason = status_code;
3728 drv_event_callback(sdata->local, sdata, &event);
3729 } else {
Olivier Deprez157378f2022-04-04 15:47:50 +02003730 if (!ieee80211_assoc_success(sdata, cbss, mgmt, len, &elems)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003731 /* oops -- internal error -- send timeout for now */
3732 ieee80211_destroy_assoc_data(sdata, false, false);
Olivier Deprez157378f2022-04-04 15:47:50 +02003733 cfg80211_assoc_timeout(sdata->dev, cbss);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003734 return;
3735 }
3736 event.u.mlme.status = MLME_SUCCESS;
3737 drv_event_callback(sdata->local, sdata, &event);
3738 sdata_info(sdata, "associated\n");
3739
3740 /*
3741 * destroy assoc_data afterwards, as otherwise an idle
3742 * recalc after assoc_data is NULL but before associated
3743 * is set can cause the interface to go idle
3744 */
3745 ieee80211_destroy_assoc_data(sdata, true, false);
3746
3747 /* get uapsd queues configuration */
3748 uapsd_queues = 0;
3749 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
3750 if (sdata->tx_conf[ac].uapsd)
3751 uapsd_queues |= ieee80211_ac_to_qos_mask[ac];
3752 }
3753
Olivier Deprez157378f2022-04-04 15:47:50 +02003754 cfg80211_rx_assoc_resp(sdata->dev, cbss, (u8 *)mgmt, len, uapsd_queues,
David Brazdil0f672f62019-12-10 10:32:29 +00003755 ifmgd->assoc_req_ies, ifmgd->assoc_req_ies_len);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003756}
3757
3758static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
3759 struct ieee80211_mgmt *mgmt, size_t len,
David Brazdil0f672f62019-12-10 10:32:29 +00003760 struct ieee80211_rx_status *rx_status)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003761{
3762 struct ieee80211_local *local = sdata->local;
3763 struct ieee80211_bss *bss;
3764 struct ieee80211_channel *channel;
3765
3766 sdata_assert_lock(sdata);
3767
Olivier Deprez157378f2022-04-04 15:47:50 +02003768 channel = ieee80211_get_channel_khz(local->hw.wiphy,
3769 ieee80211_rx_status_to_khz(rx_status));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003770 if (!channel)
3771 return;
3772
David Brazdil0f672f62019-12-10 10:32:29 +00003773 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, channel);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003774 if (bss) {
3775 sdata->vif.bss_conf.beacon_rate = bss->beacon_rate;
3776 ieee80211_rx_bss_put(local, bss);
3777 }
3778}
3779
3780
3781static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
3782 struct sk_buff *skb)
3783{
3784 struct ieee80211_mgmt *mgmt = (void *)skb->data;
3785 struct ieee80211_if_managed *ifmgd;
3786 struct ieee80211_rx_status *rx_status = (void *) skb->cb;
Olivier Deprez157378f2022-04-04 15:47:50 +02003787 struct ieee80211_channel *channel;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003788 size_t baselen, len = skb->len;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003789
3790 ifmgd = &sdata->u.mgd;
3791
3792 sdata_assert_lock(sdata);
3793
Olivier Deprez157378f2022-04-04 15:47:50 +02003794 /*
3795 * According to Draft P802.11ax D6.0 clause 26.17.2.3.2:
3796 * "If a 6 GHz AP receives a Probe Request frame and responds with
3797 * a Probe Response frame [..], the Address 1 field of the Probe
3798 * Response frame shall be set to the broadcast address [..]"
3799 * So, on 6GHz band we should also accept broadcast responses.
3800 */
3801 channel = ieee80211_get_channel(sdata->local->hw.wiphy,
3802 rx_status->freq);
3803 if (!channel)
3804 return;
3805
3806 if (!ether_addr_equal(mgmt->da, sdata->vif.addr) &&
3807 (channel->band != NL80211_BAND_6GHZ ||
3808 !is_broadcast_ether_addr(mgmt->da)))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003809 return; /* ignore ProbeResp to foreign address */
3810
3811 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
3812 if (baselen > len)
3813 return;
3814
David Brazdil0f672f62019-12-10 10:32:29 +00003815 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003816
3817 if (ifmgd->associated &&
3818 ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
3819 ieee80211_reset_ap_probe(sdata);
3820}
3821
3822/*
3823 * This is the canonical list of information elements we care about,
3824 * the filter code also gives us all changes to the Microsoft OUI
3825 * (00:50:F2) vendor IE which is used for WMM which we need to track,
3826 * as well as the DTPC IE (part of the Cisco OUI) used for signaling
3827 * changes to requested client power.
3828 *
3829 * We implement beacon filtering in software since that means we can
3830 * avoid processing the frame here and in cfg80211, and userspace
3831 * will not be able to tell whether the hardware supports it or not.
3832 *
3833 * XXX: This list needs to be dynamic -- userspace needs to be able to
3834 * add items it requires. It also needs to be able to tell us to
3835 * look out for other vendor IEs.
3836 */
3837static const u64 care_about_ies =
3838 (1ULL << WLAN_EID_COUNTRY) |
3839 (1ULL << WLAN_EID_ERP_INFO) |
3840 (1ULL << WLAN_EID_CHANNEL_SWITCH) |
3841 (1ULL << WLAN_EID_PWR_CONSTRAINT) |
3842 (1ULL << WLAN_EID_HT_CAPABILITY) |
3843 (1ULL << WLAN_EID_HT_OPERATION) |
3844 (1ULL << WLAN_EID_EXT_CHANSWITCH_ANN);
3845
3846static void ieee80211_handle_beacon_sig(struct ieee80211_sub_if_data *sdata,
3847 struct ieee80211_if_managed *ifmgd,
3848 struct ieee80211_bss_conf *bss_conf,
3849 struct ieee80211_local *local,
3850 struct ieee80211_rx_status *rx_status)
3851{
3852 /* Track average RSSI from the Beacon frames of the current AP */
3853
3854 if (ifmgd->flags & IEEE80211_STA_RESET_SIGNAL_AVE) {
3855 ifmgd->flags &= ~IEEE80211_STA_RESET_SIGNAL_AVE;
3856 ewma_beacon_signal_init(&ifmgd->ave_beacon_signal);
3857 ifmgd->last_cqm_event_signal = 0;
3858 ifmgd->count_beacon_signal = 1;
3859 ifmgd->last_ave_beacon_signal = 0;
3860 } else {
3861 ifmgd->count_beacon_signal++;
3862 }
3863
3864 ewma_beacon_signal_add(&ifmgd->ave_beacon_signal, -rx_status->signal);
3865
3866 if (ifmgd->rssi_min_thold != ifmgd->rssi_max_thold &&
3867 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
3868 int sig = -ewma_beacon_signal_read(&ifmgd->ave_beacon_signal);
3869 int last_sig = ifmgd->last_ave_beacon_signal;
3870 struct ieee80211_event event = {
3871 .type = RSSI_EVENT,
3872 };
3873
3874 /*
3875 * if signal crosses either of the boundaries, invoke callback
3876 * with appropriate parameters
3877 */
3878 if (sig > ifmgd->rssi_max_thold &&
3879 (last_sig <= ifmgd->rssi_min_thold || last_sig == 0)) {
3880 ifmgd->last_ave_beacon_signal = sig;
3881 event.u.rssi.data = RSSI_EVENT_HIGH;
3882 drv_event_callback(local, sdata, &event);
3883 } else if (sig < ifmgd->rssi_min_thold &&
3884 (last_sig >= ifmgd->rssi_max_thold ||
3885 last_sig == 0)) {
3886 ifmgd->last_ave_beacon_signal = sig;
3887 event.u.rssi.data = RSSI_EVENT_LOW;
3888 drv_event_callback(local, sdata, &event);
3889 }
3890 }
3891
3892 if (bss_conf->cqm_rssi_thold &&
3893 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT &&
3894 !(sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI)) {
3895 int sig = -ewma_beacon_signal_read(&ifmgd->ave_beacon_signal);
3896 int last_event = ifmgd->last_cqm_event_signal;
3897 int thold = bss_conf->cqm_rssi_thold;
3898 int hyst = bss_conf->cqm_rssi_hyst;
3899
3900 if (sig < thold &&
3901 (last_event == 0 || sig < last_event - hyst)) {
3902 ifmgd->last_cqm_event_signal = sig;
3903 ieee80211_cqm_rssi_notify(
3904 &sdata->vif,
3905 NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
3906 sig, GFP_KERNEL);
3907 } else if (sig > thold &&
3908 (last_event == 0 || sig > last_event + hyst)) {
3909 ifmgd->last_cqm_event_signal = sig;
3910 ieee80211_cqm_rssi_notify(
3911 &sdata->vif,
3912 NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
3913 sig, GFP_KERNEL);
3914 }
3915 }
3916
3917 if (bss_conf->cqm_rssi_low &&
3918 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
3919 int sig = -ewma_beacon_signal_read(&ifmgd->ave_beacon_signal);
3920 int last_event = ifmgd->last_cqm_event_signal;
3921 int low = bss_conf->cqm_rssi_low;
3922 int high = bss_conf->cqm_rssi_high;
3923
3924 if (sig < low &&
3925 (last_event == 0 || last_event >= low)) {
3926 ifmgd->last_cqm_event_signal = sig;
3927 ieee80211_cqm_rssi_notify(
3928 &sdata->vif,
3929 NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
3930 sig, GFP_KERNEL);
3931 } else if (sig > high &&
3932 (last_event == 0 || last_event <= high)) {
3933 ifmgd->last_cqm_event_signal = sig;
3934 ieee80211_cqm_rssi_notify(
3935 &sdata->vif,
3936 NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
3937 sig, GFP_KERNEL);
3938 }
3939 }
3940}
3941
David Brazdil0f672f62019-12-10 10:32:29 +00003942static bool ieee80211_rx_our_beacon(const u8 *tx_bssid,
3943 struct cfg80211_bss *bss)
3944{
3945 if (ether_addr_equal(tx_bssid, bss->bssid))
3946 return true;
3947 if (!bss->transmitted_bss)
3948 return false;
3949 return ether_addr_equal(tx_bssid, bss->transmitted_bss->bssid);
3950}
3951
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003952static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
Olivier Deprez157378f2022-04-04 15:47:50 +02003953 struct ieee80211_hdr *hdr, size_t len,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003954 struct ieee80211_rx_status *rx_status)
3955{
3956 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3957 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Olivier Deprez157378f2022-04-04 15:47:50 +02003958 struct ieee80211_mgmt *mgmt = (void *) hdr;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003959 size_t baselen;
3960 struct ieee802_11_elems elems;
3961 struct ieee80211_local *local = sdata->local;
3962 struct ieee80211_chanctx_conf *chanctx_conf;
3963 struct ieee80211_channel *chan;
3964 struct sta_info *sta;
3965 u32 changed = 0;
3966 bool erp_valid;
3967 u8 erp_value = 0;
Olivier Deprez157378f2022-04-04 15:47:50 +02003968 u32 ncrc = 0;
3969 u8 *bssid, *variable = mgmt->u.beacon.variable;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003970 u8 deauth_buf[IEEE80211_DEAUTH_FRAME_LEN];
3971
3972 sdata_assert_lock(sdata);
3973
3974 /* Process beacon from the current BSS */
Olivier Deprez157378f2022-04-04 15:47:50 +02003975 bssid = ieee80211_get_bssid(hdr, len, sdata->vif.type);
3976 if (ieee80211_is_s1g_beacon(mgmt->frame_control)) {
3977 struct ieee80211_ext *ext = (void *) mgmt;
3978
3979 if (ieee80211_is_s1g_short_beacon(ext->frame_control))
3980 variable = ext->u.s1g_short_beacon.variable;
3981 else
3982 variable = ext->u.s1g_beacon.variable;
3983 }
3984
3985 baselen = (u8 *) variable - (u8 *) mgmt;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003986 if (baselen > len)
3987 return;
3988
3989 rcu_read_lock();
3990 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
3991 if (!chanctx_conf) {
3992 rcu_read_unlock();
3993 return;
3994 }
3995
Olivier Deprez157378f2022-04-04 15:47:50 +02003996 if (ieee80211_rx_status_to_khz(rx_status) !=
3997 ieee80211_channel_to_khz(chanctx_conf->def.chan)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003998 rcu_read_unlock();
3999 return;
4000 }
4001 chan = chanctx_conf->def.chan;
4002 rcu_read_unlock();
4003
4004 if (ifmgd->assoc_data && ifmgd->assoc_data->need_beacon &&
Olivier Deprez157378f2022-04-04 15:47:50 +02004005 ieee80211_rx_our_beacon(bssid, ifmgd->assoc_data->bss)) {
4006 ieee802_11_parse_elems(variable,
David Brazdil0f672f62019-12-10 10:32:29 +00004007 len - baselen, false, &elems,
Olivier Deprez157378f2022-04-04 15:47:50 +02004008 bssid,
David Brazdil0f672f62019-12-10 10:32:29 +00004009 ifmgd->assoc_data->bss->bssid);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004010
David Brazdil0f672f62019-12-10 10:32:29 +00004011 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status);
4012
4013 if (elems.dtim_period)
4014 ifmgd->dtim_period = elems.dtim_period;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004015 ifmgd->have_beacon = true;
4016 ifmgd->assoc_data->need_beacon = false;
4017 if (ieee80211_hw_check(&local->hw, TIMING_BEACON_ONLY)) {
4018 sdata->vif.bss_conf.sync_tsf =
4019 le64_to_cpu(mgmt->u.beacon.timestamp);
4020 sdata->vif.bss_conf.sync_device_ts =
4021 rx_status->device_timestamp;
David Brazdil0f672f62019-12-10 10:32:29 +00004022 sdata->vif.bss_conf.sync_dtim_count = elems.dtim_count;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004023 }
David Brazdil0f672f62019-12-10 10:32:29 +00004024
4025 if (elems.mbssid_config_ie)
4026 bss_conf->profile_periodicity =
4027 elems.mbssid_config_ie->profile_periodicity;
Olivier Deprez157378f2022-04-04 15:47:50 +02004028 else
4029 bss_conf->profile_periodicity = 0;
David Brazdil0f672f62019-12-10 10:32:29 +00004030
4031 if (elems.ext_capab_len >= 11 &&
4032 (elems.ext_capab[10] & WLAN_EXT_CAPA11_EMA_SUPPORT))
4033 bss_conf->ema_ap = true;
Olivier Deprez157378f2022-04-04 15:47:50 +02004034 else
4035 bss_conf->ema_ap = false;
David Brazdil0f672f62019-12-10 10:32:29 +00004036
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004037 /* continue assoc process */
4038 ifmgd->assoc_data->timeout = jiffies;
4039 ifmgd->assoc_data->timeout_started = true;
4040 run_again(sdata, ifmgd->assoc_data->timeout);
4041 return;
4042 }
4043
4044 if (!ifmgd->associated ||
Olivier Deprez157378f2022-04-04 15:47:50 +02004045 !ieee80211_rx_our_beacon(bssid, ifmgd->associated))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004046 return;
4047 bssid = ifmgd->associated->bssid;
4048
4049 if (!(rx_status->flag & RX_FLAG_NO_SIGNAL_VAL))
4050 ieee80211_handle_beacon_sig(sdata, ifmgd, bss_conf,
4051 local, rx_status);
4052
4053 if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL) {
4054 mlme_dbg_ratelimited(sdata,
4055 "cancelling AP probe due to a received beacon\n");
4056 ieee80211_reset_ap_probe(sdata);
4057 }
4058
4059 /*
4060 * Push the beacon loss detection into the future since
4061 * we are processing a beacon from the AP just now.
4062 */
4063 ieee80211_sta_reset_beacon_monitor(sdata);
4064
Olivier Deprez157378f2022-04-04 15:47:50 +02004065 /* TODO: CRC urrently not calculated on S1G Beacon Compatibility
4066 * element (which carries the beacon interval). Don't forget to add a
4067 * bit to care_about_ies[] above if mac80211 is interested in a
4068 * changing S1G element.
4069 */
4070 if (!ieee80211_is_s1g_beacon(hdr->frame_control))
4071 ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
4072 ncrc = ieee802_11_parse_elems_crc(variable,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004073 len - baselen, false, &elems,
David Brazdil0f672f62019-12-10 10:32:29 +00004074 care_about_ies, ncrc,
4075 mgmt->bssid, bssid);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004076
4077 if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK) &&
Olivier Deprez157378f2022-04-04 15:47:50 +02004078 ieee80211_check_tim(elems.tim, elems.tim_len, bss_conf->aid)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004079 if (local->hw.conf.dynamic_ps_timeout > 0) {
4080 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
4081 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
4082 ieee80211_hw_config(local,
4083 IEEE80211_CONF_CHANGE_PS);
4084 }
4085 ieee80211_send_nullfunc(local, sdata, false);
4086 } else if (!local->pspolling && sdata->u.mgd.powersave) {
4087 local->pspolling = true;
4088
4089 /*
4090 * Here is assumed that the driver will be
4091 * able to send ps-poll frame and receive a
4092 * response even though power save mode is
4093 * enabled, but some drivers might require
4094 * to disable power save here. This needs
4095 * to be investigated.
4096 */
4097 ieee80211_send_pspoll(local, sdata);
4098 }
4099 }
4100
4101 if (sdata->vif.p2p ||
4102 sdata->vif.driver_flags & IEEE80211_VIF_GET_NOA_UPDATE) {
4103 struct ieee80211_p2p_noa_attr noa = {};
4104 int ret;
4105
Olivier Deprez157378f2022-04-04 15:47:50 +02004106 ret = cfg80211_get_p2p_attr(variable,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004107 len - baselen,
4108 IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
4109 (u8 *) &noa, sizeof(noa));
4110 if (ret >= 2) {
4111 if (sdata->u.mgd.p2p_noa_index != noa.index) {
4112 /* valid noa_attr and index changed */
4113 sdata->u.mgd.p2p_noa_index = noa.index;
4114 memcpy(&bss_conf->p2p_noa_attr, &noa, sizeof(noa));
4115 changed |= BSS_CHANGED_P2P_PS;
4116 /*
4117 * make sure we update all information, the CRC
4118 * mechanism doesn't look at P2P attributes.
4119 */
4120 ifmgd->beacon_crc_valid = false;
4121 }
4122 } else if (sdata->u.mgd.p2p_noa_index != -1) {
4123 /* noa_attr not found and we had valid noa_attr before */
4124 sdata->u.mgd.p2p_noa_index = -1;
4125 memset(&bss_conf->p2p_noa_attr, 0, sizeof(bss_conf->p2p_noa_attr));
4126 changed |= BSS_CHANGED_P2P_PS;
4127 ifmgd->beacon_crc_valid = false;
4128 }
4129 }
4130
4131 if (ifmgd->csa_waiting_bcn)
4132 ieee80211_chswitch_post_beacon(sdata);
4133
4134 /*
4135 * Update beacon timing and dtim count on every beacon appearance. This
4136 * will allow the driver to use the most updated values. Do it before
4137 * comparing this one with last received beacon.
4138 * IMPORTANT: These parameters would possibly be out of sync by the time
4139 * the driver will use them. The synchronized view is currently
4140 * guaranteed only in certain callbacks.
4141 */
Olivier Deprez157378f2022-04-04 15:47:50 +02004142 if (ieee80211_hw_check(&local->hw, TIMING_BEACON_ONLY) &&
4143 !ieee80211_is_s1g_beacon(hdr->frame_control)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004144 sdata->vif.bss_conf.sync_tsf =
4145 le64_to_cpu(mgmt->u.beacon.timestamp);
4146 sdata->vif.bss_conf.sync_device_ts =
4147 rx_status->device_timestamp;
David Brazdil0f672f62019-12-10 10:32:29 +00004148 sdata->vif.bss_conf.sync_dtim_count = elems.dtim_count;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004149 }
4150
Olivier Deprez157378f2022-04-04 15:47:50 +02004151 if ((ncrc == ifmgd->beacon_crc && ifmgd->beacon_crc_valid) ||
4152 ieee80211_is_s1g_short_beacon(mgmt->frame_control))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004153 return;
4154 ifmgd->beacon_crc = ncrc;
4155 ifmgd->beacon_crc_valid = true;
4156
David Brazdil0f672f62019-12-10 10:32:29 +00004157 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004158
4159 ieee80211_sta_process_chanswitch(sdata, rx_status->mactime,
4160 rx_status->device_timestamp,
4161 &elems, true);
4162
4163 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_WMM) &&
4164 ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
4165 elems.wmm_param_len,
4166 elems.mu_edca_param_set))
4167 changed |= BSS_CHANGED_QOS;
4168
4169 /*
4170 * If we haven't had a beacon before, tell the driver about the
4171 * DTIM period (and beacon timing if desired) now.
4172 */
4173 if (!ifmgd->have_beacon) {
4174 /* a few bogus AP send dtim_period = 0 or no TIM IE */
David Brazdil0f672f62019-12-10 10:32:29 +00004175 bss_conf->dtim_period = elems.dtim_period ?: 1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004176
4177 changed |= BSS_CHANGED_BEACON_INFO;
4178 ifmgd->have_beacon = true;
4179
4180 mutex_lock(&local->iflist_mtx);
4181 ieee80211_recalc_ps(local);
4182 mutex_unlock(&local->iflist_mtx);
4183
4184 ieee80211_recalc_ps_vif(sdata);
4185 }
4186
4187 if (elems.erp_info) {
4188 erp_valid = true;
4189 erp_value = elems.erp_info[0];
4190 } else {
4191 erp_valid = false;
4192 }
Olivier Deprez157378f2022-04-04 15:47:50 +02004193
4194 if (!ieee80211_is_s1g_beacon(hdr->frame_control))
4195 changed |= ieee80211_handle_bss_capability(sdata,
4196 le16_to_cpu(mgmt->u.beacon.capab_info),
4197 erp_valid, erp_value);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004198
4199 mutex_lock(&local->sta_mtx);
4200 sta = sta_info_get(sdata, bssid);
4201
David Brazdil0f672f62019-12-10 10:32:29 +00004202 changed |= ieee80211_recalc_twt_req(sdata, sta, &elems);
4203
Olivier Deprez157378f2022-04-04 15:47:50 +02004204 if (ieee80211_config_bw(sdata, sta, elems.ht_cap_elem,
4205 elems.vht_cap_elem, elems.ht_operation,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004206 elems.vht_operation, elems.he_operation,
Olivier Deprez157378f2022-04-04 15:47:50 +02004207 elems.s1g_oper, bssid, &changed)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004208 mutex_unlock(&local->sta_mtx);
4209 sdata_info(sdata,
4210 "failed to follow AP %pM bandwidth change, disconnect\n",
4211 bssid);
4212 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
4213 WLAN_REASON_DEAUTH_LEAVING,
4214 true, deauth_buf);
4215 ieee80211_report_disconnect(sdata, deauth_buf,
4216 sizeof(deauth_buf), true,
4217 WLAN_REASON_DEAUTH_LEAVING);
4218 return;
4219 }
4220
4221 if (sta && elems.opmode_notif)
4222 ieee80211_vht_handle_opmode(sdata, sta, *elems.opmode_notif,
4223 rx_status->band);
4224 mutex_unlock(&local->sta_mtx);
4225
4226 changed |= ieee80211_handle_pwr_constr(sdata, chan, mgmt,
4227 elems.country_elem,
4228 elems.country_elem_len,
4229 elems.pwr_constr_elem,
4230 elems.cisco_dtpc_elem);
4231
4232 ieee80211_bss_info_change_notify(sdata, changed);
4233}
4234
Olivier Deprez157378f2022-04-04 15:47:50 +02004235void ieee80211_sta_rx_queued_ext(struct ieee80211_sub_if_data *sdata,
4236 struct sk_buff *skb)
4237{
4238 struct ieee80211_rx_status *rx_status;
4239 struct ieee80211_hdr *hdr;
4240 u16 fc;
4241
4242 rx_status = (struct ieee80211_rx_status *) skb->cb;
4243 hdr = (struct ieee80211_hdr *) skb->data;
4244 fc = le16_to_cpu(hdr->frame_control);
4245
4246 sdata_lock(sdata);
4247 switch (fc & IEEE80211_FCTL_STYPE) {
4248 case IEEE80211_STYPE_S1G_BEACON:
4249 ieee80211_rx_mgmt_beacon(sdata, hdr, skb->len, rx_status);
4250 break;
4251 }
4252 sdata_unlock(sdata);
4253}
4254
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004255void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
4256 struct sk_buff *skb)
4257{
4258 struct ieee80211_rx_status *rx_status;
4259 struct ieee80211_mgmt *mgmt;
4260 u16 fc;
4261 struct ieee802_11_elems elems;
4262 int ies_len;
4263
4264 rx_status = (struct ieee80211_rx_status *) skb->cb;
4265 mgmt = (struct ieee80211_mgmt *) skb->data;
4266 fc = le16_to_cpu(mgmt->frame_control);
4267
4268 sdata_lock(sdata);
4269
4270 switch (fc & IEEE80211_FCTL_STYPE) {
4271 case IEEE80211_STYPE_BEACON:
Olivier Deprez157378f2022-04-04 15:47:50 +02004272 ieee80211_rx_mgmt_beacon(sdata, (void *)mgmt,
4273 skb->len, rx_status);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004274 break;
4275 case IEEE80211_STYPE_PROBE_RESP:
4276 ieee80211_rx_mgmt_probe_resp(sdata, skb);
4277 break;
4278 case IEEE80211_STYPE_AUTH:
4279 ieee80211_rx_mgmt_auth(sdata, mgmt, skb->len);
4280 break;
4281 case IEEE80211_STYPE_DEAUTH:
4282 ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
4283 break;
4284 case IEEE80211_STYPE_DISASSOC:
4285 ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
4286 break;
4287 case IEEE80211_STYPE_ASSOC_RESP:
4288 case IEEE80211_STYPE_REASSOC_RESP:
4289 ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len);
4290 break;
4291 case IEEE80211_STYPE_ACTION:
4292 if (mgmt->u.action.category == WLAN_CATEGORY_SPECTRUM_MGMT) {
4293 ies_len = skb->len -
4294 offsetof(struct ieee80211_mgmt,
4295 u.action.u.chan_switch.variable);
4296
4297 if (ies_len < 0)
4298 break;
4299
David Brazdil0f672f62019-12-10 10:32:29 +00004300 /* CSA IE cannot be overridden, no need for BSSID */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004301 ieee802_11_parse_elems(
4302 mgmt->u.action.u.chan_switch.variable,
David Brazdil0f672f62019-12-10 10:32:29 +00004303 ies_len, true, &elems, mgmt->bssid, NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004304
4305 if (elems.parse_error)
4306 break;
4307
4308 ieee80211_sta_process_chanswitch(sdata,
4309 rx_status->mactime,
4310 rx_status->device_timestamp,
4311 &elems, false);
4312 } else if (mgmt->u.action.category == WLAN_CATEGORY_PUBLIC) {
4313 ies_len = skb->len -
4314 offsetof(struct ieee80211_mgmt,
4315 u.action.u.ext_chan_switch.variable);
4316
4317 if (ies_len < 0)
4318 break;
4319
David Brazdil0f672f62019-12-10 10:32:29 +00004320 /*
4321 * extended CSA IE can't be overridden, no need for
4322 * BSSID
4323 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004324 ieee802_11_parse_elems(
4325 mgmt->u.action.u.ext_chan_switch.variable,
David Brazdil0f672f62019-12-10 10:32:29 +00004326 ies_len, true, &elems, mgmt->bssid, NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004327
4328 if (elems.parse_error)
4329 break;
4330
4331 /* for the handling code pretend this was also an IE */
4332 elems.ext_chansw_ie =
4333 &mgmt->u.action.u.ext_chan_switch.data;
4334
4335 ieee80211_sta_process_chanswitch(sdata,
4336 rx_status->mactime,
4337 rx_status->device_timestamp,
4338 &elems, false);
4339 }
4340 break;
4341 }
4342 sdata_unlock(sdata);
4343}
4344
4345static void ieee80211_sta_timer(struct timer_list *t)
4346{
4347 struct ieee80211_sub_if_data *sdata =
4348 from_timer(sdata, t, u.mgd.timer);
4349
4350 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
4351}
4352
4353static void ieee80211_sta_connection_lost(struct ieee80211_sub_if_data *sdata,
4354 u8 *bssid, u8 reason, bool tx)
4355{
4356 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
4357
4358 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH, reason,
4359 tx, frame_buf);
4360
4361 ieee80211_report_disconnect(sdata, frame_buf, sizeof(frame_buf), true,
4362 reason);
4363}
4364
4365static int ieee80211_auth(struct ieee80211_sub_if_data *sdata)
4366{
4367 struct ieee80211_local *local = sdata->local;
4368 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4369 struct ieee80211_mgd_auth_data *auth_data = ifmgd->auth_data;
4370 u32 tx_flags = 0;
4371 u16 trans = 1;
4372 u16 status = 0;
4373 u16 prepare_tx_duration = 0;
4374
4375 sdata_assert_lock(sdata);
4376
4377 if (WARN_ON_ONCE(!auth_data))
4378 return -EINVAL;
4379
4380 auth_data->tries++;
4381
4382 if (auth_data->tries > IEEE80211_AUTH_MAX_TRIES) {
4383 sdata_info(sdata, "authentication with %pM timed out\n",
4384 auth_data->bss->bssid);
4385
4386 /*
4387 * Most likely AP is not in the range so remove the
4388 * bss struct for that AP.
4389 */
4390 cfg80211_unlink_bss(local->hw.wiphy, auth_data->bss);
4391
4392 return -ETIMEDOUT;
4393 }
4394
4395 if (auth_data->algorithm == WLAN_AUTH_SAE)
4396 prepare_tx_duration =
4397 jiffies_to_msecs(IEEE80211_AUTH_TIMEOUT_SAE);
4398
4399 drv_mgd_prepare_tx(local, sdata, prepare_tx_duration);
4400
4401 sdata_info(sdata, "send auth to %pM (try %d/%d)\n",
4402 auth_data->bss->bssid, auth_data->tries,
4403 IEEE80211_AUTH_MAX_TRIES);
4404
4405 auth_data->expected_transaction = 2;
4406
4407 if (auth_data->algorithm == WLAN_AUTH_SAE) {
4408 trans = auth_data->sae_trans;
4409 status = auth_data->sae_status;
4410 auth_data->expected_transaction = trans;
4411 }
4412
4413 if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
4414 tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
4415 IEEE80211_TX_INTFL_MLME_CONN_TX;
4416
4417 ieee80211_send_auth(sdata, trans, auth_data->algorithm, status,
4418 auth_data->data, auth_data->data_len,
4419 auth_data->bss->bssid,
4420 auth_data->bss->bssid, NULL, 0, 0,
4421 tx_flags);
4422
4423 if (tx_flags == 0) {
4424 if (auth_data->algorithm == WLAN_AUTH_SAE)
4425 auth_data->timeout = jiffies +
4426 IEEE80211_AUTH_TIMEOUT_SAE;
4427 else
4428 auth_data->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
4429 } else {
4430 auth_data->timeout =
4431 round_jiffies_up(jiffies + IEEE80211_AUTH_TIMEOUT_LONG);
4432 }
4433
4434 auth_data->timeout_started = true;
4435 run_again(sdata, auth_data->timeout);
4436
4437 return 0;
4438}
4439
4440static int ieee80211_do_assoc(struct ieee80211_sub_if_data *sdata)
4441{
4442 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
4443 struct ieee80211_local *local = sdata->local;
4444
4445 sdata_assert_lock(sdata);
4446
4447 assoc_data->tries++;
4448 if (assoc_data->tries > IEEE80211_ASSOC_MAX_TRIES) {
4449 sdata_info(sdata, "association with %pM timed out\n",
4450 assoc_data->bss->bssid);
4451
4452 /*
4453 * Most likely AP is not in the range so remove the
4454 * bss struct for that AP.
4455 */
4456 cfg80211_unlink_bss(local->hw.wiphy, assoc_data->bss);
4457
4458 return -ETIMEDOUT;
4459 }
4460
4461 sdata_info(sdata, "associate with %pM (try %d/%d)\n",
4462 assoc_data->bss->bssid, assoc_data->tries,
4463 IEEE80211_ASSOC_MAX_TRIES);
4464 ieee80211_send_assoc(sdata);
4465
4466 if (!ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
4467 assoc_data->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
4468 assoc_data->timeout_started = true;
4469 run_again(sdata, assoc_data->timeout);
4470 } else {
4471 assoc_data->timeout =
4472 round_jiffies_up(jiffies +
4473 IEEE80211_ASSOC_TIMEOUT_LONG);
4474 assoc_data->timeout_started = true;
4475 run_again(sdata, assoc_data->timeout);
4476 }
4477
4478 return 0;
4479}
4480
4481void ieee80211_mgd_conn_tx_status(struct ieee80211_sub_if_data *sdata,
4482 __le16 fc, bool acked)
4483{
4484 struct ieee80211_local *local = sdata->local;
4485
4486 sdata->u.mgd.status_fc = fc;
4487 sdata->u.mgd.status_acked = acked;
4488 sdata->u.mgd.status_received = true;
4489
4490 ieee80211_queue_work(&local->hw, &sdata->work);
4491}
4492
4493void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
4494{
4495 struct ieee80211_local *local = sdata->local;
4496 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4497
4498 sdata_lock(sdata);
4499
4500 if (ifmgd->status_received) {
4501 __le16 fc = ifmgd->status_fc;
4502 bool status_acked = ifmgd->status_acked;
4503
4504 ifmgd->status_received = false;
4505 if (ifmgd->auth_data && ieee80211_is_auth(fc)) {
4506 if (status_acked) {
4507 if (ifmgd->auth_data->algorithm ==
4508 WLAN_AUTH_SAE)
4509 ifmgd->auth_data->timeout =
4510 jiffies +
4511 IEEE80211_AUTH_TIMEOUT_SAE;
4512 else
4513 ifmgd->auth_data->timeout =
4514 jiffies +
4515 IEEE80211_AUTH_TIMEOUT_SHORT;
4516 run_again(sdata, ifmgd->auth_data->timeout);
4517 } else {
4518 ifmgd->auth_data->timeout = jiffies - 1;
4519 }
4520 ifmgd->auth_data->timeout_started = true;
4521 } else if (ifmgd->assoc_data &&
4522 (ieee80211_is_assoc_req(fc) ||
4523 ieee80211_is_reassoc_req(fc))) {
4524 if (status_acked) {
4525 ifmgd->assoc_data->timeout =
4526 jiffies + IEEE80211_ASSOC_TIMEOUT_SHORT;
4527 run_again(sdata, ifmgd->assoc_data->timeout);
4528 } else {
4529 ifmgd->assoc_data->timeout = jiffies - 1;
4530 }
4531 ifmgd->assoc_data->timeout_started = true;
4532 }
4533 }
4534
4535 if (ifmgd->auth_data && ifmgd->auth_data->timeout_started &&
4536 time_after(jiffies, ifmgd->auth_data->timeout)) {
Olivier Deprez157378f2022-04-04 15:47:50 +02004537 if (ifmgd->auth_data->done || ifmgd->auth_data->waiting) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004538 /*
Olivier Deprez157378f2022-04-04 15:47:50 +02004539 * ok ... we waited for assoc or continuation but
4540 * userspace didn't do it, so kill the auth data
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004541 */
4542 ieee80211_destroy_auth_data(sdata, false);
4543 } else if (ieee80211_auth(sdata)) {
4544 u8 bssid[ETH_ALEN];
4545 struct ieee80211_event event = {
4546 .type = MLME_EVENT,
4547 .u.mlme.data = AUTH_EVENT,
4548 .u.mlme.status = MLME_TIMEOUT,
4549 };
4550
4551 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
4552
4553 ieee80211_destroy_auth_data(sdata, false);
4554
4555 cfg80211_auth_timeout(sdata->dev, bssid);
4556 drv_event_callback(sdata->local, sdata, &event);
4557 }
4558 } else if (ifmgd->auth_data && ifmgd->auth_data->timeout_started)
4559 run_again(sdata, ifmgd->auth_data->timeout);
4560
4561 if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started &&
4562 time_after(jiffies, ifmgd->assoc_data->timeout)) {
4563 if ((ifmgd->assoc_data->need_beacon && !ifmgd->have_beacon) ||
4564 ieee80211_do_assoc(sdata)) {
4565 struct cfg80211_bss *bss = ifmgd->assoc_data->bss;
4566 struct ieee80211_event event = {
4567 .type = MLME_EVENT,
4568 .u.mlme.data = ASSOC_EVENT,
4569 .u.mlme.status = MLME_TIMEOUT,
4570 };
4571
4572 ieee80211_destroy_assoc_data(sdata, false, false);
4573 cfg80211_assoc_timeout(sdata->dev, bss);
4574 drv_event_callback(sdata->local, sdata, &event);
4575 }
4576 } else if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started)
4577 run_again(sdata, ifmgd->assoc_data->timeout);
4578
4579 if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL &&
4580 ifmgd->associated) {
4581 u8 bssid[ETH_ALEN];
4582 int max_tries;
4583
4584 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
4585
4586 if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
4587 max_tries = max_nullfunc_tries;
4588 else
4589 max_tries = max_probe_tries;
4590
4591 /* ACK received for nullfunc probing frame */
4592 if (!ifmgd->probe_send_count)
4593 ieee80211_reset_ap_probe(sdata);
4594 else if (ifmgd->nullfunc_failed) {
4595 if (ifmgd->probe_send_count < max_tries) {
4596 mlme_dbg(sdata,
4597 "No ack for nullfunc frame to AP %pM, try %d/%i\n",
4598 bssid, ifmgd->probe_send_count,
4599 max_tries);
4600 ieee80211_mgd_probe_ap_send(sdata);
4601 } else {
4602 mlme_dbg(sdata,
4603 "No ack for nullfunc frame to AP %pM, disconnecting.\n",
4604 bssid);
4605 ieee80211_sta_connection_lost(sdata, bssid,
4606 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
4607 false);
4608 }
4609 } else if (time_is_after_jiffies(ifmgd->probe_timeout))
4610 run_again(sdata, ifmgd->probe_timeout);
4611 else if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
4612 mlme_dbg(sdata,
4613 "Failed to send nullfunc to AP %pM after %dms, disconnecting\n",
4614 bssid, probe_wait_ms);
4615 ieee80211_sta_connection_lost(sdata, bssid,
4616 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
4617 } else if (ifmgd->probe_send_count < max_tries) {
4618 mlme_dbg(sdata,
4619 "No probe response from AP %pM after %dms, try %d/%i\n",
4620 bssid, probe_wait_ms,
4621 ifmgd->probe_send_count, max_tries);
4622 ieee80211_mgd_probe_ap_send(sdata);
4623 } else {
4624 /*
4625 * We actually lost the connection ... or did we?
4626 * Let's make sure!
4627 */
4628 mlme_dbg(sdata,
4629 "No probe response from AP %pM after %dms, disconnecting.\n",
4630 bssid, probe_wait_ms);
4631
4632 ieee80211_sta_connection_lost(sdata, bssid,
4633 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
4634 }
4635 }
4636
4637 sdata_unlock(sdata);
4638}
4639
4640static void ieee80211_sta_bcn_mon_timer(struct timer_list *t)
4641{
4642 struct ieee80211_sub_if_data *sdata =
4643 from_timer(sdata, t, u.mgd.bcn_mon_timer);
4644 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4645
4646 if (sdata->vif.csa_active && !ifmgd->csa_waiting_bcn)
4647 return;
4648
Olivier Deprez157378f2022-04-04 15:47:50 +02004649 if (sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)
4650 return;
4651
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004652 sdata->u.mgd.connection_loss = false;
4653 ieee80211_queue_work(&sdata->local->hw,
4654 &sdata->u.mgd.beacon_connection_loss_work);
4655}
4656
4657static void ieee80211_sta_conn_mon_timer(struct timer_list *t)
4658{
4659 struct ieee80211_sub_if_data *sdata =
4660 from_timer(sdata, t, u.mgd.conn_mon_timer);
4661 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4662 struct ieee80211_local *local = sdata->local;
Olivier Deprez157378f2022-04-04 15:47:50 +02004663 struct sta_info *sta;
4664 unsigned long timeout;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004665
4666 if (sdata->vif.csa_active && !ifmgd->csa_waiting_bcn)
4667 return;
4668
Olivier Deprez157378f2022-04-04 15:47:50 +02004669 sta = sta_info_get(sdata, ifmgd->bssid);
4670 if (!sta)
4671 return;
4672
4673 timeout = sta->status_stats.last_ack;
4674 if (time_before(sta->status_stats.last_ack, sta->rx_stats.last_rx))
4675 timeout = sta->rx_stats.last_rx;
4676 timeout += IEEE80211_CONNECTION_IDLE_TIME;
4677
4678 /* If timeout is after now, then update timer to fire at
4679 * the later date, but do not actually probe at this time.
4680 */
4681 if (time_is_after_jiffies(timeout)) {
4682 mod_timer(&ifmgd->conn_mon_timer, round_jiffies_up(timeout));
4683 return;
4684 }
4685
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004686 ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
4687}
4688
4689static void ieee80211_sta_monitor_work(struct work_struct *work)
4690{
4691 struct ieee80211_sub_if_data *sdata =
4692 container_of(work, struct ieee80211_sub_if_data,
4693 u.mgd.monitor_work);
4694
4695 ieee80211_mgd_probe_ap(sdata, false);
4696}
4697
4698static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
4699{
4700 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
4701 __ieee80211_stop_poll(sdata);
4702
4703 /* let's probe the connection once */
4704 if (!ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
4705 ieee80211_queue_work(&sdata->local->hw,
4706 &sdata->u.mgd.monitor_work);
4707 }
4708}
4709
4710#ifdef CONFIG_PM
4711void ieee80211_mgd_quiesce(struct ieee80211_sub_if_data *sdata)
4712{
4713 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4714 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
4715
4716 sdata_lock(sdata);
4717
4718 if (ifmgd->auth_data || ifmgd->assoc_data) {
4719 const u8 *bssid = ifmgd->auth_data ?
4720 ifmgd->auth_data->bss->bssid :
4721 ifmgd->assoc_data->bss->bssid;
4722
4723 /*
4724 * If we are trying to authenticate / associate while suspending,
4725 * cfg80211 won't know and won't actually abort those attempts,
4726 * thus we need to do that ourselves.
4727 */
David Brazdil0f672f62019-12-10 10:32:29 +00004728 ieee80211_send_deauth_disassoc(sdata, bssid, bssid,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004729 IEEE80211_STYPE_DEAUTH,
4730 WLAN_REASON_DEAUTH_LEAVING,
4731 false, frame_buf);
4732 if (ifmgd->assoc_data)
4733 ieee80211_destroy_assoc_data(sdata, false, true);
4734 if (ifmgd->auth_data)
4735 ieee80211_destroy_auth_data(sdata, false);
4736 cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
4737 IEEE80211_DEAUTH_FRAME_LEN);
4738 }
4739
4740 /* This is a bit of a hack - we should find a better and more generic
4741 * solution to this. Normally when suspending, cfg80211 will in fact
4742 * deauthenticate. However, it doesn't (and cannot) stop an ongoing
4743 * auth (not so important) or assoc (this is the problem) process.
4744 *
4745 * As a consequence, it can happen that we are in the process of both
4746 * associating and suspending, and receive an association response
4747 * after cfg80211 has checked if it needs to disconnect, but before
4748 * we actually set the flag to drop incoming frames. This will then
4749 * cause the workqueue flush to process the association response in
4750 * the suspend, resulting in a successful association just before it
4751 * tries to remove the interface from the driver, which now though
4752 * has a channel context assigned ... this results in issues.
4753 *
4754 * To work around this (for now) simply deauth here again if we're
4755 * now connected.
4756 */
4757 if (ifmgd->associated && !sdata->local->wowlan) {
4758 u8 bssid[ETH_ALEN];
4759 struct cfg80211_deauth_request req = {
4760 .reason_code = WLAN_REASON_DEAUTH_LEAVING,
4761 .bssid = bssid,
4762 };
4763
4764 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
4765 ieee80211_mgd_deauth(sdata, &req);
4766 }
4767
4768 sdata_unlock(sdata);
4769}
4770
4771void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
4772{
4773 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4774
4775 sdata_lock(sdata);
4776 if (!ifmgd->associated) {
4777 sdata_unlock(sdata);
4778 return;
4779 }
4780
4781 if (sdata->flags & IEEE80211_SDATA_DISCONNECT_RESUME) {
4782 sdata->flags &= ~IEEE80211_SDATA_DISCONNECT_RESUME;
4783 mlme_dbg(sdata, "driver requested disconnect after resume\n");
4784 ieee80211_sta_connection_lost(sdata,
4785 ifmgd->associated->bssid,
4786 WLAN_REASON_UNSPECIFIED,
4787 true);
4788 sdata_unlock(sdata);
4789 return;
4790 }
4791 sdata_unlock(sdata);
4792}
4793#endif
4794
4795/* interface setup */
4796void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
4797{
4798 struct ieee80211_if_managed *ifmgd;
4799
4800 ifmgd = &sdata->u.mgd;
4801 INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
4802 INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
4803 INIT_WORK(&ifmgd->beacon_connection_loss_work,
4804 ieee80211_beacon_connection_loss_work);
4805 INIT_WORK(&ifmgd->csa_connection_drop_work,
4806 ieee80211_csa_connection_drop_work);
4807 INIT_WORK(&ifmgd->request_smps_work, ieee80211_request_smps_mgd_work);
4808 INIT_DELAYED_WORK(&ifmgd->tdls_peer_del_work,
4809 ieee80211_tdls_peer_del_work);
4810 timer_setup(&ifmgd->timer, ieee80211_sta_timer, 0);
4811 timer_setup(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer, 0);
4812 timer_setup(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer, 0);
4813 timer_setup(&ifmgd->chswitch_timer, ieee80211_chswitch_timer, 0);
4814 INIT_DELAYED_WORK(&ifmgd->tx_tspec_wk,
4815 ieee80211_sta_handle_tspec_ac_params_wk);
4816
4817 ifmgd->flags = 0;
4818 ifmgd->powersave = sdata->wdev.ps;
4819 ifmgd->uapsd_queues = sdata->local->hw.uapsd_queues;
4820 ifmgd->uapsd_max_sp_len = sdata->local->hw.uapsd_max_sp_len;
4821 ifmgd->p2p_noa_index = -1;
4822
4823 if (sdata->local->hw.wiphy->features & NL80211_FEATURE_DYNAMIC_SMPS)
4824 ifmgd->req_smps = IEEE80211_SMPS_AUTOMATIC;
4825 else
4826 ifmgd->req_smps = IEEE80211_SMPS_OFF;
4827
4828 /* Setup TDLS data */
4829 spin_lock_init(&ifmgd->teardown_lock);
4830 ifmgd->teardown_skb = NULL;
4831 ifmgd->orig_teardown_skb = NULL;
4832}
4833
4834/* scan finished notification */
4835void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
4836{
4837 struct ieee80211_sub_if_data *sdata;
4838
4839 /* Restart STA timers */
4840 rcu_read_lock();
4841 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
4842 if (ieee80211_sdata_running(sdata))
4843 ieee80211_restart_sta_timer(sdata);
4844 }
4845 rcu_read_unlock();
4846}
4847
4848static u8 ieee80211_ht_vht_rx_chains(struct ieee80211_sub_if_data *sdata,
4849 struct cfg80211_bss *cbss)
4850{
4851 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4852 const u8 *ht_cap_ie, *vht_cap_ie;
4853 const struct ieee80211_ht_cap *ht_cap;
4854 const struct ieee80211_vht_cap *vht_cap;
4855 u8 chains = 1;
4856
4857 if (ifmgd->flags & IEEE80211_STA_DISABLE_HT)
4858 return chains;
4859
4860 ht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
4861 if (ht_cap_ie && ht_cap_ie[1] >= sizeof(*ht_cap)) {
4862 ht_cap = (void *)(ht_cap_ie + 2);
4863 chains = ieee80211_mcs_to_chains(&ht_cap->mcs);
4864 /*
4865 * TODO: use "Tx Maximum Number Spatial Streams Supported" and
4866 * "Tx Unequal Modulation Supported" fields.
4867 */
4868 }
4869
4870 if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
4871 return chains;
4872
4873 vht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
4874 if (vht_cap_ie && vht_cap_ie[1] >= sizeof(*vht_cap)) {
4875 u8 nss;
4876 u16 tx_mcs_map;
4877
4878 vht_cap = (void *)(vht_cap_ie + 2);
4879 tx_mcs_map = le16_to_cpu(vht_cap->supp_mcs.tx_mcs_map);
4880 for (nss = 8; nss > 0; nss--) {
4881 if (((tx_mcs_map >> (2 * (nss - 1))) & 3) !=
4882 IEEE80211_VHT_MCS_NOT_SUPPORTED)
4883 break;
4884 }
4885 /* TODO: use "Tx Highest Supported Long GI Data Rate" field? */
4886 chains = max(chains, nss);
4887 }
4888
4889 return chains;
4890}
4891
4892static bool
4893ieee80211_verify_sta_he_mcs_support(struct ieee80211_supported_band *sband,
4894 const struct ieee80211_he_operation *he_op)
4895{
4896 const struct ieee80211_sta_he_cap *sta_he_cap =
4897 ieee80211_get_he_sta_cap(sband);
4898 u16 ap_min_req_set;
4899 int i;
4900
4901 if (!sta_he_cap || !he_op)
4902 return false;
4903
4904 ap_min_req_set = le16_to_cpu(he_op->he_mcs_nss_set);
4905
4906 /* Need to go over for 80MHz, 160MHz and for 80+80 */
4907 for (i = 0; i < 3; i++) {
4908 const struct ieee80211_he_mcs_nss_supp *sta_mcs_nss_supp =
4909 &sta_he_cap->he_mcs_nss_supp;
4910 u16 sta_mcs_map_rx =
4911 le16_to_cpu(((__le16 *)sta_mcs_nss_supp)[2 * i]);
4912 u16 sta_mcs_map_tx =
4913 le16_to_cpu(((__le16 *)sta_mcs_nss_supp)[2 * i + 1]);
4914 u8 nss;
4915 bool verified = true;
4916
4917 /*
4918 * For each band there is a maximum of 8 spatial streams
4919 * possible. Each of the sta_mcs_map_* is a 16-bit struct built
4920 * of 2 bits per NSS (1-8), with the values defined in enum
4921 * ieee80211_he_mcs_support. Need to make sure STA TX and RX
4922 * capabilities aren't less than the AP's minimum requirements
4923 * for this HE BSS per SS.
4924 * It is enough to find one such band that meets the reqs.
4925 */
4926 for (nss = 8; nss > 0; nss--) {
4927 u8 sta_rx_val = (sta_mcs_map_rx >> (2 * (nss - 1))) & 3;
4928 u8 sta_tx_val = (sta_mcs_map_tx >> (2 * (nss - 1))) & 3;
4929 u8 ap_val = (ap_min_req_set >> (2 * (nss - 1))) & 3;
4930
4931 if (ap_val == IEEE80211_HE_MCS_NOT_SUPPORTED)
4932 continue;
4933
4934 /*
4935 * Make sure the HE AP doesn't require MCSs that aren't
4936 * supported by the client
4937 */
4938 if (sta_rx_val == IEEE80211_HE_MCS_NOT_SUPPORTED ||
4939 sta_tx_val == IEEE80211_HE_MCS_NOT_SUPPORTED ||
4940 (ap_val > sta_rx_val) || (ap_val > sta_tx_val)) {
4941 verified = false;
4942 break;
4943 }
4944 }
4945
4946 if (verified)
4947 return true;
4948 }
4949
4950 /* If here, STA doesn't meet AP's HE min requirements */
4951 return false;
4952}
4953
4954static int ieee80211_prep_channel(struct ieee80211_sub_if_data *sdata,
4955 struct cfg80211_bss *cbss)
4956{
4957 struct ieee80211_local *local = sdata->local;
4958 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4959 const struct ieee80211_ht_cap *ht_cap = NULL;
4960 const struct ieee80211_ht_operation *ht_oper = NULL;
4961 const struct ieee80211_vht_operation *vht_oper = NULL;
4962 const struct ieee80211_he_operation *he_oper = NULL;
Olivier Deprez157378f2022-04-04 15:47:50 +02004963 const struct ieee80211_s1g_oper_ie *s1g_oper = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004964 struct ieee80211_supported_band *sband;
4965 struct cfg80211_chan_def chandef;
Olivier Deprez157378f2022-04-04 15:47:50 +02004966 bool is_6ghz = cbss->channel->band == NL80211_BAND_6GHZ;
4967 bool is_5ghz = cbss->channel->band == NL80211_BAND_5GHZ;
4968 struct ieee80211_bss *bss = (void *)cbss->priv;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004969 int ret;
4970 u32 i;
4971 bool have_80mhz;
4972
4973 sband = local->hw.wiphy->bands[cbss->channel->band];
4974
4975 ifmgd->flags &= ~(IEEE80211_STA_DISABLE_40MHZ |
4976 IEEE80211_STA_DISABLE_80P80MHZ |
4977 IEEE80211_STA_DISABLE_160MHZ);
4978
Olivier Deprez157378f2022-04-04 15:47:50 +02004979 /* disable HT/VHT/HE if we don't support them */
4980 if (!sband->ht_cap.ht_supported && !is_6ghz) {
4981 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
4982 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
4983 ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
4984 }
4985
4986 if (!sband->vht_cap.vht_supported && is_5ghz) {
4987 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
4988 ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
4989 }
4990
4991 if (!ieee80211_get_he_sta_cap(sband))
4992 ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
4993
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004994 rcu_read_lock();
4995
Olivier Deprez157378f2022-04-04 15:47:50 +02004996 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) && !is_6ghz) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004997 const u8 *ht_oper_ie, *ht_cap_ie;
4998
4999 ht_oper_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_OPERATION);
5000 if (ht_oper_ie && ht_oper_ie[1] >= sizeof(*ht_oper))
5001 ht_oper = (void *)(ht_oper_ie + 2);
5002
5003 ht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
5004 if (ht_cap_ie && ht_cap_ie[1] >= sizeof(*ht_cap))
5005 ht_cap = (void *)(ht_cap_ie + 2);
5006
5007 if (!ht_cap) {
5008 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
5009 ht_oper = NULL;
5010 }
5011 }
5012
Olivier Deprez157378f2022-04-04 15:47:50 +02005013 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) && !is_6ghz) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005014 const u8 *vht_oper_ie, *vht_cap;
5015
5016 vht_oper_ie = ieee80211_bss_get_ie(cbss,
5017 WLAN_EID_VHT_OPERATION);
5018 if (vht_oper_ie && vht_oper_ie[1] >= sizeof(*vht_oper))
5019 vht_oper = (void *)(vht_oper_ie + 2);
5020 if (vht_oper && !ht_oper) {
5021 vht_oper = NULL;
5022 sdata_info(sdata,
Olivier Deprez157378f2022-04-04 15:47:50 +02005023 "AP advertised VHT without HT, disabling HT/VHT/HE\n");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005024 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
5025 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Olivier Deprez157378f2022-04-04 15:47:50 +02005026 ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005027 }
5028
5029 vht_cap = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
5030 if (!vht_cap || vht_cap[1] < sizeof(struct ieee80211_vht_cap)) {
5031 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
5032 vht_oper = NULL;
5033 }
5034 }
5035
David Brazdil0f672f62019-12-10 10:32:29 +00005036 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005037 const struct cfg80211_bss_ies *ies;
5038 const u8 *he_oper_ie;
5039
5040 ies = rcu_dereference(cbss->ies);
5041 he_oper_ie = cfg80211_find_ext_ie(WLAN_EID_EXT_HE_OPERATION,
5042 ies->data, ies->len);
5043 if (he_oper_ie &&
Olivier Deprez157378f2022-04-04 15:47:50 +02005044 he_oper_ie[1] >= ieee80211_he_oper_size(&he_oper_ie[3]))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005045 he_oper = (void *)(he_oper_ie + 3);
5046 else
5047 he_oper = NULL;
5048
5049 if (!ieee80211_verify_sta_he_mcs_support(sband, he_oper))
5050 ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
5051 }
5052
5053 /* Allow VHT if at least one channel on the sband supports 80 MHz */
5054 have_80mhz = false;
5055 for (i = 0; i < sband->n_channels; i++) {
5056 if (sband->channels[i].flags & (IEEE80211_CHAN_DISABLED |
5057 IEEE80211_CHAN_NO_80MHZ))
5058 continue;
5059
5060 have_80mhz = true;
5061 break;
5062 }
5063
5064 if (!have_80mhz)
5065 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
5066
Olivier Deprez157378f2022-04-04 15:47:50 +02005067 if (sband->band == NL80211_BAND_S1GHZ) {
5068 const u8 *s1g_oper_ie;
5069
5070 s1g_oper_ie = ieee80211_bss_get_ie(cbss,
5071 WLAN_EID_S1G_OPERATION);
5072 if (s1g_oper_ie && s1g_oper_ie[1] >= sizeof(*s1g_oper))
5073 s1g_oper = (void *)(s1g_oper_ie + 2);
5074 else
5075 sdata_info(sdata,
5076 "AP missing S1G operation element?\n");
5077 }
5078
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005079 ifmgd->flags |= ieee80211_determine_chantype(sdata, sband,
5080 cbss->channel,
Olivier Deprez157378f2022-04-04 15:47:50 +02005081 bss->vht_cap_info,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005082 ht_oper, vht_oper, he_oper,
Olivier Deprez157378f2022-04-04 15:47:50 +02005083 s1g_oper,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005084 &chandef, false);
5085
5086 sdata->needed_rx_chains = min(ieee80211_ht_vht_rx_chains(sdata, cbss),
5087 local->rx_chains);
5088
5089 rcu_read_unlock();
5090
Olivier Deprez157378f2022-04-04 15:47:50 +02005091 if (ifmgd->flags & IEEE80211_STA_DISABLE_HE && is_6ghz) {
5092 sdata_info(sdata, "Rejecting non-HE 6/7 GHz connection");
5093 return -EINVAL;
5094 }
5095
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005096 /* will change later if needed */
5097 sdata->smps_mode = IEEE80211_SMPS_OFF;
5098
5099 mutex_lock(&local->mtx);
5100 /*
5101 * If this fails (possibly due to channel context sharing
5102 * on incompatible channels, e.g. 80+80 and 160 sharing the
5103 * same control channel) try to use a smaller bandwidth.
5104 */
5105 ret = ieee80211_vif_use_channel(sdata, &chandef,
5106 IEEE80211_CHANCTX_SHARED);
5107
5108 /* don't downgrade for 5 and 10 MHz channels, though. */
5109 if (chandef.width == NL80211_CHAN_WIDTH_5 ||
5110 chandef.width == NL80211_CHAN_WIDTH_10)
5111 goto out;
5112
5113 while (ret && chandef.width != NL80211_CHAN_WIDTH_20_NOHT) {
5114 ifmgd->flags |= ieee80211_chandef_downgrade(&chandef);
5115 ret = ieee80211_vif_use_channel(sdata, &chandef,
5116 IEEE80211_CHANCTX_SHARED);
5117 }
5118 out:
5119 mutex_unlock(&local->mtx);
5120 return ret;
5121}
5122
David Brazdil0f672f62019-12-10 10:32:29 +00005123static bool ieee80211_get_dtim(const struct cfg80211_bss_ies *ies,
5124 u8 *dtim_count, u8 *dtim_period)
5125{
5126 const u8 *tim_ie = cfg80211_find_ie(WLAN_EID_TIM, ies->data, ies->len);
5127 const u8 *idx_ie = cfg80211_find_ie(WLAN_EID_MULTI_BSSID_IDX, ies->data,
5128 ies->len);
5129 const struct ieee80211_tim_ie *tim = NULL;
5130 const struct ieee80211_bssid_index *idx;
5131 bool valid = tim_ie && tim_ie[1] >= 2;
5132
5133 if (valid)
5134 tim = (void *)(tim_ie + 2);
5135
5136 if (dtim_count)
5137 *dtim_count = valid ? tim->dtim_count : 0;
5138
5139 if (dtim_period)
5140 *dtim_period = valid ? tim->dtim_period : 0;
5141
5142 /* Check if value is overridden by non-transmitted profile */
5143 if (!idx_ie || idx_ie[1] < 3)
5144 return valid;
5145
5146 idx = (void *)(idx_ie + 2);
5147
5148 if (dtim_count)
5149 *dtim_count = idx->dtim_count;
5150
5151 if (dtim_period)
5152 *dtim_period = idx->dtim_period;
5153
5154 return true;
5155}
5156
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005157static int ieee80211_prep_connection(struct ieee80211_sub_if_data *sdata,
5158 struct cfg80211_bss *cbss, bool assoc,
5159 bool override)
5160{
5161 struct ieee80211_local *local = sdata->local;
5162 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
5163 struct ieee80211_bss *bss = (void *)cbss->priv;
5164 struct sta_info *new_sta = NULL;
5165 struct ieee80211_supported_band *sband;
5166 bool have_sta = false;
5167 int err;
5168
5169 sband = local->hw.wiphy->bands[cbss->channel->band];
5170
5171 if (WARN_ON(!ifmgd->auth_data && !ifmgd->assoc_data))
5172 return -EINVAL;
5173
5174 /* If a reconfig is happening, bail out */
5175 if (local->in_reconfig)
5176 return -EBUSY;
5177
5178 if (assoc) {
5179 rcu_read_lock();
5180 have_sta = sta_info_get(sdata, cbss->bssid);
5181 rcu_read_unlock();
5182 }
5183
5184 if (!have_sta) {
5185 new_sta = sta_info_alloc(sdata, cbss->bssid, GFP_KERNEL);
5186 if (!new_sta)
5187 return -ENOMEM;
5188 }
5189
5190 /*
5191 * Set up the information for the new channel before setting the
5192 * new channel. We can't - completely race-free - change the basic
5193 * rates bitmap and the channel (sband) that it refers to, but if
5194 * we set it up before we at least avoid calling into the driver's
5195 * bss_info_changed() method with invalid information (since we do
5196 * call that from changing the channel - only for IDLE and perhaps
5197 * some others, but ...).
5198 *
5199 * So to avoid that, just set up all the new information before the
5200 * channel, but tell the driver to apply it only afterwards, since
5201 * it might need the new channel for that.
5202 */
5203 if (new_sta) {
5204 u32 rates = 0, basic_rates = 0;
Olivier Deprez157378f2022-04-04 15:47:50 +02005205 bool have_higher_than_11mbit = false;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005206 int min_rate = INT_MAX, min_rate_index = -1;
5207 const struct cfg80211_bss_ies *ies;
5208 int shift = ieee80211_vif_get_shift(&sdata->vif);
5209
Olivier Deprez157378f2022-04-04 15:47:50 +02005210 /* TODO: S1G Basic Rate Set is expressed elsewhere */
5211 if (cbss->channel->band == NL80211_BAND_S1GHZ) {
5212 ieee80211_s1g_sta_rate_init(new_sta);
5213 goto skip_rates;
5214 }
5215
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005216 ieee80211_get_rates(sband, bss->supp_rates,
5217 bss->supp_rates_len,
5218 &rates, &basic_rates,
5219 &have_higher_than_11mbit,
5220 &min_rate, &min_rate_index,
5221 shift);
5222
5223 /*
5224 * This used to be a workaround for basic rates missing
5225 * in the association response frame. Now that we no
5226 * longer use the basic rates from there, it probably
5227 * doesn't happen any more, but keep the workaround so
5228 * in case some *other* APs are buggy in different ways
5229 * we can connect -- with a warning.
Olivier Deprez157378f2022-04-04 15:47:50 +02005230 * Allow this workaround only in case the AP provided at least
5231 * one rate.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005232 */
Olivier Deprez157378f2022-04-04 15:47:50 +02005233 if (min_rate_index < 0) {
5234 sdata_info(sdata,
5235 "No legacy rates in association response\n");
5236
5237 sta_info_free(local, new_sta);
5238 return -EINVAL;
5239 } else if (!basic_rates) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005240 sdata_info(sdata,
5241 "No basic rates, using min rate instead\n");
5242 basic_rates = BIT(min_rate_index);
5243 }
5244
David Brazdil0f672f62019-12-10 10:32:29 +00005245 if (rates)
5246 new_sta->sta.supp_rates[cbss->channel->band] = rates;
5247 else
5248 sdata_info(sdata,
5249 "No rates found, keeping mandatory only\n");
5250
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005251 sdata->vif.bss_conf.basic_rates = basic_rates;
5252
5253 /* cf. IEEE 802.11 9.2.12 */
5254 if (cbss->channel->band == NL80211_BAND_2GHZ &&
5255 have_higher_than_11mbit)
5256 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
5257 else
5258 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
5259
Olivier Deprez157378f2022-04-04 15:47:50 +02005260skip_rates:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005261 memcpy(ifmgd->bssid, cbss->bssid, ETH_ALEN);
5262
5263 /* set timing information */
5264 sdata->vif.bss_conf.beacon_int = cbss->beacon_interval;
5265 rcu_read_lock();
5266 ies = rcu_dereference(cbss->beacon_ies);
5267 if (ies) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005268 sdata->vif.bss_conf.sync_tsf = ies->tsf;
5269 sdata->vif.bss_conf.sync_device_ts =
5270 bss->device_ts_beacon;
David Brazdil0f672f62019-12-10 10:32:29 +00005271
5272 ieee80211_get_dtim(ies,
5273 &sdata->vif.bss_conf.sync_dtim_count,
5274 NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005275 } else if (!ieee80211_hw_check(&sdata->local->hw,
5276 TIMING_BEACON_ONLY)) {
5277 ies = rcu_dereference(cbss->proberesp_ies);
5278 /* must be non-NULL since beacon IEs were NULL */
5279 sdata->vif.bss_conf.sync_tsf = ies->tsf;
5280 sdata->vif.bss_conf.sync_device_ts =
5281 bss->device_ts_presp;
5282 sdata->vif.bss_conf.sync_dtim_count = 0;
5283 } else {
5284 sdata->vif.bss_conf.sync_tsf = 0;
5285 sdata->vif.bss_conf.sync_device_ts = 0;
5286 sdata->vif.bss_conf.sync_dtim_count = 0;
5287 }
5288 rcu_read_unlock();
5289 }
5290
5291 if (new_sta || override) {
5292 err = ieee80211_prep_channel(sdata, cbss);
5293 if (err) {
5294 if (new_sta)
5295 sta_info_free(local, new_sta);
5296 return -EINVAL;
5297 }
5298 }
5299
5300 if (new_sta) {
5301 /*
5302 * tell driver about BSSID, basic rates and timing
5303 * this was set up above, before setting the channel
5304 */
5305 ieee80211_bss_info_change_notify(sdata,
5306 BSS_CHANGED_BSSID | BSS_CHANGED_BASIC_RATES |
5307 BSS_CHANGED_BEACON_INT);
5308
5309 if (assoc)
5310 sta_info_pre_move_state(new_sta, IEEE80211_STA_AUTH);
5311
5312 err = sta_info_insert(new_sta);
5313 new_sta = NULL;
5314 if (err) {
5315 sdata_info(sdata,
5316 "failed to insert STA entry for the AP (error %d)\n",
5317 err);
5318 return err;
5319 }
5320 } else
5321 WARN_ON_ONCE(!ether_addr_equal(ifmgd->bssid, cbss->bssid));
5322
5323 /* Cancel scan to ensure that nothing interferes with connection */
5324 if (local->scanning)
5325 ieee80211_scan_cancel(local);
5326
5327 return 0;
5328}
5329
5330/* config hooks */
5331int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
5332 struct cfg80211_auth_request *req)
5333{
5334 struct ieee80211_local *local = sdata->local;
5335 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
5336 struct ieee80211_mgd_auth_data *auth_data;
5337 u16 auth_alg;
5338 int err;
David Brazdil0f672f62019-12-10 10:32:29 +00005339 bool cont_auth;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005340
5341 /* prepare auth data structure */
5342
5343 switch (req->auth_type) {
5344 case NL80211_AUTHTYPE_OPEN_SYSTEM:
5345 auth_alg = WLAN_AUTH_OPEN;
5346 break;
5347 case NL80211_AUTHTYPE_SHARED_KEY:
David Brazdil0f672f62019-12-10 10:32:29 +00005348 if (fips_enabled)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005349 return -EOPNOTSUPP;
5350 auth_alg = WLAN_AUTH_SHARED_KEY;
5351 break;
5352 case NL80211_AUTHTYPE_FT:
5353 auth_alg = WLAN_AUTH_FT;
5354 break;
5355 case NL80211_AUTHTYPE_NETWORK_EAP:
5356 auth_alg = WLAN_AUTH_LEAP;
5357 break;
5358 case NL80211_AUTHTYPE_SAE:
5359 auth_alg = WLAN_AUTH_SAE;
5360 break;
5361 case NL80211_AUTHTYPE_FILS_SK:
5362 auth_alg = WLAN_AUTH_FILS_SK;
5363 break;
5364 case NL80211_AUTHTYPE_FILS_SK_PFS:
5365 auth_alg = WLAN_AUTH_FILS_SK_PFS;
5366 break;
5367 case NL80211_AUTHTYPE_FILS_PK:
5368 auth_alg = WLAN_AUTH_FILS_PK;
5369 break;
5370 default:
5371 return -EOPNOTSUPP;
5372 }
5373
David Brazdil0f672f62019-12-10 10:32:29 +00005374 if (ifmgd->assoc_data)
5375 return -EBUSY;
5376
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005377 auth_data = kzalloc(sizeof(*auth_data) + req->auth_data_len +
5378 req->ie_len, GFP_KERNEL);
5379 if (!auth_data)
5380 return -ENOMEM;
5381
5382 auth_data->bss = req->bss;
5383
5384 if (req->auth_data_len >= 4) {
5385 if (req->auth_type == NL80211_AUTHTYPE_SAE) {
5386 __le16 *pos = (__le16 *) req->auth_data;
5387
5388 auth_data->sae_trans = le16_to_cpu(pos[0]);
5389 auth_data->sae_status = le16_to_cpu(pos[1]);
5390 }
5391 memcpy(auth_data->data, req->auth_data + 4,
5392 req->auth_data_len - 4);
5393 auth_data->data_len += req->auth_data_len - 4;
5394 }
5395
David Brazdil0f672f62019-12-10 10:32:29 +00005396 /* Check if continuing authentication or trying to authenticate with the
5397 * same BSS that we were in the process of authenticating with and avoid
5398 * removal and re-addition of the STA entry in
5399 * ieee80211_prep_connection().
5400 */
5401 cont_auth = ifmgd->auth_data && req->bss == ifmgd->auth_data->bss;
5402
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005403 if (req->ie && req->ie_len) {
5404 memcpy(&auth_data->data[auth_data->data_len],
5405 req->ie, req->ie_len);
5406 auth_data->data_len += req->ie_len;
5407 }
5408
5409 if (req->key && req->key_len) {
5410 auth_data->key_len = req->key_len;
5411 auth_data->key_idx = req->key_idx;
5412 memcpy(auth_data->key, req->key, req->key_len);
5413 }
5414
5415 auth_data->algorithm = auth_alg;
5416
5417 /* try to authenticate/probe */
5418
David Brazdil0f672f62019-12-10 10:32:29 +00005419 if (ifmgd->auth_data) {
5420 if (cont_auth && req->auth_type == NL80211_AUTHTYPE_SAE) {
5421 auth_data->peer_confirmed =
5422 ifmgd->auth_data->peer_confirmed;
5423 }
5424 ieee80211_destroy_auth_data(sdata, cont_auth);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005425 }
5426
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005427 /* prep auth_data so we don't go into idle on disassoc */
5428 ifmgd->auth_data = auth_data;
5429
David Brazdil0f672f62019-12-10 10:32:29 +00005430 /* If this is continuation of an ongoing SAE authentication exchange
5431 * (i.e., request to send SAE Confirm) and the peer has already
5432 * confirmed, mark authentication completed since we are about to send
5433 * out SAE Confirm.
5434 */
5435 if (cont_auth && req->auth_type == NL80211_AUTHTYPE_SAE &&
5436 auth_data->peer_confirmed && auth_data->sae_trans == 2)
5437 ieee80211_mark_sta_auth(sdata, req->bss->bssid);
5438
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005439 if (ifmgd->associated) {
5440 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
5441
5442 sdata_info(sdata,
5443 "disconnect from AP %pM for new auth to %pM\n",
5444 ifmgd->associated->bssid, req->bss->bssid);
5445 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
5446 WLAN_REASON_UNSPECIFIED,
5447 false, frame_buf);
5448
5449 ieee80211_report_disconnect(sdata, frame_buf,
5450 sizeof(frame_buf), true,
5451 WLAN_REASON_UNSPECIFIED);
5452 }
5453
5454 sdata_info(sdata, "authenticate with %pM\n", req->bss->bssid);
5455
David Brazdil0f672f62019-12-10 10:32:29 +00005456 err = ieee80211_prep_connection(sdata, req->bss, cont_auth, false);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005457 if (err)
5458 goto err_clear;
5459
5460 err = ieee80211_auth(sdata);
5461 if (err) {
5462 sta_info_destroy_addr(sdata, req->bss->bssid);
5463 goto err_clear;
5464 }
5465
5466 /* hold our own reference */
5467 cfg80211_ref_bss(local->hw.wiphy, auth_data->bss);
5468 return 0;
5469
5470 err_clear:
5471 eth_zero_addr(ifmgd->bssid);
5472 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
5473 ifmgd->auth_data = NULL;
5474 mutex_lock(&sdata->local->mtx);
5475 ieee80211_vif_release_channel(sdata);
5476 mutex_unlock(&sdata->local->mtx);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005477 kfree(auth_data);
5478 return err;
5479}
5480
5481int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
5482 struct cfg80211_assoc_request *req)
5483{
Olivier Deprez157378f2022-04-04 15:47:50 +02005484 bool is_6ghz = req->bss->channel->band == NL80211_BAND_6GHZ;
5485 bool is_5ghz = req->bss->channel->band == NL80211_BAND_5GHZ;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005486 struct ieee80211_local *local = sdata->local;
5487 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
5488 struct ieee80211_bss *bss = (void *)req->bss->priv;
5489 struct ieee80211_mgd_assoc_data *assoc_data;
5490 const struct cfg80211_bss_ies *beacon_ies;
5491 struct ieee80211_supported_band *sband;
5492 const u8 *ssidie, *ht_ie, *vht_ie;
5493 int i, err;
5494 bool override = false;
5495
5496 assoc_data = kzalloc(sizeof(*assoc_data) + req->ie_len, GFP_KERNEL);
5497 if (!assoc_data)
5498 return -ENOMEM;
5499
5500 rcu_read_lock();
5501 ssidie = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
David Brazdil0f672f62019-12-10 10:32:29 +00005502 if (!ssidie || ssidie[1] > sizeof(assoc_data->ssid)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005503 rcu_read_unlock();
5504 kfree(assoc_data);
5505 return -EINVAL;
5506 }
5507 memcpy(assoc_data->ssid, ssidie + 2, ssidie[1]);
5508 assoc_data->ssid_len = ssidie[1];
5509 rcu_read_unlock();
5510
5511 if (ifmgd->associated) {
5512 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
5513
5514 sdata_info(sdata,
5515 "disconnect from AP %pM for new assoc to %pM\n",
5516 ifmgd->associated->bssid, req->bss->bssid);
5517 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
5518 WLAN_REASON_UNSPECIFIED,
5519 false, frame_buf);
5520
5521 ieee80211_report_disconnect(sdata, frame_buf,
5522 sizeof(frame_buf), true,
5523 WLAN_REASON_UNSPECIFIED);
5524 }
5525
5526 if (ifmgd->auth_data && !ifmgd->auth_data->done) {
5527 err = -EBUSY;
5528 goto err_free;
5529 }
5530
5531 if (ifmgd->assoc_data) {
5532 err = -EBUSY;
5533 goto err_free;
5534 }
5535
5536 if (ifmgd->auth_data) {
5537 bool match;
5538
5539 /* keep sta info, bssid if matching */
5540 match = ether_addr_equal(ifmgd->bssid, req->bss->bssid);
5541 ieee80211_destroy_auth_data(sdata, match);
5542 }
5543
5544 /* prepare assoc data */
5545
5546 ifmgd->beacon_crc_valid = false;
5547
5548 assoc_data->wmm = bss->wmm_used &&
5549 (local->hw.queues >= IEEE80211_NUM_ACS);
5550
5551 /*
5552 * IEEE802.11n does not allow TKIP/WEP as pairwise ciphers in HT mode.
5553 * We still associate in non-HT mode (11a/b/g) if any one of these
5554 * ciphers is configured as pairwise.
5555 * We can set this to true for non-11n hardware, that'll be checked
5556 * separately along with the peer capabilities.
5557 */
5558 for (i = 0; i < req->crypto.n_ciphers_pairwise; i++) {
5559 if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
5560 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
5561 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104) {
5562 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
5563 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
5564 ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
5565 netdev_info(sdata->dev,
David Brazdil0f672f62019-12-10 10:32:29 +00005566 "disabling HT/VHT/HE due to WEP/TKIP use\n");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005567 }
5568 }
5569
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005570 sband = local->hw.wiphy->bands[req->bss->channel->band];
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005571
Olivier Deprez157378f2022-04-04 15:47:50 +02005572 /* also disable HT/VHT/HE if the AP doesn't use WMM */
5573 if (!bss->wmm_used) {
5574 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005575 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Olivier Deprez157378f2022-04-04 15:47:50 +02005576 ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
5577 netdev_info(sdata->dev,
5578 "disabling HT/VHT/HE as WMM/QoS is not supported by the AP\n");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005579 }
5580
5581 memcpy(&ifmgd->ht_capa, &req->ht_capa, sizeof(ifmgd->ht_capa));
5582 memcpy(&ifmgd->ht_capa_mask, &req->ht_capa_mask,
5583 sizeof(ifmgd->ht_capa_mask));
5584
5585 memcpy(&ifmgd->vht_capa, &req->vht_capa, sizeof(ifmgd->vht_capa));
5586 memcpy(&ifmgd->vht_capa_mask, &req->vht_capa_mask,
5587 sizeof(ifmgd->vht_capa_mask));
5588
Olivier Deprez157378f2022-04-04 15:47:50 +02005589 memcpy(&ifmgd->s1g_capa, &req->s1g_capa, sizeof(ifmgd->s1g_capa));
5590 memcpy(&ifmgd->s1g_capa_mask, &req->s1g_capa_mask,
5591 sizeof(ifmgd->s1g_capa_mask));
5592
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005593 if (req->ie && req->ie_len) {
5594 memcpy(assoc_data->ie, req->ie, req->ie_len);
5595 assoc_data->ie_len = req->ie_len;
5596 }
5597
5598 if (req->fils_kek) {
5599 /* should already be checked in cfg80211 - so warn */
5600 if (WARN_ON(req->fils_kek_len > FILS_MAX_KEK_LEN)) {
5601 err = -EINVAL;
5602 goto err_free;
5603 }
5604 memcpy(assoc_data->fils_kek, req->fils_kek,
5605 req->fils_kek_len);
5606 assoc_data->fils_kek_len = req->fils_kek_len;
5607 }
5608
5609 if (req->fils_nonces)
5610 memcpy(assoc_data->fils_nonces, req->fils_nonces,
5611 2 * FILS_NONCE_LEN);
5612
5613 assoc_data->bss = req->bss;
5614
5615 if (ifmgd->req_smps == IEEE80211_SMPS_AUTOMATIC) {
5616 if (ifmgd->powersave)
5617 sdata->smps_mode = IEEE80211_SMPS_DYNAMIC;
5618 else
5619 sdata->smps_mode = IEEE80211_SMPS_OFF;
5620 } else
5621 sdata->smps_mode = ifmgd->req_smps;
5622
5623 assoc_data->capability = req->bss->capability;
5624 assoc_data->supp_rates = bss->supp_rates;
5625 assoc_data->supp_rates_len = bss->supp_rates_len;
5626
5627 rcu_read_lock();
5628 ht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_HT_OPERATION);
5629 if (ht_ie && ht_ie[1] >= sizeof(struct ieee80211_ht_operation))
5630 assoc_data->ap_ht_param =
5631 ((struct ieee80211_ht_operation *)(ht_ie + 2))->ht_param;
Olivier Deprez157378f2022-04-04 15:47:50 +02005632 else if (!is_6ghz)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005633 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
5634 vht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_VHT_CAPABILITY);
5635 if (vht_ie && vht_ie[1] >= sizeof(struct ieee80211_vht_cap))
5636 memcpy(&assoc_data->ap_vht_cap, vht_ie + 2,
5637 sizeof(struct ieee80211_vht_cap));
Olivier Deprez157378f2022-04-04 15:47:50 +02005638 else if (is_5ghz)
5639 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT |
5640 IEEE80211_STA_DISABLE_HE;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005641 rcu_read_unlock();
5642
5643 if (WARN((sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_UAPSD) &&
5644 ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK),
5645 "U-APSD not supported with HW_PS_NULLFUNC_STACK\n"))
5646 sdata->vif.driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
5647
5648 if (bss->wmm_used && bss->uapsd_supported &&
5649 (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_UAPSD)) {
5650 assoc_data->uapsd = true;
5651 ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED;
5652 } else {
5653 assoc_data->uapsd = false;
5654 ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED;
5655 }
5656
5657 if (req->prev_bssid)
5658 memcpy(assoc_data->prev_bssid, req->prev_bssid, ETH_ALEN);
5659
5660 if (req->use_mfp) {
5661 ifmgd->mfp = IEEE80211_MFP_REQUIRED;
5662 ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
5663 } else {
5664 ifmgd->mfp = IEEE80211_MFP_DISABLED;
5665 ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
5666 }
5667
5668 if (req->flags & ASSOC_REQ_USE_RRM)
5669 ifmgd->flags |= IEEE80211_STA_ENABLE_RRM;
5670 else
5671 ifmgd->flags &= ~IEEE80211_STA_ENABLE_RRM;
5672
5673 if (req->crypto.control_port)
5674 ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
5675 else
5676 ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
5677
5678 sdata->control_port_protocol = req->crypto.control_port_ethertype;
5679 sdata->control_port_no_encrypt = req->crypto.control_port_no_encrypt;
5680 sdata->control_port_over_nl80211 =
5681 req->crypto.control_port_over_nl80211;
Olivier Deprez157378f2022-04-04 15:47:50 +02005682 sdata->control_port_no_preauth = req->crypto.control_port_no_preauth;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005683 sdata->encrypt_headroom = ieee80211_cs_headroom(local, &req->crypto,
5684 sdata->vif.type);
5685
5686 /* kick off associate process */
5687
5688 ifmgd->assoc_data = assoc_data;
5689 ifmgd->dtim_period = 0;
5690 ifmgd->have_beacon = false;
5691
5692 /* override HT/VHT configuration only if the AP and we support it */
5693 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
5694 struct ieee80211_sta_ht_cap sta_ht_cap;
5695
5696 if (req->flags & ASSOC_REQ_DISABLE_HT)
5697 override = true;
5698
5699 memcpy(&sta_ht_cap, &sband->ht_cap, sizeof(sta_ht_cap));
5700 ieee80211_apply_htcap_overrides(sdata, &sta_ht_cap);
5701
5702 /* check for 40 MHz disable override */
5703 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_40MHZ) &&
5704 sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40 &&
5705 !(sta_ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40))
5706 override = true;
5707
5708 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
5709 req->flags & ASSOC_REQ_DISABLE_VHT)
5710 override = true;
5711 }
5712
5713 if (req->flags & ASSOC_REQ_DISABLE_HT) {
5714 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
5715 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Olivier Deprez157378f2022-04-04 15:47:50 +02005716 ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005717 }
5718
5719 if (req->flags & ASSOC_REQ_DISABLE_VHT)
5720 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
5721
5722 err = ieee80211_prep_connection(sdata, req->bss, true, override);
5723 if (err)
5724 goto err_clear;
5725
5726 rcu_read_lock();
5727 beacon_ies = rcu_dereference(req->bss->beacon_ies);
5728
5729 if (ieee80211_hw_check(&sdata->local->hw, NEED_DTIM_BEFORE_ASSOC) &&
5730 !beacon_ies) {
5731 /*
5732 * Wait up to one beacon interval ...
5733 * should this be more if we miss one?
5734 */
5735 sdata_info(sdata, "waiting for beacon from %pM\n",
5736 ifmgd->bssid);
5737 assoc_data->timeout = TU_TO_EXP_TIME(req->bss->beacon_interval);
5738 assoc_data->timeout_started = true;
5739 assoc_data->need_beacon = true;
5740 } else if (beacon_ies) {
Olivier Deprez157378f2022-04-04 15:47:50 +02005741 const struct element *elem;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005742 u8 dtim_count = 0;
5743
David Brazdil0f672f62019-12-10 10:32:29 +00005744 ieee80211_get_dtim(beacon_ies, &dtim_count,
5745 &ifmgd->dtim_period);
5746
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005747 ifmgd->have_beacon = true;
5748 assoc_data->timeout = jiffies;
5749 assoc_data->timeout_started = true;
5750
5751 if (ieee80211_hw_check(&local->hw, TIMING_BEACON_ONLY)) {
5752 sdata->vif.bss_conf.sync_tsf = beacon_ies->tsf;
5753 sdata->vif.bss_conf.sync_device_ts =
5754 bss->device_ts_beacon;
5755 sdata->vif.bss_conf.sync_dtim_count = dtim_count;
5756 }
David Brazdil0f672f62019-12-10 10:32:29 +00005757
Olivier Deprez157378f2022-04-04 15:47:50 +02005758 elem = cfg80211_find_ext_elem(WLAN_EID_EXT_MULTIPLE_BSSID_CONFIGURATION,
5759 beacon_ies->data, beacon_ies->len);
5760 if (elem && elem->datalen >= 3)
5761 sdata->vif.bss_conf.profile_periodicity = elem->data[2];
5762 else
5763 sdata->vif.bss_conf.profile_periodicity = 0;
David Brazdil0f672f62019-12-10 10:32:29 +00005764
Olivier Deprez157378f2022-04-04 15:47:50 +02005765 elem = cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY,
5766 beacon_ies->data, beacon_ies->len);
5767 if (elem && elem->datalen >= 11 &&
5768 (elem->data[10] & WLAN_EXT_CAPA11_EMA_SUPPORT))
David Brazdil0f672f62019-12-10 10:32:29 +00005769 sdata->vif.bss_conf.ema_ap = true;
Olivier Deprez157378f2022-04-04 15:47:50 +02005770 else
5771 sdata->vif.bss_conf.ema_ap = false;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005772 } else {
5773 assoc_data->timeout = jiffies;
5774 assoc_data->timeout_started = true;
5775 }
5776 rcu_read_unlock();
5777
5778 run_again(sdata, assoc_data->timeout);
5779
5780 if (bss->corrupt_data) {
5781 char *corrupt_type = "data";
5782 if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_BEACON) {
5783 if (bss->corrupt_data &
5784 IEEE80211_BSS_CORRUPT_PROBE_RESP)
5785 corrupt_type = "beacon and probe response";
5786 else
5787 corrupt_type = "beacon";
5788 } else if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_PROBE_RESP)
5789 corrupt_type = "probe response";
5790 sdata_info(sdata, "associating with AP with corrupt %s\n",
5791 corrupt_type);
5792 }
5793
5794 return 0;
5795 err_clear:
5796 eth_zero_addr(ifmgd->bssid);
5797 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
5798 ifmgd->assoc_data = NULL;
5799 err_free:
5800 kfree(assoc_data);
5801 return err;
5802}
5803
5804int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
5805 struct cfg80211_deauth_request *req)
5806{
5807 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
5808 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
5809 bool tx = !req->local_state_change;
5810
5811 if (ifmgd->auth_data &&
5812 ether_addr_equal(ifmgd->auth_data->bss->bssid, req->bssid)) {
5813 sdata_info(sdata,
5814 "aborting authentication with %pM by local choice (Reason: %u=%s)\n",
5815 req->bssid, req->reason_code,
5816 ieee80211_get_reason_code_string(req->reason_code));
5817
5818 drv_mgd_prepare_tx(sdata->local, sdata, 0);
David Brazdil0f672f62019-12-10 10:32:29 +00005819 ieee80211_send_deauth_disassoc(sdata, req->bssid, req->bssid,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005820 IEEE80211_STYPE_DEAUTH,
5821 req->reason_code, tx,
5822 frame_buf);
5823 ieee80211_destroy_auth_data(sdata, false);
5824 ieee80211_report_disconnect(sdata, frame_buf,
5825 sizeof(frame_buf), true,
5826 req->reason_code);
5827
5828 return 0;
5829 }
5830
5831 if (ifmgd->assoc_data &&
5832 ether_addr_equal(ifmgd->assoc_data->bss->bssid, req->bssid)) {
5833 sdata_info(sdata,
5834 "aborting association with %pM by local choice (Reason: %u=%s)\n",
5835 req->bssid, req->reason_code,
5836 ieee80211_get_reason_code_string(req->reason_code));
5837
5838 drv_mgd_prepare_tx(sdata->local, sdata, 0);
David Brazdil0f672f62019-12-10 10:32:29 +00005839 ieee80211_send_deauth_disassoc(sdata, req->bssid, req->bssid,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005840 IEEE80211_STYPE_DEAUTH,
5841 req->reason_code, tx,
5842 frame_buf);
5843 ieee80211_destroy_assoc_data(sdata, false, true);
5844 ieee80211_report_disconnect(sdata, frame_buf,
5845 sizeof(frame_buf), true,
5846 req->reason_code);
5847 return 0;
5848 }
5849
5850 if (ifmgd->associated &&
5851 ether_addr_equal(ifmgd->associated->bssid, req->bssid)) {
5852 sdata_info(sdata,
5853 "deauthenticating from %pM by local choice (Reason: %u=%s)\n",
5854 req->bssid, req->reason_code,
5855 ieee80211_get_reason_code_string(req->reason_code));
5856
5857 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
5858 req->reason_code, tx, frame_buf);
5859 ieee80211_report_disconnect(sdata, frame_buf,
5860 sizeof(frame_buf), true,
5861 req->reason_code);
5862 return 0;
5863 }
5864
5865 return -ENOTCONN;
5866}
5867
5868int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
5869 struct cfg80211_disassoc_request *req)
5870{
5871 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
5872 u8 bssid[ETH_ALEN];
5873 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
5874
5875 /*
5876 * cfg80211 should catch this ... but it's racy since
5877 * we can receive a disassoc frame, process it, hand it
5878 * to cfg80211 while that's in a locked section already
5879 * trying to tell us that the user wants to disconnect.
5880 */
5881 if (ifmgd->associated != req->bss)
5882 return -ENOLINK;
5883
5884 sdata_info(sdata,
5885 "disassociating from %pM by local choice (Reason: %u=%s)\n",
5886 req->bss->bssid, req->reason_code, ieee80211_get_reason_code_string(req->reason_code));
5887
5888 memcpy(bssid, req->bss->bssid, ETH_ALEN);
5889 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DISASSOC,
5890 req->reason_code, !req->local_state_change,
5891 frame_buf);
5892
5893 ieee80211_report_disconnect(sdata, frame_buf, sizeof(frame_buf), true,
5894 req->reason_code);
5895
5896 return 0;
5897}
5898
5899void ieee80211_mgd_stop(struct ieee80211_sub_if_data *sdata)
5900{
5901 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
5902
5903 /*
5904 * Make sure some work items will not run after this,
5905 * they will not do anything but might not have been
5906 * cancelled when disconnecting.
5907 */
5908 cancel_work_sync(&ifmgd->monitor_work);
5909 cancel_work_sync(&ifmgd->beacon_connection_loss_work);
5910 cancel_work_sync(&ifmgd->request_smps_work);
5911 cancel_work_sync(&ifmgd->csa_connection_drop_work);
5912 cancel_work_sync(&ifmgd->chswitch_work);
5913 cancel_delayed_work_sync(&ifmgd->tdls_peer_del_work);
5914
5915 sdata_lock(sdata);
5916 if (ifmgd->assoc_data) {
5917 struct cfg80211_bss *bss = ifmgd->assoc_data->bss;
5918 ieee80211_destroy_assoc_data(sdata, false, false);
5919 cfg80211_assoc_timeout(sdata->dev, bss);
5920 }
5921 if (ifmgd->auth_data)
5922 ieee80211_destroy_auth_data(sdata, false);
5923 spin_lock_bh(&ifmgd->teardown_lock);
5924 if (ifmgd->teardown_skb) {
5925 kfree_skb(ifmgd->teardown_skb);
5926 ifmgd->teardown_skb = NULL;
5927 ifmgd->orig_teardown_skb = NULL;
5928 }
David Brazdil0f672f62019-12-10 10:32:29 +00005929 kfree(ifmgd->assoc_req_ies);
5930 ifmgd->assoc_req_ies = NULL;
5931 ifmgd->assoc_req_ies_len = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005932 spin_unlock_bh(&ifmgd->teardown_lock);
5933 del_timer_sync(&ifmgd->timer);
5934 sdata_unlock(sdata);
5935}
5936
5937void ieee80211_cqm_rssi_notify(struct ieee80211_vif *vif,
5938 enum nl80211_cqm_rssi_threshold_event rssi_event,
5939 s32 rssi_level,
5940 gfp_t gfp)
5941{
5942 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
5943
5944 trace_api_cqm_rssi_notify(sdata, rssi_event, rssi_level);
5945
5946 cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, rssi_level, gfp);
5947}
5948EXPORT_SYMBOL(ieee80211_cqm_rssi_notify);
5949
5950void ieee80211_cqm_beacon_loss_notify(struct ieee80211_vif *vif, gfp_t gfp)
5951{
5952 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
5953
5954 trace_api_cqm_beacon_loss_notify(sdata->local, sdata);
5955
5956 cfg80211_cqm_beacon_loss_notify(sdata->dev, gfp);
5957}
5958EXPORT_SYMBOL(ieee80211_cqm_beacon_loss_notify);