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