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