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