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