1 // SPDX-License-Identifier: GPL-2.0-only
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 - 2022 Intel Corporation
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>
28 #include "ieee80211_i.h"
29 #include "driver-ops.h"
32 #include "fils_aead.h"
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
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).");
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).");
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.
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.");
70 * Time the connection can be idle before we probe
71 * it to see if we can still talk to the AP.
73 #define IEEE80211_CONNECTION_IDLE_TIME (30 * HZ)
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.
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).");
86 * How many Beacon frames need to have been used in average signal strength
87 * before starting to indicate signal change events.
89 #define IEEE80211_SIGNAL_AVE_MIN_COUNT 4
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
101 static void run_again(struct ieee80211_sub_if_data *sdata,
102 unsigned long timeout)
104 sdata_assert_lock(sdata);
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);
111 void ieee80211_sta_reset_beacon_monitor(struct ieee80211_sub_if_data *sdata)
113 if (sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)
116 if (ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
119 mod_timer(&sdata->u.mgd.bcn_mon_timer,
120 round_jiffies_up(jiffies + sdata->u.mgd.beacon_timeout));
123 void ieee80211_sta_reset_conn_monitor(struct ieee80211_sub_if_data *sdata)
125 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
127 if (unlikely(!ifmgd->associated))
130 if (ifmgd->probe_send_count)
131 ifmgd->probe_send_count = 0;
133 if (ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
136 mod_timer(&ifmgd->conn_mon_timer,
137 round_jiffies_up(jiffies + IEEE80211_CONNECTION_IDLE_TIME));
140 static int ecw2cw(int ecw)
142 return (1 << ecw) - 1;
145 static ieee80211_conn_flags_t
146 ieee80211_determine_chantype(struct ieee80211_sub_if_data *sdata,
147 struct ieee80211_link_data *link,
148 ieee80211_conn_flags_t conn_flags,
149 struct ieee80211_supported_band *sband,
150 struct ieee80211_channel *channel,
152 const struct ieee80211_ht_operation *ht_oper,
153 const struct ieee80211_vht_operation *vht_oper,
154 const struct ieee80211_he_operation *he_oper,
155 const struct ieee80211_eht_operation *eht_oper,
156 const struct ieee80211_s1g_oper_ie *s1g_oper,
157 struct cfg80211_chan_def *chandef, bool tracking)
159 struct cfg80211_chan_def vht_chandef;
160 struct ieee80211_sta_ht_cap sta_ht_cap;
161 ieee80211_conn_flags_t ret;
164 memset(chandef, 0, sizeof(struct cfg80211_chan_def));
165 chandef->chan = channel;
166 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
167 chandef->center_freq1 = channel->center_freq;
168 chandef->freq1_offset = channel->freq_offset;
170 if (channel->band == NL80211_BAND_6GHZ) {
171 if (!ieee80211_chandef_he_6ghz_oper(sdata, he_oper, eht_oper,
174 "bad 6 GHz operation, disabling HT/VHT/HE/EHT\n");
175 ret = IEEE80211_CONN_DISABLE_HT |
176 IEEE80211_CONN_DISABLE_VHT |
177 IEEE80211_CONN_DISABLE_HE |
178 IEEE80211_CONN_DISABLE_EHT;
182 vht_chandef = *chandef;
184 } else if (sband->band == NL80211_BAND_S1GHZ) {
185 if (!ieee80211_chandef_s1g_oper(s1g_oper, chandef)) {
187 "Missing S1G Operation Element? Trying operating == primary\n");
188 chandef->width = ieee80211_s1g_channel_width(channel);
191 ret = IEEE80211_CONN_DISABLE_HT | IEEE80211_CONN_DISABLE_40MHZ |
192 IEEE80211_CONN_DISABLE_VHT |
193 IEEE80211_CONN_DISABLE_80P80MHZ |
194 IEEE80211_CONN_DISABLE_160MHZ;
198 memcpy(&sta_ht_cap, &sband->ht_cap, sizeof(sta_ht_cap));
199 ieee80211_apply_htcap_overrides(sdata, &sta_ht_cap);
201 if (!ht_oper || !sta_ht_cap.ht_supported) {
202 mlme_dbg(sdata, "HT operation missing / HT not supported\n");
203 ret = IEEE80211_CONN_DISABLE_HT |
204 IEEE80211_CONN_DISABLE_VHT |
205 IEEE80211_CONN_DISABLE_HE |
206 IEEE80211_CONN_DISABLE_EHT;
210 chandef->width = NL80211_CHAN_WIDTH_20;
212 ht_cfreq = ieee80211_channel_to_frequency(ht_oper->primary_chan,
214 /* check that channel matches the right operating channel */
215 if (!tracking && channel->center_freq != ht_cfreq) {
217 * It's possible that some APs are confused here;
218 * Netgear WNDR3700 sometimes reports 4 higher than
219 * the actual channel in association responses, but
220 * since we look at probe response/beacon data here
224 "Wrong control channel: center-freq: %d ht-cfreq: %d ht->primary_chan: %d band: %d - Disabling HT\n",
225 channel->center_freq, ht_cfreq,
226 ht_oper->primary_chan, channel->band);
227 ret = IEEE80211_CONN_DISABLE_HT |
228 IEEE80211_CONN_DISABLE_VHT |
229 IEEE80211_CONN_DISABLE_HE |
230 IEEE80211_CONN_DISABLE_EHT;
234 /* check 40 MHz support, if we have it */
235 if (sta_ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) {
236 ieee80211_chandef_ht_oper(ht_oper, chandef);
238 mlme_dbg(sdata, "40 MHz not supported\n");
239 /* 40 MHz (and 80 MHz) must be supported for VHT */
240 ret = IEEE80211_CONN_DISABLE_VHT;
241 /* also mark 40 MHz disabled */
242 ret |= IEEE80211_CONN_DISABLE_40MHZ;
246 if (!vht_oper || !sband->vht_cap.vht_supported) {
247 mlme_dbg(sdata, "VHT operation missing / VHT not supported\n");
248 ret = IEEE80211_CONN_DISABLE_VHT;
252 vht_chandef = *chandef;
253 if (!(conn_flags & IEEE80211_CONN_DISABLE_HE) &&
255 (le32_to_cpu(he_oper->he_oper_params) &
256 IEEE80211_HE_OPERATION_VHT_OPER_INFO)) {
257 struct ieee80211_vht_operation he_oper_vht_cap;
260 * Set only first 3 bytes (other 2 aren't used in
261 * ieee80211_chandef_vht_oper() anyway)
263 memcpy(&he_oper_vht_cap, he_oper->optional, 3);
264 he_oper_vht_cap.basic_mcs_set = cpu_to_le16(0);
266 if (!ieee80211_chandef_vht_oper(&sdata->local->hw, vht_cap_info,
267 &he_oper_vht_cap, ht_oper,
269 if (!(conn_flags & IEEE80211_CONN_DISABLE_HE))
271 "HE AP VHT information is invalid, disabling HE\n");
272 ret = IEEE80211_CONN_DISABLE_HE | IEEE80211_CONN_DISABLE_EHT;
275 } else if (!ieee80211_chandef_vht_oper(&sdata->local->hw,
279 if (!(conn_flags & IEEE80211_CONN_DISABLE_VHT))
281 "AP VHT information is invalid, disabling VHT\n");
282 ret = IEEE80211_CONN_DISABLE_VHT;
286 if (!cfg80211_chandef_valid(&vht_chandef)) {
287 if (!(conn_flags & IEEE80211_CONN_DISABLE_VHT))
289 "AP VHT information is invalid, disabling VHT\n");
290 ret = IEEE80211_CONN_DISABLE_VHT;
294 if (cfg80211_chandef_identical(chandef, &vht_chandef)) {
299 if (!cfg80211_chandef_compatible(chandef, &vht_chandef)) {
300 if (!(conn_flags & IEEE80211_CONN_DISABLE_VHT))
302 "AP VHT information doesn't match HT, disabling VHT\n");
303 ret = IEEE80211_CONN_DISABLE_VHT;
307 *chandef = vht_chandef;
310 * handle the case that the EHT operation indicates that it holds EHT
311 * operation information (in case that the channel width differs from
312 * the channel width reported in HT/VHT/HE).
314 if (eht_oper && (eht_oper->params & IEEE80211_EHT_OPER_INFO_PRESENT)) {
315 struct cfg80211_chan_def eht_chandef = *chandef;
317 ieee80211_chandef_eht_oper(sdata, eht_oper,
319 NL80211_CHAN_WIDTH_160,
320 false, &eht_chandef);
322 if (!cfg80211_chandef_valid(&eht_chandef)) {
323 if (!(conn_flags & IEEE80211_CONN_DISABLE_EHT))
325 "AP EHT information is invalid, disabling EHT\n");
326 ret = IEEE80211_CONN_DISABLE_EHT;
330 if (!cfg80211_chandef_compatible(chandef, &eht_chandef)) {
331 if (!(conn_flags & IEEE80211_CONN_DISABLE_EHT))
333 "AP EHT information is incompatible, disabling EHT\n");
334 ret = IEEE80211_CONN_DISABLE_EHT;
338 *chandef = eht_chandef;
345 * When tracking the current AP, don't do any further checks if the
346 * new chandef is identical to the one we're currently using for the
347 * connection. This keeps us from playing ping-pong with regulatory,
348 * without it the following can happen (for example):
349 * - connect to an AP with 80 MHz, world regdom allows 80 MHz
350 * - AP advertises regdom US
351 * - CRDA loads regdom US with 80 MHz prohibited (old database)
352 * - the code below detects an unsupported channel, downgrades, and
353 * we disconnect from the AP in the caller
354 * - disconnect causes CRDA to reload world regdomain and the game
356 * (see https://bugzilla.kernel.org/show_bug.cgi?id=70881)
358 * It seems possible that there are still scenarios with CSA or real
359 * bandwidth changes where a this could happen, but those cases are
360 * less common and wouldn't completely prevent using the AP.
363 cfg80211_chandef_identical(chandef, &link->conf->chandef))
366 /* don't print the message below for VHT mismatch if VHT is disabled */
367 if (ret & IEEE80211_CONN_DISABLE_VHT)
368 vht_chandef = *chandef;
371 * Ignore the DISABLED flag when we're already connected and only
372 * tracking the APs beacon for bandwidth changes - otherwise we
373 * might get disconnected here if we connect to an AP, update our
374 * regulatory information based on the AP's country IE and the
375 * information we have is wrong/outdated and disables the channel
376 * that we're actually using for the connection to the AP.
378 while (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
380 IEEE80211_CHAN_DISABLED)) {
381 if (WARN_ON(chandef->width == NL80211_CHAN_WIDTH_20_NOHT)) {
382 ret = IEEE80211_CONN_DISABLE_HT |
383 IEEE80211_CONN_DISABLE_VHT |
384 IEEE80211_CONN_DISABLE_HE |
385 IEEE80211_CONN_DISABLE_EHT;
389 ret |= ieee80211_chandef_downgrade(chandef);
392 if (!he_oper || !cfg80211_chandef_usable(sdata->wdev.wiphy, chandef,
393 IEEE80211_CHAN_NO_HE))
394 ret |= IEEE80211_CONN_DISABLE_HE | IEEE80211_CONN_DISABLE_EHT;
396 if (!eht_oper || !cfg80211_chandef_usable(sdata->wdev.wiphy, chandef,
397 IEEE80211_CHAN_NO_EHT))
398 ret |= IEEE80211_CONN_DISABLE_EHT;
400 if (chandef->width != vht_chandef.width && !tracking)
402 "capabilities/regulatory prevented using AP HT/VHT configuration, downgraded\n");
404 WARN_ON_ONCE(!cfg80211_chandef_valid(chandef));
408 static int ieee80211_config_bw(struct ieee80211_link_data *link,
409 const struct ieee80211_ht_cap *ht_cap,
410 const struct ieee80211_vht_cap *vht_cap,
411 const struct ieee80211_ht_operation *ht_oper,
412 const struct ieee80211_vht_operation *vht_oper,
413 const struct ieee80211_he_operation *he_oper,
414 const struct ieee80211_eht_operation *eht_oper,
415 const struct ieee80211_s1g_oper_ie *s1g_oper,
416 const u8 *bssid, u32 *changed)
418 struct ieee80211_sub_if_data *sdata = link->sdata;
419 struct ieee80211_local *local = sdata->local;
420 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
421 struct ieee80211_channel *chan = link->conf->chandef.chan;
422 struct ieee80211_supported_band *sband =
423 local->hw.wiphy->bands[chan->band];
424 struct cfg80211_chan_def chandef;
426 ieee80211_conn_flags_t flags;
427 u32 vht_cap_info = 0;
430 /* if HT was/is disabled, don't track any bandwidth changes */
431 if (link->u.mgd.conn_flags & IEEE80211_CONN_DISABLE_HT || !ht_oper)
434 /* don't check VHT if we associated as non-VHT station */
435 if (link->u.mgd.conn_flags & IEEE80211_CONN_DISABLE_VHT)
438 /* don't check HE if we associated as non-HE station */
439 if (link->u.mgd.conn_flags & IEEE80211_CONN_DISABLE_HE ||
440 !ieee80211_get_he_iftype_cap(sband,
441 ieee80211_vif_type_p2p(&sdata->vif))) {
446 /* don't check EHT if we associated as non-EHT station */
447 if (link->u.mgd.conn_flags & IEEE80211_CONN_DISABLE_EHT ||
448 !ieee80211_get_eht_iftype_cap(sband,
449 ieee80211_vif_type_p2p(&sdata->vif)))
453 * if bss configuration changed store the new one -
454 * this may be applicable even if channel is identical
456 ht_opmode = le16_to_cpu(ht_oper->operation_mode);
457 if (link->conf->ht_operation_mode != ht_opmode) {
458 *changed |= BSS_CHANGED_HT;
459 link->conf->ht_operation_mode = ht_opmode;
463 vht_cap_info = le32_to_cpu(vht_cap->vht_cap_info);
465 /* calculate new channel (type) based on HT/VHT/HE operation IEs */
466 flags = ieee80211_determine_chantype(sdata, link,
467 link->u.mgd.conn_flags,
468 sband, chan, vht_cap_info,
471 s1g_oper, &chandef, true);
474 * Downgrade the new channel if we associated with restricted
475 * capabilities. For example, if we associated as a 20 MHz STA
476 * to a 40 MHz AP (due to regulatory, capabilities or config
477 * reasons) then switching to a 40 MHz channel now won't do us
478 * any good -- we couldn't use it with the AP.
480 if (link->u.mgd.conn_flags & IEEE80211_CONN_DISABLE_80P80MHZ &&
481 chandef.width == NL80211_CHAN_WIDTH_80P80)
482 flags |= ieee80211_chandef_downgrade(&chandef);
483 if (link->u.mgd.conn_flags & IEEE80211_CONN_DISABLE_160MHZ &&
484 chandef.width == NL80211_CHAN_WIDTH_160)
485 flags |= ieee80211_chandef_downgrade(&chandef);
486 if (link->u.mgd.conn_flags & IEEE80211_CONN_DISABLE_40MHZ &&
487 chandef.width > NL80211_CHAN_WIDTH_20)
488 flags |= ieee80211_chandef_downgrade(&chandef);
490 if (cfg80211_chandef_identical(&chandef, &link->conf->chandef))
494 "AP %pM changed bandwidth, new config is %d.%03d MHz, width %d (%d.%03d/%d MHz)\n",
495 link->u.mgd.bssid, chandef.chan->center_freq,
496 chandef.chan->freq_offset, chandef.width,
497 chandef.center_freq1, chandef.freq1_offset,
498 chandef.center_freq2);
500 if (flags != (link->u.mgd.conn_flags &
501 (IEEE80211_CONN_DISABLE_HT |
502 IEEE80211_CONN_DISABLE_VHT |
503 IEEE80211_CONN_DISABLE_HE |
504 IEEE80211_CONN_DISABLE_EHT |
505 IEEE80211_CONN_DISABLE_40MHZ |
506 IEEE80211_CONN_DISABLE_80P80MHZ |
507 IEEE80211_CONN_DISABLE_160MHZ |
508 IEEE80211_CONN_DISABLE_320MHZ)) ||
509 !cfg80211_chandef_valid(&chandef)) {
511 "AP %pM changed caps/bw in a way we can't support (0x%x/0x%x) - disconnect\n",
512 link->u.mgd.bssid, flags, ifmgd->flags);
516 ret = ieee80211_link_change_bandwidth(link, &chandef, changed);
520 "AP %pM changed bandwidth to incompatible one - disconnect\n",
528 /* frame sending functions */
530 static void ieee80211_add_ht_ie(struct ieee80211_sub_if_data *sdata,
531 struct sk_buff *skb, u8 ap_ht_param,
532 struct ieee80211_supported_band *sband,
533 struct ieee80211_channel *channel,
534 enum ieee80211_smps_mode smps,
535 ieee80211_conn_flags_t conn_flags)
538 u32 flags = channel->flags;
540 struct ieee80211_sta_ht_cap ht_cap;
542 BUILD_BUG_ON(sizeof(ht_cap) != sizeof(sband->ht_cap));
544 memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
545 ieee80211_apply_htcap_overrides(sdata, &ht_cap);
547 /* determine capability flags */
550 switch (ap_ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
551 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
552 if (flags & IEEE80211_CHAN_NO_HT40PLUS) {
553 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
554 cap &= ~IEEE80211_HT_CAP_SGI_40;
557 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
558 if (flags & IEEE80211_CHAN_NO_HT40MINUS) {
559 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
560 cap &= ~IEEE80211_HT_CAP_SGI_40;
566 * If 40 MHz was disabled associate as though we weren't
567 * capable of 40 MHz -- some broken APs will never fall
568 * back to trying to transmit in 20 MHz.
570 if (conn_flags & IEEE80211_CONN_DISABLE_40MHZ) {
571 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
572 cap &= ~IEEE80211_HT_CAP_SGI_40;
575 /* set SM PS mode properly */
576 cap &= ~IEEE80211_HT_CAP_SM_PS;
578 case IEEE80211_SMPS_AUTOMATIC:
579 case IEEE80211_SMPS_NUM_MODES:
582 case IEEE80211_SMPS_OFF:
583 cap |= WLAN_HT_CAP_SM_PS_DISABLED <<
584 IEEE80211_HT_CAP_SM_PS_SHIFT;
586 case IEEE80211_SMPS_STATIC:
587 cap |= WLAN_HT_CAP_SM_PS_STATIC <<
588 IEEE80211_HT_CAP_SM_PS_SHIFT;
590 case IEEE80211_SMPS_DYNAMIC:
591 cap |= WLAN_HT_CAP_SM_PS_DYNAMIC <<
592 IEEE80211_HT_CAP_SM_PS_SHIFT;
596 /* reserve and fill IE */
597 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
598 ieee80211_ie_build_ht_cap(pos, &ht_cap, cap);
601 /* This function determines vht capability flags for the association
603 * Note - the function returns true to own the MU-MIMO capability
605 static bool ieee80211_add_vht_ie(struct ieee80211_sub_if_data *sdata,
607 struct ieee80211_supported_band *sband,
608 struct ieee80211_vht_cap *ap_vht_cap,
609 ieee80211_conn_flags_t conn_flags)
611 struct ieee80211_local *local = sdata->local;
614 struct ieee80211_sta_vht_cap vht_cap;
615 u32 mask, ap_bf_sts, our_bf_sts;
616 bool mu_mimo_owner = false;
618 BUILD_BUG_ON(sizeof(vht_cap) != sizeof(sband->vht_cap));
620 memcpy(&vht_cap, &sband->vht_cap, sizeof(vht_cap));
621 ieee80211_apply_vhtcap_overrides(sdata, &vht_cap);
623 /* determine capability flags */
626 if (conn_flags & IEEE80211_CONN_DISABLE_80P80MHZ) {
627 u32 bw = cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
629 cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
630 if (bw == IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ ||
631 bw == IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ)
632 cap |= IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
635 if (conn_flags & IEEE80211_CONN_DISABLE_160MHZ) {
636 cap &= ~IEEE80211_VHT_CAP_SHORT_GI_160;
637 cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
641 * Some APs apparently get confused if our capabilities are better
642 * than theirs, so restrict what we advertise in the assoc request.
644 if (!(ap_vht_cap->vht_cap_info &
645 cpu_to_le32(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)))
646 cap &= ~(IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
647 IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE);
648 else if (!(ap_vht_cap->vht_cap_info &
649 cpu_to_le32(IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)))
650 cap &= ~IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE;
653 * If some other vif is using the MU-MIMO capability we cannot associate
654 * using MU-MIMO - this will lead to contradictions in the group-id
656 * Ownership is defined since association request, in order to avoid
657 * simultaneous associations with MU-MIMO.
659 if (cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE) {
660 bool disable_mu_mimo = false;
661 struct ieee80211_sub_if_data *other;
663 list_for_each_entry_rcu(other, &local->interfaces, list) {
664 if (other->vif.bss_conf.mu_mimo_owner) {
665 disable_mu_mimo = true;
670 cap &= ~IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE;
672 mu_mimo_owner = true;
675 mask = IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
677 ap_bf_sts = le32_to_cpu(ap_vht_cap->vht_cap_info) & mask;
678 our_bf_sts = cap & mask;
680 if (ap_bf_sts < our_bf_sts) {
685 /* reserve and fill IE */
686 pos = skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2);
687 ieee80211_ie_build_vht_cap(pos, &vht_cap, cap);
689 return mu_mimo_owner;
692 /* This function determines HE capability flags for the association
695 static void ieee80211_add_he_ie(struct ieee80211_sub_if_data *sdata,
697 struct ieee80211_supported_band *sband,
698 ieee80211_conn_flags_t conn_flags)
700 u8 *pos, *pre_he_pos;
701 const struct ieee80211_sta_he_cap *he_cap;
704 he_cap = ieee80211_get_he_iftype_cap(sband,
705 ieee80211_vif_type_p2p(&sdata->vif));
706 if (WARN_ON(!he_cap))
709 /* get a max size estimate */
711 2 + 1 + sizeof(he_cap->he_cap_elem) +
712 ieee80211_he_mcs_nss_size(&he_cap->he_cap_elem) +
713 ieee80211_he_ppe_size(he_cap->ppe_thres[0],
714 he_cap->he_cap_elem.phy_cap_info);
715 pos = skb_put(skb, he_cap_size);
717 pos = ieee80211_ie_build_he_cap(conn_flags,
718 pos, he_cap, pos + he_cap_size);
719 /* trim excess if any */
720 skb_trim(skb, skb->len - (pre_he_pos + he_cap_size - pos));
722 ieee80211_ie_build_he_6ghz_cap(sdata, skb);
725 static void ieee80211_add_eht_ie(struct ieee80211_sub_if_data *sdata,
727 struct ieee80211_supported_band *sband)
730 const struct ieee80211_sta_he_cap *he_cap;
731 const struct ieee80211_sta_eht_cap *eht_cap;
734 he_cap = ieee80211_get_he_iftype_cap(sband,
735 ieee80211_vif_type_p2p(&sdata->vif));
736 eht_cap = ieee80211_get_eht_iftype_cap(sband,
737 ieee80211_vif_type_p2p(&sdata->vif));
740 * EHT capabilities element is only added if the HE capabilities element
741 * was added so assume that 'he_cap' is valid and don't check it.
743 if (WARN_ON(!he_cap || !eht_cap))
747 2 + 1 + sizeof(eht_cap->eht_cap_elem) +
748 ieee80211_eht_mcs_nss_size(&he_cap->he_cap_elem,
749 &eht_cap->eht_cap_elem) +
750 ieee80211_eht_ppe_size(eht_cap->eht_ppe_thres[0],
751 eht_cap->eht_cap_elem.phy_cap_info);
752 pos = skb_put(skb, eht_cap_size);
753 ieee80211_ie_build_eht_cap(pos, he_cap, eht_cap, pos + eht_cap_size);
756 static void ieee80211_assoc_add_rates(struct sk_buff *skb,
757 enum nl80211_chan_width width,
758 struct ieee80211_supported_band *sband,
759 struct ieee80211_mgd_assoc_data *assoc_data)
761 unsigned int shift = ieee80211_chanwidth_get_shift(width);
762 unsigned int rates_len, supp_rates_len;
767 if (assoc_data->supp_rates_len) {
769 * Get all rates supported by the device and the AP as
770 * some APs don't like getting a superset of their rates
771 * in the association request (e.g. D-Link DAP 1353 in
774 rates_len = ieee80211_parse_bitrates(width, sband,
775 assoc_data->supp_rates,
776 assoc_data->supp_rates_len,
780 * In case AP not provide any supported rates information
781 * before association, we send information element(s) with
782 * all rates that we support.
784 rates_len = sband->n_bitrates;
785 for (i = 0; i < sband->n_bitrates; i++)
789 supp_rates_len = rates_len;
790 if (supp_rates_len > 8)
793 pos = skb_put(skb, supp_rates_len + 2);
794 *pos++ = WLAN_EID_SUPP_RATES;
795 *pos++ = supp_rates_len;
798 for (i = 0; i < sband->n_bitrates; i++) {
799 if (BIT(i) & rates) {
800 int rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
808 if (rates_len > count) {
809 pos = skb_put(skb, rates_len - count + 2);
810 *pos++ = WLAN_EID_EXT_SUPP_RATES;
811 *pos++ = rates_len - count;
813 for (i++; i < sband->n_bitrates; i++) {
814 if (BIT(i) & rates) {
817 rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
825 static size_t ieee80211_add_before_ht_elems(struct sk_buff *skb,
832 static const u8 before_ht[] = {
835 WLAN_EID_EXT_SUPP_RATES,
836 WLAN_EID_PWR_CAPABILITY,
837 WLAN_EID_SUPPORTED_CHANNELS,
840 WLAN_EID_RRM_ENABLED_CAPABILITIES,
841 WLAN_EID_MOBILITY_DOMAIN,
842 WLAN_EID_FAST_BSS_TRANSITION, /* reassoc only */
843 WLAN_EID_RIC_DATA, /* reassoc only */
844 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
846 static const u8 after_ric[] = {
847 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
848 WLAN_EID_HT_CAPABILITY,
849 WLAN_EID_BSS_COEX_2040,
850 /* luckily this is almost always there */
851 WLAN_EID_EXT_CAPABILITY,
852 WLAN_EID_QOS_TRAFFIC_CAPA,
853 WLAN_EID_TIM_BCAST_REQ,
854 WLAN_EID_INTERWORKING,
855 /* 60 GHz (Multi-band, DMG, MMS) can't happen */
856 WLAN_EID_VHT_CAPABILITY,
857 WLAN_EID_OPMODE_NOTIF,
863 noffset = ieee80211_ie_split_ric(elems, elems_len,
865 ARRAY_SIZE(before_ht),
867 ARRAY_SIZE(after_ric),
869 skb_put_data(skb, elems + offset, noffset - offset);
874 static size_t ieee80211_add_before_vht_elems(struct sk_buff *skb,
879 static const u8 before_vht[] = {
881 * no need to list the ones split off before HT
884 WLAN_EID_BSS_COEX_2040,
885 WLAN_EID_EXT_CAPABILITY,
886 WLAN_EID_QOS_TRAFFIC_CAPA,
887 WLAN_EID_TIM_BCAST_REQ,
888 WLAN_EID_INTERWORKING,
889 /* 60 GHz (Multi-band, DMG, MMS) can't happen */
896 /* RIC already taken care of in ieee80211_add_before_ht_elems() */
897 noffset = ieee80211_ie_split(elems, elems_len,
898 before_vht, ARRAY_SIZE(before_vht),
900 skb_put_data(skb, elems + offset, noffset - offset);
905 static size_t ieee80211_add_before_he_elems(struct sk_buff *skb,
910 static const u8 before_he[] = {
912 * no need to list the ones split off before VHT
915 WLAN_EID_OPMODE_NOTIF,
916 WLAN_EID_EXTENSION, WLAN_EID_EXT_FUTURE_CHAN_GUIDANCE,
918 WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_SESSION,
919 WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_PUBLIC_KEY,
920 WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_KEY_CONFIRM,
921 WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_HLP_CONTAINER,
922 WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_IP_ADDR_ASSIGN,
923 /* TODO: add 11ah/11aj/11ak elements */
930 /* RIC already taken care of in ieee80211_add_before_ht_elems() */
931 noffset = ieee80211_ie_split(elems, elems_len,
932 before_he, ARRAY_SIZE(before_he),
934 skb_put_data(skb, elems + offset, noffset - offset);
939 #define PRESENT_ELEMS_MAX 8
940 #define PRESENT_ELEM_EXT_OFFS 0x100
942 static void ieee80211_assoc_add_ml_elem(struct ieee80211_sub_if_data *sdata,
943 struct sk_buff *skb, u16 capab,
944 const struct element *ext_capa,
945 const u16 *present_elems);
947 static size_t ieee80211_assoc_link_elems(struct ieee80211_sub_if_data *sdata,
948 struct sk_buff *skb, u16 *capab,
949 const struct element *ext_capa,
950 const u8 *extra_elems,
951 size_t extra_elems_len,
952 unsigned int link_id,
953 struct ieee80211_link_data *link,
956 enum nl80211_iftype iftype = ieee80211_vif_type_p2p(&sdata->vif);
957 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
958 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
959 struct cfg80211_bss *cbss = assoc_data->link[link_id].bss;
960 struct ieee80211_channel *chan = cbss->channel;
961 const struct ieee80211_sband_iftype_data *iftd;
962 struct ieee80211_local *local = sdata->local;
963 struct ieee80211_supported_band *sband;
964 enum nl80211_chan_width width = NL80211_CHAN_WIDTH_20;
965 struct ieee80211_chanctx_conf *chanctx_conf;
966 enum ieee80211_smps_mode smps_mode;
967 u16 orig_capab = *capab;
969 int present_elems_len = 0;
973 #define ADD_PRESENT_ELEM(id) do { \
974 /* need a last for termination - we use 0 == SSID */ \
975 if (!WARN_ON(present_elems_len >= PRESENT_ELEMS_MAX - 1)) \
976 present_elems[present_elems_len++] = (id); \
978 #define ADD_PRESENT_EXT_ELEM(id) ADD_PRESENT_ELEM(PRESENT_ELEM_EXT_OFFS | (id))
981 smps_mode = link->smps_mode;
982 else if (sdata->u.mgd.powersave)
983 smps_mode = IEEE80211_SMPS_DYNAMIC;
985 smps_mode = IEEE80211_SMPS_OFF;
989 * 5/10 MHz scenarios are only viable without MLO, in which
990 * case this pointer should be used ... All of this is a bit
991 * unclear though, not sure this even works at all.
994 chanctx_conf = rcu_dereference(link->conf->chanctx_conf);
996 width = chanctx_conf->def.width;
1000 sband = local->hw.wiphy->bands[chan->band];
1001 iftd = ieee80211_get_sband_iftype_data(sband, iftype);
1003 if (sband->band == NL80211_BAND_2GHZ) {
1004 *capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
1005 *capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
1008 if ((cbss->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
1009 ieee80211_hw_check(&local->hw, SPECTRUM_MGMT))
1010 *capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
1012 if (sband->band != NL80211_BAND_S1GHZ)
1013 ieee80211_assoc_add_rates(skb, width, sband, assoc_data);
1015 if (*capab & WLAN_CAPABILITY_SPECTRUM_MGMT ||
1016 *capab & WLAN_CAPABILITY_RADIO_MEASURE) {
1017 struct cfg80211_chan_def chandef = {
1022 pos = skb_put(skb, 4);
1023 *pos++ = WLAN_EID_PWR_CAPABILITY;
1025 *pos++ = 0; /* min tx power */
1027 *pos++ = ieee80211_chandef_max_power(&chandef);
1028 ADD_PRESENT_ELEM(WLAN_EID_PWR_CAPABILITY);
1032 * Per spec, we shouldn't include the list of channels if we advertise
1033 * support for extended channel switching, but we've always done that;
1034 * (for now?) apply this restriction only on the (new) 6 GHz band.
1036 if (*capab & WLAN_CAPABILITY_SPECTRUM_MGMT &&
1037 (sband->band != NL80211_BAND_6GHZ ||
1038 !ext_capa || ext_capa->datalen < 1 ||
1039 !(ext_capa->data[0] & WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING))) {
1040 /* TODO: get this in reg domain format */
1041 pos = skb_put(skb, 2 * sband->n_channels + 2);
1042 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
1043 *pos++ = 2 * sband->n_channels;
1044 for (i = 0; i < sband->n_channels; i++) {
1045 int cf = sband->channels[i].center_freq;
1047 *pos++ = ieee80211_frequency_to_channel(cf);
1048 *pos++ = 1; /* one channel in the subband*/
1050 ADD_PRESENT_ELEM(WLAN_EID_SUPPORTED_CHANNELS);
1053 /* if present, add any custom IEs that go before HT */
1054 offset = ieee80211_add_before_ht_elems(skb, extra_elems,
1058 if (sband->band != NL80211_BAND_6GHZ &&
1059 !(assoc_data->link[link_id].conn_flags & IEEE80211_CONN_DISABLE_HT)) {
1060 ieee80211_add_ht_ie(sdata, skb,
1061 assoc_data->link[link_id].ap_ht_param,
1062 sband, chan, smps_mode,
1063 assoc_data->link[link_id].conn_flags);
1064 ADD_PRESENT_ELEM(WLAN_EID_HT_CAPABILITY);
1067 /* if present, add any custom IEs that go before VHT */
1068 offset = ieee80211_add_before_vht_elems(skb, extra_elems,
1072 if (sband->band != NL80211_BAND_6GHZ &&
1073 !(assoc_data->link[link_id].conn_flags & IEEE80211_CONN_DISABLE_VHT)) {
1074 bool mu_mimo_owner =
1075 ieee80211_add_vht_ie(sdata, skb, sband,
1076 &assoc_data->link[link_id].ap_vht_cap,
1077 assoc_data->link[link_id].conn_flags);
1080 link->conf->mu_mimo_owner = mu_mimo_owner;
1081 ADD_PRESENT_ELEM(WLAN_EID_VHT_CAPABILITY);
1085 * If AP doesn't support HT, mark HE and EHT as disabled.
1086 * If on the 5GHz band, make sure it supports VHT.
1088 if (assoc_data->link[link_id].conn_flags & IEEE80211_CONN_DISABLE_HT ||
1089 (sband->band == NL80211_BAND_5GHZ &&
1090 assoc_data->link[link_id].conn_flags & IEEE80211_CONN_DISABLE_VHT))
1091 assoc_data->link[link_id].conn_flags |=
1092 IEEE80211_CONN_DISABLE_HE |
1093 IEEE80211_CONN_DISABLE_EHT;
1095 /* if present, add any custom IEs that go before HE */
1096 offset = ieee80211_add_before_he_elems(skb, extra_elems,
1100 if (!(assoc_data->link[link_id].conn_flags & IEEE80211_CONN_DISABLE_HE)) {
1101 ieee80211_add_he_ie(sdata, skb, sband,
1102 assoc_data->link[link_id].conn_flags);
1103 ADD_PRESENT_EXT_ELEM(WLAN_EID_EXT_HE_CAPABILITY);
1107 * careful - need to know about all the present elems before
1108 * calling ieee80211_assoc_add_ml_elem(), so add this one if
1109 * we're going to put it after the ML element
1111 if (!(assoc_data->link[link_id].conn_flags & IEEE80211_CONN_DISABLE_EHT))
1112 ADD_PRESENT_EXT_ELEM(WLAN_EID_EXT_EHT_CAPABILITY);
1114 if (link_id == assoc_data->assoc_link_id)
1115 ieee80211_assoc_add_ml_elem(sdata, skb, orig_capab, ext_capa,
1118 /* crash if somebody gets it wrong */
1119 present_elems = NULL;
1121 if (!(assoc_data->link[link_id].conn_flags & IEEE80211_CONN_DISABLE_EHT))
1122 ieee80211_add_eht_ie(sdata, skb, sband);
1124 if (sband->band == NL80211_BAND_S1GHZ) {
1125 ieee80211_add_aid_request_ie(sdata, skb);
1126 ieee80211_add_s1g_capab_ie(sdata, &sband->s1g_cap, skb);
1129 if (iftd && iftd->vendor_elems.data && iftd->vendor_elems.len)
1130 skb_put_data(skb, iftd->vendor_elems.data, iftd->vendor_elems.len);
1133 link->u.mgd.conn_flags = assoc_data->link[link_id].conn_flags;
1138 static void ieee80211_add_non_inheritance_elem(struct sk_buff *skb,
1142 unsigned int skb_len = skb->len;
1145 u8 *len, *list_len = NULL;
1147 skb_put_u8(skb, WLAN_EID_EXTENSION);
1148 len = skb_put(skb, 1);
1149 skb_put_u8(skb, WLAN_EID_EXT_NON_INHERITANCE);
1151 for (i = 0; i < PRESENT_ELEMS_MAX && outer[i]; i++) {
1152 u16 elem = outer[i];
1153 bool have_inner = false;
1154 bool at_extension = false;
1156 /* should at least be sorted in the sense of normal -> ext */
1157 WARN_ON(at_extension && elem < PRESENT_ELEM_EXT_OFFS);
1159 /* switch to extension list */
1160 if (!at_extension && elem >= PRESENT_ELEM_EXT_OFFS) {
1161 at_extension = true;
1167 for (j = 0; j < PRESENT_ELEMS_MAX && inner[j]; j++) {
1168 if (elem == inner[j]) {
1178 list_len = skb_put(skb, 1);
1182 skb_put_u8(skb, (u8)elem);
1186 skb_trim(skb, skb_len);
1188 *len = skb->len - skb_len - 2;
1191 static void ieee80211_assoc_add_ml_elem(struct ieee80211_sub_if_data *sdata,
1192 struct sk_buff *skb, u16 capab,
1193 const struct element *ext_capa,
1194 const u16 *outer_present_elems)
1196 struct ieee80211_local *local = sdata->local;
1197 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1198 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
1199 struct ieee80211_multi_link_elem *ml_elem;
1200 struct ieee80211_mle_basic_common_info *common;
1201 const struct wiphy_iftype_ext_capab *ift_ext_capa;
1202 __le16 eml_capa = 0, mld_capa_ops = 0;
1203 unsigned int link_id;
1207 if (!sdata->vif.valid_links)
1210 ift_ext_capa = cfg80211_get_iftype_ext_capa(local->hw.wiphy,
1211 ieee80211_vif_type_p2p(&sdata->vif));
1213 eml_capa = cpu_to_le16(ift_ext_capa->eml_capabilities);
1214 mld_capa_ops = cpu_to_le16(ift_ext_capa->mld_capa_and_ops);
1217 skb_put_u8(skb, WLAN_EID_EXTENSION);
1218 ml_elem_len = skb_put(skb, 1);
1219 skb_put_u8(skb, WLAN_EID_EXT_EHT_MULTI_LINK);
1220 ml_elem = skb_put(skb, sizeof(*ml_elem));
1222 cpu_to_le16(IEEE80211_ML_CONTROL_TYPE_BASIC |
1223 IEEE80211_MLC_BASIC_PRES_EML_CAPA |
1224 IEEE80211_MLC_BASIC_PRES_MLD_CAPA_OP);
1225 common = skb_put(skb, sizeof(*common));
1226 common->len = sizeof(*common) +
1227 2 + /* EML capabilities */
1228 2; /* MLD capa/ops */
1229 memcpy(common->mld_mac_addr, sdata->vif.addr, ETH_ALEN);
1230 skb_put_data(skb, &eml_capa, sizeof(eml_capa));
1231 /* need indication from userspace to support this */
1232 mld_capa_ops &= ~cpu_to_le16(IEEE80211_MLD_CAP_OP_TID_TO_LINK_MAP_NEG_SUPP);
1233 skb_put_data(skb, &mld_capa_ops, sizeof(mld_capa_ops));
1235 for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {
1236 u16 link_present_elems[PRESENT_ELEMS_MAX] = {};
1237 const u8 *extra_elems;
1238 size_t extra_elems_len;
1240 u8 *subelem_len = NULL;
1243 if (!assoc_data->link[link_id].bss ||
1244 link_id == assoc_data->assoc_link_id)
1247 extra_elems = assoc_data->link[link_id].elems;
1248 extra_elems_len = assoc_data->link[link_id].elems_len;
1250 skb_put_u8(skb, IEEE80211_MLE_SUBELEM_PER_STA_PROFILE);
1251 subelem_len = skb_put(skb, 1);
1253 ctrl = cpu_to_le16(link_id |
1254 IEEE80211_MLE_STA_CONTROL_COMPLETE_PROFILE |
1255 IEEE80211_MLE_STA_CONTROL_STA_MAC_ADDR_PRESENT);
1256 skb_put_data(skb, &ctrl, sizeof(ctrl));
1257 skb_put_u8(skb, 1 + ETH_ALEN); /* STA Info Length */
1258 skb_put_data(skb, assoc_data->link[link_id].addr,
1261 * Now add the contents of the (re)association request,
1262 * but the "listen interval" and "current AP address"
1263 * (if applicable) are skipped. So we only have
1264 * the capability field (remember the position and fill
1265 * later), followed by the elements added below by
1266 * calling ieee80211_assoc_link_elems().
1268 capab_pos = skb_put(skb, 2);
1270 extra_used = ieee80211_assoc_link_elems(sdata, skb, &capab,
1275 link_present_elems);
1277 skb_put_data(skb, extra_elems + extra_used,
1278 extra_elems_len - extra_used);
1280 put_unaligned_le16(capab, capab_pos);
1282 ieee80211_add_non_inheritance_elem(skb, outer_present_elems,
1283 link_present_elems);
1285 ieee80211_fragment_element(skb, subelem_len);
1288 ieee80211_fragment_element(skb, ml_elem_len);
1291 static int ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata)
1293 struct ieee80211_local *local = sdata->local;
1294 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1295 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
1296 struct ieee80211_link_data *link;
1297 struct sk_buff *skb;
1298 struct ieee80211_mgmt *mgmt;
1299 u8 *pos, qos_info, *ie_start;
1300 size_t offset, noffset;
1301 u16 capab = WLAN_CAPABILITY_ESS, link_capab;
1303 struct element *ext_capa = NULL;
1304 enum nl80211_iftype iftype = ieee80211_vif_type_p2p(&sdata->vif);
1305 struct ieee80211_prep_tx_info info = {};
1306 unsigned int link_id, n_links = 0;
1307 u16 present_elems[PRESENT_ELEMS_MAX] = {};
1312 /* we know it's writable, cast away the const */
1313 if (assoc_data->ie_len)
1314 ext_capa = (void *)cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY,
1316 assoc_data->ie_len);
1318 sdata_assert_lock(sdata);
1320 size = local->hw.extra_tx_headroom +
1321 sizeof(*mgmt) + /* bit too much but doesn't matter */
1322 2 + assoc_data->ssid_len + /* SSID */
1323 assoc_data->ie_len + /* extra IEs */
1324 (assoc_data->fils_kek_len ? 16 /* AES-SIV */ : 0) +
1327 for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {
1328 struct cfg80211_bss *cbss = assoc_data->link[link_id].bss;
1329 const struct ieee80211_sband_iftype_data *iftd;
1330 struct ieee80211_supported_band *sband;
1335 sband = local->hw.wiphy->bands[cbss->channel->band];
1338 /* add STA profile elements length */
1339 size += assoc_data->link[link_id].elems_len;
1340 /* and supported rates length */
1341 size += 4 + sband->n_bitrates;
1342 /* supported channels */
1343 size += 2 + 2 * sband->n_channels;
1345 iftd = ieee80211_get_sband_iftype_data(sband, iftype);
1347 size += iftd->vendor_elems.len;
1349 /* power capability */
1352 /* HT, VHT, HE, EHT */
1353 size += 2 + sizeof(struct ieee80211_ht_cap);
1354 size += 2 + sizeof(struct ieee80211_vht_cap);
1355 size += 2 + 1 + sizeof(struct ieee80211_he_cap_elem) +
1356 sizeof(struct ieee80211_he_mcs_nss_supp) +
1357 IEEE80211_HE_PPE_THRES_MAX_LEN;
1359 if (sband->band == NL80211_BAND_6GHZ)
1360 size += 2 + 1 + sizeof(struct ieee80211_he_6ghz_capa);
1362 size += 2 + 1 + sizeof(struct ieee80211_eht_cap_elem) +
1363 sizeof(struct ieee80211_eht_mcs_nss_supp) +
1364 IEEE80211_EHT_PPE_THRES_MAX_LEN;
1366 /* non-inheritance element */
1367 size += 2 + 2 + PRESENT_ELEMS_MAX;
1369 /* should be the same across all BSSes */
1370 if (cbss->capability & WLAN_CAPABILITY_PRIVACY)
1371 capab |= WLAN_CAPABILITY_PRIVACY;
1374 if (sdata->vif.valid_links) {
1375 /* consider the multi-link element with STA profile */
1376 size += sizeof(struct ieee80211_multi_link_elem);
1377 /* max common info field in basic multi-link element */
1378 size += sizeof(struct ieee80211_mle_basic_common_info) +
1383 * The capability elements were already considered above;
1384 * note this over-estimates a bit because there's no
1385 * STA profile for the assoc link.
1387 size += (n_links - 1) *
1388 (1 + 1 + /* subelement ID/length */
1389 2 + /* STA control */
1390 1 + ETH_ALEN + 2 /* STA Info field */);
1393 link = sdata_dereference(sdata->link[assoc_data->assoc_link_id], sdata);
1397 if (WARN_ON(!assoc_data->link[assoc_data->assoc_link_id].bss))
1400 skb = alloc_skb(size, GFP_KERNEL);
1404 skb_reserve(skb, local->hw.extra_tx_headroom);
1406 if (ifmgd->flags & IEEE80211_STA_ENABLE_RRM)
1407 capab |= WLAN_CAPABILITY_RADIO_MEASURE;
1409 /* Set MBSSID support for HE AP if needed */
1410 if (ieee80211_hw_check(&local->hw, SUPPORTS_ONLY_HE_MULTI_BSSID) &&
1411 !(link->u.mgd.conn_flags & IEEE80211_CONN_DISABLE_HE) &&
1412 ext_capa && ext_capa->datalen >= 3)
1413 ext_capa->data[2] |= WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT;
1415 mgmt = skb_put_zero(skb, 24);
1416 memcpy(mgmt->da, sdata->vif.cfg.ap_addr, ETH_ALEN);
1417 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
1418 memcpy(mgmt->bssid, sdata->vif.cfg.ap_addr, ETH_ALEN);
1420 listen_int = cpu_to_le16(assoc_data->s1g ?
1421 ieee80211_encode_usf(local->hw.conf.listen_interval) :
1422 local->hw.conf.listen_interval);
1423 if (!is_zero_ether_addr(assoc_data->prev_ap_addr)) {
1425 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1426 IEEE80211_STYPE_REASSOC_REQ);
1427 capab_pos = &mgmt->u.reassoc_req.capab_info;
1428 mgmt->u.reassoc_req.listen_interval = listen_int;
1429 memcpy(mgmt->u.reassoc_req.current_ap,
1430 assoc_data->prev_ap_addr, ETH_ALEN);
1431 info.subtype = IEEE80211_STYPE_REASSOC_REQ;
1434 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1435 IEEE80211_STYPE_ASSOC_REQ);
1436 capab_pos = &mgmt->u.assoc_req.capab_info;
1437 mgmt->u.assoc_req.listen_interval = listen_int;
1438 info.subtype = IEEE80211_STYPE_ASSOC_REQ;
1442 pos = skb_put(skb, 2 + assoc_data->ssid_len);
1444 *pos++ = WLAN_EID_SSID;
1445 *pos++ = assoc_data->ssid_len;
1446 memcpy(pos, assoc_data->ssid, assoc_data->ssid_len);
1448 /* add the elements for the assoc (main) link */
1450 offset = ieee80211_assoc_link_elems(sdata, skb, &link_capab,
1454 assoc_data->assoc_link_id, link,
1456 put_unaligned_le16(link_capab, capab_pos);
1458 /* if present, add any custom non-vendor IEs */
1459 if (assoc_data->ie_len) {
1460 noffset = ieee80211_ie_split_vendor(assoc_data->ie,
1463 skb_put_data(skb, assoc_data->ie + offset, noffset - offset);
1467 if (assoc_data->wmm) {
1468 if (assoc_data->uapsd) {
1469 qos_info = ifmgd->uapsd_queues;
1470 qos_info |= (ifmgd->uapsd_max_sp_len <<
1471 IEEE80211_WMM_IE_STA_QOSINFO_SP_SHIFT);
1476 pos = ieee80211_add_wmm_info_ie(skb_put(skb, 9), qos_info);
1479 /* add any remaining custom (i.e. vendor specific here) IEs */
1480 if (assoc_data->ie_len) {
1481 noffset = assoc_data->ie_len;
1482 skb_put_data(skb, assoc_data->ie + offset, noffset - offset);
1485 if (assoc_data->fils_kek_len) {
1486 ret = fils_encrypt_assoc_req(skb, assoc_data);
1493 pos = skb_tail_pointer(skb);
1494 kfree(ifmgd->assoc_req_ies);
1495 ifmgd->assoc_req_ies = kmemdup(ie_start, pos - ie_start, GFP_ATOMIC);
1496 if (!ifmgd->assoc_req_ies) {
1501 ifmgd->assoc_req_ies_len = pos - ie_start;
1503 drv_mgd_prepare_tx(local, sdata, &info);
1505 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
1506 if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
1507 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS |
1508 IEEE80211_TX_INTFL_MLME_CONN_TX;
1509 ieee80211_tx_skb(sdata, skb);
1514 void ieee80211_send_pspoll(struct ieee80211_local *local,
1515 struct ieee80211_sub_if_data *sdata)
1517 struct ieee80211_pspoll *pspoll;
1518 struct sk_buff *skb;
1520 skb = ieee80211_pspoll_get(&local->hw, &sdata->vif);
1524 pspoll = (struct ieee80211_pspoll *) skb->data;
1525 pspoll->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
1527 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
1528 ieee80211_tx_skb(sdata, skb);
1531 void ieee80211_send_nullfunc(struct ieee80211_local *local,
1532 struct ieee80211_sub_if_data *sdata,
1535 struct sk_buff *skb;
1536 struct ieee80211_hdr_3addr *nullfunc;
1537 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1539 skb = ieee80211_nullfunc_get(&local->hw, &sdata->vif,
1540 !ieee80211_hw_check(&local->hw, DOESNT_SUPPORT_QOS_NDP));
1544 nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
1546 nullfunc->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
1548 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
1549 IEEE80211_TX_INTFL_OFFCHAN_TX_OK;
1551 if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
1552 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
1554 if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL)
1555 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_USE_MINRATE;
1557 ieee80211_tx_skb(sdata, skb);
1560 void ieee80211_send_4addr_nullfunc(struct ieee80211_local *local,
1561 struct ieee80211_sub_if_data *sdata)
1563 struct sk_buff *skb;
1564 struct ieee80211_hdr *nullfunc;
1567 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
1570 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 30);
1574 skb_reserve(skb, local->hw.extra_tx_headroom);
1576 nullfunc = skb_put_zero(skb, 30);
1577 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
1578 IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
1579 nullfunc->frame_control = fc;
1580 memcpy(nullfunc->addr1, sdata->deflink.u.mgd.bssid, ETH_ALEN);
1581 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1582 memcpy(nullfunc->addr3, sdata->deflink.u.mgd.bssid, ETH_ALEN);
1583 memcpy(nullfunc->addr4, sdata->vif.addr, ETH_ALEN);
1585 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
1586 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_USE_MINRATE;
1587 ieee80211_tx_skb(sdata, skb);
1590 /* spectrum management related things */
1591 static void ieee80211_chswitch_work(struct work_struct *work)
1593 struct ieee80211_link_data *link =
1594 container_of(work, struct ieee80211_link_data, u.mgd.chswitch_work);
1595 struct ieee80211_sub_if_data *sdata = link->sdata;
1596 struct ieee80211_local *local = sdata->local;
1597 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1600 if (!ieee80211_sdata_running(sdata))
1604 mutex_lock(&local->mtx);
1605 mutex_lock(&local->chanctx_mtx);
1607 if (!ifmgd->associated)
1610 if (!link->conf->csa_active)
1614 * using reservation isn't immediate as it may be deferred until later
1615 * with multi-vif. once reservation is complete it will re-schedule the
1616 * work with no reserved_chanctx so verify chandef to check if it
1617 * completed successfully
1620 if (link->reserved_chanctx) {
1622 * with multi-vif csa driver may call ieee80211_csa_finish()
1623 * many times while waiting for other interfaces to use their
1626 if (link->reserved_ready)
1629 ret = ieee80211_link_use_reserved_context(link);
1632 "failed to use reserved channel context, disconnecting (err=%d)\n",
1634 ieee80211_queue_work(&sdata->local->hw,
1635 &ifmgd->csa_connection_drop_work);
1642 if (!cfg80211_chandef_identical(&link->conf->chandef,
1643 &link->csa_chandef)) {
1645 "failed to finalize channel switch, disconnecting\n");
1646 ieee80211_queue_work(&sdata->local->hw,
1647 &ifmgd->csa_connection_drop_work);
1651 link->u.mgd.csa_waiting_bcn = true;
1653 ieee80211_sta_reset_beacon_monitor(sdata);
1654 ieee80211_sta_reset_conn_monitor(sdata);
1657 mutex_unlock(&local->chanctx_mtx);
1658 mutex_unlock(&local->mtx);
1659 sdata_unlock(sdata);
1662 static void ieee80211_chswitch_post_beacon(struct ieee80211_link_data *link)
1664 struct ieee80211_sub_if_data *sdata = link->sdata;
1665 struct ieee80211_local *local = sdata->local;
1666 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1669 sdata_assert_lock(sdata);
1671 WARN_ON(!link->conf->csa_active);
1673 if (link->csa_block_tx) {
1674 ieee80211_wake_vif_queues(local, sdata,
1675 IEEE80211_QUEUE_STOP_REASON_CSA);
1676 link->csa_block_tx = false;
1679 link->conf->csa_active = false;
1680 link->u.mgd.csa_waiting_bcn = false;
1682 * If the CSA IE is still present on the beacon after the switch,
1683 * we need to consider it as a new CSA (possibly to self).
1685 link->u.mgd.beacon_crc_valid = false;
1687 ret = drv_post_channel_switch(sdata);
1690 "driver post channel switch failed, disconnecting\n");
1691 ieee80211_queue_work(&local->hw,
1692 &ifmgd->csa_connection_drop_work);
1696 cfg80211_ch_switch_notify(sdata->dev, &link->reserved_chandef, 0);
1699 void ieee80211_chswitch_done(struct ieee80211_vif *vif, bool success)
1701 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1702 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1704 if (WARN_ON(sdata->vif.valid_links))
1707 trace_api_chswitch_done(sdata, success);
1710 "driver channel switch failed, disconnecting\n");
1711 ieee80211_queue_work(&sdata->local->hw,
1712 &ifmgd->csa_connection_drop_work);
1714 ieee80211_queue_work(&sdata->local->hw,
1715 &sdata->deflink.u.mgd.chswitch_work);
1718 EXPORT_SYMBOL(ieee80211_chswitch_done);
1720 static void ieee80211_chswitch_timer(struct timer_list *t)
1722 struct ieee80211_link_data *link =
1723 from_timer(link, t, u.mgd.chswitch_timer);
1725 ieee80211_queue_work(&link->sdata->local->hw,
1726 &link->u.mgd.chswitch_work);
1730 ieee80211_sta_abort_chanswitch(struct ieee80211_link_data *link)
1732 struct ieee80211_sub_if_data *sdata = link->sdata;
1733 struct ieee80211_local *local = sdata->local;
1735 if (!local->ops->abort_channel_switch)
1738 mutex_lock(&local->mtx);
1740 mutex_lock(&local->chanctx_mtx);
1741 ieee80211_link_unreserve_chanctx(link);
1742 mutex_unlock(&local->chanctx_mtx);
1744 if (link->csa_block_tx)
1745 ieee80211_wake_vif_queues(local, sdata,
1746 IEEE80211_QUEUE_STOP_REASON_CSA);
1748 link->csa_block_tx = false;
1749 link->conf->csa_active = false;
1751 mutex_unlock(&local->mtx);
1753 drv_abort_channel_switch(sdata);
1757 ieee80211_sta_process_chanswitch(struct ieee80211_link_data *link,
1758 u64 timestamp, u32 device_timestamp,
1759 struct ieee802_11_elems *elems,
1762 struct ieee80211_sub_if_data *sdata = link->sdata;
1763 struct ieee80211_local *local = sdata->local;
1764 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1765 struct cfg80211_bss *cbss = link->u.mgd.bss;
1766 struct ieee80211_chanctx_conf *conf;
1767 struct ieee80211_chanctx *chanctx;
1768 enum nl80211_band current_band;
1769 struct ieee80211_csa_ie csa_ie;
1770 struct ieee80211_channel_switch ch_switch;
1771 struct ieee80211_bss *bss;
1774 sdata_assert_lock(sdata);
1779 if (local->scanning)
1782 current_band = cbss->channel->band;
1783 bss = (void *)cbss->priv;
1784 res = ieee80211_parse_ch_switch_ie(sdata, elems, current_band,
1786 link->u.mgd.conn_flags,
1787 link->u.mgd.bssid, &csa_ie);
1790 ch_switch.timestamp = timestamp;
1791 ch_switch.device_timestamp = device_timestamp;
1792 ch_switch.block_tx = csa_ie.mode;
1793 ch_switch.chandef = csa_ie.chandef;
1794 ch_switch.count = csa_ie.count;
1795 ch_switch.delay = csa_ie.max_switch_time;
1799 goto lock_and_drop_connection;
1801 if (beacon && link->conf->csa_active &&
1802 !link->u.mgd.csa_waiting_bcn) {
1804 ieee80211_sta_abort_chanswitch(link);
1806 drv_channel_switch_rx_beacon(sdata, &ch_switch);
1808 } else if (link->conf->csa_active || res) {
1809 /* disregard subsequent announcements if already processing */
1813 if (link->conf->chandef.chan->band !=
1814 csa_ie.chandef.chan->band) {
1816 "AP %pM switches to different band (%d MHz, width:%d, CF1/2: %d/%d MHz), disconnecting\n",
1818 csa_ie.chandef.chan->center_freq,
1819 csa_ie.chandef.width, csa_ie.chandef.center_freq1,
1820 csa_ie.chandef.center_freq2);
1821 goto lock_and_drop_connection;
1824 if (!cfg80211_chandef_usable(local->hw.wiphy, &csa_ie.chandef,
1825 IEEE80211_CHAN_DISABLED)) {
1827 "AP %pM switches to unsupported channel "
1828 "(%d.%03d MHz, width:%d, CF1/2: %d.%03d/%d MHz), "
1831 csa_ie.chandef.chan->center_freq,
1832 csa_ie.chandef.chan->freq_offset,
1833 csa_ie.chandef.width, csa_ie.chandef.center_freq1,
1834 csa_ie.chandef.freq1_offset,
1835 csa_ie.chandef.center_freq2);
1836 goto lock_and_drop_connection;
1839 if (cfg80211_chandef_identical(&csa_ie.chandef,
1840 &link->conf->chandef) &&
1841 (!csa_ie.mode || !beacon)) {
1842 if (link->u.mgd.csa_ignored_same_chan)
1845 "AP %pM tries to chanswitch to same channel, ignore\n",
1847 link->u.mgd.csa_ignored_same_chan = true;
1852 * Drop all TDLS peers - either we disconnect or move to a different
1853 * channel from this point on. There's no telling what our peer will do.
1854 * The TDLS WIDER_BW scenario is also problematic, as peers might now
1855 * have an incompatible wider chandef.
1857 ieee80211_teardown_tdls_peers(sdata);
1859 mutex_lock(&local->mtx);
1860 mutex_lock(&local->chanctx_mtx);
1861 conf = rcu_dereference_protected(link->conf->chanctx_conf,
1862 lockdep_is_held(&local->chanctx_mtx));
1865 "no channel context assigned to vif?, disconnecting\n");
1866 goto drop_connection;
1869 chanctx = container_of(conf, struct ieee80211_chanctx, conf);
1871 if (local->use_chanctx &&
1872 !ieee80211_hw_check(&local->hw, CHANCTX_STA_CSA)) {
1874 "driver doesn't support chan-switch with channel contexts\n");
1875 goto drop_connection;
1878 if (drv_pre_channel_switch(sdata, &ch_switch)) {
1880 "preparing for channel switch failed, disconnecting\n");
1881 goto drop_connection;
1884 res = ieee80211_link_reserve_chanctx(link, &csa_ie.chandef,
1885 chanctx->mode, false);
1888 "failed to reserve channel context for channel switch, disconnecting (err=%d)\n",
1890 goto drop_connection;
1892 mutex_unlock(&local->chanctx_mtx);
1894 link->conf->csa_active = true;
1895 link->csa_chandef = csa_ie.chandef;
1896 link->csa_block_tx = csa_ie.mode;
1897 link->u.mgd.csa_ignored_same_chan = false;
1898 link->u.mgd.beacon_crc_valid = false;
1900 if (link->csa_block_tx)
1901 ieee80211_stop_vif_queues(local, sdata,
1902 IEEE80211_QUEUE_STOP_REASON_CSA);
1903 mutex_unlock(&local->mtx);
1905 cfg80211_ch_switch_started_notify(sdata->dev, &csa_ie.chandef,
1906 csa_ie.count, csa_ie.mode);
1908 if (local->ops->channel_switch) {
1909 /* use driver's channel switch callback */
1910 drv_channel_switch(local, sdata, &ch_switch);
1914 /* channel switch handled in software */
1915 if (csa_ie.count <= 1)
1916 ieee80211_queue_work(&local->hw, &link->u.mgd.chswitch_work);
1918 mod_timer(&link->u.mgd.chswitch_timer,
1919 TU_TO_EXP_TIME((csa_ie.count - 1) *
1920 cbss->beacon_interval));
1922 lock_and_drop_connection:
1923 mutex_lock(&local->mtx);
1924 mutex_lock(&local->chanctx_mtx);
1927 * This is just so that the disconnect flow will know that
1928 * we were trying to switch channel and failed. In case the
1929 * mode is 1 (we are not allowed to Tx), we will know not to
1930 * send a deauthentication frame. Those two fields will be
1931 * reset when the disconnection worker runs.
1933 link->conf->csa_active = true;
1934 link->csa_block_tx = csa_ie.mode;
1936 ieee80211_queue_work(&local->hw, &ifmgd->csa_connection_drop_work);
1937 mutex_unlock(&local->chanctx_mtx);
1938 mutex_unlock(&local->mtx);
1942 ieee80211_find_80211h_pwr_constr(struct ieee80211_sub_if_data *sdata,
1943 struct ieee80211_channel *channel,
1944 const u8 *country_ie, u8 country_ie_len,
1945 const u8 *pwr_constr_elem,
1946 int *chan_pwr, int *pwr_reduction)
1948 struct ieee80211_country_ie_triplet *triplet;
1949 int chan = ieee80211_frequency_to_channel(channel->center_freq);
1950 int i, chan_increment;
1951 bool have_chan_pwr = false;
1954 if (country_ie_len % 2 || country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
1957 triplet = (void *)(country_ie + 3);
1958 country_ie_len -= 3;
1960 switch (channel->band) {
1964 case NL80211_BAND_2GHZ:
1965 case NL80211_BAND_60GHZ:
1966 case NL80211_BAND_LC:
1969 case NL80211_BAND_5GHZ:
1972 case NL80211_BAND_6GHZ:
1974 * In the 6 GHz band, the "maximum transmit power level"
1975 * field in the triplets is reserved, and thus will be
1976 * zero and we shouldn't use it to control TX power.
1977 * The actual TX power will be given in the transmit
1978 * power envelope element instead.
1984 while (country_ie_len >= 3) {
1985 u8 first_channel = triplet->chans.first_channel;
1987 if (first_channel >= IEEE80211_COUNTRY_EXTENSION_ID)
1990 for (i = 0; i < triplet->chans.num_channels; i++) {
1991 if (first_channel + i * chan_increment == chan) {
1992 have_chan_pwr = true;
1993 *chan_pwr = triplet->chans.max_power;
2002 country_ie_len -= 3;
2005 if (have_chan_pwr && pwr_constr_elem)
2006 *pwr_reduction = *pwr_constr_elem;
2010 return have_chan_pwr;
2013 static void ieee80211_find_cisco_dtpc(struct ieee80211_sub_if_data *sdata,
2014 struct ieee80211_channel *channel,
2015 const u8 *cisco_dtpc_ie,
2018 /* From practical testing, the first data byte of the DTPC element
2019 * seems to contain the requested dBm level, and the CLI on Cisco
2020 * APs clearly state the range is -127 to 127 dBm, which indicates
2021 * a signed byte, although it seemingly never actually goes negative.
2022 * The other byte seems to always be zero.
2024 *pwr_level = (__s8)cisco_dtpc_ie[4];
2027 static u32 ieee80211_handle_pwr_constr(struct ieee80211_link_data *link,
2028 struct ieee80211_channel *channel,
2029 struct ieee80211_mgmt *mgmt,
2030 const u8 *country_ie, u8 country_ie_len,
2031 const u8 *pwr_constr_ie,
2032 const u8 *cisco_dtpc_ie)
2034 struct ieee80211_sub_if_data *sdata = link->sdata;
2035 bool has_80211h_pwr = false, has_cisco_pwr = false;
2036 int chan_pwr = 0, pwr_reduction_80211h = 0;
2037 int pwr_level_cisco, pwr_level_80211h;
2039 __le16 capab = mgmt->u.probe_resp.capab_info;
2041 if (ieee80211_is_s1g_beacon(mgmt->frame_control))
2042 return 0; /* TODO */
2045 (capab & cpu_to_le16(WLAN_CAPABILITY_SPECTRUM_MGMT) ||
2046 capab & cpu_to_le16(WLAN_CAPABILITY_RADIO_MEASURE))) {
2047 has_80211h_pwr = ieee80211_find_80211h_pwr_constr(
2048 sdata, channel, country_ie, country_ie_len,
2049 pwr_constr_ie, &chan_pwr, &pwr_reduction_80211h);
2051 max_t(int, 0, chan_pwr - pwr_reduction_80211h);
2054 if (cisco_dtpc_ie) {
2055 ieee80211_find_cisco_dtpc(
2056 sdata, channel, cisco_dtpc_ie, &pwr_level_cisco);
2057 has_cisco_pwr = true;
2060 if (!has_80211h_pwr && !has_cisco_pwr)
2063 /* If we have both 802.11h and Cisco DTPC, apply both limits
2064 * by picking the smallest of the two power levels advertised.
2066 if (has_80211h_pwr &&
2067 (!has_cisco_pwr || pwr_level_80211h <= pwr_level_cisco)) {
2068 new_ap_level = pwr_level_80211h;
2070 if (link->ap_power_level == new_ap_level)
2074 "Limiting TX power to %d (%d - %d) dBm as advertised by %pM\n",
2075 pwr_level_80211h, chan_pwr, pwr_reduction_80211h,
2077 } else { /* has_cisco_pwr is always true here. */
2078 new_ap_level = pwr_level_cisco;
2080 if (link->ap_power_level == new_ap_level)
2084 "Limiting TX power to %d dBm as advertised by %pM\n",
2085 pwr_level_cisco, link->u.mgd.bssid);
2088 link->ap_power_level = new_ap_level;
2089 if (__ieee80211_recalc_txpower(sdata))
2090 return BSS_CHANGED_TXPOWER;
2095 static void ieee80211_enable_ps(struct ieee80211_local *local,
2096 struct ieee80211_sub_if_data *sdata)
2098 struct ieee80211_conf *conf = &local->hw.conf;
2101 * If we are scanning right now then the parameters will
2102 * take effect when scan finishes.
2104 if (local->scanning)
2107 if (conf->dynamic_ps_timeout > 0 &&
2108 !ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS)) {
2109 mod_timer(&local->dynamic_ps_timer, jiffies +
2110 msecs_to_jiffies(conf->dynamic_ps_timeout));
2112 if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK))
2113 ieee80211_send_nullfunc(local, sdata, true);
2115 if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK) &&
2116 ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
2119 conf->flags |= IEEE80211_CONF_PS;
2120 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
2124 static void ieee80211_change_ps(struct ieee80211_local *local)
2126 struct ieee80211_conf *conf = &local->hw.conf;
2128 if (local->ps_sdata) {
2129 ieee80211_enable_ps(local, local->ps_sdata);
2130 } else if (conf->flags & IEEE80211_CONF_PS) {
2131 conf->flags &= ~IEEE80211_CONF_PS;
2132 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
2133 del_timer_sync(&local->dynamic_ps_timer);
2134 cancel_work_sync(&local->dynamic_ps_enable_work);
2138 static bool ieee80211_powersave_allowed(struct ieee80211_sub_if_data *sdata)
2140 struct ieee80211_local *local = sdata->local;
2141 struct ieee80211_if_managed *mgd = &sdata->u.mgd;
2142 struct sta_info *sta = NULL;
2143 bool authorized = false;
2145 if (!mgd->powersave)
2151 if (!mgd->associated)
2154 if (mgd->flags & IEEE80211_STA_CONNECTION_POLL)
2157 if (!(local->hw.wiphy->flags & WIPHY_FLAG_SUPPORTS_MLO) &&
2158 !sdata->deflink.u.mgd.have_beacon)
2162 sta = sta_info_get(sdata, sdata->vif.cfg.ap_addr);
2164 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
2170 /* need to hold RTNL or interface lock */
2171 void ieee80211_recalc_ps(struct ieee80211_local *local)
2173 struct ieee80211_sub_if_data *sdata, *found = NULL;
2177 if (!ieee80211_hw_check(&local->hw, SUPPORTS_PS) ||
2178 ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS)) {
2179 local->ps_sdata = NULL;
2183 list_for_each_entry(sdata, &local->interfaces, list) {
2184 if (!ieee80211_sdata_running(sdata))
2186 if (sdata->vif.type == NL80211_IFTYPE_AP) {
2187 /* If an AP vif is found, then disable PS
2188 * by setting the count to zero thereby setting
2194 if (sdata->vif.type != NL80211_IFTYPE_STATION)
2200 if (count == 1 && ieee80211_powersave_allowed(found)) {
2201 u8 dtimper = found->deflink.u.mgd.dtim_period;
2203 timeout = local->dynamic_ps_forced_timeout;
2206 local->hw.conf.dynamic_ps_timeout = timeout;
2208 /* If the TIM IE is invalid, pretend the value is 1 */
2212 local->hw.conf.ps_dtim_period = dtimper;
2213 local->ps_sdata = found;
2215 local->ps_sdata = NULL;
2218 ieee80211_change_ps(local);
2221 void ieee80211_recalc_ps_vif(struct ieee80211_sub_if_data *sdata)
2223 bool ps_allowed = ieee80211_powersave_allowed(sdata);
2225 if (sdata->vif.cfg.ps != ps_allowed) {
2226 sdata->vif.cfg.ps = ps_allowed;
2227 ieee80211_vif_cfg_change_notify(sdata, BSS_CHANGED_PS);
2231 void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
2233 struct ieee80211_local *local =
2234 container_of(work, struct ieee80211_local,
2235 dynamic_ps_disable_work);
2237 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
2238 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
2239 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
2242 ieee80211_wake_queues_by_reason(&local->hw,
2243 IEEE80211_MAX_QUEUE_MAP,
2244 IEEE80211_QUEUE_STOP_REASON_PS,
2248 void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
2250 struct ieee80211_local *local =
2251 container_of(work, struct ieee80211_local,
2252 dynamic_ps_enable_work);
2253 struct ieee80211_sub_if_data *sdata = local->ps_sdata;
2254 struct ieee80211_if_managed *ifmgd;
2255 unsigned long flags;
2258 /* can only happen when PS was just disabled anyway */
2262 ifmgd = &sdata->u.mgd;
2264 if (local->hw.conf.flags & IEEE80211_CONF_PS)
2267 if (local->hw.conf.dynamic_ps_timeout > 0) {
2268 /* don't enter PS if TX frames are pending */
2269 if (drv_tx_frames_pending(local)) {
2270 mod_timer(&local->dynamic_ps_timer, jiffies +
2272 local->hw.conf.dynamic_ps_timeout));
2277 * transmission can be stopped by others which leads to
2278 * dynamic_ps_timer expiry. Postpone the ps timer if it
2279 * is not the actual idle state.
2281 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
2282 for (q = 0; q < local->hw.queues; q++) {
2283 if (local->queue_stop_reasons[q]) {
2284 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
2286 mod_timer(&local->dynamic_ps_timer, jiffies +
2288 local->hw.conf.dynamic_ps_timeout));
2292 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
2295 if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK) &&
2296 !(ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
2297 if (drv_tx_frames_pending(local)) {
2298 mod_timer(&local->dynamic_ps_timer, jiffies +
2300 local->hw.conf.dynamic_ps_timeout));
2302 ieee80211_send_nullfunc(local, sdata, true);
2303 /* Flush to get the tx status of nullfunc frame */
2304 ieee80211_flush_queues(local, sdata, false);
2308 if (!(ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS) &&
2309 ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK)) ||
2310 (ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
2311 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
2312 local->hw.conf.flags |= IEEE80211_CONF_PS;
2313 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
2317 void ieee80211_dynamic_ps_timer(struct timer_list *t)
2319 struct ieee80211_local *local = from_timer(local, t, dynamic_ps_timer);
2321 ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
2324 void ieee80211_dfs_cac_timer_work(struct work_struct *work)
2326 struct delayed_work *delayed_work = to_delayed_work(work);
2327 struct ieee80211_link_data *link =
2328 container_of(delayed_work, struct ieee80211_link_data,
2329 dfs_cac_timer_work);
2330 struct cfg80211_chan_def chandef = link->conf->chandef;
2331 struct ieee80211_sub_if_data *sdata = link->sdata;
2333 mutex_lock(&sdata->local->mtx);
2334 if (sdata->wdev.cac_started) {
2335 ieee80211_link_release_channel(link);
2336 cfg80211_cac_event(sdata->dev, &chandef,
2337 NL80211_RADAR_CAC_FINISHED,
2340 mutex_unlock(&sdata->local->mtx);
2344 __ieee80211_sta_handle_tspec_ac_params(struct ieee80211_sub_if_data *sdata)
2346 struct ieee80211_local *local = sdata->local;
2347 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2351 if (local->hw.queues < IEEE80211_NUM_ACS)
2354 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
2355 struct ieee80211_sta_tx_tspec *tx_tspec = &ifmgd->tx_tspec[ac];
2357 unsigned long now = jiffies;
2359 if (tx_tspec->action == TX_TSPEC_ACTION_NONE &&
2360 tx_tspec->admitted_time &&
2361 time_after(now, tx_tspec->time_slice_start + HZ)) {
2362 tx_tspec->consumed_tx_time = 0;
2363 tx_tspec->time_slice_start = now;
2365 if (tx_tspec->downgraded)
2367 TX_TSPEC_ACTION_STOP_DOWNGRADE;
2370 switch (tx_tspec->action) {
2371 case TX_TSPEC_ACTION_STOP_DOWNGRADE:
2372 /* take the original parameters */
2373 if (drv_conf_tx(local, &sdata->deflink, ac,
2374 &sdata->deflink.tx_conf[ac]))
2375 link_err(&sdata->deflink,
2376 "failed to set TX queue parameters for queue %d\n",
2378 tx_tspec->action = TX_TSPEC_ACTION_NONE;
2379 tx_tspec->downgraded = false;
2382 case TX_TSPEC_ACTION_DOWNGRADE:
2383 if (time_after(now, tx_tspec->time_slice_start + HZ)) {
2384 tx_tspec->action = TX_TSPEC_ACTION_NONE;
2388 /* downgrade next lower non-ACM AC */
2389 for (non_acm_ac = ac + 1;
2390 non_acm_ac < IEEE80211_NUM_ACS;
2392 if (!(sdata->wmm_acm & BIT(7 - 2 * non_acm_ac)))
2394 /* Usually the loop will result in using BK even if it
2395 * requires admission control, but such a configuration
2396 * makes no sense and we have to transmit somehow - the
2397 * AC selection does the same thing.
2398 * If we started out trying to downgrade from BK, then
2399 * the extra condition here might be needed.
2401 if (non_acm_ac >= IEEE80211_NUM_ACS)
2402 non_acm_ac = IEEE80211_AC_BK;
2403 if (drv_conf_tx(local, &sdata->deflink, ac,
2404 &sdata->deflink.tx_conf[non_acm_ac]))
2405 link_err(&sdata->deflink,
2406 "failed to set TX queue parameters for queue %d\n",
2408 tx_tspec->action = TX_TSPEC_ACTION_NONE;
2410 schedule_delayed_work(&ifmgd->tx_tspec_wk,
2411 tx_tspec->time_slice_start + HZ - now + 1);
2413 case TX_TSPEC_ACTION_NONE:
2422 void ieee80211_sta_handle_tspec_ac_params(struct ieee80211_sub_if_data *sdata)
2424 if (__ieee80211_sta_handle_tspec_ac_params(sdata))
2425 ieee80211_link_info_change_notify(sdata, &sdata->deflink,
2429 static void ieee80211_sta_handle_tspec_ac_params_wk(struct work_struct *work)
2431 struct ieee80211_sub_if_data *sdata;
2433 sdata = container_of(work, struct ieee80211_sub_if_data,
2434 u.mgd.tx_tspec_wk.work);
2435 ieee80211_sta_handle_tspec_ac_params(sdata);
2440 ieee80211_sta_wmm_params(struct ieee80211_local *local,
2441 struct ieee80211_link_data *link,
2442 const u8 *wmm_param, size_t wmm_param_len,
2443 const struct ieee80211_mu_edca_param_set *mu_edca)
2445 struct ieee80211_sub_if_data *sdata = link->sdata;
2446 struct ieee80211_tx_queue_params params[IEEE80211_NUM_ACS];
2447 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2449 int count, mu_edca_count, ac;
2451 u8 uapsd_queues = 0;
2453 if (!local->ops->conf_tx)
2456 if (local->hw.queues < IEEE80211_NUM_ACS)
2462 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
2465 if (ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED)
2466 uapsd_queues = ifmgd->uapsd_queues;
2468 count = wmm_param[6] & 0x0f;
2469 /* -1 is the initial value of ifmgd->mu_edca_last_param_set.
2470 * if mu_edca was preset before and now it disappeared tell
2471 * the driver about it.
2473 mu_edca_count = mu_edca ? mu_edca->mu_qos_info & 0x0f : -1;
2474 if (count == link->u.mgd.wmm_last_param_set &&
2475 mu_edca_count == link->u.mgd.mu_edca_last_param_set)
2477 link->u.mgd.wmm_last_param_set = count;
2478 link->u.mgd.mu_edca_last_param_set = mu_edca_count;
2480 pos = wmm_param + 8;
2481 left = wmm_param_len - 8;
2483 memset(¶ms, 0, sizeof(params));
2486 for (; left >= 4; left -= 4, pos += 4) {
2487 int aci = (pos[0] >> 5) & 0x03;
2488 int acm = (pos[0] >> 4) & 0x01;
2493 ac = IEEE80211_AC_BK;
2495 sdata->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
2496 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
2498 params[ac].mu_edca = !!mu_edca;
2500 params[ac].mu_edca_param_rec = mu_edca->ac_bk;
2503 ac = IEEE80211_AC_VI;
2505 sdata->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
2506 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
2508 params[ac].mu_edca = !!mu_edca;
2510 params[ac].mu_edca_param_rec = mu_edca->ac_vi;
2513 ac = IEEE80211_AC_VO;
2515 sdata->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
2516 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
2518 params[ac].mu_edca = !!mu_edca;
2520 params[ac].mu_edca_param_rec = mu_edca->ac_vo;
2524 ac = IEEE80211_AC_BE;
2526 sdata->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
2527 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
2529 params[ac].mu_edca = !!mu_edca;
2531 params[ac].mu_edca_param_rec = mu_edca->ac_be;
2535 params[ac].aifs = pos[0] & 0x0f;
2537 if (params[ac].aifs < 2) {
2539 "AP has invalid WMM params (AIFSN=%d for ACI %d), will use 2\n",
2540 params[ac].aifs, aci);
2541 params[ac].aifs = 2;
2543 params[ac].cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
2544 params[ac].cw_min = ecw2cw(pos[1] & 0x0f);
2545 params[ac].txop = get_unaligned_le16(pos + 2);
2546 params[ac].acm = acm;
2547 params[ac].uapsd = uapsd;
2549 if (params[ac].cw_min == 0 ||
2550 params[ac].cw_min > params[ac].cw_max) {
2552 "AP has invalid WMM params (CWmin/max=%d/%d for ACI %d), using defaults\n",
2553 params[ac].cw_min, params[ac].cw_max, aci);
2556 ieee80211_regulatory_limit_wmm_params(sdata, ¶ms[ac], ac);
2559 /* WMM specification requires all 4 ACIs. */
2560 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
2561 if (params[ac].cw_min == 0) {
2563 "AP has invalid WMM params (missing AC %d), using defaults\n",
2569 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
2571 "WMM AC=%d acm=%d aifs=%d cWmin=%d cWmax=%d txop=%d uapsd=%d, downgraded=%d\n",
2573 params[ac].aifs, params[ac].cw_min, params[ac].cw_max,
2574 params[ac].txop, params[ac].uapsd,
2575 ifmgd->tx_tspec[ac].downgraded);
2576 link->tx_conf[ac] = params[ac];
2577 if (!ifmgd->tx_tspec[ac].downgraded &&
2578 drv_conf_tx(local, link, ac, ¶ms[ac]))
2580 "failed to set TX queue parameters for AC %d\n",
2584 /* enable WMM or activate new settings */
2585 link->conf->qos = true;
2589 static void __ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
2591 lockdep_assert_held(&sdata->local->mtx);
2593 sdata->u.mgd.flags &= ~IEEE80211_STA_CONNECTION_POLL;
2594 ieee80211_run_deferred_scan(sdata->local);
2597 static void ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
2599 mutex_lock(&sdata->local->mtx);
2600 __ieee80211_stop_poll(sdata);
2601 mutex_unlock(&sdata->local->mtx);
2604 static u32 ieee80211_handle_bss_capability(struct ieee80211_link_data *link,
2605 u16 capab, bool erp_valid, u8 erp)
2607 struct ieee80211_bss_conf *bss_conf = link->conf;
2608 struct ieee80211_supported_band *sband;
2610 bool use_protection;
2611 bool use_short_preamble;
2612 bool use_short_slot;
2614 sband = ieee80211_get_link_sband(link);
2619 use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
2620 use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
2622 use_protection = false;
2623 use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
2626 use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
2627 if (sband->band == NL80211_BAND_5GHZ ||
2628 sband->band == NL80211_BAND_6GHZ)
2629 use_short_slot = true;
2631 if (use_protection != bss_conf->use_cts_prot) {
2632 bss_conf->use_cts_prot = use_protection;
2633 changed |= BSS_CHANGED_ERP_CTS_PROT;
2636 if (use_short_preamble != bss_conf->use_short_preamble) {
2637 bss_conf->use_short_preamble = use_short_preamble;
2638 changed |= BSS_CHANGED_ERP_PREAMBLE;
2641 if (use_short_slot != bss_conf->use_short_slot) {
2642 bss_conf->use_short_slot = use_short_slot;
2643 changed |= BSS_CHANGED_ERP_SLOT;
2649 static u32 ieee80211_link_set_associated(struct ieee80211_link_data *link,
2650 struct cfg80211_bss *cbss)
2652 struct ieee80211_sub_if_data *sdata = link->sdata;
2653 struct ieee80211_bss_conf *bss_conf = link->conf;
2654 struct ieee80211_bss *bss = (void *)cbss->priv;
2655 u32 changed = BSS_CHANGED_QOS;
2657 /* not really used in MLO */
2658 sdata->u.mgd.beacon_timeout =
2659 usecs_to_jiffies(ieee80211_tu_to_usec(beacon_loss_count *
2660 bss_conf->beacon_int));
2662 changed |= ieee80211_handle_bss_capability(link,
2663 bss_conf->assoc_capability,
2667 ieee80211_check_rate_mask(link);
2669 link->u.mgd.bss = cbss;
2670 memcpy(link->u.mgd.bssid, cbss->bssid, ETH_ALEN);
2672 if (sdata->vif.p2p ||
2673 sdata->vif.driver_flags & IEEE80211_VIF_GET_NOA_UPDATE) {
2674 const struct cfg80211_bss_ies *ies;
2677 ies = rcu_dereference(cbss->ies);
2681 ret = cfg80211_get_p2p_attr(
2682 ies->data, ies->len,
2683 IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
2684 (u8 *) &bss_conf->p2p_noa_attr,
2685 sizeof(bss_conf->p2p_noa_attr));
2687 link->u.mgd.p2p_noa_index =
2688 bss_conf->p2p_noa_attr.index;
2689 changed |= BSS_CHANGED_P2P_PS;
2695 if (link->u.mgd.have_beacon) {
2697 * If the AP is buggy we may get here with no DTIM period
2698 * known, so assume it's 1 which is the only safe assumption
2699 * in that case, although if the TIM IE is broken powersave
2700 * probably just won't work at all.
2702 bss_conf->dtim_period = link->u.mgd.dtim_period ?: 1;
2703 bss_conf->beacon_rate = bss->beacon_rate;
2704 changed |= BSS_CHANGED_BEACON_INFO;
2706 bss_conf->beacon_rate = NULL;
2707 bss_conf->dtim_period = 0;
2710 /* Tell the driver to monitor connection quality (if supported) */
2711 if (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI &&
2712 bss_conf->cqm_rssi_thold)
2713 changed |= BSS_CHANGED_CQM;
2718 static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
2719 struct ieee80211_mgd_assoc_data *assoc_data,
2720 u64 changed[IEEE80211_MLD_MAX_NUM_LINKS])
2722 struct ieee80211_local *local = sdata->local;
2723 struct ieee80211_vif_cfg *vif_cfg = &sdata->vif.cfg;
2724 u64 vif_changed = BSS_CHANGED_ASSOC;
2725 unsigned int link_id;
2727 sdata->u.mgd.associated = true;
2729 for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {
2730 struct cfg80211_bss *cbss = assoc_data->link[link_id].bss;
2731 struct ieee80211_link_data *link;
2736 link = sdata_dereference(sdata->link[link_id], sdata);
2740 changed[link_id] |= ieee80211_link_set_associated(link, cbss);
2743 /* just to be sure */
2744 ieee80211_stop_poll(sdata);
2746 ieee80211_led_assoc(local, 1);
2750 /* Enable ARP filtering */
2751 if (vif_cfg->arp_addr_cnt)
2752 vif_changed |= BSS_CHANGED_ARP_FILTER;
2754 if (sdata->vif.valid_links) {
2756 link_id < IEEE80211_MLD_MAX_NUM_LINKS;
2758 struct ieee80211_link_data *link;
2759 struct cfg80211_bss *cbss = assoc_data->link[link_id].bss;
2764 link = sdata_dereference(sdata->link[link_id], sdata);
2768 ieee80211_link_info_change_notify(sdata, link,
2771 ieee80211_recalc_smps(sdata, link);
2774 ieee80211_vif_cfg_change_notify(sdata, vif_changed);
2776 ieee80211_bss_info_change_notify(sdata,
2777 vif_changed | changed[0]);
2780 mutex_lock(&local->iflist_mtx);
2781 ieee80211_recalc_ps(local);
2782 mutex_unlock(&local->iflist_mtx);
2784 /* leave this here to not change ordering in non-MLO cases */
2785 if (!sdata->vif.valid_links)
2786 ieee80211_recalc_smps(sdata, &sdata->deflink);
2787 ieee80211_recalc_ps_vif(sdata);
2789 netif_carrier_on(sdata->dev);
2792 static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
2793 u16 stype, u16 reason, bool tx,
2796 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2797 struct ieee80211_local *local = sdata->local;
2798 unsigned int link_id;
2800 struct ieee80211_prep_tx_info info = {
2804 sdata_assert_lock(sdata);
2806 if (WARN_ON_ONCE(tx && !frame_buf))
2809 if (WARN_ON(!ifmgd->associated))
2812 ieee80211_stop_poll(sdata);
2814 ifmgd->associated = false;
2816 /* other links will be destroyed */
2817 sdata->deflink.u.mgd.bss = NULL;
2819 netif_carrier_off(sdata->dev);
2822 * if we want to get out of ps before disassoc (why?) we have
2823 * to do it before sending disassoc, as otherwise the null-packet
2826 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
2827 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
2828 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
2830 local->ps_sdata = NULL;
2832 /* disable per-vif ps */
2833 ieee80211_recalc_ps_vif(sdata);
2835 /* make sure ongoing transmission finishes */
2839 * drop any frame before deauth/disassoc, this can be data or
2840 * management frame. Since we are disconnecting, we should not
2841 * insist sending these frames which can take time and delay
2842 * the disconnection and possible the roaming.
2845 ieee80211_flush_queues(local, sdata, true);
2847 /* deauthenticate/disassociate now */
2848 if (tx || frame_buf) {
2850 * In multi channel scenarios guarantee that the virtual
2851 * interface is granted immediate airtime to transmit the
2852 * deauthentication frame by calling mgd_prepare_tx, if the
2853 * driver requested so.
2855 if (ieee80211_hw_check(&local->hw, DEAUTH_NEED_MGD_TX_PREP) &&
2856 !sdata->deflink.u.mgd.have_beacon) {
2857 drv_mgd_prepare_tx(sdata->local, sdata, &info);
2860 ieee80211_send_deauth_disassoc(sdata, sdata->vif.cfg.ap_addr,
2861 sdata->vif.cfg.ap_addr, stype,
2862 reason, tx, frame_buf);
2865 /* flush out frame - make sure the deauth was actually sent */
2867 ieee80211_flush_queues(local, sdata, false);
2869 drv_mgd_complete_tx(sdata->local, sdata, &info);
2871 /* clear AP addr only after building the needed mgmt frames */
2872 eth_zero_addr(sdata->deflink.u.mgd.bssid);
2873 eth_zero_addr(sdata->vif.cfg.ap_addr);
2875 sdata->vif.cfg.ssid_len = 0;
2877 /* remove AP and TDLS peers */
2878 sta_info_flush(sdata);
2880 /* finally reset all BSS / config parameters */
2881 if (!sdata->vif.valid_links)
2882 changed |= ieee80211_reset_erp_info(sdata);
2884 ieee80211_led_assoc(local, 0);
2885 changed |= BSS_CHANGED_ASSOC;
2886 sdata->vif.cfg.assoc = false;
2888 sdata->deflink.u.mgd.p2p_noa_index = -1;
2889 memset(&sdata->vif.bss_conf.p2p_noa_attr, 0,
2890 sizeof(sdata->vif.bss_conf.p2p_noa_attr));
2892 /* on the next assoc, re-program HT/VHT parameters */
2893 memset(&ifmgd->ht_capa, 0, sizeof(ifmgd->ht_capa));
2894 memset(&ifmgd->ht_capa_mask, 0, sizeof(ifmgd->ht_capa_mask));
2895 memset(&ifmgd->vht_capa, 0, sizeof(ifmgd->vht_capa));
2896 memset(&ifmgd->vht_capa_mask, 0, sizeof(ifmgd->vht_capa_mask));
2899 * reset MU-MIMO ownership and group data in default link,
2900 * if used, other links are destroyed
2902 memset(sdata->vif.bss_conf.mu_group.membership, 0,
2903 sizeof(sdata->vif.bss_conf.mu_group.membership));
2904 memset(sdata->vif.bss_conf.mu_group.position, 0,
2905 sizeof(sdata->vif.bss_conf.mu_group.position));
2906 if (!sdata->vif.valid_links)
2907 changed |= BSS_CHANGED_MU_GROUPS;
2908 sdata->vif.bss_conf.mu_mimo_owner = false;
2910 sdata->deflink.ap_power_level = IEEE80211_UNSET_POWER_LEVEL;
2912 del_timer_sync(&local->dynamic_ps_timer);
2913 cancel_work_sync(&local->dynamic_ps_enable_work);
2915 /* Disable ARP filtering */
2916 if (sdata->vif.cfg.arp_addr_cnt)
2917 changed |= BSS_CHANGED_ARP_FILTER;
2919 sdata->vif.bss_conf.qos = false;
2920 if (!sdata->vif.valid_links) {
2921 changed |= BSS_CHANGED_QOS;
2922 /* The BSSID (not really interesting) and HT changed */
2923 changed |= BSS_CHANGED_BSSID | BSS_CHANGED_HT;
2924 ieee80211_bss_info_change_notify(sdata, changed);
2926 ieee80211_vif_cfg_change_notify(sdata, changed);
2929 /* disassociated - set to defaults now */
2930 ieee80211_set_wmm_default(&sdata->deflink, false, false);
2932 del_timer_sync(&sdata->u.mgd.conn_mon_timer);
2933 del_timer_sync(&sdata->u.mgd.bcn_mon_timer);
2934 del_timer_sync(&sdata->u.mgd.timer);
2935 del_timer_sync(&sdata->deflink.u.mgd.chswitch_timer);
2937 sdata->vif.bss_conf.dtim_period = 0;
2938 sdata->vif.bss_conf.beacon_rate = NULL;
2940 sdata->deflink.u.mgd.have_beacon = false;
2941 sdata->deflink.u.mgd.tracking_signal_avg = false;
2942 sdata->deflink.u.mgd.disable_wmm_tracking = false;
2945 sdata->deflink.u.mgd.conn_flags = 0;
2946 mutex_lock(&local->mtx);
2948 for (link_id = 0; link_id < ARRAY_SIZE(sdata->link); link_id++) {
2949 struct ieee80211_link_data *link;
2951 link = sdata_dereference(sdata->link[link_id], sdata);
2954 ieee80211_link_release_channel(link);
2957 sdata->vif.bss_conf.csa_active = false;
2958 sdata->deflink.u.mgd.csa_waiting_bcn = false;
2959 sdata->deflink.u.mgd.csa_ignored_same_chan = false;
2960 if (sdata->deflink.csa_block_tx) {
2961 ieee80211_wake_vif_queues(local, sdata,
2962 IEEE80211_QUEUE_STOP_REASON_CSA);
2963 sdata->deflink.csa_block_tx = false;
2965 mutex_unlock(&local->mtx);
2967 /* existing TX TSPEC sessions no longer exist */
2968 memset(ifmgd->tx_tspec, 0, sizeof(ifmgd->tx_tspec));
2969 cancel_delayed_work_sync(&ifmgd->tx_tspec_wk);
2971 sdata->vif.bss_conf.pwr_reduction = 0;
2972 sdata->vif.bss_conf.tx_pwr_env_num = 0;
2973 memset(sdata->vif.bss_conf.tx_pwr_env, 0,
2974 sizeof(sdata->vif.bss_conf.tx_pwr_env));
2976 ieee80211_vif_set_links(sdata, 0);
2979 static void ieee80211_reset_ap_probe(struct ieee80211_sub_if_data *sdata)
2981 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2982 struct ieee80211_local *local = sdata->local;
2984 mutex_lock(&local->mtx);
2985 if (!(ifmgd->flags & IEEE80211_STA_CONNECTION_POLL))
2988 __ieee80211_stop_poll(sdata);
2990 mutex_lock(&local->iflist_mtx);
2991 ieee80211_recalc_ps(local);
2992 mutex_unlock(&local->iflist_mtx);
2994 if (ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
2998 * We've received a probe response, but are not sure whether
2999 * we have or will be receiving any beacons or data, so let's
3000 * schedule the timers again, just in case.
3002 ieee80211_sta_reset_beacon_monitor(sdata);
3004 mod_timer(&ifmgd->conn_mon_timer,
3005 round_jiffies_up(jiffies +
3006 IEEE80211_CONNECTION_IDLE_TIME));
3008 mutex_unlock(&local->mtx);
3011 static void ieee80211_sta_tx_wmm_ac_notify(struct ieee80211_sub_if_data *sdata,
3012 struct ieee80211_hdr *hdr,
3015 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3018 struct ieee80211_sta_tx_tspec *tx_tspec;
3019 unsigned long now = jiffies;
3021 if (!ieee80211_is_data_qos(hdr->frame_control))
3024 tid = ieee80211_get_tid(hdr);
3025 ac = ieee80211_ac_from_tid(tid);
3026 tx_tspec = &ifmgd->tx_tspec[ac];
3028 if (likely(!tx_tspec->admitted_time))
3031 if (time_after(now, tx_tspec->time_slice_start + HZ)) {
3032 tx_tspec->consumed_tx_time = 0;
3033 tx_tspec->time_slice_start = now;
3035 if (tx_tspec->downgraded) {
3036 tx_tspec->action = TX_TSPEC_ACTION_STOP_DOWNGRADE;
3037 schedule_delayed_work(&ifmgd->tx_tspec_wk, 0);
3041 if (tx_tspec->downgraded)
3044 tx_tspec->consumed_tx_time += tx_time;
3046 if (tx_tspec->consumed_tx_time >= tx_tspec->admitted_time) {
3047 tx_tspec->downgraded = true;
3048 tx_tspec->action = TX_TSPEC_ACTION_DOWNGRADE;
3049 schedule_delayed_work(&ifmgd->tx_tspec_wk, 0);
3053 void ieee80211_sta_tx_notify(struct ieee80211_sub_if_data *sdata,
3054 struct ieee80211_hdr *hdr, bool ack, u16 tx_time)
3056 ieee80211_sta_tx_wmm_ac_notify(sdata, hdr, tx_time);
3058 if (!ieee80211_is_any_nullfunc(hdr->frame_control) ||
3059 !sdata->u.mgd.probe_send_count)
3063 sdata->u.mgd.probe_send_count = 0;
3065 sdata->u.mgd.nullfunc_failed = true;
3066 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
3069 static void ieee80211_mlme_send_probe_req(struct ieee80211_sub_if_data *sdata,
3070 const u8 *src, const u8 *dst,
3071 const u8 *ssid, size_t ssid_len,
3072 struct ieee80211_channel *channel)
3074 struct sk_buff *skb;
3076 skb = ieee80211_build_probe_req(sdata, src, dst, (u32)-1, channel,
3077 ssid, ssid_len, NULL, 0,
3078 IEEE80211_PROBE_FLAG_DIRECTED);
3080 ieee80211_tx_skb(sdata, skb);
3083 static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
3085 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3086 u8 *dst = sdata->vif.cfg.ap_addr;
3087 u8 unicast_limit = max(1, max_probe_tries - 3);
3088 struct sta_info *sta;
3090 if (WARN_ON(sdata->vif.valid_links))
3094 * Try sending broadcast probe requests for the last three
3095 * probe requests after the first ones failed since some
3096 * buggy APs only support broadcast probe requests.
3098 if (ifmgd->probe_send_count >= unicast_limit)
3102 * When the hardware reports an accurate Tx ACK status, it's
3103 * better to send a nullfunc frame instead of a probe request,
3104 * as it will kick us off the AP quickly if we aren't associated
3105 * anymore. The timeout will be reset if the frame is ACKed by
3108 ifmgd->probe_send_count++;
3111 mutex_lock(&sdata->local->sta_mtx);
3112 sta = sta_info_get(sdata, dst);
3114 ieee80211_check_fast_rx(sta);
3115 mutex_unlock(&sdata->local->sta_mtx);
3118 if (ieee80211_hw_check(&sdata->local->hw, REPORTS_TX_ACK_STATUS)) {
3119 ifmgd->nullfunc_failed = false;
3120 ieee80211_send_nullfunc(sdata->local, sdata, false);
3122 ieee80211_mlme_send_probe_req(sdata, sdata->vif.addr, dst,
3123 sdata->vif.cfg.ssid,
3124 sdata->vif.cfg.ssid_len,
3125 sdata->deflink.u.mgd.bss->channel);
3128 ifmgd->probe_timeout = jiffies + msecs_to_jiffies(probe_wait_ms);
3129 run_again(sdata, ifmgd->probe_timeout);
3132 static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
3135 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3136 bool already = false;
3138 if (WARN_ON(sdata->vif.valid_links))
3141 if (!ieee80211_sdata_running(sdata))
3146 if (!ifmgd->associated)
3149 mutex_lock(&sdata->local->mtx);
3151 if (sdata->local->tmp_channel || sdata->local->scanning) {
3152 mutex_unlock(&sdata->local->mtx);
3156 if (sdata->local->suspending) {
3157 /* reschedule after resume */
3158 mutex_unlock(&sdata->local->mtx);
3159 ieee80211_reset_ap_probe(sdata);
3164 mlme_dbg_ratelimited(sdata,
3165 "detected beacon loss from AP (missed %d beacons) - probing\n",
3168 ieee80211_cqm_beacon_loss_notify(&sdata->vif, GFP_KERNEL);
3172 * The driver/our work has already reported this event or the
3173 * connection monitoring has kicked in and we have already sent
3174 * a probe request. Or maybe the AP died and the driver keeps
3175 * reporting until we disassociate...
3177 * In either case we have to ignore the current call to this
3178 * function (except for setting the correct probe reason bit)
3179 * because otherwise we would reset the timer every time and
3180 * never check whether we received a probe response!
3182 if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL)
3185 ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
3187 mutex_unlock(&sdata->local->mtx);
3192 mutex_lock(&sdata->local->iflist_mtx);
3193 ieee80211_recalc_ps(sdata->local);
3194 mutex_unlock(&sdata->local->iflist_mtx);
3196 ifmgd->probe_send_count = 0;
3197 ieee80211_mgd_probe_ap_send(sdata);
3199 sdata_unlock(sdata);
3202 struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw,
3203 struct ieee80211_vif *vif)
3205 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
3206 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3207 struct cfg80211_bss *cbss;
3208 struct sk_buff *skb;
3209 const struct element *ssid;
3212 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION ||
3213 sdata->vif.valid_links))
3216 sdata_assert_lock(sdata);
3218 if (ifmgd->associated)
3219 cbss = sdata->deflink.u.mgd.bss;
3220 else if (ifmgd->auth_data)
3221 cbss = ifmgd->auth_data->bss;
3222 else if (ifmgd->assoc_data && ifmgd->assoc_data->link[0].bss)
3223 cbss = ifmgd->assoc_data->link[0].bss;
3228 ssid = ieee80211_bss_get_elem(cbss, WLAN_EID_SSID);
3229 if (WARN_ONCE(!ssid || ssid->datalen > IEEE80211_MAX_SSID_LEN,
3230 "invalid SSID element (len=%d)",
3231 ssid ? ssid->datalen : -1))
3234 ssid_len = ssid->datalen;
3236 skb = ieee80211_build_probe_req(sdata, sdata->vif.addr, cbss->bssid,
3237 (u32) -1, cbss->channel,
3238 ssid->data, ssid_len,
3239 NULL, 0, IEEE80211_PROBE_FLAG_DIRECTED);
3244 EXPORT_SYMBOL(ieee80211_ap_probereq_get);
3246 static void ieee80211_report_disconnect(struct ieee80211_sub_if_data *sdata,
3247 const u8 *buf, size_t len, bool tx,
3248 u16 reason, bool reconnect)
3250 struct ieee80211_event event = {
3252 .u.mlme.data = tx ? DEAUTH_TX_EVENT : DEAUTH_RX_EVENT,
3253 .u.mlme.reason = reason,
3257 cfg80211_tx_mlme_mgmt(sdata->dev, buf, len, reconnect);
3259 cfg80211_rx_mlme_mgmt(sdata->dev, buf, len);
3261 drv_event_callback(sdata->local, sdata, &event);
3264 static void __ieee80211_disconnect(struct ieee80211_sub_if_data *sdata)
3266 struct ieee80211_local *local = sdata->local;
3267 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3268 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
3272 if (!ifmgd->associated) {
3273 sdata_unlock(sdata);
3277 /* in MLO assume we have a link where we can TX the frame */
3278 tx = sdata->vif.valid_links || !sdata->deflink.csa_block_tx;
3280 if (!ifmgd->driver_disconnect) {
3281 unsigned int link_id;
3284 * AP is probably out of range (or not reachable for another
3285 * reason) so remove the bss structs for that AP. In the case
3286 * of multi-link, it's not clear that all of them really are
3287 * out of range, but if they weren't the driver likely would
3288 * have switched to just have a single link active?
3291 link_id < ARRAY_SIZE(sdata->link);
3293 struct ieee80211_link_data *link;
3295 link = sdata_dereference(sdata->link[link_id], sdata);
3298 cfg80211_unlink_bss(local->hw.wiphy, link->u.mgd.bss);
3299 link->u.mgd.bss = NULL;
3303 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
3304 ifmgd->driver_disconnect ?
3305 WLAN_REASON_DEAUTH_LEAVING :
3306 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
3308 mutex_lock(&local->mtx);
3309 /* the other links will be destroyed */
3310 sdata->vif.bss_conf.csa_active = false;
3311 sdata->deflink.u.mgd.csa_waiting_bcn = false;
3312 if (sdata->deflink.csa_block_tx) {
3313 ieee80211_wake_vif_queues(local, sdata,
3314 IEEE80211_QUEUE_STOP_REASON_CSA);
3315 sdata->deflink.csa_block_tx = false;
3317 mutex_unlock(&local->mtx);
3319 ieee80211_report_disconnect(sdata, frame_buf, sizeof(frame_buf), tx,
3320 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
3322 ifmgd->reconnect = false;
3324 sdata_unlock(sdata);
3327 static void ieee80211_beacon_connection_loss_work(struct work_struct *work)
3329 struct ieee80211_sub_if_data *sdata =
3330 container_of(work, struct ieee80211_sub_if_data,
3331 u.mgd.beacon_connection_loss_work);
3332 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3334 if (ifmgd->connection_loss) {
3335 sdata_info(sdata, "Connection to AP %pM lost\n",
3336 sdata->vif.cfg.ap_addr);
3337 __ieee80211_disconnect(sdata);
3338 ifmgd->connection_loss = false;
3339 } else if (ifmgd->driver_disconnect) {
3341 "Driver requested disconnection from AP %pM\n",
3342 sdata->vif.cfg.ap_addr);
3343 __ieee80211_disconnect(sdata);
3344 ifmgd->driver_disconnect = false;
3346 if (ifmgd->associated)
3347 sdata->deflink.u.mgd.beacon_loss_count++;
3348 ieee80211_mgd_probe_ap(sdata, true);
3352 static void ieee80211_csa_connection_drop_work(struct work_struct *work)
3354 struct ieee80211_sub_if_data *sdata =
3355 container_of(work, struct ieee80211_sub_if_data,
3356 u.mgd.csa_connection_drop_work);
3358 __ieee80211_disconnect(sdata);
3361 void ieee80211_beacon_loss(struct ieee80211_vif *vif)
3363 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
3364 struct ieee80211_hw *hw = &sdata->local->hw;
3366 trace_api_beacon_loss(sdata);
3368 sdata->u.mgd.connection_loss = false;
3369 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
3371 EXPORT_SYMBOL(ieee80211_beacon_loss);
3373 void ieee80211_connection_loss(struct ieee80211_vif *vif)
3375 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
3376 struct ieee80211_hw *hw = &sdata->local->hw;
3378 trace_api_connection_loss(sdata);
3380 sdata->u.mgd.connection_loss = true;
3381 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
3383 EXPORT_SYMBOL(ieee80211_connection_loss);
3385 void ieee80211_disconnect(struct ieee80211_vif *vif, bool reconnect)
3387 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
3388 struct ieee80211_hw *hw = &sdata->local->hw;
3390 trace_api_disconnect(sdata, reconnect);
3392 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
3395 sdata->u.mgd.driver_disconnect = true;
3396 sdata->u.mgd.reconnect = reconnect;
3397 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
3399 EXPORT_SYMBOL(ieee80211_disconnect);
3401 static void ieee80211_destroy_auth_data(struct ieee80211_sub_if_data *sdata,
3404 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
3406 sdata_assert_lock(sdata);
3410 * we are not authenticated yet, the only timer that could be
3411 * running is the timeout for the authentication response which
3412 * which is not relevant anymore.
3414 del_timer_sync(&sdata->u.mgd.timer);
3415 sta_info_destroy_addr(sdata, auth_data->ap_addr);
3417 /* other links are destroyed */
3418 sdata->deflink.u.mgd.conn_flags = 0;
3419 eth_zero_addr(sdata->deflink.u.mgd.bssid);
3420 ieee80211_link_info_change_notify(sdata, &sdata->deflink,
3422 sdata->u.mgd.flags = 0;
3424 mutex_lock(&sdata->local->mtx);
3425 ieee80211_link_release_channel(&sdata->deflink);
3426 ieee80211_vif_set_links(sdata, 0);
3427 mutex_unlock(&sdata->local->mtx);
3430 cfg80211_put_bss(sdata->local->hw.wiphy, auth_data->bss);
3432 sdata->u.mgd.auth_data = NULL;
3442 static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata,
3443 enum assoc_status status)
3445 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
3447 sdata_assert_lock(sdata);
3449 if (status != ASSOC_SUCCESS) {
3451 * we are not associated yet, the only timer that could be
3452 * running is the timeout for the association response which
3453 * which is not relevant anymore.
3455 del_timer_sync(&sdata->u.mgd.timer);
3456 sta_info_destroy_addr(sdata, assoc_data->ap_addr);
3458 sdata->deflink.u.mgd.conn_flags = 0;
3459 eth_zero_addr(sdata->deflink.u.mgd.bssid);
3460 ieee80211_link_info_change_notify(sdata, &sdata->deflink,
3462 sdata->u.mgd.flags = 0;
3463 sdata->vif.bss_conf.mu_mimo_owner = false;
3465 if (status != ASSOC_REJECTED) {
3466 struct cfg80211_assoc_failure data = {
3467 .timeout = status == ASSOC_TIMEOUT,
3471 BUILD_BUG_ON(ARRAY_SIZE(data.bss) !=
3472 ARRAY_SIZE(assoc_data->link));
3474 for (i = 0; i < ARRAY_SIZE(data.bss); i++)
3475 data.bss[i] = assoc_data->link[i].bss;
3477 if (sdata->vif.valid_links)
3478 data.ap_mld_addr = assoc_data->ap_addr;
3480 cfg80211_assoc_failure(sdata->dev, &data);
3483 mutex_lock(&sdata->local->mtx);
3484 ieee80211_link_release_channel(&sdata->deflink);
3485 ieee80211_vif_set_links(sdata, 0);
3486 mutex_unlock(&sdata->local->mtx);
3490 sdata->u.mgd.assoc_data = NULL;
3493 static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
3494 struct ieee80211_mgmt *mgmt, size_t len)
3496 struct ieee80211_local *local = sdata->local;
3497 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
3498 const struct element *challenge;
3501 struct ieee80211_prep_tx_info info = {
3502 .subtype = IEEE80211_STYPE_AUTH,
3505 pos = mgmt->u.auth.variable;
3506 challenge = cfg80211_find_elem(WLAN_EID_CHALLENGE, pos,
3507 len - (pos - (u8 *)mgmt));
3510 auth_data->expected_transaction = 4;
3511 drv_mgd_prepare_tx(sdata->local, sdata, &info);
3512 if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
3513 tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
3514 IEEE80211_TX_INTFL_MLME_CONN_TX;
3515 ieee80211_send_auth(sdata, 3, auth_data->algorithm, 0,
3517 challenge->datalen + sizeof(*challenge),
3518 auth_data->ap_addr, auth_data->ap_addr,
3519 auth_data->key, auth_data->key_len,
3520 auth_data->key_idx, tx_flags);
3523 static bool ieee80211_mark_sta_auth(struct ieee80211_sub_if_data *sdata)
3525 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3526 const u8 *ap_addr = ifmgd->auth_data->ap_addr;
3527 struct sta_info *sta;
3530 sdata_info(sdata, "authenticated\n");
3531 ifmgd->auth_data->done = true;
3532 ifmgd->auth_data->timeout = jiffies + IEEE80211_AUTH_WAIT_ASSOC;
3533 ifmgd->auth_data->timeout_started = true;
3534 run_again(sdata, ifmgd->auth_data->timeout);
3536 /* move station state to auth */
3537 mutex_lock(&sdata->local->sta_mtx);
3538 sta = sta_info_get(sdata, ap_addr);
3540 WARN_ONCE(1, "%s: STA %pM not found", sdata->name, ap_addr);
3544 if (sta_info_move_state(sta, IEEE80211_STA_AUTH)) {
3545 sdata_info(sdata, "failed moving %pM to auth\n", ap_addr);
3551 mutex_unlock(&sdata->local->sta_mtx);
3555 static void ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
3556 struct ieee80211_mgmt *mgmt, size_t len)
3558 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3559 u16 auth_alg, auth_transaction, status_code;
3560 struct ieee80211_event event = {
3562 .u.mlme.data = AUTH_EVENT,
3564 struct ieee80211_prep_tx_info info = {
3565 .subtype = IEEE80211_STYPE_AUTH,
3568 sdata_assert_lock(sdata);
3573 if (!ifmgd->auth_data || ifmgd->auth_data->done)
3576 if (!ether_addr_equal(ifmgd->auth_data->ap_addr, mgmt->bssid))
3579 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
3580 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
3581 status_code = le16_to_cpu(mgmt->u.auth.status_code);
3583 if (auth_alg != ifmgd->auth_data->algorithm ||
3584 (auth_alg != WLAN_AUTH_SAE &&
3585 auth_transaction != ifmgd->auth_data->expected_transaction) ||
3586 (auth_alg == WLAN_AUTH_SAE &&
3587 (auth_transaction < ifmgd->auth_data->expected_transaction ||
3588 auth_transaction > 2))) {
3589 sdata_info(sdata, "%pM unexpected authentication state: alg %d (expected %d) transact %d (expected %d)\n",
3590 mgmt->sa, auth_alg, ifmgd->auth_data->algorithm,
3592 ifmgd->auth_data->expected_transaction);
3596 if (status_code != WLAN_STATUS_SUCCESS) {
3597 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
3599 if (auth_alg == WLAN_AUTH_SAE &&
3600 (status_code == WLAN_STATUS_ANTI_CLOG_REQUIRED ||
3601 (auth_transaction == 1 &&
3602 (status_code == WLAN_STATUS_SAE_HASH_TO_ELEMENT ||
3603 status_code == WLAN_STATUS_SAE_PK)))) {
3604 /* waiting for userspace now */
3605 ifmgd->auth_data->waiting = true;
3606 ifmgd->auth_data->timeout =
3607 jiffies + IEEE80211_AUTH_WAIT_SAE_RETRY;
3608 ifmgd->auth_data->timeout_started = true;
3609 run_again(sdata, ifmgd->auth_data->timeout);
3613 sdata_info(sdata, "%pM denied authentication (status %d)\n",
3614 mgmt->sa, status_code);
3615 ieee80211_destroy_auth_data(sdata, false);
3616 event.u.mlme.status = MLME_DENIED;
3617 event.u.mlme.reason = status_code;
3618 drv_event_callback(sdata->local, sdata, &event);
3622 switch (ifmgd->auth_data->algorithm) {
3623 case WLAN_AUTH_OPEN:
3624 case WLAN_AUTH_LEAP:
3627 case WLAN_AUTH_FILS_SK:
3628 case WLAN_AUTH_FILS_SK_PFS:
3629 case WLAN_AUTH_FILS_PK:
3631 case WLAN_AUTH_SHARED_KEY:
3632 if (ifmgd->auth_data->expected_transaction != 4) {
3633 ieee80211_auth_challenge(sdata, mgmt, len);
3634 /* need another frame */
3639 WARN_ONCE(1, "invalid auth alg %d",
3640 ifmgd->auth_data->algorithm);
3644 event.u.mlme.status = MLME_SUCCESS;
3646 drv_event_callback(sdata->local, sdata, &event);
3647 if (ifmgd->auth_data->algorithm != WLAN_AUTH_SAE ||
3648 (auth_transaction == 2 &&
3649 ifmgd->auth_data->expected_transaction == 2)) {
3650 if (!ieee80211_mark_sta_auth(sdata))
3651 return; /* ignore frame -- wait for timeout */
3652 } else if (ifmgd->auth_data->algorithm == WLAN_AUTH_SAE &&
3653 auth_transaction == 2) {
3654 sdata_info(sdata, "SAE peer confirmed\n");
3655 ifmgd->auth_data->peer_confirmed = true;
3658 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
3660 drv_mgd_complete_tx(sdata->local, sdata, &info);
3663 #define case_WLAN(type) \
3664 case WLAN_REASON_##type: return #type
3666 const char *ieee80211_get_reason_code_string(u16 reason_code)
3668 switch (reason_code) {
3669 case_WLAN(UNSPECIFIED);
3670 case_WLAN(PREV_AUTH_NOT_VALID);
3671 case_WLAN(DEAUTH_LEAVING);
3672 case_WLAN(DISASSOC_DUE_TO_INACTIVITY);
3673 case_WLAN(DISASSOC_AP_BUSY);
3674 case_WLAN(CLASS2_FRAME_FROM_NONAUTH_STA);
3675 case_WLAN(CLASS3_FRAME_FROM_NONASSOC_STA);
3676 case_WLAN(DISASSOC_STA_HAS_LEFT);
3677 case_WLAN(STA_REQ_ASSOC_WITHOUT_AUTH);
3678 case_WLAN(DISASSOC_BAD_POWER);
3679 case_WLAN(DISASSOC_BAD_SUPP_CHAN);
3680 case_WLAN(INVALID_IE);
3681 case_WLAN(MIC_FAILURE);
3682 case_WLAN(4WAY_HANDSHAKE_TIMEOUT);
3683 case_WLAN(GROUP_KEY_HANDSHAKE_TIMEOUT);
3684 case_WLAN(IE_DIFFERENT);
3685 case_WLAN(INVALID_GROUP_CIPHER);
3686 case_WLAN(INVALID_PAIRWISE_CIPHER);
3687 case_WLAN(INVALID_AKMP);
3688 case_WLAN(UNSUPP_RSN_VERSION);
3689 case_WLAN(INVALID_RSN_IE_CAP);
3690 case_WLAN(IEEE8021X_FAILED);
3691 case_WLAN(CIPHER_SUITE_REJECTED);
3692 case_WLAN(DISASSOC_UNSPECIFIED_QOS);
3693 case_WLAN(DISASSOC_QAP_NO_BANDWIDTH);
3694 case_WLAN(DISASSOC_LOW_ACK);
3695 case_WLAN(DISASSOC_QAP_EXCEED_TXOP);
3696 case_WLAN(QSTA_LEAVE_QBSS);
3697 case_WLAN(QSTA_NOT_USE);
3698 case_WLAN(QSTA_REQUIRE_SETUP);
3699 case_WLAN(QSTA_TIMEOUT);
3700 case_WLAN(QSTA_CIPHER_NOT_SUPP);
3701 case_WLAN(MESH_PEER_CANCELED);
3702 case_WLAN(MESH_MAX_PEERS);
3703 case_WLAN(MESH_CONFIG);
3704 case_WLAN(MESH_CLOSE);
3705 case_WLAN(MESH_MAX_RETRIES);
3706 case_WLAN(MESH_CONFIRM_TIMEOUT);
3707 case_WLAN(MESH_INVALID_GTK);
3708 case_WLAN(MESH_INCONSISTENT_PARAM);
3709 case_WLAN(MESH_INVALID_SECURITY);
3710 case_WLAN(MESH_PATH_ERROR);
3711 case_WLAN(MESH_PATH_NOFORWARD);
3712 case_WLAN(MESH_PATH_DEST_UNREACHABLE);
3713 case_WLAN(MAC_EXISTS_IN_MBSS);
3714 case_WLAN(MESH_CHAN_REGULATORY);
3715 case_WLAN(MESH_CHAN);
3716 default: return "<unknown>";
3720 static void ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
3721 struct ieee80211_mgmt *mgmt, size_t len)
3723 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3724 u16 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
3726 sdata_assert_lock(sdata);
3731 if (!ether_addr_equal(mgmt->bssid, mgmt->sa)) {
3732 ieee80211_tdls_handle_disconnect(sdata, mgmt->sa, reason_code);
3736 if (ifmgd->associated &&
3737 ether_addr_equal(mgmt->bssid, sdata->vif.cfg.ap_addr)) {
3738 sdata_info(sdata, "deauthenticated from %pM (Reason: %u=%s)\n",
3739 sdata->vif.cfg.ap_addr, reason_code,
3740 ieee80211_get_reason_code_string(reason_code));
3742 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
3744 ieee80211_report_disconnect(sdata, (u8 *)mgmt, len, false,
3745 reason_code, false);
3749 if (ifmgd->assoc_data &&
3750 ether_addr_equal(mgmt->bssid, ifmgd->assoc_data->ap_addr)) {
3752 "deauthenticated from %pM while associating (Reason: %u=%s)\n",
3753 ifmgd->assoc_data->ap_addr, reason_code,
3754 ieee80211_get_reason_code_string(reason_code));
3756 ieee80211_destroy_assoc_data(sdata, ASSOC_ABANDON);
3758 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
3764 static void ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
3765 struct ieee80211_mgmt *mgmt, size_t len)
3767 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3770 sdata_assert_lock(sdata);
3775 if (!ifmgd->associated ||
3776 !ether_addr_equal(mgmt->bssid, sdata->vif.cfg.ap_addr))
3779 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
3781 if (!ether_addr_equal(mgmt->bssid, mgmt->sa)) {
3782 ieee80211_tdls_handle_disconnect(sdata, mgmt->sa, reason_code);
3786 sdata_info(sdata, "disassociated from %pM (Reason: %u=%s)\n",
3787 sdata->vif.cfg.ap_addr, reason_code,
3788 ieee80211_get_reason_code_string(reason_code));
3790 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
3792 ieee80211_report_disconnect(sdata, (u8 *)mgmt, len, false, reason_code,
3796 static void ieee80211_get_rates(struct ieee80211_supported_band *sband,
3797 u8 *supp_rates, unsigned int supp_rates_len,
3798 u32 *rates, u32 *basic_rates,
3799 bool *have_higher_than_11mbit,
3800 int *min_rate, int *min_rate_index,
3805 for (i = 0; i < supp_rates_len; i++) {
3806 int rate = supp_rates[i] & 0x7f;
3807 bool is_basic = !!(supp_rates[i] & 0x80);
3809 if ((rate * 5 * (1 << shift)) > 110)
3810 *have_higher_than_11mbit = true;
3813 * Skip HT, VHT, HE and SAE H2E only BSS membership selectors
3814 * since they're not rates.
3816 * Note: Even though the membership selector and the basic
3817 * rate flag share the same bit, they are not exactly
3820 if (supp_rates[i] == (0x80 | BSS_MEMBERSHIP_SELECTOR_HT_PHY) ||
3821 supp_rates[i] == (0x80 | BSS_MEMBERSHIP_SELECTOR_VHT_PHY) ||
3822 supp_rates[i] == (0x80 | BSS_MEMBERSHIP_SELECTOR_HE_PHY) ||
3823 supp_rates[i] == (0x80 | BSS_MEMBERSHIP_SELECTOR_SAE_H2E))
3826 for (j = 0; j < sband->n_bitrates; j++) {
3827 struct ieee80211_rate *br;
3830 br = &sband->bitrates[j];
3832 brate = DIV_ROUND_UP(br->bitrate, (1 << shift) * 5);
3833 if (brate == rate) {
3836 *basic_rates |= BIT(j);
3837 if ((rate * 5) < *min_rate) {
3838 *min_rate = rate * 5;
3839 *min_rate_index = j;
3847 static bool ieee80211_twt_req_supported(const struct link_sta_info *link_sta,
3848 const struct ieee802_11_elems *elems)
3850 if (elems->ext_capab_len < 10)
3853 if (!(elems->ext_capab[9] & WLAN_EXT_CAPA10_TWT_RESPONDER_SUPPORT))
3856 return link_sta->pub->he_cap.he_cap_elem.mac_cap_info[0] &
3857 IEEE80211_HE_MAC_CAP0_TWT_RES;
3860 static int ieee80211_recalc_twt_req(struct ieee80211_link_data *link,
3861 struct link_sta_info *link_sta,
3862 struct ieee802_11_elems *elems)
3864 bool twt = ieee80211_twt_req_supported(link_sta, elems);
3866 if (link->conf->twt_requester != twt) {
3867 link->conf->twt_requester = twt;
3868 return BSS_CHANGED_TWT;
3873 static bool ieee80211_twt_bcast_support(struct ieee80211_sub_if_data *sdata,
3874 struct ieee80211_bss_conf *bss_conf,
3875 struct ieee80211_supported_band *sband,
3876 struct link_sta_info *link_sta)
3878 const struct ieee80211_sta_he_cap *own_he_cap =
3879 ieee80211_get_he_iftype_cap(sband,
3880 ieee80211_vif_type_p2p(&sdata->vif));
3882 return bss_conf->he_support &&
3883 (link_sta->pub->he_cap.he_cap_elem.mac_cap_info[2] &
3884 IEEE80211_HE_MAC_CAP2_BCAST_TWT) &&
3886 (own_he_cap->he_cap_elem.mac_cap_info[2] &
3887 IEEE80211_HE_MAC_CAP2_BCAST_TWT);
3890 static bool ieee80211_assoc_config_link(struct ieee80211_link_data *link,
3891 struct link_sta_info *link_sta,
3892 struct cfg80211_bss *cbss,
3893 struct ieee80211_mgmt *mgmt,
3894 const u8 *elem_start,
3895 unsigned int elem_len,
3898 struct ieee80211_sub_if_data *sdata = link->sdata;
3899 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
3900 struct ieee80211_bss_conf *bss_conf = link->conf;
3901 struct ieee80211_local *local = sdata->local;
3902 struct ieee80211_elems_parse_params parse_params = {
3903 .start = elem_start,
3906 .link_id = link == &sdata->deflink ? -1 : link->link_id,
3908 bool is_6ghz = cbss->channel->band == NL80211_BAND_6GHZ;
3909 bool is_s1g = cbss->channel->band == NL80211_BAND_S1GHZ;
3910 const struct cfg80211_bss_ies *bss_ies = NULL;
3911 struct ieee80211_supported_band *sband;
3912 struct ieee802_11_elems *elems;
3916 elems = ieee802_11_parse_elems_full(&parse_params);
3920 /* FIXME: use from STA profile element after parsing that */
3921 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
3923 if (!is_s1g && !elems->supp_rates) {
3924 sdata_info(sdata, "no SuppRates element in AssocResp\n");
3929 link->u.mgd.tdls_chan_switch_prohibited =
3930 elems->ext_capab && elems->ext_capab_len >= 5 &&
3931 (elems->ext_capab[4] & WLAN_EXT_CAPA5_TDLS_CH_SW_PROHIBITED);
3934 * Some APs are erroneously not including some information in their
3935 * (re)association response frames. Try to recover by using the data
3936 * from the beacon or probe response. This seems to afflict mobile
3937 * 2G/3G/4G wifi routers, reported models include the "Onda PN51T",
3938 * "Vodafone PocketWiFi 2", "ZTE MF60" and a similar T-Mobile device.
3941 ((assoc_data->wmm && !elems->wmm_param) ||
3942 (!(link->u.mgd.conn_flags & IEEE80211_CONN_DISABLE_HT) &&
3943 (!elems->ht_cap_elem || !elems->ht_operation)) ||
3944 (!(link->u.mgd.conn_flags & IEEE80211_CONN_DISABLE_VHT) &&
3945 (!elems->vht_cap_elem || !elems->vht_operation)))) {
3946 const struct cfg80211_bss_ies *ies;
3947 struct ieee802_11_elems *bss_elems;
3950 ies = rcu_dereference(cbss->ies);
3952 bss_ies = kmemdup(ies, sizeof(*ies) + ies->len,
3960 parse_params.start = bss_ies->data;
3961 parse_params.len = bss_ies->len;
3962 bss_elems = ieee802_11_parse_elems_full(&parse_params);
3968 if (assoc_data->wmm &&
3969 !elems->wmm_param && bss_elems->wmm_param) {
3970 elems->wmm_param = bss_elems->wmm_param;
3972 "AP bug: WMM param missing from AssocResp\n");
3976 * Also check if we requested HT/VHT, otherwise the AP doesn't
3977 * have to include the IEs in the (re)association response.
3979 if (!elems->ht_cap_elem && bss_elems->ht_cap_elem &&
3980 !(link->u.mgd.conn_flags & IEEE80211_CONN_DISABLE_HT)) {
3981 elems->ht_cap_elem = bss_elems->ht_cap_elem;
3983 "AP bug: HT capability missing from AssocResp\n");
3985 if (!elems->ht_operation && bss_elems->ht_operation &&
3986 !(link->u.mgd.conn_flags & IEEE80211_CONN_DISABLE_HT)) {
3987 elems->ht_operation = bss_elems->ht_operation;
3989 "AP bug: HT operation missing from AssocResp\n");
3991 if (!elems->vht_cap_elem && bss_elems->vht_cap_elem &&
3992 !(link->u.mgd.conn_flags & IEEE80211_CONN_DISABLE_VHT)) {
3993 elems->vht_cap_elem = bss_elems->vht_cap_elem;
3995 "AP bug: VHT capa missing from AssocResp\n");
3997 if (!elems->vht_operation && bss_elems->vht_operation &&
3998 !(link->u.mgd.conn_flags & IEEE80211_CONN_DISABLE_VHT)) {
3999 elems->vht_operation = bss_elems->vht_operation;
4001 "AP bug: VHT operation missing from AssocResp\n");
4008 * We previously checked these in the beacon/probe response, so
4009 * they should be present here. This is just a safety net.
4011 if (!is_6ghz && !(link->u.mgd.conn_flags & IEEE80211_CONN_DISABLE_HT) &&
4012 (!elems->wmm_param || !elems->ht_cap_elem || !elems->ht_operation)) {
4014 "HT AP is missing WMM params or HT capability/operation\n");
4019 if (!is_6ghz && !(link->u.mgd.conn_flags & IEEE80211_CONN_DISABLE_VHT) &&
4020 (!elems->vht_cap_elem || !elems->vht_operation)) {
4022 "VHT AP is missing VHT capability/operation\n");
4027 if (is_6ghz && !(link->u.mgd.conn_flags & IEEE80211_CONN_DISABLE_HE) &&
4028 !elems->he_6ghz_capa) {
4030 "HE 6 GHz AP is missing HE 6 GHz band capability\n");
4035 sband = ieee80211_get_link_sband(link);
4041 if (!(link->u.mgd.conn_flags & IEEE80211_CONN_DISABLE_HE) &&
4042 (!elems->he_cap || !elems->he_operation)) {
4043 mutex_unlock(&sdata->local->sta_mtx);
4045 "HE AP is missing HE capability/operation\n");
4050 /* Set up internal HT/VHT capabilities */
4051 if (elems->ht_cap_elem && !(link->u.mgd.conn_flags & IEEE80211_CONN_DISABLE_HT))
4052 ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
4056 if (elems->vht_cap_elem && !(link->u.mgd.conn_flags & IEEE80211_CONN_DISABLE_VHT))
4057 ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband,
4058 elems->vht_cap_elem,
4061 if (elems->he_operation && !(link->u.mgd.conn_flags & IEEE80211_CONN_DISABLE_HE) &&
4063 ieee80211_he_cap_ie_to_sta_he_cap(sdata, sband,
4066 elems->he_6ghz_capa,
4069 bss_conf->he_support = link_sta->pub->he_cap.has_he;
4070 if (elems->rsnx && elems->rsnx_len &&
4071 (elems->rsnx[0] & WLAN_RSNX_CAPA_PROTECTED_TWT) &&
4072 wiphy_ext_feature_isset(local->hw.wiphy,
4073 NL80211_EXT_FEATURE_PROTECTED_TWT))
4074 bss_conf->twt_protected = true;
4076 bss_conf->twt_protected = false;
4078 *changed |= ieee80211_recalc_twt_req(link, link_sta, elems);
4080 if (elems->eht_operation && elems->eht_cap &&
4081 !(link->u.mgd.conn_flags & IEEE80211_CONN_DISABLE_EHT)) {
4082 ieee80211_eht_cap_ie_to_sta_eht_cap(sdata, sband,
4089 bss_conf->eht_support = link_sta->pub->eht_cap.has_eht;
4091 bss_conf->eht_support = false;
4094 bss_conf->he_support = false;
4095 bss_conf->twt_requester = false;
4096 bss_conf->twt_protected = false;
4097 bss_conf->eht_support = false;
4100 bss_conf->twt_broadcast =
4101 ieee80211_twt_bcast_support(sdata, bss_conf, sband, link_sta);
4103 if (bss_conf->he_support) {
4104 bss_conf->he_bss_color.color =
4105 le32_get_bits(elems->he_operation->he_oper_params,
4106 IEEE80211_HE_OPERATION_BSS_COLOR_MASK);
4107 bss_conf->he_bss_color.partial =
4108 le32_get_bits(elems->he_operation->he_oper_params,
4109 IEEE80211_HE_OPERATION_PARTIAL_BSS_COLOR);
4110 bss_conf->he_bss_color.enabled =
4111 !le32_get_bits(elems->he_operation->he_oper_params,
4112 IEEE80211_HE_OPERATION_BSS_COLOR_DISABLED);
4114 if (bss_conf->he_bss_color.enabled)
4115 *changed |= BSS_CHANGED_HE_BSS_COLOR;
4117 bss_conf->htc_trig_based_pkt_ext =
4118 le32_get_bits(elems->he_operation->he_oper_params,
4119 IEEE80211_HE_OPERATION_DFLT_PE_DURATION_MASK);
4120 bss_conf->frame_time_rts_th =
4121 le32_get_bits(elems->he_operation->he_oper_params,
4122 IEEE80211_HE_OPERATION_RTS_THRESHOLD_MASK);
4124 bss_conf->uora_exists = !!elems->uora_element;
4125 if (elems->uora_element)
4126 bss_conf->uora_ocw_range = elems->uora_element[0];
4128 ieee80211_he_op_ie_to_bss_conf(&sdata->vif, elems->he_operation);
4129 ieee80211_he_spr_ie_to_bss_conf(&sdata->vif, elems->he_spr);
4130 /* TODO: OPEN: what happens if BSS color disable is set? */
4133 if (cbss->transmitted_bss) {
4134 bss_conf->nontransmitted = true;
4135 ether_addr_copy(bss_conf->transmitter_bssid,
4136 cbss->transmitted_bss->bssid);
4137 bss_conf->bssid_indicator = cbss->max_bssid_indicator;
4138 bss_conf->bssid_index = cbss->bssid_index;
4142 * Some APs, e.g. Netgear WNDR3700, report invalid HT operation data
4143 * in their association response, so ignore that data for our own
4144 * configuration. If it changed since the last beacon, we'll get the
4145 * next beacon and update then.
4149 * If an operating mode notification IE is present, override the
4150 * NSS calculation (that would be done in rate_control_rate_init())
4151 * and use the # of streams from that element.
4153 if (elems->opmode_notif &&
4154 !(*elems->opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_TYPE_BF)) {
4157 nss = *elems->opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_MASK;
4158 nss >>= IEEE80211_OPMODE_NOTIF_RX_NSS_SHIFT;
4160 link_sta->pub->rx_nss = nss;
4164 * Always handle WMM once after association regardless
4165 * of the first value the AP uses. Setting -1 here has
4166 * that effect because the AP values is an unsigned
4169 link->u.mgd.wmm_last_param_set = -1;
4170 link->u.mgd.mu_edca_last_param_set = -1;
4172 if (link->u.mgd.disable_wmm_tracking) {
4173 ieee80211_set_wmm_default(link, false, false);
4174 } else if (!ieee80211_sta_wmm_params(local, link, elems->wmm_param,
4175 elems->wmm_param_len,
4176 elems->mu_edca_param_set)) {
4177 /* still enable QoS since we might have HT/VHT */
4178 ieee80211_set_wmm_default(link, false, true);
4179 /* disable WMM tracking in this case to disable
4180 * tracking WMM parameter changes in the beacon if
4181 * the parameters weren't actually valid. Doing so
4182 * avoids changing parameters very strangely when
4183 * the AP is going back and forth between valid and
4184 * invalid parameters.
4186 link->u.mgd.disable_wmm_tracking = true;
4189 if (elems->max_idle_period_ie) {
4190 bss_conf->max_idle_period =
4191 le16_to_cpu(elems->max_idle_period_ie->max_idle_period);
4192 bss_conf->protected_keep_alive =
4193 !!(elems->max_idle_period_ie->idle_options &
4194 WLAN_IDLE_OPTIONS_PROTECTED_KEEP_ALIVE);
4195 *changed |= BSS_CHANGED_KEEP_ALIVE;
4197 bss_conf->max_idle_period = 0;
4198 bss_conf->protected_keep_alive = false;
4201 /* set assoc capability (AID was already set earlier),
4202 * ieee80211_set_associated() will tell the driver */
4203 bss_conf->assoc_capability = capab_info;
4212 static int ieee80211_mgd_setup_link_sta(struct ieee80211_link_data *link,
4213 struct sta_info *sta,
4214 struct link_sta_info *link_sta,
4215 struct cfg80211_bss *cbss)
4217 struct ieee80211_sub_if_data *sdata = link->sdata;
4218 struct ieee80211_local *local = sdata->local;
4219 struct ieee80211_bss *bss = (void *)cbss->priv;
4220 u32 rates = 0, basic_rates = 0;
4221 bool have_higher_than_11mbit = false;
4222 int min_rate = INT_MAX, min_rate_index = -1;
4223 /* this is clearly wrong for MLO but we'll just remove it later */
4224 int shift = ieee80211_vif_get_shift(&sdata->vif);
4225 struct ieee80211_supported_band *sband;
4227 memcpy(link_sta->addr, cbss->bssid, ETH_ALEN);
4228 memcpy(link_sta->pub->addr, cbss->bssid, ETH_ALEN);
4230 /* TODO: S1G Basic Rate Set is expressed elsewhere */
4231 if (cbss->channel->band == NL80211_BAND_S1GHZ) {
4232 ieee80211_s1g_sta_rate_init(sta);
4236 sband = local->hw.wiphy->bands[cbss->channel->band];
4238 ieee80211_get_rates(sband, bss->supp_rates, bss->supp_rates_len,
4239 &rates, &basic_rates, &have_higher_than_11mbit,
4240 &min_rate, &min_rate_index, shift);
4243 * This used to be a workaround for basic rates missing
4244 * in the association response frame. Now that we no
4245 * longer use the basic rates from there, it probably
4246 * doesn't happen any more, but keep the workaround so
4247 * in case some *other* APs are buggy in different ways
4248 * we can connect -- with a warning.
4249 * Allow this workaround only in case the AP provided at least
4252 if (min_rate_index < 0) {
4253 link_info(link, "No legacy rates in association response\n");
4255 } else if (!basic_rates) {
4256 link_info(link, "No basic rates, using min rate instead\n");
4257 basic_rates = BIT(min_rate_index);
4261 link_sta->pub->supp_rates[cbss->channel->band] = rates;
4263 link_info(link, "No rates found, keeping mandatory only\n");
4265 link->conf->basic_rates = basic_rates;
4267 /* cf. IEEE 802.11 9.2.12 */
4268 link->operating_11g_mode = sband->band == NL80211_BAND_2GHZ &&
4269 have_higher_than_11mbit;
4274 static u8 ieee80211_max_rx_chains(struct ieee80211_link_data *link,
4275 struct cfg80211_bss *cbss)
4277 struct ieee80211_he_mcs_nss_supp *he_mcs_nss_supp;
4278 const struct element *ht_cap_elem, *vht_cap_elem;
4279 const struct cfg80211_bss_ies *ies;
4280 const struct ieee80211_ht_cap *ht_cap;
4281 const struct ieee80211_vht_cap *vht_cap;
4282 const struct ieee80211_he_cap_elem *he_cap;
4283 const struct element *he_cap_elem;
4284 u16 mcs_80_map, mcs_160_map;
4285 int i, mcs_nss_size;
4289 if (link->u.mgd.conn_flags & IEEE80211_CONN_DISABLE_HT)
4292 ht_cap_elem = ieee80211_bss_get_elem(cbss, WLAN_EID_HT_CAPABILITY);
4293 if (ht_cap_elem && ht_cap_elem->datalen >= sizeof(*ht_cap)) {
4294 ht_cap = (void *)ht_cap_elem->data;
4295 chains = ieee80211_mcs_to_chains(&ht_cap->mcs);
4297 * TODO: use "Tx Maximum Number Spatial Streams Supported" and
4298 * "Tx Unequal Modulation Supported" fields.
4302 if (link->u.mgd.conn_flags & IEEE80211_CONN_DISABLE_VHT)
4305 vht_cap_elem = ieee80211_bss_get_elem(cbss, WLAN_EID_VHT_CAPABILITY);
4306 if (vht_cap_elem && vht_cap_elem->datalen >= sizeof(*vht_cap)) {
4310 vht_cap = (void *)vht_cap_elem->data;
4311 tx_mcs_map = le16_to_cpu(vht_cap->supp_mcs.tx_mcs_map);
4312 for (nss = 8; nss > 0; nss--) {
4313 if (((tx_mcs_map >> (2 * (nss - 1))) & 3) !=
4314 IEEE80211_VHT_MCS_NOT_SUPPORTED)
4317 /* TODO: use "Tx Highest Supported Long GI Data Rate" field? */
4318 chains = max(chains, nss);
4321 if (link->u.mgd.conn_flags & IEEE80211_CONN_DISABLE_HE)
4324 ies = rcu_dereference(cbss->ies);
4325 he_cap_elem = cfg80211_find_ext_elem(WLAN_EID_EXT_HE_CAPABILITY,
4326 ies->data, ies->len);
4328 if (!he_cap_elem || he_cap_elem->datalen < sizeof(*he_cap))
4331 /* skip one byte ext_tag_id */
4332 he_cap = (void *)(he_cap_elem->data + 1);
4333 mcs_nss_size = ieee80211_he_mcs_nss_size(he_cap);
4336 if (he_cap_elem->datalen < 1 + mcs_nss_size + sizeof(*he_cap))
4339 /* mcs_nss is right after he_cap info */
4340 he_mcs_nss_supp = (void *)(he_cap + 1);
4342 mcs_80_map = le16_to_cpu(he_mcs_nss_supp->tx_mcs_80);
4344 for (i = 7; i >= 0; i--) {
4345 u8 mcs_80 = mcs_80_map >> (2 * i) & 3;
4347 if (mcs_80 != IEEE80211_VHT_MCS_NOT_SUPPORTED) {
4348 chains = max_t(u8, chains, i + 1);
4353 support_160 = he_cap->phy_cap_info[0] &
4354 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G;
4359 mcs_160_map = le16_to_cpu(he_mcs_nss_supp->tx_mcs_160);
4360 for (i = 7; i >= 0; i--) {
4361 u8 mcs_160 = mcs_160_map >> (2 * i) & 3;
4363 if (mcs_160 != IEEE80211_VHT_MCS_NOT_SUPPORTED) {
4364 chains = max_t(u8, chains, i + 1);
4373 ieee80211_verify_peer_he_mcs_support(struct ieee80211_sub_if_data *sdata,
4374 const struct cfg80211_bss_ies *ies,
4375 const struct ieee80211_he_operation *he_op)
4377 const struct element *he_cap_elem;
4378 const struct ieee80211_he_cap_elem *he_cap;
4379 struct ieee80211_he_mcs_nss_supp *he_mcs_nss_supp;
4380 u16 mcs_80_map_tx, mcs_80_map_rx;
4385 he_cap_elem = cfg80211_find_ext_elem(WLAN_EID_EXT_HE_CAPABILITY,
4386 ies->data, ies->len);
4389 if (!he_cap_elem || he_cap_elem->datalen < 1 + sizeof(*he_cap)) {
4391 "Invalid HE elem, Disable HE\n");
4395 /* skip one byte ext_tag_id */
4396 he_cap = (void *)(he_cap_elem->data + 1);
4397 mcs_nss_size = ieee80211_he_mcs_nss_size(he_cap);
4400 if (he_cap_elem->datalen < 1 + sizeof(*he_cap) + mcs_nss_size) {
4402 "Invalid HE elem with nss size, Disable HE\n");
4406 /* mcs_nss is right after he_cap info */
4407 he_mcs_nss_supp = (void *)(he_cap + 1);
4409 mcs_80_map_tx = le16_to_cpu(he_mcs_nss_supp->tx_mcs_80);
4410 mcs_80_map_rx = le16_to_cpu(he_mcs_nss_supp->rx_mcs_80);
4412 /* P802.11-REVme/D0.3
4413 * 27.1.1 Introduction to the HE PHY
4415 * An HE STA shall support the following features:
4417 * Single spatial stream HE-MCSs 0 to 7 (transmit and receive) in all
4418 * supported channel widths for HE SU PPDUs
4420 if ((mcs_80_map_tx & 0x3) == IEEE80211_HE_MCS_NOT_SUPPORTED ||
4421 (mcs_80_map_rx & 0x3) == IEEE80211_HE_MCS_NOT_SUPPORTED) {
4423 "Missing mandatory rates for 1 Nss, rx 0x%x, tx 0x%x, disable HE\n",
4424 mcs_80_map_tx, mcs_80_map_rx);
4431 ap_min_req_set = le16_to_cpu(he_op->he_mcs_nss_set);
4434 * Apparently iPhone 13 (at least iOS version 15.3.1) sets this to all
4435 * zeroes, which is nonsense, and completely inconsistent with itself
4436 * (it doesn't have 8 streams). Accept the settings in this case anyway.
4438 if (!ap_min_req_set)
4441 /* make sure the AP is consistent with itself
4443 * P802.11-REVme/D0.3
4444 * 26.17.1 Basic HE BSS operation
4446 * A STA that is operating in an HE BSS shall be able to receive and
4447 * transmit at each of the <HE-MCS, NSS> tuple values indicated by the
4448 * Basic HE-MCS And NSS Set field of the HE Operation parameter of the
4449 * MLME-START.request primitive and shall be able to receive at each of
4450 * the <HE-MCS, NSS> tuple values indicated by the Supported HE-MCS and
4451 * NSS Set field in the HE Capabilities parameter of the MLMESTART.request
4454 for (nss = 8; nss > 0; nss--) {
4455 u8 ap_op_val = (ap_min_req_set >> (2 * (nss - 1))) & 3;
4459 if (ap_op_val == IEEE80211_HE_MCS_NOT_SUPPORTED)
4462 ap_rx_val = (mcs_80_map_rx >> (2 * (nss - 1))) & 3;
4463 ap_tx_val = (mcs_80_map_tx >> (2 * (nss - 1))) & 3;
4465 if (ap_rx_val == IEEE80211_HE_MCS_NOT_SUPPORTED ||
4466 ap_tx_val == IEEE80211_HE_MCS_NOT_SUPPORTED ||
4467 ap_rx_val < ap_op_val || ap_tx_val < ap_op_val) {
4469 "Invalid rates for %d Nss, rx %d, tx %d oper %d, disable HE\n",
4470 nss, ap_rx_val, ap_rx_val, ap_op_val);
4479 ieee80211_verify_sta_he_mcs_support(struct ieee80211_sub_if_data *sdata,
4480 struct ieee80211_supported_band *sband,
4481 const struct ieee80211_he_operation *he_op)
4483 const struct ieee80211_sta_he_cap *sta_he_cap =
4484 ieee80211_get_he_iftype_cap(sband,
4485 ieee80211_vif_type_p2p(&sdata->vif));
4489 if (!sta_he_cap || !he_op)
4492 ap_min_req_set = le16_to_cpu(he_op->he_mcs_nss_set);
4495 * Apparently iPhone 13 (at least iOS version 15.3.1) sets this to all
4496 * zeroes, which is nonsense, and completely inconsistent with itself
4497 * (it doesn't have 8 streams). Accept the settings in this case anyway.
4499 if (!ap_min_req_set)
4502 /* Need to go over for 80MHz, 160MHz and for 80+80 */
4503 for (i = 0; i < 3; i++) {
4504 const struct ieee80211_he_mcs_nss_supp *sta_mcs_nss_supp =
4505 &sta_he_cap->he_mcs_nss_supp;
4506 u16 sta_mcs_map_rx =
4507 le16_to_cpu(((__le16 *)sta_mcs_nss_supp)[2 * i]);
4508 u16 sta_mcs_map_tx =
4509 le16_to_cpu(((__le16 *)sta_mcs_nss_supp)[2 * i + 1]);
4511 bool verified = true;
4514 * For each band there is a maximum of 8 spatial streams
4515 * possible. Each of the sta_mcs_map_* is a 16-bit struct built
4516 * of 2 bits per NSS (1-8), with the values defined in enum
4517 * ieee80211_he_mcs_support. Need to make sure STA TX and RX
4518 * capabilities aren't less than the AP's minimum requirements
4519 * for this HE BSS per SS.
4520 * It is enough to find one such band that meets the reqs.
4522 for (nss = 8; nss > 0; nss--) {
4523 u8 sta_rx_val = (sta_mcs_map_rx >> (2 * (nss - 1))) & 3;
4524 u8 sta_tx_val = (sta_mcs_map_tx >> (2 * (nss - 1))) & 3;
4525 u8 ap_val = (ap_min_req_set >> (2 * (nss - 1))) & 3;
4527 if (ap_val == IEEE80211_HE_MCS_NOT_SUPPORTED)
4531 * Make sure the HE AP doesn't require MCSs that aren't
4532 * supported by the client as required by spec
4534 * P802.11-REVme/D0.3
4535 * 26.17.1 Basic HE BSS operation
4537 * An HE STA shall not attempt to join * (MLME-JOIN.request primitive)
4538 * a BSS, unless it supports (i.e., is able to both transmit and
4539 * receive using) all of the <HE-MCS, NSS> tuples in the basic
4540 * HE-MCS and NSS set.
4542 if (sta_rx_val == IEEE80211_HE_MCS_NOT_SUPPORTED ||
4543 sta_tx_val == IEEE80211_HE_MCS_NOT_SUPPORTED ||
4544 (ap_val > sta_rx_val) || (ap_val > sta_tx_val)) {
4554 /* If here, STA doesn't meet AP's HE min requirements */
4558 static int ieee80211_prep_channel(struct ieee80211_sub_if_data *sdata,
4559 struct ieee80211_link_data *link,
4560 struct cfg80211_bss *cbss,
4561 ieee80211_conn_flags_t *conn_flags)
4563 struct ieee80211_local *local = sdata->local;
4564 const struct ieee80211_ht_cap *ht_cap = NULL;
4565 const struct ieee80211_ht_operation *ht_oper = NULL;
4566 const struct ieee80211_vht_operation *vht_oper = NULL;
4567 const struct ieee80211_he_operation *he_oper = NULL;
4568 const struct ieee80211_eht_operation *eht_oper = NULL;
4569 const struct ieee80211_s1g_oper_ie *s1g_oper = NULL;
4570 struct ieee80211_supported_band *sband;
4571 struct cfg80211_chan_def chandef;
4572 bool is_6ghz = cbss->channel->band == NL80211_BAND_6GHZ;
4573 bool is_5ghz = cbss->channel->band == NL80211_BAND_5GHZ;
4574 struct ieee80211_bss *bss = (void *)cbss->priv;
4575 struct ieee802_11_elems *elems;
4576 const struct cfg80211_bss_ies *ies;
4583 ies = rcu_dereference(cbss->ies);
4584 elems = ieee802_11_parse_elems(ies->data, ies->len, false, cbss);
4590 sband = local->hw.wiphy->bands[cbss->channel->band];
4592 *conn_flags &= ~(IEEE80211_CONN_DISABLE_40MHZ |
4593 IEEE80211_CONN_DISABLE_80P80MHZ |
4594 IEEE80211_CONN_DISABLE_160MHZ);
4596 /* disable HT/VHT/HE if we don't support them */
4597 if (!sband->ht_cap.ht_supported && !is_6ghz) {
4598 mlme_dbg(sdata, "HT not supported, disabling HT/VHT/HE/EHT\n");
4599 *conn_flags |= IEEE80211_CONN_DISABLE_HT;
4600 *conn_flags |= IEEE80211_CONN_DISABLE_VHT;
4601 *conn_flags |= IEEE80211_CONN_DISABLE_HE;
4602 *conn_flags |= IEEE80211_CONN_DISABLE_EHT;
4605 if (!sband->vht_cap.vht_supported && is_5ghz) {
4606 mlme_dbg(sdata, "VHT not supported, disabling VHT/HE/EHT\n");
4607 *conn_flags |= IEEE80211_CONN_DISABLE_VHT;
4608 *conn_flags |= IEEE80211_CONN_DISABLE_HE;
4609 *conn_flags |= IEEE80211_CONN_DISABLE_EHT;
4612 if (!ieee80211_get_he_iftype_cap(sband,
4613 ieee80211_vif_type_p2p(&sdata->vif))) {
4614 mlme_dbg(sdata, "HE not supported, disabling HE and EHT\n");
4615 *conn_flags |= IEEE80211_CONN_DISABLE_HE;
4616 *conn_flags |= IEEE80211_CONN_DISABLE_EHT;
4619 if (!ieee80211_get_eht_iftype_cap(sband,
4620 ieee80211_vif_type_p2p(&sdata->vif))) {
4621 mlme_dbg(sdata, "EHT not supported, disabling EHT\n");
4622 *conn_flags |= IEEE80211_CONN_DISABLE_EHT;
4625 if (!(*conn_flags & IEEE80211_CONN_DISABLE_HT) && !is_6ghz) {
4626 ht_oper = elems->ht_operation;
4627 ht_cap = elems->ht_cap_elem;
4630 *conn_flags |= IEEE80211_CONN_DISABLE_HT;
4635 if (!(*conn_flags & IEEE80211_CONN_DISABLE_VHT) && !is_6ghz) {
4636 vht_oper = elems->vht_operation;
4637 if (vht_oper && !ht_oper) {
4640 "AP advertised VHT without HT, disabling HT/VHT/HE\n");
4641 *conn_flags |= IEEE80211_CONN_DISABLE_HT;
4642 *conn_flags |= IEEE80211_CONN_DISABLE_VHT;
4643 *conn_flags |= IEEE80211_CONN_DISABLE_HE;
4644 *conn_flags |= IEEE80211_CONN_DISABLE_EHT;
4647 if (!elems->vht_cap_elem) {
4649 "bad VHT capabilities, disabling VHT\n");
4650 *conn_flags |= IEEE80211_CONN_DISABLE_VHT;
4655 if (!(*conn_flags & IEEE80211_CONN_DISABLE_HE)) {
4656 he_oper = elems->he_operation;
4658 if (link && is_6ghz) {
4659 struct ieee80211_bss_conf *bss_conf;
4662 bss_conf = link->conf;
4664 if (elems->pwr_constr_elem)
4665 bss_conf->pwr_reduction = *elems->pwr_constr_elem;
4667 BUILD_BUG_ON(ARRAY_SIZE(bss_conf->tx_pwr_env) !=
4668 ARRAY_SIZE(elems->tx_pwr_env));
4670 for (i = 0; i < elems->tx_pwr_env_num; i++) {
4671 if (elems->tx_pwr_env_len[i] >
4672 sizeof(bss_conf->tx_pwr_env[j]))
4675 bss_conf->tx_pwr_env_num++;
4676 memcpy(&bss_conf->tx_pwr_env[j], elems->tx_pwr_env[i],
4677 elems->tx_pwr_env_len[i]);
4682 if (!ieee80211_verify_peer_he_mcs_support(sdata, ies, he_oper) ||
4683 !ieee80211_verify_sta_he_mcs_support(sdata, sband, he_oper))
4684 *conn_flags |= IEEE80211_CONN_DISABLE_HE |
4685 IEEE80211_CONN_DISABLE_EHT;
4689 * EHT requires HE to be supported as well. Specifically for 6 GHz
4690 * channels, the operation channel information can only be deduced from
4691 * both the 6 GHz operation information (from the HE operation IE) and
4695 (IEEE80211_CONN_DISABLE_HE |
4696 IEEE80211_CONN_DISABLE_EHT)) &&
4698 const struct cfg80211_bss_ies *cbss_ies;
4699 const u8 *eht_oper_ie;
4701 cbss_ies = rcu_dereference(cbss->ies);
4702 eht_oper_ie = cfg80211_find_ext_ie(WLAN_EID_EXT_EHT_OPERATION,
4703 cbss_ies->data, cbss_ies->len);
4704 if (eht_oper_ie && eht_oper_ie[1] >=
4705 1 + sizeof(struct ieee80211_eht_operation))
4706 eht_oper = (void *)(eht_oper_ie + 3);
4711 /* Allow VHT if at least one channel on the sband supports 80 MHz */
4713 for (i = 0; i < sband->n_channels; i++) {
4714 if (sband->channels[i].flags & (IEEE80211_CHAN_DISABLED |
4715 IEEE80211_CHAN_NO_80MHZ))
4723 sdata_info(sdata, "80 MHz not supported, disabling VHT\n");
4724 *conn_flags |= IEEE80211_CONN_DISABLE_VHT;
4727 if (sband->band == NL80211_BAND_S1GHZ) {
4728 s1g_oper = elems->s1g_oper;
4731 "AP missing S1G operation element?\n");
4735 ieee80211_determine_chantype(sdata, link, *conn_flags,
4745 link->needed_rx_chains =
4746 min(ieee80211_max_rx_chains(link, cbss),
4750 /* the element data was RCU protected so no longer valid anyway */
4754 if (*conn_flags & IEEE80211_CONN_DISABLE_HE && is_6ghz) {
4755 sdata_info(sdata, "Rejecting non-HE 6/7 GHz connection");
4762 /* will change later if needed */
4763 link->smps_mode = IEEE80211_SMPS_OFF;
4765 mutex_lock(&local->mtx);
4767 * If this fails (possibly due to channel context sharing
4768 * on incompatible channels, e.g. 80+80 and 160 sharing the
4769 * same control channel) try to use a smaller bandwidth.
4771 ret = ieee80211_link_use_channel(link, &chandef,
4772 IEEE80211_CHANCTX_SHARED);
4774 /* don't downgrade for 5 and 10 MHz channels, though. */
4775 if (chandef.width == NL80211_CHAN_WIDTH_5 ||
4776 chandef.width == NL80211_CHAN_WIDTH_10)
4779 while (ret && chandef.width != NL80211_CHAN_WIDTH_20_NOHT) {
4781 ieee80211_chandef_downgrade(&chandef);
4782 ret = ieee80211_link_use_channel(link, &chandef,
4783 IEEE80211_CHANCTX_SHARED);
4786 mutex_unlock(&local->mtx);
4790 static bool ieee80211_assoc_success(struct ieee80211_sub_if_data *sdata,
4791 struct ieee80211_mgmt *mgmt,
4792 struct ieee802_11_elems *elems,
4793 const u8 *elem_start, unsigned int elem_len)
4795 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4796 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
4797 struct ieee80211_local *local = sdata->local;
4798 unsigned int link_id;
4799 struct sta_info *sta;
4800 u64 changed[IEEE80211_MLD_MAX_NUM_LINKS] = {};
4803 mutex_lock(&sdata->local->sta_mtx);
4805 * station info was already allocated and inserted before
4806 * the association and should be available to us
4808 sta = sta_info_get(sdata, assoc_data->ap_addr);
4812 if (sdata->vif.valid_links) {
4813 u16 valid_links = 0;
4815 for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {
4816 if (!assoc_data->link[link_id].bss)
4818 valid_links |= BIT(link_id);
4820 if (link_id != assoc_data->assoc_link_id) {
4821 err = ieee80211_sta_allocate_link(sta, link_id);
4827 ieee80211_vif_set_links(sdata, valid_links);
4830 for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {
4831 struct ieee80211_link_data *link;
4832 struct link_sta_info *link_sta;
4834 if (!assoc_data->link[link_id].bss)
4837 link = sdata_dereference(sdata->link[link_id], sdata);
4841 if (sdata->vif.valid_links)
4843 "local address %pM, AP link address %pM\n",
4845 assoc_data->link[link_id].bss->bssid);
4847 link_sta = rcu_dereference_protected(sta->link[link_id],
4848 lockdep_is_held(&local->sta_mtx));
4849 if (WARN_ON(!link_sta))
4852 if (link_id != assoc_data->assoc_link_id) {
4853 err = ieee80211_prep_channel(sdata, link,
4854 assoc_data->link[link_id].bss,
4855 &link->u.mgd.conn_flags);
4860 err = ieee80211_mgd_setup_link_sta(link, sta, link_sta,
4861 assoc_data->link[link_id].bss);
4865 if (!ieee80211_assoc_config_link(link, link_sta,
4866 assoc_data->link[link_id].bss,
4867 mgmt, elem_start, elem_len,
4871 if (link_id != assoc_data->assoc_link_id) {
4872 err = ieee80211_sta_activate_link(sta, link_id);
4878 rate_control_rate_init(sta);
4880 if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED) {
4881 set_sta_flag(sta, WLAN_STA_MFP);
4882 sta->sta.mfp = true;
4884 sta->sta.mfp = false;
4887 ieee80211_sta_set_max_amsdu_subframes(sta, elems->ext_capab,
4888 elems->ext_capab_len);
4890 sta->sta.wme = (elems->wmm_param || elems->s1g_capab) &&
4891 local->hw.queues >= IEEE80211_NUM_ACS;
4893 err = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
4894 if (!err && !(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
4895 err = sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
4898 "failed to move station %pM to desired state\n",
4900 WARN_ON(__sta_info_destroy(sta));
4904 if (sdata->wdev.use_4addr)
4905 drv_sta_set_4addr(local, sdata, &sta->sta, true);
4907 mutex_unlock(&sdata->local->sta_mtx);
4909 ieee80211_set_associated(sdata, assoc_data, changed);
4912 * If we're using 4-addr mode, let the AP know that we're
4913 * doing so, so that it can create the STA VLAN on its side
4915 if (ifmgd->use_4addr)
4916 ieee80211_send_4addr_nullfunc(local, sdata);
4919 * Start timer to probe the connection to the AP now.
4920 * Also start the timer that will detect beacon loss.
4922 ieee80211_sta_reset_beacon_monitor(sdata);
4923 ieee80211_sta_reset_conn_monitor(sdata);
4927 eth_zero_addr(sdata->vif.cfg.ap_addr);
4928 mutex_unlock(&sdata->local->sta_mtx);
4932 static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
4933 struct ieee80211_mgmt *mgmt,
4936 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4937 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
4938 u16 capab_info, status_code, aid;
4939 struct ieee802_11_elems *elems;
4941 const u8 *elem_start;
4942 unsigned int elem_len;
4944 struct ieee80211_event event = {
4946 .u.mlme.data = ASSOC_EVENT,
4948 struct ieee80211_prep_tx_info info = {};
4949 struct cfg80211_rx_assoc_resp resp = {
4952 unsigned int link_id;
4954 sdata_assert_lock(sdata);
4959 if (!ether_addr_equal(assoc_data->ap_addr, mgmt->bssid) ||
4960 !ether_addr_equal(assoc_data->ap_addr, mgmt->sa))
4964 * AssocResp and ReassocResp have identical structure, so process both
4965 * of them in this function.
4971 reassoc = ieee80211_is_reassoc_resp(mgmt->frame_control);
4972 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
4973 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
4974 if (assoc_data->s1g)
4975 elem_start = mgmt->u.s1g_assoc_resp.variable;
4977 elem_start = mgmt->u.assoc_resp.variable;
4980 * Note: this may not be perfect, AP might misbehave - if
4981 * anyone needs to rely on perfect complete notification
4982 * with the exact right subtype, then we need to track what
4983 * we actually transmitted.
4985 info.subtype = reassoc ? IEEE80211_STYPE_REASSOC_REQ :
4986 IEEE80211_STYPE_ASSOC_REQ;
4988 if (assoc_data->fils_kek_len &&
4989 fils_decrypt_assoc_resp(sdata, (u8 *)mgmt, &len, assoc_data) < 0)
4992 elem_len = len - (elem_start - (u8 *)mgmt);
4993 elems = ieee802_11_parse_elems(elem_start, elem_len, false, NULL);
4997 if (elems->aid_resp)
4998 aid = le16_to_cpu(elems->aid_resp->aid);
4999 else if (assoc_data->s1g)
5002 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
5005 * The 5 MSB of the AID field are reserved
5006 * (802.11-2016 9.4.1.8 AID field)
5011 "RX %sssocResp from %pM (capab=0x%x status=%d aid=%d)\n",
5012 reassoc ? "Rea" : "A", assoc_data->ap_addr,
5013 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
5015 ifmgd->broken_ap = false;
5017 if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
5018 elems->timeout_int &&
5019 elems->timeout_int->type == WLAN_TIMEOUT_ASSOC_COMEBACK) {
5022 cfg80211_assoc_comeback(sdata->dev, assoc_data->ap_addr,
5023 le32_to_cpu(elems->timeout_int->value));
5025 tu = le32_to_cpu(elems->timeout_int->value);
5026 ms = tu * 1024 / 1000;
5028 "%pM rejected association temporarily; comeback duration %u TU (%u ms)\n",
5029 assoc_data->ap_addr, tu, ms);
5030 assoc_data->timeout = jiffies + msecs_to_jiffies(ms);
5031 assoc_data->timeout_started = true;
5032 if (ms > IEEE80211_ASSOC_TIMEOUT)
5033 run_again(sdata, assoc_data->timeout);
5037 if (status_code != WLAN_STATUS_SUCCESS) {
5038 sdata_info(sdata, "%pM denied association (code=%d)\n",
5039 assoc_data->ap_addr, status_code);
5040 event.u.mlme.status = MLME_DENIED;
5041 event.u.mlme.reason = status_code;
5042 drv_event_callback(sdata->local, sdata, &event);
5044 if (aid == 0 || aid > IEEE80211_MAX_AID) {
5046 "invalid AID value %d (out of range), turn off PS\n",
5049 ifmgd->broken_ap = true;
5052 if (sdata->vif.valid_links) {
5053 if (!elems->multi_link) {
5055 "MLO association with %pM but no multi-link element in response!\n",
5056 assoc_data->ap_addr);
5060 if (le16_get_bits(elems->multi_link->control,
5061 IEEE80211_ML_CONTROL_TYPE) !=
5062 IEEE80211_ML_CONTROL_TYPE_BASIC) {
5064 "bad multi-link element (control=0x%x)\n",
5065 le16_to_cpu(elems->multi_link->control));
5068 struct ieee80211_mle_basic_common_info *common;
5070 common = (void *)elems->multi_link->variable;
5072 if (memcmp(assoc_data->ap_addr,
5073 common->mld_mac_addr, ETH_ALEN)) {
5075 "AP MLD MAC address mismatch: got %pM expected %pM\n",
5076 common->mld_mac_addr,
5077 assoc_data->ap_addr);
5083 sdata->vif.cfg.aid = aid;
5085 if (!ieee80211_assoc_success(sdata, mgmt, elems,
5086 elem_start, elem_len)) {
5087 /* oops -- internal error -- send timeout for now */
5088 ieee80211_destroy_assoc_data(sdata, ASSOC_TIMEOUT);
5091 event.u.mlme.status = MLME_SUCCESS;
5092 drv_event_callback(sdata->local, sdata, &event);
5093 sdata_info(sdata, "associated\n");
5098 for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) {
5099 struct ieee80211_link_data *link;
5101 link = sdata_dereference(sdata->link[link_id], sdata);
5104 if (!assoc_data->link[link_id].bss)
5106 resp.links[link_id].bss = assoc_data->link[link_id].bss;
5107 resp.links[link_id].addr = link->conf->addr;
5109 /* get uapsd queues configuration - same for all links */
5110 resp.uapsd_queues = 0;
5111 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
5112 if (link->tx_conf[ac].uapsd)
5113 resp.uapsd_queues |= ieee80211_ac_to_qos_mask[ac];
5116 ieee80211_destroy_assoc_data(sdata,
5117 status_code == WLAN_STATUS_SUCCESS ?
5121 resp.buf = (u8 *)mgmt;
5123 resp.req_ies = ifmgd->assoc_req_ies;
5124 resp.req_ies_len = ifmgd->assoc_req_ies_len;
5125 if (sdata->vif.valid_links)
5126 resp.ap_mld_addr = assoc_data->ap_addr;
5127 cfg80211_rx_assoc_resp(sdata->dev, &resp);
5129 drv_mgd_complete_tx(sdata->local, sdata, &info);
5133 ieee80211_destroy_assoc_data(sdata, ASSOC_ABANDON);
5137 static void ieee80211_rx_bss_info(struct ieee80211_link_data *link,
5138 struct ieee80211_mgmt *mgmt, size_t len,
5139 struct ieee80211_rx_status *rx_status)
5141 struct ieee80211_sub_if_data *sdata = link->sdata;
5142 struct ieee80211_local *local = sdata->local;
5143 struct ieee80211_bss *bss;
5144 struct ieee80211_channel *channel;
5146 sdata_assert_lock(sdata);
5148 channel = ieee80211_get_channel_khz(local->hw.wiphy,
5149 ieee80211_rx_status_to_khz(rx_status));
5153 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, channel);
5155 link->conf->beacon_rate = bss->beacon_rate;
5156 ieee80211_rx_bss_put(local, bss);
5161 static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_link_data *link,
5162 struct sk_buff *skb)
5164 struct ieee80211_sub_if_data *sdata = link->sdata;
5165 struct ieee80211_mgmt *mgmt = (void *)skb->data;
5166 struct ieee80211_if_managed *ifmgd;
5167 struct ieee80211_rx_status *rx_status = (void *) skb->cb;
5168 struct ieee80211_channel *channel;
5169 size_t baselen, len = skb->len;
5171 ifmgd = &sdata->u.mgd;
5173 sdata_assert_lock(sdata);
5176 * According to Draft P802.11ax D6.0 clause 26.17.2.3.2:
5177 * "If a 6 GHz AP receives a Probe Request frame and responds with
5178 * a Probe Response frame [..], the Address 1 field of the Probe
5179 * Response frame shall be set to the broadcast address [..]"
5180 * So, on 6GHz band we should also accept broadcast responses.
5182 channel = ieee80211_get_channel(sdata->local->hw.wiphy,
5187 if (!ether_addr_equal(mgmt->da, sdata->vif.addr) &&
5188 (channel->band != NL80211_BAND_6GHZ ||
5189 !is_broadcast_ether_addr(mgmt->da)))
5190 return; /* ignore ProbeResp to foreign address */
5192 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
5196 ieee80211_rx_bss_info(link, mgmt, len, rx_status);
5198 if (ifmgd->associated &&
5199 ether_addr_equal(mgmt->bssid, link->u.mgd.bssid))
5200 ieee80211_reset_ap_probe(sdata);
5204 * This is the canonical list of information elements we care about,
5205 * the filter code also gives us all changes to the Microsoft OUI
5206 * (00:50:F2) vendor IE which is used for WMM which we need to track,
5207 * as well as the DTPC IE (part of the Cisco OUI) used for signaling
5208 * changes to requested client power.
5210 * We implement beacon filtering in software since that means we can
5211 * avoid processing the frame here and in cfg80211, and userspace
5212 * will not be able to tell whether the hardware supports it or not.
5214 * XXX: This list needs to be dynamic -- userspace needs to be able to
5215 * add items it requires. It also needs to be able to tell us to
5216 * look out for other vendor IEs.
5218 static const u64 care_about_ies =
5219 (1ULL << WLAN_EID_COUNTRY) |
5220 (1ULL << WLAN_EID_ERP_INFO) |
5221 (1ULL << WLAN_EID_CHANNEL_SWITCH) |
5222 (1ULL << WLAN_EID_PWR_CONSTRAINT) |
5223 (1ULL << WLAN_EID_HT_CAPABILITY) |
5224 (1ULL << WLAN_EID_HT_OPERATION) |
5225 (1ULL << WLAN_EID_EXT_CHANSWITCH_ANN);
5227 static void ieee80211_handle_beacon_sig(struct ieee80211_link_data *link,
5228 struct ieee80211_if_managed *ifmgd,
5229 struct ieee80211_bss_conf *bss_conf,
5230 struct ieee80211_local *local,
5231 struct ieee80211_rx_status *rx_status)
5233 struct ieee80211_sub_if_data *sdata = link->sdata;
5235 /* Track average RSSI from the Beacon frames of the current AP */
5237 if (!link->u.mgd.tracking_signal_avg) {
5238 link->u.mgd.tracking_signal_avg = true;
5239 ewma_beacon_signal_init(&link->u.mgd.ave_beacon_signal);
5240 link->u.mgd.last_cqm_event_signal = 0;
5241 link->u.mgd.count_beacon_signal = 1;
5242 link->u.mgd.last_ave_beacon_signal = 0;
5244 link->u.mgd.count_beacon_signal++;
5247 ewma_beacon_signal_add(&link->u.mgd.ave_beacon_signal,
5248 -rx_status->signal);
5250 if (ifmgd->rssi_min_thold != ifmgd->rssi_max_thold &&
5251 link->u.mgd.count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
5252 int sig = -ewma_beacon_signal_read(&link->u.mgd.ave_beacon_signal);
5253 int last_sig = link->u.mgd.last_ave_beacon_signal;
5254 struct ieee80211_event event = {
5259 * if signal crosses either of the boundaries, invoke callback
5260 * with appropriate parameters
5262 if (sig > ifmgd->rssi_max_thold &&
5263 (last_sig <= ifmgd->rssi_min_thold || last_sig == 0)) {
5264 link->u.mgd.last_ave_beacon_signal = sig;
5265 event.u.rssi.data = RSSI_EVENT_HIGH;
5266 drv_event_callback(local, sdata, &event);
5267 } else if (sig < ifmgd->rssi_min_thold &&
5268 (last_sig >= ifmgd->rssi_max_thold ||
5270 link->u.mgd.last_ave_beacon_signal = sig;
5271 event.u.rssi.data = RSSI_EVENT_LOW;
5272 drv_event_callback(local, sdata, &event);
5276 if (bss_conf->cqm_rssi_thold &&
5277 link->u.mgd.count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT &&
5278 !(sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI)) {
5279 int sig = -ewma_beacon_signal_read(&link->u.mgd.ave_beacon_signal);
5280 int last_event = link->u.mgd.last_cqm_event_signal;
5281 int thold = bss_conf->cqm_rssi_thold;
5282 int hyst = bss_conf->cqm_rssi_hyst;
5285 (last_event == 0 || sig < last_event - hyst)) {
5286 link->u.mgd.last_cqm_event_signal = sig;
5287 ieee80211_cqm_rssi_notify(
5289 NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
5291 } else if (sig > thold &&
5292 (last_event == 0 || sig > last_event + hyst)) {
5293 link->u.mgd.last_cqm_event_signal = sig;
5294 ieee80211_cqm_rssi_notify(
5296 NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
5301 if (bss_conf->cqm_rssi_low &&
5302 link->u.mgd.count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
5303 int sig = -ewma_beacon_signal_read(&link->u.mgd.ave_beacon_signal);
5304 int last_event = link->u.mgd.last_cqm_event_signal;
5305 int low = bss_conf->cqm_rssi_low;
5306 int high = bss_conf->cqm_rssi_high;
5309 (last_event == 0 || last_event >= low)) {
5310 link->u.mgd.last_cqm_event_signal = sig;
5311 ieee80211_cqm_rssi_notify(
5313 NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
5315 } else if (sig > high &&
5316 (last_event == 0 || last_event <= high)) {
5317 link->u.mgd.last_cqm_event_signal = sig;
5318 ieee80211_cqm_rssi_notify(
5320 NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
5326 static bool ieee80211_rx_our_beacon(const u8 *tx_bssid,
5327 struct cfg80211_bss *bss)
5329 if (ether_addr_equal(tx_bssid, bss->bssid))
5331 if (!bss->transmitted_bss)
5333 return ether_addr_equal(tx_bssid, bss->transmitted_bss->bssid);
5336 static void ieee80211_rx_mgmt_beacon(struct ieee80211_link_data *link,
5337 struct ieee80211_hdr *hdr, size_t len,
5338 struct ieee80211_rx_status *rx_status)
5340 struct ieee80211_sub_if_data *sdata = link->sdata;
5341 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
5342 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
5343 struct ieee80211_vif_cfg *vif_cfg = &sdata->vif.cfg;
5344 struct ieee80211_mgmt *mgmt = (void *) hdr;
5346 struct ieee802_11_elems *elems;
5347 struct ieee80211_local *local = sdata->local;
5348 struct ieee80211_chanctx_conf *chanctx_conf;
5349 struct ieee80211_channel *chan;
5350 struct link_sta_info *link_sta;
5351 struct sta_info *sta;
5356 u8 *bssid, *variable = mgmt->u.beacon.variable;
5357 u8 deauth_buf[IEEE80211_DEAUTH_FRAME_LEN];
5359 sdata_assert_lock(sdata);
5361 /* Process beacon from the current BSS */
5362 bssid = ieee80211_get_bssid(hdr, len, sdata->vif.type);
5363 if (ieee80211_is_s1g_beacon(mgmt->frame_control)) {
5364 struct ieee80211_ext *ext = (void *) mgmt;
5366 if (ieee80211_is_s1g_short_beacon(ext->frame_control))
5367 variable = ext->u.s1g_short_beacon.variable;
5369 variable = ext->u.s1g_beacon.variable;
5372 baselen = (u8 *) variable - (u8 *) mgmt;
5377 chanctx_conf = rcu_dereference(link->conf->chanctx_conf);
5378 if (!chanctx_conf) {
5383 if (ieee80211_rx_status_to_khz(rx_status) !=
5384 ieee80211_channel_to_khz(chanctx_conf->def.chan)) {
5388 chan = chanctx_conf->def.chan;
5391 if (ifmgd->assoc_data && ifmgd->assoc_data->need_beacon &&
5392 !WARN_ON(sdata->vif.valid_links) &&
5393 ieee80211_rx_our_beacon(bssid, ifmgd->assoc_data->link[0].bss)) {
5394 elems = ieee802_11_parse_elems(variable, len - baselen, false,
5395 ifmgd->assoc_data->link[0].bss);
5399 ieee80211_rx_bss_info(link, mgmt, len, rx_status);
5401 if (elems->dtim_period)
5402 link->u.mgd.dtim_period = elems->dtim_period;
5403 link->u.mgd.have_beacon = true;
5404 ifmgd->assoc_data->need_beacon = false;
5405 if (ieee80211_hw_check(&local->hw, TIMING_BEACON_ONLY)) {
5406 link->conf->sync_tsf =
5407 le64_to_cpu(mgmt->u.beacon.timestamp);
5408 link->conf->sync_device_ts =
5409 rx_status->device_timestamp;
5410 link->conf->sync_dtim_count = elems->dtim_count;
5413 if (elems->mbssid_config_ie)
5414 bss_conf->profile_periodicity =
5415 elems->mbssid_config_ie->profile_periodicity;
5417 bss_conf->profile_periodicity = 0;
5419 if (elems->ext_capab_len >= 11 &&
5420 (elems->ext_capab[10] & WLAN_EXT_CAPA11_EMA_SUPPORT))
5421 bss_conf->ema_ap = true;
5423 bss_conf->ema_ap = false;
5425 /* continue assoc process */
5426 ifmgd->assoc_data->timeout = jiffies;
5427 ifmgd->assoc_data->timeout_started = true;
5428 run_again(sdata, ifmgd->assoc_data->timeout);
5433 if (!ifmgd->associated ||
5434 !ieee80211_rx_our_beacon(bssid, link->u.mgd.bss))
5436 bssid = link->u.mgd.bssid;
5438 if (!(rx_status->flag & RX_FLAG_NO_SIGNAL_VAL))
5439 ieee80211_handle_beacon_sig(link, ifmgd, bss_conf,
5442 if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL) {
5443 mlme_dbg_ratelimited(sdata,
5444 "cancelling AP probe due to a received beacon\n");
5445 ieee80211_reset_ap_probe(sdata);
5449 * Push the beacon loss detection into the future since
5450 * we are processing a beacon from the AP just now.
5452 ieee80211_sta_reset_beacon_monitor(sdata);
5454 /* TODO: CRC urrently not calculated on S1G Beacon Compatibility
5455 * element (which carries the beacon interval). Don't forget to add a
5456 * bit to care_about_ies[] above if mac80211 is interested in a
5457 * changing S1G element.
5459 if (!ieee80211_is_s1g_beacon(hdr->frame_control))
5460 ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
5461 elems = ieee802_11_parse_elems_crc(variable, len - baselen,
5462 false, care_about_ies, ncrc,
5468 if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK) &&
5469 ieee80211_check_tim(elems->tim, elems->tim_len, vif_cfg->aid)) {
5470 if (local->hw.conf.dynamic_ps_timeout > 0) {
5471 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
5472 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
5473 ieee80211_hw_config(local,
5474 IEEE80211_CONF_CHANGE_PS);
5476 ieee80211_send_nullfunc(local, sdata, false);
5477 } else if (!local->pspolling && sdata->u.mgd.powersave) {
5478 local->pspolling = true;
5481 * Here is assumed that the driver will be
5482 * able to send ps-poll frame and receive a
5483 * response even though power save mode is
5484 * enabled, but some drivers might require
5485 * to disable power save here. This needs
5486 * to be investigated.
5488 ieee80211_send_pspoll(local, sdata);
5492 if (sdata->vif.p2p ||
5493 sdata->vif.driver_flags & IEEE80211_VIF_GET_NOA_UPDATE) {
5494 struct ieee80211_p2p_noa_attr noa = {};
5497 ret = cfg80211_get_p2p_attr(variable,
5499 IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
5500 (u8 *) &noa, sizeof(noa));
5502 if (link->u.mgd.p2p_noa_index != noa.index) {
5503 /* valid noa_attr and index changed */
5504 link->u.mgd.p2p_noa_index = noa.index;
5505 memcpy(&bss_conf->p2p_noa_attr, &noa, sizeof(noa));
5506 changed |= BSS_CHANGED_P2P_PS;
5508 * make sure we update all information, the CRC
5509 * mechanism doesn't look at P2P attributes.
5511 link->u.mgd.beacon_crc_valid = false;
5513 } else if (link->u.mgd.p2p_noa_index != -1) {
5514 /* noa_attr not found and we had valid noa_attr before */
5515 link->u.mgd.p2p_noa_index = -1;
5516 memset(&bss_conf->p2p_noa_attr, 0, sizeof(bss_conf->p2p_noa_attr));
5517 changed |= BSS_CHANGED_P2P_PS;
5518 link->u.mgd.beacon_crc_valid = false;
5522 if (link->u.mgd.csa_waiting_bcn)
5523 ieee80211_chswitch_post_beacon(link);
5526 * Update beacon timing and dtim count on every beacon appearance. This
5527 * will allow the driver to use the most updated values. Do it before
5528 * comparing this one with last received beacon.
5529 * IMPORTANT: These parameters would possibly be out of sync by the time
5530 * the driver will use them. The synchronized view is currently
5531 * guaranteed only in certain callbacks.
5533 if (ieee80211_hw_check(&local->hw, TIMING_BEACON_ONLY) &&
5534 !ieee80211_is_s1g_beacon(hdr->frame_control)) {
5535 link->conf->sync_tsf =
5536 le64_to_cpu(mgmt->u.beacon.timestamp);
5537 link->conf->sync_device_ts =
5538 rx_status->device_timestamp;
5539 link->conf->sync_dtim_count = elems->dtim_count;
5542 if ((ncrc == link->u.mgd.beacon_crc && link->u.mgd.beacon_crc_valid) ||
5543 ieee80211_is_s1g_short_beacon(mgmt->frame_control))
5545 link->u.mgd.beacon_crc = ncrc;
5546 link->u.mgd.beacon_crc_valid = true;
5548 ieee80211_rx_bss_info(link, mgmt, len, rx_status);
5550 ieee80211_sta_process_chanswitch(link, rx_status->mactime,
5551 rx_status->device_timestamp,
5554 if (!link->u.mgd.disable_wmm_tracking &&
5555 ieee80211_sta_wmm_params(local, link, elems->wmm_param,
5556 elems->wmm_param_len,
5557 elems->mu_edca_param_set))
5558 changed |= BSS_CHANGED_QOS;
5561 * If we haven't had a beacon before, tell the driver about the
5562 * DTIM period (and beacon timing if desired) now.
5564 if (!link->u.mgd.have_beacon) {
5565 /* a few bogus AP send dtim_period = 0 or no TIM IE */
5566 bss_conf->dtim_period = elems->dtim_period ?: 1;
5568 changed |= BSS_CHANGED_BEACON_INFO;
5569 link->u.mgd.have_beacon = true;
5571 mutex_lock(&local->iflist_mtx);
5572 ieee80211_recalc_ps(local);
5573 mutex_unlock(&local->iflist_mtx);
5575 ieee80211_recalc_ps_vif(sdata);
5578 if (elems->erp_info) {
5580 erp_value = elems->erp_info[0];
5585 if (!ieee80211_is_s1g_beacon(hdr->frame_control))
5586 changed |= ieee80211_handle_bss_capability(link,
5587 le16_to_cpu(mgmt->u.beacon.capab_info),
5588 erp_valid, erp_value);
5590 mutex_lock(&local->sta_mtx);
5591 sta = sta_info_get(sdata, sdata->vif.cfg.ap_addr);
5592 if (WARN_ON(!sta)) {
5593 mutex_unlock(&local->sta_mtx);
5596 link_sta = rcu_dereference_protected(sta->link[link->link_id],
5597 lockdep_is_held(&local->sta_mtx));
5598 if (WARN_ON(!link_sta)) {
5599 mutex_unlock(&local->sta_mtx);
5603 changed |= ieee80211_recalc_twt_req(link, link_sta, elems);
5605 if (ieee80211_config_bw(link, elems->ht_cap_elem,
5606 elems->vht_cap_elem, elems->ht_operation,
5607 elems->vht_operation, elems->he_operation,
5608 elems->eht_operation,
5609 elems->s1g_oper, bssid, &changed)) {
5610 mutex_unlock(&local->sta_mtx);
5612 "failed to follow AP %pM bandwidth change, disconnect\n",
5614 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
5615 WLAN_REASON_DEAUTH_LEAVING,
5617 ieee80211_report_disconnect(sdata, deauth_buf,
5618 sizeof(deauth_buf), true,
5619 WLAN_REASON_DEAUTH_LEAVING,
5624 if (sta && elems->opmode_notif)
5625 ieee80211_vht_handle_opmode(sdata, link_sta,
5626 *elems->opmode_notif,
5628 mutex_unlock(&local->sta_mtx);
5630 changed |= ieee80211_handle_pwr_constr(link, chan, mgmt,
5631 elems->country_elem,
5632 elems->country_elem_len,
5633 elems->pwr_constr_elem,
5634 elems->cisco_dtpc_elem);
5636 ieee80211_link_info_change_notify(sdata, link, changed);
5641 void ieee80211_sta_rx_queued_ext(struct ieee80211_sub_if_data *sdata,
5642 struct sk_buff *skb)
5644 struct ieee80211_link_data *link = &sdata->deflink;
5645 struct ieee80211_rx_status *rx_status;
5646 struct ieee80211_hdr *hdr;
5649 rx_status = (struct ieee80211_rx_status *) skb->cb;
5650 hdr = (struct ieee80211_hdr *) skb->data;
5651 fc = le16_to_cpu(hdr->frame_control);
5654 switch (fc & IEEE80211_FCTL_STYPE) {
5655 case IEEE80211_STYPE_S1G_BEACON:
5656 ieee80211_rx_mgmt_beacon(link, hdr, skb->len, rx_status);
5659 sdata_unlock(sdata);
5662 void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
5663 struct sk_buff *skb)
5665 struct ieee80211_link_data *link = &sdata->deflink;
5666 struct ieee80211_rx_status *rx_status;
5667 struct ieee80211_mgmt *mgmt;
5671 rx_status = (struct ieee80211_rx_status *) skb->cb;
5672 mgmt = (struct ieee80211_mgmt *) skb->data;
5673 fc = le16_to_cpu(mgmt->frame_control);
5677 switch (fc & IEEE80211_FCTL_STYPE) {
5678 case IEEE80211_STYPE_BEACON:
5679 ieee80211_rx_mgmt_beacon(link, (void *)mgmt,
5680 skb->len, rx_status);
5682 case IEEE80211_STYPE_PROBE_RESP:
5683 ieee80211_rx_mgmt_probe_resp(link, skb);
5685 case IEEE80211_STYPE_AUTH:
5686 ieee80211_rx_mgmt_auth(sdata, mgmt, skb->len);
5688 case IEEE80211_STYPE_DEAUTH:
5689 ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
5691 case IEEE80211_STYPE_DISASSOC:
5692 ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
5694 case IEEE80211_STYPE_ASSOC_RESP:
5695 case IEEE80211_STYPE_REASSOC_RESP:
5696 ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len);
5698 case IEEE80211_STYPE_ACTION:
5699 if (mgmt->u.action.category == WLAN_CATEGORY_SPECTRUM_MGMT) {
5700 struct ieee802_11_elems *elems;
5702 ies_len = skb->len -
5703 offsetof(struct ieee80211_mgmt,
5704 u.action.u.chan_switch.variable);
5709 /* CSA IE cannot be overridden, no need for BSSID */
5710 elems = ieee802_11_parse_elems(
5711 mgmt->u.action.u.chan_switch.variable,
5712 ies_len, true, NULL);
5714 if (elems && !elems->parse_error)
5715 ieee80211_sta_process_chanswitch(link,
5717 rx_status->device_timestamp,
5720 } else if (mgmt->u.action.category == WLAN_CATEGORY_PUBLIC) {
5721 struct ieee802_11_elems *elems;
5723 ies_len = skb->len -
5724 offsetof(struct ieee80211_mgmt,
5725 u.action.u.ext_chan_switch.variable);
5731 * extended CSA IE can't be overridden, no need for
5734 elems = ieee802_11_parse_elems(
5735 mgmt->u.action.u.ext_chan_switch.variable,
5736 ies_len, true, NULL);
5738 if (elems && !elems->parse_error) {
5739 /* for the handling code pretend it was an IE */
5740 elems->ext_chansw_ie =
5741 &mgmt->u.action.u.ext_chan_switch.data;
5743 ieee80211_sta_process_chanswitch(link,
5745 rx_status->device_timestamp,
5753 sdata_unlock(sdata);
5756 static void ieee80211_sta_timer(struct timer_list *t)
5758 struct ieee80211_sub_if_data *sdata =
5759 from_timer(sdata, t, u.mgd.timer);
5761 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
5764 void ieee80211_sta_connection_lost(struct ieee80211_sub_if_data *sdata,
5767 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
5769 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH, reason,
5772 ieee80211_report_disconnect(sdata, frame_buf, sizeof(frame_buf), true,
5776 static int ieee80211_auth(struct ieee80211_sub_if_data *sdata)
5778 struct ieee80211_local *local = sdata->local;
5779 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
5780 struct ieee80211_mgd_auth_data *auth_data = ifmgd->auth_data;
5784 struct ieee80211_prep_tx_info info = {
5785 .subtype = IEEE80211_STYPE_AUTH,
5788 sdata_assert_lock(sdata);
5790 if (WARN_ON_ONCE(!auth_data))
5795 if (auth_data->tries > IEEE80211_AUTH_MAX_TRIES) {
5796 sdata_info(sdata, "authentication with %pM timed out\n",
5797 auth_data->ap_addr);
5800 * Most likely AP is not in the range so remove the
5801 * bss struct for that AP.
5803 cfg80211_unlink_bss(local->hw.wiphy, auth_data->bss);
5808 if (auth_data->algorithm == WLAN_AUTH_SAE)
5809 info.duration = jiffies_to_msecs(IEEE80211_AUTH_TIMEOUT_SAE);
5811 drv_mgd_prepare_tx(local, sdata, &info);
5813 sdata_info(sdata, "send auth to %pM (try %d/%d)\n",
5814 auth_data->ap_addr, auth_data->tries,
5815 IEEE80211_AUTH_MAX_TRIES);
5817 auth_data->expected_transaction = 2;
5819 if (auth_data->algorithm == WLAN_AUTH_SAE) {
5820 trans = auth_data->sae_trans;
5821 status = auth_data->sae_status;
5822 auth_data->expected_transaction = trans;
5825 if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
5826 tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
5827 IEEE80211_TX_INTFL_MLME_CONN_TX;
5829 ieee80211_send_auth(sdata, trans, auth_data->algorithm, status,
5830 auth_data->data, auth_data->data_len,
5831 auth_data->ap_addr, auth_data->ap_addr,
5832 NULL, 0, 0, tx_flags);
5834 if (tx_flags == 0) {
5835 if (auth_data->algorithm == WLAN_AUTH_SAE)
5836 auth_data->timeout = jiffies +
5837 IEEE80211_AUTH_TIMEOUT_SAE;
5839 auth_data->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
5841 auth_data->timeout =
5842 round_jiffies_up(jiffies + IEEE80211_AUTH_TIMEOUT_LONG);
5845 auth_data->timeout_started = true;
5846 run_again(sdata, auth_data->timeout);
5851 static int ieee80211_do_assoc(struct ieee80211_sub_if_data *sdata)
5853 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
5854 struct ieee80211_local *local = sdata->local;
5857 sdata_assert_lock(sdata);
5859 assoc_data->tries++;
5860 if (assoc_data->tries > IEEE80211_ASSOC_MAX_TRIES) {
5861 sdata_info(sdata, "association with %pM timed out\n",
5862 assoc_data->ap_addr);
5865 * Most likely AP is not in the range so remove the
5866 * bss struct for that AP.
5868 cfg80211_unlink_bss(local->hw.wiphy,
5869 assoc_data->link[assoc_data->assoc_link_id].bss);
5874 sdata_info(sdata, "associate with %pM (try %d/%d)\n",
5875 assoc_data->ap_addr, assoc_data->tries,
5876 IEEE80211_ASSOC_MAX_TRIES);
5877 ret = ieee80211_send_assoc(sdata);
5881 if (!ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
5882 assoc_data->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
5883 assoc_data->timeout_started = true;
5884 run_again(sdata, assoc_data->timeout);
5886 assoc_data->timeout =
5887 round_jiffies_up(jiffies +
5888 IEEE80211_ASSOC_TIMEOUT_LONG);
5889 assoc_data->timeout_started = true;
5890 run_again(sdata, assoc_data->timeout);
5896 void ieee80211_mgd_conn_tx_status(struct ieee80211_sub_if_data *sdata,
5897 __le16 fc, bool acked)
5899 struct ieee80211_local *local = sdata->local;
5901 sdata->u.mgd.status_fc = fc;
5902 sdata->u.mgd.status_acked = acked;
5903 sdata->u.mgd.status_received = true;
5905 ieee80211_queue_work(&local->hw, &sdata->work);
5908 void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
5910 struct ieee80211_local *local = sdata->local;
5911 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
5915 if (ifmgd->status_received) {
5916 __le16 fc = ifmgd->status_fc;
5917 bool status_acked = ifmgd->status_acked;
5919 ifmgd->status_received = false;
5920 if (ifmgd->auth_data && ieee80211_is_auth(fc)) {
5922 if (ifmgd->auth_data->algorithm ==
5924 ifmgd->auth_data->timeout =
5926 IEEE80211_AUTH_TIMEOUT_SAE;
5928 ifmgd->auth_data->timeout =
5930 IEEE80211_AUTH_TIMEOUT_SHORT;
5931 run_again(sdata, ifmgd->auth_data->timeout);
5933 ifmgd->auth_data->timeout = jiffies - 1;
5935 ifmgd->auth_data->timeout_started = true;
5936 } else if (ifmgd->assoc_data &&
5937 (ieee80211_is_assoc_req(fc) ||
5938 ieee80211_is_reassoc_req(fc))) {
5940 ifmgd->assoc_data->timeout =
5941 jiffies + IEEE80211_ASSOC_TIMEOUT_SHORT;
5942 run_again(sdata, ifmgd->assoc_data->timeout);
5944 ifmgd->assoc_data->timeout = jiffies - 1;
5946 ifmgd->assoc_data->timeout_started = true;
5950 if (ifmgd->auth_data && ifmgd->auth_data->timeout_started &&
5951 time_after(jiffies, ifmgd->auth_data->timeout)) {
5952 if (ifmgd->auth_data->done || ifmgd->auth_data->waiting) {
5954 * ok ... we waited for assoc or continuation but
5955 * userspace didn't do it, so kill the auth data
5957 ieee80211_destroy_auth_data(sdata, false);
5958 } else if (ieee80211_auth(sdata)) {
5959 u8 ap_addr[ETH_ALEN];
5960 struct ieee80211_event event = {
5962 .u.mlme.data = AUTH_EVENT,
5963 .u.mlme.status = MLME_TIMEOUT,
5966 memcpy(ap_addr, ifmgd->auth_data->ap_addr, ETH_ALEN);
5968 ieee80211_destroy_auth_data(sdata, false);
5970 cfg80211_auth_timeout(sdata->dev, ap_addr);
5971 drv_event_callback(sdata->local, sdata, &event);
5973 } else if (ifmgd->auth_data && ifmgd->auth_data->timeout_started)
5974 run_again(sdata, ifmgd->auth_data->timeout);
5976 if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started &&
5977 time_after(jiffies, ifmgd->assoc_data->timeout)) {
5978 if ((ifmgd->assoc_data->need_beacon &&
5979 !sdata->deflink.u.mgd.have_beacon) ||
5980 ieee80211_do_assoc(sdata)) {
5981 struct ieee80211_event event = {
5983 .u.mlme.data = ASSOC_EVENT,
5984 .u.mlme.status = MLME_TIMEOUT,
5987 ieee80211_destroy_assoc_data(sdata, ASSOC_TIMEOUT);
5988 drv_event_callback(sdata->local, sdata, &event);
5990 } else if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started)
5991 run_again(sdata, ifmgd->assoc_data->timeout);
5993 if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL &&
5994 ifmgd->associated) {
5995 u8 *bssid = sdata->deflink.u.mgd.bssid;
5998 if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
5999 max_tries = max_nullfunc_tries;
6001 max_tries = max_probe_tries;
6003 /* ACK received for nullfunc probing frame */
6004 if (!ifmgd->probe_send_count)
6005 ieee80211_reset_ap_probe(sdata);
6006 else if (ifmgd->nullfunc_failed) {
6007 if (ifmgd->probe_send_count < max_tries) {
6009 "No ack for nullfunc frame to AP %pM, try %d/%i\n",
6010 bssid, ifmgd->probe_send_count,
6012 ieee80211_mgd_probe_ap_send(sdata);
6015 "No ack for nullfunc frame to AP %pM, disconnecting.\n",
6017 ieee80211_sta_connection_lost(sdata,
6018 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
6021 } else if (time_is_after_jiffies(ifmgd->probe_timeout))
6022 run_again(sdata, ifmgd->probe_timeout);
6023 else if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
6025 "Failed to send nullfunc to AP %pM after %dms, disconnecting\n",
6026 bssid, probe_wait_ms);
6027 ieee80211_sta_connection_lost(sdata,
6028 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
6029 } else if (ifmgd->probe_send_count < max_tries) {
6031 "No probe response from AP %pM after %dms, try %d/%i\n",
6032 bssid, probe_wait_ms,
6033 ifmgd->probe_send_count, max_tries);
6034 ieee80211_mgd_probe_ap_send(sdata);
6037 * We actually lost the connection ... or did we?
6041 "No probe response from AP %pM after %dms, disconnecting.\n",
6042 bssid, probe_wait_ms);
6044 ieee80211_sta_connection_lost(sdata,
6045 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
6049 sdata_unlock(sdata);
6052 static void ieee80211_sta_bcn_mon_timer(struct timer_list *t)
6054 struct ieee80211_sub_if_data *sdata =
6055 from_timer(sdata, t, u.mgd.bcn_mon_timer);
6057 if (WARN_ON(sdata->vif.valid_links))
6060 if (sdata->vif.bss_conf.csa_active &&
6061 !sdata->deflink.u.mgd.csa_waiting_bcn)
6064 if (sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)
6067 sdata->u.mgd.connection_loss = false;
6068 ieee80211_queue_work(&sdata->local->hw,
6069 &sdata->u.mgd.beacon_connection_loss_work);
6072 static void ieee80211_sta_conn_mon_timer(struct timer_list *t)
6074 struct ieee80211_sub_if_data *sdata =
6075 from_timer(sdata, t, u.mgd.conn_mon_timer);
6076 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
6077 struct ieee80211_local *local = sdata->local;
6078 struct sta_info *sta;
6079 unsigned long timeout;
6081 if (WARN_ON(sdata->vif.valid_links))
6084 if (sdata->vif.bss_conf.csa_active &&
6085 !sdata->deflink.u.mgd.csa_waiting_bcn)
6088 sta = sta_info_get(sdata, sdata->vif.cfg.ap_addr);
6092 timeout = sta->deflink.status_stats.last_ack;
6093 if (time_before(sta->deflink.status_stats.last_ack, sta->deflink.rx_stats.last_rx))
6094 timeout = sta->deflink.rx_stats.last_rx;
6095 timeout += IEEE80211_CONNECTION_IDLE_TIME;
6097 /* If timeout is after now, then update timer to fire at
6098 * the later date, but do not actually probe at this time.
6100 if (time_is_after_jiffies(timeout)) {
6101 mod_timer(&ifmgd->conn_mon_timer, round_jiffies_up(timeout));
6105 ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
6108 static void ieee80211_sta_monitor_work(struct work_struct *work)
6110 struct ieee80211_sub_if_data *sdata =
6111 container_of(work, struct ieee80211_sub_if_data,
6112 u.mgd.monitor_work);
6114 ieee80211_mgd_probe_ap(sdata, false);
6117 static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
6119 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
6120 __ieee80211_stop_poll(sdata);
6122 /* let's probe the connection once */
6123 if (!ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
6124 ieee80211_queue_work(&sdata->local->hw,
6125 &sdata->u.mgd.monitor_work);
6130 void ieee80211_mgd_quiesce(struct ieee80211_sub_if_data *sdata)
6132 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
6133 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
6137 if (ifmgd->auth_data || ifmgd->assoc_data) {
6138 const u8 *ap_addr = ifmgd->auth_data ?
6139 ifmgd->auth_data->ap_addr :
6140 ifmgd->assoc_data->ap_addr;
6143 * If we are trying to authenticate / associate while suspending,
6144 * cfg80211 won't know and won't actually abort those attempts,
6145 * thus we need to do that ourselves.
6147 ieee80211_send_deauth_disassoc(sdata, ap_addr, ap_addr,
6148 IEEE80211_STYPE_DEAUTH,
6149 WLAN_REASON_DEAUTH_LEAVING,
6151 if (ifmgd->assoc_data)
6152 ieee80211_destroy_assoc_data(sdata, ASSOC_ABANDON);
6153 if (ifmgd->auth_data)
6154 ieee80211_destroy_auth_data(sdata, false);
6155 cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
6156 IEEE80211_DEAUTH_FRAME_LEN,
6160 /* This is a bit of a hack - we should find a better and more generic
6161 * solution to this. Normally when suspending, cfg80211 will in fact
6162 * deauthenticate. However, it doesn't (and cannot) stop an ongoing
6163 * auth (not so important) or assoc (this is the problem) process.
6165 * As a consequence, it can happen that we are in the process of both
6166 * associating and suspending, and receive an association response
6167 * after cfg80211 has checked if it needs to disconnect, but before
6168 * we actually set the flag to drop incoming frames. This will then
6169 * cause the workqueue flush to process the association response in
6170 * the suspend, resulting in a successful association just before it
6171 * tries to remove the interface from the driver, which now though
6172 * has a channel context assigned ... this results in issues.
6174 * To work around this (for now) simply deauth here again if we're
6177 if (ifmgd->associated && !sdata->local->wowlan) {
6179 struct cfg80211_deauth_request req = {
6180 .reason_code = WLAN_REASON_DEAUTH_LEAVING,
6184 memcpy(bssid, sdata->vif.cfg.ap_addr, ETH_ALEN);
6185 ieee80211_mgd_deauth(sdata, &req);
6188 sdata_unlock(sdata);
6192 void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
6194 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
6197 if (!ifmgd->associated) {
6198 sdata_unlock(sdata);
6202 if (sdata->flags & IEEE80211_SDATA_DISCONNECT_RESUME) {
6203 sdata->flags &= ~IEEE80211_SDATA_DISCONNECT_RESUME;
6204 mlme_dbg(sdata, "driver requested disconnect after resume\n");
6205 ieee80211_sta_connection_lost(sdata,
6206 WLAN_REASON_UNSPECIFIED,
6208 sdata_unlock(sdata);
6212 if (sdata->flags & IEEE80211_SDATA_DISCONNECT_HW_RESTART) {
6213 sdata->flags &= ~IEEE80211_SDATA_DISCONNECT_HW_RESTART;
6214 mlme_dbg(sdata, "driver requested disconnect after hardware restart\n");
6215 ieee80211_sta_connection_lost(sdata,
6216 WLAN_REASON_UNSPECIFIED,
6218 sdata_unlock(sdata);
6222 sdata_unlock(sdata);
6225 static void ieee80211_request_smps_mgd_work(struct work_struct *work)
6227 struct ieee80211_link_data *link =
6228 container_of(work, struct ieee80211_link_data,
6229 u.mgd.request_smps_work);
6231 sdata_lock(link->sdata);
6232 __ieee80211_request_smps_mgd(link->sdata, link,
6233 link->u.mgd.driver_smps_mode);
6234 sdata_unlock(link->sdata);
6237 /* interface setup */
6238 void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
6240 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
6242 INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
6243 INIT_WORK(&ifmgd->beacon_connection_loss_work,
6244 ieee80211_beacon_connection_loss_work);
6245 INIT_WORK(&ifmgd->csa_connection_drop_work,
6246 ieee80211_csa_connection_drop_work);
6247 INIT_DELAYED_WORK(&ifmgd->tdls_peer_del_work,
6248 ieee80211_tdls_peer_del_work);
6249 timer_setup(&ifmgd->timer, ieee80211_sta_timer, 0);
6250 timer_setup(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer, 0);
6251 timer_setup(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer, 0);
6252 INIT_DELAYED_WORK(&ifmgd->tx_tspec_wk,
6253 ieee80211_sta_handle_tspec_ac_params_wk);
6256 ifmgd->powersave = sdata->wdev.ps;
6257 ifmgd->uapsd_queues = sdata->local->hw.uapsd_queues;
6258 ifmgd->uapsd_max_sp_len = sdata->local->hw.uapsd_max_sp_len;
6259 /* Setup TDLS data */
6260 spin_lock_init(&ifmgd->teardown_lock);
6261 ifmgd->teardown_skb = NULL;
6262 ifmgd->orig_teardown_skb = NULL;
6265 void ieee80211_mgd_setup_link(struct ieee80211_link_data *link)
6267 struct ieee80211_sub_if_data *sdata = link->sdata;
6268 struct ieee80211_local *local = sdata->local;
6269 unsigned int link_id = link->link_id;
6271 link->u.mgd.p2p_noa_index = -1;
6272 link->u.mgd.conn_flags = 0;
6273 link->conf->bssid = link->u.mgd.bssid;
6275 INIT_WORK(&link->u.mgd.request_smps_work,
6276 ieee80211_request_smps_mgd_work);
6277 if (local->hw.wiphy->features & NL80211_FEATURE_DYNAMIC_SMPS)
6278 link->u.mgd.req_smps = IEEE80211_SMPS_AUTOMATIC;
6280 link->u.mgd.req_smps = IEEE80211_SMPS_OFF;
6282 INIT_WORK(&link->u.mgd.chswitch_work, ieee80211_chswitch_work);
6283 timer_setup(&link->u.mgd.chswitch_timer, ieee80211_chswitch_timer, 0);
6285 if (sdata->u.mgd.assoc_data)
6286 ether_addr_copy(link->conf->addr,
6287 sdata->u.mgd.assoc_data->link[link_id].addr);
6290 /* scan finished notification */
6291 void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
6293 struct ieee80211_sub_if_data *sdata;
6295 /* Restart STA timers */
6297 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
6298 if (ieee80211_sdata_running(sdata))
6299 ieee80211_restart_sta_timer(sdata);
6304 static bool ieee80211_get_dtim(const struct cfg80211_bss_ies *ies,
6305 u8 *dtim_count, u8 *dtim_period)
6307 const u8 *tim_ie = cfg80211_find_ie(WLAN_EID_TIM, ies->data, ies->len);
6308 const u8 *idx_ie = cfg80211_find_ie(WLAN_EID_MULTI_BSSID_IDX, ies->data,
6310 const struct ieee80211_tim_ie *tim = NULL;
6311 const struct ieee80211_bssid_index *idx;
6312 bool valid = tim_ie && tim_ie[1] >= 2;
6315 tim = (void *)(tim_ie + 2);
6318 *dtim_count = valid ? tim->dtim_count : 0;
6321 *dtim_period = valid ? tim->dtim_period : 0;
6323 /* Check if value is overridden by non-transmitted profile */
6324 if (!idx_ie || idx_ie[1] < 3)
6327 idx = (void *)(idx_ie + 2);
6330 *dtim_count = idx->dtim_count;
6333 *dtim_period = idx->dtim_period;
6338 static int ieee80211_prep_connection(struct ieee80211_sub_if_data *sdata,
6339 struct cfg80211_bss *cbss, s8 link_id,
6340 const u8 *ap_mld_addr, bool assoc,
6343 struct ieee80211_local *local = sdata->local;
6344 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
6345 struct ieee80211_bss *bss = (void *)cbss->priv;
6346 struct sta_info *new_sta = NULL;
6347 struct ieee80211_link_data *link;
6348 bool have_sta = false;
6354 if (WARN_ON(!ap_mld_addr))
6356 err = ieee80211_vif_set_links(sdata, BIT(link_id));
6358 if (WARN_ON(ap_mld_addr))
6360 ap_mld_addr = cbss->bssid;
6361 err = ieee80211_vif_set_links(sdata, 0);
6369 link = sdata_dereference(sdata->link[link_id], sdata);
6370 if (WARN_ON(!link)) {
6375 if (mlo && !is_valid_ether_addr(link->conf->addr))
6376 eth_random_addr(link->conf->addr);
6378 if (WARN_ON(!ifmgd->auth_data && !ifmgd->assoc_data)) {
6383 /* If a reconfig is happening, bail out */
6384 if (local->in_reconfig) {
6391 have_sta = sta_info_get(sdata, ap_mld_addr);
6397 new_sta = sta_info_alloc_with_link(sdata, ap_mld_addr,
6398 link_id, cbss->bssid,
6401 new_sta = sta_info_alloc(sdata, ap_mld_addr, GFP_KERNEL);
6408 new_sta->sta.mlo = mlo;
6412 * Set up the information for the new channel before setting the
6413 * new channel. We can't - completely race-free - change the basic
6414 * rates bitmap and the channel (sband) that it refers to, but if
6415 * we set it up before we at least avoid calling into the driver's
6416 * bss_info_changed() method with invalid information (since we do
6417 * call that from changing the channel - only for IDLE and perhaps
6418 * some others, but ...).
6420 * So to avoid that, just set up all the new information before the
6421 * channel, but tell the driver to apply it only afterwards, since
6422 * it might need the new channel for that.
6425 const struct cfg80211_bss_ies *ies;
6426 struct link_sta_info *link_sta;
6429 link_sta = rcu_dereference(new_sta->link[link_id]);
6430 if (WARN_ON(!link_sta)) {
6432 sta_info_free(local, new_sta);
6437 err = ieee80211_mgd_setup_link_sta(link, new_sta,
6441 sta_info_free(local, new_sta);
6445 memcpy(link->u.mgd.bssid, cbss->bssid, ETH_ALEN);
6447 /* set timing information */
6448 link->conf->beacon_int = cbss->beacon_interval;
6449 ies = rcu_dereference(cbss->beacon_ies);
6451 link->conf->sync_tsf = ies->tsf;
6452 link->conf->sync_device_ts =
6453 bss->device_ts_beacon;
6455 ieee80211_get_dtim(ies,
6456 &link->conf->sync_dtim_count,
6458 } else if (!ieee80211_hw_check(&sdata->local->hw,
6459 TIMING_BEACON_ONLY)) {
6460 ies = rcu_dereference(cbss->proberesp_ies);
6461 /* must be non-NULL since beacon IEs were NULL */
6462 link->conf->sync_tsf = ies->tsf;
6463 link->conf->sync_device_ts =
6464 bss->device_ts_presp;
6465 link->conf->sync_dtim_count = 0;
6467 link->conf->sync_tsf = 0;
6468 link->conf->sync_device_ts = 0;
6469 link->conf->sync_dtim_count = 0;
6474 if (new_sta || override) {
6475 err = ieee80211_prep_channel(sdata, link, cbss,
6476 &link->u.mgd.conn_flags);
6479 sta_info_free(local, new_sta);
6486 * tell driver about BSSID, basic rates and timing
6487 * this was set up above, before setting the channel
6489 ieee80211_link_info_change_notify(sdata, link,
6491 BSS_CHANGED_BASIC_RATES |
6492 BSS_CHANGED_BEACON_INT);
6495 sta_info_pre_move_state(new_sta, IEEE80211_STA_AUTH);
6497 err = sta_info_insert(new_sta);
6501 "failed to insert STA entry for the AP (error %d)\n",
6506 WARN_ON_ONCE(!ether_addr_equal(link->u.mgd.bssid, cbss->bssid));
6508 /* Cancel scan to ensure that nothing interferes with connection */
6509 if (local->scanning)
6510 ieee80211_scan_cancel(local);
6515 ieee80211_link_release_channel(&sdata->deflink);
6516 ieee80211_vif_set_links(sdata, 0);
6521 int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
6522 struct cfg80211_auth_request *req)
6524 struct ieee80211_local *local = sdata->local;
6525 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
6526 struct ieee80211_mgd_auth_data *auth_data;
6531 /* prepare auth data structure */
6533 switch (req->auth_type) {
6534 case NL80211_AUTHTYPE_OPEN_SYSTEM:
6535 auth_alg = WLAN_AUTH_OPEN;
6537 case NL80211_AUTHTYPE_SHARED_KEY:
6540 auth_alg = WLAN_AUTH_SHARED_KEY;
6542 case NL80211_AUTHTYPE_FT:
6543 auth_alg = WLAN_AUTH_FT;
6545 case NL80211_AUTHTYPE_NETWORK_EAP:
6546 auth_alg = WLAN_AUTH_LEAP;
6548 case NL80211_AUTHTYPE_SAE:
6549 auth_alg = WLAN_AUTH_SAE;
6551 case NL80211_AUTHTYPE_FILS_SK:
6552 auth_alg = WLAN_AUTH_FILS_SK;
6554 case NL80211_AUTHTYPE_FILS_SK_PFS:
6555 auth_alg = WLAN_AUTH_FILS_SK_PFS;
6557 case NL80211_AUTHTYPE_FILS_PK:
6558 auth_alg = WLAN_AUTH_FILS_PK;
6564 if (ifmgd->assoc_data)
6567 auth_data = kzalloc(sizeof(*auth_data) + req->auth_data_len +
6568 req->ie_len, GFP_KERNEL);
6572 memcpy(auth_data->ap_addr,
6573 req->ap_mld_addr ?: req->bss->bssid,
6575 auth_data->bss = req->bss;
6577 if (req->auth_data_len >= 4) {
6578 if (req->auth_type == NL80211_AUTHTYPE_SAE) {
6579 __le16 *pos = (__le16 *) req->auth_data;
6581 auth_data->sae_trans = le16_to_cpu(pos[0]);
6582 auth_data->sae_status = le16_to_cpu(pos[1]);
6584 memcpy(auth_data->data, req->auth_data + 4,
6585 req->auth_data_len - 4);
6586 auth_data->data_len += req->auth_data_len - 4;
6589 /* Check if continuing authentication or trying to authenticate with the
6590 * same BSS that we were in the process of authenticating with and avoid
6591 * removal and re-addition of the STA entry in
6592 * ieee80211_prep_connection().
6594 cont_auth = ifmgd->auth_data && req->bss == ifmgd->auth_data->bss;
6596 if (req->ie && req->ie_len) {
6597 memcpy(&auth_data->data[auth_data->data_len],
6598 req->ie, req->ie_len);
6599 auth_data->data_len += req->ie_len;
6602 if (req->key && req->key_len) {
6603 auth_data->key_len = req->key_len;
6604 auth_data->key_idx = req->key_idx;
6605 memcpy(auth_data->key, req->key, req->key_len);
6608 auth_data->algorithm = auth_alg;
6610 /* try to authenticate/probe */
6612 if (ifmgd->auth_data) {
6613 if (cont_auth && req->auth_type == NL80211_AUTHTYPE_SAE) {
6614 auth_data->peer_confirmed =
6615 ifmgd->auth_data->peer_confirmed;
6617 ieee80211_destroy_auth_data(sdata, cont_auth);
6620 /* prep auth_data so we don't go into idle on disassoc */
6621 ifmgd->auth_data = auth_data;
6623 /* If this is continuation of an ongoing SAE authentication exchange
6624 * (i.e., request to send SAE Confirm) and the peer has already
6625 * confirmed, mark authentication completed since we are about to send
6628 if (cont_auth && req->auth_type == NL80211_AUTHTYPE_SAE &&
6629 auth_data->peer_confirmed && auth_data->sae_trans == 2)
6630 ieee80211_mark_sta_auth(sdata);
6632 if (ifmgd->associated) {
6633 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
6636 "disconnect from AP %pM for new auth to %pM\n",
6637 sdata->vif.cfg.ap_addr, auth_data->ap_addr);
6638 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
6639 WLAN_REASON_UNSPECIFIED,
6642 ieee80211_report_disconnect(sdata, frame_buf,
6643 sizeof(frame_buf), true,
6644 WLAN_REASON_UNSPECIFIED,
6648 sdata_info(sdata, "authenticate with %pM\n", auth_data->ap_addr);
6650 /* needed for transmitting the auth frame(s) properly */
6651 memcpy(sdata->vif.cfg.ap_addr, auth_data->ap_addr, ETH_ALEN);
6653 err = ieee80211_prep_connection(sdata, req->bss, req->link_id,
6654 req->ap_mld_addr, cont_auth, false);
6658 err = ieee80211_auth(sdata);
6660 sta_info_destroy_addr(sdata, auth_data->ap_addr);
6664 /* hold our own reference */
6665 cfg80211_ref_bss(local->hw.wiphy, auth_data->bss);
6669 if (!sdata->vif.valid_links) {
6670 eth_zero_addr(sdata->deflink.u.mgd.bssid);
6671 ieee80211_link_info_change_notify(sdata, &sdata->deflink,
6673 mutex_lock(&sdata->local->mtx);
6674 ieee80211_link_release_channel(&sdata->deflink);
6675 mutex_unlock(&sdata->local->mtx);
6677 ifmgd->auth_data = NULL;
6682 static ieee80211_conn_flags_t
6683 ieee80211_setup_assoc_link(struct ieee80211_sub_if_data *sdata,
6684 struct ieee80211_mgd_assoc_data *assoc_data,
6685 struct cfg80211_assoc_request *req,
6686 ieee80211_conn_flags_t conn_flags,
6687 unsigned int link_id)
6689 struct ieee80211_local *local = sdata->local;
6690 const struct cfg80211_bss_ies *beacon_ies;
6691 struct ieee80211_supported_band *sband;
6692 const struct element *ht_elem, *vht_elem;
6693 struct ieee80211_link_data *link;
6694 struct cfg80211_bss *cbss;
6695 struct ieee80211_bss *bss;
6696 bool is_5ghz, is_6ghz;
6698 cbss = assoc_data->link[link_id].bss;
6702 bss = (void *)cbss->priv;
6704 sband = local->hw.wiphy->bands[cbss->channel->band];
6705 if (WARN_ON(!sband))
6708 link = sdata_dereference(sdata->link[link_id], sdata);
6712 is_5ghz = cbss->channel->band == NL80211_BAND_5GHZ;
6713 is_6ghz = cbss->channel->band == NL80211_BAND_6GHZ;
6715 /* for MLO connections assume advertising all rates is OK */
6716 if (!req->ap_mld_addr) {
6717 assoc_data->supp_rates = bss->supp_rates;
6718 assoc_data->supp_rates_len = bss->supp_rates_len;
6721 /* copy and link elems for the STA profile */
6722 if (req->links[link_id].elems_len) {
6723 memcpy(assoc_data->ie_pos, req->links[link_id].elems,
6724 req->links[link_id].elems_len);
6725 assoc_data->link[link_id].elems = assoc_data->ie_pos;
6726 assoc_data->link[link_id].elems_len = req->links[link_id].elems_len;
6727 assoc_data->ie_pos += req->links[link_id].elems_len;
6731 ht_elem = ieee80211_bss_get_elem(cbss, WLAN_EID_HT_OPERATION);
6732 if (ht_elem && ht_elem->datalen >= sizeof(struct ieee80211_ht_operation))
6733 assoc_data->link[link_id].ap_ht_param =
6734 ((struct ieee80211_ht_operation *)(ht_elem->data))->ht_param;
6736 conn_flags |= IEEE80211_CONN_DISABLE_HT;
6737 vht_elem = ieee80211_bss_get_elem(cbss, WLAN_EID_VHT_CAPABILITY);
6738 if (vht_elem && vht_elem->datalen >= sizeof(struct ieee80211_vht_cap)) {
6739 memcpy(&assoc_data->link[link_id].ap_vht_cap, vht_elem->data,
6740 sizeof(struct ieee80211_vht_cap));
6741 } else if (is_5ghz) {
6743 "VHT capa missing/short, disabling VHT/HE/EHT\n");
6744 conn_flags |= IEEE80211_CONN_DISABLE_VHT |
6745 IEEE80211_CONN_DISABLE_HE |
6746 IEEE80211_CONN_DISABLE_EHT;
6750 link->u.mgd.beacon_crc_valid = false;
6751 link->u.mgd.dtim_period = 0;
6752 link->u.mgd.have_beacon = false;
6754 /* override HT/VHT configuration only if the AP and we support it */
6755 if (!(conn_flags & IEEE80211_CONN_DISABLE_HT)) {
6756 struct ieee80211_sta_ht_cap sta_ht_cap;
6758 memcpy(&sta_ht_cap, &sband->ht_cap, sizeof(sta_ht_cap));
6759 ieee80211_apply_htcap_overrides(sdata, &sta_ht_cap);
6763 beacon_ies = rcu_dereference(cbss->beacon_ies);
6765 const struct element *elem;
6768 ieee80211_get_dtim(beacon_ies, &dtim_count,
6769 &link->u.mgd.dtim_period);
6771 sdata->deflink.u.mgd.have_beacon = true;
6773 if (ieee80211_hw_check(&local->hw, TIMING_BEACON_ONLY)) {
6774 link->conf->sync_tsf = beacon_ies->tsf;
6775 link->conf->sync_device_ts = bss->device_ts_beacon;
6776 link->conf->sync_dtim_count = dtim_count;
6779 elem = cfg80211_find_ext_elem(WLAN_EID_EXT_MULTIPLE_BSSID_CONFIGURATION,
6780 beacon_ies->data, beacon_ies->len);
6781 if (elem && elem->datalen >= 3)
6782 link->conf->profile_periodicity = elem->data[2];
6784 link->conf->profile_periodicity = 0;
6786 elem = cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY,
6787 beacon_ies->data, beacon_ies->len);
6788 if (elem && elem->datalen >= 11 &&
6789 (elem->data[10] & WLAN_EXT_CAPA11_EMA_SUPPORT))
6790 link->conf->ema_ap = true;
6792 link->conf->ema_ap = false;
6796 if (bss->corrupt_data) {
6797 char *corrupt_type = "data";
6799 if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_BEACON) {
6800 if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_PROBE_RESP)
6801 corrupt_type = "beacon and probe response";
6803 corrupt_type = "beacon";
6804 } else if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_PROBE_RESP) {
6805 corrupt_type = "probe response";
6807 sdata_info(sdata, "associating to AP %pM with corrupt %s\n",
6808 cbss->bssid, corrupt_type);
6811 if (link->u.mgd.req_smps == IEEE80211_SMPS_AUTOMATIC) {
6812 if (sdata->u.mgd.powersave)
6813 link->smps_mode = IEEE80211_SMPS_DYNAMIC;
6815 link->smps_mode = IEEE80211_SMPS_OFF;
6817 link->smps_mode = link->u.mgd.req_smps;
6823 int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
6824 struct cfg80211_assoc_request *req)
6826 unsigned int assoc_link_id = req->link_id < 0 ? 0 : req->link_id;
6827 struct ieee80211_local *local = sdata->local;
6828 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
6829 struct ieee80211_mgd_assoc_data *assoc_data;
6830 const struct element *ssid_elem;
6831 struct ieee80211_vif_cfg *vif_cfg = &sdata->vif.cfg;
6832 ieee80211_conn_flags_t conn_flags = 0;
6833 struct ieee80211_link_data *link;
6834 struct cfg80211_bss *cbss;
6835 struct ieee80211_bss *bss;
6838 size_t size = sizeof(*assoc_data) + req->ie_len;
6840 for (i = 0; i < IEEE80211_MLD_MAX_NUM_LINKS; i++)
6841 size += req->links[i].elems_len;
6843 if (req->ap_mld_addr) {
6844 for (i = 0; i < IEEE80211_MLD_MAX_NUM_LINKS; i++) {
6845 if (!req->links[i].bss)
6847 if (i == assoc_link_id)
6850 * For now, support only a single link in MLO, we
6851 * don't have the necessary parsing of the multi-
6852 * link element in the association response, etc.
6855 "refusing MLO association with >1 links\n");
6860 assoc_data = kzalloc(size, GFP_KERNEL);
6864 cbss = req->link_id < 0 ? req->bss : req->links[req->link_id].bss;
6867 ssid_elem = ieee80211_bss_get_elem(cbss, WLAN_EID_SSID);
6868 if (!ssid_elem || ssid_elem->datalen > sizeof(assoc_data->ssid)) {
6873 memcpy(assoc_data->ssid, ssid_elem->data, ssid_elem->datalen);
6874 assoc_data->ssid_len = ssid_elem->datalen;
6875 memcpy(vif_cfg->ssid, assoc_data->ssid, assoc_data->ssid_len);
6876 vif_cfg->ssid_len = assoc_data->ssid_len;
6879 if (req->ap_mld_addr) {
6880 for (i = 0; i < IEEE80211_MLD_MAX_NUM_LINKS; i++) {
6881 if (!req->links[i].bss)
6883 link = sdata_dereference(sdata->link[i], sdata);
6885 ether_addr_copy(assoc_data->link[i].addr,
6888 eth_random_addr(assoc_data->link[i].addr);
6891 memcpy(assoc_data->link[0].addr, sdata->vif.addr, ETH_ALEN);
6894 assoc_data->s1g = cbss->channel->band == NL80211_BAND_S1GHZ;
6896 memcpy(assoc_data->ap_addr,
6897 req->ap_mld_addr ?: req->bss->bssid,
6900 if (ifmgd->associated) {
6901 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
6904 "disconnect from AP %pM for new assoc to %pM\n",
6905 sdata->vif.cfg.ap_addr, assoc_data->ap_addr);
6906 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
6907 WLAN_REASON_UNSPECIFIED,
6910 ieee80211_report_disconnect(sdata, frame_buf,
6911 sizeof(frame_buf), true,
6912 WLAN_REASON_UNSPECIFIED,
6916 if (ifmgd->auth_data && !ifmgd->auth_data->done) {
6921 if (ifmgd->assoc_data) {
6926 if (ifmgd->auth_data) {
6929 /* keep sta info, bssid if matching */
6930 match = ether_addr_equal(ifmgd->auth_data->ap_addr,
6931 assoc_data->ap_addr);
6932 ieee80211_destroy_auth_data(sdata, match);
6935 /* prepare assoc data */
6937 bss = (void *)cbss->priv;
6938 assoc_data->wmm = bss->wmm_used &&
6939 (local->hw.queues >= IEEE80211_NUM_ACS);
6942 * IEEE802.11n does not allow TKIP/WEP as pairwise ciphers in HT mode.
6943 * We still associate in non-HT mode (11a/b/g) if any one of these
6944 * ciphers is configured as pairwise.
6945 * We can set this to true for non-11n hardware, that'll be checked
6946 * separately along with the peer capabilities.
6948 for (i = 0; i < req->crypto.n_ciphers_pairwise; i++) {
6949 if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
6950 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
6951 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104) {
6952 conn_flags |= IEEE80211_CONN_DISABLE_HT;
6953 conn_flags |= IEEE80211_CONN_DISABLE_VHT;
6954 conn_flags |= IEEE80211_CONN_DISABLE_HE;
6955 conn_flags |= IEEE80211_CONN_DISABLE_EHT;
6956 netdev_info(sdata->dev,
6957 "disabling HT/VHT/HE due to WEP/TKIP use\n");
6961 /* also disable HT/VHT/HE/EHT if the AP doesn't use WMM */
6962 if (!bss->wmm_used) {
6963 conn_flags |= IEEE80211_CONN_DISABLE_HT;
6964 conn_flags |= IEEE80211_CONN_DISABLE_VHT;
6965 conn_flags |= IEEE80211_CONN_DISABLE_HE;
6966 conn_flags |= IEEE80211_CONN_DISABLE_EHT;
6967 netdev_info(sdata->dev,
6968 "disabling HT/VHT/HE as WMM/QoS is not supported by the AP\n");
6971 if (req->flags & ASSOC_REQ_DISABLE_HT) {
6972 mlme_dbg(sdata, "HT disabled by flag, disabling HT/VHT/HE\n");
6973 conn_flags |= IEEE80211_CONN_DISABLE_HT;
6974 conn_flags |= IEEE80211_CONN_DISABLE_VHT;
6975 conn_flags |= IEEE80211_CONN_DISABLE_HE;
6976 conn_flags |= IEEE80211_CONN_DISABLE_EHT;
6979 if (req->flags & ASSOC_REQ_DISABLE_VHT) {
6980 mlme_dbg(sdata, "VHT disabled by flag, disabling VHT\n");
6981 conn_flags |= IEEE80211_CONN_DISABLE_VHT;
6984 if (req->flags & ASSOC_REQ_DISABLE_HE) {
6985 mlme_dbg(sdata, "HE disabled by flag, disabling HE/EHT\n");
6986 conn_flags |= IEEE80211_CONN_DISABLE_HE;
6987 conn_flags |= IEEE80211_CONN_DISABLE_EHT;
6990 if (req->flags & ASSOC_REQ_DISABLE_EHT)
6991 conn_flags |= IEEE80211_CONN_DISABLE_EHT;
6993 memcpy(&ifmgd->ht_capa, &req->ht_capa, sizeof(ifmgd->ht_capa));
6994 memcpy(&ifmgd->ht_capa_mask, &req->ht_capa_mask,
6995 sizeof(ifmgd->ht_capa_mask));
6997 memcpy(&ifmgd->vht_capa, &req->vht_capa, sizeof(ifmgd->vht_capa));
6998 memcpy(&ifmgd->vht_capa_mask, &req->vht_capa_mask,
6999 sizeof(ifmgd->vht_capa_mask));
7001 memcpy(&ifmgd->s1g_capa, &req->s1g_capa, sizeof(ifmgd->s1g_capa));
7002 memcpy(&ifmgd->s1g_capa_mask, &req->s1g_capa_mask,
7003 sizeof(ifmgd->s1g_capa_mask));
7005 if (req->ie && req->ie_len) {
7006 memcpy(assoc_data->ie, req->ie, req->ie_len);
7007 assoc_data->ie_len = req->ie_len;
7008 assoc_data->ie_pos = assoc_data->ie + assoc_data->ie_len;
7010 assoc_data->ie_pos = assoc_data->ie;
7013 if (req->fils_kek) {
7014 /* should already be checked in cfg80211 - so warn */
7015 if (WARN_ON(req->fils_kek_len > FILS_MAX_KEK_LEN)) {
7019 memcpy(assoc_data->fils_kek, req->fils_kek,
7021 assoc_data->fils_kek_len = req->fils_kek_len;
7024 if (req->fils_nonces)
7025 memcpy(assoc_data->fils_nonces, req->fils_nonces,
7026 2 * FILS_NONCE_LEN);
7028 /* default timeout */
7029 assoc_data->timeout = jiffies;
7030 assoc_data->timeout_started = true;
7032 assoc_data->assoc_link_id = assoc_link_id;
7034 if (req->ap_mld_addr) {
7035 for (i = 0; i < ARRAY_SIZE(assoc_data->link); i++) {
7036 assoc_data->link[i].conn_flags = conn_flags;
7037 assoc_data->link[i].bss = req->links[i].bss;
7040 /* if there was no authentication, set up the link */
7041 err = ieee80211_vif_set_links(sdata, BIT(assoc_link_id));
7045 assoc_data->link[0].conn_flags = conn_flags;
7046 assoc_data->link[0].bss = cbss;
7049 link = sdata_dereference(sdata->link[assoc_link_id], sdata);
7050 if (WARN_ON(!link)) {
7055 /* keep old conn_flags from ieee80211_prep_channel() from auth */
7056 conn_flags |= link->u.mgd.conn_flags;
7057 conn_flags |= ieee80211_setup_assoc_link(sdata, assoc_data, req,
7058 conn_flags, assoc_link_id);
7059 override = link->u.mgd.conn_flags != conn_flags;
7060 link->u.mgd.conn_flags |= conn_flags;
7062 if (WARN((sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_UAPSD) &&
7063 ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK),
7064 "U-APSD not supported with HW_PS_NULLFUNC_STACK\n"))
7065 sdata->vif.driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
7067 if (bss->wmm_used && bss->uapsd_supported &&
7068 (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_UAPSD)) {
7069 assoc_data->uapsd = true;
7070 ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED;
7072 assoc_data->uapsd = false;
7073 ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED;
7076 if (req->prev_bssid)
7077 memcpy(assoc_data->prev_ap_addr, req->prev_bssid, ETH_ALEN);
7080 ifmgd->mfp = IEEE80211_MFP_REQUIRED;
7081 ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
7083 ifmgd->mfp = IEEE80211_MFP_DISABLED;
7084 ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
7087 if (req->flags & ASSOC_REQ_USE_RRM)
7088 ifmgd->flags |= IEEE80211_STA_ENABLE_RRM;
7090 ifmgd->flags &= ~IEEE80211_STA_ENABLE_RRM;
7092 if (req->crypto.control_port)
7093 ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
7095 ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
7097 sdata->control_port_protocol = req->crypto.control_port_ethertype;
7098 sdata->control_port_no_encrypt = req->crypto.control_port_no_encrypt;
7099 sdata->control_port_over_nl80211 =
7100 req->crypto.control_port_over_nl80211;
7101 sdata->control_port_no_preauth = req->crypto.control_port_no_preauth;
7103 /* kick off associate process */
7104 ifmgd->assoc_data = assoc_data;
7106 for (i = 0; i < ARRAY_SIZE(assoc_data->link); i++) {
7107 if (!assoc_data->link[i].bss)
7109 if (i == assoc_data->assoc_link_id)
7111 /* only calculate the flags, hence link == NULL */
7112 err = ieee80211_prep_channel(sdata, NULL, assoc_data->link[i].bss,
7113 &assoc_data->link[i].conn_flags);
7118 /* needed for transmitting the assoc frames properly */
7119 memcpy(sdata->vif.cfg.ap_addr, assoc_data->ap_addr, ETH_ALEN);
7121 err = ieee80211_prep_connection(sdata, cbss, req->link_id,
7122 req->ap_mld_addr, true, override);
7126 assoc_data->link[assoc_data->assoc_link_id].conn_flags =
7127 link->u.mgd.conn_flags;
7129 if (ieee80211_hw_check(&sdata->local->hw, NEED_DTIM_BEFORE_ASSOC)) {
7130 const struct cfg80211_bss_ies *beacon_ies;
7133 beacon_ies = rcu_dereference(req->bss->beacon_ies);
7137 * Wait up to one beacon interval ...
7138 * should this be more if we miss one?
7140 sdata_info(sdata, "waiting for beacon from %pM\n",
7142 assoc_data->timeout = TU_TO_EXP_TIME(req->bss->beacon_interval);
7143 assoc_data->timeout_started = true;
7144 assoc_data->need_beacon = true;
7149 run_again(sdata, assoc_data->timeout);
7153 eth_zero_addr(sdata->deflink.u.mgd.bssid);
7154 ieee80211_link_info_change_notify(sdata, &sdata->deflink,
7156 ifmgd->assoc_data = NULL;
7162 int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
7163 struct cfg80211_deauth_request *req)
7165 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
7166 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
7167 bool tx = !req->local_state_change;
7168 struct ieee80211_prep_tx_info info = {
7169 .subtype = IEEE80211_STYPE_DEAUTH,
7172 if (ifmgd->auth_data &&
7173 ether_addr_equal(ifmgd->auth_data->ap_addr, req->bssid)) {
7175 "aborting authentication with %pM by local choice (Reason: %u=%s)\n",
7176 req->bssid, req->reason_code,
7177 ieee80211_get_reason_code_string(req->reason_code));
7179 drv_mgd_prepare_tx(sdata->local, sdata, &info);
7180 ieee80211_send_deauth_disassoc(sdata, req->bssid, req->bssid,
7181 IEEE80211_STYPE_DEAUTH,
7182 req->reason_code, tx,
7184 ieee80211_destroy_auth_data(sdata, false);
7185 ieee80211_report_disconnect(sdata, frame_buf,
7186 sizeof(frame_buf), true,
7187 req->reason_code, false);
7188 drv_mgd_complete_tx(sdata->local, sdata, &info);
7192 if (ifmgd->assoc_data &&
7193 ether_addr_equal(ifmgd->assoc_data->ap_addr, req->bssid)) {
7195 "aborting association with %pM by local choice (Reason: %u=%s)\n",
7196 req->bssid, req->reason_code,
7197 ieee80211_get_reason_code_string(req->reason_code));
7199 drv_mgd_prepare_tx(sdata->local, sdata, &info);
7200 ieee80211_send_deauth_disassoc(sdata, req->bssid, req->bssid,
7201 IEEE80211_STYPE_DEAUTH,
7202 req->reason_code, tx,
7204 ieee80211_destroy_assoc_data(sdata, ASSOC_ABANDON);
7205 ieee80211_report_disconnect(sdata, frame_buf,
7206 sizeof(frame_buf), true,
7207 req->reason_code, false);
7211 if (ifmgd->associated &&
7212 ether_addr_equal(sdata->vif.cfg.ap_addr, req->bssid)) {
7214 "deauthenticating from %pM by local choice (Reason: %u=%s)\n",
7215 req->bssid, req->reason_code,
7216 ieee80211_get_reason_code_string(req->reason_code));
7218 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
7219 req->reason_code, tx, frame_buf);
7220 ieee80211_report_disconnect(sdata, frame_buf,
7221 sizeof(frame_buf), true,
7222 req->reason_code, false);
7223 drv_mgd_complete_tx(sdata->local, sdata, &info);
7230 int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
7231 struct cfg80211_disassoc_request *req)
7233 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
7235 if (!sdata->u.mgd.associated ||
7236 memcmp(sdata->vif.cfg.ap_addr, req->ap_addr, ETH_ALEN))
7240 "disassociating from %pM by local choice (Reason: %u=%s)\n",
7241 req->ap_addr, req->reason_code,
7242 ieee80211_get_reason_code_string(req->reason_code));
7244 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DISASSOC,
7245 req->reason_code, !req->local_state_change,
7248 ieee80211_report_disconnect(sdata, frame_buf, sizeof(frame_buf), true,
7249 req->reason_code, false);
7254 void ieee80211_mgd_stop_link(struct ieee80211_link_data *link)
7256 cancel_work_sync(&link->u.mgd.request_smps_work);
7257 cancel_work_sync(&link->u.mgd.chswitch_work);
7260 void ieee80211_mgd_stop(struct ieee80211_sub_if_data *sdata)
7262 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
7265 * Make sure some work items will not run after this,
7266 * they will not do anything but might not have been
7267 * cancelled when disconnecting.
7269 cancel_work_sync(&ifmgd->monitor_work);
7270 cancel_work_sync(&ifmgd->beacon_connection_loss_work);
7271 cancel_work_sync(&ifmgd->csa_connection_drop_work);
7272 cancel_delayed_work_sync(&ifmgd->tdls_peer_del_work);
7275 if (ifmgd->assoc_data)
7276 ieee80211_destroy_assoc_data(sdata, ASSOC_TIMEOUT);
7277 if (ifmgd->auth_data)
7278 ieee80211_destroy_auth_data(sdata, false);
7279 spin_lock_bh(&ifmgd->teardown_lock);
7280 if (ifmgd->teardown_skb) {
7281 kfree_skb(ifmgd->teardown_skb);
7282 ifmgd->teardown_skb = NULL;
7283 ifmgd->orig_teardown_skb = NULL;
7285 kfree(ifmgd->assoc_req_ies);
7286 ifmgd->assoc_req_ies = NULL;
7287 ifmgd->assoc_req_ies_len = 0;
7288 spin_unlock_bh(&ifmgd->teardown_lock);
7289 del_timer_sync(&ifmgd->timer);
7290 sdata_unlock(sdata);
7293 void ieee80211_cqm_rssi_notify(struct ieee80211_vif *vif,
7294 enum nl80211_cqm_rssi_threshold_event rssi_event,
7298 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
7300 trace_api_cqm_rssi_notify(sdata, rssi_event, rssi_level);
7302 cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, rssi_level, gfp);
7304 EXPORT_SYMBOL(ieee80211_cqm_rssi_notify);
7306 void ieee80211_cqm_beacon_loss_notify(struct ieee80211_vif *vif, gfp_t gfp)
7308 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
7310 trace_api_cqm_beacon_loss_notify(sdata->local, sdata);
7312 cfg80211_cqm_beacon_loss_notify(sdata->dev, gfp);
7314 EXPORT_SYMBOL(ieee80211_cqm_beacon_loss_notify);
7316 static void _ieee80211_enable_rssi_reports(struct ieee80211_sub_if_data *sdata,
7320 trace_api_enable_rssi_reports(sdata, rssi_min_thold, rssi_max_thold);
7322 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
7326 * Scale up threshold values before storing it, as the RSSI averaging
7327 * algorithm uses a scaled up value as well. Change this scaling
7328 * factor if the RSSI averaging algorithm changes.
7330 sdata->u.mgd.rssi_min_thold = rssi_min_thold*16;
7331 sdata->u.mgd.rssi_max_thold = rssi_max_thold*16;
7334 void ieee80211_enable_rssi_reports(struct ieee80211_vif *vif,
7338 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
7340 WARN_ON(rssi_min_thold == rssi_max_thold ||
7341 rssi_min_thold > rssi_max_thold);
7343 _ieee80211_enable_rssi_reports(sdata, rssi_min_thold,
7346 EXPORT_SYMBOL(ieee80211_enable_rssi_reports);
7348 void ieee80211_disable_rssi_reports(struct ieee80211_vif *vif)
7350 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
7352 _ieee80211_enable_rssi_reports(sdata, 0, 0);
7354 EXPORT_SYMBOL(ieee80211_disable_rssi_reports);