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