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>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
14 #include <linux/delay.h>
15 #include <linux/if_ether.h>
16 #include <linux/skbuff.h>
17 #include <linux/if_arp.h>
18 #include <linux/etherdevice.h>
19 #include <linux/moduleparam.h>
20 #include <linux/rtnetlink.h>
21 #include <linux/pm_qos.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"
33 #define IEEE80211_AUTH_TIMEOUT (HZ / 5)
34 #define IEEE80211_AUTH_TIMEOUT_LONG (HZ / 2)
35 #define IEEE80211_AUTH_TIMEOUT_SHORT (HZ / 10)
36 #define IEEE80211_AUTH_MAX_TRIES 3
37 #define IEEE80211_AUTH_WAIT_ASSOC (HZ * 5)
38 #define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
39 #define IEEE80211_ASSOC_TIMEOUT_LONG (HZ / 2)
40 #define IEEE80211_ASSOC_TIMEOUT_SHORT (HZ / 10)
41 #define IEEE80211_ASSOC_MAX_TRIES 3
43 static int max_nullfunc_tries = 2;
44 module_param(max_nullfunc_tries, int, 0644);
45 MODULE_PARM_DESC(max_nullfunc_tries,
46 "Maximum nullfunc tx tries before disconnecting (reason 4).");
48 static int max_probe_tries = 5;
49 module_param(max_probe_tries, int, 0644);
50 MODULE_PARM_DESC(max_probe_tries,
51 "Maximum probe tries before disconnecting (reason 4).");
54 * Beacon loss timeout is calculated as N frames times the
55 * advertised beacon interval. This may need to be somewhat
56 * higher than what hardware might detect to account for
57 * delays in the host processing frames. But since we also
58 * probe on beacon miss before declaring the connection lost
59 * default to what we want.
61 static int beacon_loss_count = 7;
62 module_param(beacon_loss_count, int, 0644);
63 MODULE_PARM_DESC(beacon_loss_count,
64 "Number of beacon intervals before we decide beacon was lost.");
67 * Time the connection can be idle before we probe
68 * it to see if we can still talk to the AP.
70 #define IEEE80211_CONNECTION_IDLE_TIME (30 * HZ)
72 * Time we wait for a probe response after sending
73 * a probe request because of beacon loss or for
74 * checking the connection still works.
76 static int probe_wait_ms = 500;
77 module_param(probe_wait_ms, int, 0644);
78 MODULE_PARM_DESC(probe_wait_ms,
79 "Maximum time(ms) to wait for probe response"
80 " before disconnecting (reason 4).");
83 * Weight given to the latest Beacon frame when calculating average signal
84 * strength for Beacon frames received in the current BSS. This must be
87 #define IEEE80211_SIGNAL_AVE_WEIGHT 3
90 * How many Beacon frames need to have been used in average signal strength
91 * before starting to indicate signal change events.
93 #define IEEE80211_SIGNAL_AVE_MIN_COUNT 4
96 * We can have multiple work items (and connection probing)
97 * scheduling this timer, but we need to take care to only
98 * reschedule it when it should fire _earlier_ than it was
99 * asked for before, or if it's not pending right now. This
100 * function ensures that. Note that it then is required to
101 * run this function for all timeouts after the first one
102 * has happened -- the work that runs from this timer will
105 static void run_again(struct ieee80211_sub_if_data *sdata,
106 unsigned long timeout)
108 sdata_assert_lock(sdata);
110 if (!timer_pending(&sdata->u.mgd.timer) ||
111 time_before(timeout, sdata->u.mgd.timer.expires))
112 mod_timer(&sdata->u.mgd.timer, timeout);
115 void ieee80211_sta_reset_beacon_monitor(struct ieee80211_sub_if_data *sdata)
117 if (sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)
120 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
123 mod_timer(&sdata->u.mgd.bcn_mon_timer,
124 round_jiffies_up(jiffies + sdata->u.mgd.beacon_timeout));
127 void ieee80211_sta_reset_conn_monitor(struct ieee80211_sub_if_data *sdata)
129 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
131 if (unlikely(!sdata->u.mgd.associated))
134 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
137 mod_timer(&sdata->u.mgd.conn_mon_timer,
138 round_jiffies_up(jiffies + IEEE80211_CONNECTION_IDLE_TIME));
140 ifmgd->probe_send_count = 0;
143 static int ecw2cw(int ecw)
145 return (1 << ecw) - 1;
148 static u32 chandef_downgrade(struct cfg80211_chan_def *c)
154 case NL80211_CHAN_WIDTH_20:
155 c->width = NL80211_CHAN_WIDTH_20_NOHT;
156 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
158 case NL80211_CHAN_WIDTH_40:
159 c->width = NL80211_CHAN_WIDTH_20;
160 c->center_freq1 = c->chan->center_freq;
161 ret = IEEE80211_STA_DISABLE_40MHZ |
162 IEEE80211_STA_DISABLE_VHT;
164 case NL80211_CHAN_WIDTH_80:
165 tmp = (30 + c->chan->center_freq - c->center_freq1)/20;
169 c->center_freq1 = c->center_freq1 - 20 + 40 * tmp;
170 c->width = NL80211_CHAN_WIDTH_40;
171 ret = IEEE80211_STA_DISABLE_VHT;
173 case NL80211_CHAN_WIDTH_80P80:
175 c->width = NL80211_CHAN_WIDTH_80;
176 ret = IEEE80211_STA_DISABLE_80P80MHZ |
177 IEEE80211_STA_DISABLE_160MHZ;
179 case NL80211_CHAN_WIDTH_160:
181 tmp = (70 + c->chan->center_freq - c->center_freq1)/20;
184 c->center_freq1 = c->center_freq1 - 40 + 80 * tmp;
185 c->width = NL80211_CHAN_WIDTH_80;
186 ret = IEEE80211_STA_DISABLE_80P80MHZ |
187 IEEE80211_STA_DISABLE_160MHZ;
190 case NL80211_CHAN_WIDTH_20_NOHT:
192 c->width = NL80211_CHAN_WIDTH_20_NOHT;
193 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
195 case NL80211_CHAN_WIDTH_5:
196 case NL80211_CHAN_WIDTH_10:
199 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
203 WARN_ON_ONCE(!cfg80211_chandef_valid(c));
209 ieee80211_determine_chantype(struct ieee80211_sub_if_data *sdata,
210 struct ieee80211_supported_band *sband,
211 struct ieee80211_channel *channel,
212 const struct ieee80211_ht_operation *ht_oper,
213 const struct ieee80211_vht_operation *vht_oper,
214 struct cfg80211_chan_def *chandef, bool tracking)
216 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
217 struct cfg80211_chan_def vht_chandef;
220 chandef->chan = channel;
221 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
222 chandef->center_freq1 = channel->center_freq;
223 chandef->center_freq2 = 0;
225 if (!ht_oper || !sband->ht_cap.ht_supported) {
226 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
230 chandef->width = NL80211_CHAN_WIDTH_20;
232 ht_cfreq = ieee80211_channel_to_frequency(ht_oper->primary_chan,
234 /* check that channel matches the right operating channel */
235 if (!tracking && channel->center_freq != ht_cfreq) {
237 * It's possible that some APs are confused here;
238 * Netgear WNDR3700 sometimes reports 4 higher than
239 * the actual channel in association responses, but
240 * since we look at probe response/beacon data here
244 "Wrong control channel: center-freq: %d ht-cfreq: %d ht->primary_chan: %d band: %d - Disabling HT\n",
245 channel->center_freq, ht_cfreq,
246 ht_oper->primary_chan, channel->band);
247 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
251 /* check 40 MHz support, if we have it */
252 if (sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) {
253 switch (ht_oper->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
254 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
255 chandef->width = NL80211_CHAN_WIDTH_40;
256 chandef->center_freq1 += 10;
258 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
259 chandef->width = NL80211_CHAN_WIDTH_40;
260 chandef->center_freq1 -= 10;
264 /* 40 MHz (and 80 MHz) must be supported for VHT */
265 ret = IEEE80211_STA_DISABLE_VHT;
266 /* also mark 40 MHz disabled */
267 ret |= IEEE80211_STA_DISABLE_40MHZ;
271 if (!vht_oper || !sband->vht_cap.vht_supported) {
272 ret = IEEE80211_STA_DISABLE_VHT;
276 vht_chandef.chan = channel;
277 vht_chandef.center_freq1 =
278 ieee80211_channel_to_frequency(vht_oper->center_freq_seg1_idx,
280 vht_chandef.center_freq2 = 0;
282 switch (vht_oper->chan_width) {
283 case IEEE80211_VHT_CHANWIDTH_USE_HT:
284 vht_chandef.width = chandef->width;
286 case IEEE80211_VHT_CHANWIDTH_80MHZ:
287 vht_chandef.width = NL80211_CHAN_WIDTH_80;
289 case IEEE80211_VHT_CHANWIDTH_160MHZ:
290 vht_chandef.width = NL80211_CHAN_WIDTH_160;
292 case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
293 vht_chandef.width = NL80211_CHAN_WIDTH_80P80;
294 vht_chandef.center_freq2 =
295 ieee80211_channel_to_frequency(
296 vht_oper->center_freq_seg2_idx,
300 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
302 "AP VHT operation IE has invalid channel width (%d), disable VHT\n",
303 vht_oper->chan_width);
304 ret = IEEE80211_STA_DISABLE_VHT;
308 if (!cfg80211_chandef_valid(&vht_chandef)) {
309 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
311 "AP VHT information is invalid, disable VHT\n");
312 ret = IEEE80211_STA_DISABLE_VHT;
316 if (cfg80211_chandef_identical(chandef, &vht_chandef)) {
321 if (!cfg80211_chandef_compatible(chandef, &vht_chandef)) {
322 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
324 "AP VHT information doesn't match HT, disable VHT\n");
325 ret = IEEE80211_STA_DISABLE_VHT;
329 *chandef = vht_chandef;
334 /* don't print the message below for VHT mismatch if VHT is disabled */
335 if (ret & IEEE80211_STA_DISABLE_VHT)
336 vht_chandef = *chandef;
339 * Ignore the DISABLED flag when we're already connected and only
340 * tracking the APs beacon for bandwidth changes - otherwise we
341 * might get disconnected here if we connect to an AP, update our
342 * regulatory information based on the AP's country IE and the
343 * information we have is wrong/outdated and disables the channel
344 * that we're actually using for the connection to the AP.
346 while (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
348 IEEE80211_CHAN_DISABLED)) {
349 if (WARN_ON(chandef->width == NL80211_CHAN_WIDTH_20_NOHT)) {
350 ret = IEEE80211_STA_DISABLE_HT |
351 IEEE80211_STA_DISABLE_VHT;
355 ret |= chandef_downgrade(chandef);
358 if (chandef->width != vht_chandef.width && !tracking)
360 "capabilities/regulatory prevented using AP HT/VHT configuration, downgraded\n");
362 WARN_ON_ONCE(!cfg80211_chandef_valid(chandef));
366 static int ieee80211_config_bw(struct ieee80211_sub_if_data *sdata,
367 struct sta_info *sta,
368 const struct ieee80211_ht_operation *ht_oper,
369 const struct ieee80211_vht_operation *vht_oper,
370 const u8 *bssid, u32 *changed)
372 struct ieee80211_local *local = sdata->local;
373 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
374 struct ieee80211_supported_band *sband;
375 struct ieee80211_channel *chan;
376 struct cfg80211_chan_def chandef;
379 enum ieee80211_sta_rx_bandwidth new_sta_bw;
382 /* if HT was/is disabled, don't track any bandwidth changes */
383 if (ifmgd->flags & IEEE80211_STA_DISABLE_HT || !ht_oper)
386 /* don't check VHT if we associated as non-VHT station */
387 if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
390 if (WARN_ON_ONCE(!sta))
393 chan = sdata->vif.bss_conf.chandef.chan;
394 sband = local->hw.wiphy->bands[chan->band];
396 /* calculate new channel (type) based on HT/VHT operation IEs */
397 flags = ieee80211_determine_chantype(sdata, sband, chan, ht_oper,
398 vht_oper, &chandef, true);
401 * Downgrade the new channel if we associated with restricted
402 * capabilities. For example, if we associated as a 20 MHz STA
403 * to a 40 MHz AP (due to regulatory, capabilities or config
404 * reasons) then switching to a 40 MHz channel now won't do us
405 * any good -- we couldn't use it with the AP.
407 if (ifmgd->flags & IEEE80211_STA_DISABLE_80P80MHZ &&
408 chandef.width == NL80211_CHAN_WIDTH_80P80)
409 flags |= chandef_downgrade(&chandef);
410 if (ifmgd->flags & IEEE80211_STA_DISABLE_160MHZ &&
411 chandef.width == NL80211_CHAN_WIDTH_160)
412 flags |= chandef_downgrade(&chandef);
413 if (ifmgd->flags & IEEE80211_STA_DISABLE_40MHZ &&
414 chandef.width > NL80211_CHAN_WIDTH_20)
415 flags |= chandef_downgrade(&chandef);
417 if (cfg80211_chandef_identical(&chandef, &sdata->vif.bss_conf.chandef))
421 "AP %pM changed bandwidth, new config is %d MHz, width %d (%d/%d MHz)\n",
422 ifmgd->bssid, chandef.chan->center_freq, chandef.width,
423 chandef.center_freq1, chandef.center_freq2);
425 if (flags != (ifmgd->flags & (IEEE80211_STA_DISABLE_HT |
426 IEEE80211_STA_DISABLE_VHT |
427 IEEE80211_STA_DISABLE_40MHZ |
428 IEEE80211_STA_DISABLE_80P80MHZ |
429 IEEE80211_STA_DISABLE_160MHZ)) ||
430 !cfg80211_chandef_valid(&chandef)) {
432 "AP %pM changed bandwidth in a way we can't support - disconnect\n",
437 switch (chandef.width) {
438 case NL80211_CHAN_WIDTH_20_NOHT:
439 case NL80211_CHAN_WIDTH_20:
440 new_sta_bw = IEEE80211_STA_RX_BW_20;
442 case NL80211_CHAN_WIDTH_40:
443 new_sta_bw = IEEE80211_STA_RX_BW_40;
445 case NL80211_CHAN_WIDTH_80:
446 new_sta_bw = IEEE80211_STA_RX_BW_80;
448 case NL80211_CHAN_WIDTH_80P80:
449 case NL80211_CHAN_WIDTH_160:
450 new_sta_bw = IEEE80211_STA_RX_BW_160;
456 if (new_sta_bw > sta->cur_max_bandwidth)
457 new_sta_bw = sta->cur_max_bandwidth;
459 if (new_sta_bw < sta->sta.bandwidth) {
460 sta->sta.bandwidth = new_sta_bw;
461 rate_control_rate_update(local, sband, sta,
462 IEEE80211_RC_BW_CHANGED);
465 ret = ieee80211_vif_change_bandwidth(sdata, &chandef, changed);
468 "AP %pM changed bandwidth to incompatible one - disconnect\n",
473 if (new_sta_bw > sta->sta.bandwidth) {
474 sta->sta.bandwidth = new_sta_bw;
475 rate_control_rate_update(local, sband, sta,
476 IEEE80211_RC_BW_CHANGED);
479 ht_opmode = le16_to_cpu(ht_oper->operation_mode);
481 /* if bss configuration changed store the new one */
482 if (sdata->vif.bss_conf.ht_operation_mode != ht_opmode) {
483 *changed |= BSS_CHANGED_HT;
484 sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
490 /* frame sending functions */
492 static int ieee80211_compatible_rates(const u8 *supp_rates, int supp_rates_len,
493 struct ieee80211_supported_band *sband,
499 for (i = 0; i < supp_rates_len; i++) {
500 int rate = (supp_rates[i] & 0x7F) * 5;
502 for (j = 0; j < sband->n_bitrates; j++)
503 if (sband->bitrates[j].bitrate == rate) {
513 static void ieee80211_add_ht_ie(struct ieee80211_sub_if_data *sdata,
514 struct sk_buff *skb, u8 ap_ht_param,
515 struct ieee80211_supported_band *sband,
516 struct ieee80211_channel *channel,
517 enum ieee80211_smps_mode smps)
520 u32 flags = channel->flags;
522 struct ieee80211_sta_ht_cap ht_cap;
524 BUILD_BUG_ON(sizeof(ht_cap) != sizeof(sband->ht_cap));
526 memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
527 ieee80211_apply_htcap_overrides(sdata, &ht_cap);
529 /* determine capability flags */
532 switch (ap_ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
533 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
534 if (flags & IEEE80211_CHAN_NO_HT40PLUS) {
535 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
536 cap &= ~IEEE80211_HT_CAP_SGI_40;
539 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
540 if (flags & IEEE80211_CHAN_NO_HT40MINUS) {
541 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
542 cap &= ~IEEE80211_HT_CAP_SGI_40;
548 * If 40 MHz was disabled associate as though we weren't
549 * capable of 40 MHz -- some broken APs will never fall
550 * back to trying to transmit in 20 MHz.
552 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_40MHZ) {
553 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
554 cap &= ~IEEE80211_HT_CAP_SGI_40;
557 /* set SM PS mode properly */
558 cap &= ~IEEE80211_HT_CAP_SM_PS;
560 case IEEE80211_SMPS_AUTOMATIC:
561 case IEEE80211_SMPS_NUM_MODES:
563 case IEEE80211_SMPS_OFF:
564 cap |= WLAN_HT_CAP_SM_PS_DISABLED <<
565 IEEE80211_HT_CAP_SM_PS_SHIFT;
567 case IEEE80211_SMPS_STATIC:
568 cap |= WLAN_HT_CAP_SM_PS_STATIC <<
569 IEEE80211_HT_CAP_SM_PS_SHIFT;
571 case IEEE80211_SMPS_DYNAMIC:
572 cap |= WLAN_HT_CAP_SM_PS_DYNAMIC <<
573 IEEE80211_HT_CAP_SM_PS_SHIFT;
577 /* reserve and fill IE */
578 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
579 ieee80211_ie_build_ht_cap(pos, &ht_cap, cap);
582 static void ieee80211_add_vht_ie(struct ieee80211_sub_if_data *sdata,
584 struct ieee80211_supported_band *sband,
585 struct ieee80211_vht_cap *ap_vht_cap)
589 struct ieee80211_sta_vht_cap vht_cap;
591 BUILD_BUG_ON(sizeof(vht_cap) != sizeof(sband->vht_cap));
593 memcpy(&vht_cap, &sband->vht_cap, sizeof(vht_cap));
594 ieee80211_apply_vhtcap_overrides(sdata, &vht_cap);
596 /* determine capability flags */
599 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_80P80MHZ) {
600 cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ;
601 cap |= IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
604 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_160MHZ) {
605 cap &= ~IEEE80211_VHT_CAP_SHORT_GI_160;
606 cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
610 * Some APs apparently get confused if our capabilities are better
611 * than theirs, so restrict what we advertise in the assoc request.
613 if (!(ap_vht_cap->vht_cap_info &
614 cpu_to_le32(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)))
615 cap &= ~IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE;
617 /* reserve and fill IE */
618 pos = skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2);
619 ieee80211_ie_build_vht_cap(pos, &vht_cap, cap);
622 static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata)
624 struct ieee80211_local *local = sdata->local;
625 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
626 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
628 struct ieee80211_mgmt *mgmt;
630 size_t offset = 0, noffset;
631 int i, count, rates_len, supp_rates_len;
633 struct ieee80211_supported_band *sband;
634 struct ieee80211_chanctx_conf *chanctx_conf;
635 struct ieee80211_channel *chan;
638 sdata_assert_lock(sdata);
641 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
642 if (WARN_ON(!chanctx_conf)) {
646 chan = chanctx_conf->def.chan;
648 sband = local->hw.wiphy->bands[chan->band];
650 if (assoc_data->supp_rates_len) {
652 * Get all rates supported by the device and the AP as
653 * some APs don't like getting a superset of their rates
654 * in the association request (e.g. D-Link DAP 1353 in
657 rates_len = ieee80211_compatible_rates(assoc_data->supp_rates,
658 assoc_data->supp_rates_len,
662 * In case AP not provide any supported rates information
663 * before association, we send information element(s) with
664 * all rates that we support.
667 rates_len = sband->n_bitrates;
670 skb = alloc_skb(local->hw.extra_tx_headroom +
671 sizeof(*mgmt) + /* bit too much but doesn't matter */
672 2 + assoc_data->ssid_len + /* SSID */
673 4 + rates_len + /* (extended) rates */
674 4 + /* power capability */
675 2 + 2 * sband->n_channels + /* supported channels */
676 2 + sizeof(struct ieee80211_ht_cap) + /* HT */
677 2 + sizeof(struct ieee80211_vht_cap) + /* VHT */
678 assoc_data->ie_len + /* extra IEs */
684 skb_reserve(skb, local->hw.extra_tx_headroom);
686 capab = WLAN_CAPABILITY_ESS;
688 if (sband->band == IEEE80211_BAND_2GHZ) {
689 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
690 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
691 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
692 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
695 if (assoc_data->capability & WLAN_CAPABILITY_PRIVACY)
696 capab |= WLAN_CAPABILITY_PRIVACY;
698 if ((assoc_data->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
699 (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
700 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
702 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
704 memcpy(mgmt->da, assoc_data->bss->bssid, ETH_ALEN);
705 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
706 memcpy(mgmt->bssid, assoc_data->bss->bssid, ETH_ALEN);
708 if (!is_zero_ether_addr(assoc_data->prev_bssid)) {
710 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
711 IEEE80211_STYPE_REASSOC_REQ);
712 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
713 mgmt->u.reassoc_req.listen_interval =
714 cpu_to_le16(local->hw.conf.listen_interval);
715 memcpy(mgmt->u.reassoc_req.current_ap, assoc_data->prev_bssid,
719 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
720 IEEE80211_STYPE_ASSOC_REQ);
721 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
722 mgmt->u.assoc_req.listen_interval =
723 cpu_to_le16(local->hw.conf.listen_interval);
727 pos = skb_put(skb, 2 + assoc_data->ssid_len);
728 *pos++ = WLAN_EID_SSID;
729 *pos++ = assoc_data->ssid_len;
730 memcpy(pos, assoc_data->ssid, assoc_data->ssid_len);
732 /* add all rates which were marked to be used above */
733 supp_rates_len = rates_len;
734 if (supp_rates_len > 8)
737 pos = skb_put(skb, supp_rates_len + 2);
738 *pos++ = WLAN_EID_SUPP_RATES;
739 *pos++ = supp_rates_len;
742 for (i = 0; i < sband->n_bitrates; i++) {
743 if (BIT(i) & rates) {
744 int rate = sband->bitrates[i].bitrate;
745 *pos++ = (u8) (rate / 5);
751 if (rates_len > count) {
752 pos = skb_put(skb, rates_len - count + 2);
753 *pos++ = WLAN_EID_EXT_SUPP_RATES;
754 *pos++ = rates_len - count;
756 for (i++; i < sband->n_bitrates; i++) {
757 if (BIT(i) & rates) {
758 int rate = sband->bitrates[i].bitrate;
759 *pos++ = (u8) (rate / 5);
764 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
765 /* 1. power capabilities */
766 pos = skb_put(skb, 4);
767 *pos++ = WLAN_EID_PWR_CAPABILITY;
769 *pos++ = 0; /* min tx power */
770 *pos++ = chan->max_power; /* max tx power */
772 /* 2. supported channels */
773 /* TODO: get this in reg domain format */
774 pos = skb_put(skb, 2 * sband->n_channels + 2);
775 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
776 *pos++ = 2 * sband->n_channels;
777 for (i = 0; i < sband->n_channels; i++) {
778 *pos++ = ieee80211_frequency_to_channel(
779 sband->channels[i].center_freq);
780 *pos++ = 1; /* one channel in the subband*/
784 /* if present, add any custom IEs that go before HT */
785 if (assoc_data->ie_len && assoc_data->ie) {
786 static const u8 before_ht[] = {
789 WLAN_EID_EXT_SUPP_RATES,
790 WLAN_EID_PWR_CAPABILITY,
791 WLAN_EID_SUPPORTED_CHANNELS,
794 WLAN_EID_RRM_ENABLED_CAPABILITIES,
795 WLAN_EID_MOBILITY_DOMAIN,
796 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
798 noffset = ieee80211_ie_split(assoc_data->ie, assoc_data->ie_len,
799 before_ht, ARRAY_SIZE(before_ht),
801 pos = skb_put(skb, noffset - offset);
802 memcpy(pos, assoc_data->ie + offset, noffset - offset);
806 if (WARN_ON_ONCE((ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
807 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)))
808 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
810 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
811 ieee80211_add_ht_ie(sdata, skb, assoc_data->ap_ht_param,
812 sband, chan, sdata->smps_mode);
814 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
815 ieee80211_add_vht_ie(sdata, skb, sband,
816 &assoc_data->ap_vht_cap);
818 /* if present, add any custom non-vendor IEs that go after HT */
819 if (assoc_data->ie_len && assoc_data->ie) {
820 noffset = ieee80211_ie_split_vendor(assoc_data->ie,
823 pos = skb_put(skb, noffset - offset);
824 memcpy(pos, assoc_data->ie + offset, noffset - offset);
828 if (assoc_data->wmm) {
829 if (assoc_data->uapsd) {
830 qos_info = ifmgd->uapsd_queues;
831 qos_info |= (ifmgd->uapsd_max_sp_len <<
832 IEEE80211_WMM_IE_STA_QOSINFO_SP_SHIFT);
837 pos = skb_put(skb, 9);
838 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
839 *pos++ = 7; /* len */
840 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
843 *pos++ = 2; /* WME */
844 *pos++ = 0; /* WME info */
845 *pos++ = 1; /* WME ver */
849 /* add any remaining custom (i.e. vendor specific here) IEs */
850 if (assoc_data->ie_len && assoc_data->ie) {
851 noffset = assoc_data->ie_len;
852 pos = skb_put(skb, noffset - offset);
853 memcpy(pos, assoc_data->ie + offset, noffset - offset);
856 drv_mgd_prepare_tx(local, sdata);
858 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
859 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
860 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS |
861 IEEE80211_TX_INTFL_MLME_CONN_TX;
862 ieee80211_tx_skb(sdata, skb);
865 void ieee80211_send_pspoll(struct ieee80211_local *local,
866 struct ieee80211_sub_if_data *sdata)
868 struct ieee80211_pspoll *pspoll;
871 skb = ieee80211_pspoll_get(&local->hw, &sdata->vif);
875 pspoll = (struct ieee80211_pspoll *) skb->data;
876 pspoll->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
878 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
879 ieee80211_tx_skb(sdata, skb);
882 void ieee80211_send_nullfunc(struct ieee80211_local *local,
883 struct ieee80211_sub_if_data *sdata,
887 struct ieee80211_hdr_3addr *nullfunc;
888 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
890 skb = ieee80211_nullfunc_get(&local->hw, &sdata->vif);
894 nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
896 nullfunc->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
898 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
899 IEEE80211_TX_INTFL_OFFCHAN_TX_OK;
901 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
902 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
904 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
905 IEEE80211_STA_CONNECTION_POLL))
906 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_USE_MINRATE;
908 ieee80211_tx_skb(sdata, skb);
911 static void ieee80211_send_4addr_nullfunc(struct ieee80211_local *local,
912 struct ieee80211_sub_if_data *sdata)
915 struct ieee80211_hdr *nullfunc;
918 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
921 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 30);
925 skb_reserve(skb, local->hw.extra_tx_headroom);
927 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 30);
928 memset(nullfunc, 0, 30);
929 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
930 IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
931 nullfunc->frame_control = fc;
932 memcpy(nullfunc->addr1, sdata->u.mgd.bssid, ETH_ALEN);
933 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
934 memcpy(nullfunc->addr3, sdata->u.mgd.bssid, ETH_ALEN);
935 memcpy(nullfunc->addr4, sdata->vif.addr, ETH_ALEN);
937 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
938 ieee80211_tx_skb(sdata, skb);
941 /* spectrum management related things */
942 static void ieee80211_chswitch_work(struct work_struct *work)
944 struct ieee80211_sub_if_data *sdata =
945 container_of(work, struct ieee80211_sub_if_data, u.mgd.chswitch_work);
946 struct ieee80211_local *local = sdata->local;
947 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
949 if (!ieee80211_sdata_running(sdata))
953 if (!ifmgd->associated)
956 local->_oper_chandef = local->csa_chandef;
958 if (!local->ops->channel_switch) {
959 /* call "hw_config" only if doing sw channel switch */
960 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
962 /* update the device channel directly */
963 local->hw.conf.chandef = local->_oper_chandef;
966 /* XXX: shouldn't really modify cfg80211-owned data! */
967 ifmgd->associated->channel = local->_oper_chandef.chan;
969 /* XXX: wait for a beacon first? */
970 ieee80211_wake_queues_by_reason(&local->hw,
971 IEEE80211_MAX_QUEUE_MAP,
972 IEEE80211_QUEUE_STOP_REASON_CSA);
974 ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
978 void ieee80211_chswitch_done(struct ieee80211_vif *vif, bool success)
980 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
981 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
983 trace_api_chswitch_done(sdata, success);
986 "driver channel switch failed, disconnecting\n");
987 ieee80211_queue_work(&sdata->local->hw,
988 &ifmgd->csa_connection_drop_work);
990 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
993 EXPORT_SYMBOL(ieee80211_chswitch_done);
995 static void ieee80211_chswitch_timer(unsigned long data)
997 struct ieee80211_sub_if_data *sdata =
998 (struct ieee80211_sub_if_data *) data;
1000 ieee80211_queue_work(&sdata->local->hw, &sdata->u.mgd.chswitch_work);
1004 ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
1005 u64 timestamp, struct ieee802_11_elems *elems,
1008 struct ieee80211_local *local = sdata->local;
1009 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1010 struct cfg80211_bss *cbss = ifmgd->associated;
1011 struct ieee80211_bss *bss;
1012 struct ieee80211_chanctx *chanctx;
1013 enum ieee80211_band new_band;
1018 struct ieee80211_channel *new_chan;
1019 struct cfg80211_chan_def new_chandef = {};
1020 struct cfg80211_chan_def new_vht_chandef = {};
1021 const struct ieee80211_sec_chan_offs_ie *sec_chan_offs;
1022 const struct ieee80211_wide_bw_chansw_ie *wide_bw_chansw_ie;
1023 const struct ieee80211_ht_operation *ht_oper;
1024 int secondary_channel_offset = -1;
1026 sdata_assert_lock(sdata);
1031 if (local->scanning)
1034 /* disregard subsequent announcements if we are already processing */
1035 if (ifmgd->flags & IEEE80211_STA_CSA_RECEIVED)
1038 sec_chan_offs = elems->sec_chan_offs;
1039 wide_bw_chansw_ie = elems->wide_bw_chansw_ie;
1040 ht_oper = elems->ht_operation;
1042 if (ifmgd->flags & (IEEE80211_STA_DISABLE_HT |
1043 IEEE80211_STA_DISABLE_40MHZ)) {
1044 sec_chan_offs = NULL;
1045 wide_bw_chansw_ie = NULL;
1046 /* only used for bandwidth here */
1050 if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
1051 wide_bw_chansw_ie = NULL;
1053 if (elems->ext_chansw_ie) {
1054 if (!ieee80211_operating_class_to_band(
1055 elems->ext_chansw_ie->new_operating_class,
1058 "cannot understand ECSA IE operating class %d, disconnecting\n",
1059 elems->ext_chansw_ie->new_operating_class);
1060 ieee80211_queue_work(&local->hw,
1061 &ifmgd->csa_connection_drop_work);
1063 new_chan_no = elems->ext_chansw_ie->new_ch_num;
1064 count = elems->ext_chansw_ie->count;
1065 mode = elems->ext_chansw_ie->mode;
1066 } else if (elems->ch_switch_ie) {
1067 new_band = cbss->channel->band;
1068 new_chan_no = elems->ch_switch_ie->new_ch_num;
1069 count = elems->ch_switch_ie->count;
1070 mode = elems->ch_switch_ie->mode;
1072 /* nothing here we understand */
1076 bss = (void *)cbss->priv;
1078 new_freq = ieee80211_channel_to_frequency(new_chan_no, new_band);
1079 new_chan = ieee80211_get_channel(sdata->local->hw.wiphy, new_freq);
1080 if (!new_chan || new_chan->flags & IEEE80211_CHAN_DISABLED) {
1082 "AP %pM switches to unsupported channel (%d MHz), disconnecting\n",
1083 ifmgd->associated->bssid, new_freq);
1084 ieee80211_queue_work(&local->hw,
1085 &ifmgd->csa_connection_drop_work);
1089 if (!beacon && sec_chan_offs) {
1090 secondary_channel_offset = sec_chan_offs->sec_chan_offs;
1091 } else if (beacon && ht_oper) {
1092 secondary_channel_offset =
1093 ht_oper->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET;
1094 } else if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
1096 * If it's not a beacon, HT is enabled and the IE not present,
1097 * it's 20 MHz, 802.11-2012 8.5.2.6:
1098 * This element [the Secondary Channel Offset Element] is
1099 * present when switching to a 40 MHz channel. It may be
1100 * present when switching to a 20 MHz channel (in which
1101 * case the secondary channel offset is set to SCN).
1103 secondary_channel_offset = IEEE80211_HT_PARAM_CHA_SEC_NONE;
1106 switch (secondary_channel_offset) {
1108 /* secondary_channel_offset was present but is invalid */
1109 case IEEE80211_HT_PARAM_CHA_SEC_NONE:
1110 cfg80211_chandef_create(&new_chandef, new_chan,
1113 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
1114 cfg80211_chandef_create(&new_chandef, new_chan,
1115 NL80211_CHAN_HT40PLUS);
1117 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
1118 cfg80211_chandef_create(&new_chandef, new_chan,
1119 NL80211_CHAN_HT40MINUS);
1122 cfg80211_chandef_create(&new_chandef, new_chan,
1123 NL80211_CHAN_NO_HT);
1127 if (wide_bw_chansw_ie) {
1128 new_vht_chandef.chan = new_chan;
1129 new_vht_chandef.center_freq1 =
1130 ieee80211_channel_to_frequency(
1131 wide_bw_chansw_ie->new_center_freq_seg0,
1134 switch (wide_bw_chansw_ie->new_channel_width) {
1136 /* hmmm, ignore VHT and use HT if present */
1137 case IEEE80211_VHT_CHANWIDTH_USE_HT:
1138 new_vht_chandef.chan = NULL;
1140 case IEEE80211_VHT_CHANWIDTH_80MHZ:
1141 new_vht_chandef.width = NL80211_CHAN_WIDTH_80;
1143 case IEEE80211_VHT_CHANWIDTH_160MHZ:
1144 new_vht_chandef.width = NL80211_CHAN_WIDTH_160;
1146 case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
1147 /* field is otherwise reserved */
1148 new_vht_chandef.center_freq2 =
1149 ieee80211_channel_to_frequency(
1150 wide_bw_chansw_ie->new_center_freq_seg1,
1152 new_vht_chandef.width = NL80211_CHAN_WIDTH_80P80;
1155 if (ifmgd->flags & IEEE80211_STA_DISABLE_80P80MHZ &&
1156 new_vht_chandef.width == NL80211_CHAN_WIDTH_80P80)
1157 chandef_downgrade(&new_vht_chandef);
1158 if (ifmgd->flags & IEEE80211_STA_DISABLE_160MHZ &&
1159 new_vht_chandef.width == NL80211_CHAN_WIDTH_160)
1160 chandef_downgrade(&new_vht_chandef);
1161 if (ifmgd->flags & IEEE80211_STA_DISABLE_40MHZ &&
1162 new_vht_chandef.width > NL80211_CHAN_WIDTH_20)
1163 chandef_downgrade(&new_vht_chandef);
1166 /* if VHT data is there validate & use it */
1167 if (new_vht_chandef.chan) {
1168 if (!cfg80211_chandef_compatible(&new_vht_chandef,
1171 "AP %pM CSA has inconsistent channel data, disconnecting\n",
1172 ifmgd->associated->bssid);
1173 ieee80211_queue_work(&local->hw,
1174 &ifmgd->csa_connection_drop_work);
1177 new_chandef = new_vht_chandef;
1180 if (!cfg80211_chandef_usable(local->hw.wiphy, &new_chandef,
1181 IEEE80211_CHAN_DISABLED)) {
1183 "AP %pM switches to unsupported channel (%d MHz, width:%d, CF1/2: %d/%d MHz), disconnecting\n",
1184 ifmgd->associated->bssid, new_freq,
1185 new_chandef.width, new_chandef.center_freq1,
1186 new_chandef.center_freq2);
1187 ieee80211_queue_work(&local->hw,
1188 &ifmgd->csa_connection_drop_work);
1192 ifmgd->flags |= IEEE80211_STA_CSA_RECEIVED;
1194 if (local->use_chanctx) {
1196 "not handling channel switch with channel contexts\n");
1197 ieee80211_queue_work(&local->hw,
1198 &ifmgd->csa_connection_drop_work);
1202 mutex_lock(&local->chanctx_mtx);
1203 if (WARN_ON(!rcu_access_pointer(sdata->vif.chanctx_conf))) {
1204 mutex_unlock(&local->chanctx_mtx);
1207 chanctx = container_of(rcu_access_pointer(sdata->vif.chanctx_conf),
1208 struct ieee80211_chanctx, conf);
1209 if (chanctx->refcount > 1) {
1211 "channel switch with multiple interfaces on the same channel, disconnecting\n");
1212 ieee80211_queue_work(&local->hw,
1213 &ifmgd->csa_connection_drop_work);
1214 mutex_unlock(&local->chanctx_mtx);
1217 mutex_unlock(&local->chanctx_mtx);
1219 local->csa_chandef = new_chandef;
1222 ieee80211_stop_queues_by_reason(&local->hw,
1223 IEEE80211_MAX_QUEUE_MAP,
1224 IEEE80211_QUEUE_STOP_REASON_CSA);
1226 if (local->ops->channel_switch) {
1227 /* use driver's channel switch callback */
1228 struct ieee80211_channel_switch ch_switch = {
1229 .timestamp = timestamp,
1231 .chandef = new_chandef,
1235 drv_channel_switch(local, &ch_switch);
1239 /* channel switch handled in software */
1241 ieee80211_queue_work(&local->hw, &ifmgd->chswitch_work);
1243 mod_timer(&ifmgd->chswitch_timer,
1244 TU_TO_EXP_TIME(count * cbss->beacon_interval));
1247 static u32 ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data *sdata,
1248 struct ieee80211_channel *channel,
1249 const u8 *country_ie, u8 country_ie_len,
1250 const u8 *pwr_constr_elem)
1252 struct ieee80211_country_ie_triplet *triplet;
1253 int chan = ieee80211_frequency_to_channel(channel->center_freq);
1254 int i, chan_pwr, chan_increment, new_ap_level;
1255 bool have_chan_pwr = false;
1258 if (country_ie_len % 2 || country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
1261 triplet = (void *)(country_ie + 3);
1262 country_ie_len -= 3;
1264 switch (channel->band) {
1268 case IEEE80211_BAND_2GHZ:
1269 case IEEE80211_BAND_60GHZ:
1272 case IEEE80211_BAND_5GHZ:
1278 while (country_ie_len >= 3) {
1279 u8 first_channel = triplet->chans.first_channel;
1281 if (first_channel >= IEEE80211_COUNTRY_EXTENSION_ID)
1284 for (i = 0; i < triplet->chans.num_channels; i++) {
1285 if (first_channel + i * chan_increment == chan) {
1286 have_chan_pwr = true;
1287 chan_pwr = triplet->chans.max_power;
1296 country_ie_len -= 3;
1302 new_ap_level = max_t(int, 0, chan_pwr - *pwr_constr_elem);
1304 if (sdata->ap_power_level == new_ap_level)
1308 "Limiting TX power to %d (%d - %d) dBm as advertised by %pM\n",
1309 new_ap_level, chan_pwr, *pwr_constr_elem,
1310 sdata->u.mgd.bssid);
1311 sdata->ap_power_level = new_ap_level;
1312 if (__ieee80211_recalc_txpower(sdata))
1313 return BSS_CHANGED_TXPOWER;
1318 static void ieee80211_enable_ps(struct ieee80211_local *local,
1319 struct ieee80211_sub_if_data *sdata)
1321 struct ieee80211_conf *conf = &local->hw.conf;
1324 * If we are scanning right now then the parameters will
1325 * take effect when scan finishes.
1327 if (local->scanning)
1330 if (conf->dynamic_ps_timeout > 0 &&
1331 !(local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)) {
1332 mod_timer(&local->dynamic_ps_timer, jiffies +
1333 msecs_to_jiffies(conf->dynamic_ps_timeout));
1335 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
1336 ieee80211_send_nullfunc(local, sdata, 1);
1338 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
1339 (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS))
1342 conf->flags |= IEEE80211_CONF_PS;
1343 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1347 static void ieee80211_change_ps(struct ieee80211_local *local)
1349 struct ieee80211_conf *conf = &local->hw.conf;
1351 if (local->ps_sdata) {
1352 ieee80211_enable_ps(local, local->ps_sdata);
1353 } else if (conf->flags & IEEE80211_CONF_PS) {
1354 conf->flags &= ~IEEE80211_CONF_PS;
1355 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1356 del_timer_sync(&local->dynamic_ps_timer);
1357 cancel_work_sync(&local->dynamic_ps_enable_work);
1361 static bool ieee80211_powersave_allowed(struct ieee80211_sub_if_data *sdata)
1363 struct ieee80211_if_managed *mgd = &sdata->u.mgd;
1364 struct sta_info *sta = NULL;
1365 bool authorized = false;
1367 if (!mgd->powersave)
1373 if (!mgd->associated)
1376 if (mgd->flags & (IEEE80211_STA_BEACON_POLL |
1377 IEEE80211_STA_CONNECTION_POLL))
1380 if (!mgd->have_beacon)
1384 sta = sta_info_get(sdata, mgd->bssid);
1386 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
1392 /* need to hold RTNL or interface lock */
1393 void ieee80211_recalc_ps(struct ieee80211_local *local, s32 latency)
1395 struct ieee80211_sub_if_data *sdata, *found = NULL;
1399 if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS)) {
1400 local->ps_sdata = NULL;
1404 list_for_each_entry(sdata, &local->interfaces, list) {
1405 if (!ieee80211_sdata_running(sdata))
1407 if (sdata->vif.type == NL80211_IFTYPE_AP) {
1408 /* If an AP vif is found, then disable PS
1409 * by setting the count to zero thereby setting
1415 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1421 if (count == 1 && ieee80211_powersave_allowed(found)) {
1425 latency = pm_qos_request(PM_QOS_NETWORK_LATENCY);
1427 beaconint_us = ieee80211_tu_to_usec(
1428 found->vif.bss_conf.beacon_int);
1430 timeout = local->dynamic_ps_forced_timeout;
1433 * Go to full PSM if the user configures a very low
1434 * latency requirement.
1435 * The 2000 second value is there for compatibility
1436 * until the PM_QOS_NETWORK_LATENCY is configured
1439 if (latency > (1900 * USEC_PER_MSEC) &&
1440 latency != (2000 * USEC_PER_SEC))
1445 local->hw.conf.dynamic_ps_timeout = timeout;
1447 if (beaconint_us > latency) {
1448 local->ps_sdata = NULL;
1451 u8 dtimper = found->u.mgd.dtim_period;
1453 /* If the TIM IE is invalid, pretend the value is 1 */
1456 else if (dtimper > 1)
1457 maxslp = min_t(int, dtimper,
1458 latency / beaconint_us);
1460 local->hw.conf.max_sleep_period = maxslp;
1461 local->hw.conf.ps_dtim_period = dtimper;
1462 local->ps_sdata = found;
1465 local->ps_sdata = NULL;
1468 ieee80211_change_ps(local);
1471 void ieee80211_recalc_ps_vif(struct ieee80211_sub_if_data *sdata)
1473 bool ps_allowed = ieee80211_powersave_allowed(sdata);
1475 if (sdata->vif.bss_conf.ps != ps_allowed) {
1476 sdata->vif.bss_conf.ps = ps_allowed;
1477 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_PS);
1481 void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
1483 struct ieee80211_local *local =
1484 container_of(work, struct ieee80211_local,
1485 dynamic_ps_disable_work);
1487 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1488 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1489 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1492 ieee80211_wake_queues_by_reason(&local->hw,
1493 IEEE80211_MAX_QUEUE_MAP,
1494 IEEE80211_QUEUE_STOP_REASON_PS);
1497 void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
1499 struct ieee80211_local *local =
1500 container_of(work, struct ieee80211_local,
1501 dynamic_ps_enable_work);
1502 struct ieee80211_sub_if_data *sdata = local->ps_sdata;
1503 struct ieee80211_if_managed *ifmgd;
1504 unsigned long flags;
1507 /* can only happen when PS was just disabled anyway */
1511 ifmgd = &sdata->u.mgd;
1513 if (local->hw.conf.flags & IEEE80211_CONF_PS)
1516 if (local->hw.conf.dynamic_ps_timeout > 0) {
1517 /* don't enter PS if TX frames are pending */
1518 if (drv_tx_frames_pending(local)) {
1519 mod_timer(&local->dynamic_ps_timer, jiffies +
1521 local->hw.conf.dynamic_ps_timeout));
1526 * transmission can be stopped by others which leads to
1527 * dynamic_ps_timer expiry. Postpone the ps timer if it
1528 * is not the actual idle state.
1530 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
1531 for (q = 0; q < local->hw.queues; q++) {
1532 if (local->queue_stop_reasons[q]) {
1533 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1535 mod_timer(&local->dynamic_ps_timer, jiffies +
1537 local->hw.conf.dynamic_ps_timeout));
1541 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
1544 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
1545 !(ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
1546 if (drv_tx_frames_pending(local)) {
1547 mod_timer(&local->dynamic_ps_timer, jiffies +
1549 local->hw.conf.dynamic_ps_timeout));
1551 ieee80211_send_nullfunc(local, sdata, 1);
1552 /* Flush to get the tx status of nullfunc frame */
1553 ieee80211_flush_queues(local, sdata);
1557 if (!((local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) &&
1558 (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)) ||
1559 (ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
1560 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
1561 local->hw.conf.flags |= IEEE80211_CONF_PS;
1562 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1566 void ieee80211_dynamic_ps_timer(unsigned long data)
1568 struct ieee80211_local *local = (void *) data;
1570 if (local->quiescing || local->suspended)
1573 ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
1576 void ieee80211_dfs_cac_timer_work(struct work_struct *work)
1578 struct delayed_work *delayed_work =
1579 container_of(work, struct delayed_work, work);
1580 struct ieee80211_sub_if_data *sdata =
1581 container_of(delayed_work, struct ieee80211_sub_if_data,
1582 dfs_cac_timer_work);
1584 ieee80211_vif_release_channel(sdata);
1586 cfg80211_cac_event(sdata->dev, NL80211_RADAR_CAC_FINISHED, GFP_KERNEL);
1590 static bool ieee80211_sta_wmm_params(struct ieee80211_local *local,
1591 struct ieee80211_sub_if_data *sdata,
1592 const u8 *wmm_param, size_t wmm_param_len)
1594 struct ieee80211_tx_queue_params params;
1595 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1599 u8 uapsd_queues = 0;
1601 if (!local->ops->conf_tx)
1604 if (local->hw.queues < IEEE80211_NUM_ACS)
1610 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
1613 if (ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED)
1614 uapsd_queues = ifmgd->uapsd_queues;
1616 count = wmm_param[6] & 0x0f;
1617 if (count == ifmgd->wmm_last_param_set)
1619 ifmgd->wmm_last_param_set = count;
1621 pos = wmm_param + 8;
1622 left = wmm_param_len - 8;
1624 memset(¶ms, 0, sizeof(params));
1627 for (; left >= 4; left -= 4, pos += 4) {
1628 int aci = (pos[0] >> 5) & 0x03;
1629 int acm = (pos[0] >> 4) & 0x01;
1637 sdata->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
1638 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
1644 sdata->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
1645 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
1651 sdata->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
1652 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
1659 sdata->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
1660 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
1665 params.aifs = pos[0] & 0x0f;
1666 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
1667 params.cw_min = ecw2cw(pos[1] & 0x0f);
1668 params.txop = get_unaligned_le16(pos + 2);
1670 params.uapsd = uapsd;
1673 "WMM queue=%d aci=%d acm=%d aifs=%d cWmin=%d cWmax=%d txop=%d uapsd=%d\n",
1675 params.aifs, params.cw_min, params.cw_max,
1676 params.txop, params.uapsd);
1677 sdata->tx_conf[queue] = params;
1678 if (drv_conf_tx(local, sdata, queue, ¶ms))
1680 "failed to set TX queue parameters for queue %d\n",
1684 /* enable WMM or activate new settings */
1685 sdata->vif.bss_conf.qos = true;
1689 static void __ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
1691 lockdep_assert_held(&sdata->local->mtx);
1693 sdata->u.mgd.flags &= ~(IEEE80211_STA_CONNECTION_POLL |
1694 IEEE80211_STA_BEACON_POLL);
1695 ieee80211_run_deferred_scan(sdata->local);
1698 static void ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
1700 mutex_lock(&sdata->local->mtx);
1701 __ieee80211_stop_poll(sdata);
1702 mutex_unlock(&sdata->local->mtx);
1705 static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
1706 u16 capab, bool erp_valid, u8 erp)
1708 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
1710 bool use_protection;
1711 bool use_short_preamble;
1712 bool use_short_slot;
1715 use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
1716 use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
1718 use_protection = false;
1719 use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
1722 use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
1723 if (ieee80211_get_sdata_band(sdata) == IEEE80211_BAND_5GHZ)
1724 use_short_slot = true;
1726 if (use_protection != bss_conf->use_cts_prot) {
1727 bss_conf->use_cts_prot = use_protection;
1728 changed |= BSS_CHANGED_ERP_CTS_PROT;
1731 if (use_short_preamble != bss_conf->use_short_preamble) {
1732 bss_conf->use_short_preamble = use_short_preamble;
1733 changed |= BSS_CHANGED_ERP_PREAMBLE;
1736 if (use_short_slot != bss_conf->use_short_slot) {
1737 bss_conf->use_short_slot = use_short_slot;
1738 changed |= BSS_CHANGED_ERP_SLOT;
1744 static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
1745 struct cfg80211_bss *cbss,
1746 u32 bss_info_changed)
1748 struct ieee80211_bss *bss = (void *)cbss->priv;
1749 struct ieee80211_local *local = sdata->local;
1750 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
1752 bss_info_changed |= BSS_CHANGED_ASSOC;
1753 bss_info_changed |= ieee80211_handle_bss_capability(sdata,
1754 bss_conf->assoc_capability, bss->has_erp_value, bss->erp_value);
1756 sdata->u.mgd.beacon_timeout = usecs_to_jiffies(ieee80211_tu_to_usec(
1757 beacon_loss_count * bss_conf->beacon_int));
1759 sdata->u.mgd.associated = cbss;
1760 memcpy(sdata->u.mgd.bssid, cbss->bssid, ETH_ALEN);
1762 sdata->u.mgd.flags |= IEEE80211_STA_RESET_SIGNAL_AVE;
1764 if (sdata->vif.p2p) {
1765 const struct cfg80211_bss_ies *ies;
1768 ies = rcu_dereference(cbss->ies);
1772 ret = cfg80211_get_p2p_attr(
1773 ies->data, ies->len,
1774 IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
1775 (u8 *) &bss_conf->p2p_noa_attr,
1776 sizeof(bss_conf->p2p_noa_attr));
1778 sdata->u.mgd.p2p_noa_index =
1779 bss_conf->p2p_noa_attr.index;
1780 bss_info_changed |= BSS_CHANGED_P2P_PS;
1786 /* just to be sure */
1787 ieee80211_stop_poll(sdata);
1789 ieee80211_led_assoc(local, 1);
1791 if (sdata->u.mgd.have_beacon) {
1793 * If the AP is buggy we may get here with no DTIM period
1794 * known, so assume it's 1 which is the only safe assumption
1795 * in that case, although if the TIM IE is broken powersave
1796 * probably just won't work at all.
1798 bss_conf->dtim_period = sdata->u.mgd.dtim_period ?: 1;
1799 bss_conf->beacon_rate = bss->beacon_rate;
1800 bss_info_changed |= BSS_CHANGED_BEACON_INFO;
1802 bss_conf->beacon_rate = NULL;
1803 bss_conf->dtim_period = 0;
1806 bss_conf->assoc = 1;
1808 /* Tell the driver to monitor connection quality (if supported) */
1809 if (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI &&
1810 bss_conf->cqm_rssi_thold)
1811 bss_info_changed |= BSS_CHANGED_CQM;
1813 /* Enable ARP filtering */
1814 if (bss_conf->arp_addr_cnt)
1815 bss_info_changed |= BSS_CHANGED_ARP_FILTER;
1817 ieee80211_bss_info_change_notify(sdata, bss_info_changed);
1819 mutex_lock(&local->iflist_mtx);
1820 ieee80211_recalc_ps(local, -1);
1821 mutex_unlock(&local->iflist_mtx);
1823 ieee80211_recalc_smps(sdata);
1824 ieee80211_recalc_ps_vif(sdata);
1826 netif_carrier_on(sdata->dev);
1829 static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
1830 u16 stype, u16 reason, bool tx,
1833 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1834 struct ieee80211_local *local = sdata->local;
1837 sdata_assert_lock(sdata);
1839 if (WARN_ON_ONCE(tx && !frame_buf))
1842 if (WARN_ON(!ifmgd->associated))
1845 ieee80211_stop_poll(sdata);
1847 ifmgd->associated = NULL;
1848 netif_carrier_off(sdata->dev);
1851 * if we want to get out of ps before disassoc (why?) we have
1852 * to do it before sending disassoc, as otherwise the null-packet
1855 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1856 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1857 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1859 local->ps_sdata = NULL;
1861 /* disable per-vif ps */
1862 ieee80211_recalc_ps_vif(sdata);
1864 /* flush out any pending frame (e.g. DELBA) before deauth/disassoc */
1866 ieee80211_flush_queues(local, sdata);
1868 /* deauthenticate/disassociate now */
1869 if (tx || frame_buf)
1870 ieee80211_send_deauth_disassoc(sdata, ifmgd->bssid, stype,
1871 reason, tx, frame_buf);
1873 /* flush out frame */
1875 ieee80211_flush_queues(local, sdata);
1877 /* clear bssid only after building the needed mgmt frames */
1878 memset(ifmgd->bssid, 0, ETH_ALEN);
1880 /* remove AP and TDLS peers */
1881 sta_info_flush_defer(sdata);
1883 /* finally reset all BSS / config parameters */
1884 changed |= ieee80211_reset_erp_info(sdata);
1886 ieee80211_led_assoc(local, 0);
1887 changed |= BSS_CHANGED_ASSOC;
1888 sdata->vif.bss_conf.assoc = false;
1890 ifmgd->p2p_noa_index = -1;
1891 memset(&sdata->vif.bss_conf.p2p_noa_attr, 0,
1892 sizeof(sdata->vif.bss_conf.p2p_noa_attr));
1894 /* on the next assoc, re-program HT/VHT parameters */
1895 memset(&ifmgd->ht_capa, 0, sizeof(ifmgd->ht_capa));
1896 memset(&ifmgd->ht_capa_mask, 0, sizeof(ifmgd->ht_capa_mask));
1897 memset(&ifmgd->vht_capa, 0, sizeof(ifmgd->vht_capa));
1898 memset(&ifmgd->vht_capa_mask, 0, sizeof(ifmgd->vht_capa_mask));
1900 sdata->ap_power_level = IEEE80211_UNSET_POWER_LEVEL;
1902 del_timer_sync(&local->dynamic_ps_timer);
1903 cancel_work_sync(&local->dynamic_ps_enable_work);
1905 /* Disable ARP filtering */
1906 if (sdata->vif.bss_conf.arp_addr_cnt)
1907 changed |= BSS_CHANGED_ARP_FILTER;
1909 sdata->vif.bss_conf.qos = false;
1910 changed |= BSS_CHANGED_QOS;
1912 /* The BSSID (not really interesting) and HT changed */
1913 changed |= BSS_CHANGED_BSSID | BSS_CHANGED_HT;
1914 ieee80211_bss_info_change_notify(sdata, changed);
1916 /* disassociated - set to defaults now */
1917 ieee80211_set_wmm_default(sdata, false);
1919 del_timer_sync(&sdata->u.mgd.conn_mon_timer);
1920 del_timer_sync(&sdata->u.mgd.bcn_mon_timer);
1921 del_timer_sync(&sdata->u.mgd.timer);
1922 del_timer_sync(&sdata->u.mgd.chswitch_timer);
1924 sdata->vif.bss_conf.dtim_period = 0;
1925 sdata->vif.bss_conf.beacon_rate = NULL;
1927 ifmgd->have_beacon = false;
1930 ieee80211_vif_release_channel(sdata);
1933 void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
1934 struct ieee80211_hdr *hdr)
1937 * We can postpone the mgd.timer whenever receiving unicast frames
1938 * from AP because we know that the connection is working both ways
1939 * at that time. But multicast frames (and hence also beacons) must
1940 * be ignored here, because we need to trigger the timer during
1941 * data idle periods for sending the periodic probe request to the
1942 * AP we're connected to.
1944 if (is_multicast_ether_addr(hdr->addr1))
1947 ieee80211_sta_reset_conn_monitor(sdata);
1950 static void ieee80211_reset_ap_probe(struct ieee80211_sub_if_data *sdata)
1952 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1953 struct ieee80211_local *local = sdata->local;
1955 mutex_lock(&local->mtx);
1956 if (!(ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
1957 IEEE80211_STA_CONNECTION_POLL))) {
1958 mutex_unlock(&local->mtx);
1962 __ieee80211_stop_poll(sdata);
1964 mutex_lock(&local->iflist_mtx);
1965 ieee80211_recalc_ps(local, -1);
1966 mutex_unlock(&local->iflist_mtx);
1968 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
1972 * We've received a probe response, but are not sure whether
1973 * we have or will be receiving any beacons or data, so let's
1974 * schedule the timers again, just in case.
1976 ieee80211_sta_reset_beacon_monitor(sdata);
1978 mod_timer(&ifmgd->conn_mon_timer,
1979 round_jiffies_up(jiffies +
1980 IEEE80211_CONNECTION_IDLE_TIME));
1982 mutex_unlock(&local->mtx);
1985 void ieee80211_sta_tx_notify(struct ieee80211_sub_if_data *sdata,
1986 struct ieee80211_hdr *hdr, bool ack)
1988 if (!ieee80211_is_data(hdr->frame_control))
1991 if (ieee80211_is_nullfunc(hdr->frame_control) &&
1992 sdata->u.mgd.probe_send_count > 0) {
1994 ieee80211_sta_reset_conn_monitor(sdata);
1996 sdata->u.mgd.nullfunc_failed = true;
1997 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
2002 ieee80211_sta_reset_conn_monitor(sdata);
2005 static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
2007 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2009 u8 *dst = ifmgd->associated->bssid;
2010 u8 unicast_limit = max(1, max_probe_tries - 3);
2013 * Try sending broadcast probe requests for the last three
2014 * probe requests after the first ones failed since some
2015 * buggy APs only support broadcast probe requests.
2017 if (ifmgd->probe_send_count >= unicast_limit)
2021 * When the hardware reports an accurate Tx ACK status, it's
2022 * better to send a nullfunc frame instead of a probe request,
2023 * as it will kick us off the AP quickly if we aren't associated
2024 * anymore. The timeout will be reset if the frame is ACKed by
2027 ifmgd->probe_send_count++;
2029 if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
2030 ifmgd->nullfunc_failed = false;
2031 ieee80211_send_nullfunc(sdata->local, sdata, 0);
2036 ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
2037 if (WARN_ON_ONCE(ssid == NULL))
2042 ieee80211_send_probe_req(sdata, dst, ssid + 2, ssid_len, NULL,
2043 0, (u32) -1, true, 0,
2044 ifmgd->associated->channel, false);
2048 ifmgd->probe_timeout = jiffies + msecs_to_jiffies(probe_wait_ms);
2049 run_again(sdata, ifmgd->probe_timeout);
2050 if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
2051 ieee80211_flush_queues(sdata->local, sdata);
2054 static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
2057 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2058 bool already = false;
2060 if (!ieee80211_sdata_running(sdata))
2065 if (!ifmgd->associated)
2068 mutex_lock(&sdata->local->mtx);
2070 if (sdata->local->tmp_channel || sdata->local->scanning) {
2071 mutex_unlock(&sdata->local->mtx);
2076 mlme_dbg_ratelimited(sdata,
2077 "detected beacon loss from AP (missed %d beacons) - probing\n",
2080 ieee80211_cqm_rssi_notify(&sdata->vif,
2081 NL80211_CQM_RSSI_BEACON_LOSS_EVENT,
2086 * The driver/our work has already reported this event or the
2087 * connection monitoring has kicked in and we have already sent
2088 * a probe request. Or maybe the AP died and the driver keeps
2089 * reporting until we disassociate...
2091 * In either case we have to ignore the current call to this
2092 * function (except for setting the correct probe reason bit)
2093 * because otherwise we would reset the timer every time and
2094 * never check whether we received a probe response!
2096 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
2097 IEEE80211_STA_CONNECTION_POLL))
2101 ifmgd->flags |= IEEE80211_STA_BEACON_POLL;
2103 ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
2105 mutex_unlock(&sdata->local->mtx);
2110 mutex_lock(&sdata->local->iflist_mtx);
2111 ieee80211_recalc_ps(sdata->local, -1);
2112 mutex_unlock(&sdata->local->iflist_mtx);
2114 ifmgd->probe_send_count = 0;
2115 ieee80211_mgd_probe_ap_send(sdata);
2117 sdata_unlock(sdata);
2120 struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw,
2121 struct ieee80211_vif *vif)
2123 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2124 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2125 struct cfg80211_bss *cbss;
2126 struct sk_buff *skb;
2130 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
2133 sdata_assert_lock(sdata);
2135 if (ifmgd->associated)
2136 cbss = ifmgd->associated;
2137 else if (ifmgd->auth_data)
2138 cbss = ifmgd->auth_data->bss;
2139 else if (ifmgd->assoc_data)
2140 cbss = ifmgd->assoc_data->bss;
2145 ssid = ieee80211_bss_get_ie(cbss, WLAN_EID_SSID);
2146 if (WARN_ON_ONCE(ssid == NULL))
2151 skb = ieee80211_build_probe_req(sdata, cbss->bssid,
2152 (u32) -1, cbss->channel,
2159 EXPORT_SYMBOL(ieee80211_ap_probereq_get);
2161 static void __ieee80211_disconnect(struct ieee80211_sub_if_data *sdata)
2163 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2164 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
2167 if (!ifmgd->associated) {
2168 sdata_unlock(sdata);
2172 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
2173 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
2175 ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
2176 ieee80211_wake_queues_by_reason(&sdata->local->hw,
2177 IEEE80211_MAX_QUEUE_MAP,
2178 IEEE80211_QUEUE_STOP_REASON_CSA);
2180 cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
2181 IEEE80211_DEAUTH_FRAME_LEN);
2182 sdata_unlock(sdata);
2185 static void ieee80211_beacon_connection_loss_work(struct work_struct *work)
2187 struct ieee80211_sub_if_data *sdata =
2188 container_of(work, struct ieee80211_sub_if_data,
2189 u.mgd.beacon_connection_loss_work);
2190 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2191 struct sta_info *sta;
2193 if (ifmgd->associated) {
2195 sta = sta_info_get(sdata, ifmgd->bssid);
2197 sta->beacon_loss_count++;
2201 if (ifmgd->connection_loss) {
2202 sdata_info(sdata, "Connection to AP %pM lost\n",
2204 __ieee80211_disconnect(sdata);
2206 ieee80211_mgd_probe_ap(sdata, true);
2210 static void ieee80211_csa_connection_drop_work(struct work_struct *work)
2212 struct ieee80211_sub_if_data *sdata =
2213 container_of(work, struct ieee80211_sub_if_data,
2214 u.mgd.csa_connection_drop_work);
2216 __ieee80211_disconnect(sdata);
2219 void ieee80211_beacon_loss(struct ieee80211_vif *vif)
2221 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2222 struct ieee80211_hw *hw = &sdata->local->hw;
2224 trace_api_beacon_loss(sdata);
2226 sdata->u.mgd.connection_loss = false;
2227 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
2229 EXPORT_SYMBOL(ieee80211_beacon_loss);
2231 void ieee80211_connection_loss(struct ieee80211_vif *vif)
2233 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2234 struct ieee80211_hw *hw = &sdata->local->hw;
2236 trace_api_connection_loss(sdata);
2238 sdata->u.mgd.connection_loss = true;
2239 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
2241 EXPORT_SYMBOL(ieee80211_connection_loss);
2244 static void ieee80211_destroy_auth_data(struct ieee80211_sub_if_data *sdata,
2247 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
2249 sdata_assert_lock(sdata);
2252 sta_info_destroy_addr(sdata, auth_data->bss->bssid);
2254 memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
2255 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
2256 sdata->u.mgd.flags = 0;
2257 ieee80211_vif_release_channel(sdata);
2260 cfg80211_put_bss(sdata->local->hw.wiphy, auth_data->bss);
2262 sdata->u.mgd.auth_data = NULL;
2265 static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
2266 struct ieee80211_mgmt *mgmt, size_t len)
2268 struct ieee80211_local *local = sdata->local;
2269 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
2271 struct ieee802_11_elems elems;
2274 pos = mgmt->u.auth.variable;
2275 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), false, &elems);
2276 if (!elems.challenge)
2278 auth_data->expected_transaction = 4;
2279 drv_mgd_prepare_tx(sdata->local, sdata);
2280 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
2281 tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
2282 IEEE80211_TX_INTFL_MLME_CONN_TX;
2283 ieee80211_send_auth(sdata, 3, auth_data->algorithm, 0,
2284 elems.challenge - 2, elems.challenge_len + 2,
2285 auth_data->bss->bssid, auth_data->bss->bssid,
2286 auth_data->key, auth_data->key_len,
2287 auth_data->key_idx, tx_flags);
2290 static void ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
2291 struct ieee80211_mgmt *mgmt, size_t len)
2293 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2295 u16 auth_alg, auth_transaction, status_code;
2296 struct sta_info *sta;
2298 sdata_assert_lock(sdata);
2303 if (!ifmgd->auth_data || ifmgd->auth_data->done)
2306 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
2308 if (!ether_addr_equal(bssid, mgmt->bssid))
2311 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
2312 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
2313 status_code = le16_to_cpu(mgmt->u.auth.status_code);
2315 if (auth_alg != ifmgd->auth_data->algorithm ||
2316 auth_transaction != ifmgd->auth_data->expected_transaction) {
2317 sdata_info(sdata, "%pM unexpected authentication state: alg %d (expected %d) transact %d (expected %d)\n",
2318 mgmt->sa, auth_alg, ifmgd->auth_data->algorithm,
2320 ifmgd->auth_data->expected_transaction);
2324 if (status_code != WLAN_STATUS_SUCCESS) {
2325 sdata_info(sdata, "%pM denied authentication (status %d)\n",
2326 mgmt->sa, status_code);
2327 ieee80211_destroy_auth_data(sdata, false);
2328 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
2332 switch (ifmgd->auth_data->algorithm) {
2333 case WLAN_AUTH_OPEN:
2334 case WLAN_AUTH_LEAP:
2338 case WLAN_AUTH_SHARED_KEY:
2339 if (ifmgd->auth_data->expected_transaction != 4) {
2340 ieee80211_auth_challenge(sdata, mgmt, len);
2341 /* need another frame */
2346 WARN_ONCE(1, "invalid auth alg %d",
2347 ifmgd->auth_data->algorithm);
2351 sdata_info(sdata, "authenticated\n");
2352 ifmgd->auth_data->done = true;
2353 ifmgd->auth_data->timeout = jiffies + IEEE80211_AUTH_WAIT_ASSOC;
2354 ifmgd->auth_data->timeout_started = true;
2355 run_again(sdata, ifmgd->auth_data->timeout);
2357 if (ifmgd->auth_data->algorithm == WLAN_AUTH_SAE &&
2358 ifmgd->auth_data->expected_transaction != 2) {
2360 * Report auth frame to user space for processing since another
2361 * round of Authentication frames is still needed.
2363 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
2367 /* move station state to auth */
2368 mutex_lock(&sdata->local->sta_mtx);
2369 sta = sta_info_get(sdata, bssid);
2371 WARN_ONCE(1, "%s: STA %pM not found", sdata->name, bssid);
2374 if (sta_info_move_state(sta, IEEE80211_STA_AUTH)) {
2375 sdata_info(sdata, "failed moving %pM to auth\n", bssid);
2378 mutex_unlock(&sdata->local->sta_mtx);
2380 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
2383 mutex_unlock(&sdata->local->sta_mtx);
2384 /* ignore frame -- wait for timeout */
2388 static void ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
2389 struct ieee80211_mgmt *mgmt, size_t len)
2391 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2392 const u8 *bssid = NULL;
2395 sdata_assert_lock(sdata);
2400 if (!ifmgd->associated ||
2401 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
2404 bssid = ifmgd->associated->bssid;
2406 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
2408 sdata_info(sdata, "deauthenticated from %pM (Reason: %u)\n",
2409 bssid, reason_code);
2411 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
2413 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
2417 static void ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
2418 struct ieee80211_mgmt *mgmt, size_t len)
2420 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2423 sdata_assert_lock(sdata);
2428 if (!ifmgd->associated ||
2429 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
2432 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
2434 sdata_info(sdata, "disassociated from %pM (Reason: %u)\n",
2435 mgmt->sa, reason_code);
2437 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
2439 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
2442 static void ieee80211_get_rates(struct ieee80211_supported_band *sband,
2443 u8 *supp_rates, unsigned int supp_rates_len,
2444 u32 *rates, u32 *basic_rates,
2445 bool *have_higher_than_11mbit,
2446 int *min_rate, int *min_rate_index)
2450 for (i = 0; i < supp_rates_len; i++) {
2451 int rate = (supp_rates[i] & 0x7f) * 5;
2452 bool is_basic = !!(supp_rates[i] & 0x80);
2455 *have_higher_than_11mbit = true;
2458 * BSS_MEMBERSHIP_SELECTOR_HT_PHY is defined in 802.11n-2009
2459 * 7.3.2.2 as a magic value instead of a rate. Hence, skip it.
2461 * Note: Even through the membership selector and the basic
2462 * rate flag share the same bit, they are not exactly
2465 if (!!(supp_rates[i] & 0x80) &&
2466 (supp_rates[i] & 0x7f) == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
2469 for (j = 0; j < sband->n_bitrates; j++) {
2470 if (sband->bitrates[j].bitrate == rate) {
2473 *basic_rates |= BIT(j);
2474 if (rate < *min_rate) {
2476 *min_rate_index = j;
2484 static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata,
2487 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
2489 sdata_assert_lock(sdata);
2492 sta_info_destroy_addr(sdata, assoc_data->bss->bssid);
2494 memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
2495 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
2496 sdata->u.mgd.flags = 0;
2497 ieee80211_vif_release_channel(sdata);
2501 sdata->u.mgd.assoc_data = NULL;
2504 static bool ieee80211_assoc_success(struct ieee80211_sub_if_data *sdata,
2505 struct cfg80211_bss *cbss,
2506 struct ieee80211_mgmt *mgmt, size_t len)
2508 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2509 struct ieee80211_local *local = sdata->local;
2510 struct ieee80211_supported_band *sband;
2511 struct sta_info *sta;
2513 u16 capab_info, aid;
2514 struct ieee802_11_elems elems;
2515 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
2516 const struct cfg80211_bss_ies *bss_ies = NULL;
2517 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
2522 /* AssocResp and ReassocResp have identical structure */
2524 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
2525 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
2527 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
2528 sdata_info(sdata, "invalid AID value 0x%x; bits 15:14 not set\n",
2530 aid &= ~(BIT(15) | BIT(14));
2532 ifmgd->broken_ap = false;
2534 if (aid == 0 || aid > IEEE80211_MAX_AID) {
2535 sdata_info(sdata, "invalid AID value %d (out of range), turn off PS\n",
2538 ifmgd->broken_ap = true;
2541 pos = mgmt->u.assoc_resp.variable;
2542 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), false, &elems);
2544 if (!elems.supp_rates) {
2545 sdata_info(sdata, "no SuppRates element in AssocResp\n");
2552 * Some APs are erroneously not including some information in their
2553 * (re)association response frames. Try to recover by using the data
2554 * from the beacon or probe response. This seems to afflict mobile
2555 * 2G/3G/4G wifi routers, reported models include the "Onda PN51T",
2556 * "Vodafone PocketWiFi 2", "ZTE MF60" and a similar T-Mobile device.
2558 if ((assoc_data->wmm && !elems.wmm_param) ||
2559 (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
2560 (!elems.ht_cap_elem || !elems.ht_operation)) ||
2561 (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
2562 (!elems.vht_cap_elem || !elems.vht_operation))) {
2563 const struct cfg80211_bss_ies *ies;
2564 struct ieee802_11_elems bss_elems;
2567 ies = rcu_dereference(cbss->ies);
2569 bss_ies = kmemdup(ies, sizeof(*ies) + ies->len,
2575 ieee802_11_parse_elems(bss_ies->data, bss_ies->len,
2577 if (assoc_data->wmm &&
2578 !elems.wmm_param && bss_elems.wmm_param) {
2579 elems.wmm_param = bss_elems.wmm_param;
2581 "AP bug: WMM param missing from AssocResp\n");
2585 * Also check if we requested HT/VHT, otherwise the AP doesn't
2586 * have to include the IEs in the (re)association response.
2588 if (!elems.ht_cap_elem && bss_elems.ht_cap_elem &&
2589 !(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
2590 elems.ht_cap_elem = bss_elems.ht_cap_elem;
2592 "AP bug: HT capability missing from AssocResp\n");
2594 if (!elems.ht_operation && bss_elems.ht_operation &&
2595 !(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
2596 elems.ht_operation = bss_elems.ht_operation;
2598 "AP bug: HT operation missing from AssocResp\n");
2600 if (!elems.vht_cap_elem && bss_elems.vht_cap_elem &&
2601 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)) {
2602 elems.vht_cap_elem = bss_elems.vht_cap_elem;
2604 "AP bug: VHT capa missing from AssocResp\n");
2606 if (!elems.vht_operation && bss_elems.vht_operation &&
2607 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)) {
2608 elems.vht_operation = bss_elems.vht_operation;
2610 "AP bug: VHT operation missing from AssocResp\n");
2615 * We previously checked these in the beacon/probe response, so
2616 * they should be present here. This is just a safety net.
2618 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
2619 (!elems.wmm_param || !elems.ht_cap_elem || !elems.ht_operation)) {
2621 "HT AP is missing WMM params or HT capability/operation\n");
2626 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
2627 (!elems.vht_cap_elem || !elems.vht_operation)) {
2629 "VHT AP is missing VHT capability/operation\n");
2634 mutex_lock(&sdata->local->sta_mtx);
2636 * station info was already allocated and inserted before
2637 * the association and should be available to us
2639 sta = sta_info_get(sdata, cbss->bssid);
2640 if (WARN_ON(!sta)) {
2641 mutex_unlock(&sdata->local->sta_mtx);
2646 sband = local->hw.wiphy->bands[ieee80211_get_sdata_band(sdata)];
2648 /* Set up internal HT/VHT capabilities */
2649 if (elems.ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
2650 ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
2651 elems.ht_cap_elem, sta);
2653 if (elems.vht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
2654 ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband,
2655 elems.vht_cap_elem, sta);
2658 * Some APs, e.g. Netgear WNDR3700, report invalid HT operation data
2659 * in their association response, so ignore that data for our own
2660 * configuration. If it changed since the last beacon, we'll get the
2661 * next beacon and update then.
2665 * If an operating mode notification IE is present, override the
2666 * NSS calculation (that would be done in rate_control_rate_init())
2667 * and use the # of streams from that element.
2669 if (elems.opmode_notif &&
2670 !(*elems.opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_TYPE_BF)) {
2673 nss = *elems.opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_MASK;
2674 nss >>= IEEE80211_OPMODE_NOTIF_RX_NSS_SHIFT;
2676 sta->sta.rx_nss = nss;
2679 rate_control_rate_init(sta);
2681 if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED)
2682 set_sta_flag(sta, WLAN_STA_MFP);
2684 if (elems.wmm_param)
2685 set_sta_flag(sta, WLAN_STA_WME);
2687 err = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
2688 if (!err && !(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
2689 err = sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
2692 "failed to move station %pM to desired state\n",
2694 WARN_ON(__sta_info_destroy(sta));
2695 mutex_unlock(&sdata->local->sta_mtx);
2700 mutex_unlock(&sdata->local->sta_mtx);
2703 * Always handle WMM once after association regardless
2704 * of the first value the AP uses. Setting -1 here has
2705 * that effect because the AP values is an unsigned
2708 ifmgd->wmm_last_param_set = -1;
2710 if (elems.wmm_param)
2711 ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
2712 elems.wmm_param_len);
2714 ieee80211_set_wmm_default(sdata, false);
2715 changed |= BSS_CHANGED_QOS;
2717 /* set AID and assoc capability,
2718 * ieee80211_set_associated() will tell the driver */
2719 bss_conf->aid = aid;
2720 bss_conf->assoc_capability = capab_info;
2721 ieee80211_set_associated(sdata, cbss, changed);
2724 * If we're using 4-addr mode, let the AP know that we're
2725 * doing so, so that it can create the STA VLAN on its side
2727 if (ifmgd->use_4addr)
2728 ieee80211_send_4addr_nullfunc(local, sdata);
2731 * Start timer to probe the connection to the AP now.
2732 * Also start the timer that will detect beacon loss.
2734 ieee80211_sta_rx_notify(sdata, (struct ieee80211_hdr *)mgmt);
2735 ieee80211_sta_reset_beacon_monitor(sdata);
2743 static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
2744 struct ieee80211_mgmt *mgmt,
2747 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2748 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
2749 u16 capab_info, status_code, aid;
2750 struct ieee802_11_elems elems;
2753 struct cfg80211_bss *bss;
2755 sdata_assert_lock(sdata);
2759 if (!ether_addr_equal(assoc_data->bss->bssid, mgmt->bssid))
2763 * AssocResp and ReassocResp have identical structure, so process both
2764 * of them in this function.
2770 reassoc = ieee80211_is_reassoc_req(mgmt->frame_control);
2771 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
2772 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
2773 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
2776 "RX %sssocResp from %pM (capab=0x%x status=%d aid=%d)\n",
2777 reassoc ? "Rea" : "A", mgmt->sa,
2778 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
2780 pos = mgmt->u.assoc_resp.variable;
2781 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), false, &elems);
2783 if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
2784 elems.timeout_int &&
2785 elems.timeout_int->type == WLAN_TIMEOUT_ASSOC_COMEBACK) {
2787 tu = le32_to_cpu(elems.timeout_int->value);
2788 ms = tu * 1024 / 1000;
2790 "%pM rejected association temporarily; comeback duration %u TU (%u ms)\n",
2792 assoc_data->timeout = jiffies + msecs_to_jiffies(ms);
2793 assoc_data->timeout_started = true;
2794 if (ms > IEEE80211_ASSOC_TIMEOUT)
2795 run_again(sdata, assoc_data->timeout);
2799 bss = assoc_data->bss;
2801 if (status_code != WLAN_STATUS_SUCCESS) {
2802 sdata_info(sdata, "%pM denied association (code=%d)\n",
2803 mgmt->sa, status_code);
2804 ieee80211_destroy_assoc_data(sdata, false);
2806 if (!ieee80211_assoc_success(sdata, bss, mgmt, len)) {
2807 /* oops -- internal error -- send timeout for now */
2808 ieee80211_destroy_assoc_data(sdata, false);
2809 cfg80211_assoc_timeout(sdata->dev, bss);
2812 sdata_info(sdata, "associated\n");
2815 * destroy assoc_data afterwards, as otherwise an idle
2816 * recalc after assoc_data is NULL but before associated
2817 * is set can cause the interface to go idle
2819 ieee80211_destroy_assoc_data(sdata, true);
2822 cfg80211_rx_assoc_resp(sdata->dev, bss, (u8 *)mgmt, len);
2825 static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
2826 struct ieee80211_mgmt *mgmt, size_t len,
2827 struct ieee80211_rx_status *rx_status,
2828 struct ieee802_11_elems *elems)
2830 struct ieee80211_local *local = sdata->local;
2832 struct ieee80211_bss *bss;
2833 struct ieee80211_channel *channel;
2835 sdata_assert_lock(sdata);
2837 if (elems->ds_params)
2838 freq = ieee80211_channel_to_frequency(elems->ds_params[0],
2841 freq = rx_status->freq;
2843 channel = ieee80211_get_channel(local->hw.wiphy, freq);
2845 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
2848 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
2851 ieee80211_rx_bss_put(local, bss);
2852 sdata->vif.bss_conf.beacon_rate = bss->beacon_rate;
2855 if (!sdata->u.mgd.associated ||
2856 !ether_addr_equal(mgmt->bssid, sdata->u.mgd.associated->bssid))
2859 ieee80211_sta_process_chanswitch(sdata, rx_status->mactime,
2865 static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
2866 struct sk_buff *skb)
2868 struct ieee80211_mgmt *mgmt = (void *)skb->data;
2869 struct ieee80211_if_managed *ifmgd;
2870 struct ieee80211_rx_status *rx_status = (void *) skb->cb;
2871 size_t baselen, len = skb->len;
2872 struct ieee802_11_elems elems;
2874 ifmgd = &sdata->u.mgd;
2876 sdata_assert_lock(sdata);
2878 if (!ether_addr_equal(mgmt->da, sdata->vif.addr))
2879 return; /* ignore ProbeResp to foreign address */
2881 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
2885 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
2888 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
2890 if (ifmgd->associated &&
2891 ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
2892 ieee80211_reset_ap_probe(sdata);
2894 if (ifmgd->auth_data && !ifmgd->auth_data->bss->proberesp_ies &&
2895 ether_addr_equal(mgmt->bssid, ifmgd->auth_data->bss->bssid)) {
2896 /* got probe response, continue with auth */
2897 sdata_info(sdata, "direct probe responded\n");
2898 ifmgd->auth_data->tries = 0;
2899 ifmgd->auth_data->timeout = jiffies;
2900 ifmgd->auth_data->timeout_started = true;
2901 run_again(sdata, ifmgd->auth_data->timeout);
2906 * This is the canonical list of information elements we care about,
2907 * the filter code also gives us all changes to the Microsoft OUI
2908 * (00:50:F2) vendor IE which is used for WMM which we need to track.
2910 * We implement beacon filtering in software since that means we can
2911 * avoid processing the frame here and in cfg80211, and userspace
2912 * will not be able to tell whether the hardware supports it or not.
2914 * XXX: This list needs to be dynamic -- userspace needs to be able to
2915 * add items it requires. It also needs to be able to tell us to
2916 * look out for other vendor IEs.
2918 static const u64 care_about_ies =
2919 (1ULL << WLAN_EID_COUNTRY) |
2920 (1ULL << WLAN_EID_ERP_INFO) |
2921 (1ULL << WLAN_EID_CHANNEL_SWITCH) |
2922 (1ULL << WLAN_EID_PWR_CONSTRAINT) |
2923 (1ULL << WLAN_EID_HT_CAPABILITY) |
2924 (1ULL << WLAN_EID_HT_OPERATION);
2926 static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
2927 struct ieee80211_mgmt *mgmt, size_t len,
2928 struct ieee80211_rx_status *rx_status)
2930 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2931 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
2933 struct ieee802_11_elems elems;
2934 struct ieee80211_local *local = sdata->local;
2935 struct ieee80211_chanctx_conf *chanctx_conf;
2936 struct ieee80211_channel *chan;
2937 struct sta_info *sta;
2943 u8 deauth_buf[IEEE80211_DEAUTH_FRAME_LEN];
2945 sdata_assert_lock(sdata);
2947 /* Process beacon from the current BSS */
2948 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2953 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2954 if (!chanctx_conf) {
2959 if (rx_status->freq != chanctx_conf->def.chan->center_freq) {
2963 chan = chanctx_conf->def.chan;
2966 if (ifmgd->assoc_data && ifmgd->assoc_data->need_beacon &&
2967 ether_addr_equal(mgmt->bssid, ifmgd->assoc_data->bss->bssid)) {
2968 ieee802_11_parse_elems(mgmt->u.beacon.variable,
2969 len - baselen, false, &elems);
2971 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
2972 if (elems.tim && !elems.parse_error) {
2973 const struct ieee80211_tim_ie *tim_ie = elems.tim;
2974 ifmgd->dtim_period = tim_ie->dtim_period;
2976 ifmgd->have_beacon = true;
2977 ifmgd->assoc_data->need_beacon = false;
2978 if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
2979 sdata->vif.bss_conf.sync_tsf =
2980 le64_to_cpu(mgmt->u.beacon.timestamp);
2981 sdata->vif.bss_conf.sync_device_ts =
2982 rx_status->device_timestamp;
2984 sdata->vif.bss_conf.sync_dtim_count =
2985 elems.tim->dtim_count;
2987 sdata->vif.bss_conf.sync_dtim_count = 0;
2989 /* continue assoc process */
2990 ifmgd->assoc_data->timeout = jiffies;
2991 ifmgd->assoc_data->timeout_started = true;
2992 run_again(sdata, ifmgd->assoc_data->timeout);
2996 if (!ifmgd->associated ||
2997 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
2999 bssid = ifmgd->associated->bssid;
3001 /* Track average RSSI from the Beacon frames of the current AP */
3002 ifmgd->last_beacon_signal = rx_status->signal;
3003 if (ifmgd->flags & IEEE80211_STA_RESET_SIGNAL_AVE) {
3004 ifmgd->flags &= ~IEEE80211_STA_RESET_SIGNAL_AVE;
3005 ifmgd->ave_beacon_signal = rx_status->signal * 16;
3006 ifmgd->last_cqm_event_signal = 0;
3007 ifmgd->count_beacon_signal = 1;
3008 ifmgd->last_ave_beacon_signal = 0;
3010 ifmgd->ave_beacon_signal =
3011 (IEEE80211_SIGNAL_AVE_WEIGHT * rx_status->signal * 16 +
3012 (16 - IEEE80211_SIGNAL_AVE_WEIGHT) *
3013 ifmgd->ave_beacon_signal) / 16;
3014 ifmgd->count_beacon_signal++;
3017 if (ifmgd->rssi_min_thold != ifmgd->rssi_max_thold &&
3018 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
3019 int sig = ifmgd->ave_beacon_signal;
3020 int last_sig = ifmgd->last_ave_beacon_signal;
3023 * if signal crosses either of the boundaries, invoke callback
3024 * with appropriate parameters
3026 if (sig > ifmgd->rssi_max_thold &&
3027 (last_sig <= ifmgd->rssi_min_thold || last_sig == 0)) {
3028 ifmgd->last_ave_beacon_signal = sig;
3029 drv_rssi_callback(local, sdata, RSSI_EVENT_HIGH);
3030 } else if (sig < ifmgd->rssi_min_thold &&
3031 (last_sig >= ifmgd->rssi_max_thold ||
3033 ifmgd->last_ave_beacon_signal = sig;
3034 drv_rssi_callback(local, sdata, RSSI_EVENT_LOW);
3038 if (bss_conf->cqm_rssi_thold &&
3039 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT &&
3040 !(sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI)) {
3041 int sig = ifmgd->ave_beacon_signal / 16;
3042 int last_event = ifmgd->last_cqm_event_signal;
3043 int thold = bss_conf->cqm_rssi_thold;
3044 int hyst = bss_conf->cqm_rssi_hyst;
3046 (last_event == 0 || sig < last_event - hyst)) {
3047 ifmgd->last_cqm_event_signal = sig;
3048 ieee80211_cqm_rssi_notify(
3050 NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
3052 } else if (sig > thold &&
3053 (last_event == 0 || sig > last_event + hyst)) {
3054 ifmgd->last_cqm_event_signal = sig;
3055 ieee80211_cqm_rssi_notify(
3057 NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
3062 if (ifmgd->flags & IEEE80211_STA_BEACON_POLL) {
3063 mlme_dbg_ratelimited(sdata,
3064 "cancelling AP probe due to a received beacon\n");
3065 mutex_lock(&local->mtx);
3066 ifmgd->flags &= ~IEEE80211_STA_BEACON_POLL;
3067 ieee80211_run_deferred_scan(local);
3068 mutex_unlock(&local->mtx);
3070 mutex_lock(&local->iflist_mtx);
3071 ieee80211_recalc_ps(local, -1);
3072 mutex_unlock(&local->iflist_mtx);
3076 * Push the beacon loss detection into the future since
3077 * we are processing a beacon from the AP just now.
3079 ieee80211_sta_reset_beacon_monitor(sdata);
3081 ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
3082 ncrc = ieee802_11_parse_elems_crc(mgmt->u.beacon.variable,
3083 len - baselen, false, &elems,
3084 care_about_ies, ncrc);
3086 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) {
3087 bool directed_tim = ieee80211_check_tim(elems.tim,
3091 if (local->hw.conf.dynamic_ps_timeout > 0) {
3092 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
3093 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
3094 ieee80211_hw_config(local,
3095 IEEE80211_CONF_CHANGE_PS);
3097 ieee80211_send_nullfunc(local, sdata, 0);
3098 } else if (!local->pspolling && sdata->u.mgd.powersave) {
3099 local->pspolling = true;
3102 * Here is assumed that the driver will be
3103 * able to send ps-poll frame and receive a
3104 * response even though power save mode is
3105 * enabled, but some drivers might require
3106 * to disable power save here. This needs
3107 * to be investigated.
3109 ieee80211_send_pspoll(local, sdata);
3114 if (sdata->vif.p2p) {
3115 struct ieee80211_p2p_noa_attr noa = {};
3118 ret = cfg80211_get_p2p_attr(mgmt->u.beacon.variable,
3120 IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
3121 (u8 *) &noa, sizeof(noa));
3123 if (sdata->u.mgd.p2p_noa_index != noa.index) {
3124 /* valid noa_attr and index changed */
3125 sdata->u.mgd.p2p_noa_index = noa.index;
3126 memcpy(&bss_conf->p2p_noa_attr, &noa, sizeof(noa));
3127 changed |= BSS_CHANGED_P2P_PS;
3129 * make sure we update all information, the CRC
3130 * mechanism doesn't look at P2P attributes.
3132 ifmgd->beacon_crc_valid = false;
3134 } else if (sdata->u.mgd.p2p_noa_index != -1) {
3135 /* noa_attr not found and we had valid noa_attr before */
3136 sdata->u.mgd.p2p_noa_index = -1;
3137 memset(&bss_conf->p2p_noa_attr, 0, sizeof(bss_conf->p2p_noa_attr));
3138 changed |= BSS_CHANGED_P2P_PS;
3139 ifmgd->beacon_crc_valid = false;
3143 if (ncrc == ifmgd->beacon_crc && ifmgd->beacon_crc_valid)
3145 ifmgd->beacon_crc = ncrc;
3146 ifmgd->beacon_crc_valid = true;
3148 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
3150 if (ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
3151 elems.wmm_param_len))
3152 changed |= BSS_CHANGED_QOS;
3155 * If we haven't had a beacon before, tell the driver about the
3156 * DTIM period (and beacon timing if desired) now.
3158 if (!ifmgd->have_beacon) {
3159 /* a few bogus AP send dtim_period = 0 or no TIM IE */
3161 bss_conf->dtim_period = elems.tim->dtim_period ?: 1;
3163 bss_conf->dtim_period = 1;
3165 if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
3166 sdata->vif.bss_conf.sync_tsf =
3167 le64_to_cpu(mgmt->u.beacon.timestamp);
3168 sdata->vif.bss_conf.sync_device_ts =
3169 rx_status->device_timestamp;
3171 sdata->vif.bss_conf.sync_dtim_count =
3172 elems.tim->dtim_count;
3174 sdata->vif.bss_conf.sync_dtim_count = 0;
3177 changed |= BSS_CHANGED_BEACON_INFO;
3178 ifmgd->have_beacon = true;
3180 mutex_lock(&local->iflist_mtx);
3181 ieee80211_recalc_ps(local, -1);
3182 mutex_unlock(&local->iflist_mtx);
3184 ieee80211_recalc_ps_vif(sdata);
3187 if (elems.erp_info) {
3189 erp_value = elems.erp_info[0];
3193 changed |= ieee80211_handle_bss_capability(sdata,
3194 le16_to_cpu(mgmt->u.beacon.capab_info),
3195 erp_valid, erp_value);
3197 mutex_lock(&local->sta_mtx);
3198 sta = sta_info_get(sdata, bssid);
3200 if (ieee80211_config_bw(sdata, sta, elems.ht_operation,
3201 elems.vht_operation, bssid, &changed)) {
3202 mutex_unlock(&local->sta_mtx);
3203 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
3204 WLAN_REASON_DEAUTH_LEAVING,
3206 cfg80211_tx_mlme_mgmt(sdata->dev, deauth_buf,
3207 sizeof(deauth_buf));
3211 if (sta && elems.opmode_notif)
3212 ieee80211_vht_handle_opmode(sdata, sta, *elems.opmode_notif,
3213 rx_status->band, true);
3214 mutex_unlock(&local->sta_mtx);
3216 if (elems.country_elem && elems.pwr_constr_elem &&
3217 mgmt->u.probe_resp.capab_info &
3218 cpu_to_le16(WLAN_CAPABILITY_SPECTRUM_MGMT))
3219 changed |= ieee80211_handle_pwr_constr(sdata, chan,
3221 elems.country_elem_len,
3222 elems.pwr_constr_elem);
3224 ieee80211_bss_info_change_notify(sdata, changed);
3227 void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
3228 struct sk_buff *skb)
3230 struct ieee80211_rx_status *rx_status;
3231 struct ieee80211_mgmt *mgmt;
3233 struct ieee802_11_elems elems;
3236 rx_status = (struct ieee80211_rx_status *) skb->cb;
3237 mgmt = (struct ieee80211_mgmt *) skb->data;
3238 fc = le16_to_cpu(mgmt->frame_control);
3242 switch (fc & IEEE80211_FCTL_STYPE) {
3243 case IEEE80211_STYPE_BEACON:
3244 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len, rx_status);
3246 case IEEE80211_STYPE_PROBE_RESP:
3247 ieee80211_rx_mgmt_probe_resp(sdata, skb);
3249 case IEEE80211_STYPE_AUTH:
3250 ieee80211_rx_mgmt_auth(sdata, mgmt, skb->len);
3252 case IEEE80211_STYPE_DEAUTH:
3253 ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
3255 case IEEE80211_STYPE_DISASSOC:
3256 ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
3258 case IEEE80211_STYPE_ASSOC_RESP:
3259 case IEEE80211_STYPE_REASSOC_RESP:
3260 ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len);
3262 case IEEE80211_STYPE_ACTION:
3263 if (mgmt->u.action.category == WLAN_CATEGORY_SPECTRUM_MGMT) {
3264 ies_len = skb->len -
3265 offsetof(struct ieee80211_mgmt,
3266 u.action.u.chan_switch.variable);
3271 ieee802_11_parse_elems(
3272 mgmt->u.action.u.chan_switch.variable,
3273 ies_len, true, &elems);
3275 if (elems.parse_error)
3278 ieee80211_sta_process_chanswitch(sdata,
3281 } else if (mgmt->u.action.category == WLAN_CATEGORY_PUBLIC) {
3282 ies_len = skb->len -
3283 offsetof(struct ieee80211_mgmt,
3284 u.action.u.ext_chan_switch.variable);
3289 ieee802_11_parse_elems(
3290 mgmt->u.action.u.ext_chan_switch.variable,
3291 ies_len, true, &elems);
3293 if (elems.parse_error)
3296 /* for the handling code pretend this was also an IE */
3297 elems.ext_chansw_ie =
3298 &mgmt->u.action.u.ext_chan_switch.data;
3300 ieee80211_sta_process_chanswitch(sdata,
3306 sdata_unlock(sdata);
3309 static void ieee80211_sta_timer(unsigned long data)
3311 struct ieee80211_sub_if_data *sdata =
3312 (struct ieee80211_sub_if_data *) data;
3314 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
3317 static void ieee80211_sta_connection_lost(struct ieee80211_sub_if_data *sdata,
3318 u8 *bssid, u8 reason, bool tx)
3320 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
3322 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH, reason,
3325 cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
3326 IEEE80211_DEAUTH_FRAME_LEN);
3329 static int ieee80211_probe_auth(struct ieee80211_sub_if_data *sdata)
3331 struct ieee80211_local *local = sdata->local;
3332 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3333 struct ieee80211_mgd_auth_data *auth_data = ifmgd->auth_data;
3336 sdata_assert_lock(sdata);
3338 if (WARN_ON_ONCE(!auth_data))
3343 if (auth_data->tries > IEEE80211_AUTH_MAX_TRIES) {
3344 sdata_info(sdata, "authentication with %pM timed out\n",
3345 auth_data->bss->bssid);
3348 * Most likely AP is not in the range so remove the
3349 * bss struct for that AP.
3351 cfg80211_unlink_bss(local->hw.wiphy, auth_data->bss);
3356 drv_mgd_prepare_tx(local, sdata);
3358 if (auth_data->bss->proberesp_ies) {
3362 sdata_info(sdata, "send auth to %pM (try %d/%d)\n",
3363 auth_data->bss->bssid, auth_data->tries,
3364 IEEE80211_AUTH_MAX_TRIES);
3366 auth_data->expected_transaction = 2;
3368 if (auth_data->algorithm == WLAN_AUTH_SAE) {
3369 trans = auth_data->sae_trans;
3370 status = auth_data->sae_status;
3371 auth_data->expected_transaction = trans;
3374 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
3375 tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
3376 IEEE80211_TX_INTFL_MLME_CONN_TX;
3378 ieee80211_send_auth(sdata, trans, auth_data->algorithm, status,
3379 auth_data->data, auth_data->data_len,
3380 auth_data->bss->bssid,
3381 auth_data->bss->bssid, NULL, 0, 0,
3386 sdata_info(sdata, "direct probe to %pM (try %d/%i)\n",
3387 auth_data->bss->bssid, auth_data->tries,
3388 IEEE80211_AUTH_MAX_TRIES);
3391 ssidie = ieee80211_bss_get_ie(auth_data->bss, WLAN_EID_SSID);
3397 * Direct probe is sent to broadcast address as some APs
3398 * will not answer to direct packet in unassociated state.
3400 ieee80211_send_probe_req(sdata, NULL, ssidie + 2, ssidie[1],
3401 NULL, 0, (u32) -1, true, 0,
3402 auth_data->bss->channel, false);
3406 if (tx_flags == 0) {
3407 auth_data->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
3408 auth_data->timeout_started = true;
3409 run_again(sdata, auth_data->timeout);
3411 auth_data->timeout =
3412 round_jiffies_up(jiffies + IEEE80211_AUTH_TIMEOUT_LONG);
3413 auth_data->timeout_started = true;
3414 run_again(sdata, auth_data->timeout);
3420 static int ieee80211_do_assoc(struct ieee80211_sub_if_data *sdata)
3422 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
3423 struct ieee80211_local *local = sdata->local;
3425 sdata_assert_lock(sdata);
3427 assoc_data->tries++;
3428 if (assoc_data->tries > IEEE80211_ASSOC_MAX_TRIES) {
3429 sdata_info(sdata, "association with %pM timed out\n",
3430 assoc_data->bss->bssid);
3433 * Most likely AP is not in the range so remove the
3434 * bss struct for that AP.
3436 cfg80211_unlink_bss(local->hw.wiphy, assoc_data->bss);
3441 sdata_info(sdata, "associate with %pM (try %d/%d)\n",
3442 assoc_data->bss->bssid, assoc_data->tries,
3443 IEEE80211_ASSOC_MAX_TRIES);
3444 ieee80211_send_assoc(sdata);
3446 if (!(local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)) {
3447 assoc_data->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
3448 assoc_data->timeout_started = true;
3449 run_again(sdata, assoc_data->timeout);
3451 assoc_data->timeout =
3452 round_jiffies_up(jiffies +
3453 IEEE80211_ASSOC_TIMEOUT_LONG);
3454 assoc_data->timeout_started = true;
3455 run_again(sdata, assoc_data->timeout);
3461 void ieee80211_mgd_conn_tx_status(struct ieee80211_sub_if_data *sdata,
3462 __le16 fc, bool acked)
3464 struct ieee80211_local *local = sdata->local;
3466 sdata->u.mgd.status_fc = fc;
3467 sdata->u.mgd.status_acked = acked;
3468 sdata->u.mgd.status_received = true;
3470 ieee80211_queue_work(&local->hw, &sdata->work);
3473 void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
3475 struct ieee80211_local *local = sdata->local;
3476 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3480 if (ifmgd->status_received) {
3481 __le16 fc = ifmgd->status_fc;
3482 bool status_acked = ifmgd->status_acked;
3484 ifmgd->status_received = false;
3485 if (ifmgd->auth_data &&
3486 (ieee80211_is_probe_req(fc) || ieee80211_is_auth(fc))) {
3488 ifmgd->auth_data->timeout =
3489 jiffies + IEEE80211_AUTH_TIMEOUT_SHORT;
3490 run_again(sdata, ifmgd->auth_data->timeout);
3492 ifmgd->auth_data->timeout = jiffies - 1;
3494 ifmgd->auth_data->timeout_started = true;
3495 } else if (ifmgd->assoc_data &&
3496 (ieee80211_is_assoc_req(fc) ||
3497 ieee80211_is_reassoc_req(fc))) {
3499 ifmgd->assoc_data->timeout =
3500 jiffies + IEEE80211_ASSOC_TIMEOUT_SHORT;
3501 run_again(sdata, ifmgd->assoc_data->timeout);
3503 ifmgd->assoc_data->timeout = jiffies - 1;
3505 ifmgd->assoc_data->timeout_started = true;
3509 if (ifmgd->auth_data && ifmgd->auth_data->timeout_started &&
3510 time_after(jiffies, ifmgd->auth_data->timeout)) {
3511 if (ifmgd->auth_data->done) {
3513 * ok ... we waited for assoc but userspace didn't,
3514 * so let's just kill the auth data
3516 ieee80211_destroy_auth_data(sdata, false);
3517 } else if (ieee80211_probe_auth(sdata)) {
3520 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
3522 ieee80211_destroy_auth_data(sdata, false);
3524 cfg80211_auth_timeout(sdata->dev, bssid);
3526 } else if (ifmgd->auth_data && ifmgd->auth_data->timeout_started)
3527 run_again(sdata, ifmgd->auth_data->timeout);
3529 if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started &&
3530 time_after(jiffies, ifmgd->assoc_data->timeout)) {
3531 if ((ifmgd->assoc_data->need_beacon && !ifmgd->have_beacon) ||
3532 ieee80211_do_assoc(sdata)) {
3533 struct cfg80211_bss *bss = ifmgd->assoc_data->bss;
3535 ieee80211_destroy_assoc_data(sdata, false);
3536 cfg80211_assoc_timeout(sdata->dev, bss);
3538 } else if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started)
3539 run_again(sdata, ifmgd->assoc_data->timeout);
3541 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
3542 IEEE80211_STA_CONNECTION_POLL) &&
3543 ifmgd->associated) {
3547 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
3549 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
3550 max_tries = max_nullfunc_tries;
3552 max_tries = max_probe_tries;
3554 /* ACK received for nullfunc probing frame */
3555 if (!ifmgd->probe_send_count)
3556 ieee80211_reset_ap_probe(sdata);
3557 else if (ifmgd->nullfunc_failed) {
3558 if (ifmgd->probe_send_count < max_tries) {
3560 "No ack for nullfunc frame to AP %pM, try %d/%i\n",
3561 bssid, ifmgd->probe_send_count,
3563 ieee80211_mgd_probe_ap_send(sdata);
3566 "No ack for nullfunc frame to AP %pM, disconnecting.\n",
3568 ieee80211_sta_connection_lost(sdata, bssid,
3569 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
3572 } else if (time_is_after_jiffies(ifmgd->probe_timeout))
3573 run_again(sdata, ifmgd->probe_timeout);
3574 else if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
3576 "Failed to send nullfunc to AP %pM after %dms, disconnecting\n",
3577 bssid, probe_wait_ms);
3578 ieee80211_sta_connection_lost(sdata, bssid,
3579 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
3580 } else if (ifmgd->probe_send_count < max_tries) {
3582 "No probe response from AP %pM after %dms, try %d/%i\n",
3583 bssid, probe_wait_ms,
3584 ifmgd->probe_send_count, max_tries);
3585 ieee80211_mgd_probe_ap_send(sdata);
3588 * We actually lost the connection ... or did we?
3591 wiphy_debug(local->hw.wiphy,
3592 "%s: No probe response from AP %pM"
3593 " after %dms, disconnecting.\n",
3595 bssid, probe_wait_ms);
3597 ieee80211_sta_connection_lost(sdata, bssid,
3598 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
3602 sdata_unlock(sdata);
3605 static void ieee80211_sta_bcn_mon_timer(unsigned long data)
3607 struct ieee80211_sub_if_data *sdata =
3608 (struct ieee80211_sub_if_data *) data;
3609 struct ieee80211_local *local = sdata->local;
3611 if (local->quiescing)
3614 sdata->u.mgd.connection_loss = false;
3615 ieee80211_queue_work(&sdata->local->hw,
3616 &sdata->u.mgd.beacon_connection_loss_work);
3619 static void ieee80211_sta_conn_mon_timer(unsigned long data)
3621 struct ieee80211_sub_if_data *sdata =
3622 (struct ieee80211_sub_if_data *) data;
3623 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3624 struct ieee80211_local *local = sdata->local;
3626 if (local->quiescing)
3629 ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
3632 static void ieee80211_sta_monitor_work(struct work_struct *work)
3634 struct ieee80211_sub_if_data *sdata =
3635 container_of(work, struct ieee80211_sub_if_data,
3636 u.mgd.monitor_work);
3638 ieee80211_mgd_probe_ap(sdata, false);
3641 static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
3645 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
3646 __ieee80211_stop_poll(sdata);
3648 /* let's probe the connection once */
3649 flags = sdata->local->hw.flags;
3650 if (!(flags & IEEE80211_HW_CONNECTION_MONITOR))
3651 ieee80211_queue_work(&sdata->local->hw,
3652 &sdata->u.mgd.monitor_work);
3653 /* and do all the other regular work too */
3654 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
3659 void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
3661 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3664 if (!ifmgd->associated) {
3665 sdata_unlock(sdata);
3669 if (sdata->flags & IEEE80211_SDATA_DISCONNECT_RESUME) {
3670 sdata->flags &= ~IEEE80211_SDATA_DISCONNECT_RESUME;
3671 mlme_dbg(sdata, "driver requested disconnect after resume\n");
3672 ieee80211_sta_connection_lost(sdata,
3673 ifmgd->associated->bssid,
3674 WLAN_REASON_UNSPECIFIED,
3676 sdata_unlock(sdata);
3679 sdata_unlock(sdata);
3683 /* interface setup */
3684 void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
3686 struct ieee80211_if_managed *ifmgd;
3688 ifmgd = &sdata->u.mgd;
3689 INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
3690 INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
3691 INIT_WORK(&ifmgd->beacon_connection_loss_work,
3692 ieee80211_beacon_connection_loss_work);
3693 INIT_WORK(&ifmgd->csa_connection_drop_work,
3694 ieee80211_csa_connection_drop_work);
3695 INIT_WORK(&ifmgd->request_smps_work, ieee80211_request_smps_work);
3696 setup_timer(&ifmgd->timer, ieee80211_sta_timer,
3697 (unsigned long) sdata);
3698 setup_timer(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer,
3699 (unsigned long) sdata);
3700 setup_timer(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer,
3701 (unsigned long) sdata);
3702 setup_timer(&ifmgd->chswitch_timer, ieee80211_chswitch_timer,
3703 (unsigned long) sdata);
3706 ifmgd->powersave = sdata->wdev.ps;
3707 ifmgd->uapsd_queues = sdata->local->hw.uapsd_queues;
3708 ifmgd->uapsd_max_sp_len = sdata->local->hw.uapsd_max_sp_len;
3709 ifmgd->p2p_noa_index = -1;
3711 if (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS)
3712 ifmgd->req_smps = IEEE80211_SMPS_AUTOMATIC;
3714 ifmgd->req_smps = IEEE80211_SMPS_OFF;
3717 /* scan finished notification */
3718 void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
3720 struct ieee80211_sub_if_data *sdata;
3722 /* Restart STA timers */
3724 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
3725 if (ieee80211_sdata_running(sdata))
3726 ieee80211_restart_sta_timer(sdata);
3731 int ieee80211_max_network_latency(struct notifier_block *nb,
3732 unsigned long data, void *dummy)
3734 s32 latency_usec = (s32) data;
3735 struct ieee80211_local *local =
3736 container_of(nb, struct ieee80211_local,
3737 network_latency_notifier);
3739 mutex_lock(&local->iflist_mtx);
3740 ieee80211_recalc_ps(local, latency_usec);
3741 mutex_unlock(&local->iflist_mtx);
3746 static u8 ieee80211_ht_vht_rx_chains(struct ieee80211_sub_if_data *sdata,
3747 struct cfg80211_bss *cbss)
3749 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3750 const u8 *ht_cap_ie, *vht_cap_ie;
3751 const struct ieee80211_ht_cap *ht_cap;
3752 const struct ieee80211_vht_cap *vht_cap;
3755 if (ifmgd->flags & IEEE80211_STA_DISABLE_HT)
3758 ht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
3759 if (ht_cap_ie && ht_cap_ie[1] >= sizeof(*ht_cap)) {
3760 ht_cap = (void *)(ht_cap_ie + 2);
3761 chains = ieee80211_mcs_to_chains(&ht_cap->mcs);
3763 * TODO: use "Tx Maximum Number Spatial Streams Supported" and
3764 * "Tx Unequal Modulation Supported" fields.
3768 if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
3771 vht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
3772 if (vht_cap_ie && vht_cap_ie[1] >= sizeof(*vht_cap)) {
3776 vht_cap = (void *)(vht_cap_ie + 2);
3777 tx_mcs_map = le16_to_cpu(vht_cap->supp_mcs.tx_mcs_map);
3778 for (nss = 8; nss > 0; nss--) {
3779 if (((tx_mcs_map >> (2 * (nss - 1))) & 3) !=
3780 IEEE80211_VHT_MCS_NOT_SUPPORTED)
3783 /* TODO: use "Tx Highest Supported Long GI Data Rate" field? */
3784 chains = max(chains, nss);
3790 static int ieee80211_prep_channel(struct ieee80211_sub_if_data *sdata,
3791 struct cfg80211_bss *cbss)
3793 struct ieee80211_local *local = sdata->local;
3794 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3795 const struct ieee80211_ht_operation *ht_oper = NULL;
3796 const struct ieee80211_vht_operation *vht_oper = NULL;
3797 struct ieee80211_supported_band *sband;
3798 struct cfg80211_chan_def chandef;
3801 sband = local->hw.wiphy->bands[cbss->channel->band];
3803 ifmgd->flags &= ~(IEEE80211_STA_DISABLE_40MHZ |
3804 IEEE80211_STA_DISABLE_80P80MHZ |
3805 IEEE80211_STA_DISABLE_160MHZ);
3809 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
3810 sband->ht_cap.ht_supported) {
3811 const u8 *ht_oper_ie, *ht_cap;
3813 ht_oper_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_OPERATION);
3814 if (ht_oper_ie && ht_oper_ie[1] >= sizeof(*ht_oper))
3815 ht_oper = (void *)(ht_oper_ie + 2);
3817 ht_cap = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
3818 if (!ht_cap || ht_cap[1] < sizeof(struct ieee80211_ht_cap)) {
3819 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
3824 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
3825 sband->vht_cap.vht_supported) {
3826 const u8 *vht_oper_ie, *vht_cap;
3828 vht_oper_ie = ieee80211_bss_get_ie(cbss,
3829 WLAN_EID_VHT_OPERATION);
3830 if (vht_oper_ie && vht_oper_ie[1] >= sizeof(*vht_oper))
3831 vht_oper = (void *)(vht_oper_ie + 2);
3832 if (vht_oper && !ht_oper) {
3835 "AP advertised VHT without HT, disabling both\n");
3836 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
3837 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
3840 vht_cap = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
3841 if (!vht_cap || vht_cap[1] < sizeof(struct ieee80211_vht_cap)) {
3842 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
3847 ifmgd->flags |= ieee80211_determine_chantype(sdata, sband,
3852 sdata->needed_rx_chains = min(ieee80211_ht_vht_rx_chains(sdata, cbss),
3857 /* will change later if needed */
3858 sdata->smps_mode = IEEE80211_SMPS_OFF;
3861 * If this fails (possibly due to channel context sharing
3862 * on incompatible channels, e.g. 80+80 and 160 sharing the
3863 * same control channel) try to use a smaller bandwidth.
3865 ret = ieee80211_vif_use_channel(sdata, &chandef,
3866 IEEE80211_CHANCTX_SHARED);
3868 /* don't downgrade for 5 and 10 MHz channels, though. */
3869 if (chandef.width == NL80211_CHAN_WIDTH_5 ||
3870 chandef.width == NL80211_CHAN_WIDTH_10)
3873 while (ret && chandef.width != NL80211_CHAN_WIDTH_20_NOHT) {
3874 ifmgd->flags |= chandef_downgrade(&chandef);
3875 ret = ieee80211_vif_use_channel(sdata, &chandef,
3876 IEEE80211_CHANCTX_SHARED);
3881 static int ieee80211_prep_connection(struct ieee80211_sub_if_data *sdata,
3882 struct cfg80211_bss *cbss, bool assoc)
3884 struct ieee80211_local *local = sdata->local;
3885 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3886 struct ieee80211_bss *bss = (void *)cbss->priv;
3887 struct sta_info *new_sta = NULL;
3888 bool have_sta = false;
3891 if (WARN_ON(!ifmgd->auth_data && !ifmgd->assoc_data))
3896 have_sta = sta_info_get(sdata, cbss->bssid);
3901 new_sta = sta_info_alloc(sdata, cbss->bssid, GFP_KERNEL);
3907 u32 rates = 0, basic_rates = 0;
3908 bool have_higher_than_11mbit;
3909 int min_rate = INT_MAX, min_rate_index = -1;
3910 struct ieee80211_supported_band *sband;
3911 const struct cfg80211_bss_ies *ies;
3913 sband = local->hw.wiphy->bands[cbss->channel->band];
3915 err = ieee80211_prep_channel(sdata, cbss);
3917 sta_info_free(local, new_sta);
3921 ieee80211_get_rates(sband, bss->supp_rates,
3922 bss->supp_rates_len,
3923 &rates, &basic_rates,
3924 &have_higher_than_11mbit,
3925 &min_rate, &min_rate_index);
3928 * This used to be a workaround for basic rates missing
3929 * in the association response frame. Now that we no
3930 * longer use the basic rates from there, it probably
3931 * doesn't happen any more, but keep the workaround so
3932 * in case some *other* APs are buggy in different ways
3933 * we can connect -- with a warning.
3935 if (!basic_rates && min_rate_index >= 0) {
3937 "No basic rates, using min rate instead\n");
3938 basic_rates = BIT(min_rate_index);
3941 new_sta->sta.supp_rates[cbss->channel->band] = rates;
3942 sdata->vif.bss_conf.basic_rates = basic_rates;
3944 /* cf. IEEE 802.11 9.2.12 */
3945 if (cbss->channel->band == IEEE80211_BAND_2GHZ &&
3946 have_higher_than_11mbit)
3947 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
3949 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
3951 memcpy(ifmgd->bssid, cbss->bssid, ETH_ALEN);
3953 /* set timing information */
3954 sdata->vif.bss_conf.beacon_int = cbss->beacon_interval;
3956 ies = rcu_dereference(cbss->beacon_ies);
3960 sdata->vif.bss_conf.sync_tsf = ies->tsf;
3961 sdata->vif.bss_conf.sync_device_ts =
3962 bss->device_ts_beacon;
3963 tim_ie = cfg80211_find_ie(WLAN_EID_TIM,
3964 ies->data, ies->len);
3965 if (tim_ie && tim_ie[1] >= 2)
3966 sdata->vif.bss_conf.sync_dtim_count = tim_ie[2];
3968 sdata->vif.bss_conf.sync_dtim_count = 0;
3969 } else if (!(local->hw.flags &
3970 IEEE80211_HW_TIMING_BEACON_ONLY)) {
3971 ies = rcu_dereference(cbss->proberesp_ies);
3972 /* must be non-NULL since beacon IEs were NULL */
3973 sdata->vif.bss_conf.sync_tsf = ies->tsf;
3974 sdata->vif.bss_conf.sync_device_ts =
3975 bss->device_ts_presp;
3976 sdata->vif.bss_conf.sync_dtim_count = 0;
3978 sdata->vif.bss_conf.sync_tsf = 0;
3979 sdata->vif.bss_conf.sync_device_ts = 0;
3980 sdata->vif.bss_conf.sync_dtim_count = 0;
3984 /* tell driver about BSSID, basic rates and timing */
3985 ieee80211_bss_info_change_notify(sdata,
3986 BSS_CHANGED_BSSID | BSS_CHANGED_BASIC_RATES |
3987 BSS_CHANGED_BEACON_INT);
3990 sta_info_pre_move_state(new_sta, IEEE80211_STA_AUTH);
3992 err = sta_info_insert(new_sta);
3996 "failed to insert STA entry for the AP (error %d)\n",
4001 WARN_ON_ONCE(!ether_addr_equal(ifmgd->bssid, cbss->bssid));
4007 int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
4008 struct cfg80211_auth_request *req)
4010 struct ieee80211_local *local = sdata->local;
4011 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4012 struct ieee80211_mgd_auth_data *auth_data;
4016 /* prepare auth data structure */
4018 switch (req->auth_type) {
4019 case NL80211_AUTHTYPE_OPEN_SYSTEM:
4020 auth_alg = WLAN_AUTH_OPEN;
4022 case NL80211_AUTHTYPE_SHARED_KEY:
4023 if (IS_ERR(local->wep_tx_tfm))
4025 auth_alg = WLAN_AUTH_SHARED_KEY;
4027 case NL80211_AUTHTYPE_FT:
4028 auth_alg = WLAN_AUTH_FT;
4030 case NL80211_AUTHTYPE_NETWORK_EAP:
4031 auth_alg = WLAN_AUTH_LEAP;
4033 case NL80211_AUTHTYPE_SAE:
4034 auth_alg = WLAN_AUTH_SAE;
4040 auth_data = kzalloc(sizeof(*auth_data) + req->sae_data_len +
4041 req->ie_len, GFP_KERNEL);
4045 auth_data->bss = req->bss;
4047 if (req->sae_data_len >= 4) {
4048 __le16 *pos = (__le16 *) req->sae_data;
4049 auth_data->sae_trans = le16_to_cpu(pos[0]);
4050 auth_data->sae_status = le16_to_cpu(pos[1]);
4051 memcpy(auth_data->data, req->sae_data + 4,
4052 req->sae_data_len - 4);
4053 auth_data->data_len += req->sae_data_len - 4;
4056 if (req->ie && req->ie_len) {
4057 memcpy(&auth_data->data[auth_data->data_len],
4058 req->ie, req->ie_len);
4059 auth_data->data_len += req->ie_len;
4062 if (req->key && req->key_len) {
4063 auth_data->key_len = req->key_len;
4064 auth_data->key_idx = req->key_idx;
4065 memcpy(auth_data->key, req->key, req->key_len);
4068 auth_data->algorithm = auth_alg;
4070 /* try to authenticate/probe */
4072 if ((ifmgd->auth_data && !ifmgd->auth_data->done) ||
4073 ifmgd->assoc_data) {
4078 if (ifmgd->auth_data)
4079 ieee80211_destroy_auth_data(sdata, false);
4081 /* prep auth_data so we don't go into idle on disassoc */
4082 ifmgd->auth_data = auth_data;
4084 if (ifmgd->associated) {
4085 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
4087 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
4088 WLAN_REASON_UNSPECIFIED,
4091 cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
4095 sdata_info(sdata, "authenticate with %pM\n", req->bss->bssid);
4097 err = ieee80211_prep_connection(sdata, req->bss, false);
4101 err = ieee80211_probe_auth(sdata);
4103 sta_info_destroy_addr(sdata, req->bss->bssid);
4107 /* hold our own reference */
4108 cfg80211_ref_bss(local->hw.wiphy, auth_data->bss);
4112 memset(ifmgd->bssid, 0, ETH_ALEN);
4113 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
4114 ifmgd->auth_data = NULL;
4120 int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
4121 struct cfg80211_assoc_request *req)
4123 struct ieee80211_local *local = sdata->local;
4124 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4125 struct ieee80211_bss *bss = (void *)req->bss->priv;
4126 struct ieee80211_mgd_assoc_data *assoc_data;
4127 const struct cfg80211_bss_ies *beacon_ies;
4128 struct ieee80211_supported_band *sband;
4129 const u8 *ssidie, *ht_ie, *vht_ie;
4132 assoc_data = kzalloc(sizeof(*assoc_data) + req->ie_len, GFP_KERNEL);
4137 ssidie = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
4143 memcpy(assoc_data->ssid, ssidie + 2, ssidie[1]);
4144 assoc_data->ssid_len = ssidie[1];
4147 if (ifmgd->associated) {
4148 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
4150 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
4151 WLAN_REASON_UNSPECIFIED,
4154 cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
4158 if (ifmgd->auth_data && !ifmgd->auth_data->done) {
4163 if (ifmgd->assoc_data) {
4168 if (ifmgd->auth_data) {
4171 /* keep sta info, bssid if matching */
4172 match = ether_addr_equal(ifmgd->bssid, req->bss->bssid);
4173 ieee80211_destroy_auth_data(sdata, match);
4176 /* prepare assoc data */
4178 ifmgd->beacon_crc_valid = false;
4181 * IEEE802.11n does not allow TKIP/WEP as pairwise ciphers in HT mode.
4182 * We still associate in non-HT mode (11a/b/g) if any one of these
4183 * ciphers is configured as pairwise.
4184 * We can set this to true for non-11n hardware, that'll be checked
4185 * separately along with the peer capabilities.
4187 for (i = 0; i < req->crypto.n_ciphers_pairwise; i++) {
4188 if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
4189 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
4190 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104) {
4191 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
4192 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
4193 netdev_info(sdata->dev,
4194 "disabling HT/VHT due to WEP/TKIP use\n");
4198 if (req->flags & ASSOC_REQ_DISABLE_HT) {
4199 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
4200 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
4203 if (req->flags & ASSOC_REQ_DISABLE_VHT)
4204 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
4206 /* Also disable HT if we don't support it or the AP doesn't use WMM */
4207 sband = local->hw.wiphy->bands[req->bss->channel->band];
4208 if (!sband->ht_cap.ht_supported ||
4209 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used) {
4210 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
4212 netdev_info(sdata->dev,
4213 "disabling HT as WMM/QoS is not supported by the AP\n");
4216 /* disable VHT if we don't support it or the AP doesn't use WMM */
4217 if (!sband->vht_cap.vht_supported ||
4218 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used) {
4219 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
4221 netdev_info(sdata->dev,
4222 "disabling VHT as WMM/QoS is not supported by the AP\n");
4225 memcpy(&ifmgd->ht_capa, &req->ht_capa, sizeof(ifmgd->ht_capa));
4226 memcpy(&ifmgd->ht_capa_mask, &req->ht_capa_mask,
4227 sizeof(ifmgd->ht_capa_mask));
4229 memcpy(&ifmgd->vht_capa, &req->vht_capa, sizeof(ifmgd->vht_capa));
4230 memcpy(&ifmgd->vht_capa_mask, &req->vht_capa_mask,
4231 sizeof(ifmgd->vht_capa_mask));
4233 if (req->ie && req->ie_len) {
4234 memcpy(assoc_data->ie, req->ie, req->ie_len);
4235 assoc_data->ie_len = req->ie_len;
4238 assoc_data->bss = req->bss;
4240 if (ifmgd->req_smps == IEEE80211_SMPS_AUTOMATIC) {
4241 if (ifmgd->powersave)
4242 sdata->smps_mode = IEEE80211_SMPS_DYNAMIC;
4244 sdata->smps_mode = IEEE80211_SMPS_OFF;
4246 sdata->smps_mode = ifmgd->req_smps;
4248 assoc_data->capability = req->bss->capability;
4249 assoc_data->wmm = bss->wmm_used &&
4250 (local->hw.queues >= IEEE80211_NUM_ACS);
4251 assoc_data->supp_rates = bss->supp_rates;
4252 assoc_data->supp_rates_len = bss->supp_rates_len;
4255 ht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_HT_OPERATION);
4256 if (ht_ie && ht_ie[1] >= sizeof(struct ieee80211_ht_operation))
4257 assoc_data->ap_ht_param =
4258 ((struct ieee80211_ht_operation *)(ht_ie + 2))->ht_param;
4260 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
4261 vht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_VHT_CAPABILITY);
4262 if (vht_ie && vht_ie[1] >= sizeof(struct ieee80211_vht_cap))
4263 memcpy(&assoc_data->ap_vht_cap, vht_ie + 2,
4264 sizeof(struct ieee80211_vht_cap));
4266 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
4269 if (bss->wmm_used && bss->uapsd_supported &&
4270 (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_UAPSD) &&
4271 sdata->wmm_acm != 0xff) {
4272 assoc_data->uapsd = true;
4273 ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED;
4275 assoc_data->uapsd = false;
4276 ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED;
4279 if (req->prev_bssid)
4280 memcpy(assoc_data->prev_bssid, req->prev_bssid, ETH_ALEN);
4283 ifmgd->mfp = IEEE80211_MFP_REQUIRED;
4284 ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
4286 ifmgd->mfp = IEEE80211_MFP_DISABLED;
4287 ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
4290 if (req->crypto.control_port)
4291 ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
4293 ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
4295 sdata->control_port_protocol = req->crypto.control_port_ethertype;
4296 sdata->control_port_no_encrypt = req->crypto.control_port_no_encrypt;
4298 /* kick off associate process */
4300 ifmgd->assoc_data = assoc_data;
4301 ifmgd->dtim_period = 0;
4302 ifmgd->have_beacon = false;
4304 err = ieee80211_prep_connection(sdata, req->bss, true);
4309 beacon_ies = rcu_dereference(req->bss->beacon_ies);
4311 if (sdata->local->hw.flags & IEEE80211_HW_NEED_DTIM_BEFORE_ASSOC &&
4314 * Wait up to one beacon interval ...
4315 * should this be more if we miss one?
4317 sdata_info(sdata, "waiting for beacon from %pM\n",
4319 assoc_data->timeout = TU_TO_EXP_TIME(req->bss->beacon_interval);
4320 assoc_data->timeout_started = true;
4321 assoc_data->need_beacon = true;
4322 } else if (beacon_ies) {
4323 const u8 *tim_ie = cfg80211_find_ie(WLAN_EID_TIM,
4328 if (tim_ie && tim_ie[1] >= sizeof(struct ieee80211_tim_ie)) {
4329 const struct ieee80211_tim_ie *tim;
4330 tim = (void *)(tim_ie + 2);
4331 ifmgd->dtim_period = tim->dtim_period;
4332 dtim_count = tim->dtim_count;
4334 ifmgd->have_beacon = true;
4335 assoc_data->timeout = jiffies;
4336 assoc_data->timeout_started = true;
4338 if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
4339 sdata->vif.bss_conf.sync_tsf = beacon_ies->tsf;
4340 sdata->vif.bss_conf.sync_device_ts =
4341 bss->device_ts_beacon;
4342 sdata->vif.bss_conf.sync_dtim_count = dtim_count;
4345 assoc_data->timeout = jiffies;
4346 assoc_data->timeout_started = true;
4350 run_again(sdata, assoc_data->timeout);
4352 if (bss->corrupt_data) {
4353 char *corrupt_type = "data";
4354 if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_BEACON) {
4355 if (bss->corrupt_data &
4356 IEEE80211_BSS_CORRUPT_PROBE_RESP)
4357 corrupt_type = "beacon and probe response";
4359 corrupt_type = "beacon";
4360 } else if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_PROBE_RESP)
4361 corrupt_type = "probe response";
4362 sdata_info(sdata, "associating with AP with corrupt %s\n",
4368 memset(ifmgd->bssid, 0, ETH_ALEN);
4369 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
4370 ifmgd->assoc_data = NULL;
4376 int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
4377 struct cfg80211_deauth_request *req)
4379 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4380 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
4381 bool tx = !req->local_state_change;
4382 bool report_frame = false;
4385 "deauthenticating from %pM by local choice (reason=%d)\n",
4386 req->bssid, req->reason_code);
4388 if (ifmgd->auth_data) {
4389 drv_mgd_prepare_tx(sdata->local, sdata);
4390 ieee80211_send_deauth_disassoc(sdata, req->bssid,
4391 IEEE80211_STYPE_DEAUTH,
4392 req->reason_code, tx,
4394 ieee80211_destroy_auth_data(sdata, false);
4396 report_frame = true;
4400 if (ifmgd->associated &&
4401 ether_addr_equal(ifmgd->associated->bssid, req->bssid)) {
4402 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
4403 req->reason_code, tx, frame_buf);
4404 report_frame = true;
4409 cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
4410 IEEE80211_DEAUTH_FRAME_LEN);
4415 int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
4416 struct cfg80211_disassoc_request *req)
4418 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4420 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
4423 * cfg80211 should catch this ... but it's racy since
4424 * we can receive a disassoc frame, process it, hand it
4425 * to cfg80211 while that's in a locked section already
4426 * trying to tell us that the user wants to disconnect.
4428 if (ifmgd->associated != req->bss)
4432 "disassociating from %pM by local choice (reason=%d)\n",
4433 req->bss->bssid, req->reason_code);
4435 memcpy(bssid, req->bss->bssid, ETH_ALEN);
4436 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DISASSOC,
4437 req->reason_code, !req->local_state_change,
4440 cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
4441 IEEE80211_DEAUTH_FRAME_LEN);
4446 void ieee80211_mgd_stop(struct ieee80211_sub_if_data *sdata)
4448 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4451 * Make sure some work items will not run after this,
4452 * they will not do anything but might not have been
4453 * cancelled when disconnecting.
4455 cancel_work_sync(&ifmgd->monitor_work);
4456 cancel_work_sync(&ifmgd->beacon_connection_loss_work);
4457 cancel_work_sync(&ifmgd->request_smps_work);
4458 cancel_work_sync(&ifmgd->csa_connection_drop_work);
4459 cancel_work_sync(&ifmgd->chswitch_work);
4462 if (ifmgd->assoc_data) {
4463 struct cfg80211_bss *bss = ifmgd->assoc_data->bss;
4464 ieee80211_destroy_assoc_data(sdata, false);
4465 cfg80211_assoc_timeout(sdata->dev, bss);
4467 if (ifmgd->auth_data)
4468 ieee80211_destroy_auth_data(sdata, false);
4469 del_timer_sync(&ifmgd->timer);
4470 sdata_unlock(sdata);
4473 void ieee80211_cqm_rssi_notify(struct ieee80211_vif *vif,
4474 enum nl80211_cqm_rssi_threshold_event rssi_event,
4477 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4479 trace_api_cqm_rssi_notify(sdata, rssi_event);
4481 cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, gfp);
4483 EXPORT_SYMBOL(ieee80211_cqm_rssi_notify);