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