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 * All cfg80211 functions have to be called outside a locked
97 * section so that they can acquire a lock themselves... This
98 * is much simpler than queuing up things in cfg80211, but we
99 * do need some indirection for that here.
101 enum rx_mgmt_action {
102 /* no action required */
105 /* caller must call cfg80211_send_deauth() */
106 RX_MGMT_CFG80211_DEAUTH,
108 /* caller must call cfg80211_send_disassoc() */
109 RX_MGMT_CFG80211_DISASSOC,
111 /* caller must call cfg80211_send_rx_auth() */
112 RX_MGMT_CFG80211_RX_AUTH,
114 /* caller must call cfg80211_send_rx_assoc() */
115 RX_MGMT_CFG80211_RX_ASSOC,
117 /* caller must call cfg80211_send_assoc_timeout() */
118 RX_MGMT_CFG80211_ASSOC_TIMEOUT,
120 /* used when a processed beacon causes a deauth */
121 RX_MGMT_CFG80211_TX_DEAUTH,
125 static inline void ASSERT_MGD_MTX(struct ieee80211_if_managed *ifmgd)
127 lockdep_assert_held(&ifmgd->mtx);
131 * We can have multiple work items (and connection probing)
132 * scheduling this timer, but we need to take care to only
133 * reschedule it when it should fire _earlier_ than it was
134 * asked for before, or if it's not pending right now. This
135 * function ensures that. Note that it then is required to
136 * run this function for all timeouts after the first one
137 * has happened -- the work that runs from this timer will
140 static void run_again(struct ieee80211_if_managed *ifmgd, unsigned long timeout)
142 ASSERT_MGD_MTX(ifmgd);
144 if (!timer_pending(&ifmgd->timer) ||
145 time_before(timeout, ifmgd->timer.expires))
146 mod_timer(&ifmgd->timer, timeout);
149 void ieee80211_sta_reset_beacon_monitor(struct ieee80211_sub_if_data *sdata)
151 if (sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)
154 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
157 mod_timer(&sdata->u.mgd.bcn_mon_timer,
158 round_jiffies_up(jiffies + sdata->u.mgd.beacon_timeout));
161 void ieee80211_sta_reset_conn_monitor(struct ieee80211_sub_if_data *sdata)
163 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
165 if (unlikely(!sdata->u.mgd.associated))
168 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
171 mod_timer(&sdata->u.mgd.conn_mon_timer,
172 round_jiffies_up(jiffies + IEEE80211_CONNECTION_IDLE_TIME));
174 ifmgd->probe_send_count = 0;
177 static int ecw2cw(int ecw)
179 return (1 << ecw) - 1;
182 static u32 chandef_downgrade(struct cfg80211_chan_def *c)
188 case NL80211_CHAN_WIDTH_20:
189 c->width = NL80211_CHAN_WIDTH_20_NOHT;
190 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
192 case NL80211_CHAN_WIDTH_40:
193 c->width = NL80211_CHAN_WIDTH_20;
194 c->center_freq1 = c->chan->center_freq;
195 ret = IEEE80211_STA_DISABLE_40MHZ |
196 IEEE80211_STA_DISABLE_VHT;
198 case NL80211_CHAN_WIDTH_80:
199 tmp = (30 + c->chan->center_freq - c->center_freq1)/20;
203 c->center_freq1 = c->center_freq1 - 20 + 40 * tmp;
204 c->width = NL80211_CHAN_WIDTH_40;
205 ret = IEEE80211_STA_DISABLE_VHT;
207 case NL80211_CHAN_WIDTH_80P80:
209 c->width = NL80211_CHAN_WIDTH_80;
210 ret = IEEE80211_STA_DISABLE_80P80MHZ |
211 IEEE80211_STA_DISABLE_160MHZ;
213 case NL80211_CHAN_WIDTH_160:
215 tmp = (70 + c->chan->center_freq - c->center_freq1)/20;
218 c->center_freq1 = c->center_freq1 - 40 + 80 * tmp;
219 c->width = NL80211_CHAN_WIDTH_80;
220 ret = IEEE80211_STA_DISABLE_80P80MHZ |
221 IEEE80211_STA_DISABLE_160MHZ;
224 case NL80211_CHAN_WIDTH_20_NOHT:
226 c->width = NL80211_CHAN_WIDTH_20_NOHT;
227 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
231 WARN_ON_ONCE(!cfg80211_chandef_valid(c));
237 ieee80211_determine_chantype(struct ieee80211_sub_if_data *sdata,
238 struct ieee80211_supported_band *sband,
239 struct ieee80211_channel *channel,
240 const struct ieee80211_ht_operation *ht_oper,
241 const struct ieee80211_vht_operation *vht_oper,
242 struct cfg80211_chan_def *chandef, bool tracking)
244 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
245 struct cfg80211_chan_def vht_chandef;
248 chandef->chan = channel;
249 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
250 chandef->center_freq1 = channel->center_freq;
251 chandef->center_freq2 = 0;
253 if (!ht_oper || !sband->ht_cap.ht_supported) {
254 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
258 chandef->width = NL80211_CHAN_WIDTH_20;
260 ht_cfreq = ieee80211_channel_to_frequency(ht_oper->primary_chan,
262 /* check that channel matches the right operating channel */
263 if (!tracking && channel->center_freq != ht_cfreq) {
265 * It's possible that some APs are confused here;
266 * Netgear WNDR3700 sometimes reports 4 higher than
267 * the actual channel in association responses, but
268 * since we look at probe response/beacon data here
272 "Wrong control channel: center-freq: %d ht-cfreq: %d ht->primary_chan: %d band: %d - Disabling HT\n",
273 channel->center_freq, ht_cfreq,
274 ht_oper->primary_chan, channel->band);
275 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
279 /* check 40 MHz support, if we have it */
280 if (sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) {
281 switch (ht_oper->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
282 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
283 chandef->width = NL80211_CHAN_WIDTH_40;
284 chandef->center_freq1 += 10;
286 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
287 chandef->width = NL80211_CHAN_WIDTH_40;
288 chandef->center_freq1 -= 10;
292 /* 40 MHz (and 80 MHz) must be supported for VHT */
293 ret = IEEE80211_STA_DISABLE_VHT;
294 /* also mark 40 MHz disabled */
295 ret |= IEEE80211_STA_DISABLE_40MHZ;
299 if (!vht_oper || !sband->vht_cap.vht_supported) {
300 ret = IEEE80211_STA_DISABLE_VHT;
304 vht_chandef.chan = channel;
305 vht_chandef.center_freq1 =
306 ieee80211_channel_to_frequency(vht_oper->center_freq_seg1_idx,
308 vht_chandef.center_freq2 = 0;
310 switch (vht_oper->chan_width) {
311 case IEEE80211_VHT_CHANWIDTH_USE_HT:
312 vht_chandef.width = chandef->width;
314 case IEEE80211_VHT_CHANWIDTH_80MHZ:
315 vht_chandef.width = NL80211_CHAN_WIDTH_80;
317 case IEEE80211_VHT_CHANWIDTH_160MHZ:
318 vht_chandef.width = NL80211_CHAN_WIDTH_160;
320 case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
321 vht_chandef.width = NL80211_CHAN_WIDTH_80P80;
322 vht_chandef.center_freq2 =
323 ieee80211_channel_to_frequency(
324 vht_oper->center_freq_seg2_idx,
328 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
330 "AP VHT operation IE has invalid channel width (%d), disable VHT\n",
331 vht_oper->chan_width);
332 ret = IEEE80211_STA_DISABLE_VHT;
336 if (!cfg80211_chandef_valid(&vht_chandef)) {
337 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
339 "AP VHT information is invalid, disable VHT\n");
340 ret = IEEE80211_STA_DISABLE_VHT;
344 if (cfg80211_chandef_identical(chandef, &vht_chandef)) {
349 if (!cfg80211_chandef_compatible(chandef, &vht_chandef)) {
350 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
352 "AP VHT information doesn't match HT, disable VHT\n");
353 ret = IEEE80211_STA_DISABLE_VHT;
357 *chandef = vht_chandef;
362 /* don't print the message below for VHT mismatch if VHT is disabled */
363 if (ret & IEEE80211_STA_DISABLE_VHT)
364 vht_chandef = *chandef;
367 * Ignore the DISABLED flag when we're already connected and only
368 * tracking the APs beacon for bandwidth changes - otherwise we
369 * might get disconnected here if we connect to an AP, update our
370 * regulatory information based on the AP's country IE and the
371 * information we have is wrong/outdated and disables the channel
372 * that we're actually using for the connection to the AP.
374 while (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
376 IEEE80211_CHAN_DISABLED)) {
377 if (WARN_ON(chandef->width == NL80211_CHAN_WIDTH_20_NOHT)) {
378 ret = IEEE80211_STA_DISABLE_HT |
379 IEEE80211_STA_DISABLE_VHT;
383 ret |= chandef_downgrade(chandef);
386 if (chandef->width != vht_chandef.width && !tracking)
388 "capabilities/regulatory prevented using AP HT/VHT configuration, downgraded\n");
390 WARN_ON_ONCE(!cfg80211_chandef_valid(chandef));
394 static int ieee80211_config_bw(struct ieee80211_sub_if_data *sdata,
395 struct sta_info *sta,
396 const struct ieee80211_ht_operation *ht_oper,
397 const struct ieee80211_vht_operation *vht_oper,
398 const u8 *bssid, u32 *changed)
400 struct ieee80211_local *local = sdata->local;
401 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
402 struct ieee80211_supported_band *sband;
403 struct ieee80211_channel *chan;
404 struct cfg80211_chan_def chandef;
407 enum ieee80211_sta_rx_bandwidth new_sta_bw;
410 /* if HT was/is disabled, don't track any bandwidth changes */
411 if (ifmgd->flags & IEEE80211_STA_DISABLE_HT || !ht_oper)
414 /* don't check VHT if we associated as non-VHT station */
415 if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
418 if (WARN_ON_ONCE(!sta))
421 chan = sdata->vif.bss_conf.chandef.chan;
422 sband = local->hw.wiphy->bands[chan->band];
424 /* calculate new channel (type) based on HT/VHT operation IEs */
425 flags = ieee80211_determine_chantype(sdata, sband, chan, ht_oper,
426 vht_oper, &chandef, true);
429 * Downgrade the new channel if we associated with restricted
430 * capabilities. For example, if we associated as a 20 MHz STA
431 * to a 40 MHz AP (due to regulatory, capabilities or config
432 * reasons) then switching to a 40 MHz channel now won't do us
433 * any good -- we couldn't use it with the AP.
435 if (ifmgd->flags & IEEE80211_STA_DISABLE_80P80MHZ &&
436 chandef.width == NL80211_CHAN_WIDTH_80P80)
437 flags |= chandef_downgrade(&chandef);
438 if (ifmgd->flags & IEEE80211_STA_DISABLE_160MHZ &&
439 chandef.width == NL80211_CHAN_WIDTH_160)
440 flags |= chandef_downgrade(&chandef);
441 if (ifmgd->flags & IEEE80211_STA_DISABLE_40MHZ &&
442 chandef.width > NL80211_CHAN_WIDTH_20)
443 flags |= chandef_downgrade(&chandef);
445 if (cfg80211_chandef_identical(&chandef, &sdata->vif.bss_conf.chandef))
449 "AP %pM changed bandwidth, new config is %d MHz, width %d (%d/%d MHz)\n",
450 ifmgd->bssid, chandef.chan->center_freq, chandef.width,
451 chandef.center_freq1, chandef.center_freq2);
453 if (flags != (ifmgd->flags & (IEEE80211_STA_DISABLE_HT |
454 IEEE80211_STA_DISABLE_VHT |
455 IEEE80211_STA_DISABLE_40MHZ |
456 IEEE80211_STA_DISABLE_80P80MHZ |
457 IEEE80211_STA_DISABLE_160MHZ)) ||
458 !cfg80211_chandef_valid(&chandef)) {
460 "AP %pM changed bandwidth in a way we can't support - disconnect\n",
465 switch (chandef.width) {
466 case NL80211_CHAN_WIDTH_20_NOHT:
467 case NL80211_CHAN_WIDTH_20:
468 new_sta_bw = IEEE80211_STA_RX_BW_20;
470 case NL80211_CHAN_WIDTH_40:
471 new_sta_bw = IEEE80211_STA_RX_BW_40;
473 case NL80211_CHAN_WIDTH_80:
474 new_sta_bw = IEEE80211_STA_RX_BW_80;
476 case NL80211_CHAN_WIDTH_80P80:
477 case NL80211_CHAN_WIDTH_160:
478 new_sta_bw = IEEE80211_STA_RX_BW_160;
484 if (new_sta_bw > sta->cur_max_bandwidth)
485 new_sta_bw = sta->cur_max_bandwidth;
487 if (new_sta_bw < sta->sta.bandwidth) {
488 sta->sta.bandwidth = new_sta_bw;
489 rate_control_rate_update(local, sband, sta,
490 IEEE80211_RC_BW_CHANGED);
493 ret = ieee80211_vif_change_bandwidth(sdata, &chandef, changed);
496 "AP %pM changed bandwidth to incompatible one - disconnect\n",
501 if (new_sta_bw > sta->sta.bandwidth) {
502 sta->sta.bandwidth = new_sta_bw;
503 rate_control_rate_update(local, sband, sta,
504 IEEE80211_RC_BW_CHANGED);
507 ht_opmode = le16_to_cpu(ht_oper->operation_mode);
509 /* if bss configuration changed store the new one */
510 if (sdata->vif.bss_conf.ht_operation_mode != ht_opmode) {
511 *changed |= BSS_CHANGED_HT;
512 sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
518 /* frame sending functions */
520 static int ieee80211_compatible_rates(const u8 *supp_rates, int supp_rates_len,
521 struct ieee80211_supported_band *sband,
527 for (i = 0; i < supp_rates_len; i++) {
528 int rate = (supp_rates[i] & 0x7F) * 5;
530 for (j = 0; j < sband->n_bitrates; j++)
531 if (sband->bitrates[j].bitrate == rate) {
541 static void ieee80211_add_ht_ie(struct ieee80211_sub_if_data *sdata,
542 struct sk_buff *skb, u8 ap_ht_param,
543 struct ieee80211_supported_band *sband,
544 struct ieee80211_channel *channel,
545 enum ieee80211_smps_mode smps)
548 u32 flags = channel->flags;
550 struct ieee80211_sta_ht_cap ht_cap;
552 BUILD_BUG_ON(sizeof(ht_cap) != sizeof(sband->ht_cap));
554 memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
555 ieee80211_apply_htcap_overrides(sdata, &ht_cap);
557 /* determine capability flags */
560 switch (ap_ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
561 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
562 if (flags & IEEE80211_CHAN_NO_HT40PLUS) {
563 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
564 cap &= ~IEEE80211_HT_CAP_SGI_40;
567 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
568 if (flags & IEEE80211_CHAN_NO_HT40MINUS) {
569 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
570 cap &= ~IEEE80211_HT_CAP_SGI_40;
576 * If 40 MHz was disabled associate as though we weren't
577 * capable of 40 MHz -- some broken APs will never fall
578 * back to trying to transmit in 20 MHz.
580 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_40MHZ) {
581 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
582 cap &= ~IEEE80211_HT_CAP_SGI_40;
585 /* set SM PS mode properly */
586 cap &= ~IEEE80211_HT_CAP_SM_PS;
588 case IEEE80211_SMPS_AUTOMATIC:
589 case IEEE80211_SMPS_NUM_MODES:
591 case IEEE80211_SMPS_OFF:
592 cap |= WLAN_HT_CAP_SM_PS_DISABLED <<
593 IEEE80211_HT_CAP_SM_PS_SHIFT;
595 case IEEE80211_SMPS_STATIC:
596 cap |= WLAN_HT_CAP_SM_PS_STATIC <<
597 IEEE80211_HT_CAP_SM_PS_SHIFT;
599 case IEEE80211_SMPS_DYNAMIC:
600 cap |= WLAN_HT_CAP_SM_PS_DYNAMIC <<
601 IEEE80211_HT_CAP_SM_PS_SHIFT;
605 /* reserve and fill IE */
606 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
607 ieee80211_ie_build_ht_cap(pos, &ht_cap, cap);
610 static void ieee80211_add_vht_ie(struct ieee80211_sub_if_data *sdata,
612 struct ieee80211_supported_band *sband,
613 struct ieee80211_vht_cap *ap_vht_cap)
617 struct ieee80211_sta_vht_cap vht_cap;
619 BUILD_BUG_ON(sizeof(vht_cap) != sizeof(sband->vht_cap));
621 memcpy(&vht_cap, &sband->vht_cap, sizeof(vht_cap));
622 ieee80211_apply_vhtcap_overrides(sdata, &vht_cap);
624 /* determine capability flags */
627 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_80P80MHZ) {
628 cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ;
629 cap |= IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
632 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_160MHZ) {
633 cap &= ~IEEE80211_VHT_CAP_SHORT_GI_160;
634 cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
638 * Some APs apparently get confused if our capabilities are better
639 * than theirs, so restrict what we advertise in the assoc request.
641 if (!(ap_vht_cap->vht_cap_info &
642 cpu_to_le32(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)))
643 cap &= ~IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE;
645 /* reserve and fill IE */
646 pos = skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2);
647 ieee80211_ie_build_vht_cap(pos, &vht_cap, cap);
650 static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata)
652 struct ieee80211_local *local = sdata->local;
653 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
654 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
656 struct ieee80211_mgmt *mgmt;
658 size_t offset = 0, noffset;
659 int i, count, rates_len, supp_rates_len;
661 struct ieee80211_supported_band *sband;
662 struct ieee80211_chanctx_conf *chanctx_conf;
663 struct ieee80211_channel *chan;
666 lockdep_assert_held(&ifmgd->mtx);
669 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
670 if (WARN_ON(!chanctx_conf)) {
674 chan = chanctx_conf->def.chan;
676 sband = local->hw.wiphy->bands[chan->band];
678 if (assoc_data->supp_rates_len) {
680 * Get all rates supported by the device and the AP as
681 * some APs don't like getting a superset of their rates
682 * in the association request (e.g. D-Link DAP 1353 in
685 rates_len = ieee80211_compatible_rates(assoc_data->supp_rates,
686 assoc_data->supp_rates_len,
690 * In case AP not provide any supported rates information
691 * before association, we send information element(s) with
692 * all rates that we support.
695 rates_len = sband->n_bitrates;
698 skb = alloc_skb(local->hw.extra_tx_headroom +
699 sizeof(*mgmt) + /* bit too much but doesn't matter */
700 2 + assoc_data->ssid_len + /* SSID */
701 4 + rates_len + /* (extended) rates */
702 4 + /* power capability */
703 2 + 2 * sband->n_channels + /* supported channels */
704 2 + sizeof(struct ieee80211_ht_cap) + /* HT */
705 2 + sizeof(struct ieee80211_vht_cap) + /* VHT */
706 assoc_data->ie_len + /* extra IEs */
712 skb_reserve(skb, local->hw.extra_tx_headroom);
714 capab = WLAN_CAPABILITY_ESS;
716 if (sband->band == IEEE80211_BAND_2GHZ) {
717 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
718 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
719 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
720 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
723 if (assoc_data->capability & WLAN_CAPABILITY_PRIVACY)
724 capab |= WLAN_CAPABILITY_PRIVACY;
726 if ((assoc_data->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
727 (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
728 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
730 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
732 memcpy(mgmt->da, assoc_data->bss->bssid, ETH_ALEN);
733 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
734 memcpy(mgmt->bssid, assoc_data->bss->bssid, ETH_ALEN);
736 if (!is_zero_ether_addr(assoc_data->prev_bssid)) {
738 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
739 IEEE80211_STYPE_REASSOC_REQ);
740 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
741 mgmt->u.reassoc_req.listen_interval =
742 cpu_to_le16(local->hw.conf.listen_interval);
743 memcpy(mgmt->u.reassoc_req.current_ap, assoc_data->prev_bssid,
747 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
748 IEEE80211_STYPE_ASSOC_REQ);
749 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
750 mgmt->u.assoc_req.listen_interval =
751 cpu_to_le16(local->hw.conf.listen_interval);
755 pos = skb_put(skb, 2 + assoc_data->ssid_len);
756 *pos++ = WLAN_EID_SSID;
757 *pos++ = assoc_data->ssid_len;
758 memcpy(pos, assoc_data->ssid, assoc_data->ssid_len);
760 /* add all rates which were marked to be used above */
761 supp_rates_len = rates_len;
762 if (supp_rates_len > 8)
765 pos = skb_put(skb, supp_rates_len + 2);
766 *pos++ = WLAN_EID_SUPP_RATES;
767 *pos++ = supp_rates_len;
770 for (i = 0; i < sband->n_bitrates; i++) {
771 if (BIT(i) & rates) {
772 int rate = sband->bitrates[i].bitrate;
773 *pos++ = (u8) (rate / 5);
779 if (rates_len > count) {
780 pos = skb_put(skb, rates_len - count + 2);
781 *pos++ = WLAN_EID_EXT_SUPP_RATES;
782 *pos++ = rates_len - count;
784 for (i++; i < sband->n_bitrates; i++) {
785 if (BIT(i) & rates) {
786 int rate = sband->bitrates[i].bitrate;
787 *pos++ = (u8) (rate / 5);
792 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
793 /* 1. power capabilities */
794 pos = skb_put(skb, 4);
795 *pos++ = WLAN_EID_PWR_CAPABILITY;
797 *pos++ = 0; /* min tx power */
798 *pos++ = chan->max_power; /* max tx power */
800 /* 2. supported channels */
801 /* TODO: get this in reg domain format */
802 pos = skb_put(skb, 2 * sband->n_channels + 2);
803 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
804 *pos++ = 2 * sband->n_channels;
805 for (i = 0; i < sband->n_channels; i++) {
806 *pos++ = ieee80211_frequency_to_channel(
807 sband->channels[i].center_freq);
808 *pos++ = 1; /* one channel in the subband*/
812 /* if present, add any custom IEs that go before HT */
813 if (assoc_data->ie_len && assoc_data->ie) {
814 static const u8 before_ht[] = {
817 WLAN_EID_EXT_SUPP_RATES,
818 WLAN_EID_PWR_CAPABILITY,
819 WLAN_EID_SUPPORTED_CHANNELS,
822 WLAN_EID_RRM_ENABLED_CAPABILITIES,
823 WLAN_EID_MOBILITY_DOMAIN,
824 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
826 noffset = ieee80211_ie_split(assoc_data->ie, assoc_data->ie_len,
827 before_ht, ARRAY_SIZE(before_ht),
829 pos = skb_put(skb, noffset - offset);
830 memcpy(pos, assoc_data->ie + offset, noffset - offset);
834 if (WARN_ON_ONCE((ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
835 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)))
836 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
838 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
839 ieee80211_add_ht_ie(sdata, skb, assoc_data->ap_ht_param,
840 sband, chan, sdata->smps_mode);
842 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
843 ieee80211_add_vht_ie(sdata, skb, sband,
844 &assoc_data->ap_vht_cap);
846 /* if present, add any custom non-vendor IEs that go after HT */
847 if (assoc_data->ie_len && assoc_data->ie) {
848 noffset = ieee80211_ie_split_vendor(assoc_data->ie,
851 pos = skb_put(skb, noffset - offset);
852 memcpy(pos, assoc_data->ie + offset, noffset - offset);
856 if (assoc_data->wmm) {
857 if (assoc_data->uapsd) {
858 qos_info = ifmgd->uapsd_queues;
859 qos_info |= (ifmgd->uapsd_max_sp_len <<
860 IEEE80211_WMM_IE_STA_QOSINFO_SP_SHIFT);
865 pos = skb_put(skb, 9);
866 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
867 *pos++ = 7; /* len */
868 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
871 *pos++ = 2; /* WME */
872 *pos++ = 0; /* WME info */
873 *pos++ = 1; /* WME ver */
877 /* add any remaining custom (i.e. vendor specific here) IEs */
878 if (assoc_data->ie_len && assoc_data->ie) {
879 noffset = assoc_data->ie_len;
880 pos = skb_put(skb, noffset - offset);
881 memcpy(pos, assoc_data->ie + offset, noffset - offset);
884 drv_mgd_prepare_tx(local, sdata);
886 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
887 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
888 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS |
889 IEEE80211_TX_INTFL_MLME_CONN_TX;
890 ieee80211_tx_skb(sdata, skb);
893 void ieee80211_send_pspoll(struct ieee80211_local *local,
894 struct ieee80211_sub_if_data *sdata)
896 struct ieee80211_pspoll *pspoll;
899 skb = ieee80211_pspoll_get(&local->hw, &sdata->vif);
903 pspoll = (struct ieee80211_pspoll *) skb->data;
904 pspoll->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
906 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
907 ieee80211_tx_skb(sdata, skb);
910 void ieee80211_send_nullfunc(struct ieee80211_local *local,
911 struct ieee80211_sub_if_data *sdata,
915 struct ieee80211_hdr_3addr *nullfunc;
916 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
918 skb = ieee80211_nullfunc_get(&local->hw, &sdata->vif);
922 nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
924 nullfunc->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
926 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
927 IEEE80211_TX_INTFL_OFFCHAN_TX_OK;
928 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
929 IEEE80211_STA_CONNECTION_POLL))
930 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_USE_MINRATE;
932 ieee80211_tx_skb(sdata, skb);
935 static void ieee80211_send_4addr_nullfunc(struct ieee80211_local *local,
936 struct ieee80211_sub_if_data *sdata)
939 struct ieee80211_hdr *nullfunc;
942 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
945 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 30);
949 skb_reserve(skb, local->hw.extra_tx_headroom);
951 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 30);
952 memset(nullfunc, 0, 30);
953 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
954 IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
955 nullfunc->frame_control = fc;
956 memcpy(nullfunc->addr1, sdata->u.mgd.bssid, ETH_ALEN);
957 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
958 memcpy(nullfunc->addr3, sdata->u.mgd.bssid, ETH_ALEN);
959 memcpy(nullfunc->addr4, sdata->vif.addr, ETH_ALEN);
961 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
962 ieee80211_tx_skb(sdata, skb);
965 /* spectrum management related things */
966 static void ieee80211_chswitch_work(struct work_struct *work)
968 struct ieee80211_sub_if_data *sdata =
969 container_of(work, struct ieee80211_sub_if_data, u.mgd.chswitch_work);
970 struct ieee80211_local *local = sdata->local;
971 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
973 if (!ieee80211_sdata_running(sdata))
976 mutex_lock(&ifmgd->mtx);
977 if (!ifmgd->associated)
980 local->_oper_chandef = local->csa_chandef;
982 if (!local->ops->channel_switch) {
983 /* call "hw_config" only if doing sw channel switch */
984 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
986 /* update the device channel directly */
987 local->hw.conf.chandef = local->_oper_chandef;
990 /* XXX: shouldn't really modify cfg80211-owned data! */
991 ifmgd->associated->channel = local->_oper_chandef.chan;
993 /* XXX: wait for a beacon first? */
994 ieee80211_wake_queues_by_reason(&local->hw,
995 IEEE80211_MAX_QUEUE_MAP,
996 IEEE80211_QUEUE_STOP_REASON_CSA);
998 ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
999 mutex_unlock(&ifmgd->mtx);
1002 void ieee80211_chswitch_done(struct ieee80211_vif *vif, bool success)
1004 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1005 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1007 trace_api_chswitch_done(sdata, success);
1010 "driver channel switch failed, disconnecting\n");
1011 ieee80211_queue_work(&sdata->local->hw,
1012 &ifmgd->csa_connection_drop_work);
1014 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
1017 EXPORT_SYMBOL(ieee80211_chswitch_done);
1019 static void ieee80211_chswitch_timer(unsigned long data)
1021 struct ieee80211_sub_if_data *sdata =
1022 (struct ieee80211_sub_if_data *) data;
1024 ieee80211_queue_work(&sdata->local->hw, &sdata->u.mgd.chswitch_work);
1028 ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
1029 u64 timestamp, struct ieee802_11_elems *elems,
1032 struct ieee80211_local *local = sdata->local;
1033 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1034 struct cfg80211_bss *cbss = ifmgd->associated;
1035 struct ieee80211_bss *bss;
1036 struct ieee80211_chanctx *chanctx;
1037 enum ieee80211_band new_band;
1042 struct ieee80211_channel *new_chan;
1043 struct cfg80211_chan_def new_chandef = {};
1044 struct cfg80211_chan_def new_vht_chandef = {};
1045 const struct ieee80211_sec_chan_offs_ie *sec_chan_offs;
1046 const struct ieee80211_wide_bw_chansw_ie *wide_bw_chansw_ie;
1047 const struct ieee80211_ht_operation *ht_oper;
1048 int secondary_channel_offset = -1;
1050 ASSERT_MGD_MTX(ifmgd);
1055 if (local->scanning)
1058 /* disregard subsequent announcements if we are already processing */
1059 if (ifmgd->flags & IEEE80211_STA_CSA_RECEIVED)
1062 sec_chan_offs = elems->sec_chan_offs;
1063 wide_bw_chansw_ie = elems->wide_bw_chansw_ie;
1064 ht_oper = elems->ht_operation;
1066 if (ifmgd->flags & (IEEE80211_STA_DISABLE_HT |
1067 IEEE80211_STA_DISABLE_40MHZ)) {
1068 sec_chan_offs = NULL;
1069 wide_bw_chansw_ie = NULL;
1070 /* only used for bandwidth here */
1074 if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
1075 wide_bw_chansw_ie = NULL;
1077 if (elems->ext_chansw_ie) {
1078 if (!ieee80211_operating_class_to_band(
1079 elems->ext_chansw_ie->new_operating_class,
1082 "cannot understand ECSA IE operating class %d, disconnecting\n",
1083 elems->ext_chansw_ie->new_operating_class);
1084 ieee80211_queue_work(&local->hw,
1085 &ifmgd->csa_connection_drop_work);
1087 new_chan_no = elems->ext_chansw_ie->new_ch_num;
1088 count = elems->ext_chansw_ie->count;
1089 mode = elems->ext_chansw_ie->mode;
1090 } else if (elems->ch_switch_ie) {
1091 new_band = cbss->channel->band;
1092 new_chan_no = elems->ch_switch_ie->new_ch_num;
1093 count = elems->ch_switch_ie->count;
1094 mode = elems->ch_switch_ie->mode;
1096 /* nothing here we understand */
1100 bss = (void *)cbss->priv;
1102 new_freq = ieee80211_channel_to_frequency(new_chan_no, new_band);
1103 new_chan = ieee80211_get_channel(sdata->local->hw.wiphy, new_freq);
1104 if (!new_chan || new_chan->flags & IEEE80211_CHAN_DISABLED) {
1106 "AP %pM switches to unsupported channel (%d MHz), disconnecting\n",
1107 ifmgd->associated->bssid, new_freq);
1108 ieee80211_queue_work(&local->hw,
1109 &ifmgd->csa_connection_drop_work);
1113 if (!beacon && sec_chan_offs) {
1114 secondary_channel_offset = sec_chan_offs->sec_chan_offs;
1115 } else if (beacon && ht_oper) {
1116 secondary_channel_offset =
1117 ht_oper->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET;
1118 } else if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
1120 * If it's not a beacon, HT is enabled and the IE not present,
1121 * it's 20 MHz, 802.11-2012 8.5.2.6:
1122 * This element [the Secondary Channel Offset Element] is
1123 * present when switching to a 40 MHz channel. It may be
1124 * present when switching to a 20 MHz channel (in which
1125 * case the secondary channel offset is set to SCN).
1127 secondary_channel_offset = IEEE80211_HT_PARAM_CHA_SEC_NONE;
1130 switch (secondary_channel_offset) {
1132 /* secondary_channel_offset was present but is invalid */
1133 case IEEE80211_HT_PARAM_CHA_SEC_NONE:
1134 cfg80211_chandef_create(&new_chandef, new_chan,
1137 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
1138 cfg80211_chandef_create(&new_chandef, new_chan,
1139 NL80211_CHAN_HT40PLUS);
1141 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
1142 cfg80211_chandef_create(&new_chandef, new_chan,
1143 NL80211_CHAN_HT40MINUS);
1146 cfg80211_chandef_create(&new_chandef, new_chan,
1147 NL80211_CHAN_NO_HT);
1151 if (wide_bw_chansw_ie) {
1152 new_vht_chandef.chan = new_chan;
1153 new_vht_chandef.center_freq1 =
1154 ieee80211_channel_to_frequency(
1155 wide_bw_chansw_ie->new_center_freq_seg0,
1158 switch (wide_bw_chansw_ie->new_channel_width) {
1160 /* hmmm, ignore VHT and use HT if present */
1161 case IEEE80211_VHT_CHANWIDTH_USE_HT:
1162 new_vht_chandef.chan = NULL;
1164 case IEEE80211_VHT_CHANWIDTH_80MHZ:
1165 new_vht_chandef.width = NL80211_CHAN_WIDTH_80;
1167 case IEEE80211_VHT_CHANWIDTH_160MHZ:
1168 new_vht_chandef.width = NL80211_CHAN_WIDTH_160;
1170 case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
1171 /* field is otherwise reserved */
1172 new_vht_chandef.center_freq2 =
1173 ieee80211_channel_to_frequency(
1174 wide_bw_chansw_ie->new_center_freq_seg1,
1176 new_vht_chandef.width = NL80211_CHAN_WIDTH_80P80;
1179 if (ifmgd->flags & IEEE80211_STA_DISABLE_80P80MHZ &&
1180 new_vht_chandef.width == NL80211_CHAN_WIDTH_80P80)
1181 chandef_downgrade(&new_vht_chandef);
1182 if (ifmgd->flags & IEEE80211_STA_DISABLE_160MHZ &&
1183 new_vht_chandef.width == NL80211_CHAN_WIDTH_160)
1184 chandef_downgrade(&new_vht_chandef);
1185 if (ifmgd->flags & IEEE80211_STA_DISABLE_40MHZ &&
1186 new_vht_chandef.width > NL80211_CHAN_WIDTH_20)
1187 chandef_downgrade(&new_vht_chandef);
1190 /* if VHT data is there validate & use it */
1191 if (new_vht_chandef.chan) {
1192 if (!cfg80211_chandef_compatible(&new_vht_chandef,
1195 "AP %pM CSA has inconsistent channel data, disconnecting\n",
1196 ifmgd->associated->bssid);
1197 ieee80211_queue_work(&local->hw,
1198 &ifmgd->csa_connection_drop_work);
1201 new_chandef = new_vht_chandef;
1204 if (!cfg80211_chandef_usable(local->hw.wiphy, &new_chandef,
1205 IEEE80211_CHAN_DISABLED)) {
1207 "AP %pM switches to unsupported channel (%d MHz, width:%d, CF1/2: %d/%d MHz), disconnecting\n",
1208 ifmgd->associated->bssid, new_freq,
1209 new_chandef.width, new_chandef.center_freq1,
1210 new_chandef.center_freq2);
1211 ieee80211_queue_work(&local->hw,
1212 &ifmgd->csa_connection_drop_work);
1216 ifmgd->flags |= IEEE80211_STA_CSA_RECEIVED;
1218 if (local->use_chanctx) {
1220 "not handling channel switch with channel contexts\n");
1221 ieee80211_queue_work(&local->hw,
1222 &ifmgd->csa_connection_drop_work);
1226 mutex_lock(&local->chanctx_mtx);
1227 if (WARN_ON(!rcu_access_pointer(sdata->vif.chanctx_conf))) {
1228 mutex_unlock(&local->chanctx_mtx);
1231 chanctx = container_of(rcu_access_pointer(sdata->vif.chanctx_conf),
1232 struct ieee80211_chanctx, conf);
1233 if (chanctx->refcount > 1) {
1235 "channel switch with multiple interfaces on the same channel, disconnecting\n");
1236 ieee80211_queue_work(&local->hw,
1237 &ifmgd->csa_connection_drop_work);
1238 mutex_unlock(&local->chanctx_mtx);
1241 mutex_unlock(&local->chanctx_mtx);
1243 local->csa_chandef = new_chandef;
1246 ieee80211_stop_queues_by_reason(&local->hw,
1247 IEEE80211_MAX_QUEUE_MAP,
1248 IEEE80211_QUEUE_STOP_REASON_CSA);
1250 if (local->ops->channel_switch) {
1251 /* use driver's channel switch callback */
1252 struct ieee80211_channel_switch ch_switch = {
1253 .timestamp = timestamp,
1255 .chandef = new_chandef,
1259 drv_channel_switch(local, &ch_switch);
1263 /* channel switch handled in software */
1265 ieee80211_queue_work(&local->hw, &ifmgd->chswitch_work);
1267 mod_timer(&ifmgd->chswitch_timer,
1268 TU_TO_EXP_TIME(count * cbss->beacon_interval));
1271 static u32 ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data *sdata,
1272 struct ieee80211_channel *channel,
1273 const u8 *country_ie, u8 country_ie_len,
1274 const u8 *pwr_constr_elem)
1276 struct ieee80211_country_ie_triplet *triplet;
1277 int chan = ieee80211_frequency_to_channel(channel->center_freq);
1278 int i, chan_pwr, chan_increment, new_ap_level;
1279 bool have_chan_pwr = false;
1282 if (country_ie_len % 2 || country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
1285 triplet = (void *)(country_ie + 3);
1286 country_ie_len -= 3;
1288 switch (channel->band) {
1292 case IEEE80211_BAND_2GHZ:
1293 case IEEE80211_BAND_60GHZ:
1296 case IEEE80211_BAND_5GHZ:
1302 while (country_ie_len >= 3) {
1303 u8 first_channel = triplet->chans.first_channel;
1305 if (first_channel >= IEEE80211_COUNTRY_EXTENSION_ID)
1308 for (i = 0; i < triplet->chans.num_channels; i++) {
1309 if (first_channel + i * chan_increment == chan) {
1310 have_chan_pwr = true;
1311 chan_pwr = triplet->chans.max_power;
1320 country_ie_len -= 3;
1326 new_ap_level = max_t(int, 0, chan_pwr - *pwr_constr_elem);
1328 if (sdata->ap_power_level == new_ap_level)
1332 "Limiting TX power to %d (%d - %d) dBm as advertised by %pM\n",
1333 new_ap_level, chan_pwr, *pwr_constr_elem,
1334 sdata->u.mgd.bssid);
1335 sdata->ap_power_level = new_ap_level;
1336 if (__ieee80211_recalc_txpower(sdata))
1337 return BSS_CHANGED_TXPOWER;
1342 static void ieee80211_enable_ps(struct ieee80211_local *local,
1343 struct ieee80211_sub_if_data *sdata)
1345 struct ieee80211_conf *conf = &local->hw.conf;
1348 * If we are scanning right now then the parameters will
1349 * take effect when scan finishes.
1351 if (local->scanning)
1354 if (conf->dynamic_ps_timeout > 0 &&
1355 !(local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)) {
1356 mod_timer(&local->dynamic_ps_timer, jiffies +
1357 msecs_to_jiffies(conf->dynamic_ps_timeout));
1359 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
1360 ieee80211_send_nullfunc(local, sdata, 1);
1362 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
1363 (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS))
1366 conf->flags |= IEEE80211_CONF_PS;
1367 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1371 static void ieee80211_change_ps(struct ieee80211_local *local)
1373 struct ieee80211_conf *conf = &local->hw.conf;
1375 if (local->ps_sdata) {
1376 ieee80211_enable_ps(local, local->ps_sdata);
1377 } else if (conf->flags & IEEE80211_CONF_PS) {
1378 conf->flags &= ~IEEE80211_CONF_PS;
1379 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1380 del_timer_sync(&local->dynamic_ps_timer);
1381 cancel_work_sync(&local->dynamic_ps_enable_work);
1385 static bool ieee80211_powersave_allowed(struct ieee80211_sub_if_data *sdata)
1387 struct ieee80211_if_managed *mgd = &sdata->u.mgd;
1388 struct sta_info *sta = NULL;
1389 bool authorized = false;
1391 if (!mgd->powersave)
1397 if (!mgd->associated)
1400 if (mgd->flags & (IEEE80211_STA_BEACON_POLL |
1401 IEEE80211_STA_CONNECTION_POLL))
1405 sta = sta_info_get(sdata, mgd->bssid);
1407 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
1413 /* need to hold RTNL or interface lock */
1414 void ieee80211_recalc_ps(struct ieee80211_local *local, s32 latency)
1416 struct ieee80211_sub_if_data *sdata, *found = NULL;
1420 if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS)) {
1421 local->ps_sdata = NULL;
1425 list_for_each_entry(sdata, &local->interfaces, list) {
1426 if (!ieee80211_sdata_running(sdata))
1428 if (sdata->vif.type == NL80211_IFTYPE_AP) {
1429 /* If an AP vif is found, then disable PS
1430 * by setting the count to zero thereby setting
1436 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1442 if (count == 1 && ieee80211_powersave_allowed(found)) {
1446 latency = pm_qos_request(PM_QOS_NETWORK_LATENCY);
1448 beaconint_us = ieee80211_tu_to_usec(
1449 found->vif.bss_conf.beacon_int);
1451 timeout = local->dynamic_ps_forced_timeout;
1454 * Go to full PSM if the user configures a very low
1455 * latency requirement.
1456 * The 2000 second value is there for compatibility
1457 * until the PM_QOS_NETWORK_LATENCY is configured
1460 if (latency > (1900 * USEC_PER_MSEC) &&
1461 latency != (2000 * USEC_PER_SEC))
1466 local->hw.conf.dynamic_ps_timeout = timeout;
1468 if (beaconint_us > latency) {
1469 local->ps_sdata = NULL;
1472 u8 dtimper = found->u.mgd.dtim_period;
1474 /* If the TIM IE is invalid, pretend the value is 1 */
1477 else if (dtimper > 1)
1478 maxslp = min_t(int, dtimper,
1479 latency / beaconint_us);
1481 local->hw.conf.max_sleep_period = maxslp;
1482 local->hw.conf.ps_dtim_period = dtimper;
1483 local->ps_sdata = found;
1486 local->ps_sdata = NULL;
1489 ieee80211_change_ps(local);
1492 void ieee80211_recalc_ps_vif(struct ieee80211_sub_if_data *sdata)
1494 bool ps_allowed = ieee80211_powersave_allowed(sdata);
1496 if (sdata->vif.bss_conf.ps != ps_allowed) {
1497 sdata->vif.bss_conf.ps = ps_allowed;
1498 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_PS);
1502 void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
1504 struct ieee80211_local *local =
1505 container_of(work, struct ieee80211_local,
1506 dynamic_ps_disable_work);
1508 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1509 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1510 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1513 ieee80211_wake_queues_by_reason(&local->hw,
1514 IEEE80211_MAX_QUEUE_MAP,
1515 IEEE80211_QUEUE_STOP_REASON_PS);
1518 void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
1520 struct ieee80211_local *local =
1521 container_of(work, struct ieee80211_local,
1522 dynamic_ps_enable_work);
1523 struct ieee80211_sub_if_data *sdata = local->ps_sdata;
1524 struct ieee80211_if_managed *ifmgd;
1525 unsigned long flags;
1528 /* can only happen when PS was just disabled anyway */
1532 ifmgd = &sdata->u.mgd;
1534 if (local->hw.conf.flags & IEEE80211_CONF_PS)
1537 if (local->hw.conf.dynamic_ps_timeout > 0) {
1538 /* don't enter PS if TX frames are pending */
1539 if (drv_tx_frames_pending(local)) {
1540 mod_timer(&local->dynamic_ps_timer, jiffies +
1542 local->hw.conf.dynamic_ps_timeout));
1547 * transmission can be stopped by others which leads to
1548 * dynamic_ps_timer expiry. Postpone the ps timer if it
1549 * is not the actual idle state.
1551 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
1552 for (q = 0; q < local->hw.queues; q++) {
1553 if (local->queue_stop_reasons[q]) {
1554 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1556 mod_timer(&local->dynamic_ps_timer, jiffies +
1558 local->hw.conf.dynamic_ps_timeout));
1562 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
1565 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
1566 !(ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
1567 if (drv_tx_frames_pending(local)) {
1568 mod_timer(&local->dynamic_ps_timer, jiffies +
1570 local->hw.conf.dynamic_ps_timeout));
1572 ieee80211_send_nullfunc(local, sdata, 1);
1573 /* Flush to get the tx status of nullfunc frame */
1574 ieee80211_flush_queues(local, sdata);
1578 if (!((local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) &&
1579 (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)) ||
1580 (ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
1581 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
1582 local->hw.conf.flags |= IEEE80211_CONF_PS;
1583 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1587 void ieee80211_dynamic_ps_timer(unsigned long data)
1589 struct ieee80211_local *local = (void *) data;
1591 if (local->quiescing || local->suspended)
1594 ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
1597 void ieee80211_dfs_cac_timer_work(struct work_struct *work)
1599 struct delayed_work *delayed_work =
1600 container_of(work, struct delayed_work, work);
1601 struct ieee80211_sub_if_data *sdata =
1602 container_of(delayed_work, struct ieee80211_sub_if_data,
1603 dfs_cac_timer_work);
1605 ieee80211_vif_release_channel(sdata);
1607 cfg80211_cac_event(sdata->dev, NL80211_RADAR_CAC_FINISHED, GFP_KERNEL);
1611 static bool ieee80211_sta_wmm_params(struct ieee80211_local *local,
1612 struct ieee80211_sub_if_data *sdata,
1613 const u8 *wmm_param, size_t wmm_param_len)
1615 struct ieee80211_tx_queue_params params;
1616 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1620 u8 uapsd_queues = 0;
1622 if (!local->ops->conf_tx)
1625 if (local->hw.queues < IEEE80211_NUM_ACS)
1631 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
1634 if (ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED)
1635 uapsd_queues = ifmgd->uapsd_queues;
1637 count = wmm_param[6] & 0x0f;
1638 if (count == ifmgd->wmm_last_param_set)
1640 ifmgd->wmm_last_param_set = count;
1642 pos = wmm_param + 8;
1643 left = wmm_param_len - 8;
1645 memset(¶ms, 0, sizeof(params));
1648 for (; left >= 4; left -= 4, pos += 4) {
1649 int aci = (pos[0] >> 5) & 0x03;
1650 int acm = (pos[0] >> 4) & 0x01;
1658 sdata->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
1659 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
1665 sdata->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
1666 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
1672 sdata->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
1673 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
1680 sdata->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
1681 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
1686 params.aifs = pos[0] & 0x0f;
1687 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
1688 params.cw_min = ecw2cw(pos[1] & 0x0f);
1689 params.txop = get_unaligned_le16(pos + 2);
1691 params.uapsd = uapsd;
1694 "WMM queue=%d aci=%d acm=%d aifs=%d cWmin=%d cWmax=%d txop=%d uapsd=%d\n",
1696 params.aifs, params.cw_min, params.cw_max,
1697 params.txop, params.uapsd);
1698 sdata->tx_conf[queue] = params;
1699 if (drv_conf_tx(local, sdata, queue, ¶ms))
1701 "failed to set TX queue parameters for queue %d\n",
1705 /* enable WMM or activate new settings */
1706 sdata->vif.bss_conf.qos = true;
1710 static void __ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
1712 lockdep_assert_held(&sdata->local->mtx);
1714 sdata->u.mgd.flags &= ~(IEEE80211_STA_CONNECTION_POLL |
1715 IEEE80211_STA_BEACON_POLL);
1716 ieee80211_run_deferred_scan(sdata->local);
1719 static void ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
1721 mutex_lock(&sdata->local->mtx);
1722 __ieee80211_stop_poll(sdata);
1723 mutex_unlock(&sdata->local->mtx);
1726 static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
1727 u16 capab, bool erp_valid, u8 erp)
1729 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
1731 bool use_protection;
1732 bool use_short_preamble;
1733 bool use_short_slot;
1736 use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
1737 use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
1739 use_protection = false;
1740 use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
1743 use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
1744 if (ieee80211_get_sdata_band(sdata) == IEEE80211_BAND_5GHZ)
1745 use_short_slot = true;
1747 if (use_protection != bss_conf->use_cts_prot) {
1748 bss_conf->use_cts_prot = use_protection;
1749 changed |= BSS_CHANGED_ERP_CTS_PROT;
1752 if (use_short_preamble != bss_conf->use_short_preamble) {
1753 bss_conf->use_short_preamble = use_short_preamble;
1754 changed |= BSS_CHANGED_ERP_PREAMBLE;
1757 if (use_short_slot != bss_conf->use_short_slot) {
1758 bss_conf->use_short_slot = use_short_slot;
1759 changed |= BSS_CHANGED_ERP_SLOT;
1765 static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
1766 struct cfg80211_bss *cbss,
1767 u32 bss_info_changed)
1769 struct ieee80211_bss *bss = (void *)cbss->priv;
1770 struct ieee80211_local *local = sdata->local;
1771 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
1773 bss_info_changed |= BSS_CHANGED_ASSOC;
1774 bss_info_changed |= ieee80211_handle_bss_capability(sdata,
1775 bss_conf->assoc_capability, bss->has_erp_value, bss->erp_value);
1777 sdata->u.mgd.beacon_timeout = usecs_to_jiffies(ieee80211_tu_to_usec(
1778 beacon_loss_count * bss_conf->beacon_int));
1780 sdata->u.mgd.associated = cbss;
1781 memcpy(sdata->u.mgd.bssid, cbss->bssid, ETH_ALEN);
1783 sdata->u.mgd.flags |= IEEE80211_STA_RESET_SIGNAL_AVE;
1785 if (sdata->vif.p2p) {
1786 const struct cfg80211_bss_ies *ies;
1789 ies = rcu_dereference(cbss->ies);
1793 ret = cfg80211_get_p2p_attr(
1794 ies->data, ies->len,
1795 IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
1796 (u8 *) &bss_conf->p2p_noa_attr,
1797 sizeof(bss_conf->p2p_noa_attr));
1799 sdata->u.mgd.p2p_noa_index =
1800 bss_conf->p2p_noa_attr.index;
1801 bss_info_changed |= BSS_CHANGED_P2P_PS;
1807 /* just to be sure */
1808 ieee80211_stop_poll(sdata);
1810 ieee80211_led_assoc(local, 1);
1812 if (sdata->u.mgd.assoc_data->have_beacon) {
1814 * If the AP is buggy we may get here with no DTIM period
1815 * known, so assume it's 1 which is the only safe assumption
1816 * in that case, although if the TIM IE is broken powersave
1817 * probably just won't work at all.
1819 bss_conf->dtim_period = sdata->u.mgd.dtim_period ?: 1;
1820 bss_info_changed |= BSS_CHANGED_DTIM_PERIOD;
1822 bss_conf->dtim_period = 0;
1825 bss_conf->assoc = 1;
1827 /* Tell the driver to monitor connection quality (if supported) */
1828 if (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI &&
1829 bss_conf->cqm_rssi_thold)
1830 bss_info_changed |= BSS_CHANGED_CQM;
1832 /* Enable ARP filtering */
1833 if (bss_conf->arp_addr_cnt)
1834 bss_info_changed |= BSS_CHANGED_ARP_FILTER;
1836 ieee80211_bss_info_change_notify(sdata, bss_info_changed);
1838 mutex_lock(&local->iflist_mtx);
1839 ieee80211_recalc_ps(local, -1);
1840 mutex_unlock(&local->iflist_mtx);
1842 ieee80211_recalc_smps(sdata);
1843 ieee80211_recalc_ps_vif(sdata);
1845 netif_carrier_on(sdata->dev);
1848 static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
1849 u16 stype, u16 reason, bool tx,
1852 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1853 struct ieee80211_local *local = sdata->local;
1856 ASSERT_MGD_MTX(ifmgd);
1858 if (WARN_ON_ONCE(tx && !frame_buf))
1861 if (WARN_ON(!ifmgd->associated))
1864 ieee80211_stop_poll(sdata);
1866 ifmgd->associated = NULL;
1867 netif_carrier_off(sdata->dev);
1870 * if we want to get out of ps before disassoc (why?) we have
1871 * to do it before sending disassoc, as otherwise the null-packet
1874 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1875 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1876 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1878 local->ps_sdata = NULL;
1880 /* disable per-vif ps */
1881 ieee80211_recalc_ps_vif(sdata);
1883 /* flush out any pending frame (e.g. DELBA) before deauth/disassoc */
1885 ieee80211_flush_queues(local, sdata);
1887 /* deauthenticate/disassociate now */
1888 if (tx || frame_buf)
1889 ieee80211_send_deauth_disassoc(sdata, ifmgd->bssid, stype,
1890 reason, tx, frame_buf);
1892 /* flush out frame */
1894 ieee80211_flush_queues(local, sdata);
1896 /* clear bssid only after building the needed mgmt frames */
1897 memset(ifmgd->bssid, 0, ETH_ALEN);
1899 /* remove AP and TDLS peers */
1900 sta_info_flush_defer(sdata);
1902 /* finally reset all BSS / config parameters */
1903 changed |= ieee80211_reset_erp_info(sdata);
1905 ieee80211_led_assoc(local, 0);
1906 changed |= BSS_CHANGED_ASSOC;
1907 sdata->vif.bss_conf.assoc = false;
1909 ifmgd->p2p_noa_index = -1;
1910 memset(&sdata->vif.bss_conf.p2p_noa_attr, 0,
1911 sizeof(sdata->vif.bss_conf.p2p_noa_attr));
1913 /* on the next assoc, re-program HT/VHT parameters */
1914 memset(&ifmgd->ht_capa, 0, sizeof(ifmgd->ht_capa));
1915 memset(&ifmgd->ht_capa_mask, 0, sizeof(ifmgd->ht_capa_mask));
1916 memset(&ifmgd->vht_capa, 0, sizeof(ifmgd->vht_capa));
1917 memset(&ifmgd->vht_capa_mask, 0, sizeof(ifmgd->vht_capa_mask));
1919 sdata->ap_power_level = IEEE80211_UNSET_POWER_LEVEL;
1921 del_timer_sync(&local->dynamic_ps_timer);
1922 cancel_work_sync(&local->dynamic_ps_enable_work);
1924 /* Disable ARP filtering */
1925 if (sdata->vif.bss_conf.arp_addr_cnt)
1926 changed |= BSS_CHANGED_ARP_FILTER;
1928 sdata->vif.bss_conf.qos = false;
1929 changed |= BSS_CHANGED_QOS;
1931 /* The BSSID (not really interesting) and HT changed */
1932 changed |= BSS_CHANGED_BSSID | BSS_CHANGED_HT;
1933 ieee80211_bss_info_change_notify(sdata, changed);
1935 /* disassociated - set to defaults now */
1936 ieee80211_set_wmm_default(sdata, false);
1938 del_timer_sync(&sdata->u.mgd.conn_mon_timer);
1939 del_timer_sync(&sdata->u.mgd.bcn_mon_timer);
1940 del_timer_sync(&sdata->u.mgd.timer);
1941 del_timer_sync(&sdata->u.mgd.chswitch_timer);
1943 sdata->vif.bss_conf.dtim_period = 0;
1946 ieee80211_vif_release_channel(sdata);
1949 void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
1950 struct ieee80211_hdr *hdr)
1953 * We can postpone the mgd.timer whenever receiving unicast frames
1954 * from AP because we know that the connection is working both ways
1955 * at that time. But multicast frames (and hence also beacons) must
1956 * be ignored here, because we need to trigger the timer during
1957 * data idle periods for sending the periodic probe request to the
1958 * AP we're connected to.
1960 if (is_multicast_ether_addr(hdr->addr1))
1963 ieee80211_sta_reset_conn_monitor(sdata);
1966 static void ieee80211_reset_ap_probe(struct ieee80211_sub_if_data *sdata)
1968 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1969 struct ieee80211_local *local = sdata->local;
1971 mutex_lock(&local->mtx);
1972 if (!(ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
1973 IEEE80211_STA_CONNECTION_POLL))) {
1974 mutex_unlock(&local->mtx);
1978 __ieee80211_stop_poll(sdata);
1980 mutex_lock(&local->iflist_mtx);
1981 ieee80211_recalc_ps(local, -1);
1982 mutex_unlock(&local->iflist_mtx);
1984 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
1988 * We've received a probe response, but are not sure whether
1989 * we have or will be receiving any beacons or data, so let's
1990 * schedule the timers again, just in case.
1992 ieee80211_sta_reset_beacon_monitor(sdata);
1994 mod_timer(&ifmgd->conn_mon_timer,
1995 round_jiffies_up(jiffies +
1996 IEEE80211_CONNECTION_IDLE_TIME));
1998 mutex_unlock(&local->mtx);
2001 void ieee80211_sta_tx_notify(struct ieee80211_sub_if_data *sdata,
2002 struct ieee80211_hdr *hdr, bool ack)
2004 if (!ieee80211_is_data(hdr->frame_control))
2007 if (ieee80211_is_nullfunc(hdr->frame_control) &&
2008 sdata->u.mgd.probe_send_count > 0) {
2010 ieee80211_sta_reset_conn_monitor(sdata);
2012 sdata->u.mgd.nullfunc_failed = true;
2013 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
2018 ieee80211_sta_reset_conn_monitor(sdata);
2021 static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
2023 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2025 u8 *dst = ifmgd->associated->bssid;
2026 u8 unicast_limit = max(1, max_probe_tries - 3);
2029 * Try sending broadcast probe requests for the last three
2030 * probe requests after the first ones failed since some
2031 * buggy APs only support broadcast probe requests.
2033 if (ifmgd->probe_send_count >= unicast_limit)
2037 * When the hardware reports an accurate Tx ACK status, it's
2038 * better to send a nullfunc frame instead of a probe request,
2039 * as it will kick us off the AP quickly if we aren't associated
2040 * anymore. The timeout will be reset if the frame is ACKed by
2043 ifmgd->probe_send_count++;
2045 if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
2046 ifmgd->nullfunc_failed = false;
2047 ieee80211_send_nullfunc(sdata->local, sdata, 0);
2052 ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
2053 if (WARN_ON_ONCE(ssid == NULL))
2058 ieee80211_send_probe_req(sdata, dst, ssid + 2, ssid_len, NULL,
2059 0, (u32) -1, true, 0,
2060 ifmgd->associated->channel, false);
2064 ifmgd->probe_timeout = jiffies + msecs_to_jiffies(probe_wait_ms);
2065 run_again(ifmgd, ifmgd->probe_timeout);
2066 if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
2067 ieee80211_flush_queues(sdata->local, sdata);
2070 static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
2073 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2074 bool already = false;
2076 if (!ieee80211_sdata_running(sdata))
2079 mutex_lock(&ifmgd->mtx);
2081 if (!ifmgd->associated)
2084 mutex_lock(&sdata->local->mtx);
2086 if (sdata->local->tmp_channel || sdata->local->scanning) {
2087 mutex_unlock(&sdata->local->mtx);
2092 mlme_dbg_ratelimited(sdata,
2093 "detected beacon loss from AP (missed %d beacons) - probing\n",
2096 ieee80211_cqm_rssi_notify(&sdata->vif,
2097 NL80211_CQM_RSSI_BEACON_LOSS_EVENT,
2102 * The driver/our work has already reported this event or the
2103 * connection monitoring has kicked in and we have already sent
2104 * a probe request. Or maybe the AP died and the driver keeps
2105 * reporting until we disassociate...
2107 * In either case we have to ignore the current call to this
2108 * function (except for setting the correct probe reason bit)
2109 * because otherwise we would reset the timer every time and
2110 * never check whether we received a probe response!
2112 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
2113 IEEE80211_STA_CONNECTION_POLL))
2117 ifmgd->flags |= IEEE80211_STA_BEACON_POLL;
2119 ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
2121 mutex_unlock(&sdata->local->mtx);
2126 mutex_lock(&sdata->local->iflist_mtx);
2127 ieee80211_recalc_ps(sdata->local, -1);
2128 mutex_unlock(&sdata->local->iflist_mtx);
2130 ifmgd->probe_send_count = 0;
2131 ieee80211_mgd_probe_ap_send(sdata);
2133 mutex_unlock(&ifmgd->mtx);
2136 struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw,
2137 struct ieee80211_vif *vif)
2139 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2140 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2141 struct cfg80211_bss *cbss;
2142 struct sk_buff *skb;
2146 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
2149 ASSERT_MGD_MTX(ifmgd);
2151 if (ifmgd->associated)
2152 cbss = ifmgd->associated;
2153 else if (ifmgd->auth_data)
2154 cbss = ifmgd->auth_data->bss;
2155 else if (ifmgd->assoc_data)
2156 cbss = ifmgd->assoc_data->bss;
2161 ssid = ieee80211_bss_get_ie(cbss, WLAN_EID_SSID);
2162 if (WARN_ON_ONCE(ssid == NULL))
2167 skb = ieee80211_build_probe_req(sdata, cbss->bssid,
2168 (u32) -1, cbss->channel,
2175 EXPORT_SYMBOL(ieee80211_ap_probereq_get);
2177 static void __ieee80211_disconnect(struct ieee80211_sub_if_data *sdata)
2179 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2180 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
2182 mutex_lock(&ifmgd->mtx);
2183 if (!ifmgd->associated) {
2184 mutex_unlock(&ifmgd->mtx);
2188 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
2189 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
2191 ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
2192 ieee80211_wake_queues_by_reason(&sdata->local->hw,
2193 IEEE80211_MAX_QUEUE_MAP,
2194 IEEE80211_QUEUE_STOP_REASON_CSA);
2195 mutex_unlock(&ifmgd->mtx);
2198 * must be outside lock due to cfg80211,
2199 * but that's not a problem.
2201 cfg80211_send_deauth(sdata->dev, frame_buf, IEEE80211_DEAUTH_FRAME_LEN);
2204 static void ieee80211_beacon_connection_loss_work(struct work_struct *work)
2206 struct ieee80211_sub_if_data *sdata =
2207 container_of(work, struct ieee80211_sub_if_data,
2208 u.mgd.beacon_connection_loss_work);
2209 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2210 struct sta_info *sta;
2212 if (ifmgd->associated) {
2214 sta = sta_info_get(sdata, ifmgd->bssid);
2216 sta->beacon_loss_count++;
2220 if (ifmgd->connection_loss) {
2221 sdata_info(sdata, "Connection to AP %pM lost\n",
2223 __ieee80211_disconnect(sdata);
2225 ieee80211_mgd_probe_ap(sdata, true);
2229 static void ieee80211_csa_connection_drop_work(struct work_struct *work)
2231 struct ieee80211_sub_if_data *sdata =
2232 container_of(work, struct ieee80211_sub_if_data,
2233 u.mgd.csa_connection_drop_work);
2235 __ieee80211_disconnect(sdata);
2238 void ieee80211_beacon_loss(struct ieee80211_vif *vif)
2240 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2241 struct ieee80211_hw *hw = &sdata->local->hw;
2243 trace_api_beacon_loss(sdata);
2245 sdata->u.mgd.connection_loss = false;
2246 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
2248 EXPORT_SYMBOL(ieee80211_beacon_loss);
2250 void ieee80211_connection_loss(struct ieee80211_vif *vif)
2252 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2253 struct ieee80211_hw *hw = &sdata->local->hw;
2255 trace_api_connection_loss(sdata);
2257 sdata->u.mgd.connection_loss = true;
2258 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
2260 EXPORT_SYMBOL(ieee80211_connection_loss);
2263 static void ieee80211_destroy_auth_data(struct ieee80211_sub_if_data *sdata,
2266 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
2268 lockdep_assert_held(&sdata->u.mgd.mtx);
2271 sta_info_destroy_addr(sdata, auth_data->bss->bssid);
2273 memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
2274 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
2275 sdata->u.mgd.flags = 0;
2276 ieee80211_vif_release_channel(sdata);
2279 cfg80211_put_bss(sdata->local->hw.wiphy, auth_data->bss);
2281 sdata->u.mgd.auth_data = NULL;
2284 static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
2285 struct ieee80211_mgmt *mgmt, size_t len)
2287 struct ieee80211_local *local = sdata->local;
2288 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
2290 struct ieee802_11_elems elems;
2293 pos = mgmt->u.auth.variable;
2294 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), false, &elems);
2295 if (!elems.challenge)
2297 auth_data->expected_transaction = 4;
2298 drv_mgd_prepare_tx(sdata->local, sdata);
2299 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
2300 tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
2301 IEEE80211_TX_INTFL_MLME_CONN_TX;
2302 ieee80211_send_auth(sdata, 3, auth_data->algorithm, 0,
2303 elems.challenge - 2, elems.challenge_len + 2,
2304 auth_data->bss->bssid, auth_data->bss->bssid,
2305 auth_data->key, auth_data->key_len,
2306 auth_data->key_idx, tx_flags);
2309 static enum rx_mgmt_action __must_check
2310 ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
2311 struct ieee80211_mgmt *mgmt, size_t len)
2313 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2315 u16 auth_alg, auth_transaction, status_code;
2316 struct sta_info *sta;
2318 lockdep_assert_held(&ifmgd->mtx);
2321 return RX_MGMT_NONE;
2323 if (!ifmgd->auth_data || ifmgd->auth_data->done)
2324 return RX_MGMT_NONE;
2326 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
2328 if (!ether_addr_equal(bssid, mgmt->bssid))
2329 return RX_MGMT_NONE;
2331 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
2332 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
2333 status_code = le16_to_cpu(mgmt->u.auth.status_code);
2335 if (auth_alg != ifmgd->auth_data->algorithm ||
2336 auth_transaction != ifmgd->auth_data->expected_transaction) {
2337 sdata_info(sdata, "%pM unexpected authentication state: alg %d (expected %d) transact %d (expected %d)\n",
2338 mgmt->sa, auth_alg, ifmgd->auth_data->algorithm,
2340 ifmgd->auth_data->expected_transaction);
2341 return RX_MGMT_NONE;
2344 if (status_code != WLAN_STATUS_SUCCESS) {
2345 sdata_info(sdata, "%pM denied authentication (status %d)\n",
2346 mgmt->sa, status_code);
2347 ieee80211_destroy_auth_data(sdata, false);
2348 return RX_MGMT_CFG80211_RX_AUTH;
2351 switch (ifmgd->auth_data->algorithm) {
2352 case WLAN_AUTH_OPEN:
2353 case WLAN_AUTH_LEAP:
2357 case WLAN_AUTH_SHARED_KEY:
2358 if (ifmgd->auth_data->expected_transaction != 4) {
2359 ieee80211_auth_challenge(sdata, mgmt, len);
2360 /* need another frame */
2361 return RX_MGMT_NONE;
2365 WARN_ONCE(1, "invalid auth alg %d",
2366 ifmgd->auth_data->algorithm);
2367 return RX_MGMT_NONE;
2370 sdata_info(sdata, "authenticated\n");
2371 ifmgd->auth_data->done = true;
2372 ifmgd->auth_data->timeout = jiffies + IEEE80211_AUTH_WAIT_ASSOC;
2373 ifmgd->auth_data->timeout_started = true;
2374 run_again(ifmgd, ifmgd->auth_data->timeout);
2376 if (ifmgd->auth_data->algorithm == WLAN_AUTH_SAE &&
2377 ifmgd->auth_data->expected_transaction != 2) {
2379 * Report auth frame to user space for processing since another
2380 * round of Authentication frames is still needed.
2382 return RX_MGMT_CFG80211_RX_AUTH;
2385 /* move station state to auth */
2386 mutex_lock(&sdata->local->sta_mtx);
2387 sta = sta_info_get(sdata, bssid);
2389 WARN_ONCE(1, "%s: STA %pM not found", sdata->name, bssid);
2392 if (sta_info_move_state(sta, IEEE80211_STA_AUTH)) {
2393 sdata_info(sdata, "failed moving %pM to auth\n", bssid);
2396 mutex_unlock(&sdata->local->sta_mtx);
2398 return RX_MGMT_CFG80211_RX_AUTH;
2400 mutex_unlock(&sdata->local->sta_mtx);
2401 /* ignore frame -- wait for timeout */
2402 return RX_MGMT_NONE;
2406 static enum rx_mgmt_action __must_check
2407 ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
2408 struct ieee80211_mgmt *mgmt, size_t len)
2410 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2411 const u8 *bssid = NULL;
2414 lockdep_assert_held(&ifmgd->mtx);
2417 return RX_MGMT_NONE;
2419 if (!ifmgd->associated ||
2420 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
2421 return RX_MGMT_NONE;
2423 bssid = ifmgd->associated->bssid;
2425 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
2427 sdata_info(sdata, "deauthenticated from %pM (Reason: %u)\n",
2428 bssid, reason_code);
2430 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
2432 return RX_MGMT_CFG80211_DEAUTH;
2436 static enum rx_mgmt_action __must_check
2437 ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
2438 struct ieee80211_mgmt *mgmt, size_t len)
2440 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2443 lockdep_assert_held(&ifmgd->mtx);
2446 return RX_MGMT_NONE;
2448 if (!ifmgd->associated ||
2449 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
2450 return RX_MGMT_NONE;
2452 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
2454 sdata_info(sdata, "disassociated from %pM (Reason: %u)\n",
2455 mgmt->sa, reason_code);
2457 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
2459 return RX_MGMT_CFG80211_DISASSOC;
2462 static void ieee80211_get_rates(struct ieee80211_supported_band *sband,
2463 u8 *supp_rates, unsigned int supp_rates_len,
2464 u32 *rates, u32 *basic_rates,
2465 bool *have_higher_than_11mbit,
2466 int *min_rate, int *min_rate_index)
2470 for (i = 0; i < supp_rates_len; i++) {
2471 int rate = (supp_rates[i] & 0x7f) * 5;
2472 bool is_basic = !!(supp_rates[i] & 0x80);
2475 *have_higher_than_11mbit = true;
2478 * BSS_MEMBERSHIP_SELECTOR_HT_PHY is defined in 802.11n-2009
2479 * 7.3.2.2 as a magic value instead of a rate. Hence, skip it.
2481 * Note: Even through the membership selector and the basic
2482 * rate flag share the same bit, they are not exactly
2485 if (!!(supp_rates[i] & 0x80) &&
2486 (supp_rates[i] & 0x7f) == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
2489 for (j = 0; j < sband->n_bitrates; j++) {
2490 if (sband->bitrates[j].bitrate == rate) {
2493 *basic_rates |= BIT(j);
2494 if (rate < *min_rate) {
2496 *min_rate_index = j;
2504 static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata,
2507 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
2509 lockdep_assert_held(&sdata->u.mgd.mtx);
2512 sta_info_destroy_addr(sdata, assoc_data->bss->bssid);
2514 memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
2515 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
2516 sdata->u.mgd.flags = 0;
2517 ieee80211_vif_release_channel(sdata);
2521 sdata->u.mgd.assoc_data = NULL;
2524 static bool ieee80211_assoc_success(struct ieee80211_sub_if_data *sdata,
2525 struct cfg80211_bss *cbss,
2526 struct ieee80211_mgmt *mgmt, size_t len)
2528 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2529 struct ieee80211_local *local = sdata->local;
2530 struct ieee80211_supported_band *sband;
2531 struct sta_info *sta;
2533 u16 capab_info, aid;
2534 struct ieee802_11_elems elems;
2535 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
2536 const struct cfg80211_bss_ies *bss_ies = NULL;
2537 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
2542 /* AssocResp and ReassocResp have identical structure */
2544 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
2545 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
2547 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
2548 sdata_info(sdata, "invalid AID value 0x%x; bits 15:14 not set\n",
2550 aid &= ~(BIT(15) | BIT(14));
2552 ifmgd->broken_ap = false;
2554 if (aid == 0 || aid > IEEE80211_MAX_AID) {
2555 sdata_info(sdata, "invalid AID value %d (out of range), turn off PS\n",
2558 ifmgd->broken_ap = true;
2561 pos = mgmt->u.assoc_resp.variable;
2562 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), false, &elems);
2564 if (!elems.supp_rates) {
2565 sdata_info(sdata, "no SuppRates element in AssocResp\n");
2572 * Some APs are erroneously not including some information in their
2573 * (re)association response frames. Try to recover by using the data
2574 * from the beacon or probe response. This seems to afflict mobile
2575 * 2G/3G/4G wifi routers, reported models include the "Onda PN51T",
2576 * "Vodafone PocketWiFi 2", "ZTE MF60" and a similar T-Mobile device.
2578 if ((assoc_data->wmm && !elems.wmm_param) ||
2579 (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
2580 (!elems.ht_cap_elem || !elems.ht_operation)) ||
2581 (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
2582 (!elems.vht_cap_elem || !elems.vht_operation))) {
2583 const struct cfg80211_bss_ies *ies;
2584 struct ieee802_11_elems bss_elems;
2587 ies = rcu_dereference(cbss->ies);
2589 bss_ies = kmemdup(ies, sizeof(*ies) + ies->len,
2595 ieee802_11_parse_elems(bss_ies->data, bss_ies->len,
2597 if (assoc_data->wmm &&
2598 !elems.wmm_param && bss_elems.wmm_param) {
2599 elems.wmm_param = bss_elems.wmm_param;
2601 "AP bug: WMM param missing from AssocResp\n");
2605 * Also check if we requested HT/VHT, otherwise the AP doesn't
2606 * have to include the IEs in the (re)association response.
2608 if (!elems.ht_cap_elem && bss_elems.ht_cap_elem &&
2609 !(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
2610 elems.ht_cap_elem = bss_elems.ht_cap_elem;
2612 "AP bug: HT capability missing from AssocResp\n");
2614 if (!elems.ht_operation && bss_elems.ht_operation &&
2615 !(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
2616 elems.ht_operation = bss_elems.ht_operation;
2618 "AP bug: HT operation missing from AssocResp\n");
2620 if (!elems.vht_cap_elem && bss_elems.vht_cap_elem &&
2621 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)) {
2622 elems.vht_cap_elem = bss_elems.vht_cap_elem;
2624 "AP bug: VHT capa missing from AssocResp\n");
2626 if (!elems.vht_operation && bss_elems.vht_operation &&
2627 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)) {
2628 elems.vht_operation = bss_elems.vht_operation;
2630 "AP bug: VHT operation missing from AssocResp\n");
2635 * We previously checked these in the beacon/probe response, so
2636 * they should be present here. This is just a safety net.
2638 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
2639 (!elems.wmm_param || !elems.ht_cap_elem || !elems.ht_operation)) {
2641 "HT AP is missing WMM params or HT capability/operation\n");
2646 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
2647 (!elems.vht_cap_elem || !elems.vht_operation)) {
2649 "VHT AP is missing VHT capability/operation\n");
2654 mutex_lock(&sdata->local->sta_mtx);
2656 * station info was already allocated and inserted before
2657 * the association and should be available to us
2659 sta = sta_info_get(sdata, cbss->bssid);
2660 if (WARN_ON(!sta)) {
2661 mutex_unlock(&sdata->local->sta_mtx);
2666 sband = local->hw.wiphy->bands[ieee80211_get_sdata_band(sdata)];
2668 /* Set up internal HT/VHT capabilities */
2669 if (elems.ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
2670 ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
2671 elems.ht_cap_elem, sta);
2673 if (elems.vht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
2674 ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband,
2675 elems.vht_cap_elem, sta);
2678 * Some APs, e.g. Netgear WNDR3700, report invalid HT operation data
2679 * in their association response, so ignore that data for our own
2680 * configuration. If it changed since the last beacon, we'll get the
2681 * next beacon and update then.
2685 * If an operating mode notification IE is present, override the
2686 * NSS calculation (that would be done in rate_control_rate_init())
2687 * and use the # of streams from that element.
2689 if (elems.opmode_notif &&
2690 !(*elems.opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_TYPE_BF)) {
2693 nss = *elems.opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_MASK;
2694 nss >>= IEEE80211_OPMODE_NOTIF_RX_NSS_SHIFT;
2696 sta->sta.rx_nss = nss;
2699 rate_control_rate_init(sta);
2701 if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED)
2702 set_sta_flag(sta, WLAN_STA_MFP);
2704 if (elems.wmm_param)
2705 set_sta_flag(sta, WLAN_STA_WME);
2707 err = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
2708 if (!err && !(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
2709 err = sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
2712 "failed to move station %pM to desired state\n",
2714 WARN_ON(__sta_info_destroy(sta));
2715 mutex_unlock(&sdata->local->sta_mtx);
2720 mutex_unlock(&sdata->local->sta_mtx);
2723 * Always handle WMM once after association regardless
2724 * of the first value the AP uses. Setting -1 here has
2725 * that effect because the AP values is an unsigned
2728 ifmgd->wmm_last_param_set = -1;
2730 if (elems.wmm_param)
2731 ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
2732 elems.wmm_param_len);
2734 ieee80211_set_wmm_default(sdata, false);
2735 changed |= BSS_CHANGED_QOS;
2737 /* set AID and assoc capability,
2738 * ieee80211_set_associated() will tell the driver */
2739 bss_conf->aid = aid;
2740 bss_conf->assoc_capability = capab_info;
2741 ieee80211_set_associated(sdata, cbss, changed);
2744 * If we're using 4-addr mode, let the AP know that we're
2745 * doing so, so that it can create the STA VLAN on its side
2747 if (ifmgd->use_4addr)
2748 ieee80211_send_4addr_nullfunc(local, sdata);
2751 * Start timer to probe the connection to the AP now.
2752 * Also start the timer that will detect beacon loss.
2754 ieee80211_sta_rx_notify(sdata, (struct ieee80211_hdr *)mgmt);
2755 ieee80211_sta_reset_beacon_monitor(sdata);
2763 static enum rx_mgmt_action __must_check
2764 ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
2765 struct ieee80211_mgmt *mgmt, size_t len,
2766 struct cfg80211_bss **bss)
2768 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2769 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
2770 u16 capab_info, status_code, aid;
2771 struct ieee802_11_elems elems;
2775 lockdep_assert_held(&ifmgd->mtx);
2778 return RX_MGMT_NONE;
2779 if (!ether_addr_equal(assoc_data->bss->bssid, mgmt->bssid))
2780 return RX_MGMT_NONE;
2783 * AssocResp and ReassocResp have identical structure, so process both
2784 * of them in this function.
2788 return RX_MGMT_NONE;
2790 reassoc = ieee80211_is_reassoc_req(mgmt->frame_control);
2791 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
2792 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
2793 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
2796 "RX %sssocResp from %pM (capab=0x%x status=%d aid=%d)\n",
2797 reassoc ? "Rea" : "A", mgmt->sa,
2798 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
2800 pos = mgmt->u.assoc_resp.variable;
2801 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), false, &elems);
2803 if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
2804 elems.timeout_int &&
2805 elems.timeout_int->type == WLAN_TIMEOUT_ASSOC_COMEBACK) {
2807 tu = le32_to_cpu(elems.timeout_int->value);
2808 ms = tu * 1024 / 1000;
2810 "%pM rejected association temporarily; comeback duration %u TU (%u ms)\n",
2812 assoc_data->timeout = jiffies + msecs_to_jiffies(ms);
2813 assoc_data->timeout_started = true;
2814 if (ms > IEEE80211_ASSOC_TIMEOUT)
2815 run_again(ifmgd, assoc_data->timeout);
2816 return RX_MGMT_NONE;
2819 *bss = assoc_data->bss;
2821 if (status_code != WLAN_STATUS_SUCCESS) {
2822 sdata_info(sdata, "%pM denied association (code=%d)\n",
2823 mgmt->sa, status_code);
2824 ieee80211_destroy_assoc_data(sdata, false);
2826 if (!ieee80211_assoc_success(sdata, *bss, mgmt, len)) {
2827 /* oops -- internal error -- send timeout for now */
2828 ieee80211_destroy_assoc_data(sdata, false);
2829 cfg80211_put_bss(sdata->local->hw.wiphy, *bss);
2830 return RX_MGMT_CFG80211_ASSOC_TIMEOUT;
2832 sdata_info(sdata, "associated\n");
2835 * destroy assoc_data afterwards, as otherwise an idle
2836 * recalc after assoc_data is NULL but before associated
2837 * is set can cause the interface to go idle
2839 ieee80211_destroy_assoc_data(sdata, true);
2842 return RX_MGMT_CFG80211_RX_ASSOC;
2845 static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
2846 struct ieee80211_mgmt *mgmt, size_t len,
2847 struct ieee80211_rx_status *rx_status,
2848 struct ieee802_11_elems *elems)
2850 struct ieee80211_local *local = sdata->local;
2852 struct ieee80211_bss *bss;
2853 struct ieee80211_channel *channel;
2854 bool need_ps = false;
2856 lockdep_assert_held(&sdata->u.mgd.mtx);
2858 if ((sdata->u.mgd.associated &&
2859 ether_addr_equal(mgmt->bssid, sdata->u.mgd.associated->bssid)) ||
2860 (sdata->u.mgd.assoc_data &&
2861 ether_addr_equal(mgmt->bssid,
2862 sdata->u.mgd.assoc_data->bss->bssid))) {
2863 /* not previously set so we may need to recalc */
2864 need_ps = sdata->u.mgd.associated && !sdata->u.mgd.dtim_period;
2866 if (elems->tim && !elems->parse_error) {
2867 const struct ieee80211_tim_ie *tim_ie = elems->tim;
2868 sdata->u.mgd.dtim_period = tim_ie->dtim_period;
2872 if (elems->ds_params)
2873 freq = ieee80211_channel_to_frequency(elems->ds_params[0],
2876 freq = rx_status->freq;
2878 channel = ieee80211_get_channel(local->hw.wiphy, freq);
2880 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
2883 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
2886 ieee80211_rx_bss_put(local, bss);
2888 if (!sdata->u.mgd.associated ||
2889 !ether_addr_equal(mgmt->bssid, sdata->u.mgd.associated->bssid))
2893 mutex_lock(&local->iflist_mtx);
2894 ieee80211_recalc_ps(local, -1);
2895 mutex_unlock(&local->iflist_mtx);
2898 ieee80211_sta_process_chanswitch(sdata, rx_status->mactime,
2904 static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
2905 struct sk_buff *skb)
2907 struct ieee80211_mgmt *mgmt = (void *)skb->data;
2908 struct ieee80211_if_managed *ifmgd;
2909 struct ieee80211_rx_status *rx_status = (void *) skb->cb;
2910 size_t baselen, len = skb->len;
2911 struct ieee802_11_elems elems;
2913 ifmgd = &sdata->u.mgd;
2915 ASSERT_MGD_MTX(ifmgd);
2917 if (!ether_addr_equal(mgmt->da, sdata->vif.addr))
2918 return; /* ignore ProbeResp to foreign address */
2920 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
2924 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
2927 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
2929 if (ifmgd->associated &&
2930 ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
2931 ieee80211_reset_ap_probe(sdata);
2933 if (ifmgd->auth_data && !ifmgd->auth_data->bss->proberesp_ies &&
2934 ether_addr_equal(mgmt->bssid, ifmgd->auth_data->bss->bssid)) {
2935 /* got probe response, continue with auth */
2936 sdata_info(sdata, "direct probe responded\n");
2937 ifmgd->auth_data->tries = 0;
2938 ifmgd->auth_data->timeout = jiffies;
2939 ifmgd->auth_data->timeout_started = true;
2940 run_again(ifmgd, ifmgd->auth_data->timeout);
2945 * This is the canonical list of information elements we care about,
2946 * the filter code also gives us all changes to the Microsoft OUI
2947 * (00:50:F2) vendor IE which is used for WMM which we need to track.
2949 * We implement beacon filtering in software since that means we can
2950 * avoid processing the frame here and in cfg80211, and userspace
2951 * will not be able to tell whether the hardware supports it or not.
2953 * XXX: This list needs to be dynamic -- userspace needs to be able to
2954 * add items it requires. It also needs to be able to tell us to
2955 * look out for other vendor IEs.
2957 static const u64 care_about_ies =
2958 (1ULL << WLAN_EID_COUNTRY) |
2959 (1ULL << WLAN_EID_ERP_INFO) |
2960 (1ULL << WLAN_EID_CHANNEL_SWITCH) |
2961 (1ULL << WLAN_EID_PWR_CONSTRAINT) |
2962 (1ULL << WLAN_EID_HT_CAPABILITY) |
2963 (1ULL << WLAN_EID_HT_OPERATION);
2965 static enum rx_mgmt_action
2966 ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
2967 struct ieee80211_mgmt *mgmt, size_t len,
2968 u8 *deauth_buf, struct ieee80211_rx_status *rx_status)
2970 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2971 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
2973 struct ieee802_11_elems elems;
2974 struct ieee80211_local *local = sdata->local;
2975 struct ieee80211_chanctx_conf *chanctx_conf;
2976 struct ieee80211_channel *chan;
2977 struct sta_info *sta;
2984 lockdep_assert_held(&ifmgd->mtx);
2986 /* Process beacon from the current BSS */
2987 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2989 return RX_MGMT_NONE;
2992 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2993 if (!chanctx_conf) {
2995 return RX_MGMT_NONE;
2998 if (rx_status->freq != chanctx_conf->def.chan->center_freq) {
3000 return RX_MGMT_NONE;
3002 chan = chanctx_conf->def.chan;
3005 if (ifmgd->assoc_data && ifmgd->assoc_data->need_beacon &&
3006 ether_addr_equal(mgmt->bssid, ifmgd->assoc_data->bss->bssid)) {
3007 ieee802_11_parse_elems(mgmt->u.beacon.variable,
3008 len - baselen, false, &elems);
3010 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
3011 ifmgd->assoc_data->have_beacon = true;
3012 ifmgd->assoc_data->need_beacon = false;
3013 if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
3014 sdata->vif.bss_conf.sync_tsf =
3015 le64_to_cpu(mgmt->u.beacon.timestamp);
3016 sdata->vif.bss_conf.sync_device_ts =
3017 rx_status->device_timestamp;
3019 sdata->vif.bss_conf.sync_dtim_count =
3020 elems.tim->dtim_count;
3022 sdata->vif.bss_conf.sync_dtim_count = 0;
3024 /* continue assoc process */
3025 ifmgd->assoc_data->timeout = jiffies;
3026 ifmgd->assoc_data->timeout_started = true;
3027 run_again(ifmgd, ifmgd->assoc_data->timeout);
3028 return RX_MGMT_NONE;
3031 if (!ifmgd->associated ||
3032 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
3033 return RX_MGMT_NONE;
3034 bssid = ifmgd->associated->bssid;
3036 /* Track average RSSI from the Beacon frames of the current AP */
3037 ifmgd->last_beacon_signal = rx_status->signal;
3038 if (ifmgd->flags & IEEE80211_STA_RESET_SIGNAL_AVE) {
3039 ifmgd->flags &= ~IEEE80211_STA_RESET_SIGNAL_AVE;
3040 ifmgd->ave_beacon_signal = rx_status->signal * 16;
3041 ifmgd->last_cqm_event_signal = 0;
3042 ifmgd->count_beacon_signal = 1;
3043 ifmgd->last_ave_beacon_signal = 0;
3045 ifmgd->ave_beacon_signal =
3046 (IEEE80211_SIGNAL_AVE_WEIGHT * rx_status->signal * 16 +
3047 (16 - IEEE80211_SIGNAL_AVE_WEIGHT) *
3048 ifmgd->ave_beacon_signal) / 16;
3049 ifmgd->count_beacon_signal++;
3052 if (ifmgd->rssi_min_thold != ifmgd->rssi_max_thold &&
3053 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
3054 int sig = ifmgd->ave_beacon_signal;
3055 int last_sig = ifmgd->last_ave_beacon_signal;
3058 * if signal crosses either of the boundaries, invoke callback
3059 * with appropriate parameters
3061 if (sig > ifmgd->rssi_max_thold &&
3062 (last_sig <= ifmgd->rssi_min_thold || last_sig == 0)) {
3063 ifmgd->last_ave_beacon_signal = sig;
3064 drv_rssi_callback(local, sdata, RSSI_EVENT_HIGH);
3065 } else if (sig < ifmgd->rssi_min_thold &&
3066 (last_sig >= ifmgd->rssi_max_thold ||
3068 ifmgd->last_ave_beacon_signal = sig;
3069 drv_rssi_callback(local, sdata, RSSI_EVENT_LOW);
3073 if (bss_conf->cqm_rssi_thold &&
3074 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT &&
3075 !(sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI)) {
3076 int sig = ifmgd->ave_beacon_signal / 16;
3077 int last_event = ifmgd->last_cqm_event_signal;
3078 int thold = bss_conf->cqm_rssi_thold;
3079 int hyst = bss_conf->cqm_rssi_hyst;
3081 (last_event == 0 || sig < last_event - hyst)) {
3082 ifmgd->last_cqm_event_signal = sig;
3083 ieee80211_cqm_rssi_notify(
3085 NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
3087 } else if (sig > thold &&
3088 (last_event == 0 || sig > last_event + hyst)) {
3089 ifmgd->last_cqm_event_signal = sig;
3090 ieee80211_cqm_rssi_notify(
3092 NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
3097 if (ifmgd->flags & IEEE80211_STA_BEACON_POLL) {
3098 mlme_dbg_ratelimited(sdata,
3099 "cancelling AP probe due to a received beacon\n");
3100 mutex_lock(&local->mtx);
3101 ifmgd->flags &= ~IEEE80211_STA_BEACON_POLL;
3102 ieee80211_run_deferred_scan(local);
3103 mutex_unlock(&local->mtx);
3105 mutex_lock(&local->iflist_mtx);
3106 ieee80211_recalc_ps(local, -1);
3107 mutex_unlock(&local->iflist_mtx);
3111 * Push the beacon loss detection into the future since
3112 * we are processing a beacon from the AP just now.
3114 ieee80211_sta_reset_beacon_monitor(sdata);
3116 ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
3117 ncrc = ieee802_11_parse_elems_crc(mgmt->u.beacon.variable,
3118 len - baselen, false, &elems,
3119 care_about_ies, ncrc);
3121 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) {
3122 bool directed_tim = ieee80211_check_tim(elems.tim,
3126 if (local->hw.conf.dynamic_ps_timeout > 0) {
3127 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
3128 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
3129 ieee80211_hw_config(local,
3130 IEEE80211_CONF_CHANGE_PS);
3132 ieee80211_send_nullfunc(local, sdata, 0);
3133 } else if (!local->pspolling && sdata->u.mgd.powersave) {
3134 local->pspolling = true;
3137 * Here is assumed that the driver will be
3138 * able to send ps-poll frame and receive a
3139 * response even though power save mode is
3140 * enabled, but some drivers might require
3141 * to disable power save here. This needs
3142 * to be investigated.
3144 ieee80211_send_pspoll(local, sdata);
3149 if (sdata->vif.p2p) {
3150 struct ieee80211_p2p_noa_attr noa = {};
3153 ret = cfg80211_get_p2p_attr(mgmt->u.beacon.variable,
3155 IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
3156 (u8 *) &noa, sizeof(noa));
3158 if (sdata->u.mgd.p2p_noa_index != noa.index) {
3159 /* valid noa_attr and index changed */
3160 sdata->u.mgd.p2p_noa_index = noa.index;
3161 memcpy(&bss_conf->p2p_noa_attr, &noa, sizeof(noa));
3162 changed |= BSS_CHANGED_P2P_PS;
3164 * make sure we update all information, the CRC
3165 * mechanism doesn't look at P2P attributes.
3167 ifmgd->beacon_crc_valid = false;
3169 } else if (sdata->u.mgd.p2p_noa_index != -1) {
3170 /* noa_attr not found and we had valid noa_attr before */
3171 sdata->u.mgd.p2p_noa_index = -1;
3172 memset(&bss_conf->p2p_noa_attr, 0, sizeof(bss_conf->p2p_noa_attr));
3173 changed |= BSS_CHANGED_P2P_PS;
3174 ifmgd->beacon_crc_valid = false;
3178 if (ncrc == ifmgd->beacon_crc && ifmgd->beacon_crc_valid)
3179 return RX_MGMT_NONE;
3180 ifmgd->beacon_crc = ncrc;
3181 ifmgd->beacon_crc_valid = true;
3183 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
3185 if (ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
3186 elems.wmm_param_len))
3187 changed |= BSS_CHANGED_QOS;
3190 * If we haven't had a beacon before, tell the driver about the
3191 * DTIM period (and beacon timing if desired) now.
3193 if (!bss_conf->dtim_period) {
3194 /* a few bogus AP send dtim_period = 0 or no TIM IE */
3196 bss_conf->dtim_period = elems.tim->dtim_period ?: 1;
3198 bss_conf->dtim_period = 1;
3200 if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
3201 sdata->vif.bss_conf.sync_tsf =
3202 le64_to_cpu(mgmt->u.beacon.timestamp);
3203 sdata->vif.bss_conf.sync_device_ts =
3204 rx_status->device_timestamp;
3206 sdata->vif.bss_conf.sync_dtim_count =
3207 elems.tim->dtim_count;
3209 sdata->vif.bss_conf.sync_dtim_count = 0;
3212 changed |= BSS_CHANGED_DTIM_PERIOD;
3215 if (elems.erp_info) {
3217 erp_value = elems.erp_info[0];
3221 changed |= ieee80211_handle_bss_capability(sdata,
3222 le16_to_cpu(mgmt->u.beacon.capab_info),
3223 erp_valid, erp_value);
3225 mutex_lock(&local->sta_mtx);
3226 sta = sta_info_get(sdata, bssid);
3228 if (ieee80211_config_bw(sdata, sta, elems.ht_operation,
3229 elems.vht_operation, bssid, &changed)) {
3230 mutex_unlock(&local->sta_mtx);
3231 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
3232 WLAN_REASON_DEAUTH_LEAVING,
3234 return RX_MGMT_CFG80211_TX_DEAUTH;
3237 if (sta && elems.opmode_notif)
3238 ieee80211_vht_handle_opmode(sdata, sta, *elems.opmode_notif,
3239 rx_status->band, true);
3240 mutex_unlock(&local->sta_mtx);
3242 if (elems.country_elem && elems.pwr_constr_elem &&
3243 mgmt->u.probe_resp.capab_info &
3244 cpu_to_le16(WLAN_CAPABILITY_SPECTRUM_MGMT))
3245 changed |= ieee80211_handle_pwr_constr(sdata, chan,
3247 elems.country_elem_len,
3248 elems.pwr_constr_elem);
3250 ieee80211_bss_info_change_notify(sdata, changed);
3252 return RX_MGMT_NONE;
3255 void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
3256 struct sk_buff *skb)
3258 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3259 struct ieee80211_rx_status *rx_status;
3260 struct ieee80211_mgmt *mgmt;
3261 struct cfg80211_bss *bss = NULL;
3262 enum rx_mgmt_action rma = RX_MGMT_NONE;
3263 u8 deauth_buf[IEEE80211_DEAUTH_FRAME_LEN];
3265 struct ieee802_11_elems elems;
3268 rx_status = (struct ieee80211_rx_status *) skb->cb;
3269 mgmt = (struct ieee80211_mgmt *) skb->data;
3270 fc = le16_to_cpu(mgmt->frame_control);
3272 mutex_lock(&ifmgd->mtx);
3274 switch (fc & IEEE80211_FCTL_STYPE) {
3275 case IEEE80211_STYPE_BEACON:
3276 rma = ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len,
3277 deauth_buf, rx_status);
3279 case IEEE80211_STYPE_PROBE_RESP:
3280 ieee80211_rx_mgmt_probe_resp(sdata, skb);
3282 case IEEE80211_STYPE_AUTH:
3283 rma = ieee80211_rx_mgmt_auth(sdata, mgmt, skb->len);
3285 case IEEE80211_STYPE_DEAUTH:
3286 rma = ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
3288 case IEEE80211_STYPE_DISASSOC:
3289 rma = ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
3291 case IEEE80211_STYPE_ASSOC_RESP:
3292 case IEEE80211_STYPE_REASSOC_RESP:
3293 rma = ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len, &bss);
3295 case IEEE80211_STYPE_ACTION:
3296 if (mgmt->u.action.category == WLAN_CATEGORY_SPECTRUM_MGMT) {
3297 ies_len = skb->len -
3298 offsetof(struct ieee80211_mgmt,
3299 u.action.u.chan_switch.variable);
3304 ieee802_11_parse_elems(
3305 mgmt->u.action.u.chan_switch.variable,
3306 ies_len, true, &elems);
3308 if (elems.parse_error)
3311 ieee80211_sta_process_chanswitch(sdata,
3314 } else if (mgmt->u.action.category == WLAN_CATEGORY_PUBLIC) {
3315 ies_len = skb->len -
3316 offsetof(struct ieee80211_mgmt,
3317 u.action.u.ext_chan_switch.variable);
3322 ieee802_11_parse_elems(
3323 mgmt->u.action.u.ext_chan_switch.variable,
3324 ies_len, true, &elems);
3326 if (elems.parse_error)
3329 /* for the handling code pretend this was also an IE */
3330 elems.ext_chansw_ie =
3331 &mgmt->u.action.u.ext_chan_switch.data;
3333 ieee80211_sta_process_chanswitch(sdata,
3339 mutex_unlock(&ifmgd->mtx);
3345 case RX_MGMT_CFG80211_DEAUTH:
3346 cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
3348 case RX_MGMT_CFG80211_DISASSOC:
3349 cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
3351 case RX_MGMT_CFG80211_RX_AUTH:
3352 cfg80211_send_rx_auth(sdata->dev, (u8 *)mgmt, skb->len);
3354 case RX_MGMT_CFG80211_RX_ASSOC:
3355 cfg80211_send_rx_assoc(sdata->dev, bss, (u8 *)mgmt, skb->len);
3357 case RX_MGMT_CFG80211_ASSOC_TIMEOUT:
3358 cfg80211_send_assoc_timeout(sdata->dev, mgmt->bssid);
3360 case RX_MGMT_CFG80211_TX_DEAUTH:
3361 cfg80211_send_deauth(sdata->dev, deauth_buf,
3362 sizeof(deauth_buf));
3365 WARN(1, "unexpected: %d", rma);
3369 static void ieee80211_sta_timer(unsigned long data)
3371 struct ieee80211_sub_if_data *sdata =
3372 (struct ieee80211_sub_if_data *) data;
3374 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
3377 static void ieee80211_sta_connection_lost(struct ieee80211_sub_if_data *sdata,
3378 u8 *bssid, u8 reason, bool tx)
3380 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3381 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
3383 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH, reason,
3385 mutex_unlock(&ifmgd->mtx);
3388 * must be outside lock due to cfg80211,
3389 * but that's not a problem.
3391 cfg80211_send_deauth(sdata->dev, frame_buf, IEEE80211_DEAUTH_FRAME_LEN);
3393 mutex_lock(&ifmgd->mtx);
3396 static int ieee80211_probe_auth(struct ieee80211_sub_if_data *sdata)
3398 struct ieee80211_local *local = sdata->local;
3399 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3400 struct ieee80211_mgd_auth_data *auth_data = ifmgd->auth_data;
3403 lockdep_assert_held(&ifmgd->mtx);
3405 if (WARN_ON_ONCE(!auth_data))
3410 if (auth_data->tries > IEEE80211_AUTH_MAX_TRIES) {
3411 sdata_info(sdata, "authentication with %pM timed out\n",
3412 auth_data->bss->bssid);
3415 * Most likely AP is not in the range so remove the
3416 * bss struct for that AP.
3418 cfg80211_unlink_bss(local->hw.wiphy, auth_data->bss);
3423 drv_mgd_prepare_tx(local, sdata);
3425 if (auth_data->bss->proberesp_ies) {
3429 sdata_info(sdata, "send auth to %pM (try %d/%d)\n",
3430 auth_data->bss->bssid, auth_data->tries,
3431 IEEE80211_AUTH_MAX_TRIES);
3433 auth_data->expected_transaction = 2;
3435 if (auth_data->algorithm == WLAN_AUTH_SAE) {
3436 trans = auth_data->sae_trans;
3437 status = auth_data->sae_status;
3438 auth_data->expected_transaction = trans;
3441 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
3442 tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
3443 IEEE80211_TX_INTFL_MLME_CONN_TX;
3445 ieee80211_send_auth(sdata, trans, auth_data->algorithm, status,
3446 auth_data->data, auth_data->data_len,
3447 auth_data->bss->bssid,
3448 auth_data->bss->bssid, NULL, 0, 0,
3453 sdata_info(sdata, "direct probe to %pM (try %d/%i)\n",
3454 auth_data->bss->bssid, auth_data->tries,
3455 IEEE80211_AUTH_MAX_TRIES);
3458 ssidie = ieee80211_bss_get_ie(auth_data->bss, WLAN_EID_SSID);
3464 * Direct probe is sent to broadcast address as some APs
3465 * will not answer to direct packet in unassociated state.
3467 ieee80211_send_probe_req(sdata, NULL, ssidie + 2, ssidie[1],
3468 NULL, 0, (u32) -1, true, 0,
3469 auth_data->bss->channel, false);
3473 if (tx_flags == 0) {
3474 auth_data->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
3475 auth_data->timeout_started = true;
3476 run_again(ifmgd, auth_data->timeout);
3478 auth_data->timeout =
3479 round_jiffies_up(jiffies + IEEE80211_AUTH_TIMEOUT_LONG);
3480 auth_data->timeout_started = true;
3481 run_again(ifmgd, auth_data->timeout);
3487 static int ieee80211_do_assoc(struct ieee80211_sub_if_data *sdata)
3489 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
3490 struct ieee80211_local *local = sdata->local;
3492 lockdep_assert_held(&sdata->u.mgd.mtx);
3494 assoc_data->tries++;
3495 if (assoc_data->tries > IEEE80211_ASSOC_MAX_TRIES) {
3496 sdata_info(sdata, "association with %pM timed out\n",
3497 assoc_data->bss->bssid);
3500 * Most likely AP is not in the range so remove the
3501 * bss struct for that AP.
3503 cfg80211_unlink_bss(local->hw.wiphy, assoc_data->bss);
3508 sdata_info(sdata, "associate with %pM (try %d/%d)\n",
3509 assoc_data->bss->bssid, assoc_data->tries,
3510 IEEE80211_ASSOC_MAX_TRIES);
3511 ieee80211_send_assoc(sdata);
3513 if (!(local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)) {
3514 assoc_data->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
3515 assoc_data->timeout_started = true;
3516 run_again(&sdata->u.mgd, assoc_data->timeout);
3518 assoc_data->timeout =
3519 round_jiffies_up(jiffies +
3520 IEEE80211_ASSOC_TIMEOUT_LONG);
3521 assoc_data->timeout_started = true;
3522 run_again(&sdata->u.mgd, assoc_data->timeout);
3528 void ieee80211_mgd_conn_tx_status(struct ieee80211_sub_if_data *sdata,
3529 __le16 fc, bool acked)
3531 struct ieee80211_local *local = sdata->local;
3533 sdata->u.mgd.status_fc = fc;
3534 sdata->u.mgd.status_acked = acked;
3535 sdata->u.mgd.status_received = true;
3537 ieee80211_queue_work(&local->hw, &sdata->work);
3540 void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
3542 struct ieee80211_local *local = sdata->local;
3543 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3545 mutex_lock(&ifmgd->mtx);
3547 if (ifmgd->status_received) {
3548 __le16 fc = ifmgd->status_fc;
3549 bool status_acked = ifmgd->status_acked;
3551 ifmgd->status_received = false;
3552 if (ifmgd->auth_data &&
3553 (ieee80211_is_probe_req(fc) || ieee80211_is_auth(fc))) {
3555 ifmgd->auth_data->timeout =
3556 jiffies + IEEE80211_AUTH_TIMEOUT_SHORT;
3557 run_again(ifmgd, ifmgd->auth_data->timeout);
3559 ifmgd->auth_data->timeout = jiffies - 1;
3561 ifmgd->auth_data->timeout_started = true;
3562 } else if (ifmgd->assoc_data &&
3563 (ieee80211_is_assoc_req(fc) ||
3564 ieee80211_is_reassoc_req(fc))) {
3566 ifmgd->assoc_data->timeout =
3567 jiffies + IEEE80211_ASSOC_TIMEOUT_SHORT;
3568 run_again(ifmgd, ifmgd->assoc_data->timeout);
3570 ifmgd->assoc_data->timeout = jiffies - 1;
3572 ifmgd->assoc_data->timeout_started = true;
3576 if (ifmgd->auth_data && ifmgd->auth_data->timeout_started &&
3577 time_after(jiffies, ifmgd->auth_data->timeout)) {
3578 if (ifmgd->auth_data->done) {
3580 * ok ... we waited for assoc but userspace didn't,
3581 * so let's just kill the auth data
3583 ieee80211_destroy_auth_data(sdata, false);
3584 } else if (ieee80211_probe_auth(sdata)) {
3587 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
3589 ieee80211_destroy_auth_data(sdata, false);
3591 mutex_unlock(&ifmgd->mtx);
3592 cfg80211_send_auth_timeout(sdata->dev, bssid);
3593 mutex_lock(&ifmgd->mtx);
3595 } else if (ifmgd->auth_data && ifmgd->auth_data->timeout_started)
3596 run_again(ifmgd, ifmgd->auth_data->timeout);
3598 if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started &&
3599 time_after(jiffies, ifmgd->assoc_data->timeout)) {
3600 if ((ifmgd->assoc_data->need_beacon &&
3601 !ifmgd->assoc_data->have_beacon) ||
3602 ieee80211_do_assoc(sdata)) {
3605 memcpy(bssid, ifmgd->assoc_data->bss->bssid, ETH_ALEN);
3607 ieee80211_destroy_assoc_data(sdata, false);
3609 mutex_unlock(&ifmgd->mtx);
3610 cfg80211_send_assoc_timeout(sdata->dev, bssid);
3611 mutex_lock(&ifmgd->mtx);
3613 } else if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started)
3614 run_again(ifmgd, ifmgd->assoc_data->timeout);
3616 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
3617 IEEE80211_STA_CONNECTION_POLL) &&
3618 ifmgd->associated) {
3622 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
3624 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
3625 max_tries = max_nullfunc_tries;
3627 max_tries = max_probe_tries;
3629 /* ACK received for nullfunc probing frame */
3630 if (!ifmgd->probe_send_count)
3631 ieee80211_reset_ap_probe(sdata);
3632 else if (ifmgd->nullfunc_failed) {
3633 if (ifmgd->probe_send_count < max_tries) {
3635 "No ack for nullfunc frame to AP %pM, try %d/%i\n",
3636 bssid, ifmgd->probe_send_count,
3638 ieee80211_mgd_probe_ap_send(sdata);
3641 "No ack for nullfunc frame to AP %pM, disconnecting.\n",
3643 ieee80211_sta_connection_lost(sdata, bssid,
3644 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
3647 } else if (time_is_after_jiffies(ifmgd->probe_timeout))
3648 run_again(ifmgd, ifmgd->probe_timeout);
3649 else if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
3651 "Failed to send nullfunc to AP %pM after %dms, disconnecting\n",
3652 bssid, probe_wait_ms);
3653 ieee80211_sta_connection_lost(sdata, bssid,
3654 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
3655 } else if (ifmgd->probe_send_count < max_tries) {
3657 "No probe response from AP %pM after %dms, try %d/%i\n",
3658 bssid, probe_wait_ms,
3659 ifmgd->probe_send_count, max_tries);
3660 ieee80211_mgd_probe_ap_send(sdata);
3663 * We actually lost the connection ... or did we?
3666 wiphy_debug(local->hw.wiphy,
3667 "%s: No probe response from AP %pM"
3668 " after %dms, disconnecting.\n",
3670 bssid, probe_wait_ms);
3672 ieee80211_sta_connection_lost(sdata, bssid,
3673 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
3677 mutex_unlock(&ifmgd->mtx);
3680 static void ieee80211_sta_bcn_mon_timer(unsigned long data)
3682 struct ieee80211_sub_if_data *sdata =
3683 (struct ieee80211_sub_if_data *) data;
3684 struct ieee80211_local *local = sdata->local;
3686 if (local->quiescing)
3689 sdata->u.mgd.connection_loss = false;
3690 ieee80211_queue_work(&sdata->local->hw,
3691 &sdata->u.mgd.beacon_connection_loss_work);
3694 static void ieee80211_sta_conn_mon_timer(unsigned long data)
3696 struct ieee80211_sub_if_data *sdata =
3697 (struct ieee80211_sub_if_data *) data;
3698 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3699 struct ieee80211_local *local = sdata->local;
3701 if (local->quiescing)
3704 ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
3707 static void ieee80211_sta_monitor_work(struct work_struct *work)
3709 struct ieee80211_sub_if_data *sdata =
3710 container_of(work, struct ieee80211_sub_if_data,
3711 u.mgd.monitor_work);
3713 ieee80211_mgd_probe_ap(sdata, false);
3716 static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
3720 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
3721 __ieee80211_stop_poll(sdata);
3723 /* let's probe the connection once */
3724 flags = sdata->local->hw.flags;
3725 if (!(flags & IEEE80211_HW_CONNECTION_MONITOR))
3726 ieee80211_queue_work(&sdata->local->hw,
3727 &sdata->u.mgd.monitor_work);
3728 /* and do all the other regular work too */
3729 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
3734 void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
3736 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3738 mutex_lock(&ifmgd->mtx);
3739 if (!ifmgd->associated) {
3740 mutex_unlock(&ifmgd->mtx);
3744 if (sdata->flags & IEEE80211_SDATA_DISCONNECT_RESUME) {
3745 sdata->flags &= ~IEEE80211_SDATA_DISCONNECT_RESUME;
3746 mlme_dbg(sdata, "driver requested disconnect after resume\n");
3747 ieee80211_sta_connection_lost(sdata,
3748 ifmgd->associated->bssid,
3749 WLAN_REASON_UNSPECIFIED,
3751 mutex_unlock(&ifmgd->mtx);
3754 mutex_unlock(&ifmgd->mtx);
3758 /* interface setup */
3759 void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
3761 struct ieee80211_if_managed *ifmgd;
3763 ifmgd = &sdata->u.mgd;
3764 INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
3765 INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
3766 INIT_WORK(&ifmgd->beacon_connection_loss_work,
3767 ieee80211_beacon_connection_loss_work);
3768 INIT_WORK(&ifmgd->csa_connection_drop_work,
3769 ieee80211_csa_connection_drop_work);
3770 INIT_WORK(&ifmgd->request_smps_work, ieee80211_request_smps_work);
3771 setup_timer(&ifmgd->timer, ieee80211_sta_timer,
3772 (unsigned long) sdata);
3773 setup_timer(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer,
3774 (unsigned long) sdata);
3775 setup_timer(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer,
3776 (unsigned long) sdata);
3777 setup_timer(&ifmgd->chswitch_timer, ieee80211_chswitch_timer,
3778 (unsigned long) sdata);
3781 ifmgd->powersave = sdata->wdev.ps;
3782 ifmgd->uapsd_queues = sdata->local->hw.uapsd_queues;
3783 ifmgd->uapsd_max_sp_len = sdata->local->hw.uapsd_max_sp_len;
3784 ifmgd->p2p_noa_index = -1;
3786 mutex_init(&ifmgd->mtx);
3788 if (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS)
3789 ifmgd->req_smps = IEEE80211_SMPS_AUTOMATIC;
3791 ifmgd->req_smps = IEEE80211_SMPS_OFF;
3794 /* scan finished notification */
3795 void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
3797 struct ieee80211_sub_if_data *sdata;
3799 /* Restart STA timers */
3801 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
3802 if (ieee80211_sdata_running(sdata))
3803 ieee80211_restart_sta_timer(sdata);
3808 int ieee80211_max_network_latency(struct notifier_block *nb,
3809 unsigned long data, void *dummy)
3811 s32 latency_usec = (s32) data;
3812 struct ieee80211_local *local =
3813 container_of(nb, struct ieee80211_local,
3814 network_latency_notifier);
3816 mutex_lock(&local->iflist_mtx);
3817 ieee80211_recalc_ps(local, latency_usec);
3818 mutex_unlock(&local->iflist_mtx);
3823 static u8 ieee80211_ht_vht_rx_chains(struct ieee80211_sub_if_data *sdata,
3824 struct cfg80211_bss *cbss)
3826 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3827 const u8 *ht_cap_ie, *vht_cap_ie;
3828 const struct ieee80211_ht_cap *ht_cap;
3829 const struct ieee80211_vht_cap *vht_cap;
3832 if (ifmgd->flags & IEEE80211_STA_DISABLE_HT)
3835 ht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
3836 if (ht_cap_ie && ht_cap_ie[1] >= sizeof(*ht_cap)) {
3837 ht_cap = (void *)(ht_cap_ie + 2);
3838 chains = ieee80211_mcs_to_chains(&ht_cap->mcs);
3840 * TODO: use "Tx Maximum Number Spatial Streams Supported" and
3841 * "Tx Unequal Modulation Supported" fields.
3845 if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
3848 vht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
3849 if (vht_cap_ie && vht_cap_ie[1] >= sizeof(*vht_cap)) {
3853 vht_cap = (void *)(vht_cap_ie + 2);
3854 tx_mcs_map = le16_to_cpu(vht_cap->supp_mcs.tx_mcs_map);
3855 for (nss = 8; nss > 0; nss--) {
3856 if (((tx_mcs_map >> (2 * (nss - 1))) & 3) !=
3857 IEEE80211_VHT_MCS_NOT_SUPPORTED)
3860 /* TODO: use "Tx Highest Supported Long GI Data Rate" field? */
3861 chains = max(chains, nss);
3867 static int ieee80211_prep_channel(struct ieee80211_sub_if_data *sdata,
3868 struct cfg80211_bss *cbss)
3870 struct ieee80211_local *local = sdata->local;
3871 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3872 const struct ieee80211_ht_operation *ht_oper = NULL;
3873 const struct ieee80211_vht_operation *vht_oper = NULL;
3874 struct ieee80211_supported_band *sband;
3875 struct cfg80211_chan_def chandef;
3878 sband = local->hw.wiphy->bands[cbss->channel->band];
3880 ifmgd->flags &= ~(IEEE80211_STA_DISABLE_40MHZ |
3881 IEEE80211_STA_DISABLE_80P80MHZ |
3882 IEEE80211_STA_DISABLE_160MHZ);
3886 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
3887 sband->ht_cap.ht_supported) {
3888 const u8 *ht_oper_ie, *ht_cap;
3890 ht_oper_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_OPERATION);
3891 if (ht_oper_ie && ht_oper_ie[1] >= sizeof(*ht_oper))
3892 ht_oper = (void *)(ht_oper_ie + 2);
3894 ht_cap = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
3895 if (!ht_cap || ht_cap[1] < sizeof(struct ieee80211_ht_cap)) {
3896 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
3901 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
3902 sband->vht_cap.vht_supported) {
3903 const u8 *vht_oper_ie, *vht_cap;
3905 vht_oper_ie = ieee80211_bss_get_ie(cbss,
3906 WLAN_EID_VHT_OPERATION);
3907 if (vht_oper_ie && vht_oper_ie[1] >= sizeof(*vht_oper))
3908 vht_oper = (void *)(vht_oper_ie + 2);
3909 if (vht_oper && !ht_oper) {
3912 "AP advertised VHT without HT, disabling both\n");
3913 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
3914 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
3917 vht_cap = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
3918 if (!vht_cap || vht_cap[1] < sizeof(struct ieee80211_vht_cap)) {
3919 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
3924 ifmgd->flags |= ieee80211_determine_chantype(sdata, sband,
3929 sdata->needed_rx_chains = min(ieee80211_ht_vht_rx_chains(sdata, cbss),
3934 /* will change later if needed */
3935 sdata->smps_mode = IEEE80211_SMPS_OFF;
3938 * If this fails (possibly due to channel context sharing
3939 * on incompatible channels, e.g. 80+80 and 160 sharing the
3940 * same control channel) try to use a smaller bandwidth.
3942 ret = ieee80211_vif_use_channel(sdata, &chandef,
3943 IEEE80211_CHANCTX_SHARED);
3944 while (ret && chandef.width != NL80211_CHAN_WIDTH_20_NOHT) {
3945 ifmgd->flags |= chandef_downgrade(&chandef);
3946 ret = ieee80211_vif_use_channel(sdata, &chandef,
3947 IEEE80211_CHANCTX_SHARED);
3952 static int ieee80211_prep_connection(struct ieee80211_sub_if_data *sdata,
3953 struct cfg80211_bss *cbss, bool assoc)
3955 struct ieee80211_local *local = sdata->local;
3956 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3957 struct ieee80211_bss *bss = (void *)cbss->priv;
3958 struct sta_info *new_sta = NULL;
3959 bool have_sta = false;
3962 if (WARN_ON(!ifmgd->auth_data && !ifmgd->assoc_data))
3967 have_sta = sta_info_get(sdata, cbss->bssid);
3972 new_sta = sta_info_alloc(sdata, cbss->bssid, GFP_KERNEL);
3978 u32 rates = 0, basic_rates = 0;
3979 bool have_higher_than_11mbit;
3980 int min_rate = INT_MAX, min_rate_index = -1;
3981 struct ieee80211_supported_band *sband;
3982 const struct cfg80211_bss_ies *ies;
3984 sband = local->hw.wiphy->bands[cbss->channel->band];
3986 err = ieee80211_prep_channel(sdata, cbss);
3988 sta_info_free(local, new_sta);
3992 ieee80211_get_rates(sband, bss->supp_rates,
3993 bss->supp_rates_len,
3994 &rates, &basic_rates,
3995 &have_higher_than_11mbit,
3996 &min_rate, &min_rate_index);
3999 * This used to be a workaround for basic rates missing
4000 * in the association response frame. Now that we no
4001 * longer use the basic rates from there, it probably
4002 * doesn't happen any more, but keep the workaround so
4003 * in case some *other* APs are buggy in different ways
4004 * we can connect -- with a warning.
4006 if (!basic_rates && min_rate_index >= 0) {
4008 "No basic rates, using min rate instead\n");
4009 basic_rates = BIT(min_rate_index);
4012 new_sta->sta.supp_rates[cbss->channel->band] = rates;
4013 sdata->vif.bss_conf.basic_rates = basic_rates;
4015 /* cf. IEEE 802.11 9.2.12 */
4016 if (cbss->channel->band == IEEE80211_BAND_2GHZ &&
4017 have_higher_than_11mbit)
4018 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
4020 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
4022 memcpy(ifmgd->bssid, cbss->bssid, ETH_ALEN);
4024 /* set timing information */
4025 sdata->vif.bss_conf.beacon_int = cbss->beacon_interval;
4027 ies = rcu_dereference(cbss->beacon_ies);
4031 sdata->vif.bss_conf.sync_tsf = ies->tsf;
4032 sdata->vif.bss_conf.sync_device_ts =
4033 bss->device_ts_beacon;
4034 tim_ie = cfg80211_find_ie(WLAN_EID_TIM,
4035 ies->data, ies->len);
4036 if (tim_ie && tim_ie[1] >= 2)
4037 sdata->vif.bss_conf.sync_dtim_count = tim_ie[2];
4039 sdata->vif.bss_conf.sync_dtim_count = 0;
4040 } else if (!(local->hw.flags &
4041 IEEE80211_HW_TIMING_BEACON_ONLY)) {
4042 ies = rcu_dereference(cbss->proberesp_ies);
4043 /* must be non-NULL since beacon IEs were NULL */
4044 sdata->vif.bss_conf.sync_tsf = ies->tsf;
4045 sdata->vif.bss_conf.sync_device_ts =
4046 bss->device_ts_presp;
4047 sdata->vif.bss_conf.sync_dtim_count = 0;
4049 sdata->vif.bss_conf.sync_tsf = 0;
4050 sdata->vif.bss_conf.sync_device_ts = 0;
4051 sdata->vif.bss_conf.sync_dtim_count = 0;
4055 /* tell driver about BSSID, basic rates and timing */
4056 ieee80211_bss_info_change_notify(sdata,
4057 BSS_CHANGED_BSSID | BSS_CHANGED_BASIC_RATES |
4058 BSS_CHANGED_BEACON_INT);
4061 sta_info_pre_move_state(new_sta, IEEE80211_STA_AUTH);
4063 err = sta_info_insert(new_sta);
4067 "failed to insert STA entry for the AP (error %d)\n",
4072 WARN_ON_ONCE(!ether_addr_equal(ifmgd->bssid, cbss->bssid));
4078 int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
4079 struct cfg80211_auth_request *req)
4081 struct ieee80211_local *local = sdata->local;
4082 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4083 struct ieee80211_mgd_auth_data *auth_data;
4087 /* prepare auth data structure */
4089 switch (req->auth_type) {
4090 case NL80211_AUTHTYPE_OPEN_SYSTEM:
4091 auth_alg = WLAN_AUTH_OPEN;
4093 case NL80211_AUTHTYPE_SHARED_KEY:
4094 if (IS_ERR(local->wep_tx_tfm))
4096 auth_alg = WLAN_AUTH_SHARED_KEY;
4098 case NL80211_AUTHTYPE_FT:
4099 auth_alg = WLAN_AUTH_FT;
4101 case NL80211_AUTHTYPE_NETWORK_EAP:
4102 auth_alg = WLAN_AUTH_LEAP;
4104 case NL80211_AUTHTYPE_SAE:
4105 auth_alg = WLAN_AUTH_SAE;
4111 auth_data = kzalloc(sizeof(*auth_data) + req->sae_data_len +
4112 req->ie_len, GFP_KERNEL);
4116 auth_data->bss = req->bss;
4118 if (req->sae_data_len >= 4) {
4119 __le16 *pos = (__le16 *) req->sae_data;
4120 auth_data->sae_trans = le16_to_cpu(pos[0]);
4121 auth_data->sae_status = le16_to_cpu(pos[1]);
4122 memcpy(auth_data->data, req->sae_data + 4,
4123 req->sae_data_len - 4);
4124 auth_data->data_len += req->sae_data_len - 4;
4127 if (req->ie && req->ie_len) {
4128 memcpy(&auth_data->data[auth_data->data_len],
4129 req->ie, req->ie_len);
4130 auth_data->data_len += req->ie_len;
4133 if (req->key && req->key_len) {
4134 auth_data->key_len = req->key_len;
4135 auth_data->key_idx = req->key_idx;
4136 memcpy(auth_data->key, req->key, req->key_len);
4139 auth_data->algorithm = auth_alg;
4141 /* try to authenticate/probe */
4143 mutex_lock(&ifmgd->mtx);
4145 if ((ifmgd->auth_data && !ifmgd->auth_data->done) ||
4146 ifmgd->assoc_data) {
4151 if (ifmgd->auth_data)
4152 ieee80211_destroy_auth_data(sdata, false);
4154 /* prep auth_data so we don't go into idle on disassoc */
4155 ifmgd->auth_data = auth_data;
4157 if (ifmgd->associated) {
4158 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
4160 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
4161 WLAN_REASON_UNSPECIFIED,
4164 __cfg80211_send_deauth(sdata->dev, frame_buf,
4168 sdata_info(sdata, "authenticate with %pM\n", req->bss->bssid);
4170 err = ieee80211_prep_connection(sdata, req->bss, false);
4174 err = ieee80211_probe_auth(sdata);
4176 sta_info_destroy_addr(sdata, req->bss->bssid);
4180 /* hold our own reference */
4181 cfg80211_ref_bss(local->hw.wiphy, auth_data->bss);
4186 memset(ifmgd->bssid, 0, ETH_ALEN);
4187 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
4188 ifmgd->auth_data = NULL;
4192 mutex_unlock(&ifmgd->mtx);
4197 int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
4198 struct cfg80211_assoc_request *req)
4200 struct ieee80211_local *local = sdata->local;
4201 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4202 struct ieee80211_bss *bss = (void *)req->bss->priv;
4203 struct ieee80211_mgd_assoc_data *assoc_data;
4204 const struct cfg80211_bss_ies *beacon_ies;
4205 struct ieee80211_supported_band *sband;
4206 const u8 *ssidie, *ht_ie, *vht_ie;
4209 assoc_data = kzalloc(sizeof(*assoc_data) + req->ie_len, GFP_KERNEL);
4214 ssidie = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
4220 memcpy(assoc_data->ssid, ssidie + 2, ssidie[1]);
4221 assoc_data->ssid_len = ssidie[1];
4224 mutex_lock(&ifmgd->mtx);
4226 if (ifmgd->associated) {
4227 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
4229 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
4230 WLAN_REASON_UNSPECIFIED,
4233 __cfg80211_send_deauth(sdata->dev, frame_buf,
4237 if (ifmgd->auth_data && !ifmgd->auth_data->done) {
4242 if (ifmgd->assoc_data) {
4247 if (ifmgd->auth_data) {
4250 /* keep sta info, bssid if matching */
4251 match = ether_addr_equal(ifmgd->bssid, req->bss->bssid);
4252 ieee80211_destroy_auth_data(sdata, match);
4255 /* prepare assoc data */
4257 ifmgd->beacon_crc_valid = false;
4260 * IEEE802.11n does not allow TKIP/WEP as pairwise ciphers in HT mode.
4261 * We still associate in non-HT mode (11a/b/g) if any one of these
4262 * ciphers is configured as pairwise.
4263 * We can set this to true for non-11n hardware, that'll be checked
4264 * separately along with the peer capabilities.
4266 for (i = 0; i < req->crypto.n_ciphers_pairwise; i++) {
4267 if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
4268 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
4269 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104) {
4270 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
4271 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
4272 netdev_info(sdata->dev,
4273 "disabling HT/VHT due to WEP/TKIP use\n");
4277 if (req->flags & ASSOC_REQ_DISABLE_HT) {
4278 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
4279 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
4282 if (req->flags & ASSOC_REQ_DISABLE_VHT)
4283 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
4285 /* Also disable HT if we don't support it or the AP doesn't use WMM */
4286 sband = local->hw.wiphy->bands[req->bss->channel->band];
4287 if (!sband->ht_cap.ht_supported ||
4288 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used) {
4289 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
4291 netdev_info(sdata->dev,
4292 "disabling HT as WMM/QoS is not supported by the AP\n");
4295 /* disable VHT if we don't support it or the AP doesn't use WMM */
4296 if (!sband->vht_cap.vht_supported ||
4297 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used) {
4298 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
4300 netdev_info(sdata->dev,
4301 "disabling VHT as WMM/QoS is not supported by the AP\n");
4304 memcpy(&ifmgd->ht_capa, &req->ht_capa, sizeof(ifmgd->ht_capa));
4305 memcpy(&ifmgd->ht_capa_mask, &req->ht_capa_mask,
4306 sizeof(ifmgd->ht_capa_mask));
4308 memcpy(&ifmgd->vht_capa, &req->vht_capa, sizeof(ifmgd->vht_capa));
4309 memcpy(&ifmgd->vht_capa_mask, &req->vht_capa_mask,
4310 sizeof(ifmgd->vht_capa_mask));
4312 if (req->ie && req->ie_len) {
4313 memcpy(assoc_data->ie, req->ie, req->ie_len);
4314 assoc_data->ie_len = req->ie_len;
4317 assoc_data->bss = req->bss;
4319 if (ifmgd->req_smps == IEEE80211_SMPS_AUTOMATIC) {
4320 if (ifmgd->powersave)
4321 sdata->smps_mode = IEEE80211_SMPS_DYNAMIC;
4323 sdata->smps_mode = IEEE80211_SMPS_OFF;
4325 sdata->smps_mode = ifmgd->req_smps;
4327 assoc_data->capability = req->bss->capability;
4328 assoc_data->wmm = bss->wmm_used &&
4329 (local->hw.queues >= IEEE80211_NUM_ACS);
4330 assoc_data->supp_rates = bss->supp_rates;
4331 assoc_data->supp_rates_len = bss->supp_rates_len;
4334 ht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_HT_OPERATION);
4335 if (ht_ie && ht_ie[1] >= sizeof(struct ieee80211_ht_operation))
4336 assoc_data->ap_ht_param =
4337 ((struct ieee80211_ht_operation *)(ht_ie + 2))->ht_param;
4339 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
4340 vht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_VHT_CAPABILITY);
4341 if (vht_ie && vht_ie[1] >= sizeof(struct ieee80211_vht_cap))
4342 memcpy(&assoc_data->ap_vht_cap, vht_ie + 2,
4343 sizeof(struct ieee80211_vht_cap));
4345 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
4348 if (bss->wmm_used && bss->uapsd_supported &&
4349 (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_UAPSD) &&
4350 sdata->wmm_acm != 0xff) {
4351 assoc_data->uapsd = true;
4352 ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED;
4354 assoc_data->uapsd = false;
4355 ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED;
4358 if (req->prev_bssid)
4359 memcpy(assoc_data->prev_bssid, req->prev_bssid, ETH_ALEN);
4362 ifmgd->mfp = IEEE80211_MFP_REQUIRED;
4363 ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
4365 ifmgd->mfp = IEEE80211_MFP_DISABLED;
4366 ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
4369 if (req->crypto.control_port)
4370 ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
4372 ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
4374 sdata->control_port_protocol = req->crypto.control_port_ethertype;
4375 sdata->control_port_no_encrypt = req->crypto.control_port_no_encrypt;
4377 /* kick off associate process */
4379 ifmgd->assoc_data = assoc_data;
4380 ifmgd->dtim_period = 0;
4382 err = ieee80211_prep_connection(sdata, req->bss, true);
4387 beacon_ies = rcu_dereference(req->bss->beacon_ies);
4389 if (sdata->local->hw.flags & IEEE80211_HW_NEED_DTIM_BEFORE_ASSOC &&
4392 * Wait up to one beacon interval ...
4393 * should this be more if we miss one?
4395 sdata_info(sdata, "waiting for beacon from %pM\n",
4397 assoc_data->timeout = TU_TO_EXP_TIME(req->bss->beacon_interval);
4398 assoc_data->timeout_started = true;
4399 assoc_data->need_beacon = true;
4400 } else if (beacon_ies) {
4401 const u8 *tim_ie = cfg80211_find_ie(WLAN_EID_TIM,
4406 if (tim_ie && tim_ie[1] >= sizeof(struct ieee80211_tim_ie)) {
4407 const struct ieee80211_tim_ie *tim;
4408 tim = (void *)(tim_ie + 2);
4409 ifmgd->dtim_period = tim->dtim_period;
4410 dtim_count = tim->dtim_count;
4412 assoc_data->have_beacon = true;
4413 assoc_data->timeout = jiffies;
4414 assoc_data->timeout_started = true;
4416 if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
4417 sdata->vif.bss_conf.sync_tsf = beacon_ies->tsf;
4418 sdata->vif.bss_conf.sync_device_ts =
4419 bss->device_ts_beacon;
4420 sdata->vif.bss_conf.sync_dtim_count = dtim_count;
4423 assoc_data->timeout = jiffies;
4424 assoc_data->timeout_started = true;
4428 run_again(ifmgd, assoc_data->timeout);
4430 if (bss->corrupt_data) {
4431 char *corrupt_type = "data";
4432 if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_BEACON) {
4433 if (bss->corrupt_data &
4434 IEEE80211_BSS_CORRUPT_PROBE_RESP)
4435 corrupt_type = "beacon and probe response";
4437 corrupt_type = "beacon";
4438 } else if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_PROBE_RESP)
4439 corrupt_type = "probe response";
4440 sdata_info(sdata, "associating with AP with corrupt %s\n",
4447 memset(ifmgd->bssid, 0, ETH_ALEN);
4448 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
4449 ifmgd->assoc_data = NULL;
4453 mutex_unlock(&ifmgd->mtx);
4458 int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
4459 struct cfg80211_deauth_request *req)
4461 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4462 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
4463 bool tx = !req->local_state_change;
4464 bool report_frame = false;
4466 mutex_lock(&ifmgd->mtx);
4469 "deauthenticating from %pM by local choice (reason=%d)\n",
4470 req->bssid, req->reason_code);
4472 if (ifmgd->auth_data) {
4473 drv_mgd_prepare_tx(sdata->local, sdata);
4474 ieee80211_send_deauth_disassoc(sdata, req->bssid,
4475 IEEE80211_STYPE_DEAUTH,
4476 req->reason_code, tx,
4478 ieee80211_destroy_auth_data(sdata, false);
4479 mutex_unlock(&ifmgd->mtx);
4481 report_frame = true;
4485 if (ifmgd->associated &&
4486 ether_addr_equal(ifmgd->associated->bssid, req->bssid)) {
4487 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
4488 req->reason_code, tx, frame_buf);
4489 report_frame = true;
4491 mutex_unlock(&ifmgd->mtx);
4495 __cfg80211_send_deauth(sdata->dev, frame_buf,
4496 IEEE80211_DEAUTH_FRAME_LEN);
4501 int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
4502 struct cfg80211_disassoc_request *req)
4504 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4506 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
4508 mutex_lock(&ifmgd->mtx);
4511 * cfg80211 should catch this ... but it's racy since
4512 * we can receive a disassoc frame, process it, hand it
4513 * to cfg80211 while that's in a locked section already
4514 * trying to tell us that the user wants to disconnect.
4516 if (ifmgd->associated != req->bss) {
4517 mutex_unlock(&ifmgd->mtx);
4522 "disassociating from %pM by local choice (reason=%d)\n",
4523 req->bss->bssid, req->reason_code);
4525 memcpy(bssid, req->bss->bssid, ETH_ALEN);
4526 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DISASSOC,
4527 req->reason_code, !req->local_state_change,
4529 mutex_unlock(&ifmgd->mtx);
4531 __cfg80211_send_disassoc(sdata->dev, frame_buf,
4532 IEEE80211_DEAUTH_FRAME_LEN);
4537 void ieee80211_mgd_stop(struct ieee80211_sub_if_data *sdata)
4539 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4542 * Make sure some work items will not run after this,
4543 * they will not do anything but might not have been
4544 * cancelled when disconnecting.
4546 cancel_work_sync(&ifmgd->monitor_work);
4547 cancel_work_sync(&ifmgd->beacon_connection_loss_work);
4548 cancel_work_sync(&ifmgd->request_smps_work);
4549 cancel_work_sync(&ifmgd->csa_connection_drop_work);
4550 cancel_work_sync(&ifmgd->chswitch_work);
4552 mutex_lock(&ifmgd->mtx);
4553 if (ifmgd->assoc_data)
4554 ieee80211_destroy_assoc_data(sdata, false);
4555 if (ifmgd->auth_data)
4556 ieee80211_destroy_auth_data(sdata, false);
4557 del_timer_sync(&ifmgd->timer);
4558 mutex_unlock(&ifmgd->mtx);
4561 void ieee80211_cqm_rssi_notify(struct ieee80211_vif *vif,
4562 enum nl80211_cqm_rssi_threshold_event rssi_event,
4565 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4567 trace_api_cqm_rssi_notify(sdata, rssi_event);
4569 cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, gfp);
4571 EXPORT_SYMBOL(ieee80211_cqm_rssi_notify);