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