2 * BSS client mode implementation
3 * Copyright 2003-2008, Jouni Malinen <j@w1.fi>
4 * Copyright 2004, Instant802 Networks, Inc.
5 * Copyright 2005, Devicescape Software, Inc.
6 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
7 * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
8 * Copyright 2013-2014 Intel Mobile Communications GmbH
9 * Copyright (C) 2015 - 2017 Intel Deutschland GmbH
10 * Copyright (C) 2018 Intel Corporation
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
17 #include <linux/delay.h>
18 #include <linux/if_ether.h>
19 #include <linux/skbuff.h>
20 #include <linux/if_arp.h>
21 #include <linux/etherdevice.h>
22 #include <linux/moduleparam.h>
23 #include <linux/rtnetlink.h>
24 #include <linux/crc32.h>
25 #include <linux/slab.h>
26 #include <linux/export.h>
27 #include <net/mac80211.h>
28 #include <asm/unaligned.h>
30 #include "ieee80211_i.h"
31 #include "driver-ops.h"
34 #include "fils_aead.h"
36 #define IEEE80211_AUTH_TIMEOUT (HZ / 5)
37 #define IEEE80211_AUTH_TIMEOUT_LONG (HZ / 2)
38 #define IEEE80211_AUTH_TIMEOUT_SHORT (HZ / 10)
39 #define IEEE80211_AUTH_TIMEOUT_SAE (HZ * 2)
40 #define IEEE80211_AUTH_MAX_TRIES 3
41 #define IEEE80211_AUTH_WAIT_ASSOC (HZ * 5)
42 #define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
43 #define IEEE80211_ASSOC_TIMEOUT_LONG (HZ / 2)
44 #define IEEE80211_ASSOC_TIMEOUT_SHORT (HZ / 10)
45 #define IEEE80211_ASSOC_MAX_TRIES 3
47 static int max_nullfunc_tries = 2;
48 module_param(max_nullfunc_tries, int, 0644);
49 MODULE_PARM_DESC(max_nullfunc_tries,
50 "Maximum nullfunc tx tries before disconnecting (reason 4).");
52 static int max_probe_tries = 5;
53 module_param(max_probe_tries, int, 0644);
54 MODULE_PARM_DESC(max_probe_tries,
55 "Maximum probe tries before disconnecting (reason 4).");
58 * Beacon loss timeout is calculated as N frames times the
59 * advertised beacon interval. This may need to be somewhat
60 * higher than what hardware might detect to account for
61 * delays in the host processing frames. But since we also
62 * probe on beacon miss before declaring the connection lost
63 * default to what we want.
65 static int beacon_loss_count = 7;
66 module_param(beacon_loss_count, int, 0644);
67 MODULE_PARM_DESC(beacon_loss_count,
68 "Number of beacon intervals before we decide beacon was lost.");
71 * Time the connection can be idle before we probe
72 * it to see if we can still talk to the AP.
74 #define IEEE80211_CONNECTION_IDLE_TIME (30 * HZ)
76 * Time we wait for a probe response after sending
77 * a probe request because of beacon loss or for
78 * checking the connection still works.
80 static int probe_wait_ms = 500;
81 module_param(probe_wait_ms, int, 0644);
82 MODULE_PARM_DESC(probe_wait_ms,
83 "Maximum time(ms) to wait for probe response"
84 " before disconnecting (reason 4).");
87 * How many Beacon frames need to have been used in average signal strength
88 * before starting to indicate signal change events.
90 #define IEEE80211_SIGNAL_AVE_MIN_COUNT 4
93 * We can have multiple work items (and connection probing)
94 * scheduling this timer, but we need to take care to only
95 * reschedule it when it should fire _earlier_ than it was
96 * asked for before, or if it's not pending right now. This
97 * function ensures that. Note that it then is required to
98 * run this function for all timeouts after the first one
99 * has happened -- the work that runs from this timer will
102 static void run_again(struct ieee80211_sub_if_data *sdata,
103 unsigned long timeout)
105 sdata_assert_lock(sdata);
107 if (!timer_pending(&sdata->u.mgd.timer) ||
108 time_before(timeout, sdata->u.mgd.timer.expires))
109 mod_timer(&sdata->u.mgd.timer, timeout);
112 void ieee80211_sta_reset_beacon_monitor(struct ieee80211_sub_if_data *sdata)
114 if (sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)
117 if (ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
120 mod_timer(&sdata->u.mgd.bcn_mon_timer,
121 round_jiffies_up(jiffies + sdata->u.mgd.beacon_timeout));
124 void ieee80211_sta_reset_conn_monitor(struct ieee80211_sub_if_data *sdata)
126 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
128 if (unlikely(!ifmgd->associated))
131 if (ifmgd->probe_send_count)
132 ifmgd->probe_send_count = 0;
134 if (ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
137 mod_timer(&ifmgd->conn_mon_timer,
138 round_jiffies_up(jiffies + IEEE80211_CONNECTION_IDLE_TIME));
141 static int ecw2cw(int ecw)
143 return (1 << ecw) - 1;
147 ieee80211_determine_chantype(struct ieee80211_sub_if_data *sdata,
148 struct ieee80211_supported_band *sband,
149 struct ieee80211_channel *channel,
150 const struct ieee80211_ht_operation *ht_oper,
151 const struct ieee80211_vht_operation *vht_oper,
152 const struct ieee80211_he_operation *he_oper,
153 struct cfg80211_chan_def *chandef, bool tracking)
155 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
156 struct cfg80211_chan_def vht_chandef;
157 struct ieee80211_sta_ht_cap sta_ht_cap;
160 memcpy(&sta_ht_cap, &sband->ht_cap, sizeof(sta_ht_cap));
161 ieee80211_apply_htcap_overrides(sdata, &sta_ht_cap);
163 chandef->chan = channel;
164 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
165 chandef->center_freq1 = channel->center_freq;
166 chandef->center_freq2 = 0;
168 if (!ht_oper || !sta_ht_cap.ht_supported) {
169 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
173 chandef->width = NL80211_CHAN_WIDTH_20;
175 ht_cfreq = ieee80211_channel_to_frequency(ht_oper->primary_chan,
177 /* check that channel matches the right operating channel */
178 if (!tracking && channel->center_freq != ht_cfreq) {
180 * It's possible that some APs are confused here;
181 * Netgear WNDR3700 sometimes reports 4 higher than
182 * the actual channel in association responses, but
183 * since we look at probe response/beacon data here
187 "Wrong control channel: center-freq: %d ht-cfreq: %d ht->primary_chan: %d band: %d - Disabling HT\n",
188 channel->center_freq, ht_cfreq,
189 ht_oper->primary_chan, channel->band);
190 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
194 /* check 40 MHz support, if we have it */
195 if (sta_ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) {
196 ieee80211_chandef_ht_oper(ht_oper, chandef);
198 /* 40 MHz (and 80 MHz) must be supported for VHT */
199 ret = IEEE80211_STA_DISABLE_VHT;
200 /* also mark 40 MHz disabled */
201 ret |= IEEE80211_STA_DISABLE_40MHZ;
205 if (!vht_oper || !sband->vht_cap.vht_supported) {
206 ret = IEEE80211_STA_DISABLE_VHT;
210 vht_chandef = *chandef;
211 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE) && he_oper &&
212 (le32_to_cpu(he_oper->he_oper_params) &
213 IEEE80211_HE_OPERATION_VHT_OPER_INFO)) {
214 struct ieee80211_vht_operation he_oper_vht_cap;
217 * Set only first 3 bytes (other 2 aren't used in
218 * ieee80211_chandef_vht_oper() anyway)
220 memcpy(&he_oper_vht_cap, he_oper->optional, 3);
221 he_oper_vht_cap.basic_mcs_set = cpu_to_le16(0);
223 if (!ieee80211_chandef_vht_oper(&he_oper_vht_cap,
225 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE))
227 "HE AP VHT information is invalid, disable HE\n");
228 ret = IEEE80211_STA_DISABLE_HE;
231 } else if (!ieee80211_chandef_vht_oper(vht_oper, &vht_chandef)) {
232 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
234 "AP VHT information is invalid, disable VHT\n");
235 ret = IEEE80211_STA_DISABLE_VHT;
239 if (!cfg80211_chandef_valid(&vht_chandef)) {
240 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
242 "AP VHT information is invalid, disable VHT\n");
243 ret = IEEE80211_STA_DISABLE_VHT;
247 if (cfg80211_chandef_identical(chandef, &vht_chandef)) {
252 if (!cfg80211_chandef_compatible(chandef, &vht_chandef)) {
253 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
255 "AP VHT information doesn't match HT, disable VHT\n");
256 ret = IEEE80211_STA_DISABLE_VHT;
260 *chandef = vht_chandef;
266 * When tracking the current AP, don't do any further checks if the
267 * new chandef is identical to the one we're currently using for the
268 * connection. This keeps us from playing ping-pong with regulatory,
269 * without it the following can happen (for example):
270 * - connect to an AP with 80 MHz, world regdom allows 80 MHz
271 * - AP advertises regdom US
272 * - CRDA loads regdom US with 80 MHz prohibited (old database)
273 * - the code below detects an unsupported channel, downgrades, and
274 * we disconnect from the AP in the caller
275 * - disconnect causes CRDA to reload world regdomain and the game
277 * (see https://bugzilla.kernel.org/show_bug.cgi?id=70881)
279 * It seems possible that there are still scenarios with CSA or real
280 * bandwidth changes where a this could happen, but those cases are
281 * less common and wouldn't completely prevent using the AP.
284 cfg80211_chandef_identical(chandef, &sdata->vif.bss_conf.chandef))
287 /* don't print the message below for VHT mismatch if VHT is disabled */
288 if (ret & IEEE80211_STA_DISABLE_VHT)
289 vht_chandef = *chandef;
292 * Ignore the DISABLED flag when we're already connected and only
293 * tracking the APs beacon for bandwidth changes - otherwise we
294 * might get disconnected here if we connect to an AP, update our
295 * regulatory information based on the AP's country IE and the
296 * information we have is wrong/outdated and disables the channel
297 * that we're actually using for the connection to the AP.
299 while (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
301 IEEE80211_CHAN_DISABLED)) {
302 if (WARN_ON(chandef->width == NL80211_CHAN_WIDTH_20_NOHT)) {
303 ret = IEEE80211_STA_DISABLE_HT |
304 IEEE80211_STA_DISABLE_VHT;
308 ret |= ieee80211_chandef_downgrade(chandef);
311 if (chandef->width != vht_chandef.width && !tracking)
313 "capabilities/regulatory prevented using AP HT/VHT configuration, downgraded\n");
315 WARN_ON_ONCE(!cfg80211_chandef_valid(chandef));
319 static int ieee80211_config_bw(struct ieee80211_sub_if_data *sdata,
320 struct sta_info *sta,
321 const struct ieee80211_ht_cap *ht_cap,
322 const struct ieee80211_ht_operation *ht_oper,
323 const struct ieee80211_vht_operation *vht_oper,
324 const struct ieee80211_he_operation *he_oper,
325 const u8 *bssid, u32 *changed)
327 struct ieee80211_local *local = sdata->local;
328 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
329 struct ieee80211_channel *chan = sdata->vif.bss_conf.chandef.chan;
330 struct ieee80211_supported_band *sband =
331 local->hw.wiphy->bands[chan->band];
332 struct cfg80211_chan_def chandef;
335 enum ieee80211_sta_rx_bandwidth new_sta_bw;
338 /* if HT was/is disabled, don't track any bandwidth changes */
339 if (ifmgd->flags & IEEE80211_STA_DISABLE_HT || !ht_oper)
342 /* don't check VHT if we associated as non-VHT station */
343 if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
346 /* don't check HE if we associated as non-HE station */
347 if (ifmgd->flags & IEEE80211_STA_DISABLE_HE ||
348 !ieee80211_get_he_sta_cap(sband))
351 if (WARN_ON_ONCE(!sta))
355 * if bss configuration changed store the new one -
356 * this may be applicable even if channel is identical
358 ht_opmode = le16_to_cpu(ht_oper->operation_mode);
359 if (sdata->vif.bss_conf.ht_operation_mode != ht_opmode) {
360 *changed |= BSS_CHANGED_HT;
361 sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
364 /* calculate new channel (type) based on HT/VHT/HE operation IEs */
365 flags = ieee80211_determine_chantype(sdata, sband, chan,
366 ht_oper, vht_oper, he_oper,
370 * Downgrade the new channel if we associated with restricted
371 * capabilities. For example, if we associated as a 20 MHz STA
372 * to a 40 MHz AP (due to regulatory, capabilities or config
373 * reasons) then switching to a 40 MHz channel now won't do us
374 * any good -- we couldn't use it with the AP.
376 if (ifmgd->flags & IEEE80211_STA_DISABLE_80P80MHZ &&
377 chandef.width == NL80211_CHAN_WIDTH_80P80)
378 flags |= ieee80211_chandef_downgrade(&chandef);
379 if (ifmgd->flags & IEEE80211_STA_DISABLE_160MHZ &&
380 chandef.width == NL80211_CHAN_WIDTH_160)
381 flags |= ieee80211_chandef_downgrade(&chandef);
382 if (ifmgd->flags & IEEE80211_STA_DISABLE_40MHZ &&
383 chandef.width > NL80211_CHAN_WIDTH_20)
384 flags |= ieee80211_chandef_downgrade(&chandef);
386 if (cfg80211_chandef_identical(&chandef, &sdata->vif.bss_conf.chandef))
390 "AP %pM changed bandwidth, new config is %d MHz, width %d (%d/%d MHz)\n",
391 ifmgd->bssid, chandef.chan->center_freq, chandef.width,
392 chandef.center_freq1, chandef.center_freq2);
394 if (flags != (ifmgd->flags & (IEEE80211_STA_DISABLE_HT |
395 IEEE80211_STA_DISABLE_VHT |
396 IEEE80211_STA_DISABLE_40MHZ |
397 IEEE80211_STA_DISABLE_80P80MHZ |
398 IEEE80211_STA_DISABLE_160MHZ)) ||
399 !cfg80211_chandef_valid(&chandef)) {
401 "AP %pM changed bandwidth in a way we can't support - disconnect\n",
406 switch (chandef.width) {
407 case NL80211_CHAN_WIDTH_20_NOHT:
408 case NL80211_CHAN_WIDTH_20:
409 new_sta_bw = IEEE80211_STA_RX_BW_20;
411 case NL80211_CHAN_WIDTH_40:
412 new_sta_bw = IEEE80211_STA_RX_BW_40;
414 case NL80211_CHAN_WIDTH_80:
415 new_sta_bw = IEEE80211_STA_RX_BW_80;
417 case NL80211_CHAN_WIDTH_80P80:
418 case NL80211_CHAN_WIDTH_160:
419 new_sta_bw = IEEE80211_STA_RX_BW_160;
425 if (new_sta_bw > sta->cur_max_bandwidth)
426 new_sta_bw = sta->cur_max_bandwidth;
428 if (new_sta_bw < sta->sta.bandwidth) {
429 sta->sta.bandwidth = new_sta_bw;
430 rate_control_rate_update(local, sband, sta,
431 IEEE80211_RC_BW_CHANGED);
434 ret = ieee80211_vif_change_bandwidth(sdata, &chandef, changed);
437 "AP %pM changed bandwidth to incompatible one - disconnect\n",
442 if (new_sta_bw > sta->sta.bandwidth) {
443 sta->sta.bandwidth = new_sta_bw;
444 rate_control_rate_update(local, sband, sta,
445 IEEE80211_RC_BW_CHANGED);
451 /* frame sending functions */
453 static void ieee80211_add_ht_ie(struct ieee80211_sub_if_data *sdata,
454 struct sk_buff *skb, u8 ap_ht_param,
455 struct ieee80211_supported_band *sband,
456 struct ieee80211_channel *channel,
457 enum ieee80211_smps_mode smps)
460 u32 flags = channel->flags;
462 struct ieee80211_sta_ht_cap ht_cap;
464 BUILD_BUG_ON(sizeof(ht_cap) != sizeof(sband->ht_cap));
466 memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
467 ieee80211_apply_htcap_overrides(sdata, &ht_cap);
469 /* determine capability flags */
472 switch (ap_ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
473 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
474 if (flags & IEEE80211_CHAN_NO_HT40PLUS) {
475 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
476 cap &= ~IEEE80211_HT_CAP_SGI_40;
479 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
480 if (flags & IEEE80211_CHAN_NO_HT40MINUS) {
481 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
482 cap &= ~IEEE80211_HT_CAP_SGI_40;
488 * If 40 MHz was disabled associate as though we weren't
489 * capable of 40 MHz -- some broken APs will never fall
490 * back to trying to transmit in 20 MHz.
492 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_40MHZ) {
493 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
494 cap &= ~IEEE80211_HT_CAP_SGI_40;
497 /* set SM PS mode properly */
498 cap &= ~IEEE80211_HT_CAP_SM_PS;
500 case IEEE80211_SMPS_AUTOMATIC:
501 case IEEE80211_SMPS_NUM_MODES:
504 case IEEE80211_SMPS_OFF:
505 cap |= WLAN_HT_CAP_SM_PS_DISABLED <<
506 IEEE80211_HT_CAP_SM_PS_SHIFT;
508 case IEEE80211_SMPS_STATIC:
509 cap |= WLAN_HT_CAP_SM_PS_STATIC <<
510 IEEE80211_HT_CAP_SM_PS_SHIFT;
512 case IEEE80211_SMPS_DYNAMIC:
513 cap |= WLAN_HT_CAP_SM_PS_DYNAMIC <<
514 IEEE80211_HT_CAP_SM_PS_SHIFT;
518 /* reserve and fill IE */
519 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
520 ieee80211_ie_build_ht_cap(pos, &ht_cap, cap);
523 /* This function determines vht capability flags for the association
525 * Note - the function may set the owner of the MU-MIMO capability
527 static void ieee80211_add_vht_ie(struct ieee80211_sub_if_data *sdata,
529 struct ieee80211_supported_band *sband,
530 struct ieee80211_vht_cap *ap_vht_cap)
532 struct ieee80211_local *local = sdata->local;
535 struct ieee80211_sta_vht_cap vht_cap;
536 u32 mask, ap_bf_sts, our_bf_sts;
538 BUILD_BUG_ON(sizeof(vht_cap) != sizeof(sband->vht_cap));
540 memcpy(&vht_cap, &sband->vht_cap, sizeof(vht_cap));
541 ieee80211_apply_vhtcap_overrides(sdata, &vht_cap);
543 /* determine capability flags */
546 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_80P80MHZ) {
547 u32 bw = cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
549 cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
550 if (bw == IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ ||
551 bw == IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ)
552 cap |= IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
555 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_160MHZ) {
556 cap &= ~IEEE80211_VHT_CAP_SHORT_GI_160;
557 cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
561 * Some APs apparently get confused if our capabilities are better
562 * than theirs, so restrict what we advertise in the assoc request.
564 if (!(ap_vht_cap->vht_cap_info &
565 cpu_to_le32(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)))
566 cap &= ~(IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
567 IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE);
568 else if (!(ap_vht_cap->vht_cap_info &
569 cpu_to_le32(IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)))
570 cap &= ~IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE;
573 * If some other vif is using the MU-MIMO capablity we cannot associate
574 * using MU-MIMO - this will lead to contradictions in the group-id
576 * Ownership is defined since association request, in order to avoid
577 * simultaneous associations with MU-MIMO.
579 if (cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE) {
580 bool disable_mu_mimo = false;
581 struct ieee80211_sub_if_data *other;
583 list_for_each_entry_rcu(other, &local->interfaces, list) {
584 if (other->vif.mu_mimo_owner) {
585 disable_mu_mimo = true;
590 cap &= ~IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE;
592 sdata->vif.mu_mimo_owner = true;
595 mask = IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
597 ap_bf_sts = le32_to_cpu(ap_vht_cap->vht_cap_info) & mask;
598 our_bf_sts = cap & mask;
600 if (ap_bf_sts < our_bf_sts) {
605 /* reserve and fill IE */
606 pos = skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2);
607 ieee80211_ie_build_vht_cap(pos, &vht_cap, cap);
610 /* This function determines HE capability flags for the association
613 static void ieee80211_add_he_ie(struct ieee80211_sub_if_data *sdata,
615 struct ieee80211_supported_band *sband)
618 const struct ieee80211_sta_he_cap *he_cap = NULL;
621 he_cap = ieee80211_get_he_sta_cap(sband);
626 * TODO: the 1 added is because this temporarily is under the EXTENSION
627 * IE. Get rid of it when it moves.
630 2 + 1 + sizeof(he_cap->he_cap_elem) +
631 ieee80211_he_mcs_nss_size(&he_cap->he_cap_elem) +
632 ieee80211_he_ppe_size(he_cap->ppe_thres[0],
633 he_cap->he_cap_elem.phy_cap_info);
634 pos = skb_put(skb, he_cap_size);
635 ieee80211_ie_build_he_cap(pos, he_cap, pos + he_cap_size);
638 static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata)
640 struct ieee80211_local *local = sdata->local;
641 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
642 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
644 struct ieee80211_mgmt *mgmt;
646 size_t offset = 0, noffset;
647 int i, count, rates_len, supp_rates_len, shift;
649 struct ieee80211_supported_band *sband;
650 struct ieee80211_chanctx_conf *chanctx_conf;
651 struct ieee80211_channel *chan;
654 sdata_assert_lock(sdata);
657 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
658 if (WARN_ON(!chanctx_conf)) {
662 chan = chanctx_conf->def.chan;
664 sband = local->hw.wiphy->bands[chan->band];
665 shift = ieee80211_vif_get_shift(&sdata->vif);
667 if (assoc_data->supp_rates_len) {
669 * Get all rates supported by the device and the AP as
670 * some APs don't like getting a superset of their rates
671 * in the association request (e.g. D-Link DAP 1353 in
674 rates_len = ieee80211_parse_bitrates(&chanctx_conf->def, sband,
675 assoc_data->supp_rates,
676 assoc_data->supp_rates_len,
680 * In case AP not provide any supported rates information
681 * before association, we send information element(s) with
682 * all rates that we support.
685 for (i = 0; i < sband->n_bitrates; i++) {
691 skb = alloc_skb(local->hw.extra_tx_headroom +
692 sizeof(*mgmt) + /* bit too much but doesn't matter */
693 2 + assoc_data->ssid_len + /* SSID */
694 4 + rates_len + /* (extended) rates */
695 4 + /* power capability */
696 2 + 2 * sband->n_channels + /* supported channels */
697 2 + sizeof(struct ieee80211_ht_cap) + /* HT */
698 2 + sizeof(struct ieee80211_vht_cap) + /* VHT */
699 2 + 1 + sizeof(struct ieee80211_he_cap_elem) + /* HE */
700 sizeof(struct ieee80211_he_mcs_nss_supp) +
701 IEEE80211_HE_PPE_THRES_MAX_LEN +
702 assoc_data->ie_len + /* extra IEs */
703 (assoc_data->fils_kek_len ? 16 /* AES-SIV */ : 0) +
709 skb_reserve(skb, local->hw.extra_tx_headroom);
711 capab = WLAN_CAPABILITY_ESS;
713 if (sband->band == NL80211_BAND_2GHZ) {
714 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
715 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
718 if (assoc_data->capability & WLAN_CAPABILITY_PRIVACY)
719 capab |= WLAN_CAPABILITY_PRIVACY;
721 if ((assoc_data->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
722 ieee80211_hw_check(&local->hw, SPECTRUM_MGMT))
723 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
725 if (ifmgd->flags & IEEE80211_STA_ENABLE_RRM)
726 capab |= WLAN_CAPABILITY_RADIO_MEASURE;
728 mgmt = skb_put_zero(skb, 24);
729 memcpy(mgmt->da, assoc_data->bss->bssid, ETH_ALEN);
730 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
731 memcpy(mgmt->bssid, assoc_data->bss->bssid, ETH_ALEN);
733 if (!is_zero_ether_addr(assoc_data->prev_bssid)) {
735 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
736 IEEE80211_STYPE_REASSOC_REQ);
737 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
738 mgmt->u.reassoc_req.listen_interval =
739 cpu_to_le16(local->hw.conf.listen_interval);
740 memcpy(mgmt->u.reassoc_req.current_ap, assoc_data->prev_bssid,
744 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
745 IEEE80211_STYPE_ASSOC_REQ);
746 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
747 mgmt->u.assoc_req.listen_interval =
748 cpu_to_le16(local->hw.conf.listen_interval);
752 pos = skb_put(skb, 2 + assoc_data->ssid_len);
753 *pos++ = WLAN_EID_SSID;
754 *pos++ = assoc_data->ssid_len;
755 memcpy(pos, assoc_data->ssid, assoc_data->ssid_len);
757 /* add all rates which were marked to be used above */
758 supp_rates_len = rates_len;
759 if (supp_rates_len > 8)
762 pos = skb_put(skb, supp_rates_len + 2);
763 *pos++ = WLAN_EID_SUPP_RATES;
764 *pos++ = supp_rates_len;
767 for (i = 0; i < sband->n_bitrates; i++) {
768 if (BIT(i) & rates) {
769 int rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
777 if (rates_len > count) {
778 pos = skb_put(skb, rates_len - count + 2);
779 *pos++ = WLAN_EID_EXT_SUPP_RATES;
780 *pos++ = rates_len - count;
782 for (i++; i < sband->n_bitrates; i++) {
783 if (BIT(i) & rates) {
785 rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
792 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT ||
793 capab & WLAN_CAPABILITY_RADIO_MEASURE) {
794 pos = skb_put(skb, 4);
795 *pos++ = WLAN_EID_PWR_CAPABILITY;
797 *pos++ = 0; /* min tx power */
799 *pos++ = ieee80211_chandef_max_power(&chanctx_conf->def);
802 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
803 /* TODO: get this in reg domain format */
804 pos = skb_put(skb, 2 * sband->n_channels + 2);
805 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
806 *pos++ = 2 * sband->n_channels;
807 for (i = 0; i < sband->n_channels; i++) {
808 *pos++ = ieee80211_frequency_to_channel(
809 sband->channels[i].center_freq);
810 *pos++ = 1; /* one channel in the subband*/
814 /* if present, add any custom IEs that go before HT */
815 if (assoc_data->ie_len) {
816 static const u8 before_ht[] = {
819 WLAN_EID_EXT_SUPP_RATES,
820 WLAN_EID_PWR_CAPABILITY,
821 WLAN_EID_SUPPORTED_CHANNELS,
824 WLAN_EID_RRM_ENABLED_CAPABILITIES,
825 WLAN_EID_MOBILITY_DOMAIN,
826 WLAN_EID_FAST_BSS_TRANSITION, /* reassoc only */
827 WLAN_EID_RIC_DATA, /* reassoc only */
828 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
830 static const u8 after_ric[] = {
831 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
832 WLAN_EID_HT_CAPABILITY,
833 WLAN_EID_BSS_COEX_2040,
834 /* luckily this is almost always there */
835 WLAN_EID_EXT_CAPABILITY,
836 WLAN_EID_QOS_TRAFFIC_CAPA,
837 WLAN_EID_TIM_BCAST_REQ,
838 WLAN_EID_INTERWORKING,
839 /* 60 GHz (Multi-band, DMG, MMS) can't happen */
840 WLAN_EID_VHT_CAPABILITY,
841 WLAN_EID_OPMODE_NOTIF,
844 noffset = ieee80211_ie_split_ric(assoc_data->ie,
847 ARRAY_SIZE(before_ht),
849 ARRAY_SIZE(after_ric),
851 skb_put_data(skb, assoc_data->ie + offset, noffset - offset);
855 if (WARN_ON_ONCE((ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
856 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)))
857 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
859 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
860 ieee80211_add_ht_ie(sdata, skb, assoc_data->ap_ht_param,
861 sband, chan, sdata->smps_mode);
863 /* if present, add any custom IEs that go before VHT */
864 if (assoc_data->ie_len) {
865 static const u8 before_vht[] = {
867 * no need to list the ones split off before HT
870 WLAN_EID_BSS_COEX_2040,
871 WLAN_EID_EXT_CAPABILITY,
872 WLAN_EID_QOS_TRAFFIC_CAPA,
873 WLAN_EID_TIM_BCAST_REQ,
874 WLAN_EID_INTERWORKING,
875 /* 60 GHz (Multi-band, DMG, MMS) can't happen */
878 /* RIC already taken above, so no need to handle here anymore */
879 noffset = ieee80211_ie_split(assoc_data->ie, assoc_data->ie_len,
880 before_vht, ARRAY_SIZE(before_vht),
882 skb_put_data(skb, assoc_data->ie + offset, noffset - offset);
886 /* if present, add any custom IEs that go before HE */
887 if (assoc_data->ie_len) {
888 static const u8 before_he[] = {
890 * no need to list the ones split off before VHT
893 WLAN_EID_OPMODE_NOTIF,
894 WLAN_EID_EXTENSION, WLAN_EID_EXT_FUTURE_CHAN_GUIDANCE,
896 WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_SESSION,
897 WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_PUBLIC_KEY,
898 WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_KEY_CONFIRM,
899 WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_HLP_CONTAINER,
900 WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_IP_ADDR_ASSIGN,
901 /* TODO: add 11ah/11aj/11ak elements */
904 /* RIC already taken above, so no need to handle here anymore */
905 noffset = ieee80211_ie_split(assoc_data->ie, assoc_data->ie_len,
906 before_he, ARRAY_SIZE(before_he),
908 pos = skb_put(skb, noffset - offset);
909 memcpy(pos, assoc_data->ie + offset, noffset - offset);
913 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
914 ieee80211_add_vht_ie(sdata, skb, sband,
915 &assoc_data->ap_vht_cap);
917 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE))
918 ieee80211_add_he_ie(sdata, skb, sband);
920 /* if present, add any custom non-vendor IEs that go after HE */
921 if (assoc_data->ie_len) {
922 noffset = ieee80211_ie_split_vendor(assoc_data->ie,
925 skb_put_data(skb, assoc_data->ie + offset, noffset - offset);
929 if (assoc_data->wmm) {
930 if (assoc_data->uapsd) {
931 qos_info = ifmgd->uapsd_queues;
932 qos_info |= (ifmgd->uapsd_max_sp_len <<
933 IEEE80211_WMM_IE_STA_QOSINFO_SP_SHIFT);
938 pos = ieee80211_add_wmm_info_ie(skb_put(skb, 9), qos_info);
941 /* add any remaining custom (i.e. vendor specific here) IEs */
942 if (assoc_data->ie_len) {
943 noffset = assoc_data->ie_len;
944 skb_put_data(skb, assoc_data->ie + offset, noffset - offset);
947 if (assoc_data->fils_kek_len &&
948 fils_encrypt_assoc_req(skb, assoc_data) < 0) {
953 drv_mgd_prepare_tx(local, sdata, 0);
955 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
956 if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
957 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS |
958 IEEE80211_TX_INTFL_MLME_CONN_TX;
959 ieee80211_tx_skb(sdata, skb);
962 void ieee80211_send_pspoll(struct ieee80211_local *local,
963 struct ieee80211_sub_if_data *sdata)
965 struct ieee80211_pspoll *pspoll;
968 skb = ieee80211_pspoll_get(&local->hw, &sdata->vif);
972 pspoll = (struct ieee80211_pspoll *) skb->data;
973 pspoll->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
975 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
976 ieee80211_tx_skb(sdata, skb);
979 void ieee80211_send_nullfunc(struct ieee80211_local *local,
980 struct ieee80211_sub_if_data *sdata,
984 struct ieee80211_hdr_3addr *nullfunc;
985 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
987 /* Don't send NDPs when STA is connected HE */
988 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
989 !(ifmgd->flags & IEEE80211_STA_DISABLE_HE))
992 skb = ieee80211_nullfunc_get(&local->hw, &sdata->vif,
993 !ieee80211_hw_check(&local->hw, DOESNT_SUPPORT_QOS_NDP));
997 nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
999 nullfunc->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
1001 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
1002 IEEE80211_TX_INTFL_OFFCHAN_TX_OK;
1004 if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
1005 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
1007 if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL)
1008 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_USE_MINRATE;
1010 ieee80211_tx_skb(sdata, skb);
1013 static void ieee80211_send_4addr_nullfunc(struct ieee80211_local *local,
1014 struct ieee80211_sub_if_data *sdata)
1016 struct sk_buff *skb;
1017 struct ieee80211_hdr *nullfunc;
1020 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
1023 /* Don't send NDPs when connected HE */
1024 if (!(sdata->u.mgd.flags & IEEE80211_STA_DISABLE_HE))
1027 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 30);
1031 skb_reserve(skb, local->hw.extra_tx_headroom);
1033 nullfunc = skb_put_zero(skb, 30);
1034 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
1035 IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
1036 nullfunc->frame_control = fc;
1037 memcpy(nullfunc->addr1, sdata->u.mgd.bssid, ETH_ALEN);
1038 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1039 memcpy(nullfunc->addr3, sdata->u.mgd.bssid, ETH_ALEN);
1040 memcpy(nullfunc->addr4, sdata->vif.addr, ETH_ALEN);
1042 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
1043 ieee80211_tx_skb(sdata, skb);
1046 /* spectrum management related things */
1047 static void ieee80211_chswitch_work(struct work_struct *work)
1049 struct ieee80211_sub_if_data *sdata =
1050 container_of(work, struct ieee80211_sub_if_data, u.mgd.chswitch_work);
1051 struct ieee80211_local *local = sdata->local;
1052 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1055 if (!ieee80211_sdata_running(sdata))
1059 mutex_lock(&local->mtx);
1060 mutex_lock(&local->chanctx_mtx);
1062 if (!ifmgd->associated)
1065 if (!sdata->vif.csa_active)
1069 * using reservation isn't immediate as it may be deferred until later
1070 * with multi-vif. once reservation is complete it will re-schedule the
1071 * work with no reserved_chanctx so verify chandef to check if it
1072 * completed successfully
1075 if (sdata->reserved_chanctx) {
1077 * with multi-vif csa driver may call ieee80211_csa_finish()
1078 * many times while waiting for other interfaces to use their
1081 if (sdata->reserved_ready)
1084 ret = ieee80211_vif_use_reserved_context(sdata);
1087 "failed to use reserved channel context, disconnecting (err=%d)\n",
1089 ieee80211_queue_work(&sdata->local->hw,
1090 &ifmgd->csa_connection_drop_work);
1097 if (!cfg80211_chandef_identical(&sdata->vif.bss_conf.chandef,
1098 &sdata->csa_chandef)) {
1100 "failed to finalize channel switch, disconnecting\n");
1101 ieee80211_queue_work(&sdata->local->hw,
1102 &ifmgd->csa_connection_drop_work);
1106 /* XXX: shouldn't really modify cfg80211-owned data! */
1107 ifmgd->associated->channel = sdata->csa_chandef.chan;
1109 ifmgd->csa_waiting_bcn = true;
1111 ieee80211_sta_reset_beacon_monitor(sdata);
1112 ieee80211_sta_reset_conn_monitor(sdata);
1115 mutex_unlock(&local->chanctx_mtx);
1116 mutex_unlock(&local->mtx);
1117 sdata_unlock(sdata);
1120 static void ieee80211_chswitch_post_beacon(struct ieee80211_sub_if_data *sdata)
1122 struct ieee80211_local *local = sdata->local;
1123 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1126 sdata_assert_lock(sdata);
1128 WARN_ON(!sdata->vif.csa_active);
1130 if (sdata->csa_block_tx) {
1131 ieee80211_wake_vif_queues(local, sdata,
1132 IEEE80211_QUEUE_STOP_REASON_CSA);
1133 sdata->csa_block_tx = false;
1136 sdata->vif.csa_active = false;
1137 ifmgd->csa_waiting_bcn = false;
1139 ret = drv_post_channel_switch(sdata);
1142 "driver post channel switch failed, disconnecting\n");
1143 ieee80211_queue_work(&local->hw,
1144 &ifmgd->csa_connection_drop_work);
1148 cfg80211_ch_switch_notify(sdata->dev, &sdata->reserved_chandef);
1151 void ieee80211_chswitch_done(struct ieee80211_vif *vif, bool success)
1153 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1154 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1156 trace_api_chswitch_done(sdata, success);
1159 "driver channel switch failed, disconnecting\n");
1160 ieee80211_queue_work(&sdata->local->hw,
1161 &ifmgd->csa_connection_drop_work);
1163 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
1166 EXPORT_SYMBOL(ieee80211_chswitch_done);
1168 static void ieee80211_chswitch_timer(struct timer_list *t)
1170 struct ieee80211_sub_if_data *sdata =
1171 from_timer(sdata, t, u.mgd.chswitch_timer);
1173 ieee80211_queue_work(&sdata->local->hw, &sdata->u.mgd.chswitch_work);
1177 ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
1178 u64 timestamp, u32 device_timestamp,
1179 struct ieee802_11_elems *elems,
1182 struct ieee80211_local *local = sdata->local;
1183 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1184 struct cfg80211_bss *cbss = ifmgd->associated;
1185 struct ieee80211_chanctx_conf *conf;
1186 struct ieee80211_chanctx *chanctx;
1187 enum nl80211_band current_band;
1188 struct ieee80211_csa_ie csa_ie;
1189 struct ieee80211_channel_switch ch_switch;
1192 sdata_assert_lock(sdata);
1197 if (local->scanning)
1200 /* disregard subsequent announcements if we are already processing */
1201 if (sdata->vif.csa_active)
1204 current_band = cbss->channel->band;
1205 res = ieee80211_parse_ch_switch_ie(sdata, elems, current_band,
1207 ifmgd->associated->bssid, &csa_ie);
1209 ieee80211_queue_work(&local->hw,
1210 &ifmgd->csa_connection_drop_work);
1214 if (!cfg80211_chandef_usable(local->hw.wiphy, &csa_ie.chandef,
1215 IEEE80211_CHAN_DISABLED)) {
1217 "AP %pM switches to unsupported channel (%d MHz, width:%d, CF1/2: %d/%d MHz), disconnecting\n",
1218 ifmgd->associated->bssid,
1219 csa_ie.chandef.chan->center_freq,
1220 csa_ie.chandef.width, csa_ie.chandef.center_freq1,
1221 csa_ie.chandef.center_freq2);
1222 ieee80211_queue_work(&local->hw,
1223 &ifmgd->csa_connection_drop_work);
1227 if (cfg80211_chandef_identical(&csa_ie.chandef,
1228 &sdata->vif.bss_conf.chandef)) {
1229 if (ifmgd->csa_ignored_same_chan)
1232 "AP %pM tries to chanswitch to same channel, ignore\n",
1233 ifmgd->associated->bssid);
1234 ifmgd->csa_ignored_same_chan = true;
1239 * Drop all TDLS peers - either we disconnect or move to a different
1240 * channel from this point on. There's no telling what our peer will do.
1241 * The TDLS WIDER_BW scenario is also problematic, as peers might now
1242 * have an incompatible wider chandef.
1244 ieee80211_teardown_tdls_peers(sdata);
1246 mutex_lock(&local->mtx);
1247 mutex_lock(&local->chanctx_mtx);
1248 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
1249 lockdep_is_held(&local->chanctx_mtx));
1252 "no channel context assigned to vif?, disconnecting\n");
1253 goto drop_connection;
1256 chanctx = container_of(conf, struct ieee80211_chanctx, conf);
1258 if (local->use_chanctx &&
1259 !ieee80211_hw_check(&local->hw, CHANCTX_STA_CSA)) {
1261 "driver doesn't support chan-switch with channel contexts\n");
1262 goto drop_connection;
1265 ch_switch.timestamp = timestamp;
1266 ch_switch.device_timestamp = device_timestamp;
1267 ch_switch.block_tx = csa_ie.mode;
1268 ch_switch.chandef = csa_ie.chandef;
1269 ch_switch.count = csa_ie.count;
1271 if (drv_pre_channel_switch(sdata, &ch_switch)) {
1273 "preparing for channel switch failed, disconnecting\n");
1274 goto drop_connection;
1277 res = ieee80211_vif_reserve_chanctx(sdata, &csa_ie.chandef,
1278 chanctx->mode, false);
1281 "failed to reserve channel context for channel switch, disconnecting (err=%d)\n",
1283 goto drop_connection;
1285 mutex_unlock(&local->chanctx_mtx);
1287 sdata->vif.csa_active = true;
1288 sdata->csa_chandef = csa_ie.chandef;
1289 sdata->csa_block_tx = csa_ie.mode;
1290 ifmgd->csa_ignored_same_chan = false;
1292 if (sdata->csa_block_tx)
1293 ieee80211_stop_vif_queues(local, sdata,
1294 IEEE80211_QUEUE_STOP_REASON_CSA);
1295 mutex_unlock(&local->mtx);
1297 cfg80211_ch_switch_started_notify(sdata->dev, &csa_ie.chandef,
1300 if (local->ops->channel_switch) {
1301 /* use driver's channel switch callback */
1302 drv_channel_switch(local, sdata, &ch_switch);
1306 /* channel switch handled in software */
1307 if (csa_ie.count <= 1)
1308 ieee80211_queue_work(&local->hw, &ifmgd->chswitch_work);
1310 mod_timer(&ifmgd->chswitch_timer,
1311 TU_TO_EXP_TIME((csa_ie.count - 1) *
1312 cbss->beacon_interval));
1315 ieee80211_queue_work(&local->hw, &ifmgd->csa_connection_drop_work);
1316 mutex_unlock(&local->chanctx_mtx);
1317 mutex_unlock(&local->mtx);
1321 ieee80211_find_80211h_pwr_constr(struct ieee80211_sub_if_data *sdata,
1322 struct ieee80211_channel *channel,
1323 const u8 *country_ie, u8 country_ie_len,
1324 const u8 *pwr_constr_elem,
1325 int *chan_pwr, int *pwr_reduction)
1327 struct ieee80211_country_ie_triplet *triplet;
1328 int chan = ieee80211_frequency_to_channel(channel->center_freq);
1329 int i, chan_increment;
1330 bool have_chan_pwr = false;
1333 if (country_ie_len % 2 || country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
1336 triplet = (void *)(country_ie + 3);
1337 country_ie_len -= 3;
1339 switch (channel->band) {
1343 case NL80211_BAND_2GHZ:
1344 case NL80211_BAND_60GHZ:
1347 case NL80211_BAND_5GHZ:
1353 while (country_ie_len >= 3) {
1354 u8 first_channel = triplet->chans.first_channel;
1356 if (first_channel >= IEEE80211_COUNTRY_EXTENSION_ID)
1359 for (i = 0; i < triplet->chans.num_channels; i++) {
1360 if (first_channel + i * chan_increment == chan) {
1361 have_chan_pwr = true;
1362 *chan_pwr = triplet->chans.max_power;
1371 country_ie_len -= 3;
1374 if (have_chan_pwr && pwr_constr_elem)
1375 *pwr_reduction = *pwr_constr_elem;
1379 return have_chan_pwr;
1382 static void ieee80211_find_cisco_dtpc(struct ieee80211_sub_if_data *sdata,
1383 struct ieee80211_channel *channel,
1384 const u8 *cisco_dtpc_ie,
1387 /* From practical testing, the first data byte of the DTPC element
1388 * seems to contain the requested dBm level, and the CLI on Cisco
1389 * APs clearly state the range is -127 to 127 dBm, which indicates
1390 * a signed byte, although it seemingly never actually goes negative.
1391 * The other byte seems to always be zero.
1393 *pwr_level = (__s8)cisco_dtpc_ie[4];
1396 static u32 ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data *sdata,
1397 struct ieee80211_channel *channel,
1398 struct ieee80211_mgmt *mgmt,
1399 const u8 *country_ie, u8 country_ie_len,
1400 const u8 *pwr_constr_ie,
1401 const u8 *cisco_dtpc_ie)
1403 bool has_80211h_pwr = false, has_cisco_pwr = false;
1404 int chan_pwr = 0, pwr_reduction_80211h = 0;
1405 int pwr_level_cisco, pwr_level_80211h;
1407 __le16 capab = mgmt->u.probe_resp.capab_info;
1410 (capab & cpu_to_le16(WLAN_CAPABILITY_SPECTRUM_MGMT) ||
1411 capab & cpu_to_le16(WLAN_CAPABILITY_RADIO_MEASURE))) {
1412 has_80211h_pwr = ieee80211_find_80211h_pwr_constr(
1413 sdata, channel, country_ie, country_ie_len,
1414 pwr_constr_ie, &chan_pwr, &pwr_reduction_80211h);
1416 max_t(int, 0, chan_pwr - pwr_reduction_80211h);
1419 if (cisco_dtpc_ie) {
1420 ieee80211_find_cisco_dtpc(
1421 sdata, channel, cisco_dtpc_ie, &pwr_level_cisco);
1422 has_cisco_pwr = true;
1425 if (!has_80211h_pwr && !has_cisco_pwr)
1428 /* If we have both 802.11h and Cisco DTPC, apply both limits
1429 * by picking the smallest of the two power levels advertised.
1431 if (has_80211h_pwr &&
1432 (!has_cisco_pwr || pwr_level_80211h <= pwr_level_cisco)) {
1433 new_ap_level = pwr_level_80211h;
1435 if (sdata->ap_power_level == new_ap_level)
1439 "Limiting TX power to %d (%d - %d) dBm as advertised by %pM\n",
1440 pwr_level_80211h, chan_pwr, pwr_reduction_80211h,
1441 sdata->u.mgd.bssid);
1442 } else { /* has_cisco_pwr is always true here. */
1443 new_ap_level = pwr_level_cisco;
1445 if (sdata->ap_power_level == new_ap_level)
1449 "Limiting TX power to %d dBm as advertised by %pM\n",
1450 pwr_level_cisco, sdata->u.mgd.bssid);
1453 sdata->ap_power_level = new_ap_level;
1454 if (__ieee80211_recalc_txpower(sdata))
1455 return BSS_CHANGED_TXPOWER;
1460 static void ieee80211_enable_ps(struct ieee80211_local *local,
1461 struct ieee80211_sub_if_data *sdata)
1463 struct ieee80211_conf *conf = &local->hw.conf;
1466 * If we are scanning right now then the parameters will
1467 * take effect when scan finishes.
1469 if (local->scanning)
1472 if (conf->dynamic_ps_timeout > 0 &&
1473 !ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS)) {
1474 mod_timer(&local->dynamic_ps_timer, jiffies +
1475 msecs_to_jiffies(conf->dynamic_ps_timeout));
1477 if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK))
1478 ieee80211_send_nullfunc(local, sdata, true);
1480 if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK) &&
1481 ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
1484 conf->flags |= IEEE80211_CONF_PS;
1485 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1489 static void ieee80211_change_ps(struct ieee80211_local *local)
1491 struct ieee80211_conf *conf = &local->hw.conf;
1493 if (local->ps_sdata) {
1494 ieee80211_enable_ps(local, local->ps_sdata);
1495 } else if (conf->flags & IEEE80211_CONF_PS) {
1496 conf->flags &= ~IEEE80211_CONF_PS;
1497 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1498 del_timer_sync(&local->dynamic_ps_timer);
1499 cancel_work_sync(&local->dynamic_ps_enable_work);
1503 static bool ieee80211_powersave_allowed(struct ieee80211_sub_if_data *sdata)
1505 struct ieee80211_if_managed *mgd = &sdata->u.mgd;
1506 struct sta_info *sta = NULL;
1507 bool authorized = false;
1509 if (!mgd->powersave)
1515 if (!mgd->associated)
1518 if (mgd->flags & IEEE80211_STA_CONNECTION_POLL)
1521 if (!mgd->have_beacon)
1525 sta = sta_info_get(sdata, mgd->bssid);
1527 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
1533 /* need to hold RTNL or interface lock */
1534 void ieee80211_recalc_ps(struct ieee80211_local *local)
1536 struct ieee80211_sub_if_data *sdata, *found = NULL;
1540 if (!ieee80211_hw_check(&local->hw, SUPPORTS_PS)) {
1541 local->ps_sdata = NULL;
1545 list_for_each_entry(sdata, &local->interfaces, list) {
1546 if (!ieee80211_sdata_running(sdata))
1548 if (sdata->vif.type == NL80211_IFTYPE_AP) {
1549 /* If an AP vif is found, then disable PS
1550 * by setting the count to zero thereby setting
1556 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1562 if (count == 1 && ieee80211_powersave_allowed(found)) {
1563 u8 dtimper = found->u.mgd.dtim_period;
1565 timeout = local->dynamic_ps_forced_timeout;
1568 local->hw.conf.dynamic_ps_timeout = timeout;
1570 /* If the TIM IE is invalid, pretend the value is 1 */
1574 local->hw.conf.ps_dtim_period = dtimper;
1575 local->ps_sdata = found;
1577 local->ps_sdata = NULL;
1580 ieee80211_change_ps(local);
1583 void ieee80211_recalc_ps_vif(struct ieee80211_sub_if_data *sdata)
1585 bool ps_allowed = ieee80211_powersave_allowed(sdata);
1587 if (sdata->vif.bss_conf.ps != ps_allowed) {
1588 sdata->vif.bss_conf.ps = ps_allowed;
1589 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_PS);
1593 void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
1595 struct ieee80211_local *local =
1596 container_of(work, struct ieee80211_local,
1597 dynamic_ps_disable_work);
1599 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1600 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1601 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1604 ieee80211_wake_queues_by_reason(&local->hw,
1605 IEEE80211_MAX_QUEUE_MAP,
1606 IEEE80211_QUEUE_STOP_REASON_PS,
1610 void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
1612 struct ieee80211_local *local =
1613 container_of(work, struct ieee80211_local,
1614 dynamic_ps_enable_work);
1615 struct ieee80211_sub_if_data *sdata = local->ps_sdata;
1616 struct ieee80211_if_managed *ifmgd;
1617 unsigned long flags;
1620 /* can only happen when PS was just disabled anyway */
1624 ifmgd = &sdata->u.mgd;
1626 if (local->hw.conf.flags & IEEE80211_CONF_PS)
1629 if (local->hw.conf.dynamic_ps_timeout > 0) {
1630 /* don't enter PS if TX frames are pending */
1631 if (drv_tx_frames_pending(local)) {
1632 mod_timer(&local->dynamic_ps_timer, jiffies +
1634 local->hw.conf.dynamic_ps_timeout));
1639 * transmission can be stopped by others which leads to
1640 * dynamic_ps_timer expiry. Postpone the ps timer if it
1641 * is not the actual idle state.
1643 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
1644 for (q = 0; q < local->hw.queues; q++) {
1645 if (local->queue_stop_reasons[q]) {
1646 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1648 mod_timer(&local->dynamic_ps_timer, jiffies +
1650 local->hw.conf.dynamic_ps_timeout));
1654 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
1657 if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK) &&
1658 !(ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
1659 if (drv_tx_frames_pending(local)) {
1660 mod_timer(&local->dynamic_ps_timer, jiffies +
1662 local->hw.conf.dynamic_ps_timeout));
1664 ieee80211_send_nullfunc(local, sdata, true);
1665 /* Flush to get the tx status of nullfunc frame */
1666 ieee80211_flush_queues(local, sdata, false);
1670 if (!(ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS) &&
1671 ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK)) ||
1672 (ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
1673 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
1674 local->hw.conf.flags |= IEEE80211_CONF_PS;
1675 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1679 void ieee80211_dynamic_ps_timer(struct timer_list *t)
1681 struct ieee80211_local *local = from_timer(local, t, dynamic_ps_timer);
1683 ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
1686 void ieee80211_dfs_cac_timer_work(struct work_struct *work)
1688 struct delayed_work *delayed_work = to_delayed_work(work);
1689 struct ieee80211_sub_if_data *sdata =
1690 container_of(delayed_work, struct ieee80211_sub_if_data,
1691 dfs_cac_timer_work);
1692 struct cfg80211_chan_def chandef = sdata->vif.bss_conf.chandef;
1694 mutex_lock(&sdata->local->mtx);
1695 if (sdata->wdev.cac_started) {
1696 ieee80211_vif_release_channel(sdata);
1697 cfg80211_cac_event(sdata->dev, &chandef,
1698 NL80211_RADAR_CAC_FINISHED,
1701 mutex_unlock(&sdata->local->mtx);
1705 __ieee80211_sta_handle_tspec_ac_params(struct ieee80211_sub_if_data *sdata)
1707 struct ieee80211_local *local = sdata->local;
1708 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1712 if (local->hw.queues < IEEE80211_NUM_ACS)
1715 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1716 struct ieee80211_sta_tx_tspec *tx_tspec = &ifmgd->tx_tspec[ac];
1718 unsigned long now = jiffies;
1720 if (tx_tspec->action == TX_TSPEC_ACTION_NONE &&
1721 tx_tspec->admitted_time &&
1722 time_after(now, tx_tspec->time_slice_start + HZ)) {
1723 tx_tspec->consumed_tx_time = 0;
1724 tx_tspec->time_slice_start = now;
1726 if (tx_tspec->downgraded)
1728 TX_TSPEC_ACTION_STOP_DOWNGRADE;
1731 switch (tx_tspec->action) {
1732 case TX_TSPEC_ACTION_STOP_DOWNGRADE:
1733 /* take the original parameters */
1734 if (drv_conf_tx(local, sdata, ac, &sdata->tx_conf[ac]))
1736 "failed to set TX queue parameters for queue %d\n",
1738 tx_tspec->action = TX_TSPEC_ACTION_NONE;
1739 tx_tspec->downgraded = false;
1742 case TX_TSPEC_ACTION_DOWNGRADE:
1743 if (time_after(now, tx_tspec->time_slice_start + HZ)) {
1744 tx_tspec->action = TX_TSPEC_ACTION_NONE;
1748 /* downgrade next lower non-ACM AC */
1749 for (non_acm_ac = ac + 1;
1750 non_acm_ac < IEEE80211_NUM_ACS;
1752 if (!(sdata->wmm_acm & BIT(7 - 2 * non_acm_ac)))
1754 /* Usually the loop will result in using BK even if it
1755 * requires admission control, but such a configuration
1756 * makes no sense and we have to transmit somehow - the
1757 * AC selection does the same thing.
1758 * If we started out trying to downgrade from BK, then
1759 * the extra condition here might be needed.
1761 if (non_acm_ac >= IEEE80211_NUM_ACS)
1762 non_acm_ac = IEEE80211_AC_BK;
1763 if (drv_conf_tx(local, sdata, ac,
1764 &sdata->tx_conf[non_acm_ac]))
1766 "failed to set TX queue parameters for queue %d\n",
1768 tx_tspec->action = TX_TSPEC_ACTION_NONE;
1770 schedule_delayed_work(&ifmgd->tx_tspec_wk,
1771 tx_tspec->time_slice_start + HZ - now + 1);
1773 case TX_TSPEC_ACTION_NONE:
1782 void ieee80211_sta_handle_tspec_ac_params(struct ieee80211_sub_if_data *sdata)
1784 if (__ieee80211_sta_handle_tspec_ac_params(sdata))
1785 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_QOS);
1788 static void ieee80211_sta_handle_tspec_ac_params_wk(struct work_struct *work)
1790 struct ieee80211_sub_if_data *sdata;
1792 sdata = container_of(work, struct ieee80211_sub_if_data,
1793 u.mgd.tx_tspec_wk.work);
1794 ieee80211_sta_handle_tspec_ac_params(sdata);
1799 ieee80211_sta_wmm_params(struct ieee80211_local *local,
1800 struct ieee80211_sub_if_data *sdata,
1801 const u8 *wmm_param, size_t wmm_param_len,
1802 const struct ieee80211_mu_edca_param_set *mu_edca)
1804 struct ieee80211_tx_queue_params params[IEEE80211_NUM_ACS];
1805 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1809 u8 uapsd_queues = 0;
1811 if (!local->ops->conf_tx)
1814 if (local->hw.queues < IEEE80211_NUM_ACS)
1820 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
1823 if (ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED)
1824 uapsd_queues = ifmgd->uapsd_queues;
1826 count = wmm_param[6] & 0x0f;
1827 if (count == ifmgd->wmm_last_param_set)
1829 ifmgd->wmm_last_param_set = count;
1831 pos = wmm_param + 8;
1832 left = wmm_param_len - 8;
1834 memset(¶ms, 0, sizeof(params));
1837 for (; left >= 4; left -= 4, pos += 4) {
1838 int aci = (pos[0] >> 5) & 0x03;
1839 int acm = (pos[0] >> 4) & 0x01;
1844 ac = IEEE80211_AC_BK;
1846 sdata->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
1847 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
1849 params[ac].mu_edca = !!mu_edca;
1851 params[ac].mu_edca_param_rec = mu_edca->ac_bk;
1854 ac = IEEE80211_AC_VI;
1856 sdata->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
1857 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
1859 params[ac].mu_edca = !!mu_edca;
1861 params[ac].mu_edca_param_rec = mu_edca->ac_vi;
1864 ac = IEEE80211_AC_VO;
1866 sdata->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
1867 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
1869 params[ac].mu_edca = !!mu_edca;
1871 params[ac].mu_edca_param_rec = mu_edca->ac_vo;
1875 ac = IEEE80211_AC_BE;
1877 sdata->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
1878 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
1880 params[ac].mu_edca = !!mu_edca;
1882 params[ac].mu_edca_param_rec = mu_edca->ac_be;
1886 params[ac].aifs = pos[0] & 0x0f;
1888 if (params[ac].aifs < 2) {
1890 "AP has invalid WMM params (AIFSN=%d for ACI %d), will use 2\n",
1891 params[ac].aifs, aci);
1892 params[ac].aifs = 2;
1894 params[ac].cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
1895 params[ac].cw_min = ecw2cw(pos[1] & 0x0f);
1896 params[ac].txop = get_unaligned_le16(pos + 2);
1897 params[ac].acm = acm;
1898 params[ac].uapsd = uapsd;
1900 if (params[ac].cw_min == 0 ||
1901 params[ac].cw_min > params[ac].cw_max) {
1903 "AP has invalid WMM params (CWmin/max=%d/%d for ACI %d), using defaults\n",
1904 params[ac].cw_min, params[ac].cw_max, aci);
1907 ieee80211_regulatory_limit_wmm_params(sdata, ¶ms[ac], ac);
1910 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1912 "WMM AC=%d acm=%d aifs=%d cWmin=%d cWmax=%d txop=%d uapsd=%d, downgraded=%d\n",
1914 params[ac].aifs, params[ac].cw_min, params[ac].cw_max,
1915 params[ac].txop, params[ac].uapsd,
1916 ifmgd->tx_tspec[ac].downgraded);
1917 sdata->tx_conf[ac] = params[ac];
1918 if (!ifmgd->tx_tspec[ac].downgraded &&
1919 drv_conf_tx(local, sdata, ac, ¶ms[ac]))
1921 "failed to set TX queue parameters for AC %d\n",
1925 /* enable WMM or activate new settings */
1926 sdata->vif.bss_conf.qos = true;
1930 static void __ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
1932 lockdep_assert_held(&sdata->local->mtx);
1934 sdata->u.mgd.flags &= ~IEEE80211_STA_CONNECTION_POLL;
1935 ieee80211_run_deferred_scan(sdata->local);
1938 static void ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
1940 mutex_lock(&sdata->local->mtx);
1941 __ieee80211_stop_poll(sdata);
1942 mutex_unlock(&sdata->local->mtx);
1945 static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
1946 u16 capab, bool erp_valid, u8 erp)
1948 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
1949 struct ieee80211_supported_band *sband;
1951 bool use_protection;
1952 bool use_short_preamble;
1953 bool use_short_slot;
1955 sband = ieee80211_get_sband(sdata);
1960 use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
1961 use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
1963 use_protection = false;
1964 use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
1967 use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
1968 if (sband->band == NL80211_BAND_5GHZ)
1969 use_short_slot = true;
1971 if (use_protection != bss_conf->use_cts_prot) {
1972 bss_conf->use_cts_prot = use_protection;
1973 changed |= BSS_CHANGED_ERP_CTS_PROT;
1976 if (use_short_preamble != bss_conf->use_short_preamble) {
1977 bss_conf->use_short_preamble = use_short_preamble;
1978 changed |= BSS_CHANGED_ERP_PREAMBLE;
1981 if (use_short_slot != bss_conf->use_short_slot) {
1982 bss_conf->use_short_slot = use_short_slot;
1983 changed |= BSS_CHANGED_ERP_SLOT;
1989 static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
1990 struct cfg80211_bss *cbss,
1991 u32 bss_info_changed)
1993 struct ieee80211_bss *bss = (void *)cbss->priv;
1994 struct ieee80211_local *local = sdata->local;
1995 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
1997 bss_info_changed |= BSS_CHANGED_ASSOC;
1998 bss_info_changed |= ieee80211_handle_bss_capability(sdata,
1999 bss_conf->assoc_capability, bss->has_erp_value, bss->erp_value);
2001 sdata->u.mgd.beacon_timeout = usecs_to_jiffies(ieee80211_tu_to_usec(
2002 beacon_loss_count * bss_conf->beacon_int));
2004 sdata->u.mgd.associated = cbss;
2005 memcpy(sdata->u.mgd.bssid, cbss->bssid, ETH_ALEN);
2007 ieee80211_check_rate_mask(sdata);
2009 sdata->u.mgd.flags |= IEEE80211_STA_RESET_SIGNAL_AVE;
2011 if (sdata->vif.p2p ||
2012 sdata->vif.driver_flags & IEEE80211_VIF_GET_NOA_UPDATE) {
2013 const struct cfg80211_bss_ies *ies;
2016 ies = rcu_dereference(cbss->ies);
2020 ret = cfg80211_get_p2p_attr(
2021 ies->data, ies->len,
2022 IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
2023 (u8 *) &bss_conf->p2p_noa_attr,
2024 sizeof(bss_conf->p2p_noa_attr));
2026 sdata->u.mgd.p2p_noa_index =
2027 bss_conf->p2p_noa_attr.index;
2028 bss_info_changed |= BSS_CHANGED_P2P_PS;
2034 /* just to be sure */
2035 ieee80211_stop_poll(sdata);
2037 ieee80211_led_assoc(local, 1);
2039 if (sdata->u.mgd.have_beacon) {
2041 * If the AP is buggy we may get here with no DTIM period
2042 * known, so assume it's 1 which is the only safe assumption
2043 * in that case, although if the TIM IE is broken powersave
2044 * probably just won't work at all.
2046 bss_conf->dtim_period = sdata->u.mgd.dtim_period ?: 1;
2047 bss_conf->beacon_rate = bss->beacon_rate;
2048 bss_info_changed |= BSS_CHANGED_BEACON_INFO;
2050 bss_conf->beacon_rate = NULL;
2051 bss_conf->dtim_period = 0;
2054 bss_conf->assoc = 1;
2056 /* Tell the driver to monitor connection quality (if supported) */
2057 if (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI &&
2058 bss_conf->cqm_rssi_thold)
2059 bss_info_changed |= BSS_CHANGED_CQM;
2061 /* Enable ARP filtering */
2062 if (bss_conf->arp_addr_cnt)
2063 bss_info_changed |= BSS_CHANGED_ARP_FILTER;
2065 ieee80211_bss_info_change_notify(sdata, bss_info_changed);
2067 mutex_lock(&local->iflist_mtx);
2068 ieee80211_recalc_ps(local);
2069 mutex_unlock(&local->iflist_mtx);
2071 ieee80211_recalc_smps(sdata);
2072 ieee80211_recalc_ps_vif(sdata);
2074 netif_carrier_on(sdata->dev);
2077 static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
2078 u16 stype, u16 reason, bool tx,
2081 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2082 struct ieee80211_local *local = sdata->local;
2085 sdata_assert_lock(sdata);
2087 if (WARN_ON_ONCE(tx && !frame_buf))
2090 if (WARN_ON(!ifmgd->associated))
2093 ieee80211_stop_poll(sdata);
2095 ifmgd->associated = NULL;
2096 netif_carrier_off(sdata->dev);
2099 * if we want to get out of ps before disassoc (why?) we have
2100 * to do it before sending disassoc, as otherwise the null-packet
2103 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
2104 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
2105 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
2107 local->ps_sdata = NULL;
2109 /* disable per-vif ps */
2110 ieee80211_recalc_ps_vif(sdata);
2112 /* make sure ongoing transmission finishes */
2116 * drop any frame before deauth/disassoc, this can be data or
2117 * management frame. Since we are disconnecting, we should not
2118 * insist sending these frames which can take time and delay
2119 * the disconnection and possible the roaming.
2122 ieee80211_flush_queues(local, sdata, true);
2124 /* deauthenticate/disassociate now */
2125 if (tx || frame_buf) {
2127 * In multi channel scenarios guarantee that the virtual
2128 * interface is granted immediate airtime to transmit the
2129 * deauthentication frame by calling mgd_prepare_tx, if the
2130 * driver requested so.
2132 if (ieee80211_hw_check(&local->hw, DEAUTH_NEED_MGD_TX_PREP) &&
2133 !ifmgd->have_beacon)
2134 drv_mgd_prepare_tx(sdata->local, sdata, 0);
2136 ieee80211_send_deauth_disassoc(sdata, ifmgd->bssid, stype,
2137 reason, tx, frame_buf);
2140 /* flush out frame - make sure the deauth was actually sent */
2142 ieee80211_flush_queues(local, sdata, false);
2144 /* clear bssid only after building the needed mgmt frames */
2145 eth_zero_addr(ifmgd->bssid);
2147 /* remove AP and TDLS peers */
2148 sta_info_flush(sdata);
2150 /* finally reset all BSS / config parameters */
2151 changed |= ieee80211_reset_erp_info(sdata);
2153 ieee80211_led_assoc(local, 0);
2154 changed |= BSS_CHANGED_ASSOC;
2155 sdata->vif.bss_conf.assoc = false;
2157 ifmgd->p2p_noa_index = -1;
2158 memset(&sdata->vif.bss_conf.p2p_noa_attr, 0,
2159 sizeof(sdata->vif.bss_conf.p2p_noa_attr));
2161 /* on the next assoc, re-program HT/VHT parameters */
2162 memset(&ifmgd->ht_capa, 0, sizeof(ifmgd->ht_capa));
2163 memset(&ifmgd->ht_capa_mask, 0, sizeof(ifmgd->ht_capa_mask));
2164 memset(&ifmgd->vht_capa, 0, sizeof(ifmgd->vht_capa));
2165 memset(&ifmgd->vht_capa_mask, 0, sizeof(ifmgd->vht_capa_mask));
2167 /* reset MU-MIMO ownership and group data */
2168 memset(sdata->vif.bss_conf.mu_group.membership, 0,
2169 sizeof(sdata->vif.bss_conf.mu_group.membership));
2170 memset(sdata->vif.bss_conf.mu_group.position, 0,
2171 sizeof(sdata->vif.bss_conf.mu_group.position));
2172 changed |= BSS_CHANGED_MU_GROUPS;
2173 sdata->vif.mu_mimo_owner = false;
2175 sdata->ap_power_level = IEEE80211_UNSET_POWER_LEVEL;
2177 del_timer_sync(&local->dynamic_ps_timer);
2178 cancel_work_sync(&local->dynamic_ps_enable_work);
2180 /* Disable ARP filtering */
2181 if (sdata->vif.bss_conf.arp_addr_cnt)
2182 changed |= BSS_CHANGED_ARP_FILTER;
2184 sdata->vif.bss_conf.qos = false;
2185 changed |= BSS_CHANGED_QOS;
2187 /* The BSSID (not really interesting) and HT changed */
2188 changed |= BSS_CHANGED_BSSID | BSS_CHANGED_HT;
2189 ieee80211_bss_info_change_notify(sdata, changed);
2191 /* disassociated - set to defaults now */
2192 ieee80211_set_wmm_default(sdata, false, false);
2194 del_timer_sync(&sdata->u.mgd.conn_mon_timer);
2195 del_timer_sync(&sdata->u.mgd.bcn_mon_timer);
2196 del_timer_sync(&sdata->u.mgd.timer);
2197 del_timer_sync(&sdata->u.mgd.chswitch_timer);
2199 sdata->vif.bss_conf.dtim_period = 0;
2200 sdata->vif.bss_conf.beacon_rate = NULL;
2202 ifmgd->have_beacon = false;
2205 mutex_lock(&local->mtx);
2206 ieee80211_vif_release_channel(sdata);
2208 sdata->vif.csa_active = false;
2209 ifmgd->csa_waiting_bcn = false;
2210 ifmgd->csa_ignored_same_chan = false;
2211 if (sdata->csa_block_tx) {
2212 ieee80211_wake_vif_queues(local, sdata,
2213 IEEE80211_QUEUE_STOP_REASON_CSA);
2214 sdata->csa_block_tx = false;
2216 mutex_unlock(&local->mtx);
2218 /* existing TX TSPEC sessions no longer exist */
2219 memset(ifmgd->tx_tspec, 0, sizeof(ifmgd->tx_tspec));
2220 cancel_delayed_work_sync(&ifmgd->tx_tspec_wk);
2222 sdata->encrypt_headroom = IEEE80211_ENCRYPT_HEADROOM;
2225 void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
2226 struct ieee80211_hdr *hdr)
2229 * We can postpone the mgd.timer whenever receiving unicast frames
2230 * from AP because we know that the connection is working both ways
2231 * at that time. But multicast frames (and hence also beacons) must
2232 * be ignored here, because we need to trigger the timer during
2233 * data idle periods for sending the periodic probe request to the
2234 * AP we're connected to.
2236 if (is_multicast_ether_addr(hdr->addr1))
2239 ieee80211_sta_reset_conn_monitor(sdata);
2242 static void ieee80211_reset_ap_probe(struct ieee80211_sub_if_data *sdata)
2244 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2245 struct ieee80211_local *local = sdata->local;
2247 mutex_lock(&local->mtx);
2248 if (!(ifmgd->flags & IEEE80211_STA_CONNECTION_POLL))
2251 __ieee80211_stop_poll(sdata);
2253 mutex_lock(&local->iflist_mtx);
2254 ieee80211_recalc_ps(local);
2255 mutex_unlock(&local->iflist_mtx);
2257 if (ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
2261 * We've received a probe response, but are not sure whether
2262 * we have or will be receiving any beacons or data, so let's
2263 * schedule the timers again, just in case.
2265 ieee80211_sta_reset_beacon_monitor(sdata);
2267 mod_timer(&ifmgd->conn_mon_timer,
2268 round_jiffies_up(jiffies +
2269 IEEE80211_CONNECTION_IDLE_TIME));
2271 mutex_unlock(&local->mtx);
2274 static void ieee80211_sta_tx_wmm_ac_notify(struct ieee80211_sub_if_data *sdata,
2275 struct ieee80211_hdr *hdr,
2278 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2279 u16 tid = ieee80211_get_tid(hdr);
2280 int ac = ieee80211_ac_from_tid(tid);
2281 struct ieee80211_sta_tx_tspec *tx_tspec = &ifmgd->tx_tspec[ac];
2282 unsigned long now = jiffies;
2284 if (likely(!tx_tspec->admitted_time))
2287 if (time_after(now, tx_tspec->time_slice_start + HZ)) {
2288 tx_tspec->consumed_tx_time = 0;
2289 tx_tspec->time_slice_start = now;
2291 if (tx_tspec->downgraded) {
2292 tx_tspec->action = TX_TSPEC_ACTION_STOP_DOWNGRADE;
2293 schedule_delayed_work(&ifmgd->tx_tspec_wk, 0);
2297 if (tx_tspec->downgraded)
2300 tx_tspec->consumed_tx_time += tx_time;
2302 if (tx_tspec->consumed_tx_time >= tx_tspec->admitted_time) {
2303 tx_tspec->downgraded = true;
2304 tx_tspec->action = TX_TSPEC_ACTION_DOWNGRADE;
2305 schedule_delayed_work(&ifmgd->tx_tspec_wk, 0);
2309 void ieee80211_sta_tx_notify(struct ieee80211_sub_if_data *sdata,
2310 struct ieee80211_hdr *hdr, bool ack, u16 tx_time)
2312 ieee80211_sta_tx_wmm_ac_notify(sdata, hdr, tx_time);
2314 if (!ieee80211_is_data(hdr->frame_control))
2317 if (ieee80211_is_nullfunc(hdr->frame_control) &&
2318 sdata->u.mgd.probe_send_count > 0) {
2320 ieee80211_sta_reset_conn_monitor(sdata);
2322 sdata->u.mgd.nullfunc_failed = true;
2323 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
2328 ieee80211_sta_reset_conn_monitor(sdata);
2331 static void ieee80211_mlme_send_probe_req(struct ieee80211_sub_if_data *sdata,
2332 const u8 *src, const u8 *dst,
2333 const u8 *ssid, size_t ssid_len,
2334 struct ieee80211_channel *channel)
2336 struct sk_buff *skb;
2338 skb = ieee80211_build_probe_req(sdata, src, dst, (u32)-1, channel,
2339 ssid, ssid_len, NULL, 0,
2340 IEEE80211_PROBE_FLAG_DIRECTED);
2342 ieee80211_tx_skb(sdata, skb);
2345 static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
2347 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2349 u8 *dst = ifmgd->associated->bssid;
2350 u8 unicast_limit = max(1, max_probe_tries - 3);
2351 struct sta_info *sta;
2354 * Try sending broadcast probe requests for the last three
2355 * probe requests after the first ones failed since some
2356 * buggy APs only support broadcast probe requests.
2358 if (ifmgd->probe_send_count >= unicast_limit)
2362 * When the hardware reports an accurate Tx ACK status, it's
2363 * better to send a nullfunc frame instead of a probe request,
2364 * as it will kick us off the AP quickly if we aren't associated
2365 * anymore. The timeout will be reset if the frame is ACKed by
2368 ifmgd->probe_send_count++;
2371 mutex_lock(&sdata->local->sta_mtx);
2372 sta = sta_info_get(sdata, dst);
2374 ieee80211_check_fast_rx(sta);
2375 mutex_unlock(&sdata->local->sta_mtx);
2378 if (ieee80211_hw_check(&sdata->local->hw, REPORTS_TX_ACK_STATUS)) {
2379 ifmgd->nullfunc_failed = false;
2380 ieee80211_send_nullfunc(sdata->local, sdata, false);
2385 ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
2386 if (WARN_ON_ONCE(ssid == NULL))
2391 ieee80211_mlme_send_probe_req(sdata, sdata->vif.addr, dst,
2393 ifmgd->associated->channel);
2397 ifmgd->probe_timeout = jiffies + msecs_to_jiffies(probe_wait_ms);
2398 run_again(sdata, ifmgd->probe_timeout);
2401 static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
2404 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2405 bool already = false;
2407 if (!ieee80211_sdata_running(sdata))
2412 if (!ifmgd->associated)
2415 mutex_lock(&sdata->local->mtx);
2417 if (sdata->local->tmp_channel || sdata->local->scanning) {
2418 mutex_unlock(&sdata->local->mtx);
2423 mlme_dbg_ratelimited(sdata,
2424 "detected beacon loss from AP (missed %d beacons) - probing\n",
2427 ieee80211_cqm_beacon_loss_notify(&sdata->vif, GFP_KERNEL);
2431 * The driver/our work has already reported this event or the
2432 * connection monitoring has kicked in and we have already sent
2433 * a probe request. Or maybe the AP died and the driver keeps
2434 * reporting until we disassociate...
2436 * In either case we have to ignore the current call to this
2437 * function (except for setting the correct probe reason bit)
2438 * because otherwise we would reset the timer every time and
2439 * never check whether we received a probe response!
2441 if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL)
2444 ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
2446 mutex_unlock(&sdata->local->mtx);
2451 mutex_lock(&sdata->local->iflist_mtx);
2452 ieee80211_recalc_ps(sdata->local);
2453 mutex_unlock(&sdata->local->iflist_mtx);
2455 ifmgd->probe_send_count = 0;
2456 ieee80211_mgd_probe_ap_send(sdata);
2458 sdata_unlock(sdata);
2461 struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw,
2462 struct ieee80211_vif *vif)
2464 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2465 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2466 struct cfg80211_bss *cbss;
2467 struct sk_buff *skb;
2471 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
2474 sdata_assert_lock(sdata);
2476 if (ifmgd->associated)
2477 cbss = ifmgd->associated;
2478 else if (ifmgd->auth_data)
2479 cbss = ifmgd->auth_data->bss;
2480 else if (ifmgd->assoc_data)
2481 cbss = ifmgd->assoc_data->bss;
2486 ssid = ieee80211_bss_get_ie(cbss, WLAN_EID_SSID);
2487 if (WARN_ON_ONCE(ssid == NULL))
2492 skb = ieee80211_build_probe_req(sdata, sdata->vif.addr, cbss->bssid,
2493 (u32) -1, cbss->channel,
2495 NULL, 0, IEEE80211_PROBE_FLAG_DIRECTED);
2500 EXPORT_SYMBOL(ieee80211_ap_probereq_get);
2502 static void ieee80211_report_disconnect(struct ieee80211_sub_if_data *sdata,
2503 const u8 *buf, size_t len, bool tx,
2506 struct ieee80211_event event = {
2508 .u.mlme.data = tx ? DEAUTH_TX_EVENT : DEAUTH_RX_EVENT,
2509 .u.mlme.reason = reason,
2513 cfg80211_tx_mlme_mgmt(sdata->dev, buf, len);
2515 cfg80211_rx_mlme_mgmt(sdata->dev, buf, len);
2517 drv_event_callback(sdata->local, sdata, &event);
2520 static void __ieee80211_disconnect(struct ieee80211_sub_if_data *sdata)
2522 struct ieee80211_local *local = sdata->local;
2523 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2524 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
2527 if (!ifmgd->associated) {
2528 sdata_unlock(sdata);
2532 /* AP is probably out of range (or not reachable for another reason) so
2533 * remove the bss struct for that AP.
2535 cfg80211_unlink_bss(local->hw.wiphy, ifmgd->associated);
2537 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
2538 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
2540 mutex_lock(&local->mtx);
2541 sdata->vif.csa_active = false;
2542 ifmgd->csa_waiting_bcn = false;
2543 if (sdata->csa_block_tx) {
2544 ieee80211_wake_vif_queues(local, sdata,
2545 IEEE80211_QUEUE_STOP_REASON_CSA);
2546 sdata->csa_block_tx = false;
2548 mutex_unlock(&local->mtx);
2550 ieee80211_report_disconnect(sdata, frame_buf, sizeof(frame_buf), true,
2551 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
2553 sdata_unlock(sdata);
2556 static void ieee80211_beacon_connection_loss_work(struct work_struct *work)
2558 struct ieee80211_sub_if_data *sdata =
2559 container_of(work, struct ieee80211_sub_if_data,
2560 u.mgd.beacon_connection_loss_work);
2561 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2563 if (ifmgd->associated)
2564 ifmgd->beacon_loss_count++;
2566 if (ifmgd->connection_loss) {
2567 sdata_info(sdata, "Connection to AP %pM lost\n",
2569 __ieee80211_disconnect(sdata);
2571 ieee80211_mgd_probe_ap(sdata, true);
2575 static void ieee80211_csa_connection_drop_work(struct work_struct *work)
2577 struct ieee80211_sub_if_data *sdata =
2578 container_of(work, struct ieee80211_sub_if_data,
2579 u.mgd.csa_connection_drop_work);
2581 __ieee80211_disconnect(sdata);
2584 void ieee80211_beacon_loss(struct ieee80211_vif *vif)
2586 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2587 struct ieee80211_hw *hw = &sdata->local->hw;
2589 trace_api_beacon_loss(sdata);
2591 sdata->u.mgd.connection_loss = false;
2592 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
2594 EXPORT_SYMBOL(ieee80211_beacon_loss);
2596 void ieee80211_connection_loss(struct ieee80211_vif *vif)
2598 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2599 struct ieee80211_hw *hw = &sdata->local->hw;
2601 trace_api_connection_loss(sdata);
2603 sdata->u.mgd.connection_loss = true;
2604 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
2606 EXPORT_SYMBOL(ieee80211_connection_loss);
2609 static void ieee80211_destroy_auth_data(struct ieee80211_sub_if_data *sdata,
2612 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
2614 sdata_assert_lock(sdata);
2618 * we are not authenticated yet, the only timer that could be
2619 * running is the timeout for the authentication response which
2620 * which is not relevant anymore.
2622 del_timer_sync(&sdata->u.mgd.timer);
2623 sta_info_destroy_addr(sdata, auth_data->bss->bssid);
2625 eth_zero_addr(sdata->u.mgd.bssid);
2626 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
2627 sdata->u.mgd.flags = 0;
2628 mutex_lock(&sdata->local->mtx);
2629 ieee80211_vif_release_channel(sdata);
2630 mutex_unlock(&sdata->local->mtx);
2633 cfg80211_put_bss(sdata->local->hw.wiphy, auth_data->bss);
2635 sdata->u.mgd.auth_data = NULL;
2638 static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata,
2639 bool assoc, bool abandon)
2641 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
2643 sdata_assert_lock(sdata);
2647 * we are not associated yet, the only timer that could be
2648 * running is the timeout for the association response which
2649 * which is not relevant anymore.
2651 del_timer_sync(&sdata->u.mgd.timer);
2652 sta_info_destroy_addr(sdata, assoc_data->bss->bssid);
2654 eth_zero_addr(sdata->u.mgd.bssid);
2655 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
2656 sdata->u.mgd.flags = 0;
2657 sdata->vif.mu_mimo_owner = false;
2659 mutex_lock(&sdata->local->mtx);
2660 ieee80211_vif_release_channel(sdata);
2661 mutex_unlock(&sdata->local->mtx);
2664 cfg80211_abandon_assoc(sdata->dev, assoc_data->bss);
2668 sdata->u.mgd.assoc_data = NULL;
2671 static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
2672 struct ieee80211_mgmt *mgmt, size_t len)
2674 struct ieee80211_local *local = sdata->local;
2675 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
2677 struct ieee802_11_elems elems;
2680 pos = mgmt->u.auth.variable;
2681 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), false, &elems);
2682 if (!elems.challenge)
2684 auth_data->expected_transaction = 4;
2685 drv_mgd_prepare_tx(sdata->local, sdata, 0);
2686 if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
2687 tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
2688 IEEE80211_TX_INTFL_MLME_CONN_TX;
2689 ieee80211_send_auth(sdata, 3, auth_data->algorithm, 0,
2690 elems.challenge - 2, elems.challenge_len + 2,
2691 auth_data->bss->bssid, auth_data->bss->bssid,
2692 auth_data->key, auth_data->key_len,
2693 auth_data->key_idx, tx_flags);
2696 static void ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
2697 struct ieee80211_mgmt *mgmt, size_t len)
2699 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2701 u16 auth_alg, auth_transaction, status_code;
2702 struct sta_info *sta;
2703 struct ieee80211_event event = {
2705 .u.mlme.data = AUTH_EVENT,
2708 sdata_assert_lock(sdata);
2713 if (!ifmgd->auth_data || ifmgd->auth_data->done)
2716 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
2718 if (!ether_addr_equal(bssid, mgmt->bssid))
2721 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
2722 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
2723 status_code = le16_to_cpu(mgmt->u.auth.status_code);
2725 if (auth_alg != ifmgd->auth_data->algorithm ||
2726 auth_transaction != ifmgd->auth_data->expected_transaction) {
2727 sdata_info(sdata, "%pM unexpected authentication state: alg %d (expected %d) transact %d (expected %d)\n",
2728 mgmt->sa, auth_alg, ifmgd->auth_data->algorithm,
2730 ifmgd->auth_data->expected_transaction);
2734 if (status_code != WLAN_STATUS_SUCCESS) {
2735 sdata_info(sdata, "%pM denied authentication (status %d)\n",
2736 mgmt->sa, status_code);
2737 ieee80211_destroy_auth_data(sdata, false);
2738 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
2739 event.u.mlme.status = MLME_DENIED;
2740 event.u.mlme.reason = status_code;
2741 drv_event_callback(sdata->local, sdata, &event);
2745 switch (ifmgd->auth_data->algorithm) {
2746 case WLAN_AUTH_OPEN:
2747 case WLAN_AUTH_LEAP:
2750 case WLAN_AUTH_FILS_SK:
2751 case WLAN_AUTH_FILS_SK_PFS:
2752 case WLAN_AUTH_FILS_PK:
2754 case WLAN_AUTH_SHARED_KEY:
2755 if (ifmgd->auth_data->expected_transaction != 4) {
2756 ieee80211_auth_challenge(sdata, mgmt, len);
2757 /* need another frame */
2762 WARN_ONCE(1, "invalid auth alg %d",
2763 ifmgd->auth_data->algorithm);
2767 event.u.mlme.status = MLME_SUCCESS;
2768 drv_event_callback(sdata->local, sdata, &event);
2769 sdata_info(sdata, "authenticated\n");
2770 ifmgd->auth_data->done = true;
2771 ifmgd->auth_data->timeout = jiffies + IEEE80211_AUTH_WAIT_ASSOC;
2772 ifmgd->auth_data->timeout_started = true;
2773 run_again(sdata, ifmgd->auth_data->timeout);
2775 if (ifmgd->auth_data->algorithm == WLAN_AUTH_SAE &&
2776 ifmgd->auth_data->expected_transaction != 2) {
2778 * Report auth frame to user space for processing since another
2779 * round of Authentication frames is still needed.
2781 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
2785 /* move station state to auth */
2786 mutex_lock(&sdata->local->sta_mtx);
2787 sta = sta_info_get(sdata, bssid);
2789 WARN_ONCE(1, "%s: STA %pM not found", sdata->name, bssid);
2792 if (sta_info_move_state(sta, IEEE80211_STA_AUTH)) {
2793 sdata_info(sdata, "failed moving %pM to auth\n", bssid);
2796 mutex_unlock(&sdata->local->sta_mtx);
2798 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
2801 mutex_unlock(&sdata->local->sta_mtx);
2802 /* ignore frame -- wait for timeout */
2805 #define case_WLAN(type) \
2806 case WLAN_REASON_##type: return #type
2808 static const char *ieee80211_get_reason_code_string(u16 reason_code)
2810 switch (reason_code) {
2811 case_WLAN(UNSPECIFIED);
2812 case_WLAN(PREV_AUTH_NOT_VALID);
2813 case_WLAN(DEAUTH_LEAVING);
2814 case_WLAN(DISASSOC_DUE_TO_INACTIVITY);
2815 case_WLAN(DISASSOC_AP_BUSY);
2816 case_WLAN(CLASS2_FRAME_FROM_NONAUTH_STA);
2817 case_WLAN(CLASS3_FRAME_FROM_NONASSOC_STA);
2818 case_WLAN(DISASSOC_STA_HAS_LEFT);
2819 case_WLAN(STA_REQ_ASSOC_WITHOUT_AUTH);
2820 case_WLAN(DISASSOC_BAD_POWER);
2821 case_WLAN(DISASSOC_BAD_SUPP_CHAN);
2822 case_WLAN(INVALID_IE);
2823 case_WLAN(MIC_FAILURE);
2824 case_WLAN(4WAY_HANDSHAKE_TIMEOUT);
2825 case_WLAN(GROUP_KEY_HANDSHAKE_TIMEOUT);
2826 case_WLAN(IE_DIFFERENT);
2827 case_WLAN(INVALID_GROUP_CIPHER);
2828 case_WLAN(INVALID_PAIRWISE_CIPHER);
2829 case_WLAN(INVALID_AKMP);
2830 case_WLAN(UNSUPP_RSN_VERSION);
2831 case_WLAN(INVALID_RSN_IE_CAP);
2832 case_WLAN(IEEE8021X_FAILED);
2833 case_WLAN(CIPHER_SUITE_REJECTED);
2834 case_WLAN(DISASSOC_UNSPECIFIED_QOS);
2835 case_WLAN(DISASSOC_QAP_NO_BANDWIDTH);
2836 case_WLAN(DISASSOC_LOW_ACK);
2837 case_WLAN(DISASSOC_QAP_EXCEED_TXOP);
2838 case_WLAN(QSTA_LEAVE_QBSS);
2839 case_WLAN(QSTA_NOT_USE);
2840 case_WLAN(QSTA_REQUIRE_SETUP);
2841 case_WLAN(QSTA_TIMEOUT);
2842 case_WLAN(QSTA_CIPHER_NOT_SUPP);
2843 case_WLAN(MESH_PEER_CANCELED);
2844 case_WLAN(MESH_MAX_PEERS);
2845 case_WLAN(MESH_CONFIG);
2846 case_WLAN(MESH_CLOSE);
2847 case_WLAN(MESH_MAX_RETRIES);
2848 case_WLAN(MESH_CONFIRM_TIMEOUT);
2849 case_WLAN(MESH_INVALID_GTK);
2850 case_WLAN(MESH_INCONSISTENT_PARAM);
2851 case_WLAN(MESH_INVALID_SECURITY);
2852 case_WLAN(MESH_PATH_ERROR);
2853 case_WLAN(MESH_PATH_NOFORWARD);
2854 case_WLAN(MESH_PATH_DEST_UNREACHABLE);
2855 case_WLAN(MAC_EXISTS_IN_MBSS);
2856 case_WLAN(MESH_CHAN_REGULATORY);
2857 case_WLAN(MESH_CHAN);
2858 default: return "<unknown>";
2862 static void ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
2863 struct ieee80211_mgmt *mgmt, size_t len)
2865 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2866 u16 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
2868 sdata_assert_lock(sdata);
2873 if (ifmgd->associated &&
2874 ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid)) {
2875 const u8 *bssid = ifmgd->associated->bssid;
2877 sdata_info(sdata, "deauthenticated from %pM (Reason: %u=%s)\n",
2879 ieee80211_get_reason_code_string(reason_code));
2881 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
2883 ieee80211_report_disconnect(sdata, (u8 *)mgmt, len, false,
2888 if (ifmgd->assoc_data &&
2889 ether_addr_equal(mgmt->bssid, ifmgd->assoc_data->bss->bssid)) {
2890 const u8 *bssid = ifmgd->assoc_data->bss->bssid;
2893 "deauthenticated from %pM while associating (Reason: %u=%s)\n",
2895 ieee80211_get_reason_code_string(reason_code));
2897 ieee80211_destroy_assoc_data(sdata, false, true);
2899 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
2905 static void ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
2906 struct ieee80211_mgmt *mgmt, size_t len)
2908 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2911 sdata_assert_lock(sdata);
2916 if (!ifmgd->associated ||
2917 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
2920 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
2922 sdata_info(sdata, "disassociated from %pM (Reason: %u=%s)\n",
2923 mgmt->sa, reason_code,
2924 ieee80211_get_reason_code_string(reason_code));
2926 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
2928 ieee80211_report_disconnect(sdata, (u8 *)mgmt, len, false, reason_code);
2931 static void ieee80211_get_rates(struct ieee80211_supported_band *sband,
2932 u8 *supp_rates, unsigned int supp_rates_len,
2933 u32 *rates, u32 *basic_rates,
2934 bool *have_higher_than_11mbit,
2935 int *min_rate, int *min_rate_index,
2940 for (i = 0; i < supp_rates_len; i++) {
2941 int rate = supp_rates[i] & 0x7f;
2942 bool is_basic = !!(supp_rates[i] & 0x80);
2944 if ((rate * 5 * (1 << shift)) > 110)
2945 *have_higher_than_11mbit = true;
2948 * Skip HT and VHT BSS membership selectors since they're not
2951 * Note: Even though the membership selector and the basic
2952 * rate flag share the same bit, they are not exactly
2955 if (supp_rates[i] == (0x80 | BSS_MEMBERSHIP_SELECTOR_HT_PHY) ||
2956 supp_rates[i] == (0x80 | BSS_MEMBERSHIP_SELECTOR_VHT_PHY))
2959 for (j = 0; j < sband->n_bitrates; j++) {
2960 struct ieee80211_rate *br;
2963 br = &sband->bitrates[j];
2965 brate = DIV_ROUND_UP(br->bitrate, (1 << shift) * 5);
2966 if (brate == rate) {
2969 *basic_rates |= BIT(j);
2970 if ((rate * 5) < *min_rate) {
2971 *min_rate = rate * 5;
2972 *min_rate_index = j;
2980 static bool ieee80211_assoc_success(struct ieee80211_sub_if_data *sdata,
2981 struct cfg80211_bss *cbss,
2982 struct ieee80211_mgmt *mgmt, size_t len)
2984 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2985 struct ieee80211_local *local = sdata->local;
2986 struct ieee80211_supported_band *sband;
2987 struct sta_info *sta;
2989 u16 capab_info, aid;
2990 struct ieee802_11_elems elems;
2991 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
2992 const struct cfg80211_bss_ies *bss_ies = NULL;
2993 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
2998 /* AssocResp and ReassocResp have identical structure */
3000 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
3001 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
3004 * The 5 MSB of the AID field are reserved
3005 * (802.11-2016 9.4.1.8 AID field)
3009 ifmgd->broken_ap = false;
3011 if (aid == 0 || aid > IEEE80211_MAX_AID) {
3012 sdata_info(sdata, "invalid AID value %d (out of range), turn off PS\n",
3015 ifmgd->broken_ap = true;
3018 pos = mgmt->u.assoc_resp.variable;
3019 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), false, &elems);
3021 if (!elems.supp_rates) {
3022 sdata_info(sdata, "no SuppRates element in AssocResp\n");
3027 ifmgd->tdls_chan_switch_prohibited =
3028 elems.ext_capab && elems.ext_capab_len >= 5 &&
3029 (elems.ext_capab[4] & WLAN_EXT_CAPA5_TDLS_CH_SW_PROHIBITED);
3032 * Some APs are erroneously not including some information in their
3033 * (re)association response frames. Try to recover by using the data
3034 * from the beacon or probe response. This seems to afflict mobile
3035 * 2G/3G/4G wifi routers, reported models include the "Onda PN51T",
3036 * "Vodafone PocketWiFi 2", "ZTE MF60" and a similar T-Mobile device.
3038 if ((assoc_data->wmm && !elems.wmm_param) ||
3039 (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
3040 (!elems.ht_cap_elem || !elems.ht_operation)) ||
3041 (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
3042 (!elems.vht_cap_elem || !elems.vht_operation))) {
3043 const struct cfg80211_bss_ies *ies;
3044 struct ieee802_11_elems bss_elems;
3047 ies = rcu_dereference(cbss->ies);
3049 bss_ies = kmemdup(ies, sizeof(*ies) + ies->len,
3055 ieee802_11_parse_elems(bss_ies->data, bss_ies->len,
3057 if (assoc_data->wmm &&
3058 !elems.wmm_param && bss_elems.wmm_param) {
3059 elems.wmm_param = bss_elems.wmm_param;
3061 "AP bug: WMM param missing from AssocResp\n");
3065 * Also check if we requested HT/VHT, otherwise the AP doesn't
3066 * have to include the IEs in the (re)association response.
3068 if (!elems.ht_cap_elem && bss_elems.ht_cap_elem &&
3069 !(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
3070 elems.ht_cap_elem = bss_elems.ht_cap_elem;
3072 "AP bug: HT capability missing from AssocResp\n");
3074 if (!elems.ht_operation && bss_elems.ht_operation &&
3075 !(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
3076 elems.ht_operation = bss_elems.ht_operation;
3078 "AP bug: HT operation missing from AssocResp\n");
3080 if (!elems.vht_cap_elem && bss_elems.vht_cap_elem &&
3081 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)) {
3082 elems.vht_cap_elem = bss_elems.vht_cap_elem;
3084 "AP bug: VHT capa missing from AssocResp\n");
3086 if (!elems.vht_operation && bss_elems.vht_operation &&
3087 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)) {
3088 elems.vht_operation = bss_elems.vht_operation;
3090 "AP bug: VHT operation missing from AssocResp\n");
3095 * We previously checked these in the beacon/probe response, so
3096 * they should be present here. This is just a safety net.
3098 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
3099 (!elems.wmm_param || !elems.ht_cap_elem || !elems.ht_operation)) {
3101 "HT AP is missing WMM params or HT capability/operation\n");
3106 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
3107 (!elems.vht_cap_elem || !elems.vht_operation)) {
3109 "VHT AP is missing VHT capability/operation\n");
3114 mutex_lock(&sdata->local->sta_mtx);
3116 * station info was already allocated and inserted before
3117 * the association and should be available to us
3119 sta = sta_info_get(sdata, cbss->bssid);
3120 if (WARN_ON(!sta)) {
3121 mutex_unlock(&sdata->local->sta_mtx);
3126 sband = ieee80211_get_sband(sdata);
3128 mutex_unlock(&sdata->local->sta_mtx);
3134 * If AP doesn't support HT, or it doesn't have HE mandatory IEs, mark
3135 * HE as disabled. If on the 5GHz band, make sure it supports VHT.
3137 if (ifmgd->flags & IEEE80211_STA_DISABLE_HT ||
3138 (sband->band == NL80211_BAND_5GHZ &&
3139 ifmgd->flags & IEEE80211_STA_DISABLE_VHT) ||
3140 (!elems.he_cap && !elems.he_operation))
3141 ifmgd->flags |= IEEE80211_STA_DISABLE_HE;
3143 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HE) &&
3144 (!elems.he_cap || !elems.he_operation)) {
3145 mutex_unlock(&sdata->local->sta_mtx);
3147 "HE AP is missing HE capability/operation\n");
3152 /* Set up internal HT/VHT capabilities */
3153 if (elems.ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
3154 ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
3155 elems.ht_cap_elem, sta);
3157 if (elems.vht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
3158 ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband,
3159 elems.vht_cap_elem, sta);
3161 if (elems.he_operation && !(ifmgd->flags & IEEE80211_STA_DISABLE_HE) &&
3163 ieee80211_he_cap_ie_to_sta_he_cap(sdata, sband,
3168 bss_conf->he_support = sta->sta.he_cap.has_he;
3170 bss_conf->he_support = false;
3173 if (bss_conf->he_support) {
3174 u32 he_oper_params =
3175 le32_to_cpu(elems.he_operation->he_oper_params);
3177 bss_conf->bss_color = he_oper_params &
3178 IEEE80211_HE_OPERATION_BSS_COLOR_MASK;
3179 bss_conf->htc_trig_based_pkt_ext =
3181 IEEE80211_HE_OPERATION_DFLT_PE_DURATION_MASK) <<
3182 IEEE80211_HE_OPERATION_DFLT_PE_DURATION_OFFSET;
3183 bss_conf->frame_time_rts_th =
3185 IEEE80211_HE_OPERATION_RTS_THRESHOLD_MASK) <<
3186 IEEE80211_HE_OPERATION_RTS_THRESHOLD_OFFSET;
3188 bss_conf->multi_sta_back_32bit =
3189 sta->sta.he_cap.he_cap_elem.mac_cap_info[2] &
3190 IEEE80211_HE_MAC_CAP2_32BIT_BA_BITMAP;
3192 bss_conf->ack_enabled =
3193 sta->sta.he_cap.he_cap_elem.mac_cap_info[2] &
3194 IEEE80211_HE_MAC_CAP2_ACK_EN;
3196 bss_conf->uora_exists = !!elems.uora_element;
3197 if (elems.uora_element)
3198 bss_conf->uora_ocw_range = elems.uora_element[0];
3200 /* TODO: OPEN: what happens if BSS color disable is set? */
3204 * Some APs, e.g. Netgear WNDR3700, report invalid HT operation data
3205 * in their association response, so ignore that data for our own
3206 * configuration. If it changed since the last beacon, we'll get the
3207 * next beacon and update then.
3211 * If an operating mode notification IE is present, override the
3212 * NSS calculation (that would be done in rate_control_rate_init())
3213 * and use the # of streams from that element.
3215 if (elems.opmode_notif &&
3216 !(*elems.opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_TYPE_BF)) {
3219 nss = *elems.opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_MASK;
3220 nss >>= IEEE80211_OPMODE_NOTIF_RX_NSS_SHIFT;
3222 sta->sta.rx_nss = nss;
3225 rate_control_rate_init(sta);
3227 if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED) {
3228 set_sta_flag(sta, WLAN_STA_MFP);
3229 sta->sta.mfp = true;
3231 sta->sta.mfp = false;
3234 sta->sta.wme = elems.wmm_param && local->hw.queues >= IEEE80211_NUM_ACS;
3236 err = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
3237 if (!err && !(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
3238 err = sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
3241 "failed to move station %pM to desired state\n",
3243 WARN_ON(__sta_info_destroy(sta));
3244 mutex_unlock(&sdata->local->sta_mtx);
3249 mutex_unlock(&sdata->local->sta_mtx);
3252 * Always handle WMM once after association regardless
3253 * of the first value the AP uses. Setting -1 here has
3254 * that effect because the AP values is an unsigned
3257 ifmgd->wmm_last_param_set = -1;
3259 if (ifmgd->flags & IEEE80211_STA_DISABLE_WMM) {
3260 ieee80211_set_wmm_default(sdata, false, false);
3261 } else if (!ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
3262 elems.wmm_param_len,
3263 elems.mu_edca_param_set)) {
3264 /* still enable QoS since we might have HT/VHT */
3265 ieee80211_set_wmm_default(sdata, false, true);
3266 /* set the disable-WMM flag in this case to disable
3267 * tracking WMM parameter changes in the beacon if
3268 * the parameters weren't actually valid. Doing so
3269 * avoids changing parameters very strangely when
3270 * the AP is going back and forth between valid and
3271 * invalid parameters.
3273 ifmgd->flags |= IEEE80211_STA_DISABLE_WMM;
3275 changed |= BSS_CHANGED_QOS;
3277 if (elems.max_idle_period_ie) {
3278 bss_conf->max_idle_period =
3279 le16_to_cpu(elems.max_idle_period_ie->max_idle_period);
3280 bss_conf->protected_keep_alive =
3281 !!(elems.max_idle_period_ie->idle_options &
3282 WLAN_IDLE_OPTIONS_PROTECTED_KEEP_ALIVE);
3283 changed |= BSS_CHANGED_KEEP_ALIVE;
3285 bss_conf->max_idle_period = 0;
3286 bss_conf->protected_keep_alive = false;