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