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