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