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