]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - net/mac80211/mlme.c
6c9ebca02394abf6e6e1fa84052ba9922414b104
[mirror_ubuntu-artful-kernel.git] / net / mac80211 / mlme.c
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>
8 *
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.
12 */
13
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>
27
28 #include "ieee80211_i.h"
29 #include "driver-ops.h"
30 #include "rate.h"
31 #include "led.h"
32
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
42
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).");
47
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).");
52
53 /*
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.
60 */
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.");
65
66 /*
67 * Time the connection can be idle before we probe
68 * it to see if we can still talk to the AP.
69 */
70 #define IEEE80211_CONNECTION_IDLE_TIME (30 * HZ)
71 /*
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.
75 */
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).");
81
82 /*
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.
86 */
87 #define IEEE80211_SIGNAL_AVE_WEIGHT 3
88
89 /*
90 * How many Beacon frames need to have been used in average signal strength
91 * before starting to indicate signal change events.
92 */
93 #define IEEE80211_SIGNAL_AVE_MIN_COUNT 4
94
95 /*
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.
104 */
105 static void run_again(struct ieee80211_sub_if_data *sdata,
106 unsigned long timeout)
107 {
108 sdata_assert_lock(sdata);
109
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);
113 }
114
115 void ieee80211_sta_reset_beacon_monitor(struct ieee80211_sub_if_data *sdata)
116 {
117 if (sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)
118 return;
119
120 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
121 return;
122
123 mod_timer(&sdata->u.mgd.bcn_mon_timer,
124 round_jiffies_up(jiffies + sdata->u.mgd.beacon_timeout));
125 }
126
127 void ieee80211_sta_reset_conn_monitor(struct ieee80211_sub_if_data *sdata)
128 {
129 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
130
131 if (unlikely(!sdata->u.mgd.associated))
132 return;
133
134 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
135 return;
136
137 mod_timer(&sdata->u.mgd.conn_mon_timer,
138 round_jiffies_up(jiffies + IEEE80211_CONNECTION_IDLE_TIME));
139
140 ifmgd->probe_send_count = 0;
141 }
142
143 static int ecw2cw(int ecw)
144 {
145 return (1 << ecw) - 1;
146 }
147
148 static u32
149 ieee80211_determine_chantype(struct ieee80211_sub_if_data *sdata,
150 struct ieee80211_supported_band *sband,
151 struct ieee80211_channel *channel,
152 const struct ieee80211_ht_operation *ht_oper,
153 const struct ieee80211_vht_operation *vht_oper,
154 struct cfg80211_chan_def *chandef, bool tracking)
155 {
156 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
157 struct cfg80211_chan_def vht_chandef;
158 u32 ht_cfreq, ret;
159
160 chandef->chan = channel;
161 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
162 chandef->center_freq1 = channel->center_freq;
163 chandef->center_freq2 = 0;
164
165 if (!ht_oper || !sband->ht_cap.ht_supported) {
166 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
167 goto out;
168 }
169
170 chandef->width = NL80211_CHAN_WIDTH_20;
171
172 ht_cfreq = ieee80211_channel_to_frequency(ht_oper->primary_chan,
173 channel->band);
174 /* check that channel matches the right operating channel */
175 if (!tracking && channel->center_freq != ht_cfreq) {
176 /*
177 * It's possible that some APs are confused here;
178 * Netgear WNDR3700 sometimes reports 4 higher than
179 * the actual channel in association responses, but
180 * since we look at probe response/beacon data here
181 * it should be OK.
182 */
183 sdata_info(sdata,
184 "Wrong control channel: center-freq: %d ht-cfreq: %d ht->primary_chan: %d band: %d - Disabling HT\n",
185 channel->center_freq, ht_cfreq,
186 ht_oper->primary_chan, channel->band);
187 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
188 goto out;
189 }
190
191 /* check 40 MHz support, if we have it */
192 if (sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) {
193 switch (ht_oper->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
194 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
195 chandef->width = NL80211_CHAN_WIDTH_40;
196 chandef->center_freq1 += 10;
197 break;
198 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
199 chandef->width = NL80211_CHAN_WIDTH_40;
200 chandef->center_freq1 -= 10;
201 break;
202 }
203 } else {
204 /* 40 MHz (and 80 MHz) must be supported for VHT */
205 ret = IEEE80211_STA_DISABLE_VHT;
206 /* also mark 40 MHz disabled */
207 ret |= IEEE80211_STA_DISABLE_40MHZ;
208 goto out;
209 }
210
211 if (!vht_oper || !sband->vht_cap.vht_supported) {
212 ret = IEEE80211_STA_DISABLE_VHT;
213 goto out;
214 }
215
216 vht_chandef.chan = channel;
217 vht_chandef.center_freq1 =
218 ieee80211_channel_to_frequency(vht_oper->center_freq_seg1_idx,
219 channel->band);
220 vht_chandef.center_freq2 = 0;
221
222 switch (vht_oper->chan_width) {
223 case IEEE80211_VHT_CHANWIDTH_USE_HT:
224 vht_chandef.width = chandef->width;
225 break;
226 case IEEE80211_VHT_CHANWIDTH_80MHZ:
227 vht_chandef.width = NL80211_CHAN_WIDTH_80;
228 break;
229 case IEEE80211_VHT_CHANWIDTH_160MHZ:
230 vht_chandef.width = NL80211_CHAN_WIDTH_160;
231 break;
232 case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
233 vht_chandef.width = NL80211_CHAN_WIDTH_80P80;
234 vht_chandef.center_freq2 =
235 ieee80211_channel_to_frequency(
236 vht_oper->center_freq_seg2_idx,
237 channel->band);
238 break;
239 default:
240 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
241 sdata_info(sdata,
242 "AP VHT operation IE has invalid channel width (%d), disable VHT\n",
243 vht_oper->chan_width);
244 ret = IEEE80211_STA_DISABLE_VHT;
245 goto out;
246 }
247
248 if (!cfg80211_chandef_valid(&vht_chandef)) {
249 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
250 sdata_info(sdata,
251 "AP VHT information is invalid, disable VHT\n");
252 ret = IEEE80211_STA_DISABLE_VHT;
253 goto out;
254 }
255
256 if (cfg80211_chandef_identical(chandef, &vht_chandef)) {
257 ret = 0;
258 goto out;
259 }
260
261 if (!cfg80211_chandef_compatible(chandef, &vht_chandef)) {
262 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
263 sdata_info(sdata,
264 "AP VHT information doesn't match HT, disable VHT\n");
265 ret = IEEE80211_STA_DISABLE_VHT;
266 goto out;
267 }
268
269 *chandef = vht_chandef;
270
271 ret = 0;
272
273 out:
274 /* don't print the message below for VHT mismatch if VHT is disabled */
275 if (ret & IEEE80211_STA_DISABLE_VHT)
276 vht_chandef = *chandef;
277
278 /*
279 * Ignore the DISABLED flag when we're already connected and only
280 * tracking the APs beacon for bandwidth changes - otherwise we
281 * might get disconnected here if we connect to an AP, update our
282 * regulatory information based on the AP's country IE and the
283 * information we have is wrong/outdated and disables the channel
284 * that we're actually using for the connection to the AP.
285 */
286 while (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
287 tracking ? 0 :
288 IEEE80211_CHAN_DISABLED)) {
289 if (WARN_ON(chandef->width == NL80211_CHAN_WIDTH_20_NOHT)) {
290 ret = IEEE80211_STA_DISABLE_HT |
291 IEEE80211_STA_DISABLE_VHT;
292 break;
293 }
294
295 ret |= ieee80211_chandef_downgrade(chandef);
296 }
297
298 if (chandef->width != vht_chandef.width && !tracking)
299 sdata_info(sdata,
300 "capabilities/regulatory prevented using AP HT/VHT configuration, downgraded\n");
301
302 WARN_ON_ONCE(!cfg80211_chandef_valid(chandef));
303 return ret;
304 }
305
306 static int ieee80211_config_bw(struct ieee80211_sub_if_data *sdata,
307 struct sta_info *sta,
308 const struct ieee80211_ht_operation *ht_oper,
309 const struct ieee80211_vht_operation *vht_oper,
310 const u8 *bssid, u32 *changed)
311 {
312 struct ieee80211_local *local = sdata->local;
313 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
314 struct ieee80211_supported_band *sband;
315 struct ieee80211_channel *chan;
316 struct cfg80211_chan_def chandef;
317 u16 ht_opmode;
318 u32 flags;
319 enum ieee80211_sta_rx_bandwidth new_sta_bw;
320 int ret;
321
322 /* if HT was/is disabled, don't track any bandwidth changes */
323 if (ifmgd->flags & IEEE80211_STA_DISABLE_HT || !ht_oper)
324 return 0;
325
326 /* don't check VHT if we associated as non-VHT station */
327 if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
328 vht_oper = NULL;
329
330 if (WARN_ON_ONCE(!sta))
331 return -EINVAL;
332
333 /*
334 * if bss configuration changed store the new one -
335 * this may be applicable even if channel is identical
336 */
337 ht_opmode = le16_to_cpu(ht_oper->operation_mode);
338 if (sdata->vif.bss_conf.ht_operation_mode != ht_opmode) {
339 *changed |= BSS_CHANGED_HT;
340 sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
341 }
342
343 chan = sdata->vif.bss_conf.chandef.chan;
344 sband = local->hw.wiphy->bands[chan->band];
345
346 /* calculate new channel (type) based on HT/VHT operation IEs */
347 flags = ieee80211_determine_chantype(sdata, sband, chan, ht_oper,
348 vht_oper, &chandef, true);
349
350 /*
351 * Downgrade the new channel if we associated with restricted
352 * capabilities. For example, if we associated as a 20 MHz STA
353 * to a 40 MHz AP (due to regulatory, capabilities or config
354 * reasons) then switching to a 40 MHz channel now won't do us
355 * any good -- we couldn't use it with the AP.
356 */
357 if (ifmgd->flags & IEEE80211_STA_DISABLE_80P80MHZ &&
358 chandef.width == NL80211_CHAN_WIDTH_80P80)
359 flags |= ieee80211_chandef_downgrade(&chandef);
360 if (ifmgd->flags & IEEE80211_STA_DISABLE_160MHZ &&
361 chandef.width == NL80211_CHAN_WIDTH_160)
362 flags |= ieee80211_chandef_downgrade(&chandef);
363 if (ifmgd->flags & IEEE80211_STA_DISABLE_40MHZ &&
364 chandef.width > NL80211_CHAN_WIDTH_20)
365 flags |= ieee80211_chandef_downgrade(&chandef);
366
367 if (cfg80211_chandef_identical(&chandef, &sdata->vif.bss_conf.chandef))
368 return 0;
369
370 sdata_info(sdata,
371 "AP %pM changed bandwidth, new config is %d MHz, width %d (%d/%d MHz)\n",
372 ifmgd->bssid, chandef.chan->center_freq, chandef.width,
373 chandef.center_freq1, chandef.center_freq2);
374
375 if (flags != (ifmgd->flags & (IEEE80211_STA_DISABLE_HT |
376 IEEE80211_STA_DISABLE_VHT |
377 IEEE80211_STA_DISABLE_40MHZ |
378 IEEE80211_STA_DISABLE_80P80MHZ |
379 IEEE80211_STA_DISABLE_160MHZ)) ||
380 !cfg80211_chandef_valid(&chandef)) {
381 sdata_info(sdata,
382 "AP %pM changed bandwidth in a way we can't support - disconnect\n",
383 ifmgd->bssid);
384 return -EINVAL;
385 }
386
387 switch (chandef.width) {
388 case NL80211_CHAN_WIDTH_20_NOHT:
389 case NL80211_CHAN_WIDTH_20:
390 new_sta_bw = IEEE80211_STA_RX_BW_20;
391 break;
392 case NL80211_CHAN_WIDTH_40:
393 new_sta_bw = IEEE80211_STA_RX_BW_40;
394 break;
395 case NL80211_CHAN_WIDTH_80:
396 new_sta_bw = IEEE80211_STA_RX_BW_80;
397 break;
398 case NL80211_CHAN_WIDTH_80P80:
399 case NL80211_CHAN_WIDTH_160:
400 new_sta_bw = IEEE80211_STA_RX_BW_160;
401 break;
402 default:
403 return -EINVAL;
404 }
405
406 if (new_sta_bw > sta->cur_max_bandwidth)
407 new_sta_bw = sta->cur_max_bandwidth;
408
409 if (new_sta_bw < sta->sta.bandwidth) {
410 sta->sta.bandwidth = new_sta_bw;
411 rate_control_rate_update(local, sband, sta,
412 IEEE80211_RC_BW_CHANGED);
413 }
414
415 ret = ieee80211_vif_change_bandwidth(sdata, &chandef, changed);
416 if (ret) {
417 sdata_info(sdata,
418 "AP %pM changed bandwidth to incompatible one - disconnect\n",
419 ifmgd->bssid);
420 return ret;
421 }
422
423 if (new_sta_bw > sta->sta.bandwidth) {
424 sta->sta.bandwidth = new_sta_bw;
425 rate_control_rate_update(local, sband, sta,
426 IEEE80211_RC_BW_CHANGED);
427 }
428
429 return 0;
430 }
431
432 /* frame sending functions */
433
434 static void ieee80211_add_ht_ie(struct ieee80211_sub_if_data *sdata,
435 struct sk_buff *skb, u8 ap_ht_param,
436 struct ieee80211_supported_band *sband,
437 struct ieee80211_channel *channel,
438 enum ieee80211_smps_mode smps)
439 {
440 u8 *pos;
441 u32 flags = channel->flags;
442 u16 cap;
443 struct ieee80211_sta_ht_cap ht_cap;
444
445 BUILD_BUG_ON(sizeof(ht_cap) != sizeof(sband->ht_cap));
446
447 memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
448 ieee80211_apply_htcap_overrides(sdata, &ht_cap);
449
450 /* determine capability flags */
451 cap = ht_cap.cap;
452
453 switch (ap_ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
454 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
455 if (flags & IEEE80211_CHAN_NO_HT40PLUS) {
456 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
457 cap &= ~IEEE80211_HT_CAP_SGI_40;
458 }
459 break;
460 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
461 if (flags & IEEE80211_CHAN_NO_HT40MINUS) {
462 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
463 cap &= ~IEEE80211_HT_CAP_SGI_40;
464 }
465 break;
466 }
467
468 /*
469 * If 40 MHz was disabled associate as though we weren't
470 * capable of 40 MHz -- some broken APs will never fall
471 * back to trying to transmit in 20 MHz.
472 */
473 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_40MHZ) {
474 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
475 cap &= ~IEEE80211_HT_CAP_SGI_40;
476 }
477
478 /* set SM PS mode properly */
479 cap &= ~IEEE80211_HT_CAP_SM_PS;
480 switch (smps) {
481 case IEEE80211_SMPS_AUTOMATIC:
482 case IEEE80211_SMPS_NUM_MODES:
483 WARN_ON(1);
484 case IEEE80211_SMPS_OFF:
485 cap |= WLAN_HT_CAP_SM_PS_DISABLED <<
486 IEEE80211_HT_CAP_SM_PS_SHIFT;
487 break;
488 case IEEE80211_SMPS_STATIC:
489 cap |= WLAN_HT_CAP_SM_PS_STATIC <<
490 IEEE80211_HT_CAP_SM_PS_SHIFT;
491 break;
492 case IEEE80211_SMPS_DYNAMIC:
493 cap |= WLAN_HT_CAP_SM_PS_DYNAMIC <<
494 IEEE80211_HT_CAP_SM_PS_SHIFT;
495 break;
496 }
497
498 /* reserve and fill IE */
499 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
500 ieee80211_ie_build_ht_cap(pos, &ht_cap, cap);
501 }
502
503 static void ieee80211_add_vht_ie(struct ieee80211_sub_if_data *sdata,
504 struct sk_buff *skb,
505 struct ieee80211_supported_band *sband,
506 struct ieee80211_vht_cap *ap_vht_cap)
507 {
508 u8 *pos;
509 u32 cap;
510 struct ieee80211_sta_vht_cap vht_cap;
511 u32 mask, ap_bf_sts, our_bf_sts;
512
513 BUILD_BUG_ON(sizeof(vht_cap) != sizeof(sband->vht_cap));
514
515 memcpy(&vht_cap, &sband->vht_cap, sizeof(vht_cap));
516 ieee80211_apply_vhtcap_overrides(sdata, &vht_cap);
517
518 /* determine capability flags */
519 cap = vht_cap.cap;
520
521 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_80P80MHZ) {
522 cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ;
523 cap |= IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
524 }
525
526 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_160MHZ) {
527 cap &= ~IEEE80211_VHT_CAP_SHORT_GI_160;
528 cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
529 }
530
531 /*
532 * Some APs apparently get confused if our capabilities are better
533 * than theirs, so restrict what we advertise in the assoc request.
534 */
535 if (!(ap_vht_cap->vht_cap_info &
536 cpu_to_le32(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)))
537 cap &= ~IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE;
538
539 mask = IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
540
541 ap_bf_sts = le32_to_cpu(ap_vht_cap->vht_cap_info) & mask;
542 our_bf_sts = cap & mask;
543
544 if (ap_bf_sts < our_bf_sts) {
545 cap &= ~mask;
546 cap |= ap_bf_sts;
547 }
548
549 /* reserve and fill IE */
550 pos = skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2);
551 ieee80211_ie_build_vht_cap(pos, &vht_cap, cap);
552 }
553
554 static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata)
555 {
556 struct ieee80211_local *local = sdata->local;
557 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
558 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
559 struct sk_buff *skb;
560 struct ieee80211_mgmt *mgmt;
561 u8 *pos, qos_info;
562 size_t offset = 0, noffset;
563 int i, count, rates_len, supp_rates_len, shift;
564 u16 capab;
565 struct ieee80211_supported_band *sband;
566 struct ieee80211_chanctx_conf *chanctx_conf;
567 struct ieee80211_channel *chan;
568 u32 rate_flags, rates = 0;
569
570 sdata_assert_lock(sdata);
571
572 rcu_read_lock();
573 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
574 if (WARN_ON(!chanctx_conf)) {
575 rcu_read_unlock();
576 return;
577 }
578 chan = chanctx_conf->def.chan;
579 rate_flags = ieee80211_chandef_rate_flags(&chanctx_conf->def);
580 rcu_read_unlock();
581 sband = local->hw.wiphy->bands[chan->band];
582 shift = ieee80211_vif_get_shift(&sdata->vif);
583
584 if (assoc_data->supp_rates_len) {
585 /*
586 * Get all rates supported by the device and the AP as
587 * some APs don't like getting a superset of their rates
588 * in the association request (e.g. D-Link DAP 1353 in
589 * b-only mode)...
590 */
591 rates_len = ieee80211_parse_bitrates(&chanctx_conf->def, sband,
592 assoc_data->supp_rates,
593 assoc_data->supp_rates_len,
594 &rates);
595 } else {
596 /*
597 * In case AP not provide any supported rates information
598 * before association, we send information element(s) with
599 * all rates that we support.
600 */
601 rates_len = 0;
602 for (i = 0; i < sband->n_bitrates; i++) {
603 if ((rate_flags & sband->bitrates[i].flags)
604 != rate_flags)
605 continue;
606 rates |= BIT(i);
607 rates_len++;
608 }
609 }
610
611 skb = alloc_skb(local->hw.extra_tx_headroom +
612 sizeof(*mgmt) + /* bit too much but doesn't matter */
613 2 + assoc_data->ssid_len + /* SSID */
614 4 + rates_len + /* (extended) rates */
615 4 + /* power capability */
616 2 + 2 * sband->n_channels + /* supported channels */
617 2 + sizeof(struct ieee80211_ht_cap) + /* HT */
618 2 + sizeof(struct ieee80211_vht_cap) + /* VHT */
619 assoc_data->ie_len + /* extra IEs */
620 9, /* WMM */
621 GFP_KERNEL);
622 if (!skb)
623 return;
624
625 skb_reserve(skb, local->hw.extra_tx_headroom);
626
627 capab = WLAN_CAPABILITY_ESS;
628
629 if (sband->band == IEEE80211_BAND_2GHZ) {
630 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
631 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
632 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
633 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
634 }
635
636 if (assoc_data->capability & WLAN_CAPABILITY_PRIVACY)
637 capab |= WLAN_CAPABILITY_PRIVACY;
638
639 if ((assoc_data->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
640 (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
641 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
642
643 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
644 memset(mgmt, 0, 24);
645 memcpy(mgmt->da, assoc_data->bss->bssid, ETH_ALEN);
646 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
647 memcpy(mgmt->bssid, assoc_data->bss->bssid, ETH_ALEN);
648
649 if (!is_zero_ether_addr(assoc_data->prev_bssid)) {
650 skb_put(skb, 10);
651 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
652 IEEE80211_STYPE_REASSOC_REQ);
653 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
654 mgmt->u.reassoc_req.listen_interval =
655 cpu_to_le16(local->hw.conf.listen_interval);
656 memcpy(mgmt->u.reassoc_req.current_ap, assoc_data->prev_bssid,
657 ETH_ALEN);
658 } else {
659 skb_put(skb, 4);
660 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
661 IEEE80211_STYPE_ASSOC_REQ);
662 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
663 mgmt->u.assoc_req.listen_interval =
664 cpu_to_le16(local->hw.conf.listen_interval);
665 }
666
667 /* SSID */
668 pos = skb_put(skb, 2 + assoc_data->ssid_len);
669 *pos++ = WLAN_EID_SSID;
670 *pos++ = assoc_data->ssid_len;
671 memcpy(pos, assoc_data->ssid, assoc_data->ssid_len);
672
673 /* add all rates which were marked to be used above */
674 supp_rates_len = rates_len;
675 if (supp_rates_len > 8)
676 supp_rates_len = 8;
677
678 pos = skb_put(skb, supp_rates_len + 2);
679 *pos++ = WLAN_EID_SUPP_RATES;
680 *pos++ = supp_rates_len;
681
682 count = 0;
683 for (i = 0; i < sband->n_bitrates; i++) {
684 if (BIT(i) & rates) {
685 int rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
686 5 * (1 << shift));
687 *pos++ = (u8) rate;
688 if (++count == 8)
689 break;
690 }
691 }
692
693 if (rates_len > count) {
694 pos = skb_put(skb, rates_len - count + 2);
695 *pos++ = WLAN_EID_EXT_SUPP_RATES;
696 *pos++ = rates_len - count;
697
698 for (i++; i < sband->n_bitrates; i++) {
699 if (BIT(i) & rates) {
700 int rate;
701 rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
702 5 * (1 << shift));
703 *pos++ = (u8) rate;
704 }
705 }
706 }
707
708 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
709 /* 1. power capabilities */
710 pos = skb_put(skb, 4);
711 *pos++ = WLAN_EID_PWR_CAPABILITY;
712 *pos++ = 2;
713 *pos++ = 0; /* min tx power */
714 /* max tx power */
715 *pos++ = ieee80211_chandef_max_power(&chanctx_conf->def);
716
717 /* 2. supported channels */
718 /* TODO: get this in reg domain format */
719 pos = skb_put(skb, 2 * sband->n_channels + 2);
720 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
721 *pos++ = 2 * sband->n_channels;
722 for (i = 0; i < sband->n_channels; i++) {
723 *pos++ = ieee80211_frequency_to_channel(
724 sband->channels[i].center_freq);
725 *pos++ = 1; /* one channel in the subband*/
726 }
727 }
728
729 /* if present, add any custom IEs that go before HT */
730 if (assoc_data->ie_len) {
731 static const u8 before_ht[] = {
732 WLAN_EID_SSID,
733 WLAN_EID_SUPP_RATES,
734 WLAN_EID_EXT_SUPP_RATES,
735 WLAN_EID_PWR_CAPABILITY,
736 WLAN_EID_SUPPORTED_CHANNELS,
737 WLAN_EID_RSN,
738 WLAN_EID_QOS_CAPA,
739 WLAN_EID_RRM_ENABLED_CAPABILITIES,
740 WLAN_EID_MOBILITY_DOMAIN,
741 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
742 };
743 noffset = ieee80211_ie_split(assoc_data->ie, assoc_data->ie_len,
744 before_ht, ARRAY_SIZE(before_ht),
745 offset);
746 pos = skb_put(skb, noffset - offset);
747 memcpy(pos, assoc_data->ie + offset, noffset - offset);
748 offset = noffset;
749 }
750
751 if (WARN_ON_ONCE((ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
752 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)))
753 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
754
755 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
756 ieee80211_add_ht_ie(sdata, skb, assoc_data->ap_ht_param,
757 sband, chan, sdata->smps_mode);
758
759 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
760 ieee80211_add_vht_ie(sdata, skb, sband,
761 &assoc_data->ap_vht_cap);
762
763 /* if present, add any custom non-vendor IEs that go after HT */
764 if (assoc_data->ie_len) {
765 noffset = ieee80211_ie_split_vendor(assoc_data->ie,
766 assoc_data->ie_len,
767 offset);
768 pos = skb_put(skb, noffset - offset);
769 memcpy(pos, assoc_data->ie + offset, noffset - offset);
770 offset = noffset;
771 }
772
773 if (assoc_data->wmm) {
774 if (assoc_data->uapsd) {
775 qos_info = ifmgd->uapsd_queues;
776 qos_info |= (ifmgd->uapsd_max_sp_len <<
777 IEEE80211_WMM_IE_STA_QOSINFO_SP_SHIFT);
778 } else {
779 qos_info = 0;
780 }
781
782 pos = skb_put(skb, 9);
783 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
784 *pos++ = 7; /* len */
785 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
786 *pos++ = 0x50;
787 *pos++ = 0xf2;
788 *pos++ = 2; /* WME */
789 *pos++ = 0; /* WME info */
790 *pos++ = 1; /* WME ver */
791 *pos++ = qos_info;
792 }
793
794 /* add any remaining custom (i.e. vendor specific here) IEs */
795 if (assoc_data->ie_len) {
796 noffset = assoc_data->ie_len;
797 pos = skb_put(skb, noffset - offset);
798 memcpy(pos, assoc_data->ie + offset, noffset - offset);
799 }
800
801 drv_mgd_prepare_tx(local, sdata);
802
803 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
804 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
805 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS |
806 IEEE80211_TX_INTFL_MLME_CONN_TX;
807 ieee80211_tx_skb(sdata, skb);
808 }
809
810 void ieee80211_send_pspoll(struct ieee80211_local *local,
811 struct ieee80211_sub_if_data *sdata)
812 {
813 struct ieee80211_pspoll *pspoll;
814 struct sk_buff *skb;
815
816 skb = ieee80211_pspoll_get(&local->hw, &sdata->vif);
817 if (!skb)
818 return;
819
820 pspoll = (struct ieee80211_pspoll *) skb->data;
821 pspoll->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
822
823 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
824 ieee80211_tx_skb(sdata, skb);
825 }
826
827 void ieee80211_send_nullfunc(struct ieee80211_local *local,
828 struct ieee80211_sub_if_data *sdata,
829 int powersave)
830 {
831 struct sk_buff *skb;
832 struct ieee80211_hdr_3addr *nullfunc;
833 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
834
835 skb = ieee80211_nullfunc_get(&local->hw, &sdata->vif);
836 if (!skb)
837 return;
838
839 nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
840 if (powersave)
841 nullfunc->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
842
843 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
844 IEEE80211_TX_INTFL_OFFCHAN_TX_OK;
845
846 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
847 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
848
849 if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL)
850 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_USE_MINRATE;
851
852 ieee80211_tx_skb(sdata, skb);
853 }
854
855 static void ieee80211_send_4addr_nullfunc(struct ieee80211_local *local,
856 struct ieee80211_sub_if_data *sdata)
857 {
858 struct sk_buff *skb;
859 struct ieee80211_hdr *nullfunc;
860 __le16 fc;
861
862 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
863 return;
864
865 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 30);
866 if (!skb)
867 return;
868
869 skb_reserve(skb, local->hw.extra_tx_headroom);
870
871 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 30);
872 memset(nullfunc, 0, 30);
873 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
874 IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
875 nullfunc->frame_control = fc;
876 memcpy(nullfunc->addr1, sdata->u.mgd.bssid, ETH_ALEN);
877 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
878 memcpy(nullfunc->addr3, sdata->u.mgd.bssid, ETH_ALEN);
879 memcpy(nullfunc->addr4, sdata->vif.addr, ETH_ALEN);
880
881 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
882 ieee80211_tx_skb(sdata, skb);
883 }
884
885 /* spectrum management related things */
886 static void ieee80211_chswitch_work(struct work_struct *work)
887 {
888 struct ieee80211_sub_if_data *sdata =
889 container_of(work, struct ieee80211_sub_if_data, u.mgd.chswitch_work);
890 struct ieee80211_local *local = sdata->local;
891 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
892 u32 changed = 0;
893 int ret;
894
895 if (!ieee80211_sdata_running(sdata))
896 return;
897
898 sdata_lock(sdata);
899 if (!ifmgd->associated)
900 goto out;
901
902 mutex_lock(&local->mtx);
903 ret = ieee80211_vif_change_channel(sdata, &changed);
904 mutex_unlock(&local->mtx);
905 if (ret) {
906 sdata_info(sdata,
907 "vif channel switch failed, disconnecting\n");
908 ieee80211_queue_work(&sdata->local->hw,
909 &ifmgd->csa_connection_drop_work);
910 goto out;
911 }
912
913 if (!local->use_chanctx) {
914 local->_oper_chandef = sdata->csa_chandef;
915 /* Call "hw_config" only if doing sw channel switch.
916 * Otherwise update the channel directly
917 */
918 if (!local->ops->channel_switch)
919 ieee80211_hw_config(local, 0);
920 else
921 local->hw.conf.chandef = local->_oper_chandef;
922 }
923
924 /* XXX: shouldn't really modify cfg80211-owned data! */
925 ifmgd->associated->channel = sdata->csa_chandef.chan;
926
927 /* XXX: wait for a beacon first? */
928 ieee80211_wake_queues_by_reason(&local->hw,
929 IEEE80211_MAX_QUEUE_MAP,
930 IEEE80211_QUEUE_STOP_REASON_CSA);
931
932 ieee80211_bss_info_change_notify(sdata, changed);
933
934 out:
935 sdata->vif.csa_active = false;
936 ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
937 sdata_unlock(sdata);
938 }
939
940 void ieee80211_chswitch_done(struct ieee80211_vif *vif, bool success)
941 {
942 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
943 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
944
945 trace_api_chswitch_done(sdata, success);
946 if (!success) {
947 sdata_info(sdata,
948 "driver channel switch failed, disconnecting\n");
949 ieee80211_queue_work(&sdata->local->hw,
950 &ifmgd->csa_connection_drop_work);
951 } else {
952 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
953 }
954 }
955 EXPORT_SYMBOL(ieee80211_chswitch_done);
956
957 static void ieee80211_chswitch_timer(unsigned long data)
958 {
959 struct ieee80211_sub_if_data *sdata =
960 (struct ieee80211_sub_if_data *) data;
961
962 ieee80211_queue_work(&sdata->local->hw, &sdata->u.mgd.chswitch_work);
963 }
964
965 static void
966 ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
967 u64 timestamp, struct ieee802_11_elems *elems,
968 bool beacon)
969 {
970 struct ieee80211_local *local = sdata->local;
971 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
972 struct cfg80211_bss *cbss = ifmgd->associated;
973 struct ieee80211_chanctx *chanctx;
974 enum ieee80211_band current_band;
975 struct ieee80211_csa_ie csa_ie;
976 int res;
977
978 sdata_assert_lock(sdata);
979
980 if (!cbss)
981 return;
982
983 if (local->scanning)
984 return;
985
986 /* disregard subsequent announcements if we are already processing */
987 if (ifmgd->flags & IEEE80211_STA_CSA_RECEIVED)
988 return;
989
990 current_band = cbss->channel->band;
991 memset(&csa_ie, 0, sizeof(csa_ie));
992 res = ieee80211_parse_ch_switch_ie(sdata, elems, beacon, current_band,
993 ifmgd->flags,
994 ifmgd->associated->bssid, &csa_ie);
995 if (res < 0)
996 ieee80211_queue_work(&local->hw,
997 &ifmgd->csa_connection_drop_work);
998 if (res)
999 return;
1000
1001 if (!cfg80211_chandef_usable(local->hw.wiphy, &csa_ie.chandef,
1002 IEEE80211_CHAN_DISABLED)) {
1003 sdata_info(sdata,
1004 "AP %pM switches to unsupported channel (%d MHz, width:%d, CF1/2: %d/%d MHz), disconnecting\n",
1005 ifmgd->associated->bssid,
1006 csa_ie.chandef.chan->center_freq,
1007 csa_ie.chandef.width, csa_ie.chandef.center_freq1,
1008 csa_ie.chandef.center_freq2);
1009 ieee80211_queue_work(&local->hw,
1010 &ifmgd->csa_connection_drop_work);
1011 return;
1012 }
1013
1014 ifmgd->flags |= IEEE80211_STA_CSA_RECEIVED;
1015
1016 mutex_lock(&local->chanctx_mtx);
1017 if (local->use_chanctx) {
1018 u32 num_chanctx = 0;
1019 list_for_each_entry(chanctx, &local->chanctx_list, list)
1020 num_chanctx++;
1021
1022 if (num_chanctx > 1 ||
1023 !(local->hw.flags & IEEE80211_HW_CHANCTX_STA_CSA)) {
1024 sdata_info(sdata,
1025 "not handling chan-switch with channel contexts\n");
1026 ieee80211_queue_work(&local->hw,
1027 &ifmgd->csa_connection_drop_work);
1028 mutex_unlock(&local->chanctx_mtx);
1029 return;
1030 }
1031 }
1032
1033 if (WARN_ON(!rcu_access_pointer(sdata->vif.chanctx_conf))) {
1034 ieee80211_queue_work(&local->hw,
1035 &ifmgd->csa_connection_drop_work);
1036 mutex_unlock(&local->chanctx_mtx);
1037 return;
1038 }
1039 chanctx = container_of(rcu_access_pointer(sdata->vif.chanctx_conf),
1040 struct ieee80211_chanctx, conf);
1041 if (chanctx->refcount > 1) {
1042 sdata_info(sdata,
1043 "channel switch with multiple interfaces on the same channel, disconnecting\n");
1044 ieee80211_queue_work(&local->hw,
1045 &ifmgd->csa_connection_drop_work);
1046 mutex_unlock(&local->chanctx_mtx);
1047 return;
1048 }
1049 mutex_unlock(&local->chanctx_mtx);
1050
1051 sdata->csa_chandef = csa_ie.chandef;
1052 sdata->vif.csa_active = true;
1053
1054 if (csa_ie.mode)
1055 ieee80211_stop_queues_by_reason(&local->hw,
1056 IEEE80211_MAX_QUEUE_MAP,
1057 IEEE80211_QUEUE_STOP_REASON_CSA);
1058
1059 if (local->ops->channel_switch) {
1060 /* use driver's channel switch callback */
1061 struct ieee80211_channel_switch ch_switch = {
1062 .timestamp = timestamp,
1063 .block_tx = csa_ie.mode,
1064 .chandef = csa_ie.chandef,
1065 .count = csa_ie.count,
1066 };
1067
1068 drv_channel_switch(local, &ch_switch);
1069 return;
1070 }
1071
1072 /* channel switch handled in software */
1073 if (csa_ie.count <= 1)
1074 ieee80211_queue_work(&local->hw, &ifmgd->chswitch_work);
1075 else
1076 mod_timer(&ifmgd->chswitch_timer,
1077 TU_TO_EXP_TIME(csa_ie.count * cbss->beacon_interval));
1078 }
1079
1080 static u32 ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data *sdata,
1081 struct ieee80211_channel *channel,
1082 const u8 *country_ie, u8 country_ie_len,
1083 const u8 *pwr_constr_elem)
1084 {
1085 struct ieee80211_country_ie_triplet *triplet;
1086 int chan = ieee80211_frequency_to_channel(channel->center_freq);
1087 int i, chan_pwr, chan_increment, new_ap_level;
1088 bool have_chan_pwr = false;
1089
1090 /* Invalid IE */
1091 if (country_ie_len % 2 || country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
1092 return 0;
1093
1094 triplet = (void *)(country_ie + 3);
1095 country_ie_len -= 3;
1096
1097 switch (channel->band) {
1098 default:
1099 WARN_ON_ONCE(1);
1100 /* fall through */
1101 case IEEE80211_BAND_2GHZ:
1102 case IEEE80211_BAND_60GHZ:
1103 chan_increment = 1;
1104 break;
1105 case IEEE80211_BAND_5GHZ:
1106 chan_increment = 4;
1107 break;
1108 }
1109
1110 /* find channel */
1111 while (country_ie_len >= 3) {
1112 u8 first_channel = triplet->chans.first_channel;
1113
1114 if (first_channel >= IEEE80211_COUNTRY_EXTENSION_ID)
1115 goto next;
1116
1117 for (i = 0; i < triplet->chans.num_channels; i++) {
1118 if (first_channel + i * chan_increment == chan) {
1119 have_chan_pwr = true;
1120 chan_pwr = triplet->chans.max_power;
1121 break;
1122 }
1123 }
1124 if (have_chan_pwr)
1125 break;
1126
1127 next:
1128 triplet++;
1129 country_ie_len -= 3;
1130 }
1131
1132 if (!have_chan_pwr)
1133 return 0;
1134
1135 new_ap_level = max_t(int, 0, chan_pwr - *pwr_constr_elem);
1136
1137 if (sdata->ap_power_level == new_ap_level)
1138 return 0;
1139
1140 sdata_info(sdata,
1141 "Limiting TX power to %d (%d - %d) dBm as advertised by %pM\n",
1142 new_ap_level, chan_pwr, *pwr_constr_elem,
1143 sdata->u.mgd.bssid);
1144 sdata->ap_power_level = new_ap_level;
1145 if (__ieee80211_recalc_txpower(sdata))
1146 return BSS_CHANGED_TXPOWER;
1147 return 0;
1148 }
1149
1150 /* powersave */
1151 static void ieee80211_enable_ps(struct ieee80211_local *local,
1152 struct ieee80211_sub_if_data *sdata)
1153 {
1154 struct ieee80211_conf *conf = &local->hw.conf;
1155
1156 /*
1157 * If we are scanning right now then the parameters will
1158 * take effect when scan finishes.
1159 */
1160 if (local->scanning)
1161 return;
1162
1163 if (conf->dynamic_ps_timeout > 0 &&
1164 !(local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)) {
1165 mod_timer(&local->dynamic_ps_timer, jiffies +
1166 msecs_to_jiffies(conf->dynamic_ps_timeout));
1167 } else {
1168 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
1169 ieee80211_send_nullfunc(local, sdata, 1);
1170
1171 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
1172 (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS))
1173 return;
1174
1175 conf->flags |= IEEE80211_CONF_PS;
1176 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1177 }
1178 }
1179
1180 static void ieee80211_change_ps(struct ieee80211_local *local)
1181 {
1182 struct ieee80211_conf *conf = &local->hw.conf;
1183
1184 if (local->ps_sdata) {
1185 ieee80211_enable_ps(local, local->ps_sdata);
1186 } else if (conf->flags & IEEE80211_CONF_PS) {
1187 conf->flags &= ~IEEE80211_CONF_PS;
1188 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1189 del_timer_sync(&local->dynamic_ps_timer);
1190 cancel_work_sync(&local->dynamic_ps_enable_work);
1191 }
1192 }
1193
1194 static bool ieee80211_powersave_allowed(struct ieee80211_sub_if_data *sdata)
1195 {
1196 struct ieee80211_if_managed *mgd = &sdata->u.mgd;
1197 struct sta_info *sta = NULL;
1198 bool authorized = false;
1199
1200 if (!mgd->powersave)
1201 return false;
1202
1203 if (mgd->broken_ap)
1204 return false;
1205
1206 if (!mgd->associated)
1207 return false;
1208
1209 if (mgd->flags & IEEE80211_STA_CONNECTION_POLL)
1210 return false;
1211
1212 if (!mgd->have_beacon)
1213 return false;
1214
1215 rcu_read_lock();
1216 sta = sta_info_get(sdata, mgd->bssid);
1217 if (sta)
1218 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
1219 rcu_read_unlock();
1220
1221 return authorized;
1222 }
1223
1224 /* need to hold RTNL or interface lock */
1225 void ieee80211_recalc_ps(struct ieee80211_local *local, s32 latency)
1226 {
1227 struct ieee80211_sub_if_data *sdata, *found = NULL;
1228 int count = 0;
1229 int timeout;
1230
1231 if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS)) {
1232 local->ps_sdata = NULL;
1233 return;
1234 }
1235
1236 list_for_each_entry(sdata, &local->interfaces, list) {
1237 if (!ieee80211_sdata_running(sdata))
1238 continue;
1239 if (sdata->vif.type == NL80211_IFTYPE_AP) {
1240 /* If an AP vif is found, then disable PS
1241 * by setting the count to zero thereby setting
1242 * ps_sdata to NULL.
1243 */
1244 count = 0;
1245 break;
1246 }
1247 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1248 continue;
1249 found = sdata;
1250 count++;
1251 }
1252
1253 if (count == 1 && ieee80211_powersave_allowed(found)) {
1254 s32 beaconint_us;
1255
1256 if (latency < 0)
1257 latency = pm_qos_request(PM_QOS_NETWORK_LATENCY);
1258
1259 beaconint_us = ieee80211_tu_to_usec(
1260 found->vif.bss_conf.beacon_int);
1261
1262 timeout = local->dynamic_ps_forced_timeout;
1263 if (timeout < 0) {
1264 /*
1265 * Go to full PSM if the user configures a very low
1266 * latency requirement.
1267 * The 2000 second value is there for compatibility
1268 * until the PM_QOS_NETWORK_LATENCY is configured
1269 * with real values.
1270 */
1271 if (latency > (1900 * USEC_PER_MSEC) &&
1272 latency != (2000 * USEC_PER_SEC))
1273 timeout = 0;
1274 else
1275 timeout = 100;
1276 }
1277 local->hw.conf.dynamic_ps_timeout = timeout;
1278
1279 if (beaconint_us > latency) {
1280 local->ps_sdata = NULL;
1281 } else {
1282 int maxslp = 1;
1283 u8 dtimper = found->u.mgd.dtim_period;
1284
1285 /* If the TIM IE is invalid, pretend the value is 1 */
1286 if (!dtimper)
1287 dtimper = 1;
1288 else if (dtimper > 1)
1289 maxslp = min_t(int, dtimper,
1290 latency / beaconint_us);
1291
1292 local->hw.conf.max_sleep_period = maxslp;
1293 local->hw.conf.ps_dtim_period = dtimper;
1294 local->ps_sdata = found;
1295 }
1296 } else {
1297 local->ps_sdata = NULL;
1298 }
1299
1300 ieee80211_change_ps(local);
1301 }
1302
1303 void ieee80211_recalc_ps_vif(struct ieee80211_sub_if_data *sdata)
1304 {
1305 bool ps_allowed = ieee80211_powersave_allowed(sdata);
1306
1307 if (sdata->vif.bss_conf.ps != ps_allowed) {
1308 sdata->vif.bss_conf.ps = ps_allowed;
1309 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_PS);
1310 }
1311 }
1312
1313 void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
1314 {
1315 struct ieee80211_local *local =
1316 container_of(work, struct ieee80211_local,
1317 dynamic_ps_disable_work);
1318
1319 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1320 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1321 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1322 }
1323
1324 ieee80211_wake_queues_by_reason(&local->hw,
1325 IEEE80211_MAX_QUEUE_MAP,
1326 IEEE80211_QUEUE_STOP_REASON_PS);
1327 }
1328
1329 void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
1330 {
1331 struct ieee80211_local *local =
1332 container_of(work, struct ieee80211_local,
1333 dynamic_ps_enable_work);
1334 struct ieee80211_sub_if_data *sdata = local->ps_sdata;
1335 struct ieee80211_if_managed *ifmgd;
1336 unsigned long flags;
1337 int q;
1338
1339 /* can only happen when PS was just disabled anyway */
1340 if (!sdata)
1341 return;
1342
1343 ifmgd = &sdata->u.mgd;
1344
1345 if (local->hw.conf.flags & IEEE80211_CONF_PS)
1346 return;
1347
1348 if (local->hw.conf.dynamic_ps_timeout > 0) {
1349 /* don't enter PS if TX frames are pending */
1350 if (drv_tx_frames_pending(local)) {
1351 mod_timer(&local->dynamic_ps_timer, jiffies +
1352 msecs_to_jiffies(
1353 local->hw.conf.dynamic_ps_timeout));
1354 return;
1355 }
1356
1357 /*
1358 * transmission can be stopped by others which leads to
1359 * dynamic_ps_timer expiry. Postpone the ps timer if it
1360 * is not the actual idle state.
1361 */
1362 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
1363 for (q = 0; q < local->hw.queues; q++) {
1364 if (local->queue_stop_reasons[q]) {
1365 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1366 flags);
1367 mod_timer(&local->dynamic_ps_timer, jiffies +
1368 msecs_to_jiffies(
1369 local->hw.conf.dynamic_ps_timeout));
1370 return;
1371 }
1372 }
1373 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
1374 }
1375
1376 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
1377 !(ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
1378 if (drv_tx_frames_pending(local)) {
1379 mod_timer(&local->dynamic_ps_timer, jiffies +
1380 msecs_to_jiffies(
1381 local->hw.conf.dynamic_ps_timeout));
1382 } else {
1383 ieee80211_send_nullfunc(local, sdata, 1);
1384 /* Flush to get the tx status of nullfunc frame */
1385 ieee80211_flush_queues(local, sdata);
1386 }
1387 }
1388
1389 if (!((local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) &&
1390 (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)) ||
1391 (ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
1392 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
1393 local->hw.conf.flags |= IEEE80211_CONF_PS;
1394 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1395 }
1396 }
1397
1398 void ieee80211_dynamic_ps_timer(unsigned long data)
1399 {
1400 struct ieee80211_local *local = (void *) data;
1401
1402 if (local->quiescing || local->suspended)
1403 return;
1404
1405 ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
1406 }
1407
1408 void ieee80211_dfs_cac_timer_work(struct work_struct *work)
1409 {
1410 struct delayed_work *delayed_work =
1411 container_of(work, struct delayed_work, work);
1412 struct ieee80211_sub_if_data *sdata =
1413 container_of(delayed_work, struct ieee80211_sub_if_data,
1414 dfs_cac_timer_work);
1415 struct cfg80211_chan_def chandef = sdata->vif.bss_conf.chandef;
1416
1417 mutex_lock(&sdata->local->mtx);
1418 if (sdata->wdev.cac_started) {
1419 ieee80211_vif_release_channel(sdata);
1420 cfg80211_cac_event(sdata->dev, &chandef,
1421 NL80211_RADAR_CAC_FINISHED,
1422 GFP_KERNEL);
1423 }
1424 mutex_unlock(&sdata->local->mtx);
1425 }
1426
1427 /* MLME */
1428 static bool ieee80211_sta_wmm_params(struct ieee80211_local *local,
1429 struct ieee80211_sub_if_data *sdata,
1430 const u8 *wmm_param, size_t wmm_param_len)
1431 {
1432 struct ieee80211_tx_queue_params params;
1433 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1434 size_t left;
1435 int count;
1436 const u8 *pos;
1437 u8 uapsd_queues = 0;
1438
1439 if (!local->ops->conf_tx)
1440 return false;
1441
1442 if (local->hw.queues < IEEE80211_NUM_ACS)
1443 return false;
1444
1445 if (!wmm_param)
1446 return false;
1447
1448 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
1449 return false;
1450
1451 if (ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED)
1452 uapsd_queues = ifmgd->uapsd_queues;
1453
1454 count = wmm_param[6] & 0x0f;
1455 if (count == ifmgd->wmm_last_param_set)
1456 return false;
1457 ifmgd->wmm_last_param_set = count;
1458
1459 pos = wmm_param + 8;
1460 left = wmm_param_len - 8;
1461
1462 memset(&params, 0, sizeof(params));
1463
1464 sdata->wmm_acm = 0;
1465 for (; left >= 4; left -= 4, pos += 4) {
1466 int aci = (pos[0] >> 5) & 0x03;
1467 int acm = (pos[0] >> 4) & 0x01;
1468 bool uapsd = false;
1469 int queue;
1470
1471 switch (aci) {
1472 case 1: /* AC_BK */
1473 queue = 3;
1474 if (acm)
1475 sdata->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
1476 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
1477 uapsd = true;
1478 break;
1479 case 2: /* AC_VI */
1480 queue = 1;
1481 if (acm)
1482 sdata->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
1483 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
1484 uapsd = true;
1485 break;
1486 case 3: /* AC_VO */
1487 queue = 0;
1488 if (acm)
1489 sdata->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
1490 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
1491 uapsd = true;
1492 break;
1493 case 0: /* AC_BE */
1494 default:
1495 queue = 2;
1496 if (acm)
1497 sdata->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
1498 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
1499 uapsd = true;
1500 break;
1501 }
1502
1503 params.aifs = pos[0] & 0x0f;
1504 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
1505 params.cw_min = ecw2cw(pos[1] & 0x0f);
1506 params.txop = get_unaligned_le16(pos + 2);
1507 params.acm = acm;
1508 params.uapsd = uapsd;
1509
1510 mlme_dbg(sdata,
1511 "WMM queue=%d aci=%d acm=%d aifs=%d cWmin=%d cWmax=%d txop=%d uapsd=%d\n",
1512 queue, aci, acm,
1513 params.aifs, params.cw_min, params.cw_max,
1514 params.txop, params.uapsd);
1515 sdata->tx_conf[queue] = params;
1516 if (drv_conf_tx(local, sdata, queue, &params))
1517 sdata_err(sdata,
1518 "failed to set TX queue parameters for queue %d\n",
1519 queue);
1520 }
1521
1522 /* enable WMM or activate new settings */
1523 sdata->vif.bss_conf.qos = true;
1524 return true;
1525 }
1526
1527 static void __ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
1528 {
1529 lockdep_assert_held(&sdata->local->mtx);
1530
1531 sdata->u.mgd.flags &= ~IEEE80211_STA_CONNECTION_POLL;
1532 ieee80211_run_deferred_scan(sdata->local);
1533 }
1534
1535 static void ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
1536 {
1537 mutex_lock(&sdata->local->mtx);
1538 __ieee80211_stop_poll(sdata);
1539 mutex_unlock(&sdata->local->mtx);
1540 }
1541
1542 static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
1543 u16 capab, bool erp_valid, u8 erp)
1544 {
1545 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
1546 u32 changed = 0;
1547 bool use_protection;
1548 bool use_short_preamble;
1549 bool use_short_slot;
1550
1551 if (erp_valid) {
1552 use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
1553 use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
1554 } else {
1555 use_protection = false;
1556 use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
1557 }
1558
1559 use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
1560 if (ieee80211_get_sdata_band(sdata) == IEEE80211_BAND_5GHZ)
1561 use_short_slot = true;
1562
1563 if (use_protection != bss_conf->use_cts_prot) {
1564 bss_conf->use_cts_prot = use_protection;
1565 changed |= BSS_CHANGED_ERP_CTS_PROT;
1566 }
1567
1568 if (use_short_preamble != bss_conf->use_short_preamble) {
1569 bss_conf->use_short_preamble = use_short_preamble;
1570 changed |= BSS_CHANGED_ERP_PREAMBLE;
1571 }
1572
1573 if (use_short_slot != bss_conf->use_short_slot) {
1574 bss_conf->use_short_slot = use_short_slot;
1575 changed |= BSS_CHANGED_ERP_SLOT;
1576 }
1577
1578 return changed;
1579 }
1580
1581 static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
1582 struct cfg80211_bss *cbss,
1583 u32 bss_info_changed)
1584 {
1585 struct ieee80211_bss *bss = (void *)cbss->priv;
1586 struct ieee80211_local *local = sdata->local;
1587 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
1588
1589 bss_info_changed |= BSS_CHANGED_ASSOC;
1590 bss_info_changed |= ieee80211_handle_bss_capability(sdata,
1591 bss_conf->assoc_capability, bss->has_erp_value, bss->erp_value);
1592
1593 sdata->u.mgd.beacon_timeout = usecs_to_jiffies(ieee80211_tu_to_usec(
1594 beacon_loss_count * bss_conf->beacon_int));
1595
1596 sdata->u.mgd.associated = cbss;
1597 memcpy(sdata->u.mgd.bssid, cbss->bssid, ETH_ALEN);
1598
1599 sdata->u.mgd.flags |= IEEE80211_STA_RESET_SIGNAL_AVE;
1600
1601 if (sdata->vif.p2p) {
1602 const struct cfg80211_bss_ies *ies;
1603
1604 rcu_read_lock();
1605 ies = rcu_dereference(cbss->ies);
1606 if (ies) {
1607 int ret;
1608
1609 ret = cfg80211_get_p2p_attr(
1610 ies->data, ies->len,
1611 IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
1612 (u8 *) &bss_conf->p2p_noa_attr,
1613 sizeof(bss_conf->p2p_noa_attr));
1614 if (ret >= 2) {
1615 sdata->u.mgd.p2p_noa_index =
1616 bss_conf->p2p_noa_attr.index;
1617 bss_info_changed |= BSS_CHANGED_P2P_PS;
1618 }
1619 }
1620 rcu_read_unlock();
1621 }
1622
1623 /* just to be sure */
1624 ieee80211_stop_poll(sdata);
1625
1626 ieee80211_led_assoc(local, 1);
1627
1628 if (sdata->u.mgd.have_beacon) {
1629 /*
1630 * If the AP is buggy we may get here with no DTIM period
1631 * known, so assume it's 1 which is the only safe assumption
1632 * in that case, although if the TIM IE is broken powersave
1633 * probably just won't work at all.
1634 */
1635 bss_conf->dtim_period = sdata->u.mgd.dtim_period ?: 1;
1636 bss_conf->beacon_rate = bss->beacon_rate;
1637 bss_info_changed |= BSS_CHANGED_BEACON_INFO;
1638 } else {
1639 bss_conf->beacon_rate = NULL;
1640 bss_conf->dtim_period = 0;
1641 }
1642
1643 bss_conf->assoc = 1;
1644
1645 /* Tell the driver to monitor connection quality (if supported) */
1646 if (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI &&
1647 bss_conf->cqm_rssi_thold)
1648 bss_info_changed |= BSS_CHANGED_CQM;
1649
1650 /* Enable ARP filtering */
1651 if (bss_conf->arp_addr_cnt)
1652 bss_info_changed |= BSS_CHANGED_ARP_FILTER;
1653
1654 ieee80211_bss_info_change_notify(sdata, bss_info_changed);
1655
1656 mutex_lock(&local->iflist_mtx);
1657 ieee80211_recalc_ps(local, -1);
1658 mutex_unlock(&local->iflist_mtx);
1659
1660 ieee80211_recalc_smps(sdata);
1661 ieee80211_recalc_ps_vif(sdata);
1662
1663 netif_carrier_on(sdata->dev);
1664 }
1665
1666 static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
1667 u16 stype, u16 reason, bool tx,
1668 u8 *frame_buf)
1669 {
1670 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1671 struct ieee80211_local *local = sdata->local;
1672 u32 changed = 0;
1673
1674 sdata_assert_lock(sdata);
1675
1676 if (WARN_ON_ONCE(tx && !frame_buf))
1677 return;
1678
1679 if (WARN_ON(!ifmgd->associated))
1680 return;
1681
1682 ieee80211_stop_poll(sdata);
1683
1684 ifmgd->associated = NULL;
1685 netif_carrier_off(sdata->dev);
1686
1687 /*
1688 * if we want to get out of ps before disassoc (why?) we have
1689 * to do it before sending disassoc, as otherwise the null-packet
1690 * won't be valid.
1691 */
1692 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1693 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1694 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1695 }
1696 local->ps_sdata = NULL;
1697
1698 /* disable per-vif ps */
1699 ieee80211_recalc_ps_vif(sdata);
1700
1701 /* flush out any pending frame (e.g. DELBA) before deauth/disassoc */
1702 if (tx)
1703 ieee80211_flush_queues(local, sdata);
1704
1705 /* deauthenticate/disassociate now */
1706 if (tx || frame_buf)
1707 ieee80211_send_deauth_disassoc(sdata, ifmgd->bssid, stype,
1708 reason, tx, frame_buf);
1709
1710 /* flush out frame */
1711 if (tx)
1712 ieee80211_flush_queues(local, sdata);
1713
1714 /* clear bssid only after building the needed mgmt frames */
1715 memset(ifmgd->bssid, 0, ETH_ALEN);
1716
1717 /* remove AP and TDLS peers */
1718 sta_info_flush(sdata);
1719
1720 /* finally reset all BSS / config parameters */
1721 changed |= ieee80211_reset_erp_info(sdata);
1722
1723 ieee80211_led_assoc(local, 0);
1724 changed |= BSS_CHANGED_ASSOC;
1725 sdata->vif.bss_conf.assoc = false;
1726
1727 ifmgd->p2p_noa_index = -1;
1728 memset(&sdata->vif.bss_conf.p2p_noa_attr, 0,
1729 sizeof(sdata->vif.bss_conf.p2p_noa_attr));
1730
1731 /* on the next assoc, re-program HT/VHT parameters */
1732 memset(&ifmgd->ht_capa, 0, sizeof(ifmgd->ht_capa));
1733 memset(&ifmgd->ht_capa_mask, 0, sizeof(ifmgd->ht_capa_mask));
1734 memset(&ifmgd->vht_capa, 0, sizeof(ifmgd->vht_capa));
1735 memset(&ifmgd->vht_capa_mask, 0, sizeof(ifmgd->vht_capa_mask));
1736
1737 sdata->ap_power_level = IEEE80211_UNSET_POWER_LEVEL;
1738
1739 del_timer_sync(&local->dynamic_ps_timer);
1740 cancel_work_sync(&local->dynamic_ps_enable_work);
1741
1742 /* Disable ARP filtering */
1743 if (sdata->vif.bss_conf.arp_addr_cnt)
1744 changed |= BSS_CHANGED_ARP_FILTER;
1745
1746 sdata->vif.bss_conf.qos = false;
1747 changed |= BSS_CHANGED_QOS;
1748
1749 /* The BSSID (not really interesting) and HT changed */
1750 changed |= BSS_CHANGED_BSSID | BSS_CHANGED_HT;
1751 ieee80211_bss_info_change_notify(sdata, changed);
1752
1753 /* disassociated - set to defaults now */
1754 ieee80211_set_wmm_default(sdata, false);
1755
1756 del_timer_sync(&sdata->u.mgd.conn_mon_timer);
1757 del_timer_sync(&sdata->u.mgd.bcn_mon_timer);
1758 del_timer_sync(&sdata->u.mgd.timer);
1759 del_timer_sync(&sdata->u.mgd.chswitch_timer);
1760
1761 sdata->vif.bss_conf.dtim_period = 0;
1762 sdata->vif.bss_conf.beacon_rate = NULL;
1763
1764 ifmgd->have_beacon = false;
1765
1766 ifmgd->flags = 0;
1767 mutex_lock(&local->mtx);
1768 ieee80211_vif_release_channel(sdata);
1769 mutex_unlock(&local->mtx);
1770
1771 sdata->encrypt_headroom = IEEE80211_ENCRYPT_HEADROOM;
1772 }
1773
1774 void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
1775 struct ieee80211_hdr *hdr)
1776 {
1777 /*
1778 * We can postpone the mgd.timer whenever receiving unicast frames
1779 * from AP because we know that the connection is working both ways
1780 * at that time. But multicast frames (and hence also beacons) must
1781 * be ignored here, because we need to trigger the timer during
1782 * data idle periods for sending the periodic probe request to the
1783 * AP we're connected to.
1784 */
1785 if (is_multicast_ether_addr(hdr->addr1))
1786 return;
1787
1788 ieee80211_sta_reset_conn_monitor(sdata);
1789 }
1790
1791 static void ieee80211_reset_ap_probe(struct ieee80211_sub_if_data *sdata)
1792 {
1793 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1794 struct ieee80211_local *local = sdata->local;
1795
1796 mutex_lock(&local->mtx);
1797 if (!(ifmgd->flags & IEEE80211_STA_CONNECTION_POLL))
1798 goto out;
1799
1800 __ieee80211_stop_poll(sdata);
1801
1802 mutex_lock(&local->iflist_mtx);
1803 ieee80211_recalc_ps(local, -1);
1804 mutex_unlock(&local->iflist_mtx);
1805
1806 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
1807 goto out;
1808
1809 /*
1810 * We've received a probe response, but are not sure whether
1811 * we have or will be receiving any beacons or data, so let's
1812 * schedule the timers again, just in case.
1813 */
1814 ieee80211_sta_reset_beacon_monitor(sdata);
1815
1816 mod_timer(&ifmgd->conn_mon_timer,
1817 round_jiffies_up(jiffies +
1818 IEEE80211_CONNECTION_IDLE_TIME));
1819 out:
1820 mutex_unlock(&local->mtx);
1821 }
1822
1823 void ieee80211_sta_tx_notify(struct ieee80211_sub_if_data *sdata,
1824 struct ieee80211_hdr *hdr, bool ack)
1825 {
1826 if (!ieee80211_is_data(hdr->frame_control))
1827 return;
1828
1829 if (ieee80211_is_nullfunc(hdr->frame_control) &&
1830 sdata->u.mgd.probe_send_count > 0) {
1831 if (ack)
1832 ieee80211_sta_reset_conn_monitor(sdata);
1833 else
1834 sdata->u.mgd.nullfunc_failed = true;
1835 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
1836 return;
1837 }
1838
1839 if (ack)
1840 ieee80211_sta_reset_conn_monitor(sdata);
1841 }
1842
1843 static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
1844 {
1845 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1846 const u8 *ssid;
1847 u8 *dst = ifmgd->associated->bssid;
1848 u8 unicast_limit = max(1, max_probe_tries - 3);
1849
1850 /*
1851 * Try sending broadcast probe requests for the last three
1852 * probe requests after the first ones failed since some
1853 * buggy APs only support broadcast probe requests.
1854 */
1855 if (ifmgd->probe_send_count >= unicast_limit)
1856 dst = NULL;
1857
1858 /*
1859 * When the hardware reports an accurate Tx ACK status, it's
1860 * better to send a nullfunc frame instead of a probe request,
1861 * as it will kick us off the AP quickly if we aren't associated
1862 * anymore. The timeout will be reset if the frame is ACKed by
1863 * the AP.
1864 */
1865 ifmgd->probe_send_count++;
1866
1867 if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
1868 ifmgd->nullfunc_failed = false;
1869 ieee80211_send_nullfunc(sdata->local, sdata, 0);
1870 } else {
1871 int ssid_len;
1872
1873 rcu_read_lock();
1874 ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
1875 if (WARN_ON_ONCE(ssid == NULL))
1876 ssid_len = 0;
1877 else
1878 ssid_len = ssid[1];
1879
1880 ieee80211_send_probe_req(sdata, dst, ssid + 2, ssid_len, NULL,
1881 0, (u32) -1, true, 0,
1882 ifmgd->associated->channel, false);
1883 rcu_read_unlock();
1884 }
1885
1886 ifmgd->probe_timeout = jiffies + msecs_to_jiffies(probe_wait_ms);
1887 run_again(sdata, ifmgd->probe_timeout);
1888 if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
1889 ieee80211_flush_queues(sdata->local, sdata);
1890 }
1891
1892 static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
1893 bool beacon)
1894 {
1895 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1896 bool already = false;
1897
1898 if (!ieee80211_sdata_running(sdata))
1899 return;
1900
1901 sdata_lock(sdata);
1902
1903 if (!ifmgd->associated)
1904 goto out;
1905
1906 mutex_lock(&sdata->local->mtx);
1907
1908 if (sdata->local->tmp_channel || sdata->local->scanning) {
1909 mutex_unlock(&sdata->local->mtx);
1910 goto out;
1911 }
1912
1913 if (beacon) {
1914 mlme_dbg_ratelimited(sdata,
1915 "detected beacon loss from AP (missed %d beacons) - probing\n",
1916 beacon_loss_count);
1917
1918 ieee80211_cqm_rssi_notify(&sdata->vif,
1919 NL80211_CQM_RSSI_BEACON_LOSS_EVENT,
1920 GFP_KERNEL);
1921 }
1922
1923 /*
1924 * The driver/our work has already reported this event or the
1925 * connection monitoring has kicked in and we have already sent
1926 * a probe request. Or maybe the AP died and the driver keeps
1927 * reporting until we disassociate...
1928 *
1929 * In either case we have to ignore the current call to this
1930 * function (except for setting the correct probe reason bit)
1931 * because otherwise we would reset the timer every time and
1932 * never check whether we received a probe response!
1933 */
1934 if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL)
1935 already = true;
1936
1937 ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
1938
1939 mutex_unlock(&sdata->local->mtx);
1940
1941 if (already)
1942 goto out;
1943
1944 mutex_lock(&sdata->local->iflist_mtx);
1945 ieee80211_recalc_ps(sdata->local, -1);
1946 mutex_unlock(&sdata->local->iflist_mtx);
1947
1948 ifmgd->probe_send_count = 0;
1949 ieee80211_mgd_probe_ap_send(sdata);
1950 out:
1951 sdata_unlock(sdata);
1952 }
1953
1954 struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw,
1955 struct ieee80211_vif *vif)
1956 {
1957 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1958 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1959 struct cfg80211_bss *cbss;
1960 struct sk_buff *skb;
1961 const u8 *ssid;
1962 int ssid_len;
1963
1964 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
1965 return NULL;
1966
1967 sdata_assert_lock(sdata);
1968
1969 if (ifmgd->associated)
1970 cbss = ifmgd->associated;
1971 else if (ifmgd->auth_data)
1972 cbss = ifmgd->auth_data->bss;
1973 else if (ifmgd->assoc_data)
1974 cbss = ifmgd->assoc_data->bss;
1975 else
1976 return NULL;
1977
1978 rcu_read_lock();
1979 ssid = ieee80211_bss_get_ie(cbss, WLAN_EID_SSID);
1980 if (WARN_ON_ONCE(ssid == NULL))
1981 ssid_len = 0;
1982 else
1983 ssid_len = ssid[1];
1984
1985 skb = ieee80211_build_probe_req(sdata, cbss->bssid,
1986 (u32) -1, cbss->channel,
1987 ssid + 2, ssid_len,
1988 NULL, 0, true);
1989 rcu_read_unlock();
1990
1991 return skb;
1992 }
1993 EXPORT_SYMBOL(ieee80211_ap_probereq_get);
1994
1995 static void __ieee80211_disconnect(struct ieee80211_sub_if_data *sdata)
1996 {
1997 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1998 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
1999
2000 sdata_lock(sdata);
2001 if (!ifmgd->associated) {
2002 sdata_unlock(sdata);
2003 return;
2004 }
2005
2006 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
2007 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
2008 true, frame_buf);
2009 ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
2010 sdata->vif.csa_active = false;
2011 ieee80211_wake_queues_by_reason(&sdata->local->hw,
2012 IEEE80211_MAX_QUEUE_MAP,
2013 IEEE80211_QUEUE_STOP_REASON_CSA);
2014
2015 cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
2016 IEEE80211_DEAUTH_FRAME_LEN);
2017 sdata_unlock(sdata);
2018 }
2019
2020 static void ieee80211_beacon_connection_loss_work(struct work_struct *work)
2021 {
2022 struct ieee80211_sub_if_data *sdata =
2023 container_of(work, struct ieee80211_sub_if_data,
2024 u.mgd.beacon_connection_loss_work);
2025 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2026 struct sta_info *sta;
2027
2028 if (ifmgd->associated) {
2029 rcu_read_lock();
2030 sta = sta_info_get(sdata, ifmgd->bssid);
2031 if (sta)
2032 sta->beacon_loss_count++;
2033 rcu_read_unlock();
2034 }
2035
2036 if (ifmgd->connection_loss) {
2037 sdata_info(sdata, "Connection to AP %pM lost\n",
2038 ifmgd->bssid);
2039 __ieee80211_disconnect(sdata);
2040 } else {
2041 ieee80211_mgd_probe_ap(sdata, true);
2042 }
2043 }
2044
2045 static void ieee80211_csa_connection_drop_work(struct work_struct *work)
2046 {
2047 struct ieee80211_sub_if_data *sdata =
2048 container_of(work, struct ieee80211_sub_if_data,
2049 u.mgd.csa_connection_drop_work);
2050
2051 __ieee80211_disconnect(sdata);
2052 }
2053
2054 void ieee80211_beacon_loss(struct ieee80211_vif *vif)
2055 {
2056 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2057 struct ieee80211_hw *hw = &sdata->local->hw;
2058
2059 trace_api_beacon_loss(sdata);
2060
2061 sdata->u.mgd.connection_loss = false;
2062 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
2063 }
2064 EXPORT_SYMBOL(ieee80211_beacon_loss);
2065
2066 void ieee80211_connection_loss(struct ieee80211_vif *vif)
2067 {
2068 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2069 struct ieee80211_hw *hw = &sdata->local->hw;
2070
2071 trace_api_connection_loss(sdata);
2072
2073 sdata->u.mgd.connection_loss = true;
2074 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
2075 }
2076 EXPORT_SYMBOL(ieee80211_connection_loss);
2077
2078
2079 static void ieee80211_destroy_auth_data(struct ieee80211_sub_if_data *sdata,
2080 bool assoc)
2081 {
2082 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
2083
2084 sdata_assert_lock(sdata);
2085
2086 if (!assoc) {
2087 sta_info_destroy_addr(sdata, auth_data->bss->bssid);
2088
2089 memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
2090 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
2091 sdata->u.mgd.flags = 0;
2092 mutex_lock(&sdata->local->mtx);
2093 ieee80211_vif_release_channel(sdata);
2094 mutex_unlock(&sdata->local->mtx);
2095 }
2096
2097 cfg80211_put_bss(sdata->local->hw.wiphy, auth_data->bss);
2098 kfree(auth_data);
2099 sdata->u.mgd.auth_data = NULL;
2100 }
2101
2102 static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
2103 struct ieee80211_mgmt *mgmt, size_t len)
2104 {
2105 struct ieee80211_local *local = sdata->local;
2106 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
2107 u8 *pos;
2108 struct ieee802_11_elems elems;
2109 u32 tx_flags = 0;
2110
2111 pos = mgmt->u.auth.variable;
2112 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), false, &elems);
2113 if (!elems.challenge)
2114 return;
2115 auth_data->expected_transaction = 4;
2116 drv_mgd_prepare_tx(sdata->local, sdata);
2117 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
2118 tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
2119 IEEE80211_TX_INTFL_MLME_CONN_TX;
2120 ieee80211_send_auth(sdata, 3, auth_data->algorithm, 0,
2121 elems.challenge - 2, elems.challenge_len + 2,
2122 auth_data->bss->bssid, auth_data->bss->bssid,
2123 auth_data->key, auth_data->key_len,
2124 auth_data->key_idx, tx_flags);
2125 }
2126
2127 static void ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
2128 struct ieee80211_mgmt *mgmt, size_t len)
2129 {
2130 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2131 u8 bssid[ETH_ALEN];
2132 u16 auth_alg, auth_transaction, status_code;
2133 struct sta_info *sta;
2134
2135 sdata_assert_lock(sdata);
2136
2137 if (len < 24 + 6)
2138 return;
2139
2140 if (!ifmgd->auth_data || ifmgd->auth_data->done)
2141 return;
2142
2143 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
2144
2145 if (!ether_addr_equal(bssid, mgmt->bssid))
2146 return;
2147
2148 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
2149 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
2150 status_code = le16_to_cpu(mgmt->u.auth.status_code);
2151
2152 if (auth_alg != ifmgd->auth_data->algorithm ||
2153 auth_transaction != ifmgd->auth_data->expected_transaction) {
2154 sdata_info(sdata, "%pM unexpected authentication state: alg %d (expected %d) transact %d (expected %d)\n",
2155 mgmt->sa, auth_alg, ifmgd->auth_data->algorithm,
2156 auth_transaction,
2157 ifmgd->auth_data->expected_transaction);
2158 return;
2159 }
2160
2161 if (status_code != WLAN_STATUS_SUCCESS) {
2162 sdata_info(sdata, "%pM denied authentication (status %d)\n",
2163 mgmt->sa, status_code);
2164 ieee80211_destroy_auth_data(sdata, false);
2165 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
2166 return;
2167 }
2168
2169 switch (ifmgd->auth_data->algorithm) {
2170 case WLAN_AUTH_OPEN:
2171 case WLAN_AUTH_LEAP:
2172 case WLAN_AUTH_FT:
2173 case WLAN_AUTH_SAE:
2174 break;
2175 case WLAN_AUTH_SHARED_KEY:
2176 if (ifmgd->auth_data->expected_transaction != 4) {
2177 ieee80211_auth_challenge(sdata, mgmt, len);
2178 /* need another frame */
2179 return;
2180 }
2181 break;
2182 default:
2183 WARN_ONCE(1, "invalid auth alg %d",
2184 ifmgd->auth_data->algorithm);
2185 return;
2186 }
2187
2188 sdata_info(sdata, "authenticated\n");
2189 ifmgd->auth_data->done = true;
2190 ifmgd->auth_data->timeout = jiffies + IEEE80211_AUTH_WAIT_ASSOC;
2191 ifmgd->auth_data->timeout_started = true;
2192 run_again(sdata, ifmgd->auth_data->timeout);
2193
2194 if (ifmgd->auth_data->algorithm == WLAN_AUTH_SAE &&
2195 ifmgd->auth_data->expected_transaction != 2) {
2196 /*
2197 * Report auth frame to user space for processing since another
2198 * round of Authentication frames is still needed.
2199 */
2200 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
2201 return;
2202 }
2203
2204 /* move station state to auth */
2205 mutex_lock(&sdata->local->sta_mtx);
2206 sta = sta_info_get(sdata, bssid);
2207 if (!sta) {
2208 WARN_ONCE(1, "%s: STA %pM not found", sdata->name, bssid);
2209 goto out_err;
2210 }
2211 if (sta_info_move_state(sta, IEEE80211_STA_AUTH)) {
2212 sdata_info(sdata, "failed moving %pM to auth\n", bssid);
2213 goto out_err;
2214 }
2215 mutex_unlock(&sdata->local->sta_mtx);
2216
2217 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
2218 return;
2219 out_err:
2220 mutex_unlock(&sdata->local->sta_mtx);
2221 /* ignore frame -- wait for timeout */
2222 }
2223
2224
2225 static void ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
2226 struct ieee80211_mgmt *mgmt, size_t len)
2227 {
2228 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2229 const u8 *bssid = NULL;
2230 u16 reason_code;
2231
2232 sdata_assert_lock(sdata);
2233
2234 if (len < 24 + 2)
2235 return;
2236
2237 if (!ifmgd->associated ||
2238 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
2239 return;
2240
2241 bssid = ifmgd->associated->bssid;
2242
2243 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
2244
2245 sdata_info(sdata, "deauthenticated from %pM (Reason: %u)\n",
2246 bssid, reason_code);
2247
2248 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
2249
2250 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
2251 }
2252
2253
2254 static void ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
2255 struct ieee80211_mgmt *mgmt, size_t len)
2256 {
2257 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2258 u16 reason_code;
2259
2260 sdata_assert_lock(sdata);
2261
2262 if (len < 24 + 2)
2263 return;
2264
2265 if (!ifmgd->associated ||
2266 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
2267 return;
2268
2269 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
2270
2271 sdata_info(sdata, "disassociated from %pM (Reason: %u)\n",
2272 mgmt->sa, reason_code);
2273
2274 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
2275
2276 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
2277 }
2278
2279 static void ieee80211_get_rates(struct ieee80211_supported_band *sband,
2280 u8 *supp_rates, unsigned int supp_rates_len,
2281 u32 *rates, u32 *basic_rates,
2282 bool *have_higher_than_11mbit,
2283 int *min_rate, int *min_rate_index,
2284 int shift, u32 rate_flags)
2285 {
2286 int i, j;
2287
2288 for (i = 0; i < supp_rates_len; i++) {
2289 int rate = supp_rates[i] & 0x7f;
2290 bool is_basic = !!(supp_rates[i] & 0x80);
2291
2292 if ((rate * 5 * (1 << shift)) > 110)
2293 *have_higher_than_11mbit = true;
2294
2295 /*
2296 * BSS_MEMBERSHIP_SELECTOR_HT_PHY is defined in 802.11n-2009
2297 * 7.3.2.2 as a magic value instead of a rate. Hence, skip it.
2298 *
2299 * Note: Even through the membership selector and the basic
2300 * rate flag share the same bit, they are not exactly
2301 * the same.
2302 */
2303 if (!!(supp_rates[i] & 0x80) &&
2304 (supp_rates[i] & 0x7f) == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
2305 continue;
2306
2307 for (j = 0; j < sband->n_bitrates; j++) {
2308 struct ieee80211_rate *br;
2309 int brate;
2310
2311 br = &sband->bitrates[j];
2312 if ((rate_flags & br->flags) != rate_flags)
2313 continue;
2314
2315 brate = DIV_ROUND_UP(br->bitrate, (1 << shift) * 5);
2316 if (brate == rate) {
2317 *rates |= BIT(j);
2318 if (is_basic)
2319 *basic_rates |= BIT(j);
2320 if ((rate * 5) < *min_rate) {
2321 *min_rate = rate * 5;
2322 *min_rate_index = j;
2323 }
2324 break;
2325 }
2326 }
2327 }
2328 }
2329
2330 static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata,
2331 bool assoc)
2332 {
2333 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
2334
2335 sdata_assert_lock(sdata);
2336
2337 if (!assoc) {
2338 sta_info_destroy_addr(sdata, assoc_data->bss->bssid);
2339
2340 memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
2341 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
2342 sdata->u.mgd.flags = 0;
2343 mutex_lock(&sdata->local->mtx);
2344 ieee80211_vif_release_channel(sdata);
2345 mutex_unlock(&sdata->local->mtx);
2346 }
2347
2348 kfree(assoc_data);
2349 sdata->u.mgd.assoc_data = NULL;
2350 }
2351
2352 static bool ieee80211_assoc_success(struct ieee80211_sub_if_data *sdata,
2353 struct cfg80211_bss *cbss,
2354 struct ieee80211_mgmt *mgmt, size_t len)
2355 {
2356 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2357 struct ieee80211_local *local = sdata->local;
2358 struct ieee80211_supported_band *sband;
2359 struct sta_info *sta;
2360 u8 *pos;
2361 u16 capab_info, aid;
2362 struct ieee802_11_elems elems;
2363 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
2364 const struct cfg80211_bss_ies *bss_ies = NULL;
2365 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
2366 u32 changed = 0;
2367 int err;
2368 bool ret;
2369
2370 /* AssocResp and ReassocResp have identical structure */
2371
2372 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
2373 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
2374
2375 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
2376 sdata_info(sdata, "invalid AID value 0x%x; bits 15:14 not set\n",
2377 aid);
2378 aid &= ~(BIT(15) | BIT(14));
2379
2380 ifmgd->broken_ap = false;
2381
2382 if (aid == 0 || aid > IEEE80211_MAX_AID) {
2383 sdata_info(sdata, "invalid AID value %d (out of range), turn off PS\n",
2384 aid);
2385 aid = 0;
2386 ifmgd->broken_ap = true;
2387 }
2388
2389 pos = mgmt->u.assoc_resp.variable;
2390 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), false, &elems);
2391
2392 if (!elems.supp_rates) {
2393 sdata_info(sdata, "no SuppRates element in AssocResp\n");
2394 return false;
2395 }
2396
2397 ifmgd->aid = aid;
2398
2399 /*
2400 * Some APs are erroneously not including some information in their
2401 * (re)association response frames. Try to recover by using the data
2402 * from the beacon or probe response. This seems to afflict mobile
2403 * 2G/3G/4G wifi routers, reported models include the "Onda PN51T",
2404 * "Vodafone PocketWiFi 2", "ZTE MF60" and a similar T-Mobile device.
2405 */
2406 if ((assoc_data->wmm && !elems.wmm_param) ||
2407 (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
2408 (!elems.ht_cap_elem || !elems.ht_operation)) ||
2409 (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
2410 (!elems.vht_cap_elem || !elems.vht_operation))) {
2411 const struct cfg80211_bss_ies *ies;
2412 struct ieee802_11_elems bss_elems;
2413
2414 rcu_read_lock();
2415 ies = rcu_dereference(cbss->ies);
2416 if (ies)
2417 bss_ies = kmemdup(ies, sizeof(*ies) + ies->len,
2418 GFP_ATOMIC);
2419 rcu_read_unlock();
2420 if (!bss_ies)
2421 return false;
2422
2423 ieee802_11_parse_elems(bss_ies->data, bss_ies->len,
2424 false, &bss_elems);
2425 if (assoc_data->wmm &&
2426 !elems.wmm_param && bss_elems.wmm_param) {
2427 elems.wmm_param = bss_elems.wmm_param;
2428 sdata_info(sdata,
2429 "AP bug: WMM param missing from AssocResp\n");
2430 }
2431
2432 /*
2433 * Also check if we requested HT/VHT, otherwise the AP doesn't
2434 * have to include the IEs in the (re)association response.
2435 */
2436 if (!elems.ht_cap_elem && bss_elems.ht_cap_elem &&
2437 !(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
2438 elems.ht_cap_elem = bss_elems.ht_cap_elem;
2439 sdata_info(sdata,
2440 "AP bug: HT capability missing from AssocResp\n");
2441 }
2442 if (!elems.ht_operation && bss_elems.ht_operation &&
2443 !(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
2444 elems.ht_operation = bss_elems.ht_operation;
2445 sdata_info(sdata,
2446 "AP bug: HT operation missing from AssocResp\n");
2447 }
2448 if (!elems.vht_cap_elem && bss_elems.vht_cap_elem &&
2449 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)) {
2450 elems.vht_cap_elem = bss_elems.vht_cap_elem;
2451 sdata_info(sdata,
2452 "AP bug: VHT capa missing from AssocResp\n");
2453 }
2454 if (!elems.vht_operation && bss_elems.vht_operation &&
2455 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)) {
2456 elems.vht_operation = bss_elems.vht_operation;
2457 sdata_info(sdata,
2458 "AP bug: VHT operation missing from AssocResp\n");
2459 }
2460 }
2461
2462 /*
2463 * We previously checked these in the beacon/probe response, so
2464 * they should be present here. This is just a safety net.
2465 */
2466 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
2467 (!elems.wmm_param || !elems.ht_cap_elem || !elems.ht_operation)) {
2468 sdata_info(sdata,
2469 "HT AP is missing WMM params or HT capability/operation\n");
2470 ret = false;
2471 goto out;
2472 }
2473
2474 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
2475 (!elems.vht_cap_elem || !elems.vht_operation)) {
2476 sdata_info(sdata,
2477 "VHT AP is missing VHT capability/operation\n");
2478 ret = false;
2479 goto out;
2480 }
2481
2482 mutex_lock(&sdata->local->sta_mtx);
2483 /*
2484 * station info was already allocated and inserted before
2485 * the association and should be available to us
2486 */
2487 sta = sta_info_get(sdata, cbss->bssid);
2488 if (WARN_ON(!sta)) {
2489 mutex_unlock(&sdata->local->sta_mtx);
2490 ret = false;
2491 goto out;
2492 }
2493
2494 sband = local->hw.wiphy->bands[ieee80211_get_sdata_band(sdata)];
2495
2496 /* Set up internal HT/VHT capabilities */
2497 if (elems.ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
2498 ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
2499 elems.ht_cap_elem, sta);
2500
2501 if (elems.vht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
2502 ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband,
2503 elems.vht_cap_elem, sta);
2504
2505 /*
2506 * Some APs, e.g. Netgear WNDR3700, report invalid HT operation data
2507 * in their association response, so ignore that data for our own
2508 * configuration. If it changed since the last beacon, we'll get the
2509 * next beacon and update then.
2510 */
2511
2512 /*
2513 * If an operating mode notification IE is present, override the
2514 * NSS calculation (that would be done in rate_control_rate_init())
2515 * and use the # of streams from that element.
2516 */
2517 if (elems.opmode_notif &&
2518 !(*elems.opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_TYPE_BF)) {
2519 u8 nss;
2520
2521 nss = *elems.opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_MASK;
2522 nss >>= IEEE80211_OPMODE_NOTIF_RX_NSS_SHIFT;
2523 nss += 1;
2524 sta->sta.rx_nss = nss;
2525 }
2526
2527 rate_control_rate_init(sta);
2528
2529 if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED)
2530 set_sta_flag(sta, WLAN_STA_MFP);
2531
2532 if (elems.wmm_param)
2533 set_sta_flag(sta, WLAN_STA_WME);
2534
2535 err = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
2536 if (!err && !(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
2537 err = sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
2538 if (err) {
2539 sdata_info(sdata,
2540 "failed to move station %pM to desired state\n",
2541 sta->sta.addr);
2542 WARN_ON(__sta_info_destroy(sta));
2543 mutex_unlock(&sdata->local->sta_mtx);
2544 ret = false;
2545 goto out;
2546 }
2547
2548 mutex_unlock(&sdata->local->sta_mtx);
2549
2550 /*
2551 * Always handle WMM once after association regardless
2552 * of the first value the AP uses. Setting -1 here has
2553 * that effect because the AP values is an unsigned
2554 * 4-bit value.
2555 */
2556 ifmgd->wmm_last_param_set = -1;
2557
2558 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_WMM) && elems.wmm_param)
2559 ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
2560 elems.wmm_param_len);
2561 else
2562 ieee80211_set_wmm_default(sdata, false);
2563 changed |= BSS_CHANGED_QOS;
2564
2565 /* set AID and assoc capability,
2566 * ieee80211_set_associated() will tell the driver */
2567 bss_conf->aid = aid;
2568 bss_conf->assoc_capability = capab_info;
2569 ieee80211_set_associated(sdata, cbss, changed);
2570
2571 /*
2572 * If we're using 4-addr mode, let the AP know that we're
2573 * doing so, so that it can create the STA VLAN on its side
2574 */
2575 if (ifmgd->use_4addr)
2576 ieee80211_send_4addr_nullfunc(local, sdata);
2577
2578 /*
2579 * Start timer to probe the connection to the AP now.
2580 * Also start the timer that will detect beacon loss.
2581 */
2582 ieee80211_sta_rx_notify(sdata, (struct ieee80211_hdr *)mgmt);
2583 ieee80211_sta_reset_beacon_monitor(sdata);
2584
2585 ret = true;
2586 out:
2587 kfree(bss_ies);
2588 return ret;
2589 }
2590
2591 static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
2592 struct ieee80211_mgmt *mgmt,
2593 size_t len)
2594 {
2595 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2596 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
2597 u16 capab_info, status_code, aid;
2598 struct ieee802_11_elems elems;
2599 u8 *pos;
2600 bool reassoc;
2601 struct cfg80211_bss *bss;
2602
2603 sdata_assert_lock(sdata);
2604
2605 if (!assoc_data)
2606 return;
2607 if (!ether_addr_equal(assoc_data->bss->bssid, mgmt->bssid))
2608 return;
2609
2610 /*
2611 * AssocResp and ReassocResp have identical structure, so process both
2612 * of them in this function.
2613 */
2614
2615 if (len < 24 + 6)
2616 return;
2617
2618 reassoc = ieee80211_is_reassoc_req(mgmt->frame_control);
2619 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
2620 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
2621 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
2622
2623 sdata_info(sdata,
2624 "RX %sssocResp from %pM (capab=0x%x status=%d aid=%d)\n",
2625 reassoc ? "Rea" : "A", mgmt->sa,
2626 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
2627
2628 pos = mgmt->u.assoc_resp.variable;
2629 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), false, &elems);
2630
2631 if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
2632 elems.timeout_int &&
2633 elems.timeout_int->type == WLAN_TIMEOUT_ASSOC_COMEBACK) {
2634 u32 tu, ms;
2635 tu = le32_to_cpu(elems.timeout_int->value);
2636 ms = tu * 1024 / 1000;
2637 sdata_info(sdata,
2638 "%pM rejected association temporarily; comeback duration %u TU (%u ms)\n",
2639 mgmt->sa, tu, ms);
2640 assoc_data->timeout = jiffies + msecs_to_jiffies(ms);
2641 assoc_data->timeout_started = true;
2642 if (ms > IEEE80211_ASSOC_TIMEOUT)
2643 run_again(sdata, assoc_data->timeout);
2644 return;
2645 }
2646
2647 bss = assoc_data->bss;
2648
2649 if (status_code != WLAN_STATUS_SUCCESS) {
2650 sdata_info(sdata, "%pM denied association (code=%d)\n",
2651 mgmt->sa, status_code);
2652 ieee80211_destroy_assoc_data(sdata, false);
2653 } else {
2654 if (!ieee80211_assoc_success(sdata, bss, mgmt, len)) {
2655 /* oops -- internal error -- send timeout for now */
2656 ieee80211_destroy_assoc_data(sdata, false);
2657 cfg80211_assoc_timeout(sdata->dev, bss);
2658 return;
2659 }
2660 sdata_info(sdata, "associated\n");
2661
2662 /*
2663 * destroy assoc_data afterwards, as otherwise an idle
2664 * recalc after assoc_data is NULL but before associated
2665 * is set can cause the interface to go idle
2666 */
2667 ieee80211_destroy_assoc_data(sdata, true);
2668 }
2669
2670 cfg80211_rx_assoc_resp(sdata->dev, bss, (u8 *)mgmt, len);
2671 }
2672
2673 static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
2674 struct ieee80211_mgmt *mgmt, size_t len,
2675 struct ieee80211_rx_status *rx_status,
2676 struct ieee802_11_elems *elems)
2677 {
2678 struct ieee80211_local *local = sdata->local;
2679 int freq;
2680 struct ieee80211_bss *bss;
2681 struct ieee80211_channel *channel;
2682
2683 sdata_assert_lock(sdata);
2684
2685 if (elems->ds_params)
2686 freq = ieee80211_channel_to_frequency(elems->ds_params[0],
2687 rx_status->band);
2688 else
2689 freq = rx_status->freq;
2690
2691 channel = ieee80211_get_channel(local->hw.wiphy, freq);
2692
2693 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
2694 return;
2695
2696 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
2697 channel);
2698 if (bss) {
2699 ieee80211_rx_bss_put(local, bss);
2700 sdata->vif.bss_conf.beacon_rate = bss->beacon_rate;
2701 }
2702 }
2703
2704
2705 static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
2706 struct sk_buff *skb)
2707 {
2708 struct ieee80211_mgmt *mgmt = (void *)skb->data;
2709 struct ieee80211_if_managed *ifmgd;
2710 struct ieee80211_rx_status *rx_status = (void *) skb->cb;
2711 size_t baselen, len = skb->len;
2712 struct ieee802_11_elems elems;
2713
2714 ifmgd = &sdata->u.mgd;
2715
2716 sdata_assert_lock(sdata);
2717
2718 if (!ether_addr_equal(mgmt->da, sdata->vif.addr))
2719 return; /* ignore ProbeResp to foreign address */
2720
2721 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
2722 if (baselen > len)
2723 return;
2724
2725 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
2726 false, &elems);
2727
2728 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
2729
2730 if (ifmgd->associated &&
2731 ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
2732 ieee80211_reset_ap_probe(sdata);
2733
2734 if (ifmgd->auth_data && !ifmgd->auth_data->bss->proberesp_ies &&
2735 ether_addr_equal(mgmt->bssid, ifmgd->auth_data->bss->bssid)) {
2736 /* got probe response, continue with auth */
2737 sdata_info(sdata, "direct probe responded\n");
2738 ifmgd->auth_data->tries = 0;
2739 ifmgd->auth_data->timeout = jiffies;
2740 ifmgd->auth_data->timeout_started = true;
2741 run_again(sdata, ifmgd->auth_data->timeout);
2742 }
2743 }
2744
2745 /*
2746 * This is the canonical list of information elements we care about,
2747 * the filter code also gives us all changes to the Microsoft OUI
2748 * (00:50:F2) vendor IE which is used for WMM which we need to track.
2749 *
2750 * We implement beacon filtering in software since that means we can
2751 * avoid processing the frame here and in cfg80211, and userspace
2752 * will not be able to tell whether the hardware supports it or not.
2753 *
2754 * XXX: This list needs to be dynamic -- userspace needs to be able to
2755 * add items it requires. It also needs to be able to tell us to
2756 * look out for other vendor IEs.
2757 */
2758 static const u64 care_about_ies =
2759 (1ULL << WLAN_EID_COUNTRY) |
2760 (1ULL << WLAN_EID_ERP_INFO) |
2761 (1ULL << WLAN_EID_CHANNEL_SWITCH) |
2762 (1ULL << WLAN_EID_PWR_CONSTRAINT) |
2763 (1ULL << WLAN_EID_HT_CAPABILITY) |
2764 (1ULL << WLAN_EID_HT_OPERATION);
2765
2766 static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
2767 struct ieee80211_mgmt *mgmt, size_t len,
2768 struct ieee80211_rx_status *rx_status)
2769 {
2770 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2771 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
2772 size_t baselen;
2773 struct ieee802_11_elems elems;
2774 struct ieee80211_local *local = sdata->local;
2775 struct ieee80211_chanctx_conf *chanctx_conf;
2776 struct ieee80211_channel *chan;
2777 struct sta_info *sta;
2778 u32 changed = 0;
2779 bool erp_valid;
2780 u8 erp_value = 0;
2781 u32 ncrc;
2782 u8 *bssid;
2783 u8 deauth_buf[IEEE80211_DEAUTH_FRAME_LEN];
2784
2785 sdata_assert_lock(sdata);
2786
2787 /* Process beacon from the current BSS */
2788 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2789 if (baselen > len)
2790 return;
2791
2792 rcu_read_lock();
2793 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2794 if (!chanctx_conf) {
2795 rcu_read_unlock();
2796 return;
2797 }
2798
2799 if (rx_status->freq != chanctx_conf->def.chan->center_freq) {
2800 rcu_read_unlock();
2801 return;
2802 }
2803 chan = chanctx_conf->def.chan;
2804 rcu_read_unlock();
2805
2806 if (ifmgd->assoc_data && ifmgd->assoc_data->need_beacon &&
2807 ether_addr_equal(mgmt->bssid, ifmgd->assoc_data->bss->bssid)) {
2808 ieee802_11_parse_elems(mgmt->u.beacon.variable,
2809 len - baselen, false, &elems);
2810
2811 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
2812 if (elems.tim && !elems.parse_error) {
2813 const struct ieee80211_tim_ie *tim_ie = elems.tim;
2814 ifmgd->dtim_period = tim_ie->dtim_period;
2815 }
2816 ifmgd->have_beacon = true;
2817 ifmgd->assoc_data->need_beacon = false;
2818 if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
2819 sdata->vif.bss_conf.sync_tsf =
2820 le64_to_cpu(mgmt->u.beacon.timestamp);
2821 sdata->vif.bss_conf.sync_device_ts =
2822 rx_status->device_timestamp;
2823 if (elems.tim)
2824 sdata->vif.bss_conf.sync_dtim_count =
2825 elems.tim->dtim_count;
2826 else
2827 sdata->vif.bss_conf.sync_dtim_count = 0;
2828 }
2829 /* continue assoc process */
2830 ifmgd->assoc_data->timeout = jiffies;
2831 ifmgd->assoc_data->timeout_started = true;
2832 run_again(sdata, ifmgd->assoc_data->timeout);
2833 return;
2834 }
2835
2836 if (!ifmgd->associated ||
2837 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
2838 return;
2839 bssid = ifmgd->associated->bssid;
2840
2841 /* Track average RSSI from the Beacon frames of the current AP */
2842 ifmgd->last_beacon_signal = rx_status->signal;
2843 if (ifmgd->flags & IEEE80211_STA_RESET_SIGNAL_AVE) {
2844 ifmgd->flags &= ~IEEE80211_STA_RESET_SIGNAL_AVE;
2845 ifmgd->ave_beacon_signal = rx_status->signal * 16;
2846 ifmgd->last_cqm_event_signal = 0;
2847 ifmgd->count_beacon_signal = 1;
2848 ifmgd->last_ave_beacon_signal = 0;
2849 } else {
2850 ifmgd->ave_beacon_signal =
2851 (IEEE80211_SIGNAL_AVE_WEIGHT * rx_status->signal * 16 +
2852 (16 - IEEE80211_SIGNAL_AVE_WEIGHT) *
2853 ifmgd->ave_beacon_signal) / 16;
2854 ifmgd->count_beacon_signal++;
2855 }
2856
2857 if (ifmgd->rssi_min_thold != ifmgd->rssi_max_thold &&
2858 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
2859 int sig = ifmgd->ave_beacon_signal;
2860 int last_sig = ifmgd->last_ave_beacon_signal;
2861
2862 /*
2863 * if signal crosses either of the boundaries, invoke callback
2864 * with appropriate parameters
2865 */
2866 if (sig > ifmgd->rssi_max_thold &&
2867 (last_sig <= ifmgd->rssi_min_thold || last_sig == 0)) {
2868 ifmgd->last_ave_beacon_signal = sig;
2869 drv_rssi_callback(local, sdata, RSSI_EVENT_HIGH);
2870 } else if (sig < ifmgd->rssi_min_thold &&
2871 (last_sig >= ifmgd->rssi_max_thold ||
2872 last_sig == 0)) {
2873 ifmgd->last_ave_beacon_signal = sig;
2874 drv_rssi_callback(local, sdata, RSSI_EVENT_LOW);
2875 }
2876 }
2877
2878 if (bss_conf->cqm_rssi_thold &&
2879 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT &&
2880 !(sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI)) {
2881 int sig = ifmgd->ave_beacon_signal / 16;
2882 int last_event = ifmgd->last_cqm_event_signal;
2883 int thold = bss_conf->cqm_rssi_thold;
2884 int hyst = bss_conf->cqm_rssi_hyst;
2885 if (sig < thold &&
2886 (last_event == 0 || sig < last_event - hyst)) {
2887 ifmgd->last_cqm_event_signal = sig;
2888 ieee80211_cqm_rssi_notify(
2889 &sdata->vif,
2890 NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
2891 GFP_KERNEL);
2892 } else if (sig > thold &&
2893 (last_event == 0 || sig > last_event + hyst)) {
2894 ifmgd->last_cqm_event_signal = sig;
2895 ieee80211_cqm_rssi_notify(
2896 &sdata->vif,
2897 NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
2898 GFP_KERNEL);
2899 }
2900 }
2901
2902 if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL) {
2903 mlme_dbg_ratelimited(sdata,
2904 "cancelling AP probe due to a received beacon\n");
2905 ieee80211_reset_ap_probe(sdata);
2906 }
2907
2908 /*
2909 * Push the beacon loss detection into the future since
2910 * we are processing a beacon from the AP just now.
2911 */
2912 ieee80211_sta_reset_beacon_monitor(sdata);
2913
2914 ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
2915 ncrc = ieee802_11_parse_elems_crc(mgmt->u.beacon.variable,
2916 len - baselen, false, &elems,
2917 care_about_ies, ncrc);
2918
2919 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) {
2920 bool directed_tim = ieee80211_check_tim(elems.tim,
2921 elems.tim_len,
2922 ifmgd->aid);
2923 if (directed_tim) {
2924 if (local->hw.conf.dynamic_ps_timeout > 0) {
2925 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
2926 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
2927 ieee80211_hw_config(local,
2928 IEEE80211_CONF_CHANGE_PS);
2929 }
2930 ieee80211_send_nullfunc(local, sdata, 0);
2931 } else if (!local->pspolling && sdata->u.mgd.powersave) {
2932 local->pspolling = true;
2933
2934 /*
2935 * Here is assumed that the driver will be
2936 * able to send ps-poll frame and receive a
2937 * response even though power save mode is
2938 * enabled, but some drivers might require
2939 * to disable power save here. This needs
2940 * to be investigated.
2941 */
2942 ieee80211_send_pspoll(local, sdata);
2943 }
2944 }
2945 }
2946
2947 if (sdata->vif.p2p) {
2948 struct ieee80211_p2p_noa_attr noa = {};
2949 int ret;
2950
2951 ret = cfg80211_get_p2p_attr(mgmt->u.beacon.variable,
2952 len - baselen,
2953 IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
2954 (u8 *) &noa, sizeof(noa));
2955 if (ret >= 2) {
2956 if (sdata->u.mgd.p2p_noa_index != noa.index) {
2957 /* valid noa_attr and index changed */
2958 sdata->u.mgd.p2p_noa_index = noa.index;
2959 memcpy(&bss_conf->p2p_noa_attr, &noa, sizeof(noa));
2960 changed |= BSS_CHANGED_P2P_PS;
2961 /*
2962 * make sure we update all information, the CRC
2963 * mechanism doesn't look at P2P attributes.
2964 */
2965 ifmgd->beacon_crc_valid = false;
2966 }
2967 } else if (sdata->u.mgd.p2p_noa_index != -1) {
2968 /* noa_attr not found and we had valid noa_attr before */
2969 sdata->u.mgd.p2p_noa_index = -1;
2970 memset(&bss_conf->p2p_noa_attr, 0, sizeof(bss_conf->p2p_noa_attr));
2971 changed |= BSS_CHANGED_P2P_PS;
2972 ifmgd->beacon_crc_valid = false;
2973 }
2974 }
2975
2976 if (ncrc == ifmgd->beacon_crc && ifmgd->beacon_crc_valid)
2977 return;
2978 ifmgd->beacon_crc = ncrc;
2979 ifmgd->beacon_crc_valid = true;
2980
2981 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
2982
2983 ieee80211_sta_process_chanswitch(sdata, rx_status->mactime,
2984 &elems, true);
2985
2986 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_WMM) &&
2987 ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
2988 elems.wmm_param_len))
2989 changed |= BSS_CHANGED_QOS;
2990
2991 /*
2992 * If we haven't had a beacon before, tell the driver about the
2993 * DTIM period (and beacon timing if desired) now.
2994 */
2995 if (!ifmgd->have_beacon) {
2996 /* a few bogus AP send dtim_period = 0 or no TIM IE */
2997 if (elems.tim)
2998 bss_conf->dtim_period = elems.tim->dtim_period ?: 1;
2999 else
3000 bss_conf->dtim_period = 1;
3001
3002 if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
3003 sdata->vif.bss_conf.sync_tsf =
3004 le64_to_cpu(mgmt->u.beacon.timestamp);
3005 sdata->vif.bss_conf.sync_device_ts =
3006 rx_status->device_timestamp;
3007 if (elems.tim)
3008 sdata->vif.bss_conf.sync_dtim_count =
3009 elems.tim->dtim_count;
3010 else
3011 sdata->vif.bss_conf.sync_dtim_count = 0;
3012 }
3013
3014 changed |= BSS_CHANGED_BEACON_INFO;
3015 ifmgd->have_beacon = true;
3016
3017 mutex_lock(&local->iflist_mtx);
3018 ieee80211_recalc_ps(local, -1);
3019 mutex_unlock(&local->iflist_mtx);
3020
3021 ieee80211_recalc_ps_vif(sdata);
3022 }
3023
3024 if (elems.erp_info) {
3025 erp_valid = true;
3026 erp_value = elems.erp_info[0];
3027 } else {
3028 erp_valid = false;
3029 }
3030 changed |= ieee80211_handle_bss_capability(sdata,
3031 le16_to_cpu(mgmt->u.beacon.capab_info),
3032 erp_valid, erp_value);
3033
3034 mutex_lock(&local->sta_mtx);
3035 sta = sta_info_get(sdata, bssid);
3036
3037 if (ieee80211_config_bw(sdata, sta, elems.ht_operation,
3038 elems.vht_operation, bssid, &changed)) {
3039 mutex_unlock(&local->sta_mtx);
3040 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
3041 WLAN_REASON_DEAUTH_LEAVING,
3042 true, deauth_buf);
3043 cfg80211_tx_mlme_mgmt(sdata->dev, deauth_buf,
3044 sizeof(deauth_buf));
3045 return;
3046 }
3047
3048 if (sta && elems.opmode_notif)
3049 ieee80211_vht_handle_opmode(sdata, sta, *elems.opmode_notif,
3050 rx_status->band, true);
3051 mutex_unlock(&local->sta_mtx);
3052
3053 if (elems.country_elem && elems.pwr_constr_elem &&
3054 mgmt->u.probe_resp.capab_info &
3055 cpu_to_le16(WLAN_CAPABILITY_SPECTRUM_MGMT))
3056 changed |= ieee80211_handle_pwr_constr(sdata, chan,
3057 elems.country_elem,
3058 elems.country_elem_len,
3059 elems.pwr_constr_elem);
3060
3061 ieee80211_bss_info_change_notify(sdata, changed);
3062 }
3063
3064 void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
3065 struct sk_buff *skb)
3066 {
3067 struct ieee80211_rx_status *rx_status;
3068 struct ieee80211_mgmt *mgmt;
3069 u16 fc;
3070 struct ieee802_11_elems elems;
3071 int ies_len;
3072
3073 rx_status = (struct ieee80211_rx_status *) skb->cb;
3074 mgmt = (struct ieee80211_mgmt *) skb->data;
3075 fc = le16_to_cpu(mgmt->frame_control);
3076
3077 sdata_lock(sdata);
3078
3079 switch (fc & IEEE80211_FCTL_STYPE) {
3080 case IEEE80211_STYPE_BEACON:
3081 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len, rx_status);
3082 break;
3083 case IEEE80211_STYPE_PROBE_RESP:
3084 ieee80211_rx_mgmt_probe_resp(sdata, skb);
3085 break;
3086 case IEEE80211_STYPE_AUTH:
3087 ieee80211_rx_mgmt_auth(sdata, mgmt, skb->len);
3088 break;
3089 case IEEE80211_STYPE_DEAUTH:
3090 ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
3091 break;
3092 case IEEE80211_STYPE_DISASSOC:
3093 ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
3094 break;
3095 case IEEE80211_STYPE_ASSOC_RESP:
3096 case IEEE80211_STYPE_REASSOC_RESP:
3097 ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len);
3098 break;
3099 case IEEE80211_STYPE_ACTION:
3100 if (mgmt->u.action.category == WLAN_CATEGORY_SPECTRUM_MGMT) {
3101 ies_len = skb->len -
3102 offsetof(struct ieee80211_mgmt,
3103 u.action.u.chan_switch.variable);
3104
3105 if (ies_len < 0)
3106 break;
3107
3108 ieee802_11_parse_elems(
3109 mgmt->u.action.u.chan_switch.variable,
3110 ies_len, true, &elems);
3111
3112 if (elems.parse_error)
3113 break;
3114
3115 ieee80211_sta_process_chanswitch(sdata,
3116 rx_status->mactime,
3117 &elems, false);
3118 } else if (mgmt->u.action.category == WLAN_CATEGORY_PUBLIC) {
3119 ies_len = skb->len -
3120 offsetof(struct ieee80211_mgmt,
3121 u.action.u.ext_chan_switch.variable);
3122
3123 if (ies_len < 0)
3124 break;
3125
3126 ieee802_11_parse_elems(
3127 mgmt->u.action.u.ext_chan_switch.variable,
3128 ies_len, true, &elems);
3129
3130 if (elems.parse_error)
3131 break;
3132
3133 /* for the handling code pretend this was also an IE */
3134 elems.ext_chansw_ie =
3135 &mgmt->u.action.u.ext_chan_switch.data;
3136
3137 ieee80211_sta_process_chanswitch(sdata,
3138 rx_status->mactime,
3139 &elems, false);
3140 }
3141 break;
3142 }
3143 sdata_unlock(sdata);
3144 }
3145
3146 static void ieee80211_sta_timer(unsigned long data)
3147 {
3148 struct ieee80211_sub_if_data *sdata =
3149 (struct ieee80211_sub_if_data *) data;
3150
3151 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
3152 }
3153
3154 static void ieee80211_sta_connection_lost(struct ieee80211_sub_if_data *sdata,
3155 u8 *bssid, u8 reason, bool tx)
3156 {
3157 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
3158
3159 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH, reason,
3160 tx, frame_buf);
3161
3162 cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
3163 IEEE80211_DEAUTH_FRAME_LEN);
3164 }
3165
3166 static int ieee80211_probe_auth(struct ieee80211_sub_if_data *sdata)
3167 {
3168 struct ieee80211_local *local = sdata->local;
3169 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3170 struct ieee80211_mgd_auth_data *auth_data = ifmgd->auth_data;
3171 u32 tx_flags = 0;
3172
3173 sdata_assert_lock(sdata);
3174
3175 if (WARN_ON_ONCE(!auth_data))
3176 return -EINVAL;
3177
3178 auth_data->tries++;
3179
3180 if (auth_data->tries > IEEE80211_AUTH_MAX_TRIES) {
3181 sdata_info(sdata, "authentication with %pM timed out\n",
3182 auth_data->bss->bssid);
3183
3184 /*
3185 * Most likely AP is not in the range so remove the
3186 * bss struct for that AP.
3187 */
3188 cfg80211_unlink_bss(local->hw.wiphy, auth_data->bss);
3189
3190 return -ETIMEDOUT;
3191 }
3192
3193 drv_mgd_prepare_tx(local, sdata);
3194
3195 if (auth_data->bss->proberesp_ies) {
3196 u16 trans = 1;
3197 u16 status = 0;
3198
3199 sdata_info(sdata, "send auth to %pM (try %d/%d)\n",
3200 auth_data->bss->bssid, auth_data->tries,
3201 IEEE80211_AUTH_MAX_TRIES);
3202
3203 auth_data->expected_transaction = 2;
3204
3205 if (auth_data->algorithm == WLAN_AUTH_SAE) {
3206 trans = auth_data->sae_trans;
3207 status = auth_data->sae_status;
3208 auth_data->expected_transaction = trans;
3209 }
3210
3211 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
3212 tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
3213 IEEE80211_TX_INTFL_MLME_CONN_TX;
3214
3215 ieee80211_send_auth(sdata, trans, auth_data->algorithm, status,
3216 auth_data->data, auth_data->data_len,
3217 auth_data->bss->bssid,
3218 auth_data->bss->bssid, NULL, 0, 0,
3219 tx_flags);
3220 } else {
3221 const u8 *ssidie;
3222
3223 sdata_info(sdata, "direct probe to %pM (try %d/%i)\n",
3224 auth_data->bss->bssid, auth_data->tries,
3225 IEEE80211_AUTH_MAX_TRIES);
3226
3227 rcu_read_lock();
3228 ssidie = ieee80211_bss_get_ie(auth_data->bss, WLAN_EID_SSID);
3229 if (!ssidie) {
3230 rcu_read_unlock();
3231 return -EINVAL;
3232 }
3233 /*
3234 * Direct probe is sent to broadcast address as some APs
3235 * will not answer to direct packet in unassociated state.
3236 */
3237 ieee80211_send_probe_req(sdata, NULL, ssidie + 2, ssidie[1],
3238 NULL, 0, (u32) -1, true, 0,
3239 auth_data->bss->channel, false);
3240 rcu_read_unlock();
3241 }
3242
3243 if (tx_flags == 0) {
3244 auth_data->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
3245 auth_data->timeout_started = true;
3246 run_again(sdata, auth_data->timeout);
3247 } else {
3248 auth_data->timeout =
3249 round_jiffies_up(jiffies + IEEE80211_AUTH_TIMEOUT_LONG);
3250 auth_data->timeout_started = true;
3251 run_again(sdata, auth_data->timeout);
3252 }
3253
3254 return 0;
3255 }
3256
3257 static int ieee80211_do_assoc(struct ieee80211_sub_if_data *sdata)
3258 {
3259 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
3260 struct ieee80211_local *local = sdata->local;
3261
3262 sdata_assert_lock(sdata);
3263
3264 assoc_data->tries++;
3265 if (assoc_data->tries > IEEE80211_ASSOC_MAX_TRIES) {
3266 sdata_info(sdata, "association with %pM timed out\n",
3267 assoc_data->bss->bssid);
3268
3269 /*
3270 * Most likely AP is not in the range so remove the
3271 * bss struct for that AP.
3272 */
3273 cfg80211_unlink_bss(local->hw.wiphy, assoc_data->bss);
3274
3275 return -ETIMEDOUT;
3276 }
3277
3278 sdata_info(sdata, "associate with %pM (try %d/%d)\n",
3279 assoc_data->bss->bssid, assoc_data->tries,
3280 IEEE80211_ASSOC_MAX_TRIES);
3281 ieee80211_send_assoc(sdata);
3282
3283 if (!(local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)) {
3284 assoc_data->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
3285 assoc_data->timeout_started = true;
3286 run_again(sdata, assoc_data->timeout);
3287 } else {
3288 assoc_data->timeout =
3289 round_jiffies_up(jiffies +
3290 IEEE80211_ASSOC_TIMEOUT_LONG);
3291 assoc_data->timeout_started = true;
3292 run_again(sdata, assoc_data->timeout);
3293 }
3294
3295 return 0;
3296 }
3297
3298 void ieee80211_mgd_conn_tx_status(struct ieee80211_sub_if_data *sdata,
3299 __le16 fc, bool acked)
3300 {
3301 struct ieee80211_local *local = sdata->local;
3302
3303 sdata->u.mgd.status_fc = fc;
3304 sdata->u.mgd.status_acked = acked;
3305 sdata->u.mgd.status_received = true;
3306
3307 ieee80211_queue_work(&local->hw, &sdata->work);
3308 }
3309
3310 void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
3311 {
3312 struct ieee80211_local *local = sdata->local;
3313 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3314
3315 sdata_lock(sdata);
3316
3317 if (ifmgd->status_received) {
3318 __le16 fc = ifmgd->status_fc;
3319 bool status_acked = ifmgd->status_acked;
3320
3321 ifmgd->status_received = false;
3322 if (ifmgd->auth_data &&
3323 (ieee80211_is_probe_req(fc) || ieee80211_is_auth(fc))) {
3324 if (status_acked) {
3325 ifmgd->auth_data->timeout =
3326 jiffies + IEEE80211_AUTH_TIMEOUT_SHORT;
3327 run_again(sdata, ifmgd->auth_data->timeout);
3328 } else {
3329 ifmgd->auth_data->timeout = jiffies - 1;
3330 }
3331 ifmgd->auth_data->timeout_started = true;
3332 } else if (ifmgd->assoc_data &&
3333 (ieee80211_is_assoc_req(fc) ||
3334 ieee80211_is_reassoc_req(fc))) {
3335 if (status_acked) {
3336 ifmgd->assoc_data->timeout =
3337 jiffies + IEEE80211_ASSOC_TIMEOUT_SHORT;
3338 run_again(sdata, ifmgd->assoc_data->timeout);
3339 } else {
3340 ifmgd->assoc_data->timeout = jiffies - 1;
3341 }
3342 ifmgd->assoc_data->timeout_started = true;
3343 }
3344 }
3345
3346 if (ifmgd->auth_data && ifmgd->auth_data->timeout_started &&
3347 time_after(jiffies, ifmgd->auth_data->timeout)) {
3348 if (ifmgd->auth_data->done) {
3349 /*
3350 * ok ... we waited for assoc but userspace didn't,
3351 * so let's just kill the auth data
3352 */
3353 ieee80211_destroy_auth_data(sdata, false);
3354 } else if (ieee80211_probe_auth(sdata)) {
3355 u8 bssid[ETH_ALEN];
3356
3357 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
3358
3359 ieee80211_destroy_auth_data(sdata, false);
3360
3361 cfg80211_auth_timeout(sdata->dev, bssid);
3362 }
3363 } else if (ifmgd->auth_data && ifmgd->auth_data->timeout_started)
3364 run_again(sdata, ifmgd->auth_data->timeout);
3365
3366 if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started &&
3367 time_after(jiffies, ifmgd->assoc_data->timeout)) {
3368 if ((ifmgd->assoc_data->need_beacon && !ifmgd->have_beacon) ||
3369 ieee80211_do_assoc(sdata)) {
3370 struct cfg80211_bss *bss = ifmgd->assoc_data->bss;
3371
3372 ieee80211_destroy_assoc_data(sdata, false);
3373 cfg80211_assoc_timeout(sdata->dev, bss);
3374 }
3375 } else if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started)
3376 run_again(sdata, ifmgd->assoc_data->timeout);
3377
3378 if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL &&
3379 ifmgd->associated) {
3380 u8 bssid[ETH_ALEN];
3381 int max_tries;
3382
3383 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
3384
3385 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
3386 max_tries = max_nullfunc_tries;
3387 else
3388 max_tries = max_probe_tries;
3389
3390 /* ACK received for nullfunc probing frame */
3391 if (!ifmgd->probe_send_count)
3392 ieee80211_reset_ap_probe(sdata);
3393 else if (ifmgd->nullfunc_failed) {
3394 if (ifmgd->probe_send_count < max_tries) {
3395 mlme_dbg(sdata,
3396 "No ack for nullfunc frame to AP %pM, try %d/%i\n",
3397 bssid, ifmgd->probe_send_count,
3398 max_tries);
3399 ieee80211_mgd_probe_ap_send(sdata);
3400 } else {
3401 mlme_dbg(sdata,
3402 "No ack for nullfunc frame to AP %pM, disconnecting.\n",
3403 bssid);
3404 ieee80211_sta_connection_lost(sdata, bssid,
3405 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
3406 false);
3407 }
3408 } else if (time_is_after_jiffies(ifmgd->probe_timeout))
3409 run_again(sdata, ifmgd->probe_timeout);
3410 else if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
3411 mlme_dbg(sdata,
3412 "Failed to send nullfunc to AP %pM after %dms, disconnecting\n",
3413 bssid, probe_wait_ms);
3414 ieee80211_sta_connection_lost(sdata, bssid,
3415 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
3416 } else if (ifmgd->probe_send_count < max_tries) {
3417 mlme_dbg(sdata,
3418 "No probe response from AP %pM after %dms, try %d/%i\n",
3419 bssid, probe_wait_ms,
3420 ifmgd->probe_send_count, max_tries);
3421 ieee80211_mgd_probe_ap_send(sdata);
3422 } else {
3423 /*
3424 * We actually lost the connection ... or did we?
3425 * Let's make sure!
3426 */
3427 wiphy_debug(local->hw.wiphy,
3428 "%s: No probe response from AP %pM"
3429 " after %dms, disconnecting.\n",
3430 sdata->name,
3431 bssid, probe_wait_ms);
3432
3433 ieee80211_sta_connection_lost(sdata, bssid,
3434 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
3435 }
3436 }
3437
3438 sdata_unlock(sdata);
3439 }
3440
3441 static void ieee80211_sta_bcn_mon_timer(unsigned long data)
3442 {
3443 struct ieee80211_sub_if_data *sdata =
3444 (struct ieee80211_sub_if_data *) data;
3445 struct ieee80211_local *local = sdata->local;
3446
3447 if (local->quiescing)
3448 return;
3449
3450 sdata->u.mgd.connection_loss = false;
3451 ieee80211_queue_work(&sdata->local->hw,
3452 &sdata->u.mgd.beacon_connection_loss_work);
3453 }
3454
3455 static void ieee80211_sta_conn_mon_timer(unsigned long data)
3456 {
3457 struct ieee80211_sub_if_data *sdata =
3458 (struct ieee80211_sub_if_data *) data;
3459 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3460 struct ieee80211_local *local = sdata->local;
3461
3462 if (local->quiescing)
3463 return;
3464
3465 ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
3466 }
3467
3468 static void ieee80211_sta_monitor_work(struct work_struct *work)
3469 {
3470 struct ieee80211_sub_if_data *sdata =
3471 container_of(work, struct ieee80211_sub_if_data,
3472 u.mgd.monitor_work);
3473
3474 ieee80211_mgd_probe_ap(sdata, false);
3475 }
3476
3477 static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
3478 {
3479 u32 flags;
3480
3481 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
3482 __ieee80211_stop_poll(sdata);
3483
3484 /* let's probe the connection once */
3485 flags = sdata->local->hw.flags;
3486 if (!(flags & IEEE80211_HW_CONNECTION_MONITOR))
3487 ieee80211_queue_work(&sdata->local->hw,
3488 &sdata->u.mgd.monitor_work);
3489 /* and do all the other regular work too */
3490 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
3491 }
3492 }
3493
3494 #ifdef CONFIG_PM
3495 void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
3496 {
3497 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3498
3499 sdata_lock(sdata);
3500 if (!ifmgd->associated) {
3501 sdata_unlock(sdata);
3502 return;
3503 }
3504
3505 if (sdata->flags & IEEE80211_SDATA_DISCONNECT_RESUME) {
3506 sdata->flags &= ~IEEE80211_SDATA_DISCONNECT_RESUME;
3507 mlme_dbg(sdata, "driver requested disconnect after resume\n");
3508 ieee80211_sta_connection_lost(sdata,
3509 ifmgd->associated->bssid,
3510 WLAN_REASON_UNSPECIFIED,
3511 true);
3512 sdata_unlock(sdata);
3513 return;
3514 }
3515 sdata_unlock(sdata);
3516 }
3517 #endif
3518
3519 /* interface setup */
3520 void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
3521 {
3522 struct ieee80211_if_managed *ifmgd;
3523
3524 ifmgd = &sdata->u.mgd;
3525 INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
3526 INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
3527 INIT_WORK(&ifmgd->beacon_connection_loss_work,
3528 ieee80211_beacon_connection_loss_work);
3529 INIT_WORK(&ifmgd->csa_connection_drop_work,
3530 ieee80211_csa_connection_drop_work);
3531 INIT_WORK(&ifmgd->request_smps_work, ieee80211_request_smps_mgd_work);
3532 setup_timer(&ifmgd->timer, ieee80211_sta_timer,
3533 (unsigned long) sdata);
3534 setup_timer(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer,
3535 (unsigned long) sdata);
3536 setup_timer(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer,
3537 (unsigned long) sdata);
3538 setup_timer(&ifmgd->chswitch_timer, ieee80211_chswitch_timer,
3539 (unsigned long) sdata);
3540
3541 ifmgd->flags = 0;
3542 ifmgd->powersave = sdata->wdev.ps;
3543 ifmgd->uapsd_queues = sdata->local->hw.uapsd_queues;
3544 ifmgd->uapsd_max_sp_len = sdata->local->hw.uapsd_max_sp_len;
3545 ifmgd->p2p_noa_index = -1;
3546
3547 if (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS)
3548 ifmgd->req_smps = IEEE80211_SMPS_AUTOMATIC;
3549 else
3550 ifmgd->req_smps = IEEE80211_SMPS_OFF;
3551 }
3552
3553 /* scan finished notification */
3554 void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
3555 {
3556 struct ieee80211_sub_if_data *sdata;
3557
3558 /* Restart STA timers */
3559 rcu_read_lock();
3560 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
3561 if (ieee80211_sdata_running(sdata))
3562 ieee80211_restart_sta_timer(sdata);
3563 }
3564 rcu_read_unlock();
3565 }
3566
3567 int ieee80211_max_network_latency(struct notifier_block *nb,
3568 unsigned long data, void *dummy)
3569 {
3570 s32 latency_usec = (s32) data;
3571 struct ieee80211_local *local =
3572 container_of(nb, struct ieee80211_local,
3573 network_latency_notifier);
3574
3575 mutex_lock(&local->iflist_mtx);
3576 ieee80211_recalc_ps(local, latency_usec);
3577 mutex_unlock(&local->iflist_mtx);
3578
3579 return 0;
3580 }
3581
3582 static u8 ieee80211_ht_vht_rx_chains(struct ieee80211_sub_if_data *sdata,
3583 struct cfg80211_bss *cbss)
3584 {
3585 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3586 const u8 *ht_cap_ie, *vht_cap_ie;
3587 const struct ieee80211_ht_cap *ht_cap;
3588 const struct ieee80211_vht_cap *vht_cap;
3589 u8 chains = 1;
3590
3591 if (ifmgd->flags & IEEE80211_STA_DISABLE_HT)
3592 return chains;
3593
3594 ht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
3595 if (ht_cap_ie && ht_cap_ie[1] >= sizeof(*ht_cap)) {
3596 ht_cap = (void *)(ht_cap_ie + 2);
3597 chains = ieee80211_mcs_to_chains(&ht_cap->mcs);
3598 /*
3599 * TODO: use "Tx Maximum Number Spatial Streams Supported" and
3600 * "Tx Unequal Modulation Supported" fields.
3601 */
3602 }
3603
3604 if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
3605 return chains;
3606
3607 vht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
3608 if (vht_cap_ie && vht_cap_ie[1] >= sizeof(*vht_cap)) {
3609 u8 nss;
3610 u16 tx_mcs_map;
3611
3612 vht_cap = (void *)(vht_cap_ie + 2);
3613 tx_mcs_map = le16_to_cpu(vht_cap->supp_mcs.tx_mcs_map);
3614 for (nss = 8; nss > 0; nss--) {
3615 if (((tx_mcs_map >> (2 * (nss - 1))) & 3) !=
3616 IEEE80211_VHT_MCS_NOT_SUPPORTED)
3617 break;
3618 }
3619 /* TODO: use "Tx Highest Supported Long GI Data Rate" field? */
3620 chains = max(chains, nss);
3621 }
3622
3623 return chains;
3624 }
3625
3626 static int ieee80211_prep_channel(struct ieee80211_sub_if_data *sdata,
3627 struct cfg80211_bss *cbss)
3628 {
3629 struct ieee80211_local *local = sdata->local;
3630 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3631 const struct ieee80211_ht_operation *ht_oper = NULL;
3632 const struct ieee80211_vht_operation *vht_oper = NULL;
3633 struct ieee80211_supported_band *sband;
3634 struct cfg80211_chan_def chandef;
3635 int ret;
3636
3637 sband = local->hw.wiphy->bands[cbss->channel->band];
3638
3639 ifmgd->flags &= ~(IEEE80211_STA_DISABLE_40MHZ |
3640 IEEE80211_STA_DISABLE_80P80MHZ |
3641 IEEE80211_STA_DISABLE_160MHZ);
3642
3643 rcu_read_lock();
3644
3645 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
3646 sband->ht_cap.ht_supported) {
3647 const u8 *ht_oper_ie, *ht_cap;
3648
3649 ht_oper_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_OPERATION);
3650 if (ht_oper_ie && ht_oper_ie[1] >= sizeof(*ht_oper))
3651 ht_oper = (void *)(ht_oper_ie + 2);
3652
3653 ht_cap = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
3654 if (!ht_cap || ht_cap[1] < sizeof(struct ieee80211_ht_cap)) {
3655 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
3656 ht_oper = NULL;
3657 }
3658 }
3659
3660 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
3661 sband->vht_cap.vht_supported) {
3662 const u8 *vht_oper_ie, *vht_cap;
3663
3664 vht_oper_ie = ieee80211_bss_get_ie(cbss,
3665 WLAN_EID_VHT_OPERATION);
3666 if (vht_oper_ie && vht_oper_ie[1] >= sizeof(*vht_oper))
3667 vht_oper = (void *)(vht_oper_ie + 2);
3668 if (vht_oper && !ht_oper) {
3669 vht_oper = NULL;
3670 sdata_info(sdata,
3671 "AP advertised VHT without HT, disabling both\n");
3672 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
3673 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
3674 }
3675
3676 vht_cap = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
3677 if (!vht_cap || vht_cap[1] < sizeof(struct ieee80211_vht_cap)) {
3678 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
3679 vht_oper = NULL;
3680 }
3681 }
3682
3683 ifmgd->flags |= ieee80211_determine_chantype(sdata, sband,
3684 cbss->channel,
3685 ht_oper, vht_oper,
3686 &chandef, false);
3687
3688 sdata->needed_rx_chains = min(ieee80211_ht_vht_rx_chains(sdata, cbss),
3689 local->rx_chains);
3690
3691 rcu_read_unlock();
3692
3693 /* will change later if needed */
3694 sdata->smps_mode = IEEE80211_SMPS_OFF;
3695
3696 mutex_lock(&local->mtx);
3697 /*
3698 * If this fails (possibly due to channel context sharing
3699 * on incompatible channels, e.g. 80+80 and 160 sharing the
3700 * same control channel) try to use a smaller bandwidth.
3701 */
3702 ret = ieee80211_vif_use_channel(sdata, &chandef,
3703 IEEE80211_CHANCTX_SHARED);
3704
3705 /* don't downgrade for 5 and 10 MHz channels, though. */
3706 if (chandef.width == NL80211_CHAN_WIDTH_5 ||
3707 chandef.width == NL80211_CHAN_WIDTH_10)
3708 goto out;
3709
3710 while (ret && chandef.width != NL80211_CHAN_WIDTH_20_NOHT) {
3711 ifmgd->flags |= ieee80211_chandef_downgrade(&chandef);
3712 ret = ieee80211_vif_use_channel(sdata, &chandef,
3713 IEEE80211_CHANCTX_SHARED);
3714 }
3715 out:
3716 mutex_unlock(&local->mtx);
3717 return ret;
3718 }
3719
3720 static int ieee80211_prep_connection(struct ieee80211_sub_if_data *sdata,
3721 struct cfg80211_bss *cbss, bool assoc)
3722 {
3723 struct ieee80211_local *local = sdata->local;
3724 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3725 struct ieee80211_bss *bss = (void *)cbss->priv;
3726 struct sta_info *new_sta = NULL;
3727 bool have_sta = false;
3728 int err;
3729
3730 if (WARN_ON(!ifmgd->auth_data && !ifmgd->assoc_data))
3731 return -EINVAL;
3732
3733 if (assoc) {
3734 rcu_read_lock();
3735 have_sta = sta_info_get(sdata, cbss->bssid);
3736 rcu_read_unlock();
3737 }
3738
3739 if (!have_sta) {
3740 new_sta = sta_info_alloc(sdata, cbss->bssid, GFP_KERNEL);
3741 if (!new_sta)
3742 return -ENOMEM;
3743 }
3744 if (new_sta) {
3745 u32 rates = 0, basic_rates = 0;
3746 bool have_higher_than_11mbit;
3747 int min_rate = INT_MAX, min_rate_index = -1;
3748 struct ieee80211_chanctx_conf *chanctx_conf;
3749 struct ieee80211_supported_band *sband;
3750 const struct cfg80211_bss_ies *ies;
3751 int shift;
3752 u32 rate_flags;
3753
3754 sband = local->hw.wiphy->bands[cbss->channel->band];
3755
3756 err = ieee80211_prep_channel(sdata, cbss);
3757 if (err) {
3758 sta_info_free(local, new_sta);
3759 return -EINVAL;
3760 }
3761 shift = ieee80211_vif_get_shift(&sdata->vif);
3762
3763 rcu_read_lock();
3764 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
3765 if (WARN_ON(!chanctx_conf)) {
3766 rcu_read_unlock();
3767 return -EINVAL;
3768 }
3769 rate_flags = ieee80211_chandef_rate_flags(&chanctx_conf->def);
3770 rcu_read_unlock();
3771
3772 ieee80211_get_rates(sband, bss->supp_rates,
3773 bss->supp_rates_len,
3774 &rates, &basic_rates,
3775 &have_higher_than_11mbit,
3776 &min_rate, &min_rate_index,
3777 shift, rate_flags);
3778
3779 /*
3780 * This used to be a workaround for basic rates missing
3781 * in the association response frame. Now that we no
3782 * longer use the basic rates from there, it probably
3783 * doesn't happen any more, but keep the workaround so
3784 * in case some *other* APs are buggy in different ways
3785 * we can connect -- with a warning.
3786 */
3787 if (!basic_rates && min_rate_index >= 0) {
3788 sdata_info(sdata,
3789 "No basic rates, using min rate instead\n");
3790 basic_rates = BIT(min_rate_index);
3791 }
3792
3793 new_sta->sta.supp_rates[cbss->channel->band] = rates;
3794 sdata->vif.bss_conf.basic_rates = basic_rates;
3795
3796 /* cf. IEEE 802.11 9.2.12 */
3797 if (cbss->channel->band == IEEE80211_BAND_2GHZ &&
3798 have_higher_than_11mbit)
3799 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
3800 else
3801 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
3802
3803 memcpy(ifmgd->bssid, cbss->bssid, ETH_ALEN);
3804
3805 /* set timing information */
3806 sdata->vif.bss_conf.beacon_int = cbss->beacon_interval;
3807 rcu_read_lock();
3808 ies = rcu_dereference(cbss->beacon_ies);
3809 if (ies) {
3810 const u8 *tim_ie;
3811
3812 sdata->vif.bss_conf.sync_tsf = ies->tsf;
3813 sdata->vif.bss_conf.sync_device_ts =
3814 bss->device_ts_beacon;
3815 tim_ie = cfg80211_find_ie(WLAN_EID_TIM,
3816 ies->data, ies->len);
3817 if (tim_ie && tim_ie[1] >= 2)
3818 sdata->vif.bss_conf.sync_dtim_count = tim_ie[2];
3819 else
3820 sdata->vif.bss_conf.sync_dtim_count = 0;
3821 } else if (!(local->hw.flags &
3822 IEEE80211_HW_TIMING_BEACON_ONLY)) {
3823 ies = rcu_dereference(cbss->proberesp_ies);
3824 /* must be non-NULL since beacon IEs were NULL */
3825 sdata->vif.bss_conf.sync_tsf = ies->tsf;
3826 sdata->vif.bss_conf.sync_device_ts =
3827 bss->device_ts_presp;
3828 sdata->vif.bss_conf.sync_dtim_count = 0;
3829 } else {
3830 sdata->vif.bss_conf.sync_tsf = 0;
3831 sdata->vif.bss_conf.sync_device_ts = 0;
3832 sdata->vif.bss_conf.sync_dtim_count = 0;
3833 }
3834 rcu_read_unlock();
3835
3836 /* tell driver about BSSID, basic rates and timing */
3837 ieee80211_bss_info_change_notify(sdata,
3838 BSS_CHANGED_BSSID | BSS_CHANGED_BASIC_RATES |
3839 BSS_CHANGED_BEACON_INT);
3840
3841 if (assoc)
3842 sta_info_pre_move_state(new_sta, IEEE80211_STA_AUTH);
3843
3844 err = sta_info_insert(new_sta);
3845 new_sta = NULL;
3846 if (err) {
3847 sdata_info(sdata,
3848 "failed to insert STA entry for the AP (error %d)\n",
3849 err);
3850 return err;
3851 }
3852 } else
3853 WARN_ON_ONCE(!ether_addr_equal(ifmgd->bssid, cbss->bssid));
3854
3855 return 0;
3856 }
3857
3858 /* config hooks */
3859 int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
3860 struct cfg80211_auth_request *req)
3861 {
3862 struct ieee80211_local *local = sdata->local;
3863 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3864 struct ieee80211_mgd_auth_data *auth_data;
3865 u16 auth_alg;
3866 int err;
3867
3868 /* prepare auth data structure */
3869
3870 switch (req->auth_type) {
3871 case NL80211_AUTHTYPE_OPEN_SYSTEM:
3872 auth_alg = WLAN_AUTH_OPEN;
3873 break;
3874 case NL80211_AUTHTYPE_SHARED_KEY:
3875 if (IS_ERR(local->wep_tx_tfm))
3876 return -EOPNOTSUPP;
3877 auth_alg = WLAN_AUTH_SHARED_KEY;
3878 break;
3879 case NL80211_AUTHTYPE_FT:
3880 auth_alg = WLAN_AUTH_FT;
3881 break;
3882 case NL80211_AUTHTYPE_NETWORK_EAP:
3883 auth_alg = WLAN_AUTH_LEAP;
3884 break;
3885 case NL80211_AUTHTYPE_SAE:
3886 auth_alg = WLAN_AUTH_SAE;
3887 break;
3888 default:
3889 return -EOPNOTSUPP;
3890 }
3891
3892 auth_data = kzalloc(sizeof(*auth_data) + req->sae_data_len +
3893 req->ie_len, GFP_KERNEL);
3894 if (!auth_data)
3895 return -ENOMEM;
3896
3897 auth_data->bss = req->bss;
3898
3899 if (req->sae_data_len >= 4) {
3900 __le16 *pos = (__le16 *) req->sae_data;
3901 auth_data->sae_trans = le16_to_cpu(pos[0]);
3902 auth_data->sae_status = le16_to_cpu(pos[1]);
3903 memcpy(auth_data->data, req->sae_data + 4,
3904 req->sae_data_len - 4);
3905 auth_data->data_len += req->sae_data_len - 4;
3906 }
3907
3908 if (req->ie && req->ie_len) {
3909 memcpy(&auth_data->data[auth_data->data_len],
3910 req->ie, req->ie_len);
3911 auth_data->data_len += req->ie_len;
3912 }
3913
3914 if (req->key && req->key_len) {
3915 auth_data->key_len = req->key_len;
3916 auth_data->key_idx = req->key_idx;
3917 memcpy(auth_data->key, req->key, req->key_len);
3918 }
3919
3920 auth_data->algorithm = auth_alg;
3921
3922 /* try to authenticate/probe */
3923
3924 if ((ifmgd->auth_data && !ifmgd->auth_data->done) ||
3925 ifmgd->assoc_data) {
3926 err = -EBUSY;
3927 goto err_free;
3928 }
3929
3930 if (ifmgd->auth_data)
3931 ieee80211_destroy_auth_data(sdata, false);
3932
3933 /* prep auth_data so we don't go into idle on disassoc */
3934 ifmgd->auth_data = auth_data;
3935
3936 if (ifmgd->associated) {
3937 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
3938
3939 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
3940 WLAN_REASON_UNSPECIFIED,
3941 false, frame_buf);
3942
3943 cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
3944 sizeof(frame_buf));
3945 }
3946
3947 sdata_info(sdata, "authenticate with %pM\n", req->bss->bssid);
3948
3949 err = ieee80211_prep_connection(sdata, req->bss, false);
3950 if (err)
3951 goto err_clear;
3952
3953 err = ieee80211_probe_auth(sdata);
3954 if (err) {
3955 sta_info_destroy_addr(sdata, req->bss->bssid);
3956 goto err_clear;
3957 }
3958
3959 /* hold our own reference */
3960 cfg80211_ref_bss(local->hw.wiphy, auth_data->bss);
3961 return 0;
3962
3963 err_clear:
3964 memset(ifmgd->bssid, 0, ETH_ALEN);
3965 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
3966 ifmgd->auth_data = NULL;
3967 err_free:
3968 kfree(auth_data);
3969 return err;
3970 }
3971
3972 static bool ieee80211_usable_wmm_params(struct ieee80211_sub_if_data *sdata,
3973 const u8 *wmm_param, int len)
3974 {
3975 const u8 *pos;
3976 size_t left;
3977
3978 if (len < 8)
3979 return false;
3980
3981 if (wmm_param[5] != 1 /* version */)
3982 return false;
3983
3984 pos = wmm_param + 8;
3985 left = len - 8;
3986
3987 for (; left >= 4; left -= 4, pos += 4) {
3988 u8 aifsn = pos[0] & 0x0f;
3989 u8 ecwmin = pos[1] & 0x0f;
3990 u8 ecwmax = (pos[1] & 0xf0) >> 4;
3991 int aci = (pos[0] >> 5) & 0x03;
3992
3993 if (aifsn < 2) {
3994 sdata_info(sdata,
3995 "AP has invalid WMM params (AIFSN=%d for ACI %d), disabling WMM\n",
3996 aifsn, aci);
3997 return false;
3998 }
3999 if (ecwmin > ecwmax) {
4000 sdata_info(sdata,
4001 "AP has invalid WMM params (ECWmin/max=%d/%d for ACI %d), disabling WMM\n",
4002 ecwmin, ecwmax, aci);
4003 return false;
4004 }
4005 }
4006
4007 return true;
4008 }
4009
4010 int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
4011 struct cfg80211_assoc_request *req)
4012 {
4013 struct ieee80211_local *local = sdata->local;
4014 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4015 struct ieee80211_bss *bss = (void *)req->bss->priv;
4016 struct ieee80211_mgd_assoc_data *assoc_data;
4017 const struct cfg80211_bss_ies *beacon_ies;
4018 struct ieee80211_supported_band *sband;
4019 const u8 *ssidie, *ht_ie, *vht_ie;
4020 int i, err;
4021
4022 assoc_data = kzalloc(sizeof(*assoc_data) + req->ie_len, GFP_KERNEL);
4023 if (!assoc_data)
4024 return -ENOMEM;
4025
4026 rcu_read_lock();
4027 ssidie = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
4028 if (!ssidie) {
4029 rcu_read_unlock();
4030 kfree(assoc_data);
4031 return -EINVAL;
4032 }
4033 memcpy(assoc_data->ssid, ssidie + 2, ssidie[1]);
4034 assoc_data->ssid_len = ssidie[1];
4035 rcu_read_unlock();
4036
4037 if (ifmgd->associated) {
4038 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
4039
4040 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
4041 WLAN_REASON_UNSPECIFIED,
4042 false, frame_buf);
4043
4044 cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
4045 sizeof(frame_buf));
4046 }
4047
4048 if (ifmgd->auth_data && !ifmgd->auth_data->done) {
4049 err = -EBUSY;
4050 goto err_free;
4051 }
4052
4053 if (ifmgd->assoc_data) {
4054 err = -EBUSY;
4055 goto err_free;
4056 }
4057
4058 if (ifmgd->auth_data) {
4059 bool match;
4060
4061 /* keep sta info, bssid if matching */
4062 match = ether_addr_equal(ifmgd->bssid, req->bss->bssid);
4063 ieee80211_destroy_auth_data(sdata, match);
4064 }
4065
4066 /* prepare assoc data */
4067
4068 ifmgd->beacon_crc_valid = false;
4069
4070 assoc_data->wmm = bss->wmm_used &&
4071 (local->hw.queues >= IEEE80211_NUM_ACS);
4072 if (assoc_data->wmm) {
4073 /* try to check validity of WMM params IE */
4074 const struct cfg80211_bss_ies *ies;
4075 const u8 *wp, *start, *end;
4076
4077 rcu_read_lock();
4078 ies = rcu_dereference(req->bss->ies);
4079 start = ies->data;
4080 end = start + ies->len;
4081
4082 while (true) {
4083 wp = cfg80211_find_vendor_ie(
4084 WLAN_OUI_MICROSOFT,
4085 WLAN_OUI_TYPE_MICROSOFT_WMM,
4086 start, end - start);
4087 if (!wp)
4088 break;
4089 start = wp + wp[1] + 2;
4090 /* if this IE is too short, try the next */
4091 if (wp[1] <= 4)
4092 continue;
4093 /* if this IE is WMM params, we found what we wanted */
4094 if (wp[6] == 1)
4095 break;
4096 }
4097
4098 if (!wp || !ieee80211_usable_wmm_params(sdata, wp + 2,
4099 wp[1] - 2)) {
4100 assoc_data->wmm = false;
4101 ifmgd->flags |= IEEE80211_STA_DISABLE_WMM;
4102 }
4103 rcu_read_unlock();
4104 }
4105
4106 /*
4107 * IEEE802.11n does not allow TKIP/WEP as pairwise ciphers in HT mode.
4108 * We still associate in non-HT mode (11a/b/g) if any one of these
4109 * ciphers is configured as pairwise.
4110 * We can set this to true for non-11n hardware, that'll be checked
4111 * separately along with the peer capabilities.
4112 */
4113 for (i = 0; i < req->crypto.n_ciphers_pairwise; i++) {
4114 if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
4115 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
4116 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104) {
4117 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
4118 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
4119 netdev_info(sdata->dev,
4120 "disabling HT/VHT due to WEP/TKIP use\n");
4121 }
4122 }
4123
4124 if (req->flags & ASSOC_REQ_DISABLE_HT) {
4125 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
4126 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
4127 }
4128
4129 if (req->flags & ASSOC_REQ_DISABLE_VHT)
4130 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
4131
4132 /* Also disable HT if we don't support it or the AP doesn't use WMM */
4133 sband = local->hw.wiphy->bands[req->bss->channel->band];
4134 if (!sband->ht_cap.ht_supported ||
4135 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used ||
4136 ifmgd->flags & IEEE80211_STA_DISABLE_WMM) {
4137 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
4138 if (!bss->wmm_used &&
4139 !(ifmgd->flags & IEEE80211_STA_DISABLE_WMM))
4140 netdev_info(sdata->dev,
4141 "disabling HT as WMM/QoS is not supported by the AP\n");
4142 }
4143
4144 /* disable VHT if we don't support it or the AP doesn't use WMM */
4145 if (!sband->vht_cap.vht_supported ||
4146 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used ||
4147 ifmgd->flags & IEEE80211_STA_DISABLE_WMM) {
4148 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
4149 if (!bss->wmm_used &&
4150 !(ifmgd->flags & IEEE80211_STA_DISABLE_WMM))
4151 netdev_info(sdata->dev,
4152 "disabling VHT as WMM/QoS is not supported by the AP\n");
4153 }
4154
4155 memcpy(&ifmgd->ht_capa, &req->ht_capa, sizeof(ifmgd->ht_capa));
4156 memcpy(&ifmgd->ht_capa_mask, &req->ht_capa_mask,
4157 sizeof(ifmgd->ht_capa_mask));
4158
4159 memcpy(&ifmgd->vht_capa, &req->vht_capa, sizeof(ifmgd->vht_capa));
4160 memcpy(&ifmgd->vht_capa_mask, &req->vht_capa_mask,
4161 sizeof(ifmgd->vht_capa_mask));
4162
4163 if (req->ie && req->ie_len) {
4164 memcpy(assoc_data->ie, req->ie, req->ie_len);
4165 assoc_data->ie_len = req->ie_len;
4166 }
4167
4168 assoc_data->bss = req->bss;
4169
4170 if (ifmgd->req_smps == IEEE80211_SMPS_AUTOMATIC) {
4171 if (ifmgd->powersave)
4172 sdata->smps_mode = IEEE80211_SMPS_DYNAMIC;
4173 else
4174 sdata->smps_mode = IEEE80211_SMPS_OFF;
4175 } else
4176 sdata->smps_mode = ifmgd->req_smps;
4177
4178 assoc_data->capability = req->bss->capability;
4179 assoc_data->supp_rates = bss->supp_rates;
4180 assoc_data->supp_rates_len = bss->supp_rates_len;
4181
4182 rcu_read_lock();
4183 ht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_HT_OPERATION);
4184 if (ht_ie && ht_ie[1] >= sizeof(struct ieee80211_ht_operation))
4185 assoc_data->ap_ht_param =
4186 ((struct ieee80211_ht_operation *)(ht_ie + 2))->ht_param;
4187 else
4188 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
4189 vht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_VHT_CAPABILITY);
4190 if (vht_ie && vht_ie[1] >= sizeof(struct ieee80211_vht_cap))
4191 memcpy(&assoc_data->ap_vht_cap, vht_ie + 2,
4192 sizeof(struct ieee80211_vht_cap));
4193 else
4194 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
4195 rcu_read_unlock();
4196
4197 if (bss->wmm_used && bss->uapsd_supported &&
4198 (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_UAPSD) &&
4199 sdata->wmm_acm != 0xff) {
4200 assoc_data->uapsd = true;
4201 ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED;
4202 } else {
4203 assoc_data->uapsd = false;
4204 ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED;
4205 }
4206
4207 if (req->prev_bssid)
4208 memcpy(assoc_data->prev_bssid, req->prev_bssid, ETH_ALEN);
4209
4210 if (req->use_mfp) {
4211 ifmgd->mfp = IEEE80211_MFP_REQUIRED;
4212 ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
4213 } else {
4214 ifmgd->mfp = IEEE80211_MFP_DISABLED;
4215 ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
4216 }
4217
4218 if (req->crypto.control_port)
4219 ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
4220 else
4221 ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
4222
4223 sdata->control_port_protocol = req->crypto.control_port_ethertype;
4224 sdata->control_port_no_encrypt = req->crypto.control_port_no_encrypt;
4225 sdata->encrypt_headroom = ieee80211_cs_headroom(local, &req->crypto,
4226 sdata->vif.type);
4227
4228 /* kick off associate process */
4229
4230 ifmgd->assoc_data = assoc_data;
4231 ifmgd->dtim_period = 0;
4232 ifmgd->have_beacon = false;
4233
4234 err = ieee80211_prep_connection(sdata, req->bss, true);
4235 if (err)
4236 goto err_clear;
4237
4238 rcu_read_lock();
4239 beacon_ies = rcu_dereference(req->bss->beacon_ies);
4240
4241 if (sdata->local->hw.flags & IEEE80211_HW_NEED_DTIM_BEFORE_ASSOC &&
4242 !beacon_ies) {
4243 /*
4244 * Wait up to one beacon interval ...
4245 * should this be more if we miss one?
4246 */
4247 sdata_info(sdata, "waiting for beacon from %pM\n",
4248 ifmgd->bssid);
4249 assoc_data->timeout = TU_TO_EXP_TIME(req->bss->beacon_interval);
4250 assoc_data->timeout_started = true;
4251 assoc_data->need_beacon = true;
4252 } else if (beacon_ies) {
4253 const u8 *tim_ie = cfg80211_find_ie(WLAN_EID_TIM,
4254 beacon_ies->data,
4255 beacon_ies->len);
4256 u8 dtim_count = 0;
4257
4258 if (tim_ie && tim_ie[1] >= sizeof(struct ieee80211_tim_ie)) {
4259 const struct ieee80211_tim_ie *tim;
4260 tim = (void *)(tim_ie + 2);
4261 ifmgd->dtim_period = tim->dtim_period;
4262 dtim_count = tim->dtim_count;
4263 }
4264 ifmgd->have_beacon = true;
4265 assoc_data->timeout = jiffies;
4266 assoc_data->timeout_started = true;
4267
4268 if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
4269 sdata->vif.bss_conf.sync_tsf = beacon_ies->tsf;
4270 sdata->vif.bss_conf.sync_device_ts =
4271 bss->device_ts_beacon;
4272 sdata->vif.bss_conf.sync_dtim_count = dtim_count;
4273 }
4274 } else {
4275 assoc_data->timeout = jiffies;
4276 assoc_data->timeout_started = true;
4277 }
4278 rcu_read_unlock();
4279
4280 run_again(sdata, assoc_data->timeout);
4281
4282 if (bss->corrupt_data) {
4283 char *corrupt_type = "data";
4284 if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_BEACON) {
4285 if (bss->corrupt_data &
4286 IEEE80211_BSS_CORRUPT_PROBE_RESP)
4287 corrupt_type = "beacon and probe response";
4288 else
4289 corrupt_type = "beacon";
4290 } else if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_PROBE_RESP)
4291 corrupt_type = "probe response";
4292 sdata_info(sdata, "associating with AP with corrupt %s\n",
4293 corrupt_type);
4294 }
4295
4296 return 0;
4297 err_clear:
4298 memset(ifmgd->bssid, 0, ETH_ALEN);
4299 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
4300 ifmgd->assoc_data = NULL;
4301 err_free:
4302 kfree(assoc_data);
4303 return err;
4304 }
4305
4306 int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
4307 struct cfg80211_deauth_request *req)
4308 {
4309 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4310 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
4311 bool tx = !req->local_state_change;
4312 bool report_frame = false;
4313
4314 sdata_info(sdata,
4315 "deauthenticating from %pM by local choice (reason=%d)\n",
4316 req->bssid, req->reason_code);
4317
4318 if (ifmgd->auth_data) {
4319 drv_mgd_prepare_tx(sdata->local, sdata);
4320 ieee80211_send_deauth_disassoc(sdata, req->bssid,
4321 IEEE80211_STYPE_DEAUTH,
4322 req->reason_code, tx,
4323 frame_buf);
4324 ieee80211_destroy_auth_data(sdata, false);
4325
4326 report_frame = true;
4327 goto out;
4328 }
4329
4330 if (ifmgd->associated &&
4331 ether_addr_equal(ifmgd->associated->bssid, req->bssid)) {
4332 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
4333 req->reason_code, tx, frame_buf);
4334 report_frame = true;
4335 }
4336
4337 out:
4338 if (report_frame)
4339 cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
4340 IEEE80211_DEAUTH_FRAME_LEN);
4341
4342 return 0;
4343 }
4344
4345 int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
4346 struct cfg80211_disassoc_request *req)
4347 {
4348 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4349 u8 bssid[ETH_ALEN];
4350 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
4351
4352 /*
4353 * cfg80211 should catch this ... but it's racy since
4354 * we can receive a disassoc frame, process it, hand it
4355 * to cfg80211 while that's in a locked section already
4356 * trying to tell us that the user wants to disconnect.
4357 */
4358 if (ifmgd->associated != req->bss)
4359 return -ENOLINK;
4360
4361 sdata_info(sdata,
4362 "disassociating from %pM by local choice (reason=%d)\n",
4363 req->bss->bssid, req->reason_code);
4364
4365 memcpy(bssid, req->bss->bssid, ETH_ALEN);
4366 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DISASSOC,
4367 req->reason_code, !req->local_state_change,
4368 frame_buf);
4369
4370 cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
4371 IEEE80211_DEAUTH_FRAME_LEN);
4372
4373 return 0;
4374 }
4375
4376 void ieee80211_mgd_stop(struct ieee80211_sub_if_data *sdata)
4377 {
4378 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4379
4380 /*
4381 * Make sure some work items will not run after this,
4382 * they will not do anything but might not have been
4383 * cancelled when disconnecting.
4384 */
4385 cancel_work_sync(&ifmgd->monitor_work);
4386 cancel_work_sync(&ifmgd->beacon_connection_loss_work);
4387 cancel_work_sync(&ifmgd->request_smps_work);
4388 cancel_work_sync(&ifmgd->csa_connection_drop_work);
4389 cancel_work_sync(&ifmgd->chswitch_work);
4390
4391 sdata_lock(sdata);
4392 if (ifmgd->assoc_data) {
4393 struct cfg80211_bss *bss = ifmgd->assoc_data->bss;
4394 ieee80211_destroy_assoc_data(sdata, false);
4395 cfg80211_assoc_timeout(sdata->dev, bss);
4396 }
4397 if (ifmgd->auth_data)
4398 ieee80211_destroy_auth_data(sdata, false);
4399 del_timer_sync(&ifmgd->timer);
4400 sdata_unlock(sdata);
4401 }
4402
4403 void ieee80211_cqm_rssi_notify(struct ieee80211_vif *vif,
4404 enum nl80211_cqm_rssi_threshold_event rssi_event,
4405 gfp_t gfp)
4406 {
4407 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4408
4409 trace_api_cqm_rssi_notify(sdata, rssi_event);
4410
4411 cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, gfp);
4412 }
4413 EXPORT_SYMBOL(ieee80211_cqm_rssi_notify);