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