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