blob: b97b615d5711aa636989eba676d332c38c3ecc5b [file] [log] [blame]
Jiri Bencf0706e82007-05-05 11:45:53 -07001/*
2 * BSS client mode implementation
Jouni Malinen5394af42009-01-08 13:31:59 +02003 * Copyright 2003-2008, Jouni Malinen <j@w1.fi>
Jiri Bencf0706e82007-05-05 11:45:53 -07004 * 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
Geert Uytterhoeven5b323ed2007-05-08 18:40:27 -070014#include <linux/delay.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070015#include <linux/if_ether.h>
16#include <linux/skbuff.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070017#include <linux/if_arp.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070018#include <linux/etherdevice.h>
Paul Gortmakerd9b93842011-09-18 13:21:27 -040019#include <linux/moduleparam.h>
Johannes Bergd0709a62008-02-25 16:27:46 +010020#include <linux/rtnetlink.h>
Jean Pihete8db0be2011-08-25 15:35:03 +020021#include <linux/pm_qos.h>
Johannes Bergd91f36d2009-04-16 13:17:26 +020022#include <linux/crc32.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040024#include <linux/export.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070025#include <net/mac80211.h>
Johannes Berg472dbc42008-09-11 00:01:49 +020026#include <asm/unaligned.h>
Johannes Berg60f8b392008-09-08 17:44:22 +020027
Jiri Bencf0706e82007-05-05 11:45:53 -070028#include "ieee80211_i.h"
Johannes Berg24487982009-04-23 18:52:52 +020029#include "driver-ops.h"
Johannes Berg2c8dccc2008-04-08 15:14:40 -040030#include "rate.h"
31#include "led.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070032
Johannes Berg1672c0e32013-01-29 15:02:27 +010033#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
Johannes Berg66e67e42012-01-20 13:55:27 +010040
Ben Greear180205b2011-02-04 15:30:24 -080041static 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).");
Johannes Bergb291ba12009-07-10 15:29:03 +020050
51/*
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +010052 * 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.
Johannes Bergb291ba12009-07-10 15:29:03 +020058 */
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +010059#define IEEE80211_BEACON_LOSS_COUNT 7
60
Johannes Bergb291ba12009-07-10 15:29:03 +020061/*
62 * Time the connection can be idle before we probe
63 * it to see if we can still talk to the AP.
64 */
Maxim Levitskyd1c50912009-07-31 18:54:23 +030065#define IEEE80211_CONNECTION_IDLE_TIME (30 * HZ)
Johannes Bergb291ba12009-07-10 15:29:03 +020066/*
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 */
Ben Greear180205b2011-02-04 15:30:24 -080071static 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).");
Jiri Bencf0706e82007-05-05 11:45:53 -070076
Jouni Malinen17e4ec12010-03-29 23:28:30 -070077/*
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
Jouni Malinen391a2002010-08-27 22:22:00 +030084/*
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
Johannes Berg5bb644a2009-05-17 11:40:42 +020090#define TMR_RUNNING_TIMER 0
91#define TMR_RUNNING_CHANSW 1
92
Johannes Berg77fdaa12009-07-07 03:45:17 +020093/*
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
Johannes Berg77fdaa12009-07-07 03:45:17 +0200103 /* caller must call cfg80211_send_deauth() */
104 RX_MGMT_CFG80211_DEAUTH,
105
106 /* caller must call cfg80211_send_disassoc() */
107 RX_MGMT_CFG80211_DISASSOC,
Johannes Berg66e67e42012-01-20 13:55:27 +0100108
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,
Johannes Berg77fdaa12009-07-07 03:45:17 +0200117};
118
Johannes Berg5484e232008-09-08 17:44:27 +0200119/* utils */
Johannes Berg77fdaa12009-07-07 03:45:17 +0200120static inline void ASSERT_MGD_MTX(struct ieee80211_if_managed *ifmgd)
121{
Johannes Berg46a5eba2010-09-15 13:28:15 +0200122 lockdep_assert_held(&ifmgd->mtx);
Johannes Berg77fdaa12009-07-07 03:45:17 +0200123}
124
Johannes Bergca386f32009-07-10 02:39:48 +0200125/*
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 */
Johannes Berg66e67e42012-01-20 13:55:27 +0100135static void run_again(struct ieee80211_if_managed *ifmgd, unsigned long timeout)
Johannes Bergca386f32009-07-10 02:39:48 +0200136{
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
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -0400144void ieee80211_sta_reset_beacon_monitor(struct ieee80211_sub_if_data *sdata)
Johannes Bergb291ba12009-07-10 15:29:03 +0200145{
Johannes Bergc1288b12012-01-19 09:29:57 +0100146 if (sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)
Johannes Bergb291ba12009-07-10 15:29:03 +0200147 return;
148
Johannes Berg7eeff742012-07-18 10:27:27 +0200149 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
150 return;
151
Johannes Bergb291ba12009-07-10 15:29:03 +0200152 mod_timer(&sdata->u.mgd.bcn_mon_timer,
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +0100153 round_jiffies_up(jiffies + sdata->u.mgd.beacon_timeout));
Johannes Bergb291ba12009-07-10 15:29:03 +0200154}
155
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400156void ieee80211_sta_reset_conn_monitor(struct ieee80211_sub_if_data *sdata)
157{
Luis R. Rodriguez0c699c32010-09-16 15:12:30 -0400158 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
159
Stanislaw Gruszka86281722011-02-25 14:46:02 +0100160 if (unlikely(!sdata->u.mgd.associated))
161 return;
162
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400163 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));
Luis R. Rodriguez0c699c32010-09-16 15:12:30 -0400168
169 ifmgd->probe_send_count = 0;
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400170}
171
Johannes Berg5484e232008-09-08 17:44:27 +0200172static int ecw2cw(int ecw)
Johannes Berg60f8b392008-09-08 17:44:22 +0200173{
Johannes Berg5484e232008-09-08 17:44:27 +0200174 return (1 << ecw) - 1;
Johannes Berg60f8b392008-09-08 17:44:22 +0200175}
176
Johannes Berg24398e32012-03-28 10:58:36 +0200177static u32 ieee80211_config_ht_tx(struct ieee80211_sub_if_data *sdata,
Johannes Berg11289582013-02-07 17:36:12 +0100178 struct sta_info *sta,
Johannes Berg24398e32012-03-28 10:58:36 +0200179 struct ieee80211_ht_operation *ht_oper,
180 const u8 *bssid, bool reconfig)
Johannes Bergd5522e02009-03-30 13:23:35 +0200181{
182 struct ieee80211_local *local = sdata->local;
183 struct ieee80211_supported_band *sband;
Johannes Berg55de9082012-07-26 17:24:39 +0200184 struct ieee80211_chanctx_conf *chanctx_conf;
185 struct ieee80211_channel *chan;
Johannes Bergd5522e02009-03-30 13:23:35 +0200186 u32 changed = 0;
Johannes Berg9ed6bcc2009-05-08 20:47:39 +0200187 u16 ht_opmode;
Johannes Berg64f68e52012-03-28 10:58:37 +0200188 bool disable_40 = false;
Johannes Bergd5522e02009-03-30 13:23:35 +0200189
Johannes Berg11289582013-02-07 17:36:12 +0100190 if (WARN_ON_ONCE(!sta))
191 return 0;
192
Johannes Berg55de9082012-07-26 17:24:39 +0200193 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 }
Johannes Berg4bf88532012-11-09 11:39:59 +0100199 chan = chanctx_conf->def.chan;
Johannes Berg55de9082012-07-26 17:24:39 +0200200 rcu_read_unlock();
201 sband = local->hw.wiphy->bands[chan->band];
Johannes Berg0aaffa92010-05-05 15:28:27 +0200202
Johannes Berg4bf88532012-11-09 11:39:59 +0100203 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 &&
Johannes Berg75e69342013-01-11 12:32:37 +0100207 chan->flags & IEEE80211_CHAN_NO_HT40MINUS)
Johannes Berg64f68e52012-03-28 10:58:37 +0200208 disable_40 = true;
Johannes Berg4bf88532012-11-09 11:39:59 +0100209 if (sdata->vif.bss_conf.chandef.chan->center_freq <
210 sdata->vif.bss_conf.chandef.center_freq1 &&
Johannes Berg75e69342013-01-11 12:32:37 +0100211 chan->flags & IEEE80211_CHAN_NO_HT40PLUS)
Johannes Berg64f68e52012-03-28 10:58:37 +0200212 disable_40 = true;
213 break;
214 default:
215 break;
216 }
Johannes Bergd5522e02009-03-30 13:23:35 +0200217
Johannes Berg24398e32012-03-28 10:58:36 +0200218 /* This can change during the lifetime of the BSS */
219 if (!(ht_oper->ht_param & IEEE80211_HT_PARAM_CHAN_WIDTH_ANY))
Johannes Berg64f68e52012-03-28 10:58:37 +0200220 disable_40 = true;
Johannes Bergd5522e02009-03-30 13:23:35 +0200221
Johannes Berge1a0c6b2013-02-07 11:47:44 +0100222 if (!(sta->sta.ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40))
Johannes Berg64f68e52012-03-28 10:58:37 +0200223 disable_40 = true;
224
Johannes Berg11289582013-02-07 17:36:12 +0100225 if (disable_40 != (sta->sta.bandwidth < IEEE80211_STA_RX_BW_40)) {
Johannes Berg64f68e52012-03-28 10:58:37 +0200226 if (disable_40)
Johannes Berge1a0c6b2013-02-07 11:47:44 +0100227 sta->sta.bandwidth = IEEE80211_STA_RX_BW_20;
Johannes Berg64f68e52012-03-28 10:58:37 +0200228 else
Johannes Berge1a0c6b2013-02-07 11:47:44 +0100229 sta->sta.bandwidth = ieee80211_sta_cur_vht_bw(sta);
Rajkumar Manoharan7cc44ed2011-09-16 15:32:34 +0530230
Johannes Berg11289582013-02-07 17:36:12 +0100231 if (reconfig)
232 rate_control_rate_update(local, sband, sta,
233 IEEE80211_RC_BW_CHANGED);
Johannes Berg0aaffa92010-05-05 15:28:27 +0200234 }
Johannes Bergd5522e02009-03-30 13:23:35 +0200235
Johannes Berg074d46d2012-03-15 19:45:16 +0100236 ht_opmode = le16_to_cpu(ht_oper->operation_mode);
Johannes Bergd5522e02009-03-30 13:23:35 +0200237
238 /* if bss configuration changed store the new one */
Johannes Berg24398e32012-03-28 10:58:36 +0200239 if (!reconfig || (sdata->vif.bss_conf.ht_operation_mode != ht_opmode)) {
Johannes Bergd5522e02009-03-30 13:23:35 +0200240 changed |= BSS_CHANGED_HT;
Johannes Berg9ed6bcc2009-05-08 20:47:39 +0200241 sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
Johannes Bergd5522e02009-03-30 13:23:35 +0200242 }
243
244 return changed;
245}
246
Johannes Berg9c6bd792008-09-11 00:01:52 +0200247/* frame sending functions */
248
Johannes Berg66e67e42012-01-20 13:55:27 +0100249static 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,
Johannes Berg9dde6422012-05-16 23:43:19 +0200271 struct sk_buff *skb, u8 ap_ht_param,
Johannes Berg66e67e42012-01-20 13:55:27 +0100272 struct ieee80211_supported_band *sband,
273 struct ieee80211_channel *channel,
274 enum ieee80211_smps_mode smps)
275{
Johannes Berg66e67e42012-01-20 13:55:27 +0100276 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
Johannes Berg66e67e42012-01-20 13:55:27 +0100283 memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
284 ieee80211_apply_htcap_overrides(sdata, &ht_cap);
285
Johannes Berg66e67e42012-01-20 13:55:27 +0100286 /* determine capability flags */
287 cap = ht_cap.cap;
288
Johannes Berg9dde6422012-05-16 23:43:19 +0200289 switch (ap_ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
Johannes Berg66e67e42012-01-20 13:55:27 +0100290 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
Johannes Berg24398e32012-03-28 10:58:36 +0200304 /*
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
Johannes Berg66e67e42012-01-20 13:55:27 +0100314 /* 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
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000339static void ieee80211_add_vht_ie(struct ieee80211_sub_if_data *sdata,
340 struct sk_buff *skb,
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100341 struct ieee80211_supported_band *sband,
342 struct ieee80211_vht_cap *ap_vht_cap)
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000343{
344 u8 *pos;
345 u32 cap;
346 struct ieee80211_sta_vht_cap vht_cap;
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100347 int i;
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000348
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
Johannes Bergf2d9d272012-11-22 14:11:39 +0100356 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
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100366 /*
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
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000402 /* reserve and fill IE */
Mahesh Palivelad4950282012-10-10 11:25:40 +0000403 pos = skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2);
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000404 ieee80211_ie_build_vht_cap(pos, &vht_cap, cap);
405}
406
Johannes Berg66e67e42012-01-20 13:55:27 +0100407static 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;
Johannes Berg55de9082012-07-26 17:24:39 +0200419 struct ieee80211_chanctx_conf *chanctx_conf;
420 struct ieee80211_channel *chan;
Johannes Berg66e67e42012-01-20 13:55:27 +0100421 u32 rates = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +0100422
423 lockdep_assert_held(&ifmgd->mtx);
424
Johannes Berg55de9082012-07-26 17:24:39 +0200425 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 }
Johannes Berg4bf88532012-11-09 11:39:59 +0100431 chan = chanctx_conf->def.chan;
Johannes Berg55de9082012-07-26 17:24:39 +0200432 rcu_read_unlock();
433 sband = local->hw.wiphy->bands[chan->band];
Johannes Berg66e67e42012-01-20 13:55:27 +0100434
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 */
Mahesh Palivelad4950282012-10-10 11:25:40 +0000462 2 + sizeof(struct ieee80211_vht_cap) + /* VHT */
Johannes Berg66e67e42012-01-20 13:55:27 +0100463 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 */
Johannes Berg55de9082012-07-26 17:24:39 +0200555 *pos++ = chan->max_power; /* max tx power */
Johannes Berg66e67e42012-01-20 13:55:27 +0100556
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
Johannes Bergf2d9d272012-11-22 14:11:39 +0100591 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
Johannes Berga8243b72012-11-22 14:32:09 +0100595 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
Johannes Berg9dde6422012-05-16 23:43:19 +0200596 ieee80211_add_ht_ie(sdata, skb, assoc_data->ap_ht_param,
Johannes Berg04ecd252012-09-11 14:34:12 +0200597 sband, chan, sdata->smps_mode);
Johannes Berg66e67e42012-01-20 13:55:27 +0100598
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000599 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100600 ieee80211_add_vht_ie(sdata, skb, sband,
601 &assoc_data->ap_vht_cap);
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000602
Johannes Berg66e67e42012-01-20 13:55:27 +0100603 /* 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
Johannes Berg76f03032012-03-08 15:02:05 +0100613 if (assoc_data->wmm) {
614 if (assoc_data->uapsd) {
Eliad Pellerdc41e4d2012-03-14 16:15:03 +0200615 qos_info = ifmgd->uapsd_queues;
616 qos_info |= (ifmgd->uapsd_max_sp_len <<
Johannes Berg66e67e42012-01-20 13:55:27 +0100617 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
Johannes Berga1845fc2012-06-27 13:18:36 +0200641 drv_mgd_prepare_tx(local, sdata);
642
Johannes Berg66e67e42012-01-20 13:55:27 +0100643 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
Johannes Berg1672c0e32013-01-29 15:02:27 +0100644 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;
Johannes Berg66e67e42012-01-20 13:55:27 +0100647 ieee80211_tx_skb(sdata, skb);
648}
649
Kalle Valo572e0012009-02-10 17:09:31 +0200650void ieee80211_send_pspoll(struct ieee80211_local *local,
651 struct ieee80211_sub_if_data *sdata)
652{
Kalle Valo572e0012009-02-10 17:09:31 +0200653 struct ieee80211_pspoll *pspoll;
654 struct sk_buff *skb;
Kalle Valo572e0012009-02-10 17:09:31 +0200655
Kalle Valod8cd1892010-01-05 20:16:26 +0200656 skb = ieee80211_pspoll_get(&local->hw, &sdata->vif);
657 if (!skb)
Kalle Valo572e0012009-02-10 17:09:31 +0200658 return;
Kalle Valo572e0012009-02-10 17:09:31 +0200659
Kalle Valod8cd1892010-01-05 20:16:26 +0200660 pspoll = (struct ieee80211_pspoll *) skb->data;
661 pspoll->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
Kalle Valo572e0012009-02-10 17:09:31 +0200662
Johannes Berg62ae67b2009-11-18 18:42:05 +0100663 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
664 ieee80211_tx_skb(sdata, skb);
Kalle Valo572e0012009-02-10 17:09:31 +0200665}
666
Johannes Berg965beda2009-04-16 13:17:24 +0200667void ieee80211_send_nullfunc(struct ieee80211_local *local,
668 struct ieee80211_sub_if_data *sdata,
669 int powersave)
670{
671 struct sk_buff *skb;
Kalle Valod8cd1892010-01-05 20:16:26 +0200672 struct ieee80211_hdr_3addr *nullfunc;
Rajkumar Manoharanb6f35302011-09-29 20:34:04 +0530673 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg965beda2009-04-16 13:17:24 +0200674
Kalle Valod8cd1892010-01-05 20:16:26 +0200675 skb = ieee80211_nullfunc_get(&local->hw, &sdata->vif);
676 if (!skb)
Johannes Berg965beda2009-04-16 13:17:24 +0200677 return;
678
Kalle Valod8cd1892010-01-05 20:16:26 +0200679 nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
Johannes Berg965beda2009-04-16 13:17:24 +0200680 if (powersave)
Kalle Valod8cd1892010-01-05 20:16:26 +0200681 nullfunc->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
Johannes Berg965beda2009-04-16 13:17:24 +0200682
Seth Forshee6c17b772013-02-11 11:21:07 -0600683 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
684 IEEE80211_TX_INTFL_OFFCHAN_TX_OK;
Rajkumar Manoharanb6f35302011-09-29 20:34:04 +0530685 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
686 IEEE80211_STA_CONNECTION_POLL))
687 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_USE_MINRATE;
688
Johannes Berg62ae67b2009-11-18 18:42:05 +0100689 ieee80211_tx_skb(sdata, skb);
Johannes Berg965beda2009-04-16 13:17:24 +0200690}
691
Felix Fietkaud5242152010-01-08 18:06:26 +0100692static 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);
Joe Perchesd15b8452011-08-29 14:17:31 -0700703 if (!skb)
Felix Fietkaud5242152010-01-08 18:06:26 +0100704 return;
Joe Perchesd15b8452011-08-29 14:17:31 -0700705
Felix Fietkaud5242152010-01-08 18:06:26 +0100706 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
Johannes Bergcc32abd2009-05-15 11:52:31 +0200722/* 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);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200727 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
728
Johannes Berg9607e6b662009-12-23 13:15:31 +0100729 if (!ieee80211_sdata_running(sdata))
Johannes Bergcc32abd2009-05-15 11:52:31 +0200730 return;
731
Johannes Berg77fdaa12009-07-07 03:45:17 +0200732 mutex_lock(&ifmgd->mtx);
733 if (!ifmgd->associated)
734 goto out;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200735
Johannes Berg55de9082012-07-26 17:24:39 +0200736 sdata->local->_oper_channel = sdata->local->csa_channel;
Johannes Berg5ce6e432010-05-11 16:20:57 +0200737 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);
Shahar Levi1ea57b12011-09-08 08:44:05 +0300741 } else {
742 /* update the device channel directly */
Johannes Berg55de9082012-07-26 17:24:39 +0200743 sdata->local->hw.conf.channel = sdata->local->_oper_channel;
Johannes Berg5ce6e432010-05-11 16:20:57 +0200744 }
Johannes Bergcc32abd2009-05-15 11:52:31 +0200745
Johannes Berg77fdaa12009-07-07 03:45:17 +0200746 /* XXX: shouldn't really modify cfg80211-owned data! */
Johannes Berg55de9082012-07-26 17:24:39 +0200747 ifmgd->associated->channel = sdata->local->_oper_channel;
Johannes Berg77fdaa12009-07-07 03:45:17 +0200748
Johannes Berg57eebdf2012-08-01 15:50:46 +0200749 /* XXX: wait for a beacon first? */
Johannes Bergcc32abd2009-05-15 11:52:31 +0200750 ieee80211_wake_queues_by_reason(&sdata->local->hw,
751 IEEE80211_QUEUE_STOP_REASON_CSA);
Johannes Berg77fdaa12009-07-07 03:45:17 +0200752 out:
753 ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
754 mutex_unlock(&ifmgd->mtx);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200755}
756
Johannes Berg5ce6e432010-05-11 16:20:57 +0200757void ieee80211_chswitch_done(struct ieee80211_vif *vif, bool success)
758{
Johannes Berg55de9082012-07-26 17:24:39 +0200759 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
760 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg5ce6e432010-05-11 16:20:57 +0200761
762 trace_api_chswitch_done(sdata, success);
763 if (!success) {
Johannes Berg882a7c62012-08-01 22:32:45 +0200764 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);
Johannes Berg5ce6e432010-05-11 16:20:57 +0200770 }
Johannes Berg5ce6e432010-05-11 16:20:57 +0200771}
772EXPORT_SYMBOL(ieee80211_chswitch_done);
773
Johannes Bergcc32abd2009-05-15 11:52:31 +0200774static 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
Johannes Berg5bb644a2009-05-17 11:40:42 +0200780 if (sdata->local->quiescing) {
781 set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
782 return;
783 }
784
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400785 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200786}
787
788void ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
789 struct ieee80211_channel_sw_ie *sw_elem,
Johannes Berg5ce6e432010-05-11 16:20:57 +0200790 struct ieee80211_bss *bss,
791 u64 timestamp)
Johannes Bergcc32abd2009-05-15 11:52:31 +0200792{
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100793 struct cfg80211_bss *cbss =
794 container_of((void *)bss, struct cfg80211_bss, priv);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200795 struct ieee80211_channel *new_ch;
796 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Bruno Randolf59eb21a2011-01-17 13:37:28 +0900797 int new_freq = ieee80211_channel_to_frequency(sw_elem->new_ch_num,
798 cbss->channel->band);
Johannes Berg55de9082012-07-26 17:24:39 +0200799 struct ieee80211_chanctx *chanctx;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200800
Johannes Berg77fdaa12009-07-07 03:45:17 +0200801 ASSERT_MGD_MTX(ifmgd);
802
803 if (!ifmgd->associated)
Johannes Bergcc32abd2009-05-15 11:52:31 +0200804 return;
805
Helmut Schaafbe9c4292009-07-23 12:14:04 +0200806 if (sdata->local->scanning)
Johannes Bergcc32abd2009-05-15 11:52:31 +0200807 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);
Johannes Berg882a7c62012-08-01 22:32:45 +0200816 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);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200822 return;
Johannes Berg882a7c62012-08-01 22:32:45 +0200823 }
Johannes Bergcc32abd2009-05-15 11:52:31 +0200824
Johannes Berg57eebdf2012-08-01 15:50:46 +0200825 ifmgd->flags |= IEEE80211_STA_CSA_RECEIVED;
826
Johannes Berg55de9082012-07-26 17:24:39 +0200827 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);
Simon Wunderlich246dc3f2012-11-30 19:17:27 +0100832 return;
Johannes Berg55de9082012-07-26 17:24:39 +0200833 }
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
Johannes Berg57eebdf2012-08-01 15:50:46 +0200854 if (sw_elem->mode)
855 ieee80211_stop_queues_by_reason(&sdata->local->hw,
856 IEEE80211_QUEUE_STOP_REASON_CSA);
857
Johannes Berg5ce6e432010-05-11 16:20:57 +0200858 if (sdata->local->ops->channel_switch) {
859 /* use driver's channel switch callback */
Johannes Berg57eebdf2012-08-01 15:50:46 +0200860 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
Johannes Berg5ce6e432010-05-11 16:20:57 +0200867 drv_channel_switch(sdata->local, &ch_switch);
868 return;
869 }
870
871 /* channel switch handled in software */
Johannes Berg57eebdf2012-08-01 15:50:46 +0200872 if (sw_elem->count <= 1)
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400873 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
Johannes Berg57eebdf2012-08-01 15:50:46 +0200874 else
Johannes Bergcc32abd2009-05-15 11:52:31 +0200875 mod_timer(&ifmgd->chswitch_timer,
Johannes Berg30490002012-08-01 15:53:45 +0200876 TU_TO_EXP_TIME(sw_elem->count *
877 cbss->beacon_interval));
Johannes Bergcc32abd2009-05-15 11:52:31 +0200878}
879
Johannes Berg1ea6f9c2012-10-24 10:59:25 +0200880static 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)
Johannes Bergcc32abd2009-05-15 11:52:31 +0200884{
Johannes Berg04b7b2f2012-09-05 13:41:37 +0200885 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;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200889
Johannes Berg04b7b2f2012-09-05 13:41:37 +0200890 /* Invalid IE */
891 if (country_ie_len % 2 || country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
Johannes Berg1ea6f9c2012-10-24 10:59:25 +0200892 return 0;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200893
Johannes Berg04b7b2f2012-09-05 13:41:37 +0200894 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;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200908 }
Johannes Berg04b7b2f2012-09-05 13:41:37 +0200909
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)
Johannes Berg1ea6f9c2012-10-24 10:59:25 +0200933 return 0;
Johannes Berg04b7b2f2012-09-05 13:41:37 +0200934
935 new_ap_level = max_t(int, 0, chan_pwr - *pwr_constr_elem);
936
Johannes Berg1ea6f9c2012-10-24 10:59:25 +0200937 if (sdata->ap_power_level == new_ap_level)
938 return 0;
Johannes Berg04b7b2f2012-09-05 13:41:37 +0200939
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);
Johannes Berg1ea6f9c2012-10-24 10:59:25 +0200944 sdata->ap_power_level = new_ap_level;
945 if (__ieee80211_recalc_txpower(sdata))
946 return BSS_CHANGED_TXPOWER;
947 return 0;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200948}
949
Johannes Berg965beda2009-04-16 13:17:24 +0200950/* 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
Johannes Bergd5edaed2009-04-22 23:02:51 +0200956 /*
957 * If we are scanning right now then the parameters will
958 * take effect when scan finishes.
959 */
Helmut Schaafbe9c4292009-07-23 12:14:04 +0200960 if (local->scanning)
Johannes Bergd5edaed2009-04-22 23:02:51 +0200961 return;
962
Johannes Berg965beda2009-04-16 13:17:24 +0200963 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);
Vivek Natarajan375177b2010-02-09 14:50:28 +0530970
Juuso Oikarinen2a130522010-03-09 14:25:02 +0200971 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);
Johannes Berg965beda2009-04-16 13:17:24 +0200977 }
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) {
Johannes Berg965beda2009-04-16 13:17:24 +0200985 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
Jason Young808118c2011-03-10 16:43:19 -0800994static 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;
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200998 bool authorized = false;
Jason Young808118c2011-03-10 16:43:19 -0800999
1000 if (!mgd->powersave)
1001 return false;
1002
Johannes Berg05cb9102011-10-28 11:59:47 +02001003 if (mgd->broken_ap)
1004 return false;
1005
Jason Young808118c2011-03-10 16:43:19 -08001006 if (!mgd->associated)
1007 return false;
1008
Jason Young808118c2011-03-10 16:43:19 -08001009 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)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001016 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
Jason Young808118c2011-03-10 16:43:19 -08001017 rcu_read_unlock();
1018
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001019 return authorized;
Jason Young808118c2011-03-10 16:43:19 -08001020}
1021
Johannes Berg965beda2009-04-16 13:17:24 +02001022/* need to hold RTNL or interface lock */
Johannes Berg10f644a2009-04-16 13:17:25 +02001023void ieee80211_recalc_ps(struct ieee80211_local *local, s32 latency)
Johannes Berg965beda2009-04-16 13:17:24 +02001024{
1025 struct ieee80211_sub_if_data *sdata, *found = NULL;
1026 int count = 0;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001027 int timeout;
Johannes Berg965beda2009-04-16 13:17:24 +02001028
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) {
Johannes Berg9607e6b662009-12-23 13:15:31 +01001035 if (!ieee80211_sdata_running(sdata))
Johannes Berg965beda2009-04-16 13:17:24 +02001036 continue;
Rajkumar Manoharan8c7914d2011-02-01 00:28:59 +05301037 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 }
Johannes Berg965beda2009-04-16 13:17:24 +02001045 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1046 continue;
1047 found = sdata;
1048 count++;
1049 }
1050
Jason Young808118c2011-03-10 16:43:19 -08001051 if (count == 1 && ieee80211_powersave_allowed(found)) {
Johannes Berg10f644a2009-04-16 13:17:25 +02001052 s32 beaconint_us;
1053
1054 if (latency < 0)
Mark Grossed771342010-05-06 01:59:26 +02001055 latency = pm_qos_request(PM_QOS_NETWORK_LATENCY);
Johannes Berg10f644a2009-04-16 13:17:25 +02001056
1057 beaconint_us = ieee80211_tu_to_usec(
1058 found->vif.bss_conf.beacon_int);
1059
Juuso Oikarinenff616382010-06-09 09:51:52 +03001060 timeout = local->dynamic_ps_forced_timeout;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001061 if (timeout < 0) {
1062 /*
Juuso Oikarinenff616382010-06-09 09:51:52 +03001063 * Go to full PSM if the user configures a very low
1064 * latency requirement.
Eliad Peller0ab82b02010-12-03 02:16:23 +02001065 * The 2000 second value is there for compatibility
1066 * until the PM_QOS_NETWORK_LATENCY is configured
1067 * with real values.
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001068 */
Eliad Peller0ab82b02010-12-03 02:16:23 +02001069 if (latency > (1900 * USEC_PER_MSEC) &&
1070 latency != (2000 * USEC_PER_SEC))
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001071 timeout = 0;
Juuso Oikarinenff616382010-06-09 09:51:52 +03001072 else
1073 timeout = 100;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001074 }
Johannes Berg09b85562013-02-06 23:07:41 +01001075 local->hw.conf.dynamic_ps_timeout = timeout;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001076
Johannes Berg04fe2032009-04-22 18:44:37 +02001077 if (beaconint_us > latency) {
Johannes Berg10f644a2009-04-16 13:17:25 +02001078 local->ps_sdata = NULL;
Johannes Berg04fe2032009-04-22 18:44:37 +02001079 } else {
Johannes Berg04fe2032009-04-22 18:44:37 +02001080 int maxslp = 1;
Johannes Berg826262c2012-12-10 16:38:14 +02001081 u8 dtimper = found->u.mgd.dtim_period;
Johannes Berg56007a02010-01-26 14:19:52 +01001082
1083 /* If the TIM IE is invalid, pretend the value is 1 */
1084 if (!dtimper)
1085 dtimper = 1;
1086 else if (dtimper > 1)
Johannes Berg04fe2032009-04-22 18:44:37 +02001087 maxslp = min_t(int, dtimper,
1088 latency / beaconint_us);
1089
Johannes Berg9ccebe62009-04-23 10:32:36 +02001090 local->hw.conf.max_sleep_period = maxslp;
Johannes Berg56007a02010-01-26 14:19:52 +01001091 local->hw.conf.ps_dtim_period = dtimper;
Johannes Berg10f644a2009-04-16 13:17:25 +02001092 local->ps_sdata = found;
Johannes Berg04fe2032009-04-22 18:44:37 +02001093 }
Johannes Berg10f644a2009-04-16 13:17:25 +02001094 } else {
Johannes Berg965beda2009-04-16 13:17:24 +02001095 local->ps_sdata = NULL;
Johannes Berg10f644a2009-04-16 13:17:25 +02001096 }
Johannes Berg965beda2009-04-16 13:17:24 +02001097
1098 ieee80211_change_ps(local);
1099}
1100
Eliad Pellerab095872012-07-27 12:33:22 +03001101void 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
Johannes Berg965beda2009-04-16 13:17:24 +02001111void 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;
Christian Lamparter5e340692011-06-30 21:08:43 +02001132 struct ieee80211_if_managed *ifmgd;
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301133 unsigned long flags;
1134 int q;
Johannes Berg965beda2009-04-16 13:17:24 +02001135
1136 /* can only happen when PS was just disabled anyway */
1137 if (!sdata)
1138 return;
1139
Christian Lamparter5e340692011-06-30 21:08:43 +02001140 ifmgd = &sdata->u.mgd;
1141
Johannes Berg965beda2009-04-16 13:17:24 +02001142 if (local->hw.conf.flags & IEEE80211_CONF_PS)
1143 return;
1144
Johannes Berg09b85562013-02-06 23:07:41 +01001145 if (local->hw.conf.dynamic_ps_timeout > 0) {
Arik Nemtsov77b70232011-06-26 12:06:54 +03001146 /* don't enter PS if TX frames are pending */
1147 if (drv_tx_frames_pending(local)) {
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301148 mod_timer(&local->dynamic_ps_timer, jiffies +
1149 msecs_to_jiffies(
1150 local->hw.conf.dynamic_ps_timeout));
1151 return;
1152 }
Arik Nemtsov77b70232011-06-26 12:06:54 +03001153
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);
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301171 }
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301172
Vivek Natarajan375177b2010-02-09 14:50:28 +05301173 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
Mohammed Shafi Shajakhan9c38a8b2011-12-14 19:46:07 +05301174 !(ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
Vivek Natarajanf3e85b92011-02-23 13:04:32 +05301175 netif_tx_stop_all_queues(sdata->dev);
Johannes Berg965beda2009-04-16 13:17:24 +02001176
Vivek Natarajane8306f92011-04-06 11:41:10 +05301177 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 }
Vivek Natarajanf3e85b92011-02-23 13:04:32 +05301186 }
1187
Juuso Oikarinen2a130522010-03-09 14:25:02 +02001188 if (!((local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) &&
1189 (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)) ||
Vivek Natarajan375177b2010-02-09 14:50:28 +05301190 (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 }
Vivek Natarajanf3e85b92011-02-23 13:04:32 +05301195
Arik Nemtsov77b70232011-06-26 12:06:54 +03001196 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
1197 netif_tx_wake_all_queues(sdata->dev);
Johannes Berg965beda2009-04-16 13:17:24 +02001198}
1199
1200void ieee80211_dynamic_ps_timer(unsigned long data)
1201{
1202 struct ieee80211_local *local = (void *) data;
1203
Luis R. Rodriguez78f1a8b2009-07-27 08:38:25 -07001204 if (local->quiescing || local->suspended)
Johannes Berg5bb644a2009-05-17 11:40:42 +02001205 return;
1206
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04001207 ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
Johannes Berg965beda2009-04-16 13:17:24 +02001208}
1209
Simon Wunderlich164eb022013-02-08 18:16:20 +01001210void 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
Johannes Berg60f8b392008-09-08 17:44:22 +02001223/* MLME */
Johannes Berg7d257452012-07-06 17:37:43 +02001224static bool ieee80211_sta_wmm_params(struct ieee80211_local *local,
Johannes Berg4ced3f72010-07-19 16:39:04 +02001225 struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07001226 u8 *wmm_param, size_t wmm_param_len)
1227{
Jiri Bencf0706e82007-05-05 11:45:53 -07001228 struct ieee80211_tx_queue_params params;
Johannes Berg4ced3f72010-07-19 16:39:04 +02001229 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07001230 size_t left;
1231 int count;
Kalle Valoab133152010-01-12 10:42:31 +02001232 u8 *pos, uapsd_queues = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001233
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001234 if (!local->ops->conf_tx)
Johannes Berg7d257452012-07-06 17:37:43 +02001235 return false;
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001236
Johannes Berg32c50572012-03-28 11:04:29 +02001237 if (local->hw.queues < IEEE80211_NUM_ACS)
Johannes Berg7d257452012-07-06 17:37:43 +02001238 return false;
Johannes Berg3434fbd2008-05-03 00:59:37 +02001239
1240 if (!wmm_param)
Johannes Berg7d257452012-07-06 17:37:43 +02001241 return false;
Johannes Berg3434fbd2008-05-03 00:59:37 +02001242
Jiri Bencf0706e82007-05-05 11:45:53 -07001243 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
Johannes Berg7d257452012-07-06 17:37:43 +02001244 return false;
Kalle Valoab133152010-01-12 10:42:31 +02001245
1246 if (ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED)
Eliad Pellerdc41e4d2012-03-14 16:15:03 +02001247 uapsd_queues = ifmgd->uapsd_queues;
Kalle Valoab133152010-01-12 10:42:31 +02001248
Jiri Bencf0706e82007-05-05 11:45:53 -07001249 count = wmm_param[6] & 0x0f;
Johannes Berg46900292009-02-15 12:44:28 +01001250 if (count == ifmgd->wmm_last_param_set)
Johannes Berg7d257452012-07-06 17:37:43 +02001251 return false;
Johannes Berg46900292009-02-15 12:44:28 +01001252 ifmgd->wmm_last_param_set = count;
Jiri Bencf0706e82007-05-05 11:45:53 -07001253
1254 pos = wmm_param + 8;
1255 left = wmm_param_len - 8;
1256
1257 memset(&params, 0, sizeof(params));
1258
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001259 sdata->wmm_acm = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001260 for (; left >= 4; left -= 4, pos += 4) {
1261 int aci = (pos[0] >> 5) & 0x03;
1262 int acm = (pos[0] >> 4) & 0x01;
Kalle Valoab133152010-01-12 10:42:31 +02001263 bool uapsd = false;
Jiri Bencf0706e82007-05-05 11:45:53 -07001264 int queue;
1265
1266 switch (aci) {
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001267 case 1: /* AC_BK */
Johannes Berge100bb62008-04-30 18:51:21 +02001268 queue = 3;
Johannes Berg988c0f72008-04-17 19:21:22 +02001269 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001270 sdata->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
Kalle Valoab133152010-01-12 10:42:31 +02001271 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
1272 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001273 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001274 case 2: /* AC_VI */
Johannes Berge100bb62008-04-30 18:51:21 +02001275 queue = 1;
Johannes Berg988c0f72008-04-17 19:21:22 +02001276 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001277 sdata->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
Kalle Valoab133152010-01-12 10:42:31 +02001278 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
1279 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001280 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001281 case 3: /* AC_VO */
Johannes Berge100bb62008-04-30 18:51:21 +02001282 queue = 0;
Johannes Berg988c0f72008-04-17 19:21:22 +02001283 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001284 sdata->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
Kalle Valoab133152010-01-12 10:42:31 +02001285 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
1286 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001287 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001288 case 0: /* AC_BE */
Jiri Bencf0706e82007-05-05 11:45:53 -07001289 default:
Johannes Berge100bb62008-04-30 18:51:21 +02001290 queue = 2;
Johannes Berg988c0f72008-04-17 19:21:22 +02001291 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001292 sdata->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
Kalle Valoab133152010-01-12 10:42:31 +02001293 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
1294 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001295 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);
Johannes Bergf434b2d2008-07-10 11:22:31 +02001301 params.txop = get_unaligned_le16(pos + 2);
Kalle Valoab133152010-01-12 10:42:31 +02001302 params.uapsd = uapsd;
1303
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001304 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);
Eliad Pellerf6f3def2011-09-25 20:06:54 +03001309 sdata->tx_conf[queue] = params;
1310 if (drv_conf_tx(local, sdata, queue, &params))
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001311 sdata_err(sdata,
1312 "failed to set TX queue parameters for queue %d\n",
1313 queue);
Jiri Bencf0706e82007-05-05 11:45:53 -07001314 }
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001315
1316 /* enable WMM or activate new settings */
Johannes Berg4ced3f72010-07-19 16:39:04 +02001317 sdata->vif.bss_conf.qos = true;
Johannes Berg7d257452012-07-06 17:37:43 +02001318 return true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001319}
1320
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001321static 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
Johannes Berg7a5158e2008-10-08 10:59:33 +02001337static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
1338 u16 capab, bool erp_valid, u8 erp)
Daniel Drake56282212007-07-10 19:32:10 +02001339{
Johannes Bergbda39332008-10-11 01:51:51 +02001340 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001341 u32 changed = 0;
Johannes Berg7a5158e2008-10-08 10:59:33 +02001342 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);
Johannes Berg55de9082012-07-26 17:24:39 +02001355 if (ieee80211_get_sdata_band(sdata) == IEEE80211_BAND_5GHZ)
Felix Fietkau43d35342010-01-15 03:00:48 +01001356 use_short_slot = true;
Daniel Drake56282212007-07-10 19:32:10 +02001357
Johannes Berg471b3ef2007-12-28 14:32:58 +01001358 if (use_protection != bss_conf->use_cts_prot) {
Johannes Berg471b3ef2007-12-28 14:32:58 +01001359 bss_conf->use_cts_prot = use_protection;
1360 changed |= BSS_CHANGED_ERP_CTS_PROT;
Daniel Drake56282212007-07-10 19:32:10 +02001361 }
Daniel Drake7e9ed182007-07-27 15:43:24 +02001362
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +02001363 if (use_short_preamble != bss_conf->use_short_preamble) {
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +02001364 bss_conf->use_short_preamble = use_short_preamble;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001365 changed |= BSS_CHANGED_ERP_PREAMBLE;
Daniel Drake7e9ed182007-07-27 15:43:24 +02001366 }
Daniel Draked9430a32007-07-27 15:43:24 +02001367
Johannes Berg7a5158e2008-10-08 10:59:33 +02001368 if (use_short_slot != bss_conf->use_short_slot) {
Johannes Berg7a5158e2008-10-08 10:59:33 +02001369 bss_conf->use_short_slot = use_short_slot;
1370 changed |= BSS_CHANGED_ERP_SLOT;
John W. Linville50c4afb2008-04-15 14:09:27 -04001371 }
1372
1373 return changed;
1374}
1375
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001376static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001377 struct cfg80211_bss *cbss,
Johannes Bergae5eb022008-10-14 16:58:37 +02001378 u32 bss_info_changed)
Jiri Bencf0706e82007-05-05 11:45:53 -07001379{
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001380 struct ieee80211_bss *bss = (void *)cbss->priv;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001381 struct ieee80211_local *local = sdata->local;
Juuso Oikarinen68542962010-06-09 13:43:26 +03001382 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001383
Johannes Bergae5eb022008-10-14 16:58:37 +02001384 bss_info_changed |= BSS_CHANGED_ASSOC;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001385 bss_info_changed |= ieee80211_handle_bss_capability(sdata,
Luciano Coelho50ae34a2012-06-20 17:23:24 +03001386 bss_conf->assoc_capability, bss->has_erp_value, bss->erp_value);
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07001387
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +01001388 sdata->u.mgd.beacon_timeout = usecs_to_jiffies(ieee80211_tu_to_usec(
1389 IEEE80211_BEACON_LOSS_COUNT * bss_conf->beacon_int));
1390
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001391 sdata->u.mgd.associated = cbss;
1392 memcpy(sdata->u.mgd.bssid, cbss->bssid, ETH_ALEN);
Johannes Berg471b3ef2007-12-28 14:32:58 +01001393
Jouni Malinen17e4ec12010-03-29 23:28:30 -07001394 sdata->u.mgd.flags |= IEEE80211_STA_RESET_SIGNAL_AVE;
1395
Johannes Berg488dd7b2012-10-29 20:08:01 +01001396 if (sdata->vif.p2p) {
Johannes Berg9caf0362012-11-29 01:25:20 +01001397 const struct cfg80211_bss_ies *ies;
Johannes Berg488dd7b2012-10-29 20:08:01 +01001398
Johannes Berg9caf0362012-11-29 01:25:20 +01001399 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 }
Johannes Berg488dd7b2012-10-29 20:08:01 +01001415 }
Johannes Berg9caf0362012-11-29 01:25:20 +01001416 rcu_read_unlock();
Johannes Berg488dd7b2012-10-29 20:08:01 +01001417 }
1418
Johannes Bergb291ba12009-07-10 15:29:03 +02001419 /* just to be sure */
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001420 ieee80211_stop_poll(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02001421
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001422 ieee80211_led_assoc(local, 1);
1423
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02001424 if (sdata->u.mgd.assoc_data->have_beacon) {
Johannes Berg826262c2012-12-10 16:38:14 +02001425 /*
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;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02001432 bss_info_changed |= BSS_CHANGED_DTIM_PERIOD;
Johannes Berg826262c2012-12-10 16:38:14 +02001433 } else {
Johannes Berge5b900d2010-07-29 16:08:55 +02001434 bss_conf->dtim_period = 0;
Johannes Berg826262c2012-12-10 16:38:14 +02001435 }
Johannes Berge5b900d2010-07-29 16:08:55 +02001436
Juuso Oikarinen68542962010-06-09 13:43:26 +03001437 bss_conf->assoc = 1;
Johannes Berg9cef8732009-05-14 13:10:14 +02001438
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02001439 /* Tell the driver to monitor connection quality (if supported) */
Johannes Bergea086352012-01-19 09:29:58 +01001440 if (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI &&
Juuso Oikarinen68542962010-06-09 13:43:26 +03001441 bss_conf->cqm_rssi_thold)
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02001442 bss_info_changed |= BSS_CHANGED_CQM;
1443
Juuso Oikarinen68542962010-06-09 13:43:26 +03001444 /* Enable ARP filtering */
Johannes Berg0f19b412013-01-14 16:39:07 +01001445 if (bss_conf->arp_addr_cnt)
Juuso Oikarinen68542962010-06-09 13:43:26 +03001446 bss_info_changed |= BSS_CHANGED_ARP_FILTER;
Juuso Oikarinen68542962010-06-09 13:43:26 +03001447
Johannes Bergae5eb022008-10-14 16:58:37 +02001448 ieee80211_bss_info_change_notify(sdata, bss_info_changed);
Guy Cohen8db93692008-07-03 19:56:13 +03001449
Johannes Berg056508d2009-07-30 21:43:55 +02001450 mutex_lock(&local->iflist_mtx);
1451 ieee80211_recalc_ps(local, -1);
1452 mutex_unlock(&local->iflist_mtx);
Kalle Valoe0cb6862008-12-18 23:35:13 +02001453
Johannes Berg04ecd252012-09-11 14:34:12 +02001454 ieee80211_recalc_smps(sdata);
Eliad Pellerab095872012-07-27 12:33:22 +03001455 ieee80211_recalc_ps_vif(sdata);
1456
John W. Linville8a5b33f2010-01-06 15:39:39 -05001457 netif_tx_start_all_queues(sdata->dev);
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001458 netif_carrier_on(sdata->dev);
Jiri Bencf0706e82007-05-05 11:45:53 -07001459}
1460
Jouni Malinene69e95d2010-03-29 23:29:31 -07001461static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg37ad3882012-02-24 13:50:54 +01001462 u16 stype, u16 reason, bool tx,
1463 u8 *frame_buf)
Tomas Winkleraa458d12008-09-09 00:32:12 +03001464{
Johannes Berg46900292009-02-15 12:44:28 +01001465 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Tomas Winkleraa458d12008-09-09 00:32:12 +03001466 struct ieee80211_local *local = sdata->local;
Johannes Berg3cc52402012-03-09 13:12:35 +01001467 u32 changed = 0;
Tomas Winkleraa458d12008-09-09 00:32:12 +03001468
Johannes Berg77fdaa12009-07-07 03:45:17 +02001469 ASSERT_MGD_MTX(ifmgd);
1470
Johannes Berg37ad3882012-02-24 13:50:54 +01001471 if (WARN_ON_ONCE(tx && !frame_buf))
1472 return;
1473
Johannes Bergb291ba12009-07-10 15:29:03 +02001474 if (WARN_ON(!ifmgd->associated))
1475 return;
1476
David Spinadel79543d82012-06-12 09:59:45 +03001477 ieee80211_stop_poll(sdata);
1478
Johannes Berg77fdaa12009-07-07 03:45:17 +02001479 ifmgd->associated = NULL;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001480
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 */
Tomas Winkleraa458d12008-09-09 00:32:12 +03001494
John W. Linville8a5b33f2010-01-06 15:39:39 -05001495 netif_tx_stop_all_queues(sdata->dev);
Tomas Winkleraa458d12008-09-09 00:32:12 +03001496 netif_carrier_off(sdata->dev);
1497
Eliad Peller88bc40e2012-07-12 17:35:33 +03001498 /*
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
Eliad Pellerab095872012-07-27 12:33:22 +03001509 /* disable per-vif ps */
1510 ieee80211_recalc_ps_vif(sdata);
1511
Eliad Pellerf8239812012-06-27 14:18:22 +03001512 /* flush out any pending frame (e.g. DELBA) before deauth/disassoc */
1513 if (tx)
1514 drv_flush(local, false);
1515
Johannes Berg37ad3882012-02-24 13:50:54 +01001516 /* deauthenticate/disassociate now */
1517 if (tx || frame_buf)
Eliad Peller88a9e312012-06-01 11:14:03 +03001518 ieee80211_send_deauth_disassoc(sdata, ifmgd->bssid, stype,
1519 reason, tx, frame_buf);
Johannes Berg37ad3882012-02-24 13:50:54 +01001520
1521 /* flush out frame */
1522 if (tx)
1523 drv_flush(local, false);
1524
Eliad Peller88a9e312012-06-01 11:14:03 +03001525 /* clear bssid only after building the needed mgmt frames */
1526 memset(ifmgd->bssid, 0, ETH_ALEN);
1527
Johannes Berg37ad3882012-02-24 13:50:54 +01001528 /* remove AP and TDLS peers */
Johannes Berg051007d2012-12-13 23:49:02 +01001529 sta_info_flush_defer(sdata);
Johannes Berg37ad3882012-02-24 13:50:54 +01001530
1531 /* finally reset all BSS / config parameters */
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001532 changed |= ieee80211_reset_erp_info(sdata);
1533
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001534 ieee80211_led_assoc(local, 0);
Johannes Bergae5eb022008-10-14 16:58:37 +02001535 changed |= BSS_CHANGED_ASSOC;
1536 sdata->vif.bss_conf.assoc = false;
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001537
Johannes Berg488dd7b2012-10-29 20:08:01 +01001538 sdata->vif.bss_conf.p2p_ctwindow = 0;
1539 sdata->vif.bss_conf.p2p_oppps = false;
1540
Johannes Berg413ad502009-05-08 21:21:06 +02001541 /* on the next assoc, re-program HT parameters */
Ben Greearef96a8422011-11-18 11:32:00 -08001542 memset(&ifmgd->ht_capa, 0, sizeof(ifmgd->ht_capa));
1543 memset(&ifmgd->ht_capa_mask, 0, sizeof(ifmgd->ht_capa_mask));
Johannes Berg413ad502009-05-08 21:21:06 +02001544
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001545 sdata->ap_power_level = IEEE80211_UNSET_POWER_LEVEL;
Vasanthakumar Thiagarajana8302de2009-01-09 18:14:15 +05301546
Kalle Valo520eb822008-12-18 23:35:27 +02001547 del_timer_sync(&local->dynamic_ps_timer);
1548 cancel_work_sync(&local->dynamic_ps_enable_work);
1549
Juuso Oikarinen68542962010-06-09 13:43:26 +03001550 /* Disable ARP filtering */
Johannes Berg0f19b412013-01-14 16:39:07 +01001551 if (sdata->vif.bss_conf.arp_addr_cnt)
Juuso Oikarinen68542962010-06-09 13:43:26 +03001552 changed |= BSS_CHANGED_ARP_FILTER;
Juuso Oikarinen68542962010-06-09 13:43:26 +03001553
Johannes Berg3abead52012-03-02 15:56:59 +01001554 sdata->vif.bss_conf.qos = false;
1555 changed |= BSS_CHANGED_QOS;
1556
Johannes Berg0aaffa92010-05-05 15:28:27 +02001557 /* The BSSID (not really interesting) and HT changed */
1558 changed |= BSS_CHANGED_BSSID | BSS_CHANGED_HT;
Johannes Bergae5eb022008-10-14 16:58:37 +02001559 ieee80211_bss_info_change_notify(sdata, changed);
Tomas Winkler8e268e42008-11-25 13:05:44 +02001560
Johannes Berg3abead52012-03-02 15:56:59 +01001561 /* disassociated - set to defaults now */
1562 ieee80211_set_wmm_default(sdata, false);
1563
Johannes Bergb9dcf712010-08-27 12:35:54 +02001564 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);
Johannes Berg2d9957c2012-08-01 20:54:52 +02001568
1569 sdata->u.mgd.timers_running = 0;
Johannes Berg028e8da02012-11-26 11:57:41 +01001570
Johannes Berg826262c2012-12-10 16:38:14 +02001571 sdata->vif.bss_conf.dtim_period = 0;
1572
Johannes Berg028e8da02012-11-26 11:57:41 +01001573 ifmgd->flags = 0;
1574 ieee80211_vif_release_channel(sdata);
Tomas Winkleraa458d12008-09-09 00:32:12 +03001575}
Jiri Bencf0706e82007-05-05 11:45:53 -07001576
Kalle Valo3cf335d52009-03-22 21:57:06 +02001577void 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
Johannes Bergb291ba12009-07-10 15:29:03 +02001585 * data idle periods for sending the periodic probe request to the
1586 * AP we're connected to.
Kalle Valo3cf335d52009-03-22 21:57:06 +02001587 */
Johannes Bergb291ba12009-07-10 15:29:03 +02001588 if (is_multicast_ether_addr(hdr->addr1))
1589 return;
1590
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -04001591 ieee80211_sta_reset_conn_monitor(sdata);
Kalle Valo3cf335d52009-03-22 21:57:06 +02001592}
Jiri Bencf0706e82007-05-05 11:45:53 -07001593
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001594static void ieee80211_reset_ap_probe(struct ieee80211_sub_if_data *sdata)
1595{
1596 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001597 struct ieee80211_local *local = sdata->local;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001598
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001599 mutex_lock(&local->mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001600 if (!(ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001601 IEEE80211_STA_CONNECTION_POLL))) {
1602 mutex_unlock(&local->mtx);
1603 return;
1604 }
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001605
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001606 __ieee80211_stop_poll(sdata);
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001607
1608 mutex_lock(&local->iflist_mtx);
1609 ieee80211_recalc_ps(local, -1);
1610 mutex_unlock(&local->iflist_mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001611
1612 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001613 goto out;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001614
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));
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001625out:
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001626 mutex_unlock(&local->mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001627}
1628
1629void ieee80211_sta_tx_notify(struct ieee80211_sub_if_data *sdata,
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001630 struct ieee80211_hdr *hdr, bool ack)
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001631{
Felix Fietkau75706d02010-12-02 21:01:07 +01001632 if (!ieee80211_is_data(hdr->frame_control))
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001633 return;
1634
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001635 if (ieee80211_is_nullfunc(hdr->frame_control) &&
1636 sdata->u.mgd.probe_send_count > 0) {
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001637 if (ack)
Wojciech Dubowikcab1c7f2013-02-14 14:08:37 +01001638 ieee80211_sta_reset_conn_monitor(sdata);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001639 else
1640 sdata->u.mgd.nullfunc_failed = true;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001641 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Wojciech Dubowikcab1c7f2013-02-14 14:08:37 +01001642 return;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001643 }
Wojciech Dubowikcab1c7f2013-02-14 14:08:37 +01001644
1645 if (ack)
1646 ieee80211_sta_reset_conn_monitor(sdata);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001647}
1648
Maxim Levitskya43abf22009-07-31 18:54:12 +03001649static 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;
Luis R. Rodriguezf01a0672010-09-16 15:12:34 -04001653 u8 *dst = ifmgd->associated->bssid;
Ben Greear180205b2011-02-04 15:30:24 -08001654 u8 unicast_limit = max(1, max_probe_tries - 3);
Luis R. Rodriguezf01a0672010-09-16 15:12:34 -04001655
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;
Maxim Levitskya43abf22009-07-31 18:54:12 +03001663
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001664 /*
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 */
Soumik Das992e68b2012-05-20 15:31:13 +05301671 ifmgd->probe_send_count++;
1672
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001673 if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
1674 ifmgd->nullfunc_failed = false;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001675 ieee80211_send_nullfunc(sdata->local, sdata, 0);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001676 } else {
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02001677 int ssid_len;
1678
Johannes Berg9caf0362012-11-29 01:25:20 +01001679 rcu_read_lock();
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001680 ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02001681 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,
Johannes Berg1672c0e32013-01-29 15:02:27 +01001687 0, (u32) -1, true, 0,
Johannes Berg55de9082012-07-26 17:24:39 +02001688 ifmgd->associated->channel, false);
Johannes Berg9caf0362012-11-29 01:25:20 +01001689 rcu_read_unlock();
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001690 }
Maxim Levitskya43abf22009-07-31 18:54:12 +03001691
Ben Greear180205b2011-02-04 15:30:24 -08001692 ifmgd->probe_timeout = jiffies + msecs_to_jiffies(probe_wait_ms);
Maxim Levitskya43abf22009-07-31 18:54:12 +03001693 run_again(ifmgd, ifmgd->probe_timeout);
Rajkumar Manoharanf69b9c72012-03-15 06:15:26 +05301694 if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
1695 drv_flush(sdata->local, false);
Maxim Levitskya43abf22009-07-31 18:54:12 +03001696}
1697
Johannes Bergb291ba12009-07-10 15:29:03 +02001698static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
1699 bool beacon)
Kalle Valo04de8382009-03-22 21:57:35 +02001700{
Kalle Valo04de8382009-03-22 21:57:35 +02001701 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02001702 bool already = false;
Johannes Berg34bfc412009-05-12 19:58:12 +02001703
Johannes Berg9607e6b662009-12-23 13:15:31 +01001704 if (!ieee80211_sdata_running(sdata))
Johannes Berg0e2b6282009-07-13 13:23:39 +02001705 return;
1706
Johannes Berg77fdaa12009-07-07 03:45:17 +02001707 mutex_lock(&ifmgd->mtx);
1708
1709 if (!ifmgd->associated)
1710 goto out;
1711
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001712 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
Joe Perchese87cc472012-05-13 21:56:26 +00001719 if (beacon)
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001720 mlme_dbg_ratelimited(sdata,
Johannes Berg112c31f2013-02-08 22:59:00 +01001721 "detected beacon loss from AP - probing\n");
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001722
Holger Schurig6efb71b2012-05-15 15:38:59 +02001723 ieee80211_cqm_rssi_notify(&sdata->vif,
1724 NL80211_CQM_RSSI_BEACON_LOSS_EVENT, GFP_KERNEL);
Kalle Valo04de8382009-03-22 21:57:35 +02001725
Johannes Bergb291ba12009-07-10 15:29:03 +02001726 /*
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
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001746 mutex_unlock(&sdata->local->mtx);
1747
Johannes Bergb291ba12009-07-10 15:29:03 +02001748 if (already)
1749 goto out;
1750
Johannes Berg4e751842009-06-10 15:16:52 +02001751 mutex_lock(&sdata->local->iflist_mtx);
1752 ieee80211_recalc_ps(sdata->local, -1);
1753 mutex_unlock(&sdata->local->iflist_mtx);
1754
Maxim Levitskya43abf22009-07-31 18:54:12 +03001755 ifmgd->probe_send_count = 0;
1756 ieee80211_mgd_probe_ap_send(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001757 out:
1758 mutex_unlock(&ifmgd->mtx);
Kalle Valo04de8382009-03-22 21:57:35 +02001759}
1760
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001761struct 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;
Eliad Pellerd9b3b282012-06-28 15:03:13 +03001766 struct cfg80211_bss *cbss;
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001767 struct sk_buff *skb;
1768 const u8 *ssid;
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02001769 int ssid_len;
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001770
1771 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
1772 return NULL;
1773
1774 ASSERT_MGD_MTX(ifmgd);
1775
Eliad Pellerd9b3b282012-06-28 15:03:13 +03001776 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
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001783 return NULL;
1784
Johannes Berg9caf0362012-11-29 01:25:20 +01001785 rcu_read_lock();
Eliad Pellerd9b3b282012-06-28 15:03:13 +03001786 ssid = ieee80211_bss_get_ie(cbss, WLAN_EID_SSID);
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02001787 if (WARN_ON_ONCE(ssid == NULL))
1788 ssid_len = 0;
1789 else
1790 ssid_len = ssid[1];
1791
Eliad Pellerd9b3b282012-06-28 15:03:13 +03001792 skb = ieee80211_build_probe_req(sdata, cbss->bssid,
Johannes Berg55de9082012-07-26 17:24:39 +02001793 (u32) -1, cbss->channel,
Johannes Berg6b778632012-07-23 14:53:27 +02001794 ssid + 2, ssid_len,
Johannes Berg85a237f2011-07-18 18:08:36 +02001795 NULL, 0, true);
Johannes Berg9caf0362012-11-29 01:25:20 +01001796 rcu_read_unlock();
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001797
1798 return skb;
1799}
1800EXPORT_SYMBOL(ieee80211_ap_probereq_get);
1801
Johannes Bergeef9e542013-01-29 13:09:34 +01001802static void __ieee80211_disconnect(struct ieee80211_sub_if_data *sdata)
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001803{
1804 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02001805 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001806
1807 mutex_lock(&ifmgd->mtx);
1808 if (!ifmgd->associated) {
1809 mutex_unlock(&ifmgd->mtx);
1810 return;
1811 }
1812
Johannes Berg37ad3882012-02-24 13:50:54 +01001813 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
1814 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
Johannes Bergeef9e542013-01-29 13:09:34 +01001815 true, frame_buf);
Johannes Berg882a7c62012-08-01 22:32:45 +02001816 ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
Johannes Berg5b36ebd2013-02-13 14:21:45 +01001817 ieee80211_wake_queues_by_reason(&sdata->local->hw,
1818 IEEE80211_QUEUE_STOP_REASON_CSA);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001819 mutex_unlock(&ifmgd->mtx);
Johannes Berg7da7cc12010-08-05 17:02:38 +02001820
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001821 /*
1822 * must be outside lock due to cfg80211,
1823 * but that's not a problem.
1824 */
Antonio Quartulli6ae16772012-09-07 13:28:52 +02001825 cfg80211_send_deauth(sdata->dev, frame_buf, IEEE80211_DEAUTH_FRAME_LEN);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001826}
1827
Johannes Bergcc74c0c2012-08-01 16:49:34 +02001828static void ieee80211_beacon_connection_loss_work(struct work_struct *work)
Johannes Bergb291ba12009-07-10 15:29:03 +02001829{
1830 struct ieee80211_sub_if_data *sdata =
1831 container_of(work, struct ieee80211_sub_if_data,
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001832 u.mgd.beacon_connection_loss_work);
Paul Stewarta85e1d52011-12-09 11:01:49 -08001833 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1834 struct sta_info *sta;
1835
1836 if (ifmgd->associated) {
Mohammed Shafi Shajakhan30fa9042011-12-27 18:54:07 +05301837 rcu_read_lock();
Paul Stewarta85e1d52011-12-09 11:01:49 -08001838 sta = sta_info_get(sdata, ifmgd->bssid);
1839 if (sta)
1840 sta->beacon_loss_count++;
Mohammed Shafi Shajakhan30fa9042011-12-27 18:54:07 +05301841 rcu_read_unlock();
Paul Stewarta85e1d52011-12-09 11:01:49 -08001842 }
Johannes Bergb291ba12009-07-10 15:29:03 +02001843
Johannes Berg682bd382013-01-29 13:13:50 +01001844 if (ifmgd->connection_loss) {
Johannes Berg882a7c62012-08-01 22:32:45 +02001845 sdata_info(sdata, "Connection to AP %pM lost\n",
1846 ifmgd->bssid);
Johannes Bergeef9e542013-01-29 13:09:34 +01001847 __ieee80211_disconnect(sdata);
Johannes Berg882a7c62012-08-01 22:32:45 +02001848 } else {
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001849 ieee80211_mgd_probe_ap(sdata, true);
Johannes Berg882a7c62012-08-01 22:32:45 +02001850 }
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
Johannes Bergeef9e542013-01-29 13:09:34 +01001859 __ieee80211_disconnect(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02001860}
1861
Kalle Valo04de8382009-03-22 21:57:35 +02001862void ieee80211_beacon_loss(struct ieee80211_vif *vif)
1863{
1864 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001865 struct ieee80211_hw *hw = &sdata->local->hw;
Kalle Valo04de8382009-03-22 21:57:35 +02001866
Johannes Bergb5878a22010-04-07 16:48:40 +02001867 trace_api_beacon_loss(sdata);
1868
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001869 WARN_ON(hw->flags & IEEE80211_HW_CONNECTION_MONITOR);
Johannes Berg682bd382013-01-29 13:13:50 +01001870 sdata->u.mgd.connection_loss = false;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001871 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
Kalle Valo04de8382009-03-22 21:57:35 +02001872}
1873EXPORT_SYMBOL(ieee80211_beacon_loss);
1874
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001875void 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
Johannes Bergb5878a22010-04-07 16:48:40 +02001880 trace_api_connection_loss(sdata);
1881
Johannes Berg682bd382013-01-29 13:13:50 +01001882 sdata->u.mgd.connection_loss = true;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001883 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
1884}
1885EXPORT_SYMBOL(ieee80211_connection_loss);
1886
1887
Johannes Berg66e67e42012-01-20 13:55:27 +01001888static 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
Johannes Berg66e67e42012-01-20 13:55:27 +01001895 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);
Johannes Berg028e8da02012-11-26 11:57:41 +01001900 sdata->u.mgd.flags = 0;
Johannes Berg55de9082012-07-26 17:24:39 +02001901 ieee80211_vif_release_channel(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01001902 }
1903
Johannes Berg5b112d32013-02-01 01:49:58 +01001904 cfg80211_put_bss(sdata->local->hw.wiphy, auth_data->bss);
Johannes Berg66e67e42012-01-20 13:55:27 +01001905 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{
Johannes Berg1672c0e32013-01-29 15:02:27 +01001912 struct ieee80211_local *local = sdata->local;
Johannes Berg66e67e42012-01-20 13:55:27 +01001913 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
1914 u8 *pos;
1915 struct ieee802_11_elems elems;
Johannes Berg1672c0e32013-01-29 15:02:27 +01001916 u32 tx_flags = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +01001917
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;
Johannes Berga1845fc2012-06-27 13:18:36 +02001923 drv_mgd_prepare_tx(sdata->local, sdata);
Johannes Berg1672c0e32013-01-29 15:02:27 +01001924 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;
Jouni Malinen700e8ea2012-09-30 19:29:37 +03001927 ieee80211_send_auth(sdata, 3, auth_data->algorithm, 0,
Johannes Berg66e67e42012-01-20 13:55:27 +01001928 elems.challenge - 2, elems.challenge_len + 2,
1929 auth_data->bss->bssid, auth_data->bss->bssid,
1930 auth_data->key, auth_data->key_len,
Johannes Berg1672c0e32013-01-29 15:02:27 +01001931 auth_data->key_idx, tx_flags);
Johannes Berg66e67e42012-01-20 13:55:27 +01001932}
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
Joe Perchesb203ca32012-05-08 18:56:52 +00001953 if (!ether_addr_equal(bssid, mgmt->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01001954 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 ||
Jouni Malinen0f4126e2012-09-30 19:29:38 +03001961 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);
Johannes Berg66e67e42012-01-20 13:55:27 +01001966 return RX_MGMT_NONE;
Jouni Malinen0f4126e2012-09-30 19:29:38 +03001967 }
Johannes Berg66e67e42012-01-20 13:55:27 +01001968
1969 if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001970 sdata_info(sdata, "%pM denied authentication (status %d)\n",
1971 mgmt->sa, status_code);
Eliad Pellerdac211e2012-05-13 18:07:04 +03001972 ieee80211_destroy_auth_data(sdata, false);
1973 return RX_MGMT_CFG80211_RX_AUTH;
Johannes Berg66e67e42012-01-20 13:55:27 +01001974 }
1975
1976 switch (ifmgd->auth_data->algorithm) {
1977 case WLAN_AUTH_OPEN:
1978 case WLAN_AUTH_LEAP:
1979 case WLAN_AUTH_FT:
Jouni Malinen6b8ece32012-09-30 19:29:40 +03001980 case WLAN_AUTH_SAE:
Johannes Berg66e67e42012-01-20 13:55:27 +01001981 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
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001995 sdata_info(sdata, "authenticated\n");
Johannes Berg66e67e42012-01-20 13:55:27 +01001996 ifmgd->auth_data->done = true;
1997 ifmgd->auth_data->timeout = jiffies + IEEE80211_AUTH_WAIT_ASSOC;
Johannes Berg89afe612013-02-13 15:39:57 +01001998 ifmgd->auth_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01001999 run_again(ifmgd, ifmgd->auth_data->timeout);
2000
Jouni Malinen6b8ece32012-09-30 19:29:40 +03002001 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
Johannes Berg66e67e42012-01-20 13:55:27 +01002010 /* 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)) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002018 sdata_info(sdata, "failed moving %pM to auth\n", bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01002019 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
Johannes Berg77fdaa12009-07-07 03:45:17 +02002031static enum rx_mgmt_action __must_check
2032ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
Johannes Berg77fdaa12009-07-07 03:45:17 +02002033 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002034{
Johannes Berg46900292009-02-15 12:44:28 +01002035 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002036 const u8 *bssid = NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -07002037 u16 reason_code;
2038
Johannes Berg66e67e42012-01-20 13:55:27 +01002039 lockdep_assert_held(&ifmgd->mtx);
2040
Johannes Bergf4ea83d2008-06-30 15:10:46 +02002041 if (len < 24 + 2)
Johannes Berg77fdaa12009-07-07 03:45:17 +02002042 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002043
Johannes Berg66e67e42012-01-20 13:55:27 +01002044 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002045 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01002046 return RX_MGMT_NONE;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002047
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002048 bssid = ifmgd->associated->bssid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002049
2050 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
2051
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002052 sdata_info(sdata, "deauthenticated from %pM (Reason: %u)\n",
2053 bssid, reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07002054
Johannes Berg37ad3882012-02-24 13:50:54 +01002055 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
2056
Johannes Berg77fdaa12009-07-07 03:45:17 +02002057 return RX_MGMT_CFG80211_DEAUTH;
Jiri Bencf0706e82007-05-05 11:45:53 -07002058}
2059
2060
Johannes Berg77fdaa12009-07-07 03:45:17 +02002061static enum rx_mgmt_action __must_check
2062ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
2063 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002064{
Johannes Berg46900292009-02-15 12:44:28 +01002065 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07002066 u16 reason_code;
2067
Johannes Berg66e67e42012-01-20 13:55:27 +01002068 lockdep_assert_held(&ifmgd->mtx);
2069
Johannes Bergf4ea83d2008-06-30 15:10:46 +02002070 if (len < 24 + 2)
Johannes Berg77fdaa12009-07-07 03:45:17 +02002071 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002072
Johannes Berg66e67e42012-01-20 13:55:27 +01002073 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002074 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg77fdaa12009-07-07 03:45:17 +02002075 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002076
2077 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
2078
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002079 sdata_info(sdata, "disassociated from %pM (Reason: %u)\n",
2080 mgmt->sa, reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07002081
Johannes Berg37ad3882012-02-24 13:50:54 +01002082 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
2083
Johannes Berg77fdaa12009-07-07 03:45:17 +02002084 return RX_MGMT_CFG80211_DISASSOC;
Jiri Bencf0706e82007-05-05 11:45:53 -07002085}
2086
Christian Lamparterc74d0842011-10-15 00:14:49 +02002087static 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}
Jiri Bencf0706e82007-05-05 11:45:53 -07002128
Johannes Berg66e67e42012-01-20 13:55:27 +01002129static 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
Johannes Berg66e67e42012-01-20 13:55:27 +01002136 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);
Johannes Berg028e8da02012-11-26 11:57:41 +01002141 sdata->u.mgd.flags = 0;
Johannes Berg55de9082012-07-26 17:24:39 +02002142 ieee80211_vif_release_channel(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002143 }
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,
Johannes Bergaf6b6372009-12-23 13:15:35 +01002151 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002152{
Johannes Berg46900292009-02-15 12:44:28 +01002153 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002154 struct ieee80211_local *local = sdata->local;
Johannes Berg8318d782008-01-24 19:38:38 +01002155 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07002156 struct sta_info *sta;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002157 u8 *pos;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002158 u16 capab_info, aid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002159 struct ieee802_11_elems elems;
Johannes Bergbda39332008-10-11 01:51:51 +02002160 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Johannes Bergae5eb022008-10-14 16:58:37 +02002161 u32 changed = 0;
Christian Lamparterc74d0842011-10-15 00:14:49 +02002162 int err;
Jiri Bencf0706e82007-05-05 11:45:53 -07002163
Johannes Bergaf6b6372009-12-23 13:15:35 +01002164 /* AssocResp and ReassocResp have identical structure */
Jiri Bencf0706e82007-05-05 11:45:53 -07002165
Jiri Bencf0706e82007-05-05 11:45:53 -07002166 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002167 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
Jiri Bencf0706e82007-05-05 11:45:53 -07002168
Johannes Berg1dd84aa2007-10-10 12:03:41 +02002169 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002170 sdata_info(sdata, "invalid AID value 0x%x; bits 15:14 not set\n",
2171 aid);
Johannes Berg1dd84aa2007-10-10 12:03:41 +02002172 aid &= ~(BIT(15) | BIT(14));
2173
Johannes Berg05cb9102011-10-28 11:59:47 +02002174 ifmgd->broken_ap = false;
2175
2176 if (aid == 0 || aid > IEEE80211_MAX_AID) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002177 sdata_info(sdata, "invalid AID value %d (out of range), turn off PS\n",
2178 aid);
Johannes Berg05cb9102011-10-28 11:59:47 +02002179 aid = 0;
2180 ifmgd->broken_ap = true;
2181 }
2182
Johannes Bergaf6b6372009-12-23 13:15:35 +01002183 pos = mgmt->u.assoc_resp.variable;
2184 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
2185
Jiri Bencf0706e82007-05-05 11:45:53 -07002186 if (!elems.supp_rates) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002187 sdata_info(sdata, "no SuppRates element in AssocResp\n");
Johannes Bergaf6b6372009-12-23 13:15:35 +01002188 return false;
Jiri Bencf0706e82007-05-05 11:45:53 -07002189 }
2190
Johannes Berg46900292009-02-15 12:44:28 +01002191 ifmgd->aid = aid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002192
Guy Eilam2a33bee2011-08-17 15:18:15 +03002193 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 */
Johannes Berg7852e362012-01-20 13:55:24 +01002198 sta = sta_info_get(sdata, cbss->bssid);
Guy Eilam2a33bee2011-08-17 15:18:15 +03002199 if (WARN_ON(!sta)) {
2200 mutex_unlock(&sdata->local->sta_mtx);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002201 return false;
Jiri Bencf0706e82007-05-05 11:45:53 -07002202 }
2203
Johannes Berg55de9082012-07-26 17:24:39 +02002204 sband = local->hw.wiphy->bands[ieee80211_get_sdata_band(sdata)];
Johannes Berg8318d782008-01-24 19:38:38 +01002205
Johannes Berga8243b72012-11-22 14:32:09 +01002206 if (elems.ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
Ben Greearef96a8422011-11-18 11:32:00 -08002207 ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
Johannes Berge1a0c6b2013-02-07 11:47:44 +01002208 elems.ht_cap_elem, sta);
Johannes Berg64f68e52012-03-28 10:58:37 +02002209
Mahesh Palivela818255e2012-10-10 11:33:04 +00002210 if (elems.vht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
2211 ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband,
Johannes Berg4a342152013-02-07 11:58:58 +01002212 elems.vht_cap_elem, sta);
Mahesh Palivela818255e2012-10-10 11:33:04 +00002213
Johannes Berg11289582013-02-07 17:36:12 +01002214 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
Johannes Berg4b7679a2008-09-18 18:14:18 +02002220 rate_control_rate_init(sta);
Jiri Bencf0706e82007-05-05 11:45:53 -07002221
Johannes Berg46900292009-02-15 12:44:28 +01002222 if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002223 set_sta_flag(sta, WLAN_STA_MFP);
Jouni Malinen5394af42009-01-08 13:31:59 +02002224
Johannes Bergddf4ac52008-10-22 11:41:38 +02002225 if (elems.wmm_param)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002226 set_sta_flag(sta, WLAN_STA_WME);
Johannes Bergddf4ac52008-10-22 11:41:38 +02002227
Johannes Berg3e4d40f2013-02-07 17:19:08 +01002228 err = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
Johannes Bergc8987872012-01-20 13:55:17 +01002229 if (!err && !(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
2230 err = sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
2231 if (err) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002232 sdata_info(sdata,
2233 "failed to move station %pM to desired state\n",
2234 sta->sta.addr);
Johannes Bergc8987872012-01-20 13:55:17 +01002235 WARN_ON(__sta_info_destroy(sta));
2236 mutex_unlock(&sdata->local->sta_mtx);
2237 return false;
2238 }
2239
Johannes Berg7852e362012-01-20 13:55:24 +01002240 mutex_unlock(&sdata->local->sta_mtx);
Johannes Bergddf4ac52008-10-22 11:41:38 +02002241
Juuso Oikarinenf2176d72010-09-28 14:39:32 +03002242 /*
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
Johannes Bergddf4ac52008-10-22 11:41:38 +02002250 if (elems.wmm_param)
Johannes Berg4ced3f72010-07-19 16:39:04 +02002251 ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
Jiri Bencf0706e82007-05-05 11:45:53 -07002252 elems.wmm_param_len);
Johannes Bergaa837e12009-05-07 16:16:24 +02002253 else
Johannes Berg3abead52012-03-02 15:56:59 +01002254 ieee80211_set_wmm_default(sdata, false);
2255 changed |= BSS_CHANGED_QOS;
Jiri Bencf0706e82007-05-05 11:45:53 -07002256
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002257 /* set AID and assoc capability,
2258 * ieee80211_set_associated() will tell the driver */
Johannes Berg8318d782008-01-24 19:38:38 +01002259 bss_conf->aid = aid;
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002260 bss_conf->assoc_capability = capab_info;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002261 ieee80211_set_associated(sdata, cbss, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07002262
Kalle Valo15b7b062009-03-22 21:57:14 +02002263 /*
Felix Fietkaud5242152010-01-08 18:06:26 +01002264 * 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
2270 /*
Johannes Bergb291ba12009-07-10 15:29:03 +02002271 * Start timer to probe the connection to the AP now.
2272 * Also start the timer that will detect beacon loss.
Kalle Valo15b7b062009-03-22 21:57:14 +02002273 */
Johannes Bergb291ba12009-07-10 15:29:03 +02002274 ieee80211_sta_rx_notify(sdata, (struct ieee80211_hdr *)mgmt);
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -04002275 ieee80211_sta_reset_beacon_monitor(sdata);
Kalle Valo15b7b062009-03-22 21:57:14 +02002276
Johannes Bergaf6b6372009-12-23 13:15:35 +01002277 return true;
Jiri Bencf0706e82007-05-05 11:45:53 -07002278}
2279
Johannes Berg66e67e42012-01-20 13:55:27 +01002280static 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;
Jiri Bencf0706e82007-05-05 11:45:53 -07002291
Johannes Berg66e67e42012-01-20 13:55:27 +01002292 lockdep_assert_held(&ifmgd->mtx);
2293
2294 if (!assoc_data)
2295 return RX_MGMT_NONE;
Joe Perchesb203ca32012-05-08 18:56:52 +00002296 if (!ether_addr_equal(assoc_data->bss->bssid, mgmt->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01002297 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
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002312 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))));
Johannes Berg66e67e42012-01-20 13:55:27 +01002316
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;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002326 sdata_info(sdata,
2327 "%pM rejected association temporarily; comeback duration %u TU (%u ms)\n",
2328 mgmt->sa, tu, ms);
Johannes Berg66e67e42012-01-20 13:55:27 +01002329 assoc_data->timeout = jiffies + msecs_to_jiffies(ms);
Johannes Berg89afe612013-02-13 15:39:57 +01002330 assoc_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01002331 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) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002339 sdata_info(sdata, "%pM denied association (code=%d)\n",
2340 mgmt->sa, status_code);
Johannes Berg66e67e42012-01-20 13:55:27 +01002341 ieee80211_destroy_assoc_data(sdata, false);
2342 } else {
Johannes Berg66e67e42012-01-20 13:55:27 +01002343 if (!ieee80211_assoc_success(sdata, *bss, mgmt, len)) {
2344 /* oops -- internal error -- send timeout for now */
Eliad Peller10a91092012-07-02 14:42:03 +03002345 ieee80211_destroy_assoc_data(sdata, false);
Johannes Berg5b112d32013-02-01 01:49:58 +01002346 cfg80211_put_bss(sdata->local->hw.wiphy, *bss);
Johannes Berg66e67e42012-01-20 13:55:27 +01002347 return RX_MGMT_CFG80211_ASSOC_TIMEOUT;
2348 }
John W. Linville635d9992012-07-09 16:34:34 -04002349 sdata_info(sdata, "associated\n");
Johannes Berg79ebfb82012-02-20 14:19:58 +01002350
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);
Johannes Berg66e67e42012-01-20 13:55:27 +01002357 }
2358
2359 return RX_MGMT_CFG80211_RX_ASSOC;
2360}
Johannes Berg8e3c1b72012-12-10 13:44:19 +01002361
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002362static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
Johannes Berg8e3c1b72012-12-10 13:44:19 +01002363 struct ieee80211_mgmt *mgmt, size_t len,
Jiri Bencf0706e82007-05-05 11:45:53 -07002364 struct ieee80211_rx_status *rx_status,
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002365 struct ieee802_11_elems *elems)
Jiri Bencf0706e82007-05-05 11:45:53 -07002366{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002367 struct ieee80211_local *local = sdata->local;
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002368 int freq;
Johannes Bergc2b13452008-09-11 00:01:55 +02002369 struct ieee80211_bss *bss;
Johannes Bergfab7d4a2008-03-16 18:42:44 +01002370 struct ieee80211_channel *channel;
Johannes Berg56007a02010-01-26 14:19:52 +01002371 bool need_ps = false;
2372
Johannes Berg826262c2012-12-10 16:38:14 +02002373 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))) {
Johannes Berg56007a02010-01-26 14:19:52 +01002378 /* not previously set so we may need to recalc */
Johannes Berg826262c2012-12-10 16:38:14 +02002379 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 }
Johannes Berg56007a02010-01-26 14:19:52 +01002385 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002386
Johannes Berg5bda6172008-09-08 11:05:10 +02002387 if (elems->ds_params && elems->ds_params_len == 1)
Bruno Randolf59eb21a2011-01-17 13:37:28 +09002388 freq = ieee80211_channel_to_frequency(elems->ds_params[0],
2389 rx_status->band);
Johannes Berg5bda6172008-09-08 11:05:10 +02002390 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;
Jiri Bencf0706e82007-05-05 11:45:53 -07002397
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002398 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002399 channel);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002400 if (bss)
2401 ieee80211_rx_bss_put(local, bss);
2402
2403 if (!sdata->u.mgd.associated)
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002404 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002405
Johannes Berg56007a02010-01-26 14:19:52 +01002406 if (need_ps) {
2407 mutex_lock(&local->iflist_mtx);
2408 ieee80211_recalc_ps(local, -1);
2409 mutex_unlock(&local->iflist_mtx);
2410 }
2411
Johannes Berg5bc14202012-08-01 16:13:02 +02002412 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,
Johannes Berg5ce6e432010-05-11 16:20:57 +02002415 bss, rx_status->mactime);
Jiri Bencf0706e82007-05-05 11:45:53 -07002416}
2417
2418
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002419static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
Johannes Bergaf6b6372009-12-23 13:15:35 +01002420 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -07002421{
Johannes Bergaf6b6372009-12-23 13:15:35 +01002422 struct ieee80211_mgmt *mgmt = (void *)skb->data;
Kalle Valo15b7b062009-03-22 21:57:14 +02002423 struct ieee80211_if_managed *ifmgd;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002424 struct ieee80211_rx_status *rx_status = (void *) skb->cb;
2425 size_t baselen, len = skb->len;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002426 struct ieee802_11_elems elems;
2427
Kalle Valo15b7b062009-03-22 21:57:14 +02002428 ifmgd = &sdata->u.mgd;
2429
Johannes Berg77fdaa12009-07-07 03:45:17 +02002430 ASSERT_MGD_MTX(ifmgd);
2431
Joe Perchesb203ca32012-05-08 18:56:52 +00002432 if (!ether_addr_equal(mgmt->da, sdata->vif.addr))
Tomas Winkler8e7cdbb2008-08-03 14:32:01 +03002433 return; /* ignore ProbeResp to foreign address */
2434
Ester Kummerae6a44e2008-06-27 18:54:48 +03002435 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
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002442 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
Ron Rindjunsky9859b812008-08-09 03:02:19 +03002443
Johannes Berg77fdaa12009-07-07 03:45:17 +02002444 if (ifmgd->associated &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002445 ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002446 ieee80211_reset_ap_probe(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002447
2448 if (ifmgd->auth_data && !ifmgd->auth_data->bss->proberesp_ies &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002449 ether_addr_equal(mgmt->bssid, ifmgd->auth_data->bss->bssid)) {
Johannes Berg66e67e42012-01-20 13:55:27 +01002450 /* got probe response, continue with auth */
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002451 sdata_info(sdata, "direct probe responded\n");
Johannes Berg66e67e42012-01-20 13:55:27 +01002452 ifmgd->auth_data->tries = 0;
2453 ifmgd->auth_data->timeout = jiffies;
Johannes Berg89afe612013-02-13 15:39:57 +01002454 ifmgd->auth_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01002455 run_again(ifmgd, ifmgd->auth_data->timeout);
2456 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002457}
2458
Johannes Bergd91f36d2009-04-16 13:17:26 +02002459/*
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 =
Johannes Berg1d4df3a2009-04-22 11:25:43 +02002473 (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) |
Johannes Berg074d46d2012-03-15 19:45:16 +01002478 (1ULL << WLAN_EID_HT_OPERATION);
Johannes Bergd91f36d2009-04-16 13:17:26 +02002479
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002480static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07002481 struct ieee80211_mgmt *mgmt,
2482 size_t len,
2483 struct ieee80211_rx_status *rx_status)
2484{
Johannes Bergd91f36d2009-04-16 13:17:26 +02002485 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002486 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Jiri Bencf0706e82007-05-05 11:45:53 -07002487 size_t baselen;
2488 struct ieee802_11_elems elems;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002489 struct ieee80211_local *local = sdata->local;
Johannes Berg55de9082012-07-26 17:24:39 +02002490 struct ieee80211_chanctx_conf *chanctx_conf;
2491 struct ieee80211_channel *chan;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002492 u32 changed = 0;
Rami Rosenf87ad632012-10-25 10:16:23 +02002493 bool erp_valid;
Johannes Berg7a5158e2008-10-08 10:59:33 +02002494 u8 erp_value = 0;
Johannes Bergd91f36d2009-04-16 13:17:26 +02002495 u32 ncrc;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002496 u8 *bssid;
2497
Johannes Berg66e67e42012-01-20 13:55:27 +01002498 lockdep_assert_held(&ifmgd->mtx);
Jiri Bencf0706e82007-05-05 11:45:53 -07002499
Ester Kummerae6a44e2008-06-27 18:54:48 +03002500 /* Process beacon from the current BSS */
2501 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2502 if (baselen > len)
2503 return;
2504
Johannes Berg55de9082012-07-26 17:24:39 +02002505 rcu_read_lock();
2506 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2507 if (!chanctx_conf) {
2508 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07002509 return;
Johannes Berg55de9082012-07-26 17:24:39 +02002510 }
2511
Johannes Berg4bf88532012-11-09 11:39:59 +01002512 if (rx_status->freq != chanctx_conf->def.chan->center_freq) {
Johannes Berg55de9082012-07-26 17:24:39 +02002513 rcu_read_unlock();
2514 return;
2515 }
Johannes Berg4bf88532012-11-09 11:39:59 +01002516 chan = chanctx_conf->def.chan;
Johannes Berg55de9082012-07-26 17:24:39 +02002517 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07002518
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02002519 if (ifmgd->assoc_data && ifmgd->assoc_data->need_beacon &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002520 ether_addr_equal(mgmt->bssid, ifmgd->assoc_data->bss->bssid)) {
Johannes Berg66e67e42012-01-20 13:55:27 +01002521 ieee802_11_parse_elems(mgmt->u.beacon.variable,
2522 len - baselen, &elems);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002523
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002524 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
Johannes Berg66e67e42012-01-20 13:55:27 +01002525 ifmgd->assoc_data->have_beacon = true;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02002526 ifmgd->assoc_data->need_beacon = false;
Johannes Bergef429da2013-02-05 17:48:40 +01002527 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 }
Johannes Berg66e67e42012-01-20 13:55:27 +01002538 /* continue assoc process */
2539 ifmgd->assoc_data->timeout = jiffies;
Johannes Berg89afe612013-02-13 15:39:57 +01002540 ifmgd->assoc_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01002541 run_again(ifmgd, ifmgd->assoc_data->timeout);
2542 return;
2543 }
2544
2545 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002546 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01002547 return;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002548 bssid = ifmgd->associated->bssid;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002549
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002550 /* 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;
Jouni Malinen3ba06c62010-08-27 22:21:13 +03002554 ifmgd->ave_beacon_signal = rx_status->signal * 16;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002555 ifmgd->last_cqm_event_signal = 0;
Jouni Malinen391a2002010-08-27 22:22:00 +03002556 ifmgd->count_beacon_signal = 1;
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002557 ifmgd->last_ave_beacon_signal = 0;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002558 } 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;
Jouni Malinen391a2002010-08-27 22:22:00 +03002563 ifmgd->count_beacon_signal++;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002564 }
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002565
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;
Emmanuel Grumbach887da912013-01-20 17:32:41 +02002578 drv_rssi_callback(local, sdata, RSSI_EVENT_HIGH);
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002579 } 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;
Emmanuel Grumbach887da912013-01-20 17:32:41 +02002583 drv_rssi_callback(local, sdata, RSSI_EVENT_LOW);
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002584 }
2585 }
2586
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002587 if (bss_conf->cqm_rssi_thold &&
Jouni Malinen391a2002010-08-27 22:22:00 +03002588 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT &&
Johannes Bergea086352012-01-19 09:29:58 +01002589 !(sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI)) {
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002590 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
Johannes Bergb291ba12009-07-10 15:29:03 +02002611 if (ifmgd->flags & IEEE80211_STA_BEACON_POLL) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002612 mlme_dbg_ratelimited(sdata,
Johannes Berg112c31f2013-02-08 22:59:00 +01002613 "cancelling AP probe due to a received beacon\n");
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02002614 mutex_lock(&local->mtx);
Johannes Bergb291ba12009-07-10 15:29:03 +02002615 ifmgd->flags &= ~IEEE80211_STA_BEACON_POLL;
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02002616 ieee80211_run_deferred_scan(local);
2617 mutex_unlock(&local->mtx);
2618
Johannes Berg4e751842009-06-10 15:16:52 +02002619 mutex_lock(&local->iflist_mtx);
2620 ieee80211_recalc_ps(local, -1);
2621 mutex_unlock(&local->iflist_mtx);
Jouni Malinen92778182009-05-14 21:15:36 +03002622 }
2623
Johannes Bergb291ba12009-07-10 15:29:03 +02002624 /*
2625 * Push the beacon loss detection into the future since
2626 * we are processing a beacon from the AP just now.
2627 */
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -04002628 ieee80211_sta_reset_beacon_monitor(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02002629
Johannes Bergd91f36d2009-04-16 13:17:26 +02002630 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
Vivek Natarajan25c9c872009-03-02 20:20:30 +05302635 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) {
Rami Rosenf87ad632012-10-25 10:16:23 +02002636 bool directed_tim = ieee80211_check_tim(elems.tim,
2637 elems.tim_len,
2638 ifmgd->aid);
Kalle Valo1fb36062009-02-10 17:09:24 +02002639 if (directed_tim) {
Kalle Valo572e0012009-02-10 17:09:31 +02002640 if (local->hw.conf.dynamic_ps_timeout > 0) {
Ronald Wahl70b12f22012-03-19 14:37:20 +01002641 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 }
Kalle Valo572e0012009-02-10 17:09:31 +02002646 ieee80211_send_nullfunc(local, sdata, 0);
Rajkumar Manoharan6f0756a2012-03-15 05:50:36 +05302647 } else if (!local->pspolling && sdata->u.mgd.powersave) {
Kalle Valo572e0012009-02-10 17:09:31 +02002648 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 }
Vivek Natarajana97b77b2008-12-23 18:39:02 -08002660 }
2661 }
Johannes Berg7a5158e2008-10-08 10:59:33 +02002662
Johannes Berg488dd7b2012-10-29 20:08:01 +01002663 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
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03002684 if (ncrc == ifmgd->beacon_crc && ifmgd->beacon_crc_valid)
Jouni Malinen30196672009-05-19 17:01:43 +03002685 return;
2686 ifmgd->beacon_crc = ncrc;
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03002687 ifmgd->beacon_crc_valid = true;
Jouni Malinen30196672009-05-19 17:01:43 +03002688
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002689 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
Johannes Berg7d257452012-07-06 17:37:43 +02002690
2691 if (ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
2692 elems.wmm_param_len))
2693 changed |= BSS_CHANGED_QOS;
2694
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02002695 /*
2696 * If we haven't had a beacon before, tell the driver about the
Johannes Bergef429da2013-02-05 17:48:40 +01002697 * DTIM period (and beacon timing if desired) now.
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02002698 */
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;
Johannes Bergef429da2013-02-05 17:48:40 +01002705
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
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02002718 changed |= BSS_CHANGED_DTIM_PERIOD;
2719 }
2720
Johannes Berg7a5158e2008-10-08 10:59:33 +02002721 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;
John W. Linville50c4afb2008-04-15 14:09:27 -04002726 }
Johannes Berg7a5158e2008-10-08 10:59:33 +02002727 changed |= ieee80211_handle_bss_capability(sdata,
2728 le16_to_cpu(mgmt->u.beacon.capab_info),
2729 erp_valid, erp_value);
Jiri Bencf0706e82007-05-05 11:45:53 -07002730
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002731
Johannes Berg11289582013-02-07 17:36:12 +01002732 mutex_lock(&local->sta_mtx);
Johannes Berg074d46d2012-03-15 19:45:16 +01002733 if (elems.ht_cap_elem && elems.ht_operation && elems.wmm_param &&
Johannes Berga8243b72012-11-22 14:32:09 +01002734 !(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
Johannes Berg11289582013-02-07 17:36:12 +01002735 changed |= ieee80211_config_ht_tx(sdata,
2736 sta_info_get(sdata, bssid),
2737 elems.ht_operation,
Johannes Berg24398e32012-03-28 10:58:36 +02002738 bssid, true);
Johannes Berg11289582013-02-07 17:36:12 +01002739 mutex_unlock(&local->sta_mtx);
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002740
Johannes Berg04b7b2f2012-09-05 13:41:37 +02002741 if (elems.country_elem && elems.pwr_constr_elem &&
2742 mgmt->u.probe_resp.capab_info &
2743 cpu_to_le16(WLAN_CAPABILITY_SPECTRUM_MGMT))
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02002744 changed |= ieee80211_handle_pwr_constr(sdata, chan,
2745 elems.country_elem,
2746 elems.country_elem_len,
2747 elems.pwr_constr_elem);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002748
Johannes Berg471b3ef2007-12-28 14:32:58 +01002749 ieee80211_bss_info_change_notify(sdata, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07002750}
2751
Johannes Berg1fa57d02010-06-10 10:21:32 +02002752void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
2753 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -07002754{
Johannes Berg77fdaa12009-07-07 03:45:17 +02002755 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07002756 struct ieee80211_rx_status *rx_status;
Jiri Bencf0706e82007-05-05 11:45:53 -07002757 struct ieee80211_mgmt *mgmt;
Johannes Berg66e67e42012-01-20 13:55:27 +01002758 struct cfg80211_bss *bss = NULL;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002759 enum rx_mgmt_action rma = RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002760 u16 fc;
2761
Jiri Bencf0706e82007-05-05 11:45:53 -07002762 rx_status = (struct ieee80211_rx_status *) skb->cb;
2763 mgmt = (struct ieee80211_mgmt *) skb->data;
2764 fc = le16_to_cpu(mgmt->frame_control);
2765
Johannes Berg77fdaa12009-07-07 03:45:17 +02002766 mutex_lock(&ifmgd->mtx);
2767
Johannes Berg66e67e42012-01-20 13:55:27 +01002768 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);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002795 break;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002796 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02002797 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02002798 mutex_unlock(&ifmgd->mtx);
2799
Johannes Berg66e67e42012-01-20 13:55:27 +01002800 switch (rma) {
2801 case RX_MGMT_NONE:
2802 /* no action */
2803 break;
2804 case RX_MGMT_CFG80211_DEAUTH:
Holger Schurigce470612009-10-13 13:28:13 +02002805 cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
Johannes Berg66e67e42012-01-20 13:55:27 +01002806 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);
Johannes Bergb054b742010-06-07 21:50:07 +02002821 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002822}
2823
Johannes Berg9c6bd792008-09-11 00:01:52 +02002824static void ieee80211_sta_timer(unsigned long data)
Jiri Bencf0706e82007-05-05 11:45:53 -07002825{
2826 struct ieee80211_sub_if_data *sdata =
2827 (struct ieee80211_sub_if_data *) data;
Johannes Berg46900292009-02-15 12:44:28 +01002828 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002829 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07002830
Johannes Berg5bb644a2009-05-17 11:40:42 +02002831 if (local->quiescing) {
2832 set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
2833 return;
2834 }
2835
Johannes Berg64592c82010-06-10 10:21:31 +02002836 ieee80211_queue_work(&local->hw, &sdata->work);
Jiri Bencf0706e82007-05-05 11:45:53 -07002837}
2838
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002839static void ieee80211_sta_connection_lost(struct ieee80211_sub_if_data *sdata,
Johannes Berg6b684db2013-01-29 11:35:29 +01002840 u8 *bssid, u8 reason, bool tx)
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002841{
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002842 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02002843 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002844
Johannes Berg37ad3882012-02-24 13:50:54 +01002845 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH, reason,
Johannes Berg6b684db2013-01-29 11:35:29 +01002846 tx, frame_buf);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002847 mutex_unlock(&ifmgd->mtx);
Johannes Berg37ad3882012-02-24 13:50:54 +01002848
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002849 /*
2850 * must be outside lock due to cfg80211,
2851 * but that's not a problem.
2852 */
Antonio Quartulli6ae16772012-09-07 13:28:52 +02002853 cfg80211_send_deauth(sdata->dev, frame_buf, IEEE80211_DEAUTH_FRAME_LEN);
Felix Fietkaufcac4fb2011-11-16 13:34:55 +01002854
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002855 mutex_lock(&ifmgd->mtx);
2856}
2857
Johannes Berg66e67e42012-01-20 13:55:27 +01002858static 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;
Johannes Berg1672c0e32013-01-29 15:02:27 +01002863 u32 tx_flags = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +01002864
2865 lockdep_assert_held(&ifmgd->mtx);
2866
2867 if (WARN_ON_ONCE(!auth_data))
2868 return -EINVAL;
2869
Johannes Berg1672c0e32013-01-29 15:02:27 +01002870 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
Johannes Berg66e67e42012-01-20 13:55:27 +01002874 auth_data->tries++;
2875
2876 if (auth_data->tries > IEEE80211_AUTH_MAX_TRIES) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002877 sdata_info(sdata, "authentication with %pM timed out\n",
2878 auth_data->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01002879
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
Johannes Berga1845fc2012-06-27 13:18:36 +02002889 drv_mgd_prepare_tx(local, sdata);
2890
Johannes Berg66e67e42012-01-20 13:55:27 +01002891 if (auth_data->bss->proberesp_ies) {
Jouni Malinen6b8ece32012-09-30 19:29:40 +03002892 u16 trans = 1;
2893 u16 status = 0;
2894
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002895 sdata_info(sdata, "send auth to %pM (try %d/%d)\n",
2896 auth_data->bss->bssid, auth_data->tries,
2897 IEEE80211_AUTH_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01002898
2899 auth_data->expected_transaction = 2;
Jouni Malinen6b8ece32012-09-30 19:29:40 +03002900
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,
Johannes Berg66e67e42012-01-20 13:55:27 +01002909 auth_data->bss->bssid,
Johannes Berg1672c0e32013-01-29 15:02:27 +01002910 auth_data->bss->bssid, NULL, 0, 0,
2911 tx_flags);
Johannes Berg66e67e42012-01-20 13:55:27 +01002912 } else {
2913 const u8 *ssidie;
2914
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002915 sdata_info(sdata, "direct probe to %pM (try %d/%i)\n",
2916 auth_data->bss->bssid, auth_data->tries,
2917 IEEE80211_AUTH_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01002918
Johannes Berg9caf0362012-11-29 01:25:20 +01002919 rcu_read_lock();
Johannes Berg66e67e42012-01-20 13:55:27 +01002920 ssidie = ieee80211_bss_get_ie(auth_data->bss, WLAN_EID_SSID);
Johannes Berg9caf0362012-11-29 01:25:20 +01002921 if (!ssidie) {
2922 rcu_read_unlock();
Johannes Berg66e67e42012-01-20 13:55:27 +01002923 return -EINVAL;
Johannes Berg9caf0362012-11-29 01:25:20 +01002924 }
Johannes Berg66e67e42012-01-20 13:55:27 +01002925 /*
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],
Johannes Berg1672c0e32013-01-29 15:02:27 +01002930 NULL, 0, (u32) -1, true, tx_flags,
Johannes Berg55de9082012-07-26 17:24:39 +02002931 auth_data->bss->channel, false);
Johannes Berg9caf0362012-11-29 01:25:20 +01002932 rcu_read_unlock();
Johannes Berg66e67e42012-01-20 13:55:27 +01002933 }
2934
Johannes Berg1672c0e32013-01-29 15:02:27 +01002935 if (!(local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)) {
2936 auth_data->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
Johannes Berg89afe612013-02-13 15:39:57 +01002937 ifmgd->auth_data->timeout_started = true;
Johannes Berg1672c0e32013-01-29 15:02:27 +01002938 run_again(ifmgd, auth_data->timeout);
Johannes Berg89afe612013-02-13 15:39:57 +01002939 } else {
2940 auth_data->timeout_started = false;
Johannes Berg1672c0e32013-01-29 15:02:27 +01002941 }
Johannes Berg66e67e42012-01-20 13:55:27 +01002942
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
Johannes Berg66e67e42012-01-20 13:55:27 +01002953 assoc_data->tries++;
2954 if (assoc_data->tries > IEEE80211_ASSOC_MAX_TRIES) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002955 sdata_info(sdata, "association with %pM timed out\n",
2956 assoc_data->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01002957
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
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002967 sdata_info(sdata, "associate with %pM (try %d/%d)\n",
2968 assoc_data->bss->bssid, assoc_data->tries,
2969 IEEE80211_ASSOC_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01002970 ieee80211_send_assoc(sdata);
2971
Johannes Berg1672c0e32013-01-29 15:02:27 +01002972 if (!(local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)) {
2973 assoc_data->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
Johannes Berg89afe612013-02-13 15:39:57 +01002974 assoc_data->timeout_started = true;
Johannes Berg1672c0e32013-01-29 15:02:27 +01002975 run_again(&sdata->u.mgd, assoc_data->timeout);
Johannes Berg89afe612013-02-13 15:39:57 +01002976 } else {
2977 assoc_data->timeout_started = false;
Johannes Berg1672c0e32013-01-29 15:02:27 +01002978 }
Johannes Berg66e67e42012-01-20 13:55:27 +01002979
2980 return 0;
2981}
2982
Johannes Berg1672c0e32013-01-29 15:02:27 +01002983void 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
Johannes Berg1fa57d02010-06-10 10:21:32 +02002995void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
Johannes Berg60f8b392008-09-08 17:44:22 +02002996{
Johannes Berg60f8b392008-09-08 17:44:22 +02002997 struct ieee80211_local *local = sdata->local;
Johannes Berg1fa57d02010-06-10 10:21:32 +02002998 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg60f8b392008-09-08 17:44:22 +02002999
Johannes Berg77fdaa12009-07-07 03:45:17 +02003000 mutex_lock(&ifmgd->mtx);
Johannes Berg60f8b392008-09-08 17:44:22 +02003001
Johannes Berg1672c0e32013-01-29 15:02:27 +01003002 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 }
Johannes Berg89afe612013-02-13 15:39:57 +01003016 ifmgd->auth_data->timeout_started = true;
Johannes Berg1672c0e32013-01-29 15:02:27 +01003017 } 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 }
Johannes Berg89afe612013-02-13 15:39:57 +01003027 ifmgd->assoc_data->timeout_started = true;
Johannes Berg1672c0e32013-01-29 15:02:27 +01003028 }
3029 }
3030
Johannes Berg89afe612013-02-13 15:39:57 +01003031 if (ifmgd->auth_data && ifmgd->auth_data->timeout_started &&
Johannes Berg66e67e42012-01-20 13:55:27 +01003032 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 }
Johannes Berg89afe612013-02-13 15:39:57 +01003050 } else if (ifmgd->auth_data && ifmgd->auth_data->timeout_started)
Johannes Berg66e67e42012-01-20 13:55:27 +01003051 run_again(ifmgd, ifmgd->auth_data->timeout);
3052
Johannes Berg89afe612013-02-13 15:39:57 +01003053 if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started &&
Johannes Berg66e67e42012-01-20 13:55:27 +01003054 time_after(jiffies, ifmgd->assoc_data->timeout)) {
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02003055 if ((ifmgd->assoc_data->need_beacon &&
3056 !ifmgd->assoc_data->have_beacon) ||
Johannes Berg66e67e42012-01-20 13:55:27 +01003057 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 }
Johannes Berg89afe612013-02-13 15:39:57 +01003068 } else if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started)
Johannes Berg66e67e42012-01-20 13:55:27 +01003069 run_again(ifmgd, ifmgd->assoc_data->timeout);
3070
Johannes Bergb291ba12009-07-10 15:29:03 +02003071 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
3072 IEEE80211_STA_CONNECTION_POLL) &&
3073 ifmgd->associated) {
Maxim Levitskya43abf22009-07-31 18:54:12 +03003074 u8 bssid[ETH_ALEN];
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01003075 int max_tries;
Maxim Levitskya43abf22009-07-31 18:54:12 +03003076
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01003077 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01003078
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01003079 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
Ben Greear180205b2011-02-04 15:30:24 -08003080 max_tries = max_nullfunc_tries;
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01003081 else
Ben Greear180205b2011-02-04 15:30:24 -08003082 max_tries = max_probe_tries;
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01003083
Felix Fietkau4e5ff372010-11-23 03:10:31 +01003084 /* ACK received for nullfunc probing frame */
3085 if (!ifmgd->probe_send_count)
3086 ieee80211_reset_ap_probe(sdata);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003087 else if (ifmgd->nullfunc_failed) {
3088 if (ifmgd->probe_send_count < max_tries) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003089 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);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003093 ieee80211_mgd_probe_ap_send(sdata);
3094 } else {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003095 mlme_dbg(sdata,
3096 "No ack for nullfunc frame to AP %pM, disconnecting.\n",
3097 bssid);
Johannes Berg95acac62011-07-12 12:30:59 +02003098 ieee80211_sta_connection_lost(sdata, bssid,
Johannes Berg6b684db2013-01-29 11:35:29 +01003099 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
3100 false);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003101 }
3102 } else if (time_is_after_jiffies(ifmgd->probe_timeout))
Johannes Bergb291ba12009-07-10 15:29:03 +02003103 run_again(ifmgd, ifmgd->probe_timeout);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003104 else if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003105 mlme_dbg(sdata,
3106 "Failed to send nullfunc to AP %pM after %dms, disconnecting\n",
3107 bssid, probe_wait_ms);
Johannes Berg95acac62011-07-12 12:30:59 +02003108 ieee80211_sta_connection_lost(sdata, bssid,
Johannes Berg6b684db2013-01-29 11:35:29 +01003109 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003110 } else if (ifmgd->probe_send_count < max_tries) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003111 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);
Maxim Levitskya43abf22009-07-31 18:54:12 +03003115 ieee80211_mgd_probe_ap_send(sdata);
3116 } else {
Johannes Bergb291ba12009-07-10 15:29:03 +02003117 /*
3118 * We actually lost the connection ... or did we?
3119 * Let's make sure!
3120 */
Ben Greearb38afa82010-10-07 16:12:06 -07003121 wiphy_debug(local->hw.wiphy,
3122 "%s: No probe response from AP %pM"
3123 " after %dms, disconnecting.\n",
3124 sdata->name,
Ben Greear180205b2011-02-04 15:30:24 -08003125 bssid, probe_wait_ms);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003126
Johannes Berg95acac62011-07-12 12:30:59 +02003127 ieee80211_sta_connection_lost(sdata, bssid,
Johannes Berg6b684db2013-01-29 11:35:29 +01003128 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
Johannes Bergb291ba12009-07-10 15:29:03 +02003129 }
3130 }
3131
Johannes Berg77fdaa12009-07-07 03:45:17 +02003132 mutex_unlock(&ifmgd->mtx);
Johannes Berg60f8b392008-09-08 17:44:22 +02003133}
Johannes Berg0a51b272008-09-08 17:44:25 +02003134
Johannes Bergb291ba12009-07-10 15:29:03 +02003135static 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
Johannes Berg682bd382013-01-29 13:13:50 +01003144 sdata->u.mgd.connection_loss = false;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02003145 ieee80211_queue_work(&sdata->local->hw,
3146 &sdata->u.mgd.beacon_connection_loss_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02003147}
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
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04003159 ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02003160}
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
Johannes Berga1678f82008-09-11 00:01:47 +02003171static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
3172{
Eliad Peller494f1fe2012-02-19 15:26:09 +02003173 u32 flags;
3174
Kalle Vaload935682009-04-19 08:47:19 +03003175 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02003176 __ieee80211_stop_poll(sdata);
Kalle Vaload935682009-04-19 08:47:19 +03003177
Johannes Bergb291ba12009-07-10 15:29:03 +02003178 /* let's probe the connection once */
Eliad Peller494f1fe2012-02-19 15:26:09 +02003179 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);
Johannes Bergb291ba12009-07-10 15:29:03 +02003183 /* and do all the other regular work too */
Johannes Berg64592c82010-06-10 10:21:31 +02003184 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Kalle Vaload935682009-04-19 08:47:19 +03003185 }
Johannes Berga1678f82008-09-11 00:01:47 +02003186}
3187
Johannes Berg5bb644a2009-05-17 11:40:42 +02003188#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
Johannes Bergd1f5b7a2010-08-05 17:05:55 +02003199 cancel_work_sync(&ifmgd->request_smps_work);
3200
Johannes Berg0ecfe8062011-11-09 12:14:16 +01003201 cancel_work_sync(&ifmgd->monitor_work);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02003202 cancel_work_sync(&ifmgd->beacon_connection_loss_work);
Johannes Berg882a7c62012-08-01 22:32:45 +02003203 cancel_work_sync(&ifmgd->csa_connection_drop_work);
Johannes Berg5bb644a2009-05-17 11:40:42 +02003204 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);
Johannes Bergb291ba12009-07-10 15:29:03 +02003210
Johannes Bergb291ba12009-07-10 15:29:03 +02003211 /* these will just be re-established on connection */
3212 del_timer_sync(&ifmgd->conn_mon_timer);
3213 del_timer_sync(&ifmgd->bcn_mon_timer);
Johannes Berg5bb644a2009-05-17 11:40:42 +02003214}
3215
3216void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
3217{
3218 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3219
Johannes Berg782d2672013-01-18 21:21:31 +01003220 mutex_lock(&ifmgd->mtx);
3221 if (!ifmgd->associated) {
3222 mutex_unlock(&ifmgd->mtx);
Rajkumar Manoharan676b58c2011-07-07 23:33:39 +05303223 return;
Johannes Berg782d2672013-01-18 21:21:31 +01003224 }
Rajkumar Manoharan676b58c2011-07-07 23:33:39 +05303225
Johannes Berg95acac62011-07-12 12:30:59 +02003226 if (sdata->flags & IEEE80211_SDATA_DISCONNECT_RESUME) {
3227 sdata->flags &= ~IEEE80211_SDATA_DISCONNECT_RESUME;
Johannes Berg782d2672013-01-18 21:21:31 +01003228 mlme_dbg(sdata, "driver requested disconnect after resume\n");
3229 ieee80211_sta_connection_lost(sdata,
3230 ifmgd->associated->bssid,
Johannes Berg6b684db2013-01-29 11:35:29 +01003231 WLAN_REASON_UNSPECIFIED,
3232 true);
Johannes Berg95acac62011-07-12 12:30:59 +02003233 mutex_unlock(&ifmgd->mtx);
Johannes Berg782d2672013-01-18 21:21:31 +01003234 return;
Johannes Berg95acac62011-07-12 12:30:59 +02003235 }
Johannes Berg782d2672013-01-18 21:21:31 +01003236 mutex_unlock(&ifmgd->mtx);
Johannes Berg95acac62011-07-12 12:30:59 +02003237
Johannes Berg5bb644a2009-05-17 11:40:42 +02003238 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);
Felix Fietkauc8a79722010-11-19 22:55:37 +01003242 ieee80211_sta_reset_beacon_monitor(sdata);
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02003243
3244 mutex_lock(&sdata->local->mtx);
Felix Fietkau46090972010-11-23 20:28:03 +01003245 ieee80211_restart_sta_timer(sdata);
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02003246 mutex_unlock(&sdata->local->mtx);
Johannes Berg5bb644a2009-05-17 11:40:42 +02003247}
3248#endif
3249
Johannes Berg9c6bd792008-09-11 00:01:52 +02003250/* interface setup */
3251void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
3252{
Johannes Berg46900292009-02-15 12:44:28 +01003253 struct ieee80211_if_managed *ifmgd;
Johannes Berg9c6bd792008-09-11 00:01:52 +02003254
Johannes Berg46900292009-02-15 12:44:28 +01003255 ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02003256 INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
Johannes Berg46900292009-02-15 12:44:28 +01003257 INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02003258 INIT_WORK(&ifmgd->beacon_connection_loss_work,
3259 ieee80211_beacon_connection_loss_work);
Johannes Berg882a7c62012-08-01 22:32:45 +02003260 INIT_WORK(&ifmgd->csa_connection_drop_work,
3261 ieee80211_csa_connection_drop_work);
Johannes Bergd1f5b7a2010-08-05 17:05:55 +02003262 INIT_WORK(&ifmgd->request_smps_work, ieee80211_request_smps_work);
Johannes Berg46900292009-02-15 12:44:28 +01003263 setup_timer(&ifmgd->timer, ieee80211_sta_timer,
Johannes Berg9c6bd792008-09-11 00:01:52 +02003264 (unsigned long) sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02003265 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);
Johannes Berg46900292009-02-15 12:44:28 +01003269 setup_timer(&ifmgd->chswitch_timer, ieee80211_chswitch_timer,
Sujithc481ec92009-01-06 09:28:37 +05303270 (unsigned long) sdata);
Johannes Berg9c6bd792008-09-11 00:01:52 +02003271
Johannes Bergab1faea2009-07-01 21:41:17 +02003272 ifmgd->flags = 0;
Mohammed Shafi Shajakhan1478acb2011-12-14 19:46:08 +05303273 ifmgd->powersave = sdata->wdev.ps;
Eliad Pellerdc41e4d2012-03-14 16:15:03 +02003274 ifmgd->uapsd_queues = IEEE80211_DEFAULT_UAPSD_QUEUES;
3275 ifmgd->uapsd_max_sp_len = IEEE80211_DEFAULT_MAX_SP_LEN;
Johannes Bergbbbdff92009-04-16 13:27:42 +02003276
Johannes Berg77fdaa12009-07-07 03:45:17 +02003277 mutex_init(&ifmgd->mtx);
Johannes Berg0f782312009-12-01 13:37:02 +01003278
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;
Johannes Berg9c6bd792008-09-11 00:01:52 +02003283}
3284
3285/* scan finished notification */
Johannes Berg0a51b272008-09-08 17:44:25 +02003286void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
3287{
Johannes Berg31ee67a2012-07-06 21:18:24 +02003288 struct ieee80211_sub_if_data *sdata;
Johannes Berga1678f82008-09-11 00:01:47 +02003289
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();
Johannes Berg0a51b272008-09-08 17:44:25 +02003295}
Johannes Berg10f644a2009-04-16 13:17:25 +02003296
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);
3304
3305 mutex_lock(&local->iflist_mtx);
3306 ieee80211_recalc_ps(local, latency_usec);
3307 mutex_unlock(&local->iflist_mtx);
3308
3309 return 0;
3310}
Johannes Berg77fdaa12009-07-07 03:45:17 +02003311
Johannes Bergf2d9d272012-11-22 14:11:39 +01003312static 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
Johannes Berg3d9646d2013-02-09 21:46:34 +01003487out:
Johannes Bergf2d9d272012-11-22 14:11:39 +01003488 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
Johannes Berg3d9646d2013-02-09 21:46:34 +01003496 ret |= chandef_downgrade(chandef);
Johannes Bergf2d9d272012-11-22 14:11:39 +01003497 }
3498
3499 if (chandef->width != vht_chandef.width)
3500 sdata_info(sdata,
Johannes Berg3d9646d2013-02-09 21:46:34 +01003501 "capabilities/regulatory prevented using AP HT/VHT configuration, downgraded\n");
Johannes Bergf2d9d272012-11-22 14:11:39 +01003502
Johannes Bergf2d9d272012-11-22 14:11:39 +01003503 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
Johannes Berg9caf0362012-11-29 01:25:20 +01003519 ht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
Johannes Bergf2d9d272012-11-22 14:11:39 +01003520 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
Johannes Berg9caf0362012-11-29 01:25:20 +01003532 vht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
Johannes Bergf2d9d272012-11-22 14:11:39 +01003533 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
Johannes Bergb17166a2012-07-27 11:41:27 +02003551static int ieee80211_prep_channel(struct ieee80211_sub_if_data *sdata,
3552 struct cfg80211_bss *cbss)
Johannes Berga1cf7752012-03-08 15:02:07 +01003553{
3554 struct ieee80211_local *local = sdata->local;
3555 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg24398e32012-03-28 10:58:36 +02003556 const struct ieee80211_ht_operation *ht_oper = NULL;
Johannes Bergf2d9d272012-11-22 14:11:39 +01003557 const struct ieee80211_vht_operation *vht_oper = NULL;
Johannes Berg24398e32012-03-28 10:58:36 +02003558 struct ieee80211_supported_band *sband;
Johannes Berg4bf88532012-11-09 11:39:59 +01003559 struct cfg80211_chan_def chandef;
Johannes Bergf2d9d272012-11-22 14:11:39 +01003560 int ret;
Johannes Berga1cf7752012-03-08 15:02:07 +01003561
Johannes Berg24398e32012-03-28 10:58:36 +02003562 sband = local->hw.wiphy->bands[cbss->channel->band];
3563
Johannes Bergf2d9d272012-11-22 14:11:39 +01003564 ifmgd->flags &= ~(IEEE80211_STA_DISABLE_40MHZ |
3565 IEEE80211_STA_DISABLE_80P80MHZ |
3566 IEEE80211_STA_DISABLE_160MHZ);
Johannes Berg24398e32012-03-28 10:58:36 +02003567
Johannes Berg9caf0362012-11-29 01:25:20 +01003568 rcu_read_lock();
3569
Johannes Bergf2d9d272012-11-22 14:11:39 +01003570 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
3571 sband->ht_cap.ht_supported) {
3572 const u8 *ht_oper_ie;
3573
Johannes Berg9caf0362012-11-29 01:25:20 +01003574 ht_oper_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_OPERATION);
Johannes Berg24398e32012-03-28 10:58:36 +02003575 if (ht_oper_ie && ht_oper_ie[1] >= sizeof(*ht_oper))
3576 ht_oper = (void *)(ht_oper_ie + 2);
3577 }
3578
Johannes Bergf2d9d272012-11-22 14:11:39 +01003579 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
3580 sband->vht_cap.vht_supported) {
3581 const u8 *vht_oper_ie;
3582
Johannes Berg9caf0362012-11-29 01:25:20 +01003583 vht_oper_ie = ieee80211_bss_get_ie(cbss,
3584 WLAN_EID_VHT_OPERATION);
Johannes Bergf2d9d272012-11-22 14:11:39 +01003585 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;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003589 sdata_info(sdata,
Johannes Bergf2d9d272012-11-22 14:11:39 +01003590 "AP advertised VHT without HT, disabling both\n");
3591 sdata->flags |= IEEE80211_STA_DISABLE_HT;
3592 sdata->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg24398e32012-03-28 10:58:36 +02003593 }
3594 }
3595
Johannes Bergf2d9d272012-11-22 14:11:39 +01003596 ifmgd->flags |= ieee80211_determine_chantype(sdata, sband,
3597 cbss->channel,
3598 ht_oper, vht_oper,
3599 &chandef);
Johannes Berg04ecd252012-09-11 14:34:12 +02003600
Johannes Bergf2d9d272012-11-22 14:11:39 +01003601 sdata->needed_rx_chains = min(ieee80211_ht_vht_rx_chains(sdata, cbss),
3602 local->rx_chains);
Johannes Berg24398e32012-03-28 10:58:36 +02003603
Johannes Berg9caf0362012-11-29 01:25:20 +01003604 rcu_read_unlock();
3605
Johannes Berg04ecd252012-09-11 14:34:12 +02003606 /* will change later if needed */
3607 sdata->smps_mode = IEEE80211_SMPS_OFF;
3608
Johannes Bergf2d9d272012-11-22 14:11:39 +01003609 /*
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);
Johannes Bergd601cd82013-02-07 20:54:51 +01003616 while (ret && chandef.width != NL80211_CHAN_WIDTH_20_NOHT) {
Johannes Bergf2d9d272012-11-22 14:11:39 +01003617 ifmgd->flags |= chandef_downgrade(&chandef);
Johannes Bergd601cd82013-02-07 20:54:51 +01003618 ret = ieee80211_vif_use_channel(sdata, &chandef,
3619 IEEE80211_CHANCTX_SHARED);
3620 }
Johannes Bergf2d9d272012-11-22 14:11:39 +01003621 return ret;
Johannes Bergb17166a2012-07-27 11:41:27 +02003622}
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
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003649 if (new_sta) {
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003650 u32 rates = 0, basic_rates = 0;
3651 bool have_higher_than_11mbit;
3652 int min_rate = INT_MAX, min_rate_index = -1;
Johannes Bergb17166a2012-07-27 11:41:27 +02003653 struct ieee80211_supported_band *sband;
Johannes Berg8cef2c92013-02-05 16:54:31 +01003654 const struct cfg80211_bss_ies *ies;
Johannes Bergb17166a2012-07-27 11:41:27 +02003655
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 }
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003663
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003664 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) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003679 sdata_info(sdata,
3680 "No basic rates, using min rate instead\n");
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003681 basic_rates = BIT(min_rate_index);
3682 }
3683
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003684 new_sta->sta.supp_rates[cbss->channel->band] = rates;
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003685 sdata->vif.bss_conf.basic_rates = basic_rates;
3686
3687 /* cf. IEEE 802.11 9.2.12 */
Johannes Berg55de9082012-07-26 17:24:39 +02003688 if (cbss->channel->band == IEEE80211_BAND_2GHZ &&
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003689 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
Johannes Berg8c358bc2012-05-22 22:13:05 +02003696 /* set timing information */
3697 sdata->vif.bss_conf.beacon_int = cbss->beacon_interval;
Johannes Berg8cef2c92013-02-05 16:54:31 +01003698 rcu_read_lock();
Johannes Bergef429da2013-02-05 17:48:40 +01003699 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 }
Johannes Berg8cef2c92013-02-05 16:54:31 +01003725 rcu_read_unlock();
Johannes Berg8c358bc2012-05-22 22:13:05 +02003726
3727 /* tell driver about BSSID, basic rates and timing */
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003728 ieee80211_bss_info_change_notify(sdata,
Johannes Berg8c358bc2012-05-22 22:13:05 +02003729 BSS_CHANGED_BSSID | BSS_CHANGED_BASIC_RATES |
3730 BSS_CHANGED_BEACON_INT);
Johannes Berga1cf7752012-03-08 15:02:07 +01003731
3732 if (assoc)
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003733 sta_info_pre_move_state(new_sta, IEEE80211_STA_AUTH);
Johannes Berga1cf7752012-03-08 15:02:07 +01003734
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003735 err = sta_info_insert(new_sta);
3736 new_sta = NULL;
Johannes Berga1cf7752012-03-08 15:02:07 +01003737 if (err) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003738 sdata_info(sdata,
3739 "failed to insert STA entry for the AP (error %d)\n",
3740 err);
Johannes Berga1cf7752012-03-08 15:02:07 +01003741 return err;
3742 }
3743 } else
Joe Perchesb203ca32012-05-08 18:56:52 +00003744 WARN_ON_ONCE(!ether_addr_equal(ifmgd->bssid, cbss->bssid));
Johannes Berga1cf7752012-03-08 15:02:07 +01003745
3746 return 0;
3747}
3748
Johannes Berg77fdaa12009-07-07 03:45:17 +02003749/* config hooks */
3750int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
3751 struct cfg80211_auth_request *req)
3752{
Johannes Berg66e67e42012-01-20 13:55:27 +01003753 struct ieee80211_local *local = sdata->local;
3754 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3755 struct ieee80211_mgd_auth_data *auth_data;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003756 u16 auth_alg;
Johannes Berg66e67e42012-01-20 13:55:27 +01003757 int err;
3758
3759 /* prepare auth data structure */
Johannes Berg77fdaa12009-07-07 03:45:17 +02003760
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:
Johannes Berg66e67e42012-01-20 13:55:27 +01003766 if (IS_ERR(local->wep_tx_tfm))
Johannes Berg9dca9c42010-07-21 10:09:25 +02003767 return -EOPNOTSUPP;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003768 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;
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003776 case NL80211_AUTHTYPE_SAE:
3777 auth_alg = WLAN_AUTH_SAE;
3778 break;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003779 default:
3780 return -EOPNOTSUPP;
3781 }
3782
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003783 auth_data = kzalloc(sizeof(*auth_data) + req->sae_data_len +
3784 req->ie_len, GFP_KERNEL);
Johannes Berg66e67e42012-01-20 13:55:27 +01003785 if (!auth_data)
Johannes Berg77fdaa12009-07-07 03:45:17 +02003786 return -ENOMEM;
3787
Johannes Berg66e67e42012-01-20 13:55:27 +01003788 auth_data->bss = req->bss;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003789
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003790 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
Johannes Berg77fdaa12009-07-07 03:45:17 +02003799 if (req->ie && req->ie_len) {
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003800 memcpy(&auth_data->data[auth_data->data_len],
3801 req->ie, req->ie_len);
3802 auth_data->data_len += req->ie_len;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003803 }
3804
Johannes Bergfffd0932009-07-08 14:22:54 +02003805 if (req->key && req->key_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003806 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);
Johannes Bergfffd0932009-07-08 14:22:54 +02003809 }
3810
Johannes Berg66e67e42012-01-20 13:55:27 +01003811 auth_data->algorithm = auth_alg;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003812
Johannes Berg66e67e42012-01-20 13:55:27 +01003813 /* try to authenticate/probe */
Johannes Bergf679f652009-12-23 13:15:34 +01003814
Johannes Berg66e67e42012-01-20 13:55:27 +01003815 mutex_lock(&ifmgd->mtx);
Johannes Berg77fdaa12009-07-07 03:45:17 +02003816
Johannes Berg66e67e42012-01-20 13:55:27 +01003817 if ((ifmgd->auth_data && !ifmgd->auth_data->done) ||
3818 ifmgd->assoc_data) {
3819 err = -EBUSY;
3820 goto err_free;
3821 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003822
Johannes Berg66e67e42012-01-20 13:55:27 +01003823 if (ifmgd->auth_data)
3824 ieee80211_destroy_auth_data(sdata, false);
Guy Eilam2a33bee2011-08-17 15:18:15 +03003825
Johannes Berg66e67e42012-01-20 13:55:27 +01003826 /* prep auth_data so we don't go into idle on disassoc */
3827 ifmgd->auth_data = auth_data;
3828
3829 if (ifmgd->associated)
Johannes Berg37ad3882012-02-24 13:50:54 +01003830 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
Johannes Berg66e67e42012-01-20 13:55:27 +01003831
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003832 sdata_info(sdata, "authenticate with %pM\n", req->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01003833
Johannes Berga1cf7752012-03-08 15:02:07 +01003834 err = ieee80211_prep_connection(sdata, req->bss, false);
3835 if (err)
Johannes Berg66e67e42012-01-20 13:55:27 +01003836 goto err_clear;
Guy Eilam2a33bee2011-08-17 15:18:15 +03003837
Johannes Berg66e67e42012-01-20 13:55:27 +01003838 err = ieee80211_probe_auth(sdata);
3839 if (err) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003840 sta_info_destroy_addr(sdata, req->bss->bssid);
3841 goto err_clear;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003842 }
3843
Johannes Berg66e67e42012-01-20 13:55:27 +01003844 /* hold our own reference */
Johannes Berg5b112d32013-02-01 01:49:58 +01003845 cfg80211_ref_bss(local->hw.wiphy, auth_data->bss);
Johannes Berg66e67e42012-01-20 13:55:27 +01003846 err = 0;
3847 goto out_unlock;
Johannes Berge5b900d2010-07-29 16:08:55 +02003848
Johannes Berg66e67e42012-01-20 13:55:27 +01003849 err_clear:
Eliad Peller3d2abdf2012-09-04 17:44:45 +03003850 memset(ifmgd->bssid, 0, ETH_ALEN);
3851 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg66e67e42012-01-20 13:55:27 +01003852 ifmgd->auth_data = NULL;
3853 err_free:
3854 kfree(auth_data);
3855 out_unlock:
3856 mutex_unlock(&ifmgd->mtx);
Johannes Berge5b900d2010-07-29 16:08:55 +02003857
Johannes Berg66e67e42012-01-20 13:55:27 +01003858 return err;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003859}
3860
Johannes Berg77fdaa12009-07-07 03:45:17 +02003861int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
3862 struct cfg80211_assoc_request *req)
3863{
Johannes Berg66e67e42012-01-20 13:55:27 +01003864 struct ieee80211_local *local = sdata->local;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003865 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01003866 struct ieee80211_bss *bss = (void *)req->bss->priv;
Johannes Berg66e67e42012-01-20 13:55:27 +01003867 struct ieee80211_mgd_assoc_data *assoc_data;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02003868 const struct cfg80211_bss_ies *beacon_ies;
Johannes Berg4e74bfd2012-03-08 15:02:04 +01003869 struct ieee80211_supported_band *sband;
Johannes Bergb08fbbd2012-12-07 13:06:48 +01003870 const u8 *ssidie, *ht_ie, *vht_ie;
Guy Eilam2a33bee2011-08-17 15:18:15 +03003871 int i, err;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003872
Johannes Berg66e67e42012-01-20 13:55:27 +01003873 assoc_data = kzalloc(sizeof(*assoc_data) + req->ie_len, GFP_KERNEL);
3874 if (!assoc_data)
Johannes Bergaf6b6372009-12-23 13:15:35 +01003875 return -ENOMEM;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003876
Johannes Berg9caf0362012-11-29 01:25:20 +01003877 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
Johannes Berg66e67e42012-01-20 13:55:27 +01003888 mutex_lock(&ifmgd->mtx);
3889
3890 if (ifmgd->associated)
Johannes Berg37ad3882012-02-24 13:50:54 +01003891 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
Johannes Berg66e67e42012-01-20 13:55:27 +01003892
3893 if (ifmgd->auth_data && !ifmgd->auth_data->done) {
3894 err = -EBUSY;
3895 goto err_free;
Guy Eilam2a33bee2011-08-17 15:18:15 +03003896 }
3897
Johannes Berg66e67e42012-01-20 13:55:27 +01003898 if (ifmgd->assoc_data) {
3899 err = -EBUSY;
3900 goto err_free;
3901 }
3902
3903 if (ifmgd->auth_data) {
3904 bool match;
3905
3906 /* keep sta info, bssid if matching */
Joe Perchesb203ca32012-05-08 18:56:52 +00003907 match = ether_addr_equal(ifmgd->bssid, req->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01003908 ieee80211_destroy_auth_data(sdata, match);
3909 }
3910
3911 /* prepare assoc data */
Johannes Berg19c3b832012-08-01 20:13:36 +02003912
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03003913 ifmgd->beacon_crc_valid = false;
3914
Johannes Bergde5036a2012-03-08 15:02:03 +01003915 /*
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 */
Johannes Berg1c4cb922012-05-30 15:57:00 +02003922 for (i = 0; i < req->crypto.n_ciphers_pairwise; i++) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02003923 if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
3924 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
Johannes Berg1c4cb922012-05-30 15:57:00 +02003925 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104) {
Johannes Berga8243b72012-11-22 14:32:09 +01003926 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003927 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg1c4cb922012-05-30 15:57:00 +02003928 netdev_info(sdata->dev,
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003929 "disabling HT/VHT due to WEP/TKIP use\n");
Johannes Berg1c4cb922012-05-30 15:57:00 +02003930 }
3931 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003932
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003933 if (req->flags & ASSOC_REQ_DISABLE_HT) {
Johannes Berga8243b72012-11-22 14:32:09 +01003934 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003935 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
3936 }
Ben Greearef96a8422011-11-18 11:32:00 -08003937
Johannes Berg4e74bfd2012-03-08 15:02:04 +01003938 /* 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 ||
Johannes Berg1c4cb922012-05-30 15:57:00 +02003941 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used) {
Johannes Berga8243b72012-11-22 14:32:09 +01003942 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Johannes Berg1b49de22012-07-27 10:29:14 +02003943 if (!bss->wmm_used)
3944 netdev_info(sdata->dev,
3945 "disabling HT as WMM/QoS is not supported by the AP\n");
Johannes Berg1c4cb922012-05-30 15:57:00 +02003946 }
Johannes Berg4e74bfd2012-03-08 15:02:04 +01003947
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003948 /* 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;
Johannes Berg1b49de22012-07-27 10:29:14 +02003952 if (!bss->wmm_used)
3953 netdev_info(sdata->dev,
3954 "disabling VHT as WMM/QoS is not supported by the AP\n");
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003955 }
3956
Ben Greearef96a8422011-11-18 11:32:00 -08003957 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
Johannes Berg77fdaa12009-07-07 03:45:17 +02003961 if (req->ie && req->ie_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003962 memcpy(assoc_data->ie, req->ie, req->ie_len);
3963 assoc_data->ie_len = req->ie_len;
3964 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003965
Johannes Berg66e67e42012-01-20 13:55:27 +01003966 assoc_data->bss = req->bss;
Johannes Bergf679f652009-12-23 13:15:34 +01003967
Johannes Bergaf6b6372009-12-23 13:15:35 +01003968 if (ifmgd->req_smps == IEEE80211_SMPS_AUTOMATIC) {
3969 if (ifmgd->powersave)
Johannes Berg04ecd252012-09-11 14:34:12 +02003970 sdata->smps_mode = IEEE80211_SMPS_DYNAMIC;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003971 else
Johannes Berg04ecd252012-09-11 14:34:12 +02003972 sdata->smps_mode = IEEE80211_SMPS_OFF;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003973 } else
Johannes Berg04ecd252012-09-11 14:34:12 +02003974 sdata->smps_mode = ifmgd->req_smps;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003975
Johannes Berg66e67e42012-01-20 13:55:27 +01003976 assoc_data->capability = req->bss->capability;
Johannes Berg32c50572012-03-28 11:04:29 +02003977 assoc_data->wmm = bss->wmm_used &&
3978 (local->hw.queues >= IEEE80211_NUM_ACS);
Johannes Berg66e67e42012-01-20 13:55:27 +01003979 assoc_data->supp_rates = bss->supp_rates;
3980 assoc_data->supp_rates_len = bss->supp_rates_len;
Johannes Berg9dde6422012-05-16 23:43:19 +02003981
Johannes Berg9caf0362012-11-29 01:25:20 +01003982 rcu_read_lock();
Johannes Berg9dde6422012-05-16 23:43:19 +02003983 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
Johannes Berga8243b72012-11-22 14:32:09 +01003988 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Johannes Bergb08fbbd2012-12-07 13:06:48 +01003989 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;
Johannes Berg9caf0362012-11-29 01:25:20 +01003995 rcu_read_unlock();
Johannes Berg63f170e2009-12-23 13:15:33 +01003996
Kalle Valoab133152010-01-12 10:42:31 +02003997 if (bss->wmm_used && bss->uapsd_supported &&
3998 (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_UAPSD)) {
Johannes Berg76f03032012-03-08 15:02:05 +01003999 assoc_data->uapsd = true;
Kalle Valoab133152010-01-12 10:42:31 +02004000 ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED;
4001 } else {
Johannes Berg76f03032012-03-08 15:02:05 +01004002 assoc_data->uapsd = false;
Kalle Valoab133152010-01-12 10:42:31 +02004003 ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED;
4004 }
4005
Johannes Berg77fdaa12009-07-07 03:45:17 +02004006 if (req->prev_bssid)
Johannes Berg66e67e42012-01-20 13:55:27 +01004007 memcpy(assoc_data->prev_bssid, req->prev_bssid, ETH_ALEN);
Johannes Berg77fdaa12009-07-07 03:45:17 +02004008
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
Johannes Berga621fa42010-08-27 14:26:54 +03004022 sdata->control_port_protocol = req->crypto.control_port_ethertype;
4023 sdata->control_port_no_encrypt = req->crypto.control_port_no_encrypt;
4024
Johannes Berg66e67e42012-01-20 13:55:27 +01004025 /* kick off associate process */
4026
4027 ifmgd->assoc_data = assoc_data;
Johannes Berg826262c2012-12-10 16:38:14 +02004028 ifmgd->dtim_period = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +01004029
Johannes Berga1cf7752012-03-08 15:02:07 +01004030 err = ieee80211_prep_connection(sdata, req->bss, true);
4031 if (err)
4032 goto err_clear;
Johannes Berg66e67e42012-01-20 13:55:27 +01004033
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004034 rcu_read_lock();
4035 beacon_ies = rcu_dereference(req->bss->beacon_ies);
Johannes Berg826262c2012-12-10 16:38:14 +02004036
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004037 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);
Johannes Berg89afe612013-02-13 15:39:57 +01004046 assoc_data->timeout_started = true;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004047 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);
Johannes Bergef429da2013-02-05 17:48:40 +01004052 u8 dtim_count = 0;
4053
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004054 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;
Johannes Bergef429da2013-02-05 17:48:40 +01004058 dtim_count = tim->dtim_count;
Johannes Berg826262c2012-12-10 16:38:14 +02004059 }
Johannes Berg66e67e42012-01-20 13:55:27 +01004060 assoc_data->have_beacon = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01004061 assoc_data->timeout = jiffies;
Johannes Berg89afe612013-02-13 15:39:57 +01004062 assoc_data->timeout_started = true;
Johannes Bergef429da2013-02-05 17:48:40 +01004063
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 }
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004070 } else {
4071 assoc_data->timeout = jiffies;
Johannes Berg89afe612013-02-13 15:39:57 +01004072 assoc_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01004073 }
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004074 rcu_read_unlock();
4075
Johannes Berg66e67e42012-01-20 13:55:27 +01004076 run_again(ifmgd, assoc_data->timeout);
4077
Paul Stewartfcff4f12012-02-23 17:59:53 -08004078 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";
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004088 sdata_info(sdata, "associating with AP with corrupt %s\n",
4089 corrupt_type);
Paul Stewartfcff4f12012-02-23 17:59:53 -08004090 }
4091
Johannes Berg66e67e42012-01-20 13:55:27 +01004092 err = 0;
4093 goto out;
4094 err_clear:
Eliad Peller3d2abdf2012-09-04 17:44:45 +03004095 memset(ifmgd->bssid, 0, ETH_ALEN);
4096 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg66e67e42012-01-20 13:55:27 +01004097 ifmgd->assoc_data = NULL;
4098 err_free:
4099 kfree(assoc_data);
4100 out:
4101 mutex_unlock(&ifmgd->mtx);
4102
4103 return err;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004104}
4105
4106int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
Johannes Berg63c9c5e2012-02-24 13:50:51 +01004107 struct cfg80211_deauth_request *req)
Johannes Berg77fdaa12009-07-07 03:45:17 +02004108{
4109 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02004110 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Stanislaw Gruszka68632552012-10-15 14:52:41 +02004111 bool tx = !req->local_state_change;
Johannes Berg86552012012-10-29 09:46:31 +01004112 bool sent_frame = false;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004113
Johannes Berg77fdaa12009-07-07 03:45:17 +02004114 mutex_lock(&ifmgd->mtx);
4115
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004116 sdata_info(sdata,
4117 "deauthenticating from %pM by local choice (reason=%d)\n",
4118 req->bssid, req->reason_code);
Johannes Berg0ff71612009-09-26 14:45:41 +02004119
Johannes Berg86552012012-10-29 09:46:31 +01004120 if (ifmgd->auth_data) {
Emmanuel Grumbach8c7d8572012-07-25 01:42:36 +03004121 drv_mgd_prepare_tx(sdata->local, sdata);
Johannes Berg37ad3882012-02-24 13:50:54 +01004122 ieee80211_send_deauth_disassoc(sdata, req->bssid,
4123 IEEE80211_STYPE_DEAUTH,
Stanislaw Gruszka68632552012-10-15 14:52:41 +02004124 req->reason_code, tx,
Johannes Berg37ad3882012-02-24 13:50:54 +01004125 frame_buf);
Johannes Berg86552012012-10-29 09:46:31 +01004126 ieee80211_destroy_auth_data(sdata, false);
4127 mutex_unlock(&ifmgd->mtx);
4128
4129 sent_frame = tx;
4130 goto out;
Emmanuel Grumbach8c7d8572012-07-25 01:42:36 +03004131 }
4132
Johannes Berg86552012012-10-29 09:46:31 +01004133 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 }
Johannes Berg37ad3882012-02-24 13:50:54 +01004139 mutex_unlock(&ifmgd->mtx);
4140
Johannes Berg86552012012-10-29 09:46:31 +01004141 out:
Johannes Berg86552012012-10-29 09:46:31 +01004142 if (sent_frame)
4143 __cfg80211_send_deauth(sdata->dev, frame_buf,
4144 IEEE80211_DEAUTH_FRAME_LEN);
4145
Johannes Berg77fdaa12009-07-07 03:45:17 +02004146 return 0;
4147}
4148
4149int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg63c9c5e2012-02-24 13:50:51 +01004150 struct cfg80211_disassoc_request *req)
Johannes Berg77fdaa12009-07-07 03:45:17 +02004151{
4152 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jouni Malinene69e95d2010-03-29 23:29:31 -07004153 u8 bssid[ETH_ALEN];
Antonio Quartulli6ae16772012-09-07 13:28:52 +02004154 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Johannes Berg77fdaa12009-07-07 03:45:17 +02004155
Johannes Berg77fdaa12009-07-07 03:45:17 +02004156 mutex_lock(&ifmgd->mtx);
4157
Johannes Berg8d8b2612009-07-25 11:58:36 +02004158 /*
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 */
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01004164 if (ifmgd->associated != req->bss) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02004165 mutex_unlock(&ifmgd->mtx);
4166 return -ENOLINK;
4167 }
4168
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004169 sdata_info(sdata,
4170 "disassociating from %pM by local choice (reason=%d)\n",
4171 req->bss->bssid, req->reason_code);
Johannes Berg0ff71612009-09-26 14:45:41 +02004172
Jouni Malinene69e95d2010-03-29 23:29:31 -07004173 memcpy(bssid, req->bss->bssid, ETH_ALEN);
Johannes Berg37ad3882012-02-24 13:50:54 +01004174 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DISASSOC,
4175 req->reason_code, !req->local_state_change,
4176 frame_buf);
Johannes Berg77fdaa12009-07-07 03:45:17 +02004177 mutex_unlock(&ifmgd->mtx);
4178
Antonio Quartulli6ae16772012-09-07 13:28:52 +02004179 __cfg80211_send_disassoc(sdata->dev, frame_buf,
4180 IEEE80211_DEAUTH_FRAME_LEN);
Johannes Bergbc83b682009-11-29 12:19:06 +01004181
Johannes Berg77fdaa12009-07-07 03:45:17 +02004182 return 0;
4183}
Jouni Malinen026331c2010-02-15 12:53:10 +02004184
Eliad Pellerafa762f2012-04-23 14:45:15 +03004185void ieee80211_mgd_stop(struct ieee80211_sub_if_data *sdata)
Johannes Berg54e4ffb2012-02-25 21:48:08 +01004186{
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
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02004198void 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
Johannes Bergb5878a22010-04-07 16:48:40 +02004204 trace_api_cqm_rssi_notify(sdata, rssi_event);
4205
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02004206 cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, gfp);
4207}
4208EXPORT_SYMBOL(ieee80211_cqm_rssi_notify);