Linux 3.12.28
[linux/fpc-iii.git] / net / mac80211 / mlme.c
blob591d990a06e703fca3d0800f49217d6e5d368dbb
1 /*
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"
30 #include "rate.h"
31 #include "led.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
85 * between 1 and 15.
87 #define IEEE80211_SIGNAL_AVE_WEIGHT 3
90 * How many Beacon frames need to have been used in average signal strength
91 * before starting to indicate signal change events.
93 #define IEEE80211_SIGNAL_AVE_MIN_COUNT 4
96 * We can have multiple work items (and connection probing)
97 * scheduling this timer, but we need to take care to only
98 * reschedule it when it should fire _earlier_ than it was
99 * asked for before, or if it's not pending right now. This
100 * function ensures that. Note that it then is required to
101 * run this function for all timeouts after the first one
102 * has happened -- the work that runs from this timer will
103 * do that.
105 static void run_again(struct ieee80211_sub_if_data *sdata,
106 unsigned long timeout)
108 sdata_assert_lock(sdata);
110 if (!timer_pending(&sdata->u.mgd.timer) ||
111 time_before(timeout, sdata->u.mgd.timer.expires))
112 mod_timer(&sdata->u.mgd.timer, timeout);
115 void ieee80211_sta_reset_beacon_monitor(struct ieee80211_sub_if_data *sdata)
117 if (sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)
118 return;
120 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
121 return;
123 mod_timer(&sdata->u.mgd.bcn_mon_timer,
124 round_jiffies_up(jiffies + sdata->u.mgd.beacon_timeout));
127 void ieee80211_sta_reset_conn_monitor(struct ieee80211_sub_if_data *sdata)
129 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
131 if (unlikely(!sdata->u.mgd.associated))
132 return;
134 ifmgd->probe_send_count = 0;
136 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
137 return;
139 mod_timer(&sdata->u.mgd.conn_mon_timer,
140 round_jiffies_up(jiffies + IEEE80211_CONNECTION_IDLE_TIME));
143 static int ecw2cw(int ecw)
145 return (1 << ecw) - 1;
148 static u32 chandef_downgrade(struct cfg80211_chan_def *c)
150 u32 ret;
151 int tmp;
153 switch (c->width) {
154 case NL80211_CHAN_WIDTH_20:
155 c->width = NL80211_CHAN_WIDTH_20_NOHT;
156 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
157 break;
158 case NL80211_CHAN_WIDTH_40:
159 c->width = NL80211_CHAN_WIDTH_20;
160 c->center_freq1 = c->chan->center_freq;
161 ret = IEEE80211_STA_DISABLE_40MHZ |
162 IEEE80211_STA_DISABLE_VHT;
163 break;
164 case NL80211_CHAN_WIDTH_80:
165 tmp = (30 + c->chan->center_freq - c->center_freq1)/20;
166 /* n_P40 */
167 tmp /= 2;
168 /* freq_P40 */
169 c->center_freq1 = c->center_freq1 - 20 + 40 * tmp;
170 c->width = NL80211_CHAN_WIDTH_40;
171 ret = IEEE80211_STA_DISABLE_VHT;
172 break;
173 case NL80211_CHAN_WIDTH_80P80:
174 c->center_freq2 = 0;
175 c->width = NL80211_CHAN_WIDTH_80;
176 ret = IEEE80211_STA_DISABLE_80P80MHZ |
177 IEEE80211_STA_DISABLE_160MHZ;
178 break;
179 case NL80211_CHAN_WIDTH_160:
180 /* n_P20 */
181 tmp = (70 + c->chan->center_freq - c->center_freq1)/20;
182 /* n_P80 */
183 tmp /= 4;
184 c->center_freq1 = c->center_freq1 - 40 + 80 * tmp;
185 c->width = NL80211_CHAN_WIDTH_80;
186 ret = IEEE80211_STA_DISABLE_80P80MHZ |
187 IEEE80211_STA_DISABLE_160MHZ;
188 break;
189 default:
190 case NL80211_CHAN_WIDTH_20_NOHT:
191 WARN_ON_ONCE(1);
192 c->width = NL80211_CHAN_WIDTH_20_NOHT;
193 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
194 break;
195 case NL80211_CHAN_WIDTH_5:
196 case NL80211_CHAN_WIDTH_10:
197 WARN_ON_ONCE(1);
198 /* keep c->width */
199 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
200 break;
203 WARN_ON_ONCE(!cfg80211_chandef_valid(c));
205 return ret;
208 static u32
209 ieee80211_determine_chantype(struct ieee80211_sub_if_data *sdata,
210 struct ieee80211_supported_band *sband,
211 struct ieee80211_channel *channel,
212 const struct ieee80211_ht_operation *ht_oper,
213 const struct ieee80211_vht_operation *vht_oper,
214 struct cfg80211_chan_def *chandef, bool tracking)
216 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
217 struct cfg80211_chan_def vht_chandef;
218 u32 ht_cfreq, ret;
220 chandef->chan = channel;
221 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
222 chandef->center_freq1 = channel->center_freq;
223 chandef->center_freq2 = 0;
225 if (!ht_oper || !sband->ht_cap.ht_supported) {
226 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
227 goto out;
230 chandef->width = NL80211_CHAN_WIDTH_20;
232 ht_cfreq = ieee80211_channel_to_frequency(ht_oper->primary_chan,
233 channel->band);
234 /* check that channel matches the right operating channel */
235 if (!tracking && channel->center_freq != ht_cfreq) {
237 * It's possible that some APs are confused here;
238 * Netgear WNDR3700 sometimes reports 4 higher than
239 * the actual channel in association responses, but
240 * since we look at probe response/beacon data here
241 * it should be OK.
243 sdata_info(sdata,
244 "Wrong control channel: center-freq: %d ht-cfreq: %d ht->primary_chan: %d band: %d - Disabling HT\n",
245 channel->center_freq, ht_cfreq,
246 ht_oper->primary_chan, channel->band);
247 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
248 goto out;
251 /* check 40 MHz support, if we have it */
252 if (sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) {
253 switch (ht_oper->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
254 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
255 chandef->width = NL80211_CHAN_WIDTH_40;
256 chandef->center_freq1 += 10;
257 break;
258 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
259 chandef->width = NL80211_CHAN_WIDTH_40;
260 chandef->center_freq1 -= 10;
261 break;
263 } else {
264 /* 40 MHz (and 80 MHz) must be supported for VHT */
265 ret = IEEE80211_STA_DISABLE_VHT;
266 /* also mark 40 MHz disabled */
267 ret |= IEEE80211_STA_DISABLE_40MHZ;
268 goto out;
271 if (!vht_oper || !sband->vht_cap.vht_supported) {
272 ret = IEEE80211_STA_DISABLE_VHT;
273 goto out;
276 vht_chandef.chan = channel;
277 vht_chandef.center_freq1 =
278 ieee80211_channel_to_frequency(vht_oper->center_freq_seg1_idx,
279 channel->band);
280 vht_chandef.center_freq2 = 0;
282 switch (vht_oper->chan_width) {
283 case IEEE80211_VHT_CHANWIDTH_USE_HT:
284 vht_chandef.width = chandef->width;
285 vht_chandef.center_freq1 = chandef->center_freq1;
286 break;
287 case IEEE80211_VHT_CHANWIDTH_80MHZ:
288 vht_chandef.width = NL80211_CHAN_WIDTH_80;
289 break;
290 case IEEE80211_VHT_CHANWIDTH_160MHZ:
291 vht_chandef.width = NL80211_CHAN_WIDTH_160;
292 break;
293 case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
294 vht_chandef.width = NL80211_CHAN_WIDTH_80P80;
295 vht_chandef.center_freq2 =
296 ieee80211_channel_to_frequency(
297 vht_oper->center_freq_seg2_idx,
298 channel->band);
299 break;
300 default:
301 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
302 sdata_info(sdata,
303 "AP VHT operation IE has invalid channel width (%d), disable VHT\n",
304 vht_oper->chan_width);
305 ret = IEEE80211_STA_DISABLE_VHT;
306 goto out;
309 if (!cfg80211_chandef_valid(&vht_chandef)) {
310 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
311 sdata_info(sdata,
312 "AP VHT information is invalid, disable VHT\n");
313 ret = IEEE80211_STA_DISABLE_VHT;
314 goto out;
317 if (cfg80211_chandef_identical(chandef, &vht_chandef)) {
318 ret = 0;
319 goto out;
322 if (!cfg80211_chandef_compatible(chandef, &vht_chandef)) {
323 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
324 sdata_info(sdata,
325 "AP VHT information doesn't match HT, disable VHT\n");
326 ret = IEEE80211_STA_DISABLE_VHT;
327 goto out;
330 *chandef = vht_chandef;
332 ret = 0;
334 out:
336 * When tracking the current AP, don't do any further checks if the
337 * new chandef is identical to the one we're currently using for the
338 * connection. This keeps us from playing ping-pong with regulatory,
339 * without it the following can happen (for example):
340 * - connect to an AP with 80 MHz, world regdom allows 80 MHz
341 * - AP advertises regdom US
342 * - CRDA loads regdom US with 80 MHz prohibited (old database)
343 * - the code below detects an unsupported channel, downgrades, and
344 * we disconnect from the AP in the caller
345 * - disconnect causes CRDA to reload world regdomain and the game
346 * starts anew.
347 * (see https://bugzilla.kernel.org/show_bug.cgi?id=70881)
349 * It seems possible that there are still scenarios with CSA or real
350 * bandwidth changes where a this could happen, but those cases are
351 * less common and wouldn't completely prevent using the AP.
353 if (tracking &&
354 cfg80211_chandef_identical(chandef, &sdata->vif.bss_conf.chandef))
355 return ret;
357 /* don't print the message below for VHT mismatch if VHT is disabled */
358 if (ret & IEEE80211_STA_DISABLE_VHT)
359 vht_chandef = *chandef;
362 * Ignore the DISABLED flag when we're already connected and only
363 * tracking the APs beacon for bandwidth changes - otherwise we
364 * might get disconnected here if we connect to an AP, update our
365 * regulatory information based on the AP's country IE and the
366 * information we have is wrong/outdated and disables the channel
367 * that we're actually using for the connection to the AP.
369 while (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
370 tracking ? 0 :
371 IEEE80211_CHAN_DISABLED)) {
372 if (WARN_ON(chandef->width == NL80211_CHAN_WIDTH_20_NOHT)) {
373 ret = IEEE80211_STA_DISABLE_HT |
374 IEEE80211_STA_DISABLE_VHT;
375 break;
378 ret |= chandef_downgrade(chandef);
381 if (chandef->width != vht_chandef.width && !tracking)
382 sdata_info(sdata,
383 "capabilities/regulatory prevented using AP HT/VHT configuration, downgraded\n");
385 WARN_ON_ONCE(!cfg80211_chandef_valid(chandef));
386 return ret;
389 static int ieee80211_config_bw(struct ieee80211_sub_if_data *sdata,
390 struct sta_info *sta,
391 const struct ieee80211_ht_operation *ht_oper,
392 const struct ieee80211_vht_operation *vht_oper,
393 const u8 *bssid, u32 *changed)
395 struct ieee80211_local *local = sdata->local;
396 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
397 struct ieee80211_supported_band *sband;
398 struct ieee80211_channel *chan;
399 struct cfg80211_chan_def chandef;
400 u16 ht_opmode;
401 u32 flags;
402 enum ieee80211_sta_rx_bandwidth new_sta_bw;
403 int ret;
405 /* if HT was/is disabled, don't track any bandwidth changes */
406 if (ifmgd->flags & IEEE80211_STA_DISABLE_HT || !ht_oper)
407 return 0;
409 /* don't check VHT if we associated as non-VHT station */
410 if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
411 vht_oper = NULL;
413 if (WARN_ON_ONCE(!sta))
414 return -EINVAL;
416 chan = sdata->vif.bss_conf.chandef.chan;
417 sband = local->hw.wiphy->bands[chan->band];
419 /* calculate new channel (type) based on HT/VHT operation IEs */
420 flags = ieee80211_determine_chantype(sdata, sband, chan, ht_oper,
421 vht_oper, &chandef, true);
424 * Downgrade the new channel if we associated with restricted
425 * capabilities. For example, if we associated as a 20 MHz STA
426 * to a 40 MHz AP (due to regulatory, capabilities or config
427 * reasons) then switching to a 40 MHz channel now won't do us
428 * any good -- we couldn't use it with the AP.
430 if (ifmgd->flags & IEEE80211_STA_DISABLE_80P80MHZ &&
431 chandef.width == NL80211_CHAN_WIDTH_80P80)
432 flags |= chandef_downgrade(&chandef);
433 if (ifmgd->flags & IEEE80211_STA_DISABLE_160MHZ &&
434 chandef.width == NL80211_CHAN_WIDTH_160)
435 flags |= chandef_downgrade(&chandef);
436 if (ifmgd->flags & IEEE80211_STA_DISABLE_40MHZ &&
437 chandef.width > NL80211_CHAN_WIDTH_20)
438 flags |= chandef_downgrade(&chandef);
440 if (cfg80211_chandef_identical(&chandef, &sdata->vif.bss_conf.chandef))
441 return 0;
443 sdata_info(sdata,
444 "AP %pM changed bandwidth, new config is %d MHz, width %d (%d/%d MHz)\n",
445 ifmgd->bssid, chandef.chan->center_freq, chandef.width,
446 chandef.center_freq1, chandef.center_freq2);
448 if (flags != (ifmgd->flags & (IEEE80211_STA_DISABLE_HT |
449 IEEE80211_STA_DISABLE_VHT |
450 IEEE80211_STA_DISABLE_40MHZ |
451 IEEE80211_STA_DISABLE_80P80MHZ |
452 IEEE80211_STA_DISABLE_160MHZ)) ||
453 !cfg80211_chandef_valid(&chandef)) {
454 sdata_info(sdata,
455 "AP %pM changed bandwidth in a way we can't support - disconnect\n",
456 ifmgd->bssid);
457 return -EINVAL;
460 switch (chandef.width) {
461 case NL80211_CHAN_WIDTH_20_NOHT:
462 case NL80211_CHAN_WIDTH_20:
463 new_sta_bw = IEEE80211_STA_RX_BW_20;
464 break;
465 case NL80211_CHAN_WIDTH_40:
466 new_sta_bw = IEEE80211_STA_RX_BW_40;
467 break;
468 case NL80211_CHAN_WIDTH_80:
469 new_sta_bw = IEEE80211_STA_RX_BW_80;
470 break;
471 case NL80211_CHAN_WIDTH_80P80:
472 case NL80211_CHAN_WIDTH_160:
473 new_sta_bw = IEEE80211_STA_RX_BW_160;
474 break;
475 default:
476 return -EINVAL;
479 if (new_sta_bw > sta->cur_max_bandwidth)
480 new_sta_bw = sta->cur_max_bandwidth;
482 if (new_sta_bw < sta->sta.bandwidth) {
483 sta->sta.bandwidth = new_sta_bw;
484 rate_control_rate_update(local, sband, sta,
485 IEEE80211_RC_BW_CHANGED);
488 ret = ieee80211_vif_change_bandwidth(sdata, &chandef, changed);
489 if (ret) {
490 sdata_info(sdata,
491 "AP %pM changed bandwidth to incompatible one - disconnect\n",
492 ifmgd->bssid);
493 return ret;
496 if (new_sta_bw > sta->sta.bandwidth) {
497 sta->sta.bandwidth = new_sta_bw;
498 rate_control_rate_update(local, sband, sta,
499 IEEE80211_RC_BW_CHANGED);
502 ht_opmode = le16_to_cpu(ht_oper->operation_mode);
504 /* if bss configuration changed store the new one */
505 if (sdata->vif.bss_conf.ht_operation_mode != ht_opmode) {
506 *changed |= BSS_CHANGED_HT;
507 sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
510 return 0;
513 /* frame sending functions */
515 static void ieee80211_add_ht_ie(struct ieee80211_sub_if_data *sdata,
516 struct sk_buff *skb, u8 ap_ht_param,
517 struct ieee80211_supported_band *sband,
518 struct ieee80211_channel *channel,
519 enum ieee80211_smps_mode smps)
521 u8 *pos;
522 u32 flags = channel->flags;
523 u16 cap;
524 struct ieee80211_sta_ht_cap ht_cap;
526 BUILD_BUG_ON(sizeof(ht_cap) != sizeof(sband->ht_cap));
528 memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
529 ieee80211_apply_htcap_overrides(sdata, &ht_cap);
531 /* determine capability flags */
532 cap = ht_cap.cap;
534 switch (ap_ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
535 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
536 if (flags & IEEE80211_CHAN_NO_HT40PLUS) {
537 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
538 cap &= ~IEEE80211_HT_CAP_SGI_40;
540 break;
541 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
542 if (flags & IEEE80211_CHAN_NO_HT40MINUS) {
543 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
544 cap &= ~IEEE80211_HT_CAP_SGI_40;
546 break;
550 * If 40 MHz was disabled associate as though we weren't
551 * capable of 40 MHz -- some broken APs will never fall
552 * back to trying to transmit in 20 MHz.
554 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_40MHZ) {
555 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
556 cap &= ~IEEE80211_HT_CAP_SGI_40;
559 /* set SM PS mode properly */
560 cap &= ~IEEE80211_HT_CAP_SM_PS;
561 switch (smps) {
562 case IEEE80211_SMPS_AUTOMATIC:
563 case IEEE80211_SMPS_NUM_MODES:
564 WARN_ON(1);
565 case IEEE80211_SMPS_OFF:
566 cap |= WLAN_HT_CAP_SM_PS_DISABLED <<
567 IEEE80211_HT_CAP_SM_PS_SHIFT;
568 break;
569 case IEEE80211_SMPS_STATIC:
570 cap |= WLAN_HT_CAP_SM_PS_STATIC <<
571 IEEE80211_HT_CAP_SM_PS_SHIFT;
572 break;
573 case IEEE80211_SMPS_DYNAMIC:
574 cap |= WLAN_HT_CAP_SM_PS_DYNAMIC <<
575 IEEE80211_HT_CAP_SM_PS_SHIFT;
576 break;
579 /* reserve and fill IE */
580 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
581 ieee80211_ie_build_ht_cap(pos, &ht_cap, cap);
584 static void ieee80211_add_vht_ie(struct ieee80211_sub_if_data *sdata,
585 struct sk_buff *skb,
586 struct ieee80211_supported_band *sband,
587 struct ieee80211_vht_cap *ap_vht_cap)
589 u8 *pos;
590 u32 cap;
591 struct ieee80211_sta_vht_cap vht_cap;
593 BUILD_BUG_ON(sizeof(vht_cap) != sizeof(sband->vht_cap));
595 memcpy(&vht_cap, &sband->vht_cap, sizeof(vht_cap));
596 ieee80211_apply_vhtcap_overrides(sdata, &vht_cap);
598 /* determine capability flags */
599 cap = vht_cap.cap;
601 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_80P80MHZ) {
602 cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ;
603 cap |= IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
606 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_160MHZ) {
607 cap &= ~IEEE80211_VHT_CAP_SHORT_GI_160;
608 cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
612 * Some APs apparently get confused if our capabilities are better
613 * than theirs, so restrict what we advertise in the assoc request.
615 if (!(ap_vht_cap->vht_cap_info &
616 cpu_to_le32(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)))
617 cap &= ~IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE;
619 /* reserve and fill IE */
620 pos = skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2);
621 ieee80211_ie_build_vht_cap(pos, &vht_cap, cap);
624 static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata)
626 struct ieee80211_local *local = sdata->local;
627 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
628 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
629 struct sk_buff *skb;
630 struct ieee80211_mgmt *mgmt;
631 u8 *pos, qos_info;
632 size_t offset = 0, noffset;
633 int i, count, rates_len, supp_rates_len, shift;
634 u16 capab;
635 struct ieee80211_supported_band *sband;
636 struct ieee80211_chanctx_conf *chanctx_conf;
637 struct ieee80211_channel *chan;
638 u32 rate_flags, rates = 0;
640 sdata_assert_lock(sdata);
642 rcu_read_lock();
643 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
644 if (WARN_ON(!chanctx_conf)) {
645 rcu_read_unlock();
646 return;
648 chan = chanctx_conf->def.chan;
649 rate_flags = ieee80211_chandef_rate_flags(&chanctx_conf->def);
650 rcu_read_unlock();
651 sband = local->hw.wiphy->bands[chan->band];
652 shift = ieee80211_vif_get_shift(&sdata->vif);
654 if (assoc_data->supp_rates_len) {
656 * Get all rates supported by the device and the AP as
657 * some APs don't like getting a superset of their rates
658 * in the association request (e.g. D-Link DAP 1353 in
659 * b-only mode)...
661 rates_len = ieee80211_parse_bitrates(&chanctx_conf->def, sband,
662 assoc_data->supp_rates,
663 assoc_data->supp_rates_len,
664 &rates);
665 } else {
667 * In case AP not provide any supported rates information
668 * before association, we send information element(s) with
669 * all rates that we support.
671 rates_len = 0;
672 for (i = 0; i < sband->n_bitrates; i++) {
673 if ((rate_flags & sband->bitrates[i].flags)
674 != rate_flags)
675 continue;
676 rates |= BIT(i);
677 rates_len++;
681 skb = alloc_skb(local->hw.extra_tx_headroom +
682 sizeof(*mgmt) + /* bit too much but doesn't matter */
683 2 + assoc_data->ssid_len + /* SSID */
684 4 + rates_len + /* (extended) rates */
685 4 + /* power capability */
686 2 + 2 * sband->n_channels + /* supported channels */
687 2 + sizeof(struct ieee80211_ht_cap) + /* HT */
688 2 + sizeof(struct ieee80211_vht_cap) + /* VHT */
689 assoc_data->ie_len + /* extra IEs */
690 9, /* WMM */
691 GFP_KERNEL);
692 if (!skb)
693 return;
695 skb_reserve(skb, local->hw.extra_tx_headroom);
697 capab = WLAN_CAPABILITY_ESS;
699 if (sband->band == IEEE80211_BAND_2GHZ) {
700 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
701 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
702 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
703 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
706 if (assoc_data->capability & WLAN_CAPABILITY_PRIVACY)
707 capab |= WLAN_CAPABILITY_PRIVACY;
709 if ((assoc_data->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
710 (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
711 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
713 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
714 memset(mgmt, 0, 24);
715 memcpy(mgmt->da, assoc_data->bss->bssid, ETH_ALEN);
716 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
717 memcpy(mgmt->bssid, assoc_data->bss->bssid, ETH_ALEN);
719 if (!is_zero_ether_addr(assoc_data->prev_bssid)) {
720 skb_put(skb, 10);
721 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
722 IEEE80211_STYPE_REASSOC_REQ);
723 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
724 mgmt->u.reassoc_req.listen_interval =
725 cpu_to_le16(local->hw.conf.listen_interval);
726 memcpy(mgmt->u.reassoc_req.current_ap, assoc_data->prev_bssid,
727 ETH_ALEN);
728 } else {
729 skb_put(skb, 4);
730 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
731 IEEE80211_STYPE_ASSOC_REQ);
732 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
733 mgmt->u.assoc_req.listen_interval =
734 cpu_to_le16(local->hw.conf.listen_interval);
737 /* SSID */
738 pos = skb_put(skb, 2 + assoc_data->ssid_len);
739 *pos++ = WLAN_EID_SSID;
740 *pos++ = assoc_data->ssid_len;
741 memcpy(pos, assoc_data->ssid, assoc_data->ssid_len);
743 /* add all rates which were marked to be used above */
744 supp_rates_len = rates_len;
745 if (supp_rates_len > 8)
746 supp_rates_len = 8;
748 pos = skb_put(skb, supp_rates_len + 2);
749 *pos++ = WLAN_EID_SUPP_RATES;
750 *pos++ = supp_rates_len;
752 count = 0;
753 for (i = 0; i < sband->n_bitrates; i++) {
754 if (BIT(i) & rates) {
755 int rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
756 5 * (1 << shift));
757 *pos++ = (u8) rate;
758 if (++count == 8)
759 break;
763 if (rates_len > count) {
764 pos = skb_put(skb, rates_len - count + 2);
765 *pos++ = WLAN_EID_EXT_SUPP_RATES;
766 *pos++ = rates_len - count;
768 for (i++; i < sband->n_bitrates; i++) {
769 if (BIT(i) & rates) {
770 int rate;
771 rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
772 5 * (1 << shift));
773 *pos++ = (u8) rate;
778 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
779 /* 1. power capabilities */
780 pos = skb_put(skb, 4);
781 *pos++ = WLAN_EID_PWR_CAPABILITY;
782 *pos++ = 2;
783 *pos++ = 0; /* min tx power */
784 /* max tx power */
785 *pos++ = ieee80211_chandef_max_power(&chanctx_conf->def);
787 /* 2. supported channels */
788 /* TODO: get this in reg domain format */
789 pos = skb_put(skb, 2 * sband->n_channels + 2);
790 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
791 *pos++ = 2 * sband->n_channels;
792 for (i = 0; i < sband->n_channels; i++) {
793 *pos++ = ieee80211_frequency_to_channel(
794 sband->channels[i].center_freq);
795 *pos++ = 1; /* one channel in the subband*/
799 /* if present, add any custom IEs that go before HT */
800 if (assoc_data->ie_len && assoc_data->ie) {
801 static const u8 before_ht[] = {
802 WLAN_EID_SSID,
803 WLAN_EID_SUPP_RATES,
804 WLAN_EID_EXT_SUPP_RATES,
805 WLAN_EID_PWR_CAPABILITY,
806 WLAN_EID_SUPPORTED_CHANNELS,
807 WLAN_EID_RSN,
808 WLAN_EID_QOS_CAPA,
809 WLAN_EID_RRM_ENABLED_CAPABILITIES,
810 WLAN_EID_MOBILITY_DOMAIN,
811 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
813 noffset = ieee80211_ie_split(assoc_data->ie, assoc_data->ie_len,
814 before_ht, ARRAY_SIZE(before_ht),
815 offset);
816 pos = skb_put(skb, noffset - offset);
817 memcpy(pos, assoc_data->ie + offset, noffset - offset);
818 offset = noffset;
821 if (WARN_ON_ONCE((ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
822 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)))
823 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
825 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
826 ieee80211_add_ht_ie(sdata, skb, assoc_data->ap_ht_param,
827 sband, chan, sdata->smps_mode);
829 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
830 ieee80211_add_vht_ie(sdata, skb, sband,
831 &assoc_data->ap_vht_cap);
833 /* if present, add any custom non-vendor IEs that go after HT */
834 if (assoc_data->ie_len && assoc_data->ie) {
835 noffset = ieee80211_ie_split_vendor(assoc_data->ie,
836 assoc_data->ie_len,
837 offset);
838 pos = skb_put(skb, noffset - offset);
839 memcpy(pos, assoc_data->ie + offset, noffset - offset);
840 offset = noffset;
843 if (assoc_data->wmm) {
844 if (assoc_data->uapsd) {
845 qos_info = ifmgd->uapsd_queues;
846 qos_info |= (ifmgd->uapsd_max_sp_len <<
847 IEEE80211_WMM_IE_STA_QOSINFO_SP_SHIFT);
848 } else {
849 qos_info = 0;
852 pos = skb_put(skb, 9);
853 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
854 *pos++ = 7; /* len */
855 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
856 *pos++ = 0x50;
857 *pos++ = 0xf2;
858 *pos++ = 2; /* WME */
859 *pos++ = 0; /* WME info */
860 *pos++ = 1; /* WME ver */
861 *pos++ = qos_info;
864 /* add any remaining custom (i.e. vendor specific here) IEs */
865 if (assoc_data->ie_len && assoc_data->ie) {
866 noffset = assoc_data->ie_len;
867 pos = skb_put(skb, noffset - offset);
868 memcpy(pos, assoc_data->ie + offset, noffset - offset);
871 drv_mgd_prepare_tx(local, sdata);
873 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
874 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
875 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS |
876 IEEE80211_TX_INTFL_MLME_CONN_TX;
877 ieee80211_tx_skb(sdata, skb);
880 void ieee80211_send_pspoll(struct ieee80211_local *local,
881 struct ieee80211_sub_if_data *sdata)
883 struct ieee80211_pspoll *pspoll;
884 struct sk_buff *skb;
886 skb = ieee80211_pspoll_get(&local->hw, &sdata->vif);
887 if (!skb)
888 return;
890 pspoll = (struct ieee80211_pspoll *) skb->data;
891 pspoll->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
893 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
894 ieee80211_tx_skb(sdata, skb);
897 void ieee80211_send_nullfunc(struct ieee80211_local *local,
898 struct ieee80211_sub_if_data *sdata,
899 int powersave)
901 struct sk_buff *skb;
902 struct ieee80211_hdr_3addr *nullfunc;
903 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
905 skb = ieee80211_nullfunc_get(&local->hw, &sdata->vif);
906 if (!skb)
907 return;
909 nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
910 if (powersave)
911 nullfunc->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
913 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
914 IEEE80211_TX_INTFL_OFFCHAN_TX_OK;
916 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
917 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
919 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
920 IEEE80211_STA_CONNECTION_POLL))
921 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_USE_MINRATE;
923 ieee80211_tx_skb(sdata, skb);
926 static void ieee80211_send_4addr_nullfunc(struct ieee80211_local *local,
927 struct ieee80211_sub_if_data *sdata)
929 struct sk_buff *skb;
930 struct ieee80211_hdr *nullfunc;
931 __le16 fc;
933 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
934 return;
936 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 30);
937 if (!skb)
938 return;
940 skb_reserve(skb, local->hw.extra_tx_headroom);
942 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 30);
943 memset(nullfunc, 0, 30);
944 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
945 IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
946 nullfunc->frame_control = fc;
947 memcpy(nullfunc->addr1, sdata->u.mgd.bssid, ETH_ALEN);
948 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
949 memcpy(nullfunc->addr3, sdata->u.mgd.bssid, ETH_ALEN);
950 memcpy(nullfunc->addr4, sdata->vif.addr, ETH_ALEN);
952 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
953 ieee80211_tx_skb(sdata, skb);
956 /* spectrum management related things */
957 static void ieee80211_chswitch_work(struct work_struct *work)
959 struct ieee80211_sub_if_data *sdata =
960 container_of(work, struct ieee80211_sub_if_data, u.mgd.chswitch_work);
961 struct ieee80211_local *local = sdata->local;
962 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
964 if (!ieee80211_sdata_running(sdata))
965 return;
967 sdata_lock(sdata);
968 if (!ifmgd->associated)
969 goto out;
971 local->_oper_chandef = local->csa_chandef;
973 if (!local->ops->channel_switch) {
974 /* call "hw_config" only if doing sw channel switch */
975 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
976 } else {
977 /* update the device channel directly */
978 local->hw.conf.chandef = local->_oper_chandef;
981 /* XXX: shouldn't really modify cfg80211-owned data! */
982 ifmgd->associated->channel = local->_oper_chandef.chan;
984 /* XXX: wait for a beacon first? */
985 ieee80211_wake_queues_by_reason(&local->hw,
986 IEEE80211_MAX_QUEUE_MAP,
987 IEEE80211_QUEUE_STOP_REASON_CSA);
988 out:
989 ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
990 sdata_unlock(sdata);
993 void ieee80211_chswitch_done(struct ieee80211_vif *vif, bool success)
995 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
996 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
998 trace_api_chswitch_done(sdata, success);
999 if (!success) {
1000 sdata_info(sdata,
1001 "driver channel switch failed, disconnecting\n");
1002 ieee80211_queue_work(&sdata->local->hw,
1003 &ifmgd->csa_connection_drop_work);
1004 } else {
1005 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
1008 EXPORT_SYMBOL(ieee80211_chswitch_done);
1010 static void ieee80211_chswitch_timer(unsigned long data)
1012 struct ieee80211_sub_if_data *sdata =
1013 (struct ieee80211_sub_if_data *) data;
1015 ieee80211_queue_work(&sdata->local->hw, &sdata->u.mgd.chswitch_work);
1018 static void
1019 ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
1020 u64 timestamp, struct ieee802_11_elems *elems,
1021 bool beacon)
1023 struct ieee80211_local *local = sdata->local;
1024 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1025 struct cfg80211_bss *cbss = ifmgd->associated;
1026 struct ieee80211_bss *bss;
1027 struct ieee80211_chanctx *chanctx;
1028 enum ieee80211_band new_band;
1029 int new_freq;
1030 u8 new_chan_no;
1031 u8 count;
1032 u8 mode;
1033 struct ieee80211_channel *new_chan;
1034 struct cfg80211_chan_def new_chandef = {};
1035 struct cfg80211_chan_def new_vht_chandef = {};
1036 const struct ieee80211_sec_chan_offs_ie *sec_chan_offs;
1037 const struct ieee80211_wide_bw_chansw_ie *wide_bw_chansw_ie;
1038 const struct ieee80211_ht_operation *ht_oper;
1039 int secondary_channel_offset = -1;
1041 sdata_assert_lock(sdata);
1043 if (!cbss)
1044 return;
1046 if (local->scanning)
1047 return;
1049 /* disregard subsequent announcements if we are already processing */
1050 if (ifmgd->flags & IEEE80211_STA_CSA_RECEIVED)
1051 return;
1053 sec_chan_offs = elems->sec_chan_offs;
1054 wide_bw_chansw_ie = elems->wide_bw_chansw_ie;
1055 ht_oper = elems->ht_operation;
1057 if (ifmgd->flags & (IEEE80211_STA_DISABLE_HT |
1058 IEEE80211_STA_DISABLE_40MHZ)) {
1059 sec_chan_offs = NULL;
1060 wide_bw_chansw_ie = NULL;
1061 /* only used for bandwidth here */
1062 ht_oper = NULL;
1065 if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
1066 wide_bw_chansw_ie = NULL;
1068 if (elems->ext_chansw_ie) {
1069 if (!ieee80211_operating_class_to_band(
1070 elems->ext_chansw_ie->new_operating_class,
1071 &new_band)) {
1072 sdata_info(sdata,
1073 "cannot understand ECSA IE operating class %d, disconnecting\n",
1074 elems->ext_chansw_ie->new_operating_class);
1075 ieee80211_queue_work(&local->hw,
1076 &ifmgd->csa_connection_drop_work);
1078 new_chan_no = elems->ext_chansw_ie->new_ch_num;
1079 count = elems->ext_chansw_ie->count;
1080 mode = elems->ext_chansw_ie->mode;
1081 } else if (elems->ch_switch_ie) {
1082 new_band = cbss->channel->band;
1083 new_chan_no = elems->ch_switch_ie->new_ch_num;
1084 count = elems->ch_switch_ie->count;
1085 mode = elems->ch_switch_ie->mode;
1086 } else {
1087 /* nothing here we understand */
1088 return;
1091 bss = (void *)cbss->priv;
1093 new_freq = ieee80211_channel_to_frequency(new_chan_no, new_band);
1094 new_chan = ieee80211_get_channel(sdata->local->hw.wiphy, new_freq);
1095 if (!new_chan || new_chan->flags & IEEE80211_CHAN_DISABLED) {
1096 sdata_info(sdata,
1097 "AP %pM switches to unsupported channel (%d MHz), disconnecting\n",
1098 ifmgd->associated->bssid, new_freq);
1099 ieee80211_queue_work(&local->hw,
1100 &ifmgd->csa_connection_drop_work);
1101 return;
1104 if (!beacon && sec_chan_offs) {
1105 secondary_channel_offset = sec_chan_offs->sec_chan_offs;
1106 } else if (beacon && ht_oper) {
1107 secondary_channel_offset =
1108 ht_oper->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET;
1109 } else if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
1111 * If it's not a beacon, HT is enabled and the IE not present,
1112 * it's 20 MHz, 802.11-2012 8.5.2.6:
1113 * This element [the Secondary Channel Offset Element] is
1114 * present when switching to a 40 MHz channel. It may be
1115 * present when switching to a 20 MHz channel (in which
1116 * case the secondary channel offset is set to SCN).
1118 secondary_channel_offset = IEEE80211_HT_PARAM_CHA_SEC_NONE;
1121 switch (secondary_channel_offset) {
1122 default:
1123 /* secondary_channel_offset was present but is invalid */
1124 case IEEE80211_HT_PARAM_CHA_SEC_NONE:
1125 cfg80211_chandef_create(&new_chandef, new_chan,
1126 NL80211_CHAN_HT20);
1127 break;
1128 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
1129 cfg80211_chandef_create(&new_chandef, new_chan,
1130 NL80211_CHAN_HT40PLUS);
1131 break;
1132 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
1133 cfg80211_chandef_create(&new_chandef, new_chan,
1134 NL80211_CHAN_HT40MINUS);
1135 break;
1136 case -1:
1137 cfg80211_chandef_create(&new_chandef, new_chan,
1138 NL80211_CHAN_NO_HT);
1139 /* keep width for 5/10 MHz channels */
1140 switch (sdata->vif.bss_conf.chandef.width) {
1141 case NL80211_CHAN_WIDTH_5:
1142 case NL80211_CHAN_WIDTH_10:
1143 new_chandef.width = sdata->vif.bss_conf.chandef.width;
1144 break;
1145 default:
1146 break;
1148 break;
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,
1156 new_band);
1158 switch (wide_bw_chansw_ie->new_channel_width) {
1159 default:
1160 /* hmmm, ignore VHT and use HT if present */
1161 case IEEE80211_VHT_CHANWIDTH_USE_HT:
1162 new_vht_chandef.chan = NULL;
1163 break;
1164 case IEEE80211_VHT_CHANWIDTH_80MHZ:
1165 new_vht_chandef.width = NL80211_CHAN_WIDTH_80;
1166 break;
1167 case IEEE80211_VHT_CHANWIDTH_160MHZ:
1168 new_vht_chandef.width = NL80211_CHAN_WIDTH_160;
1169 break;
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,
1175 new_band);
1176 new_vht_chandef.width = NL80211_CHAN_WIDTH_80P80;
1177 break;
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,
1193 &new_chandef)) {
1194 sdata_info(sdata,
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);
1199 return;
1201 new_chandef = new_vht_chandef;
1204 if (!cfg80211_chandef_usable(local->hw.wiphy, &new_chandef,
1205 IEEE80211_CHAN_DISABLED)) {
1206 sdata_info(sdata,
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);
1213 return;
1216 ifmgd->flags |= IEEE80211_STA_CSA_RECEIVED;
1218 if (local->use_chanctx) {
1219 sdata_info(sdata,
1220 "not handling channel switch with channel contexts\n");
1221 ieee80211_queue_work(&local->hw,
1222 &ifmgd->csa_connection_drop_work);
1223 return;
1226 mutex_lock(&local->chanctx_mtx);
1227 if (WARN_ON(!rcu_access_pointer(sdata->vif.chanctx_conf))) {
1228 mutex_unlock(&local->chanctx_mtx);
1229 return;
1231 chanctx = container_of(rcu_access_pointer(sdata->vif.chanctx_conf),
1232 struct ieee80211_chanctx, conf);
1233 if (chanctx->refcount > 1) {
1234 sdata_info(sdata,
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);
1239 return;
1241 mutex_unlock(&local->chanctx_mtx);
1243 local->csa_chandef = new_chandef;
1245 if (mode)
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,
1254 .block_tx = mode,
1255 .chandef = new_chandef,
1256 .count = count,
1259 drv_channel_switch(local, &ch_switch);
1260 return;
1263 /* channel switch handled in software */
1264 if (count <= 1)
1265 ieee80211_queue_work(&local->hw, &ifmgd->chswitch_work);
1266 else
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;
1281 /* Invalid IE */
1282 if (country_ie_len % 2 || country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
1283 return 0;
1285 triplet = (void *)(country_ie + 3);
1286 country_ie_len -= 3;
1288 switch (channel->band) {
1289 default:
1290 WARN_ON_ONCE(1);
1291 /* fall through */
1292 case IEEE80211_BAND_2GHZ:
1293 case IEEE80211_BAND_60GHZ:
1294 chan_increment = 1;
1295 break;
1296 case IEEE80211_BAND_5GHZ:
1297 chan_increment = 4;
1298 break;
1301 /* find channel */
1302 while (country_ie_len >= 3) {
1303 u8 first_channel = triplet->chans.first_channel;
1305 if (first_channel >= IEEE80211_COUNTRY_EXTENSION_ID)
1306 goto next;
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;
1312 break;
1315 if (have_chan_pwr)
1316 break;
1318 next:
1319 triplet++;
1320 country_ie_len -= 3;
1323 if (!have_chan_pwr)
1324 return 0;
1326 new_ap_level = max_t(int, 0, chan_pwr - *pwr_constr_elem);
1328 if (sdata->ap_power_level == new_ap_level)
1329 return 0;
1331 sdata_info(sdata,
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;
1338 return 0;
1341 /* powersave */
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)
1352 return;
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));
1358 } else {
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))
1364 return;
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)
1392 return false;
1394 if (mgd->broken_ap)
1395 return false;
1397 if (!mgd->associated)
1398 return false;
1400 if (mgd->flags & (IEEE80211_STA_BEACON_POLL |
1401 IEEE80211_STA_CONNECTION_POLL))
1402 return false;
1404 if (!mgd->have_beacon)
1405 return false;
1407 rcu_read_lock();
1408 sta = sta_info_get(sdata, mgd->bssid);
1409 if (sta)
1410 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
1411 rcu_read_unlock();
1413 return authorized;
1416 /* need to hold RTNL or interface lock */
1417 void ieee80211_recalc_ps(struct ieee80211_local *local, s32 latency)
1419 struct ieee80211_sub_if_data *sdata, *found = NULL;
1420 int count = 0;
1421 int timeout;
1423 if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS)) {
1424 local->ps_sdata = NULL;
1425 return;
1428 list_for_each_entry(sdata, &local->interfaces, list) {
1429 if (!ieee80211_sdata_running(sdata))
1430 continue;
1431 if (sdata->vif.type == NL80211_IFTYPE_AP) {
1432 /* If an AP vif is found, then disable PS
1433 * by setting the count to zero thereby setting
1434 * ps_sdata to NULL.
1436 count = 0;
1437 break;
1439 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1440 continue;
1441 found = sdata;
1442 count++;
1445 if (count == 1 && ieee80211_powersave_allowed(found)) {
1446 s32 beaconint_us;
1448 if (latency < 0)
1449 latency = pm_qos_request(PM_QOS_NETWORK_LATENCY);
1451 beaconint_us = ieee80211_tu_to_usec(
1452 found->vif.bss_conf.beacon_int);
1454 timeout = local->dynamic_ps_forced_timeout;
1455 if (timeout < 0) {
1457 * Go to full PSM if the user configures a very low
1458 * latency requirement.
1459 * The 2000 second value is there for compatibility
1460 * until the PM_QOS_NETWORK_LATENCY is configured
1461 * with real values.
1463 if (latency > (1900 * USEC_PER_MSEC) &&
1464 latency != (2000 * USEC_PER_SEC))
1465 timeout = 0;
1466 else
1467 timeout = 100;
1469 local->hw.conf.dynamic_ps_timeout = timeout;
1471 if (beaconint_us > latency) {
1472 local->ps_sdata = NULL;
1473 } else {
1474 int maxslp = 1;
1475 u8 dtimper = found->u.mgd.dtim_period;
1477 /* If the TIM IE is invalid, pretend the value is 1 */
1478 if (!dtimper)
1479 dtimper = 1;
1480 else if (dtimper > 1)
1481 maxslp = min_t(int, dtimper,
1482 latency / beaconint_us);
1484 local->hw.conf.max_sleep_period = maxslp;
1485 local->hw.conf.ps_dtim_period = dtimper;
1486 local->ps_sdata = found;
1488 } else {
1489 local->ps_sdata = NULL;
1492 ieee80211_change_ps(local);
1495 void ieee80211_recalc_ps_vif(struct ieee80211_sub_if_data *sdata)
1497 bool ps_allowed = ieee80211_powersave_allowed(sdata);
1499 if (sdata->vif.bss_conf.ps != ps_allowed) {
1500 sdata->vif.bss_conf.ps = ps_allowed;
1501 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_PS);
1505 void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
1507 struct ieee80211_local *local =
1508 container_of(work, struct ieee80211_local,
1509 dynamic_ps_disable_work);
1511 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1512 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1513 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1516 ieee80211_wake_queues_by_reason(&local->hw,
1517 IEEE80211_MAX_QUEUE_MAP,
1518 IEEE80211_QUEUE_STOP_REASON_PS);
1521 void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
1523 struct ieee80211_local *local =
1524 container_of(work, struct ieee80211_local,
1525 dynamic_ps_enable_work);
1526 struct ieee80211_sub_if_data *sdata = local->ps_sdata;
1527 struct ieee80211_if_managed *ifmgd;
1528 unsigned long flags;
1529 int q;
1531 /* can only happen when PS was just disabled anyway */
1532 if (!sdata)
1533 return;
1535 ifmgd = &sdata->u.mgd;
1537 if (local->hw.conf.flags & IEEE80211_CONF_PS)
1538 return;
1540 if (local->hw.conf.dynamic_ps_timeout > 0) {
1541 /* don't enter PS if TX frames are pending */
1542 if (drv_tx_frames_pending(local)) {
1543 mod_timer(&local->dynamic_ps_timer, jiffies +
1544 msecs_to_jiffies(
1545 local->hw.conf.dynamic_ps_timeout));
1546 return;
1550 * transmission can be stopped by others which leads to
1551 * dynamic_ps_timer expiry. Postpone the ps timer if it
1552 * is not the actual idle state.
1554 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
1555 for (q = 0; q < local->hw.queues; q++) {
1556 if (local->queue_stop_reasons[q]) {
1557 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1558 flags);
1559 mod_timer(&local->dynamic_ps_timer, jiffies +
1560 msecs_to_jiffies(
1561 local->hw.conf.dynamic_ps_timeout));
1562 return;
1565 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
1568 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
1569 !(ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
1570 if (drv_tx_frames_pending(local)) {
1571 mod_timer(&local->dynamic_ps_timer, jiffies +
1572 msecs_to_jiffies(
1573 local->hw.conf.dynamic_ps_timeout));
1574 } else {
1575 ieee80211_send_nullfunc(local, sdata, 1);
1576 /* Flush to get the tx status of nullfunc frame */
1577 ieee80211_flush_queues(local, sdata);
1581 if (!((local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) &&
1582 (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)) ||
1583 (ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
1584 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
1585 local->hw.conf.flags |= IEEE80211_CONF_PS;
1586 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1590 void ieee80211_dynamic_ps_timer(unsigned long data)
1592 struct ieee80211_local *local = (void *) data;
1594 if (local->quiescing || local->suspended)
1595 return;
1597 ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
1600 void ieee80211_dfs_cac_timer_work(struct work_struct *work)
1602 struct delayed_work *delayed_work =
1603 container_of(work, struct delayed_work, work);
1604 struct ieee80211_sub_if_data *sdata =
1605 container_of(delayed_work, struct ieee80211_sub_if_data,
1606 dfs_cac_timer_work);
1608 ieee80211_vif_release_channel(sdata);
1610 cfg80211_cac_event(sdata->dev, NL80211_RADAR_CAC_FINISHED, GFP_KERNEL);
1613 /* MLME */
1614 static bool ieee80211_sta_wmm_params(struct ieee80211_local *local,
1615 struct ieee80211_sub_if_data *sdata,
1616 const u8 *wmm_param, size_t wmm_param_len)
1618 struct ieee80211_tx_queue_params params;
1619 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1620 size_t left;
1621 int count;
1622 const u8 *pos;
1623 u8 uapsd_queues = 0;
1625 if (!local->ops->conf_tx)
1626 return false;
1628 if (local->hw.queues < IEEE80211_NUM_ACS)
1629 return false;
1631 if (!wmm_param)
1632 return false;
1634 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
1635 return false;
1637 if (ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED)
1638 uapsd_queues = ifmgd->uapsd_queues;
1640 count = wmm_param[6] & 0x0f;
1641 if (count == ifmgd->wmm_last_param_set)
1642 return false;
1643 ifmgd->wmm_last_param_set = count;
1645 pos = wmm_param + 8;
1646 left = wmm_param_len - 8;
1648 memset(&params, 0, sizeof(params));
1650 sdata->wmm_acm = 0;
1651 for (; left >= 4; left -= 4, pos += 4) {
1652 int aci = (pos[0] >> 5) & 0x03;
1653 int acm = (pos[0] >> 4) & 0x01;
1654 bool uapsd = false;
1655 int queue;
1657 switch (aci) {
1658 case 1: /* AC_BK */
1659 queue = 3;
1660 if (acm)
1661 sdata->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
1662 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
1663 uapsd = true;
1664 break;
1665 case 2: /* AC_VI */
1666 queue = 1;
1667 if (acm)
1668 sdata->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
1669 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
1670 uapsd = true;
1671 break;
1672 case 3: /* AC_VO */
1673 queue = 0;
1674 if (acm)
1675 sdata->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
1676 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
1677 uapsd = true;
1678 break;
1679 case 0: /* AC_BE */
1680 default:
1681 queue = 2;
1682 if (acm)
1683 sdata->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
1684 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
1685 uapsd = true;
1686 break;
1689 params.aifs = pos[0] & 0x0f;
1690 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
1691 params.cw_min = ecw2cw(pos[1] & 0x0f);
1692 params.txop = get_unaligned_le16(pos + 2);
1693 params.acm = acm;
1694 params.uapsd = uapsd;
1696 mlme_dbg(sdata,
1697 "WMM queue=%d aci=%d acm=%d aifs=%d cWmin=%d cWmax=%d txop=%d uapsd=%d\n",
1698 queue, aci, acm,
1699 params.aifs, params.cw_min, params.cw_max,
1700 params.txop, params.uapsd);
1701 sdata->tx_conf[queue] = params;
1702 if (drv_conf_tx(local, sdata, queue, &params))
1703 sdata_err(sdata,
1704 "failed to set TX queue parameters for queue %d\n",
1705 queue);
1708 /* enable WMM or activate new settings */
1709 sdata->vif.bss_conf.qos = true;
1710 return true;
1713 static void __ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
1715 lockdep_assert_held(&sdata->local->mtx);
1717 sdata->u.mgd.flags &= ~(IEEE80211_STA_CONNECTION_POLL |
1718 IEEE80211_STA_BEACON_POLL);
1719 ieee80211_run_deferred_scan(sdata->local);
1722 static void ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
1724 mutex_lock(&sdata->local->mtx);
1725 __ieee80211_stop_poll(sdata);
1726 mutex_unlock(&sdata->local->mtx);
1729 static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
1730 u16 capab, bool erp_valid, u8 erp)
1732 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
1733 u32 changed = 0;
1734 bool use_protection;
1735 bool use_short_preamble;
1736 bool use_short_slot;
1738 if (erp_valid) {
1739 use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
1740 use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
1741 } else {
1742 use_protection = false;
1743 use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
1746 use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
1747 if (ieee80211_get_sdata_band(sdata) == IEEE80211_BAND_5GHZ)
1748 use_short_slot = true;
1750 if (use_protection != bss_conf->use_cts_prot) {
1751 bss_conf->use_cts_prot = use_protection;
1752 changed |= BSS_CHANGED_ERP_CTS_PROT;
1755 if (use_short_preamble != bss_conf->use_short_preamble) {
1756 bss_conf->use_short_preamble = use_short_preamble;
1757 changed |= BSS_CHANGED_ERP_PREAMBLE;
1760 if (use_short_slot != bss_conf->use_short_slot) {
1761 bss_conf->use_short_slot = use_short_slot;
1762 changed |= BSS_CHANGED_ERP_SLOT;
1765 return changed;
1768 static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
1769 struct cfg80211_bss *cbss,
1770 u32 bss_info_changed)
1772 struct ieee80211_bss *bss = (void *)cbss->priv;
1773 struct ieee80211_local *local = sdata->local;
1774 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
1776 bss_info_changed |= BSS_CHANGED_ASSOC;
1777 bss_info_changed |= ieee80211_handle_bss_capability(sdata,
1778 bss_conf->assoc_capability, bss->has_erp_value, bss->erp_value);
1780 sdata->u.mgd.beacon_timeout = usecs_to_jiffies(ieee80211_tu_to_usec(
1781 beacon_loss_count * bss_conf->beacon_int));
1783 sdata->u.mgd.associated = cbss;
1784 memcpy(sdata->u.mgd.bssid, cbss->bssid, ETH_ALEN);
1786 sdata->u.mgd.flags |= IEEE80211_STA_RESET_SIGNAL_AVE;
1788 if (sdata->vif.p2p) {
1789 const struct cfg80211_bss_ies *ies;
1791 rcu_read_lock();
1792 ies = rcu_dereference(cbss->ies);
1793 if (ies) {
1794 int ret;
1796 ret = cfg80211_get_p2p_attr(
1797 ies->data, ies->len,
1798 IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
1799 (u8 *) &bss_conf->p2p_noa_attr,
1800 sizeof(bss_conf->p2p_noa_attr));
1801 if (ret >= 2) {
1802 sdata->u.mgd.p2p_noa_index =
1803 bss_conf->p2p_noa_attr.index;
1804 bss_info_changed |= BSS_CHANGED_P2P_PS;
1807 rcu_read_unlock();
1810 /* just to be sure */
1811 ieee80211_stop_poll(sdata);
1813 ieee80211_led_assoc(local, 1);
1815 if (sdata->u.mgd.have_beacon) {
1817 * If the AP is buggy we may get here with no DTIM period
1818 * known, so assume it's 1 which is the only safe assumption
1819 * in that case, although if the TIM IE is broken powersave
1820 * probably just won't work at all.
1822 bss_conf->dtim_period = sdata->u.mgd.dtim_period ?: 1;
1823 bss_conf->beacon_rate = bss->beacon_rate;
1824 bss_info_changed |= BSS_CHANGED_BEACON_INFO;
1825 } else {
1826 bss_conf->beacon_rate = NULL;
1827 bss_conf->dtim_period = 0;
1830 bss_conf->assoc = 1;
1832 /* Tell the driver to monitor connection quality (if supported) */
1833 if (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI &&
1834 bss_conf->cqm_rssi_thold)
1835 bss_info_changed |= BSS_CHANGED_CQM;
1837 /* Enable ARP filtering */
1838 if (bss_conf->arp_addr_cnt)
1839 bss_info_changed |= BSS_CHANGED_ARP_FILTER;
1841 ieee80211_bss_info_change_notify(sdata, bss_info_changed);
1843 mutex_lock(&local->iflist_mtx);
1844 ieee80211_recalc_ps(local, -1);
1845 mutex_unlock(&local->iflist_mtx);
1847 ieee80211_recalc_smps(sdata);
1848 ieee80211_recalc_ps_vif(sdata);
1850 netif_carrier_on(sdata->dev);
1853 static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
1854 u16 stype, u16 reason, bool tx,
1855 u8 *frame_buf)
1857 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1858 struct ieee80211_local *local = sdata->local;
1859 u32 changed = 0;
1861 sdata_assert_lock(sdata);
1863 if (WARN_ON_ONCE(tx && !frame_buf))
1864 return;
1866 if (WARN_ON(!ifmgd->associated))
1867 return;
1869 ieee80211_stop_poll(sdata);
1871 ifmgd->associated = NULL;
1872 netif_carrier_off(sdata->dev);
1875 * if we want to get out of ps before disassoc (why?) we have
1876 * to do it before sending disassoc, as otherwise the null-packet
1877 * won't be valid.
1879 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1880 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1881 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1883 local->ps_sdata = NULL;
1885 /* disable per-vif ps */
1886 ieee80211_recalc_ps_vif(sdata);
1888 /* flush out any pending frame (e.g. DELBA) before deauth/disassoc */
1889 if (tx)
1890 ieee80211_flush_queues(local, sdata);
1892 /* deauthenticate/disassociate now */
1893 if (tx || frame_buf)
1894 ieee80211_send_deauth_disassoc(sdata, ifmgd->bssid, stype,
1895 reason, tx, frame_buf);
1897 /* flush out frame */
1898 if (tx)
1899 ieee80211_flush_queues(local, sdata);
1901 /* clear bssid only after building the needed mgmt frames */
1902 memset(ifmgd->bssid, 0, ETH_ALEN);
1904 /* remove AP and TDLS peers */
1905 sta_info_flush_defer(sdata);
1907 /* finally reset all BSS / config parameters */
1908 changed |= ieee80211_reset_erp_info(sdata);
1910 ieee80211_led_assoc(local, 0);
1911 changed |= BSS_CHANGED_ASSOC;
1912 sdata->vif.bss_conf.assoc = false;
1914 ifmgd->p2p_noa_index = -1;
1915 memset(&sdata->vif.bss_conf.p2p_noa_attr, 0,
1916 sizeof(sdata->vif.bss_conf.p2p_noa_attr));
1918 /* on the next assoc, re-program HT/VHT parameters */
1919 memset(&ifmgd->ht_capa, 0, sizeof(ifmgd->ht_capa));
1920 memset(&ifmgd->ht_capa_mask, 0, sizeof(ifmgd->ht_capa_mask));
1921 memset(&ifmgd->vht_capa, 0, sizeof(ifmgd->vht_capa));
1922 memset(&ifmgd->vht_capa_mask, 0, sizeof(ifmgd->vht_capa_mask));
1924 sdata->ap_power_level = IEEE80211_UNSET_POWER_LEVEL;
1926 del_timer_sync(&local->dynamic_ps_timer);
1927 cancel_work_sync(&local->dynamic_ps_enable_work);
1929 /* Disable ARP filtering */
1930 if (sdata->vif.bss_conf.arp_addr_cnt)
1931 changed |= BSS_CHANGED_ARP_FILTER;
1933 sdata->vif.bss_conf.qos = false;
1934 changed |= BSS_CHANGED_QOS;
1936 /* The BSSID (not really interesting) and HT changed */
1937 changed |= BSS_CHANGED_BSSID | BSS_CHANGED_HT;
1938 ieee80211_bss_info_change_notify(sdata, changed);
1940 /* disassociated - set to defaults now */
1941 ieee80211_set_wmm_default(sdata, false);
1943 del_timer_sync(&sdata->u.mgd.conn_mon_timer);
1944 del_timer_sync(&sdata->u.mgd.bcn_mon_timer);
1945 del_timer_sync(&sdata->u.mgd.timer);
1946 del_timer_sync(&sdata->u.mgd.chswitch_timer);
1948 sdata->vif.bss_conf.dtim_period = 0;
1949 sdata->vif.bss_conf.beacon_rate = NULL;
1951 ifmgd->have_beacon = false;
1953 ifmgd->flags = 0;
1954 ieee80211_vif_release_channel(sdata);
1957 void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
1958 struct ieee80211_hdr *hdr)
1961 * We can postpone the mgd.timer whenever receiving unicast frames
1962 * from AP because we know that the connection is working both ways
1963 * at that time. But multicast frames (and hence also beacons) must
1964 * be ignored here, because we need to trigger the timer during
1965 * data idle periods for sending the periodic probe request to the
1966 * AP we're connected to.
1968 if (is_multicast_ether_addr(hdr->addr1))
1969 return;
1971 ieee80211_sta_reset_conn_monitor(sdata);
1974 static void ieee80211_reset_ap_probe(struct ieee80211_sub_if_data *sdata)
1976 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1977 struct ieee80211_local *local = sdata->local;
1979 mutex_lock(&local->mtx);
1980 if (!(ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
1981 IEEE80211_STA_CONNECTION_POLL))) {
1982 mutex_unlock(&local->mtx);
1983 return;
1986 __ieee80211_stop_poll(sdata);
1988 mutex_lock(&local->iflist_mtx);
1989 ieee80211_recalc_ps(local, -1);
1990 mutex_unlock(&local->iflist_mtx);
1992 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
1993 goto out;
1996 * We've received a probe response, but are not sure whether
1997 * we have or will be receiving any beacons or data, so let's
1998 * schedule the timers again, just in case.
2000 ieee80211_sta_reset_beacon_monitor(sdata);
2002 mod_timer(&ifmgd->conn_mon_timer,
2003 round_jiffies_up(jiffies +
2004 IEEE80211_CONNECTION_IDLE_TIME));
2005 out:
2006 mutex_unlock(&local->mtx);
2009 void ieee80211_sta_tx_notify(struct ieee80211_sub_if_data *sdata,
2010 struct ieee80211_hdr *hdr, bool ack)
2012 if (!ieee80211_is_data(hdr->frame_control))
2013 return;
2015 if (ieee80211_is_nullfunc(hdr->frame_control) &&
2016 sdata->u.mgd.probe_send_count > 0) {
2017 if (ack)
2018 ieee80211_sta_reset_conn_monitor(sdata);
2019 else
2020 sdata->u.mgd.nullfunc_failed = true;
2021 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
2022 return;
2025 if (ack)
2026 ieee80211_sta_reset_conn_monitor(sdata);
2029 static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
2031 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2032 const u8 *ssid;
2033 u8 *dst = ifmgd->associated->bssid;
2034 u8 unicast_limit = max(1, max_probe_tries - 3);
2037 * Try sending broadcast probe requests for the last three
2038 * probe requests after the first ones failed since some
2039 * buggy APs only support broadcast probe requests.
2041 if (ifmgd->probe_send_count >= unicast_limit)
2042 dst = NULL;
2045 * When the hardware reports an accurate Tx ACK status, it's
2046 * better to send a nullfunc frame instead of a probe request,
2047 * as it will kick us off the AP quickly if we aren't associated
2048 * anymore. The timeout will be reset if the frame is ACKed by
2049 * the AP.
2051 ifmgd->probe_send_count++;
2053 if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
2054 ifmgd->nullfunc_failed = false;
2055 ieee80211_send_nullfunc(sdata->local, sdata, 0);
2056 } else {
2057 int ssid_len;
2059 rcu_read_lock();
2060 ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
2061 if (WARN_ON_ONCE(ssid == NULL))
2062 ssid_len = 0;
2063 else
2064 ssid_len = ssid[1];
2066 ieee80211_send_probe_req(sdata, dst, ssid + 2, ssid_len, NULL,
2067 0, (u32) -1, true, 0,
2068 ifmgd->associated->channel, false);
2069 rcu_read_unlock();
2072 ifmgd->probe_timeout = jiffies + msecs_to_jiffies(probe_wait_ms);
2073 run_again(sdata, ifmgd->probe_timeout);
2074 if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
2075 ieee80211_flush_queues(sdata->local, sdata);
2078 static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
2079 bool beacon)
2081 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2082 bool already = false;
2084 if (!ieee80211_sdata_running(sdata))
2085 return;
2087 sdata_lock(sdata);
2089 if (!ifmgd->associated)
2090 goto out;
2092 mutex_lock(&sdata->local->mtx);
2094 if (sdata->local->tmp_channel || sdata->local->scanning) {
2095 mutex_unlock(&sdata->local->mtx);
2096 goto out;
2099 if (beacon) {
2100 mlme_dbg_ratelimited(sdata,
2101 "detected beacon loss from AP (missed %d beacons) - probing\n",
2102 beacon_loss_count);
2104 ieee80211_cqm_rssi_notify(&sdata->vif,
2105 NL80211_CQM_RSSI_BEACON_LOSS_EVENT,
2106 GFP_KERNEL);
2110 * The driver/our work has already reported this event or the
2111 * connection monitoring has kicked in and we have already sent
2112 * a probe request. Or maybe the AP died and the driver keeps
2113 * reporting until we disassociate...
2115 * In either case we have to ignore the current call to this
2116 * function (except for setting the correct probe reason bit)
2117 * because otherwise we would reset the timer every time and
2118 * never check whether we received a probe response!
2120 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
2121 IEEE80211_STA_CONNECTION_POLL))
2122 already = true;
2124 if (beacon)
2125 ifmgd->flags |= IEEE80211_STA_BEACON_POLL;
2126 else
2127 ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
2129 mutex_unlock(&sdata->local->mtx);
2131 if (already)
2132 goto out;
2134 mutex_lock(&sdata->local->iflist_mtx);
2135 ieee80211_recalc_ps(sdata->local, -1);
2136 mutex_unlock(&sdata->local->iflist_mtx);
2138 ifmgd->probe_send_count = 0;
2139 ieee80211_mgd_probe_ap_send(sdata);
2140 out:
2141 sdata_unlock(sdata);
2144 struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw,
2145 struct ieee80211_vif *vif)
2147 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2148 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2149 struct cfg80211_bss *cbss;
2150 struct sk_buff *skb;
2151 const u8 *ssid;
2152 int ssid_len;
2154 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
2155 return NULL;
2157 sdata_assert_lock(sdata);
2159 if (ifmgd->associated)
2160 cbss = ifmgd->associated;
2161 else if (ifmgd->auth_data)
2162 cbss = ifmgd->auth_data->bss;
2163 else if (ifmgd->assoc_data)
2164 cbss = ifmgd->assoc_data->bss;
2165 else
2166 return NULL;
2168 rcu_read_lock();
2169 ssid = ieee80211_bss_get_ie(cbss, WLAN_EID_SSID);
2170 if (WARN_ON_ONCE(ssid == NULL))
2171 ssid_len = 0;
2172 else
2173 ssid_len = ssid[1];
2175 skb = ieee80211_build_probe_req(sdata, cbss->bssid,
2176 (u32) -1, cbss->channel,
2177 ssid + 2, ssid_len,
2178 NULL, 0, true);
2179 rcu_read_unlock();
2181 return skb;
2183 EXPORT_SYMBOL(ieee80211_ap_probereq_get);
2185 static void __ieee80211_disconnect(struct ieee80211_sub_if_data *sdata)
2187 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2188 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
2190 sdata_lock(sdata);
2191 if (!ifmgd->associated) {
2192 sdata_unlock(sdata);
2193 return;
2196 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
2197 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
2198 true, frame_buf);
2199 ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
2200 ieee80211_wake_queues_by_reason(&sdata->local->hw,
2201 IEEE80211_MAX_QUEUE_MAP,
2202 IEEE80211_QUEUE_STOP_REASON_CSA);
2204 cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
2205 IEEE80211_DEAUTH_FRAME_LEN);
2206 sdata_unlock(sdata);
2209 static void ieee80211_beacon_connection_loss_work(struct work_struct *work)
2211 struct ieee80211_sub_if_data *sdata =
2212 container_of(work, struct ieee80211_sub_if_data,
2213 u.mgd.beacon_connection_loss_work);
2214 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2215 struct sta_info *sta;
2217 if (ifmgd->associated) {
2218 rcu_read_lock();
2219 sta = sta_info_get(sdata, ifmgd->bssid);
2220 if (sta)
2221 sta->beacon_loss_count++;
2222 rcu_read_unlock();
2225 if (ifmgd->connection_loss) {
2226 sdata_info(sdata, "Connection to AP %pM lost\n",
2227 ifmgd->bssid);
2228 __ieee80211_disconnect(sdata);
2229 } else {
2230 ieee80211_mgd_probe_ap(sdata, true);
2234 static void ieee80211_csa_connection_drop_work(struct work_struct *work)
2236 struct ieee80211_sub_if_data *sdata =
2237 container_of(work, struct ieee80211_sub_if_data,
2238 u.mgd.csa_connection_drop_work);
2240 __ieee80211_disconnect(sdata);
2243 void ieee80211_beacon_loss(struct ieee80211_vif *vif)
2245 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2246 struct ieee80211_hw *hw = &sdata->local->hw;
2248 trace_api_beacon_loss(sdata);
2250 sdata->u.mgd.connection_loss = false;
2251 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
2253 EXPORT_SYMBOL(ieee80211_beacon_loss);
2255 void ieee80211_connection_loss(struct ieee80211_vif *vif)
2257 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2258 struct ieee80211_hw *hw = &sdata->local->hw;
2260 trace_api_connection_loss(sdata);
2262 sdata->u.mgd.connection_loss = true;
2263 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
2265 EXPORT_SYMBOL(ieee80211_connection_loss);
2268 static void ieee80211_destroy_auth_data(struct ieee80211_sub_if_data *sdata,
2269 bool assoc)
2271 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
2273 sdata_assert_lock(sdata);
2275 if (!assoc) {
2276 sta_info_destroy_addr(sdata, auth_data->bss->bssid);
2278 memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
2279 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
2280 sdata->u.mgd.flags = 0;
2281 ieee80211_vif_release_channel(sdata);
2284 cfg80211_put_bss(sdata->local->hw.wiphy, auth_data->bss);
2285 kfree(auth_data);
2286 sdata->u.mgd.auth_data = NULL;
2289 static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
2290 struct ieee80211_mgmt *mgmt, size_t len)
2292 struct ieee80211_local *local = sdata->local;
2293 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
2294 u8 *pos;
2295 struct ieee802_11_elems elems;
2296 u32 tx_flags = 0;
2298 pos = mgmt->u.auth.variable;
2299 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), false, &elems);
2300 if (!elems.challenge)
2301 return;
2302 auth_data->expected_transaction = 4;
2303 drv_mgd_prepare_tx(sdata->local, sdata);
2304 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
2305 tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
2306 IEEE80211_TX_INTFL_MLME_CONN_TX;
2307 ieee80211_send_auth(sdata, 3, auth_data->algorithm, 0,
2308 elems.challenge - 2, elems.challenge_len + 2,
2309 auth_data->bss->bssid, auth_data->bss->bssid,
2310 auth_data->key, auth_data->key_len,
2311 auth_data->key_idx, tx_flags);
2314 static void ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
2315 struct ieee80211_mgmt *mgmt, size_t len)
2317 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2318 u8 bssid[ETH_ALEN];
2319 u16 auth_alg, auth_transaction, status_code;
2320 struct sta_info *sta;
2322 sdata_assert_lock(sdata);
2324 if (len < 24 + 6)
2325 return;
2327 if (!ifmgd->auth_data || ifmgd->auth_data->done)
2328 return;
2330 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
2332 if (!ether_addr_equal(bssid, mgmt->bssid))
2333 return;
2335 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
2336 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
2337 status_code = le16_to_cpu(mgmt->u.auth.status_code);
2339 if (auth_alg != ifmgd->auth_data->algorithm ||
2340 auth_transaction != ifmgd->auth_data->expected_transaction) {
2341 sdata_info(sdata, "%pM unexpected authentication state: alg %d (expected %d) transact %d (expected %d)\n",
2342 mgmt->sa, auth_alg, ifmgd->auth_data->algorithm,
2343 auth_transaction,
2344 ifmgd->auth_data->expected_transaction);
2345 return;
2348 if (status_code != WLAN_STATUS_SUCCESS) {
2349 sdata_info(sdata, "%pM denied authentication (status %d)\n",
2350 mgmt->sa, status_code);
2351 ieee80211_destroy_auth_data(sdata, false);
2352 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
2353 return;
2356 switch (ifmgd->auth_data->algorithm) {
2357 case WLAN_AUTH_OPEN:
2358 case WLAN_AUTH_LEAP:
2359 case WLAN_AUTH_FT:
2360 case WLAN_AUTH_SAE:
2361 break;
2362 case WLAN_AUTH_SHARED_KEY:
2363 if (ifmgd->auth_data->expected_transaction != 4) {
2364 ieee80211_auth_challenge(sdata, mgmt, len);
2365 /* need another frame */
2366 return;
2368 break;
2369 default:
2370 WARN_ONCE(1, "invalid auth alg %d",
2371 ifmgd->auth_data->algorithm);
2372 return;
2375 sdata_info(sdata, "authenticated\n");
2376 ifmgd->auth_data->done = true;
2377 ifmgd->auth_data->timeout = jiffies + IEEE80211_AUTH_WAIT_ASSOC;
2378 ifmgd->auth_data->timeout_started = true;
2379 run_again(sdata, ifmgd->auth_data->timeout);
2381 if (ifmgd->auth_data->algorithm == WLAN_AUTH_SAE &&
2382 ifmgd->auth_data->expected_transaction != 2) {
2384 * Report auth frame to user space for processing since another
2385 * round of Authentication frames is still needed.
2387 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
2388 return;
2391 /* move station state to auth */
2392 mutex_lock(&sdata->local->sta_mtx);
2393 sta = sta_info_get(sdata, bssid);
2394 if (!sta) {
2395 WARN_ONCE(1, "%s: STA %pM not found", sdata->name, bssid);
2396 goto out_err;
2398 if (sta_info_move_state(sta, IEEE80211_STA_AUTH)) {
2399 sdata_info(sdata, "failed moving %pM to auth\n", bssid);
2400 goto out_err;
2402 mutex_unlock(&sdata->local->sta_mtx);
2404 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
2405 return;
2406 out_err:
2407 mutex_unlock(&sdata->local->sta_mtx);
2408 /* ignore frame -- wait for timeout */
2412 static void ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
2413 struct ieee80211_mgmt *mgmt, size_t len)
2415 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2416 const u8 *bssid = NULL;
2417 u16 reason_code;
2419 sdata_assert_lock(sdata);
2421 if (len < 24 + 2)
2422 return;
2424 if (!ifmgd->associated ||
2425 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
2426 return;
2428 bssid = ifmgd->associated->bssid;
2430 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
2432 sdata_info(sdata, "deauthenticated from %pM (Reason: %u)\n",
2433 bssid, reason_code);
2435 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
2437 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
2441 static void ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
2442 struct ieee80211_mgmt *mgmt, size_t len)
2444 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2445 u16 reason_code;
2447 sdata_assert_lock(sdata);
2449 if (len < 24 + 2)
2450 return;
2452 if (!ifmgd->associated ||
2453 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
2454 return;
2456 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
2458 sdata_info(sdata, "disassociated from %pM (Reason: %u)\n",
2459 mgmt->sa, reason_code);
2461 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
2463 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
2466 static void ieee80211_get_rates(struct ieee80211_supported_band *sband,
2467 u8 *supp_rates, unsigned int supp_rates_len,
2468 u32 *rates, u32 *basic_rates,
2469 bool *have_higher_than_11mbit,
2470 int *min_rate, int *min_rate_index,
2471 int shift, u32 rate_flags)
2473 int i, j;
2475 for (i = 0; i < supp_rates_len; i++) {
2476 int rate = supp_rates[i] & 0x7f;
2477 bool is_basic = !!(supp_rates[i] & 0x80);
2479 if ((rate * 5 * (1 << shift)) > 110)
2480 *have_higher_than_11mbit = true;
2483 * BSS_MEMBERSHIP_SELECTOR_HT_PHY is defined in 802.11n-2009
2484 * 7.3.2.2 as a magic value instead of a rate. Hence, skip it.
2486 * Note: Even through the membership selector and the basic
2487 * rate flag share the same bit, they are not exactly
2488 * the same.
2490 if (!!(supp_rates[i] & 0x80) &&
2491 (supp_rates[i] & 0x7f) == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
2492 continue;
2494 for (j = 0; j < sband->n_bitrates; j++) {
2495 struct ieee80211_rate *br;
2496 int brate;
2498 br = &sband->bitrates[j];
2499 if ((rate_flags & br->flags) != rate_flags)
2500 continue;
2502 brate = DIV_ROUND_UP(br->bitrate, (1 << shift) * 5);
2503 if (brate == rate) {
2504 *rates |= BIT(j);
2505 if (is_basic)
2506 *basic_rates |= BIT(j);
2507 if ((rate * 5) < *min_rate) {
2508 *min_rate = rate * 5;
2509 *min_rate_index = j;
2511 break;
2517 static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata,
2518 bool assoc)
2520 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
2522 sdata_assert_lock(sdata);
2524 if (!assoc) {
2525 sta_info_destroy_addr(sdata, assoc_data->bss->bssid);
2527 memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
2528 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
2529 sdata->u.mgd.flags = 0;
2530 ieee80211_vif_release_channel(sdata);
2533 kfree(assoc_data);
2534 sdata->u.mgd.assoc_data = NULL;
2537 static bool ieee80211_assoc_success(struct ieee80211_sub_if_data *sdata,
2538 struct cfg80211_bss *cbss,
2539 struct ieee80211_mgmt *mgmt, size_t len)
2541 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2542 struct ieee80211_local *local = sdata->local;
2543 struct ieee80211_supported_band *sband;
2544 struct sta_info *sta;
2545 u8 *pos;
2546 u16 capab_info, aid;
2547 struct ieee802_11_elems elems;
2548 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
2549 const struct cfg80211_bss_ies *bss_ies = NULL;
2550 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
2551 u32 changed = 0;
2552 int err;
2553 bool ret;
2555 /* AssocResp and ReassocResp have identical structure */
2557 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
2558 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
2560 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
2561 sdata_info(sdata, "invalid AID value 0x%x; bits 15:14 not set\n",
2562 aid);
2563 aid &= ~(BIT(15) | BIT(14));
2565 ifmgd->broken_ap = false;
2567 if (aid == 0 || aid > IEEE80211_MAX_AID) {
2568 sdata_info(sdata, "invalid AID value %d (out of range), turn off PS\n",
2569 aid);
2570 aid = 0;
2571 ifmgd->broken_ap = true;
2574 pos = mgmt->u.assoc_resp.variable;
2575 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), false, &elems);
2577 if (!elems.supp_rates) {
2578 sdata_info(sdata, "no SuppRates element in AssocResp\n");
2579 return false;
2582 ifmgd->aid = aid;
2585 * Some APs are erroneously not including some information in their
2586 * (re)association response frames. Try to recover by using the data
2587 * from the beacon or probe response. This seems to afflict mobile
2588 * 2G/3G/4G wifi routers, reported models include the "Onda PN51T",
2589 * "Vodafone PocketWiFi 2", "ZTE MF60" and a similar T-Mobile device.
2591 if ((assoc_data->wmm && !elems.wmm_param) ||
2592 (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
2593 (!elems.ht_cap_elem || !elems.ht_operation)) ||
2594 (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
2595 (!elems.vht_cap_elem || !elems.vht_operation))) {
2596 const struct cfg80211_bss_ies *ies;
2597 struct ieee802_11_elems bss_elems;
2599 rcu_read_lock();
2600 ies = rcu_dereference(cbss->ies);
2601 if (ies)
2602 bss_ies = kmemdup(ies, sizeof(*ies) + ies->len,
2603 GFP_ATOMIC);
2604 rcu_read_unlock();
2605 if (!bss_ies)
2606 return false;
2608 ieee802_11_parse_elems(bss_ies->data, bss_ies->len,
2609 false, &bss_elems);
2610 if (assoc_data->wmm &&
2611 !elems.wmm_param && bss_elems.wmm_param) {
2612 elems.wmm_param = bss_elems.wmm_param;
2613 sdata_info(sdata,
2614 "AP bug: WMM param missing from AssocResp\n");
2618 * Also check if we requested HT/VHT, otherwise the AP doesn't
2619 * have to include the IEs in the (re)association response.
2621 if (!elems.ht_cap_elem && bss_elems.ht_cap_elem &&
2622 !(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
2623 elems.ht_cap_elem = bss_elems.ht_cap_elem;
2624 sdata_info(sdata,
2625 "AP bug: HT capability missing from AssocResp\n");
2627 if (!elems.ht_operation && bss_elems.ht_operation &&
2628 !(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
2629 elems.ht_operation = bss_elems.ht_operation;
2630 sdata_info(sdata,
2631 "AP bug: HT operation missing from AssocResp\n");
2633 if (!elems.vht_cap_elem && bss_elems.vht_cap_elem &&
2634 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)) {
2635 elems.vht_cap_elem = bss_elems.vht_cap_elem;
2636 sdata_info(sdata,
2637 "AP bug: VHT capa missing from AssocResp\n");
2639 if (!elems.vht_operation && bss_elems.vht_operation &&
2640 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)) {
2641 elems.vht_operation = bss_elems.vht_operation;
2642 sdata_info(sdata,
2643 "AP bug: VHT operation missing from AssocResp\n");
2648 * We previously checked these in the beacon/probe response, so
2649 * they should be present here. This is just a safety net.
2651 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
2652 (!elems.wmm_param || !elems.ht_cap_elem || !elems.ht_operation)) {
2653 sdata_info(sdata,
2654 "HT AP is missing WMM params or HT capability/operation\n");
2655 ret = false;
2656 goto out;
2659 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
2660 (!elems.vht_cap_elem || !elems.vht_operation)) {
2661 sdata_info(sdata,
2662 "VHT AP is missing VHT capability/operation\n");
2663 ret = false;
2664 goto out;
2667 mutex_lock(&sdata->local->sta_mtx);
2669 * station info was already allocated and inserted before
2670 * the association and should be available to us
2672 sta = sta_info_get(sdata, cbss->bssid);
2673 if (WARN_ON(!sta)) {
2674 mutex_unlock(&sdata->local->sta_mtx);
2675 ret = false;
2676 goto out;
2679 sband = local->hw.wiphy->bands[ieee80211_get_sdata_band(sdata)];
2681 /* Set up internal HT/VHT capabilities */
2682 if (elems.ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
2683 ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
2684 elems.ht_cap_elem, sta);
2686 if (elems.vht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
2687 ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband,
2688 elems.vht_cap_elem, sta);
2691 * Some APs, e.g. Netgear WNDR3700, report invalid HT operation data
2692 * in their association response, so ignore that data for our own
2693 * configuration. If it changed since the last beacon, we'll get the
2694 * next beacon and update then.
2698 * If an operating mode notification IE is present, override the
2699 * NSS calculation (that would be done in rate_control_rate_init())
2700 * and use the # of streams from that element.
2702 if (elems.opmode_notif &&
2703 !(*elems.opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_TYPE_BF)) {
2704 u8 nss;
2706 nss = *elems.opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_MASK;
2707 nss >>= IEEE80211_OPMODE_NOTIF_RX_NSS_SHIFT;
2708 nss += 1;
2709 sta->sta.rx_nss = nss;
2712 rate_control_rate_init(sta);
2714 if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED)
2715 set_sta_flag(sta, WLAN_STA_MFP);
2717 if (elems.wmm_param)
2718 set_sta_flag(sta, WLAN_STA_WME);
2720 err = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
2721 if (!err && !(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
2722 err = sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
2723 if (err) {
2724 sdata_info(sdata,
2725 "failed to move station %pM to desired state\n",
2726 sta->sta.addr);
2727 WARN_ON(__sta_info_destroy(sta));
2728 mutex_unlock(&sdata->local->sta_mtx);
2729 ret = false;
2730 goto out;
2733 mutex_unlock(&sdata->local->sta_mtx);
2736 * Always handle WMM once after association regardless
2737 * of the first value the AP uses. Setting -1 here has
2738 * that effect because the AP values is an unsigned
2739 * 4-bit value.
2741 ifmgd->wmm_last_param_set = -1;
2743 if (elems.wmm_param)
2744 ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
2745 elems.wmm_param_len);
2746 else
2747 ieee80211_set_wmm_default(sdata, false);
2748 changed |= BSS_CHANGED_QOS;
2750 /* set AID and assoc capability,
2751 * ieee80211_set_associated() will tell the driver */
2752 bss_conf->aid = aid;
2753 bss_conf->assoc_capability = capab_info;
2754 ieee80211_set_associated(sdata, cbss, changed);
2757 * If we're using 4-addr mode, let the AP know that we're
2758 * doing so, so that it can create the STA VLAN on its side
2760 if (ifmgd->use_4addr)
2761 ieee80211_send_4addr_nullfunc(local, sdata);
2764 * Start timer to probe the connection to the AP now.
2765 * Also start the timer that will detect beacon loss.
2767 ieee80211_sta_rx_notify(sdata, (struct ieee80211_hdr *)mgmt);
2768 ieee80211_sta_reset_beacon_monitor(sdata);
2770 ret = true;
2771 out:
2772 kfree(bss_ies);
2773 return ret;
2776 static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
2777 struct ieee80211_mgmt *mgmt,
2778 size_t len)
2780 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2781 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
2782 u16 capab_info, status_code, aid;
2783 struct ieee802_11_elems elems;
2784 u8 *pos;
2785 bool reassoc;
2786 struct cfg80211_bss *bss;
2788 sdata_assert_lock(sdata);
2790 if (!assoc_data)
2791 return;
2792 if (!ether_addr_equal(assoc_data->bss->bssid, mgmt->bssid))
2793 return;
2796 * AssocResp and ReassocResp have identical structure, so process both
2797 * of them in this function.
2800 if (len < 24 + 6)
2801 return;
2803 reassoc = ieee80211_is_reassoc_req(mgmt->frame_control);
2804 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
2805 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
2806 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
2808 sdata_info(sdata,
2809 "RX %sssocResp from %pM (capab=0x%x status=%d aid=%d)\n",
2810 reassoc ? "Rea" : "A", mgmt->sa,
2811 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
2813 pos = mgmt->u.assoc_resp.variable;
2814 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), false, &elems);
2816 if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
2817 elems.timeout_int &&
2818 elems.timeout_int->type == WLAN_TIMEOUT_ASSOC_COMEBACK) {
2819 u32 tu, ms;
2820 tu = le32_to_cpu(elems.timeout_int->value);
2821 ms = tu * 1024 / 1000;
2822 sdata_info(sdata,
2823 "%pM rejected association temporarily; comeback duration %u TU (%u ms)\n",
2824 mgmt->sa, tu, ms);
2825 assoc_data->timeout = jiffies + msecs_to_jiffies(ms);
2826 assoc_data->timeout_started = true;
2827 if (ms > IEEE80211_ASSOC_TIMEOUT)
2828 run_again(sdata, assoc_data->timeout);
2829 return;
2832 bss = assoc_data->bss;
2834 if (status_code != WLAN_STATUS_SUCCESS) {
2835 sdata_info(sdata, "%pM denied association (code=%d)\n",
2836 mgmt->sa, status_code);
2837 ieee80211_destroy_assoc_data(sdata, false);
2838 } else {
2839 if (!ieee80211_assoc_success(sdata, bss, mgmt, len)) {
2840 /* oops -- internal error -- send timeout for now */
2841 ieee80211_destroy_assoc_data(sdata, false);
2842 cfg80211_assoc_timeout(sdata->dev, bss);
2843 return;
2845 sdata_info(sdata, "associated\n");
2848 * destroy assoc_data afterwards, as otherwise an idle
2849 * recalc after assoc_data is NULL but before associated
2850 * is set can cause the interface to go idle
2852 ieee80211_destroy_assoc_data(sdata, true);
2855 cfg80211_rx_assoc_resp(sdata->dev, bss, (u8 *)mgmt, len);
2858 static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
2859 struct ieee80211_mgmt *mgmt, size_t len,
2860 struct ieee80211_rx_status *rx_status,
2861 struct ieee802_11_elems *elems)
2863 struct ieee80211_local *local = sdata->local;
2864 int freq;
2865 struct ieee80211_bss *bss;
2866 struct ieee80211_channel *channel;
2868 sdata_assert_lock(sdata);
2870 if (elems->ds_params)
2871 freq = ieee80211_channel_to_frequency(elems->ds_params[0],
2872 rx_status->band);
2873 else
2874 freq = rx_status->freq;
2876 channel = ieee80211_get_channel(local->hw.wiphy, freq);
2878 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
2879 return;
2881 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
2882 channel);
2883 if (bss) {
2884 sdata->vif.bss_conf.beacon_rate = bss->beacon_rate;
2885 ieee80211_rx_bss_put(local, bss);
2890 static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
2891 struct sk_buff *skb)
2893 struct ieee80211_mgmt *mgmt = (void *)skb->data;
2894 struct ieee80211_if_managed *ifmgd;
2895 struct ieee80211_rx_status *rx_status = (void *) skb->cb;
2896 size_t baselen, len = skb->len;
2897 struct ieee802_11_elems elems;
2899 ifmgd = &sdata->u.mgd;
2901 sdata_assert_lock(sdata);
2903 if (!ether_addr_equal(mgmt->da, sdata->vif.addr))
2904 return; /* ignore ProbeResp to foreign address */
2906 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
2907 if (baselen > len)
2908 return;
2910 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
2911 false, &elems);
2913 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
2915 if (ifmgd->associated &&
2916 ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
2917 ieee80211_reset_ap_probe(sdata);
2919 if (ifmgd->auth_data && !ifmgd->auth_data->bss->proberesp_ies &&
2920 ether_addr_equal(mgmt->bssid, ifmgd->auth_data->bss->bssid)) {
2921 /* got probe response, continue with auth */
2922 sdata_info(sdata, "direct probe responded\n");
2923 ifmgd->auth_data->tries = 0;
2924 ifmgd->auth_data->timeout = jiffies;
2925 ifmgd->auth_data->timeout_started = true;
2926 run_again(sdata, ifmgd->auth_data->timeout);
2931 * This is the canonical list of information elements we care about,
2932 * the filter code also gives us all changes to the Microsoft OUI
2933 * (00:50:F2) vendor IE which is used for WMM which we need to track.
2935 * We implement beacon filtering in software since that means we can
2936 * avoid processing the frame here and in cfg80211, and userspace
2937 * will not be able to tell whether the hardware supports it or not.
2939 * XXX: This list needs to be dynamic -- userspace needs to be able to
2940 * add items it requires. It also needs to be able to tell us to
2941 * look out for other vendor IEs.
2943 static const u64 care_about_ies =
2944 (1ULL << WLAN_EID_COUNTRY) |
2945 (1ULL << WLAN_EID_ERP_INFO) |
2946 (1ULL << WLAN_EID_CHANNEL_SWITCH) |
2947 (1ULL << WLAN_EID_PWR_CONSTRAINT) |
2948 (1ULL << WLAN_EID_HT_CAPABILITY) |
2949 (1ULL << WLAN_EID_HT_OPERATION);
2951 static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
2952 struct ieee80211_mgmt *mgmt, size_t len,
2953 struct ieee80211_rx_status *rx_status)
2955 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2956 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
2957 size_t baselen;
2958 struct ieee802_11_elems elems;
2959 struct ieee80211_local *local = sdata->local;
2960 struct ieee80211_chanctx_conf *chanctx_conf;
2961 struct ieee80211_channel *chan;
2962 struct sta_info *sta;
2963 u32 changed = 0;
2964 bool erp_valid;
2965 u8 erp_value = 0;
2966 u32 ncrc;
2967 u8 *bssid;
2968 u8 deauth_buf[IEEE80211_DEAUTH_FRAME_LEN];
2970 sdata_assert_lock(sdata);
2972 /* Process beacon from the current BSS */
2973 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2974 if (baselen > len)
2975 return;
2977 rcu_read_lock();
2978 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2979 if (!chanctx_conf) {
2980 rcu_read_unlock();
2981 return;
2984 if (rx_status->freq != chanctx_conf->def.chan->center_freq) {
2985 rcu_read_unlock();
2986 return;
2988 chan = chanctx_conf->def.chan;
2989 rcu_read_unlock();
2991 if (ifmgd->assoc_data && ifmgd->assoc_data->need_beacon &&
2992 ether_addr_equal(mgmt->bssid, ifmgd->assoc_data->bss->bssid)) {
2993 ieee802_11_parse_elems(mgmt->u.beacon.variable,
2994 len - baselen, false, &elems);
2996 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
2997 if (elems.tim && !elems.parse_error) {
2998 const struct ieee80211_tim_ie *tim_ie = elems.tim;
2999 ifmgd->dtim_period = tim_ie->dtim_period;
3001 ifmgd->have_beacon = true;
3002 ifmgd->assoc_data->need_beacon = false;
3003 if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
3004 sdata->vif.bss_conf.sync_tsf =
3005 le64_to_cpu(mgmt->u.beacon.timestamp);
3006 sdata->vif.bss_conf.sync_device_ts =
3007 rx_status->device_timestamp;
3008 if (elems.tim)
3009 sdata->vif.bss_conf.sync_dtim_count =
3010 elems.tim->dtim_count;
3011 else
3012 sdata->vif.bss_conf.sync_dtim_count = 0;
3014 /* continue assoc process */
3015 ifmgd->assoc_data->timeout = jiffies;
3016 ifmgd->assoc_data->timeout_started = true;
3017 run_again(sdata, ifmgd->assoc_data->timeout);
3018 return;
3021 if (!ifmgd->associated ||
3022 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
3023 return;
3024 bssid = ifmgd->associated->bssid;
3026 /* Track average RSSI from the Beacon frames of the current AP */
3027 ifmgd->last_beacon_signal = rx_status->signal;
3028 if (ifmgd->flags & IEEE80211_STA_RESET_SIGNAL_AVE) {
3029 ifmgd->flags &= ~IEEE80211_STA_RESET_SIGNAL_AVE;
3030 ifmgd->ave_beacon_signal = rx_status->signal * 16;
3031 ifmgd->last_cqm_event_signal = 0;
3032 ifmgd->count_beacon_signal = 1;
3033 ifmgd->last_ave_beacon_signal = 0;
3034 } else {
3035 ifmgd->ave_beacon_signal =
3036 (IEEE80211_SIGNAL_AVE_WEIGHT * rx_status->signal * 16 +
3037 (16 - IEEE80211_SIGNAL_AVE_WEIGHT) *
3038 ifmgd->ave_beacon_signal) / 16;
3039 ifmgd->count_beacon_signal++;
3042 if (ifmgd->rssi_min_thold != ifmgd->rssi_max_thold &&
3043 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
3044 int sig = ifmgd->ave_beacon_signal;
3045 int last_sig = ifmgd->last_ave_beacon_signal;
3048 * if signal crosses either of the boundaries, invoke callback
3049 * with appropriate parameters
3051 if (sig > ifmgd->rssi_max_thold &&
3052 (last_sig <= ifmgd->rssi_min_thold || last_sig == 0)) {
3053 ifmgd->last_ave_beacon_signal = sig;
3054 drv_rssi_callback(local, sdata, RSSI_EVENT_HIGH);
3055 } else if (sig < ifmgd->rssi_min_thold &&
3056 (last_sig >= ifmgd->rssi_max_thold ||
3057 last_sig == 0)) {
3058 ifmgd->last_ave_beacon_signal = sig;
3059 drv_rssi_callback(local, sdata, RSSI_EVENT_LOW);
3063 if (bss_conf->cqm_rssi_thold &&
3064 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT &&
3065 !(sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI)) {
3066 int sig = ifmgd->ave_beacon_signal / 16;
3067 int last_event = ifmgd->last_cqm_event_signal;
3068 int thold = bss_conf->cqm_rssi_thold;
3069 int hyst = bss_conf->cqm_rssi_hyst;
3070 if (sig < thold &&
3071 (last_event == 0 || sig < last_event - hyst)) {
3072 ifmgd->last_cqm_event_signal = sig;
3073 ieee80211_cqm_rssi_notify(
3074 &sdata->vif,
3075 NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
3076 GFP_KERNEL);
3077 } else if (sig > thold &&
3078 (last_event == 0 || sig > last_event + hyst)) {
3079 ifmgd->last_cqm_event_signal = sig;
3080 ieee80211_cqm_rssi_notify(
3081 &sdata->vif,
3082 NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
3083 GFP_KERNEL);
3087 if (ifmgd->flags & IEEE80211_STA_BEACON_POLL) {
3088 mlme_dbg_ratelimited(sdata,
3089 "cancelling AP probe due to a received beacon\n");
3090 mutex_lock(&local->mtx);
3091 ifmgd->flags &= ~IEEE80211_STA_BEACON_POLL;
3092 ieee80211_run_deferred_scan(local);
3093 mutex_unlock(&local->mtx);
3095 mutex_lock(&local->iflist_mtx);
3096 ieee80211_recalc_ps(local, -1);
3097 mutex_unlock(&local->iflist_mtx);
3101 * Push the beacon loss detection into the future since
3102 * we are processing a beacon from the AP just now.
3104 ieee80211_sta_reset_beacon_monitor(sdata);
3106 ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
3107 ncrc = ieee802_11_parse_elems_crc(mgmt->u.beacon.variable,
3108 len - baselen, false, &elems,
3109 care_about_ies, ncrc);
3111 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) {
3112 bool directed_tim = ieee80211_check_tim(elems.tim,
3113 elems.tim_len,
3114 ifmgd->aid);
3115 if (directed_tim) {
3116 if (local->hw.conf.dynamic_ps_timeout > 0) {
3117 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
3118 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
3119 ieee80211_hw_config(local,
3120 IEEE80211_CONF_CHANGE_PS);
3122 ieee80211_send_nullfunc(local, sdata, 0);
3123 } else if (!local->pspolling && sdata->u.mgd.powersave) {
3124 local->pspolling = true;
3127 * Here is assumed that the driver will be
3128 * able to send ps-poll frame and receive a
3129 * response even though power save mode is
3130 * enabled, but some drivers might require
3131 * to disable power save here. This needs
3132 * to be investigated.
3134 ieee80211_send_pspoll(local, sdata);
3139 if (sdata->vif.p2p) {
3140 struct ieee80211_p2p_noa_attr noa = {};
3141 int ret;
3143 ret = cfg80211_get_p2p_attr(mgmt->u.beacon.variable,
3144 len - baselen,
3145 IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
3146 (u8 *) &noa, sizeof(noa));
3147 if (ret >= 2) {
3148 if (sdata->u.mgd.p2p_noa_index != noa.index) {
3149 /* valid noa_attr and index changed */
3150 sdata->u.mgd.p2p_noa_index = noa.index;
3151 memcpy(&bss_conf->p2p_noa_attr, &noa, sizeof(noa));
3152 changed |= BSS_CHANGED_P2P_PS;
3154 * make sure we update all information, the CRC
3155 * mechanism doesn't look at P2P attributes.
3157 ifmgd->beacon_crc_valid = false;
3159 } else if (sdata->u.mgd.p2p_noa_index != -1) {
3160 /* noa_attr not found and we had valid noa_attr before */
3161 sdata->u.mgd.p2p_noa_index = -1;
3162 memset(&bss_conf->p2p_noa_attr, 0, sizeof(bss_conf->p2p_noa_attr));
3163 changed |= BSS_CHANGED_P2P_PS;
3164 ifmgd->beacon_crc_valid = false;
3168 if (ncrc == ifmgd->beacon_crc && ifmgd->beacon_crc_valid)
3169 return;
3170 ifmgd->beacon_crc = ncrc;
3171 ifmgd->beacon_crc_valid = true;
3173 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
3175 ieee80211_sta_process_chanswitch(sdata, rx_status->mactime,
3176 &elems, true);
3178 if (ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
3179 elems.wmm_param_len))
3180 changed |= BSS_CHANGED_QOS;
3183 * If we haven't had a beacon before, tell the driver about the
3184 * DTIM period (and beacon timing if desired) now.
3186 if (!ifmgd->have_beacon) {
3187 /* a few bogus AP send dtim_period = 0 or no TIM IE */
3188 if (elems.tim)
3189 bss_conf->dtim_period = elems.tim->dtim_period ?: 1;
3190 else
3191 bss_conf->dtim_period = 1;
3193 if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
3194 sdata->vif.bss_conf.sync_tsf =
3195 le64_to_cpu(mgmt->u.beacon.timestamp);
3196 sdata->vif.bss_conf.sync_device_ts =
3197 rx_status->device_timestamp;
3198 if (elems.tim)
3199 sdata->vif.bss_conf.sync_dtim_count =
3200 elems.tim->dtim_count;
3201 else
3202 sdata->vif.bss_conf.sync_dtim_count = 0;
3205 changed |= BSS_CHANGED_BEACON_INFO;
3206 ifmgd->have_beacon = true;
3208 mutex_lock(&local->iflist_mtx);
3209 ieee80211_recalc_ps(local, -1);
3210 mutex_unlock(&local->iflist_mtx);
3212 ieee80211_recalc_ps_vif(sdata);
3215 if (elems.erp_info) {
3216 erp_valid = true;
3217 erp_value = elems.erp_info[0];
3218 } else {
3219 erp_valid = false;
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,
3233 true, deauth_buf);
3234 cfg80211_tx_mlme_mgmt(sdata->dev, deauth_buf,
3235 sizeof(deauth_buf));
3236 return;
3239 if (sta && elems.opmode_notif)
3240 ieee80211_vht_handle_opmode(sdata, sta, *elems.opmode_notif,
3241 rx_status->band, true);
3242 mutex_unlock(&local->sta_mtx);
3244 if (elems.country_elem && elems.pwr_constr_elem &&
3245 mgmt->u.probe_resp.capab_info &
3246 cpu_to_le16(WLAN_CAPABILITY_SPECTRUM_MGMT))
3247 changed |= ieee80211_handle_pwr_constr(sdata, chan,
3248 elems.country_elem,
3249 elems.country_elem_len,
3250 elems.pwr_constr_elem);
3252 ieee80211_bss_info_change_notify(sdata, changed);
3255 void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
3256 struct sk_buff *skb)
3258 struct ieee80211_rx_status *rx_status;
3259 struct ieee80211_mgmt *mgmt;
3260 u16 fc;
3261 struct ieee802_11_elems elems;
3262 int ies_len;
3264 rx_status = (struct ieee80211_rx_status *) skb->cb;
3265 mgmt = (struct ieee80211_mgmt *) skb->data;
3266 fc = le16_to_cpu(mgmt->frame_control);
3268 sdata_lock(sdata);
3270 switch (fc & IEEE80211_FCTL_STYPE) {
3271 case IEEE80211_STYPE_BEACON:
3272 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len, rx_status);
3273 break;
3274 case IEEE80211_STYPE_PROBE_RESP:
3275 ieee80211_rx_mgmt_probe_resp(sdata, skb);
3276 break;
3277 case IEEE80211_STYPE_AUTH:
3278 ieee80211_rx_mgmt_auth(sdata, mgmt, skb->len);
3279 break;
3280 case IEEE80211_STYPE_DEAUTH:
3281 ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
3282 break;
3283 case IEEE80211_STYPE_DISASSOC:
3284 ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
3285 break;
3286 case IEEE80211_STYPE_ASSOC_RESP:
3287 case IEEE80211_STYPE_REASSOC_RESP:
3288 ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len);
3289 break;
3290 case IEEE80211_STYPE_ACTION:
3291 if (mgmt->u.action.category == WLAN_CATEGORY_SPECTRUM_MGMT) {
3292 ies_len = skb->len -
3293 offsetof(struct ieee80211_mgmt,
3294 u.action.u.chan_switch.variable);
3296 if (ies_len < 0)
3297 break;
3299 ieee802_11_parse_elems(
3300 mgmt->u.action.u.chan_switch.variable,
3301 ies_len, true, &elems);
3303 if (elems.parse_error)
3304 break;
3306 ieee80211_sta_process_chanswitch(sdata,
3307 rx_status->mactime,
3308 &elems, false);
3309 } else if (mgmt->u.action.category == WLAN_CATEGORY_PUBLIC) {
3310 ies_len = skb->len -
3311 offsetof(struct ieee80211_mgmt,
3312 u.action.u.ext_chan_switch.variable);
3314 if (ies_len < 0)
3315 break;
3317 ieee802_11_parse_elems(
3318 mgmt->u.action.u.ext_chan_switch.variable,
3319 ies_len, true, &elems);
3321 if (elems.parse_error)
3322 break;
3324 /* for the handling code pretend this was also an IE */
3325 elems.ext_chansw_ie =
3326 &mgmt->u.action.u.ext_chan_switch.data;
3328 ieee80211_sta_process_chanswitch(sdata,
3329 rx_status->mactime,
3330 &elems, false);
3332 break;
3334 sdata_unlock(sdata);
3337 static void ieee80211_sta_timer(unsigned long data)
3339 struct ieee80211_sub_if_data *sdata =
3340 (struct ieee80211_sub_if_data *) data;
3342 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
3345 static void ieee80211_sta_connection_lost(struct ieee80211_sub_if_data *sdata,
3346 u8 *bssid, u8 reason, bool tx)
3348 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
3350 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH, reason,
3351 tx, frame_buf);
3353 cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
3354 IEEE80211_DEAUTH_FRAME_LEN);
3357 static int ieee80211_probe_auth(struct ieee80211_sub_if_data *sdata)
3359 struct ieee80211_local *local = sdata->local;
3360 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3361 struct ieee80211_mgd_auth_data *auth_data = ifmgd->auth_data;
3362 u32 tx_flags = 0;
3364 sdata_assert_lock(sdata);
3366 if (WARN_ON_ONCE(!auth_data))
3367 return -EINVAL;
3369 auth_data->tries++;
3371 if (auth_data->tries > IEEE80211_AUTH_MAX_TRIES) {
3372 sdata_info(sdata, "authentication with %pM timed out\n",
3373 auth_data->bss->bssid);
3376 * Most likely AP is not in the range so remove the
3377 * bss struct for that AP.
3379 cfg80211_unlink_bss(local->hw.wiphy, auth_data->bss);
3381 return -ETIMEDOUT;
3384 drv_mgd_prepare_tx(local, sdata);
3386 if (auth_data->bss->proberesp_ies) {
3387 u16 trans = 1;
3388 u16 status = 0;
3390 sdata_info(sdata, "send auth to %pM (try %d/%d)\n",
3391 auth_data->bss->bssid, auth_data->tries,
3392 IEEE80211_AUTH_MAX_TRIES);
3394 auth_data->expected_transaction = 2;
3396 if (auth_data->algorithm == WLAN_AUTH_SAE) {
3397 trans = auth_data->sae_trans;
3398 status = auth_data->sae_status;
3399 auth_data->expected_transaction = trans;
3402 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
3403 tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
3404 IEEE80211_TX_INTFL_MLME_CONN_TX;
3406 ieee80211_send_auth(sdata, trans, auth_data->algorithm, status,
3407 auth_data->data, auth_data->data_len,
3408 auth_data->bss->bssid,
3409 auth_data->bss->bssid, NULL, 0, 0,
3410 tx_flags);
3411 } else {
3412 const u8 *ssidie;
3414 sdata_info(sdata, "direct probe to %pM (try %d/%i)\n",
3415 auth_data->bss->bssid, auth_data->tries,
3416 IEEE80211_AUTH_MAX_TRIES);
3418 rcu_read_lock();
3419 ssidie = ieee80211_bss_get_ie(auth_data->bss, WLAN_EID_SSID);
3420 if (!ssidie) {
3421 rcu_read_unlock();
3422 return -EINVAL;
3425 * Direct probe is sent to broadcast address as some APs
3426 * will not answer to direct packet in unassociated state.
3428 ieee80211_send_probe_req(sdata, NULL, ssidie + 2, ssidie[1],
3429 NULL, 0, (u32) -1, true, 0,
3430 auth_data->bss->channel, false);
3431 rcu_read_unlock();
3434 if (tx_flags == 0) {
3435 auth_data->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
3436 auth_data->timeout_started = true;
3437 run_again(sdata, auth_data->timeout);
3438 } else {
3439 auth_data->timeout =
3440 round_jiffies_up(jiffies + IEEE80211_AUTH_TIMEOUT_LONG);
3441 auth_data->timeout_started = true;
3442 run_again(sdata, auth_data->timeout);
3445 return 0;
3448 static int ieee80211_do_assoc(struct ieee80211_sub_if_data *sdata)
3450 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
3451 struct ieee80211_local *local = sdata->local;
3453 sdata_assert_lock(sdata);
3455 assoc_data->tries++;
3456 if (assoc_data->tries > IEEE80211_ASSOC_MAX_TRIES) {
3457 sdata_info(sdata, "association with %pM timed out\n",
3458 assoc_data->bss->bssid);
3461 * Most likely AP is not in the range so remove the
3462 * bss struct for that AP.
3464 cfg80211_unlink_bss(local->hw.wiphy, assoc_data->bss);
3466 return -ETIMEDOUT;
3469 sdata_info(sdata, "associate with %pM (try %d/%d)\n",
3470 assoc_data->bss->bssid, assoc_data->tries,
3471 IEEE80211_ASSOC_MAX_TRIES);
3472 ieee80211_send_assoc(sdata);
3474 if (!(local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)) {
3475 assoc_data->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
3476 assoc_data->timeout_started = true;
3477 run_again(sdata, assoc_data->timeout);
3478 } else {
3479 assoc_data->timeout =
3480 round_jiffies_up(jiffies +
3481 IEEE80211_ASSOC_TIMEOUT_LONG);
3482 assoc_data->timeout_started = true;
3483 run_again(sdata, assoc_data->timeout);
3486 return 0;
3489 void ieee80211_mgd_conn_tx_status(struct ieee80211_sub_if_data *sdata,
3490 __le16 fc, bool acked)
3492 struct ieee80211_local *local = sdata->local;
3494 sdata->u.mgd.status_fc = fc;
3495 sdata->u.mgd.status_acked = acked;
3496 sdata->u.mgd.status_received = true;
3498 ieee80211_queue_work(&local->hw, &sdata->work);
3501 void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
3503 struct ieee80211_local *local = sdata->local;
3504 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3506 sdata_lock(sdata);
3508 if (ifmgd->status_received) {
3509 __le16 fc = ifmgd->status_fc;
3510 bool status_acked = ifmgd->status_acked;
3512 ifmgd->status_received = false;
3513 if (ifmgd->auth_data &&
3514 (ieee80211_is_probe_req(fc) || ieee80211_is_auth(fc))) {
3515 if (status_acked) {
3516 ifmgd->auth_data->timeout =
3517 jiffies + IEEE80211_AUTH_TIMEOUT_SHORT;
3518 run_again(sdata, ifmgd->auth_data->timeout);
3519 } else {
3520 ifmgd->auth_data->timeout = jiffies - 1;
3522 ifmgd->auth_data->timeout_started = true;
3523 } else if (ifmgd->assoc_data &&
3524 (ieee80211_is_assoc_req(fc) ||
3525 ieee80211_is_reassoc_req(fc))) {
3526 if (status_acked) {
3527 ifmgd->assoc_data->timeout =
3528 jiffies + IEEE80211_ASSOC_TIMEOUT_SHORT;
3529 run_again(sdata, ifmgd->assoc_data->timeout);
3530 } else {
3531 ifmgd->assoc_data->timeout = jiffies - 1;
3533 ifmgd->assoc_data->timeout_started = true;
3537 if (ifmgd->auth_data && ifmgd->auth_data->timeout_started &&
3538 time_after(jiffies, ifmgd->auth_data->timeout)) {
3539 if (ifmgd->auth_data->done) {
3541 * ok ... we waited for assoc but userspace didn't,
3542 * so let's just kill the auth data
3544 ieee80211_destroy_auth_data(sdata, false);
3545 } else if (ieee80211_probe_auth(sdata)) {
3546 u8 bssid[ETH_ALEN];
3548 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
3550 ieee80211_destroy_auth_data(sdata, false);
3552 cfg80211_auth_timeout(sdata->dev, bssid);
3554 } else if (ifmgd->auth_data && ifmgd->auth_data->timeout_started)
3555 run_again(sdata, ifmgd->auth_data->timeout);
3557 if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started &&
3558 time_after(jiffies, ifmgd->assoc_data->timeout)) {
3559 if ((ifmgd->assoc_data->need_beacon && !ifmgd->have_beacon) ||
3560 ieee80211_do_assoc(sdata)) {
3561 struct cfg80211_bss *bss = ifmgd->assoc_data->bss;
3563 ieee80211_destroy_assoc_data(sdata, false);
3564 cfg80211_assoc_timeout(sdata->dev, bss);
3566 } else if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started)
3567 run_again(sdata, ifmgd->assoc_data->timeout);
3569 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
3570 IEEE80211_STA_CONNECTION_POLL) &&
3571 ifmgd->associated) {
3572 u8 bssid[ETH_ALEN];
3573 int max_tries;
3575 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
3577 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
3578 max_tries = max_nullfunc_tries;
3579 else
3580 max_tries = max_probe_tries;
3582 /* ACK received for nullfunc probing frame */
3583 if (!ifmgd->probe_send_count)
3584 ieee80211_reset_ap_probe(sdata);
3585 else if (ifmgd->nullfunc_failed) {
3586 if (ifmgd->probe_send_count < max_tries) {
3587 mlme_dbg(sdata,
3588 "No ack for nullfunc frame to AP %pM, try %d/%i\n",
3589 bssid, ifmgd->probe_send_count,
3590 max_tries);
3591 ieee80211_mgd_probe_ap_send(sdata);
3592 } else {
3593 mlme_dbg(sdata,
3594 "No ack for nullfunc frame to AP %pM, disconnecting.\n",
3595 bssid);
3596 ieee80211_sta_connection_lost(sdata, bssid,
3597 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
3598 false);
3600 } else if (time_is_after_jiffies(ifmgd->probe_timeout))
3601 run_again(sdata, ifmgd->probe_timeout);
3602 else if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
3603 mlme_dbg(sdata,
3604 "Failed to send nullfunc to AP %pM after %dms, disconnecting\n",
3605 bssid, probe_wait_ms);
3606 ieee80211_sta_connection_lost(sdata, bssid,
3607 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
3608 } else if (ifmgd->probe_send_count < max_tries) {
3609 mlme_dbg(sdata,
3610 "No probe response from AP %pM after %dms, try %d/%i\n",
3611 bssid, probe_wait_ms,
3612 ifmgd->probe_send_count, max_tries);
3613 ieee80211_mgd_probe_ap_send(sdata);
3614 } else {
3616 * We actually lost the connection ... or did we?
3617 * Let's make sure!
3619 wiphy_debug(local->hw.wiphy,
3620 "%s: No probe response from AP %pM"
3621 " after %dms, disconnecting.\n",
3622 sdata->name,
3623 bssid, probe_wait_ms);
3625 ieee80211_sta_connection_lost(sdata, bssid,
3626 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
3630 sdata_unlock(sdata);
3633 static void ieee80211_sta_bcn_mon_timer(unsigned long data)
3635 struct ieee80211_sub_if_data *sdata =
3636 (struct ieee80211_sub_if_data *) data;
3637 struct ieee80211_local *local = sdata->local;
3639 if (local->quiescing)
3640 return;
3642 sdata->u.mgd.connection_loss = false;
3643 ieee80211_queue_work(&sdata->local->hw,
3644 &sdata->u.mgd.beacon_connection_loss_work);
3647 static void ieee80211_sta_conn_mon_timer(unsigned long data)
3649 struct ieee80211_sub_if_data *sdata =
3650 (struct ieee80211_sub_if_data *) data;
3651 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3652 struct ieee80211_local *local = sdata->local;
3654 if (local->quiescing)
3655 return;
3657 ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
3660 static void ieee80211_sta_monitor_work(struct work_struct *work)
3662 struct ieee80211_sub_if_data *sdata =
3663 container_of(work, struct ieee80211_sub_if_data,
3664 u.mgd.monitor_work);
3666 ieee80211_mgd_probe_ap(sdata, false);
3669 static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
3671 u32 flags;
3673 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
3674 __ieee80211_stop_poll(sdata);
3676 /* let's probe the connection once */
3677 flags = sdata->local->hw.flags;
3678 if (!(flags & IEEE80211_HW_CONNECTION_MONITOR))
3679 ieee80211_queue_work(&sdata->local->hw,
3680 &sdata->u.mgd.monitor_work);
3681 /* and do all the other regular work too */
3682 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
3686 #ifdef CONFIG_PM
3687 void ieee80211_mgd_quiesce(struct ieee80211_sub_if_data *sdata)
3689 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3690 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
3692 sdata_lock(sdata);
3694 if (ifmgd->auth_data || ifmgd->assoc_data) {
3695 const u8 *bssid = ifmgd->auth_data ?
3696 ifmgd->auth_data->bss->bssid :
3697 ifmgd->assoc_data->bss->bssid;
3700 * If we are trying to authenticate / associate while suspending,
3701 * cfg80211 won't know and won't actually abort those attempts,
3702 * thus we need to do that ourselves.
3704 ieee80211_send_deauth_disassoc(sdata, bssid,
3705 IEEE80211_STYPE_DEAUTH,
3706 WLAN_REASON_DEAUTH_LEAVING,
3707 false, frame_buf);
3708 if (ifmgd->assoc_data)
3709 ieee80211_destroy_assoc_data(sdata, false);
3710 if (ifmgd->auth_data)
3711 ieee80211_destroy_auth_data(sdata, false);
3712 cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
3713 IEEE80211_DEAUTH_FRAME_LEN);
3716 sdata_unlock(sdata);
3719 void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
3721 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3723 sdata_lock(sdata);
3724 if (!ifmgd->associated) {
3725 sdata_unlock(sdata);
3726 return;
3729 if (sdata->flags & IEEE80211_SDATA_DISCONNECT_RESUME) {
3730 sdata->flags &= ~IEEE80211_SDATA_DISCONNECT_RESUME;
3731 mlme_dbg(sdata, "driver requested disconnect after resume\n");
3732 ieee80211_sta_connection_lost(sdata,
3733 ifmgd->associated->bssid,
3734 WLAN_REASON_UNSPECIFIED,
3735 true);
3736 sdata_unlock(sdata);
3737 return;
3739 sdata_unlock(sdata);
3741 #endif
3743 /* interface setup */
3744 void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
3746 struct ieee80211_if_managed *ifmgd;
3748 ifmgd = &sdata->u.mgd;
3749 INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
3750 INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
3751 INIT_WORK(&ifmgd->beacon_connection_loss_work,
3752 ieee80211_beacon_connection_loss_work);
3753 INIT_WORK(&ifmgd->csa_connection_drop_work,
3754 ieee80211_csa_connection_drop_work);
3755 INIT_WORK(&ifmgd->request_smps_work, ieee80211_request_smps_work);
3756 setup_timer(&ifmgd->timer, ieee80211_sta_timer,
3757 (unsigned long) sdata);
3758 setup_timer(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer,
3759 (unsigned long) sdata);
3760 setup_timer(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer,
3761 (unsigned long) sdata);
3762 setup_timer(&ifmgd->chswitch_timer, ieee80211_chswitch_timer,
3763 (unsigned long) sdata);
3765 ifmgd->flags = 0;
3766 ifmgd->powersave = sdata->wdev.ps;
3767 ifmgd->uapsd_queues = sdata->local->hw.uapsd_queues;
3768 ifmgd->uapsd_max_sp_len = sdata->local->hw.uapsd_max_sp_len;
3769 ifmgd->p2p_noa_index = -1;
3771 if (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS)
3772 ifmgd->req_smps = IEEE80211_SMPS_AUTOMATIC;
3773 else
3774 ifmgd->req_smps = IEEE80211_SMPS_OFF;
3777 /* scan finished notification */
3778 void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
3780 struct ieee80211_sub_if_data *sdata;
3782 /* Restart STA timers */
3783 rcu_read_lock();
3784 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
3785 if (ieee80211_sdata_running(sdata))
3786 ieee80211_restart_sta_timer(sdata);
3788 rcu_read_unlock();
3791 int ieee80211_max_network_latency(struct notifier_block *nb,
3792 unsigned long data, void *dummy)
3794 s32 latency_usec = (s32) data;
3795 struct ieee80211_local *local =
3796 container_of(nb, struct ieee80211_local,
3797 network_latency_notifier);
3799 mutex_lock(&local->iflist_mtx);
3800 ieee80211_recalc_ps(local, latency_usec);
3801 mutex_unlock(&local->iflist_mtx);
3803 return 0;
3806 static u8 ieee80211_ht_vht_rx_chains(struct ieee80211_sub_if_data *sdata,
3807 struct cfg80211_bss *cbss)
3809 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3810 const u8 *ht_cap_ie, *vht_cap_ie;
3811 const struct ieee80211_ht_cap *ht_cap;
3812 const struct ieee80211_vht_cap *vht_cap;
3813 u8 chains = 1;
3815 if (ifmgd->flags & IEEE80211_STA_DISABLE_HT)
3816 return chains;
3818 ht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
3819 if (ht_cap_ie && ht_cap_ie[1] >= sizeof(*ht_cap)) {
3820 ht_cap = (void *)(ht_cap_ie + 2);
3821 chains = ieee80211_mcs_to_chains(&ht_cap->mcs);
3823 * TODO: use "Tx Maximum Number Spatial Streams Supported" and
3824 * "Tx Unequal Modulation Supported" fields.
3828 if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
3829 return chains;
3831 vht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
3832 if (vht_cap_ie && vht_cap_ie[1] >= sizeof(*vht_cap)) {
3833 u8 nss;
3834 u16 tx_mcs_map;
3836 vht_cap = (void *)(vht_cap_ie + 2);
3837 tx_mcs_map = le16_to_cpu(vht_cap->supp_mcs.tx_mcs_map);
3838 for (nss = 8; nss > 0; nss--) {
3839 if (((tx_mcs_map >> (2 * (nss - 1))) & 3) !=
3840 IEEE80211_VHT_MCS_NOT_SUPPORTED)
3841 break;
3843 /* TODO: use "Tx Highest Supported Long GI Data Rate" field? */
3844 chains = max(chains, nss);
3847 return chains;
3850 static int ieee80211_prep_channel(struct ieee80211_sub_if_data *sdata,
3851 struct cfg80211_bss *cbss)
3853 struct ieee80211_local *local = sdata->local;
3854 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3855 const struct ieee80211_ht_operation *ht_oper = NULL;
3856 const struct ieee80211_vht_operation *vht_oper = NULL;
3857 struct ieee80211_supported_band *sband;
3858 struct cfg80211_chan_def chandef;
3859 int ret;
3861 sband = local->hw.wiphy->bands[cbss->channel->band];
3863 ifmgd->flags &= ~(IEEE80211_STA_DISABLE_40MHZ |
3864 IEEE80211_STA_DISABLE_80P80MHZ |
3865 IEEE80211_STA_DISABLE_160MHZ);
3867 rcu_read_lock();
3869 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
3870 sband->ht_cap.ht_supported) {
3871 const u8 *ht_oper_ie, *ht_cap;
3873 ht_oper_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_OPERATION);
3874 if (ht_oper_ie && ht_oper_ie[1] >= sizeof(*ht_oper))
3875 ht_oper = (void *)(ht_oper_ie + 2);
3877 ht_cap = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
3878 if (!ht_cap || ht_cap[1] < sizeof(struct ieee80211_ht_cap)) {
3879 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
3880 ht_oper = NULL;
3884 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
3885 sband->vht_cap.vht_supported) {
3886 const u8 *vht_oper_ie, *vht_cap;
3888 vht_oper_ie = ieee80211_bss_get_ie(cbss,
3889 WLAN_EID_VHT_OPERATION);
3890 if (vht_oper_ie && vht_oper_ie[1] >= sizeof(*vht_oper))
3891 vht_oper = (void *)(vht_oper_ie + 2);
3892 if (vht_oper && !ht_oper) {
3893 vht_oper = NULL;
3894 sdata_info(sdata,
3895 "AP advertised VHT without HT, disabling both\n");
3896 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
3897 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
3900 vht_cap = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
3901 if (!vht_cap || vht_cap[1] < sizeof(struct ieee80211_vht_cap)) {
3902 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
3903 vht_oper = NULL;
3907 ifmgd->flags |= ieee80211_determine_chantype(sdata, sband,
3908 cbss->channel,
3909 ht_oper, vht_oper,
3910 &chandef, false);
3912 sdata->needed_rx_chains = min(ieee80211_ht_vht_rx_chains(sdata, cbss),
3913 local->rx_chains);
3915 rcu_read_unlock();
3917 /* will change later if needed */
3918 sdata->smps_mode = IEEE80211_SMPS_OFF;
3921 * If this fails (possibly due to channel context sharing
3922 * on incompatible channels, e.g. 80+80 and 160 sharing the
3923 * same control channel) try to use a smaller bandwidth.
3925 ret = ieee80211_vif_use_channel(sdata, &chandef,
3926 IEEE80211_CHANCTX_SHARED);
3928 /* don't downgrade for 5 and 10 MHz channels, though. */
3929 if (chandef.width == NL80211_CHAN_WIDTH_5 ||
3930 chandef.width == NL80211_CHAN_WIDTH_10)
3931 return ret;
3933 while (ret && chandef.width != NL80211_CHAN_WIDTH_20_NOHT) {
3934 ifmgd->flags |= chandef_downgrade(&chandef);
3935 ret = ieee80211_vif_use_channel(sdata, &chandef,
3936 IEEE80211_CHANCTX_SHARED);
3938 return ret;
3941 static int ieee80211_prep_connection(struct ieee80211_sub_if_data *sdata,
3942 struct cfg80211_bss *cbss, bool assoc)
3944 struct ieee80211_local *local = sdata->local;
3945 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3946 struct ieee80211_bss *bss = (void *)cbss->priv;
3947 struct sta_info *new_sta = NULL;
3948 bool have_sta = false;
3949 int err;
3951 if (WARN_ON(!ifmgd->auth_data && !ifmgd->assoc_data))
3952 return -EINVAL;
3954 if (assoc) {
3955 rcu_read_lock();
3956 have_sta = sta_info_get(sdata, cbss->bssid);
3957 rcu_read_unlock();
3960 if (!have_sta) {
3961 new_sta = sta_info_alloc(sdata, cbss->bssid, GFP_KERNEL);
3962 if (!new_sta)
3963 return -ENOMEM;
3965 if (new_sta) {
3966 u32 rates = 0, basic_rates = 0;
3967 bool have_higher_than_11mbit;
3968 int min_rate = INT_MAX, min_rate_index = -1;
3969 struct ieee80211_chanctx_conf *chanctx_conf;
3970 struct ieee80211_supported_band *sband;
3971 const struct cfg80211_bss_ies *ies;
3972 int shift;
3973 u32 rate_flags;
3975 sband = local->hw.wiphy->bands[cbss->channel->band];
3977 err = ieee80211_prep_channel(sdata, cbss);
3978 if (err) {
3979 sta_info_free(local, new_sta);
3980 return -EINVAL;
3982 shift = ieee80211_vif_get_shift(&sdata->vif);
3984 rcu_read_lock();
3985 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
3986 if (WARN_ON(!chanctx_conf)) {
3987 rcu_read_unlock();
3988 return -EINVAL;
3990 rate_flags = ieee80211_chandef_rate_flags(&chanctx_conf->def);
3991 rcu_read_unlock();
3993 ieee80211_get_rates(sband, bss->supp_rates,
3994 bss->supp_rates_len,
3995 &rates, &basic_rates,
3996 &have_higher_than_11mbit,
3997 &min_rate, &min_rate_index,
3998 shift, rate_flags);
4001 * This used to be a workaround for basic rates missing
4002 * in the association response frame. Now that we no
4003 * longer use the basic rates from there, it probably
4004 * doesn't happen any more, but keep the workaround so
4005 * in case some *other* APs are buggy in different ways
4006 * we can connect -- with a warning.
4008 if (!basic_rates && min_rate_index >= 0) {
4009 sdata_info(sdata,
4010 "No basic rates, using min rate instead\n");
4011 basic_rates = BIT(min_rate_index);
4014 new_sta->sta.supp_rates[cbss->channel->band] = rates;
4015 sdata->vif.bss_conf.basic_rates = basic_rates;
4017 /* cf. IEEE 802.11 9.2.12 */
4018 if (cbss->channel->band == IEEE80211_BAND_2GHZ &&
4019 have_higher_than_11mbit)
4020 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
4021 else
4022 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
4024 memcpy(ifmgd->bssid, cbss->bssid, ETH_ALEN);
4026 /* set timing information */
4027 sdata->vif.bss_conf.beacon_int = cbss->beacon_interval;
4028 rcu_read_lock();
4029 ies = rcu_dereference(cbss->beacon_ies);
4030 if (ies) {
4031 const u8 *tim_ie;
4033 sdata->vif.bss_conf.sync_tsf = ies->tsf;
4034 sdata->vif.bss_conf.sync_device_ts =
4035 bss->device_ts_beacon;
4036 tim_ie = cfg80211_find_ie(WLAN_EID_TIM,
4037 ies->data, ies->len);
4038 if (tim_ie && tim_ie[1] >= 2)
4039 sdata->vif.bss_conf.sync_dtim_count = tim_ie[2];
4040 else
4041 sdata->vif.bss_conf.sync_dtim_count = 0;
4042 } else if (!(local->hw.flags &
4043 IEEE80211_HW_TIMING_BEACON_ONLY)) {
4044 ies = rcu_dereference(cbss->proberesp_ies);
4045 /* must be non-NULL since beacon IEs were NULL */
4046 sdata->vif.bss_conf.sync_tsf = ies->tsf;
4047 sdata->vif.bss_conf.sync_device_ts =
4048 bss->device_ts_presp;
4049 sdata->vif.bss_conf.sync_dtim_count = 0;
4050 } else {
4051 sdata->vif.bss_conf.sync_tsf = 0;
4052 sdata->vif.bss_conf.sync_device_ts = 0;
4053 sdata->vif.bss_conf.sync_dtim_count = 0;
4055 rcu_read_unlock();
4057 /* tell driver about BSSID, basic rates and timing */
4058 ieee80211_bss_info_change_notify(sdata,
4059 BSS_CHANGED_BSSID | BSS_CHANGED_BASIC_RATES |
4060 BSS_CHANGED_BEACON_INT);
4062 if (assoc)
4063 sta_info_pre_move_state(new_sta, IEEE80211_STA_AUTH);
4065 err = sta_info_insert(new_sta);
4066 new_sta = NULL;
4067 if (err) {
4068 sdata_info(sdata,
4069 "failed to insert STA entry for the AP (error %d)\n",
4070 err);
4071 return err;
4073 } else
4074 WARN_ON_ONCE(!ether_addr_equal(ifmgd->bssid, cbss->bssid));
4076 return 0;
4079 /* config hooks */
4080 int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
4081 struct cfg80211_auth_request *req)
4083 struct ieee80211_local *local = sdata->local;
4084 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4085 struct ieee80211_mgd_auth_data *auth_data;
4086 u16 auth_alg;
4087 int err;
4089 /* prepare auth data structure */
4091 switch (req->auth_type) {
4092 case NL80211_AUTHTYPE_OPEN_SYSTEM:
4093 auth_alg = WLAN_AUTH_OPEN;
4094 break;
4095 case NL80211_AUTHTYPE_SHARED_KEY:
4096 if (IS_ERR(local->wep_tx_tfm))
4097 return -EOPNOTSUPP;
4098 auth_alg = WLAN_AUTH_SHARED_KEY;
4099 break;
4100 case NL80211_AUTHTYPE_FT:
4101 auth_alg = WLAN_AUTH_FT;
4102 break;
4103 case NL80211_AUTHTYPE_NETWORK_EAP:
4104 auth_alg = WLAN_AUTH_LEAP;
4105 break;
4106 case NL80211_AUTHTYPE_SAE:
4107 auth_alg = WLAN_AUTH_SAE;
4108 break;
4109 default:
4110 return -EOPNOTSUPP;
4113 auth_data = kzalloc(sizeof(*auth_data) + req->sae_data_len +
4114 req->ie_len, GFP_KERNEL);
4115 if (!auth_data)
4116 return -ENOMEM;
4118 auth_data->bss = req->bss;
4120 if (req->sae_data_len >= 4) {
4121 __le16 *pos = (__le16 *) req->sae_data;
4122 auth_data->sae_trans = le16_to_cpu(pos[0]);
4123 auth_data->sae_status = le16_to_cpu(pos[1]);
4124 memcpy(auth_data->data, req->sae_data + 4,
4125 req->sae_data_len - 4);
4126 auth_data->data_len += req->sae_data_len - 4;
4129 if (req->ie && req->ie_len) {
4130 memcpy(&auth_data->data[auth_data->data_len],
4131 req->ie, req->ie_len);
4132 auth_data->data_len += req->ie_len;
4135 if (req->key && req->key_len) {
4136 auth_data->key_len = req->key_len;
4137 auth_data->key_idx = req->key_idx;
4138 memcpy(auth_data->key, req->key, req->key_len);
4141 auth_data->algorithm = auth_alg;
4143 /* try to authenticate/probe */
4145 if ((ifmgd->auth_data && !ifmgd->auth_data->done) ||
4146 ifmgd->assoc_data) {
4147 err = -EBUSY;
4148 goto err_free;
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,
4162 false, frame_buf);
4164 cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
4165 sizeof(frame_buf));
4168 sdata_info(sdata, "authenticate with %pM\n", req->bss->bssid);
4170 err = ieee80211_prep_connection(sdata, req->bss, false);
4171 if (err)
4172 goto err_clear;
4174 err = ieee80211_probe_auth(sdata);
4175 if (err) {
4176 sta_info_destroy_addr(sdata, req->bss->bssid);
4177 goto err_clear;
4180 /* hold our own reference */
4181 cfg80211_ref_bss(local->hw.wiphy, auth_data->bss);
4182 return 0;
4184 err_clear:
4185 memset(ifmgd->bssid, 0, ETH_ALEN);
4186 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
4187 ifmgd->auth_data = NULL;
4188 err_free:
4189 kfree(auth_data);
4190 return err;
4193 int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
4194 struct cfg80211_assoc_request *req)
4196 struct ieee80211_local *local = sdata->local;
4197 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4198 struct ieee80211_bss *bss = (void *)req->bss->priv;
4199 struct ieee80211_mgd_assoc_data *assoc_data;
4200 const struct cfg80211_bss_ies *beacon_ies;
4201 struct ieee80211_supported_band *sband;
4202 const u8 *ssidie, *ht_ie, *vht_ie;
4203 int i, err;
4205 assoc_data = kzalloc(sizeof(*assoc_data) + req->ie_len, GFP_KERNEL);
4206 if (!assoc_data)
4207 return -ENOMEM;
4209 rcu_read_lock();
4210 ssidie = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
4211 if (!ssidie) {
4212 rcu_read_unlock();
4213 kfree(assoc_data);
4214 return -EINVAL;
4216 memcpy(assoc_data->ssid, ssidie + 2, ssidie[1]);
4217 assoc_data->ssid_len = ssidie[1];
4218 rcu_read_unlock();
4220 if (ifmgd->associated) {
4221 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
4223 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
4224 WLAN_REASON_UNSPECIFIED,
4225 false, frame_buf);
4227 cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
4228 sizeof(frame_buf));
4231 if (ifmgd->auth_data && !ifmgd->auth_data->done) {
4232 err = -EBUSY;
4233 goto err_free;
4236 if (ifmgd->assoc_data) {
4237 err = -EBUSY;
4238 goto err_free;
4241 if (ifmgd->auth_data) {
4242 bool match;
4244 /* keep sta info, bssid if matching */
4245 match = ether_addr_equal(ifmgd->bssid, req->bss->bssid);
4246 ieee80211_destroy_auth_data(sdata, match);
4249 /* prepare assoc data */
4251 ifmgd->beacon_crc_valid = false;
4254 * IEEE802.11n does not allow TKIP/WEP as pairwise ciphers in HT mode.
4255 * We still associate in non-HT mode (11a/b/g) if any one of these
4256 * ciphers is configured as pairwise.
4257 * We can set this to true for non-11n hardware, that'll be checked
4258 * separately along with the peer capabilities.
4260 for (i = 0; i < req->crypto.n_ciphers_pairwise; i++) {
4261 if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
4262 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
4263 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104) {
4264 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
4265 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
4266 netdev_info(sdata->dev,
4267 "disabling HT/VHT due to WEP/TKIP use\n");
4271 if (req->flags & ASSOC_REQ_DISABLE_HT) {
4272 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
4273 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
4276 if (req->flags & ASSOC_REQ_DISABLE_VHT)
4277 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
4279 /* Also disable HT if we don't support it or the AP doesn't use WMM */
4280 sband = local->hw.wiphy->bands[req->bss->channel->band];
4281 if (!sband->ht_cap.ht_supported ||
4282 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used) {
4283 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
4284 if (!bss->wmm_used)
4285 netdev_info(sdata->dev,
4286 "disabling HT as WMM/QoS is not supported by the AP\n");
4289 /* disable VHT if we don't support it or the AP doesn't use WMM */
4290 if (!sband->vht_cap.vht_supported ||
4291 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used) {
4292 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
4293 if (!bss->wmm_used)
4294 netdev_info(sdata->dev,
4295 "disabling VHT as WMM/QoS is not supported by the AP\n");
4298 memcpy(&ifmgd->ht_capa, &req->ht_capa, sizeof(ifmgd->ht_capa));
4299 memcpy(&ifmgd->ht_capa_mask, &req->ht_capa_mask,
4300 sizeof(ifmgd->ht_capa_mask));
4302 memcpy(&ifmgd->vht_capa, &req->vht_capa, sizeof(ifmgd->vht_capa));
4303 memcpy(&ifmgd->vht_capa_mask, &req->vht_capa_mask,
4304 sizeof(ifmgd->vht_capa_mask));
4306 if (req->ie && req->ie_len) {
4307 memcpy(assoc_data->ie, req->ie, req->ie_len);
4308 assoc_data->ie_len = req->ie_len;
4311 assoc_data->bss = req->bss;
4313 if (ifmgd->req_smps == IEEE80211_SMPS_AUTOMATIC) {
4314 if (ifmgd->powersave)
4315 sdata->smps_mode = IEEE80211_SMPS_DYNAMIC;
4316 else
4317 sdata->smps_mode = IEEE80211_SMPS_OFF;
4318 } else
4319 sdata->smps_mode = ifmgd->req_smps;
4321 assoc_data->capability = req->bss->capability;
4322 assoc_data->wmm = bss->wmm_used &&
4323 (local->hw.queues >= IEEE80211_NUM_ACS);
4324 assoc_data->supp_rates = bss->supp_rates;
4325 assoc_data->supp_rates_len = bss->supp_rates_len;
4327 rcu_read_lock();
4328 ht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_HT_OPERATION);
4329 if (ht_ie && ht_ie[1] >= sizeof(struct ieee80211_ht_operation))
4330 assoc_data->ap_ht_param =
4331 ((struct ieee80211_ht_operation *)(ht_ie + 2))->ht_param;
4332 else
4333 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
4334 vht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_VHT_CAPABILITY);
4335 if (vht_ie && vht_ie[1] >= sizeof(struct ieee80211_vht_cap))
4336 memcpy(&assoc_data->ap_vht_cap, vht_ie + 2,
4337 sizeof(struct ieee80211_vht_cap));
4338 else
4339 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
4340 rcu_read_unlock();
4342 if (bss->wmm_used && bss->uapsd_supported &&
4343 (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_UAPSD) &&
4344 sdata->wmm_acm != 0xff) {
4345 assoc_data->uapsd = true;
4346 ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED;
4347 } else {
4348 assoc_data->uapsd = false;
4349 ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED;
4352 if (req->prev_bssid)
4353 memcpy(assoc_data->prev_bssid, req->prev_bssid, ETH_ALEN);
4355 if (req->use_mfp) {
4356 ifmgd->mfp = IEEE80211_MFP_REQUIRED;
4357 ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
4358 } else {
4359 ifmgd->mfp = IEEE80211_MFP_DISABLED;
4360 ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
4363 if (req->crypto.control_port)
4364 ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
4365 else
4366 ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
4368 sdata->control_port_protocol = req->crypto.control_port_ethertype;
4369 sdata->control_port_no_encrypt = req->crypto.control_port_no_encrypt;
4371 /* kick off associate process */
4373 ifmgd->assoc_data = assoc_data;
4374 ifmgd->dtim_period = 0;
4375 ifmgd->have_beacon = false;
4377 err = ieee80211_prep_connection(sdata, req->bss, true);
4378 if (err)
4379 goto err_clear;
4381 rcu_read_lock();
4382 beacon_ies = rcu_dereference(req->bss->beacon_ies);
4384 if (sdata->local->hw.flags & IEEE80211_HW_NEED_DTIM_BEFORE_ASSOC &&
4385 !beacon_ies) {
4387 * Wait up to one beacon interval ...
4388 * should this be more if we miss one?
4390 sdata_info(sdata, "waiting for beacon from %pM\n",
4391 ifmgd->bssid);
4392 assoc_data->timeout = TU_TO_EXP_TIME(req->bss->beacon_interval);
4393 assoc_data->timeout_started = true;
4394 assoc_data->need_beacon = true;
4395 } else if (beacon_ies) {
4396 const u8 *tim_ie = cfg80211_find_ie(WLAN_EID_TIM,
4397 beacon_ies->data,
4398 beacon_ies->len);
4399 u8 dtim_count = 0;
4401 if (tim_ie && tim_ie[1] >= sizeof(struct ieee80211_tim_ie)) {
4402 const struct ieee80211_tim_ie *tim;
4403 tim = (void *)(tim_ie + 2);
4404 ifmgd->dtim_period = tim->dtim_period;
4405 dtim_count = tim->dtim_count;
4407 ifmgd->have_beacon = true;
4408 assoc_data->timeout = jiffies;
4409 assoc_data->timeout_started = true;
4411 if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
4412 sdata->vif.bss_conf.sync_tsf = beacon_ies->tsf;
4413 sdata->vif.bss_conf.sync_device_ts =
4414 bss->device_ts_beacon;
4415 sdata->vif.bss_conf.sync_dtim_count = dtim_count;
4417 } else {
4418 assoc_data->timeout = jiffies;
4419 assoc_data->timeout_started = true;
4421 rcu_read_unlock();
4423 run_again(sdata, assoc_data->timeout);
4425 if (bss->corrupt_data) {
4426 char *corrupt_type = "data";
4427 if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_BEACON) {
4428 if (bss->corrupt_data &
4429 IEEE80211_BSS_CORRUPT_PROBE_RESP)
4430 corrupt_type = "beacon and probe response";
4431 else
4432 corrupt_type = "beacon";
4433 } else if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_PROBE_RESP)
4434 corrupt_type = "probe response";
4435 sdata_info(sdata, "associating with AP with corrupt %s\n",
4436 corrupt_type);
4439 return 0;
4440 err_clear:
4441 memset(ifmgd->bssid, 0, ETH_ALEN);
4442 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
4443 ifmgd->assoc_data = NULL;
4444 err_free:
4445 kfree(assoc_data);
4446 return err;
4449 int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
4450 struct cfg80211_deauth_request *req)
4452 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4453 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
4454 bool tx = !req->local_state_change;
4455 bool report_frame = false;
4457 sdata_info(sdata,
4458 "deauthenticating from %pM by local choice (reason=%d)\n",
4459 req->bssid, req->reason_code);
4461 if (ifmgd->auth_data) {
4462 drv_mgd_prepare_tx(sdata->local, sdata);
4463 ieee80211_send_deauth_disassoc(sdata, req->bssid,
4464 IEEE80211_STYPE_DEAUTH,
4465 req->reason_code, tx,
4466 frame_buf);
4467 ieee80211_destroy_auth_data(sdata, false);
4469 report_frame = true;
4470 goto out;
4473 if (ifmgd->associated &&
4474 ether_addr_equal(ifmgd->associated->bssid, req->bssid)) {
4475 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
4476 req->reason_code, tx, frame_buf);
4477 report_frame = true;
4480 out:
4481 if (report_frame)
4482 cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
4483 IEEE80211_DEAUTH_FRAME_LEN);
4485 return 0;
4488 int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
4489 struct cfg80211_disassoc_request *req)
4491 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4492 u8 bssid[ETH_ALEN];
4493 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
4496 * cfg80211 should catch this ... but it's racy since
4497 * we can receive a disassoc frame, process it, hand it
4498 * to cfg80211 while that's in a locked section already
4499 * trying to tell us that the user wants to disconnect.
4501 if (ifmgd->associated != req->bss)
4502 return -ENOLINK;
4504 sdata_info(sdata,
4505 "disassociating from %pM by local choice (reason=%d)\n",
4506 req->bss->bssid, req->reason_code);
4508 memcpy(bssid, req->bss->bssid, ETH_ALEN);
4509 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DISASSOC,
4510 req->reason_code, !req->local_state_change,
4511 frame_buf);
4513 cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
4514 IEEE80211_DEAUTH_FRAME_LEN);
4516 return 0;
4519 void ieee80211_mgd_stop(struct ieee80211_sub_if_data *sdata)
4521 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4524 * Make sure some work items will not run after this,
4525 * they will not do anything but might not have been
4526 * cancelled when disconnecting.
4528 cancel_work_sync(&ifmgd->monitor_work);
4529 cancel_work_sync(&ifmgd->beacon_connection_loss_work);
4530 cancel_work_sync(&ifmgd->request_smps_work);
4531 cancel_work_sync(&ifmgd->csa_connection_drop_work);
4532 cancel_work_sync(&ifmgd->chswitch_work);
4534 sdata_lock(sdata);
4535 if (ifmgd->assoc_data) {
4536 struct cfg80211_bss *bss = ifmgd->assoc_data->bss;
4537 ieee80211_destroy_assoc_data(sdata, false);
4538 cfg80211_assoc_timeout(sdata->dev, bss);
4540 if (ifmgd->auth_data)
4541 ieee80211_destroy_auth_data(sdata, false);
4542 del_timer_sync(&ifmgd->timer);
4543 sdata_unlock(sdata);
4546 void ieee80211_cqm_rssi_notify(struct ieee80211_vif *vif,
4547 enum nl80211_cqm_rssi_threshold_event rssi_event,
4548 gfp_t gfp)
4550 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4552 trace_api_cqm_rssi_notify(sdata, rssi_event);
4554 cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, gfp);
4556 EXPORT_SYMBOL(ieee80211_cqm_rssi_notify);